~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Jay Pipes
  • Date: 2008-09-11 16:03:22 UTC
  • mto: (383.5.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 386.
  • Revision ID: jay@mysql.com-20080911160322-vrl0k1djo6q6ytv1
Removed SQL_MODE variances from comment_table.test and ensured correct error thrown when a comment that is too long was input.  After moving to proto buffer definition for table, the 2048 length will go away.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#ifndef DRIZZLED_SQL_STRING_H
21
 
#define DRIZZLED_SQL_STRING_H
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef DRIZZLE_SERVER_SQL_STRING_H
 
17
#define DRIZZLE_SERVER_SQL_STRING_H
22
18
 
23
19
/* This file is originally from the mysql distribution. Coded by monty */
24
20
 
25
 
#include <drizzled/common.h>
26
 
#include <cassert>
27
 
#include <cstdlib>
28
 
#include <cstring>
29
 
#include <string>
 
21
#ifdef USE_PRAGMA_INTERFACE
 
22
#pragma interface                       /* gcc class implementation */
 
23
#endif
30
24
 
31
25
#ifndef NOT_FIXED_DEC
32
 
#define NOT_FIXED_DEC                   (uint8_t)31
 
26
#define NOT_FIXED_DEC                   31
33
27
#endif
34
28
 
35
 
namespace drizzled
36
 
{
37
 
 
38
29
class String;
39
 
 
40
 
extern String my_empty_string;
41
 
extern const String my_null_string;
42
 
namespace memory { class Root; }
43
 
typedef struct charset_info_st CHARSET_INFO;
44
 
 
45
 
std::string String_to_std_string(String const& s);
46
 
String* set_String_from_std_string(String* s, std::string const& cs);
47
 
 
48
30
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
49
 
int stringcmp(const String *a,const String *b);
50
 
String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
51
 
size_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
52
 
                                 char *to, size_t to_length,
53
 
                                 const CHARSET_INFO * const from_cs,
54
 
                                 const char *from, size_t from_length,
55
 
                                 size_t nchars,
56
 
                                 const char **well_formed_error_pos,
57
 
                                 const char **cannot_convert_error_pos,
58
 
                                 const char **from_end_pos);
59
 
 
 
31
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
32
uint32_t copy_and_convert(char *to, uint32_t to_length, const CHARSET_INFO * const to_cs,
 
33
                        const char *from, uint32_t from_length,
 
34
                        const CHARSET_INFO * const from_cs, uint *errors);
 
35
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
 
36
                               char *to, uint to_length,
 
37
                               const CHARSET_INFO * const from_cs,
 
38
                               const char *from, uint from_length,
 
39
                               uint nchars,
 
40
                               const char **well_formed_error_pos,
 
41
                               const char **cannot_convert_error_pos,
 
42
                               const char **from_end_pos);
 
43
size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
 
44
                                 char *dst, size_t dstlen,
 
45
                                 const char *src, size_t srclen);
60
46
 
61
47
class String
62
48
{
63
49
  char *Ptr;
64
 
  size_t str_length,Alloced_length;
 
50
  uint32_t str_length,Alloced_length;
65
51
  bool alloced;
66
52
  const CHARSET_INFO *str_charset;
67
 
 
68
53
public:
69
 
  String();
70
 
  String(size_t length_arg);
71
 
  String(const char *str, const CHARSET_INFO * const cs);
72
 
  String(const char *str, size_t len, const CHARSET_INFO * const cs);
73
 
  String(char *str, size_t len, const CHARSET_INFO * const cs);
74
 
  String(const String &str);
75
 
 
76
 
  static void *operator new(size_t size, memory::Root *mem_root);
77
 
  static void operator delete(void *, size_t)
78
 
  { }
79
 
  static void operator delete(void *, memory::Root *)
80
 
  { }
81
 
  ~String();
 
54
  String()
 
55
  { 
 
56
    Ptr=0; str_length=Alloced_length=0; alloced=0; 
 
57
    str_charset= &my_charset_bin; 
 
58
  }
 
59
  String(uint32_t length_arg)
 
60
  { 
 
61
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg); 
 
62
    str_charset= &my_charset_bin;
 
63
  }
 
64
  String(const char *str, const CHARSET_INFO * const cs)
 
65
  { 
 
66
    Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
 
67
    str_charset=cs;
 
68
  }
 
69
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
 
70
  { 
 
71
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
 
72
    str_charset=cs;
 
73
  }
 
74
  String(char *str,uint32_t len, const CHARSET_INFO * const cs)
 
75
  { 
 
76
    Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
 
77
    str_charset=cs;
 
78
  }
 
79
  String(const String &str)
 
80
  { 
 
81
    Ptr=str.Ptr ; str_length=str.str_length ;
 
82
    Alloced_length=str.Alloced_length; alloced=0; 
 
83
    str_charset=str.str_charset;
 
84
  }
 
85
  static void *operator new(size_t size, MEM_ROOT *mem_root)
 
86
  { return (void*) alloc_root(mem_root, (uint) size); }
 
87
  static void operator delete(void *ptr_arg __attribute__((unused)),
 
88
                              size_t size __attribute__((unused)))
 
89
  { TRASH(ptr_arg, size); }
 
90
  static void operator delete(void *ptr_arg __attribute__((unused)),
 
91
                              MEM_ROOT *mem_root __attribute__((unused)))
 
92
  { /* never called */ }
 
93
  ~String() { free(); }
82
94
 
83
95
  inline void set_charset(const CHARSET_INFO * const charset_arg)
84
96
  { str_charset= charset_arg; }
85
97
  inline const CHARSET_INFO *charset() const { return str_charset; }
86
 
  inline size_t length() const { return str_length;}
87
 
  inline size_t alloced_length() const { return Alloced_length;}
88
 
  inline char& operator [] (size_t i) const { return Ptr[i]; }
89
 
  inline void length(size_t len) { str_length=len ; }
 
98
  inline uint32_t length() const { return str_length;}
 
99
  inline uint32_t alloced_length() const { return Alloced_length;}
 
100
  inline char& operator [] (uint32_t i) const { return Ptr[i]; }
 
101
  inline void length(uint32_t len) { str_length=len ; }
90
102
  inline bool is_empty() { return (str_length == 0); }
91
103
  inline void mark_as_const() { Alloced_length= 0;}
92
104
  inline char *ptr() { return Ptr; }
93
105
  inline const char *ptr() const { return Ptr; }
94
106
  inline char *c_ptr()
95
107
  {
96
 
    if (str_length == Alloced_length)
 
108
    if (!Ptr || Ptr[str_length])                /* Should be safe */
97
109
      (void) realloc(str_length);
98
 
    else
99
 
      Ptr[str_length]= 0;
100
 
 
101
110
    return Ptr;
102
111
  }
103
112
  inline char *c_ptr_quick()
114
123
      (void) realloc(str_length);
115
124
    return Ptr;
116
125
  }
117
 
  inline char *c_str()
118
 
  {
119
 
    if (Ptr && str_length < Alloced_length)
120
 
      Ptr[str_length]=0;
121
 
    else
122
 
      (void) realloc(str_length);
123
 
    return Ptr;
124
 
  }
125
 
  void append_identifier(const char *name, size_t length);
126
126
 
127
 
  void set(String &str,size_t offset,size_t arg_length)
 
127
  void set(String &str,uint32_t offset,uint32_t arg_length)
128
128
  {
129
129
    assert(&str != this);
130
130
    free();
131
 
    Ptr= str.ptr()+offset; str_length=arg_length; alloced=0;
 
131
    Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
132
132
    if (str.Alloced_length)
133
133
      Alloced_length=str.Alloced_length-offset;
134
134
    else
135
135
      Alloced_length=0;
136
136
    str_charset=str.str_charset;
137
137
  }
138
 
  inline void set(char *str,size_t arg_length, const CHARSET_INFO * const cs)
139
 
  {
140
 
    free();
141
 
    Ptr= str; str_length=Alloced_length=arg_length ; alloced=0;
142
 
    str_charset=cs;
143
 
  }
144
 
  inline void set(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
145
 
  {
146
 
    free();
147
 
    Ptr= const_cast<char*>(str);
148
 
    str_length=arg_length; Alloced_length=0 ; alloced=0;
149
 
    str_charset=cs;
150
 
  }
151
 
  bool set_ascii(const char *str, size_t arg_length);
152
 
  inline void set_quick(char *str,size_t arg_length, const CHARSET_INFO * const cs)
 
138
  inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
 
139
  {
 
140
    free();
 
141
    Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
 
142
    str_charset=cs;
 
143
  }
 
144
  inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
 
145
  {
 
146
    free();
 
147
    Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
 
148
    str_charset=cs;
 
149
  }
 
150
  bool set_ascii(const char *str, uint32_t arg_length);
 
151
  inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
153
152
  {
154
153
    if (!alloced)
155
154
    {
156
 
      Ptr= str; str_length= Alloced_length= arg_length;
 
155
      Ptr=(char*) str; str_length=Alloced_length=arg_length;
157
156
    }
158
 
    str_charset= cs;
 
157
    str_charset=cs;
159
158
  }
160
159
  bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
161
160
  bool set(int64_t num, const CHARSET_INFO * const cs)
162
161
  { return set_int(num, false, cs); }
163
162
  bool set(uint64_t num, const CHARSET_INFO * const cs)
164
 
  { return set_int(static_cast<int64_t>(num), true, cs); }
165
 
  bool set_real(double num,size_t decimals, const CHARSET_INFO * const cs);
 
163
  { return set_int((int64_t)num, true, cs); }
 
164
  bool set_real(double num,uint decimals, const CHARSET_INFO * const cs);
166
165
 
167
166
  /*
168
167
    PMG 2004.11.12
173
172
    statement to be run on the remote server, and have a comma after each.
174
173
    When the list is complete, I "chop" off the trailing comma
175
174
 
176
 
    ex.
177
 
      String stringobj;
 
175
    ex. 
 
176
      String stringobj; 
178
177
      stringobj.append("VALUES ('foo', 'fi', 'fo',");
179
178
      stringobj.chop();
180
179
      stringobj.append(")");
184
183
    VALUES ('foo', 'fi', 'fo',
185
184
    VALUES ('foo', 'fi', 'fo'
186
185
    VALUES ('foo', 'fi', 'fo')
187
 
 
 
186
      
188
187
  */
189
188
  inline void chop()
190
189
  {
191
 
    Ptr[str_length--]= '\0';
 
190
    Ptr[str_length--]= '\0'; 
192
191
  }
193
192
 
194
193
  inline void free()
197
196
    {
198
197
      alloced=0;
199
198
      Alloced_length=0;
200
 
      ::free(Ptr);
 
199
      my_free(Ptr,MYF(0));
201
200
      Ptr=0;
202
201
      str_length=0;                             /* Safety */
203
202
    }
204
203
  }
205
 
  inline bool alloc(size_t arg_length)
 
204
  inline bool alloc(uint32_t arg_length)
206
205
  {
207
206
    if (arg_length < Alloced_length)
208
207
      return 0;
209
208
    return real_alloc(arg_length);
210
209
  }
211
 
  bool real_alloc(size_t arg_length);                   // Empties old string
212
 
  bool realloc(size_t arg_length);
213
 
  inline void shrink(size_t arg_length)         // Shrink buffer
 
210
  bool real_alloc(uint32_t arg_length);                 // Empties old string
 
211
  bool realloc(uint32_t arg_length);
 
212
  inline void shrink(uint32_t arg_length)               // Shrink buffer
214
213
  {
215
214
    if (arg_length < Alloced_length)
216
215
    {
217
216
      char *new_ptr;
218
 
      if (!(new_ptr= reinterpret_cast<char*>(::realloc(Ptr,arg_length))))
 
217
      if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
219
218
      {
220
219
        Alloced_length = 0;
221
220
        real_alloc(arg_length);
233
232
    if (&s != this)
234
233
    {
235
234
      /*
236
 
        It is forbidden to do assignments like
 
235
        It is forbidden to do assignments like 
237
236
        some_string = substring_of_that_string
238
237
       */
239
238
      assert(!s.uses_buffer_owned_by(this));
246
245
 
247
246
  bool copy();                                  // Alloc string if not alloced
248
247
  bool copy(const String &s);                   // Allocate new string
249
 
  bool copy(const char *s,size_t arg_length, const CHARSET_INFO * const cs);    // Allocate new string
250
 
  static bool needs_conversion(size_t arg_length,
 
248
  bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);  // Allocate new string
 
249
  static bool needs_conversion(uint32_t arg_length,
251
250
                               const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
252
 
                               size_t *offset);
253
 
  bool set_or_copy_aligned(const char *s, size_t arg_length, const CHARSET_INFO * const cs);
254
 
  bool copy(const char*s,size_t arg_length, const CHARSET_INFO * const csfrom,
255
 
            const CHARSET_INFO * const csto, size_t *errors);
 
251
                               uint32_t *offset);
 
252
  bool copy_aligned(const char *s, uint32_t arg_length, uint32_t offset,
 
253
                    const CHARSET_INFO * const cs);
 
254
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
 
255
  bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
 
256
            const CHARSET_INFO * const csto, uint *errors);
256
257
  bool append(const String &s);
257
258
  bool append(const char *s);
258
 
  bool append(const char *s,size_t arg_length);
259
 
  bool append(const char *s,size_t arg_length, const CHARSET_INFO * const cs);
260
 
  bool append_with_prefill(const char *s, size_t arg_length,
261
 
                           size_t full_length, char fill_char);
262
 
  int strstr(const String &search,size_t offset=0); // Returns offset to substring or -1
263
 
  int strrstr(const String &search,size_t offset=0); // Returns offset to substring or -1
264
 
  bool replace(size_t offset,size_t arg_length,const char *to,size_t length);
265
 
  bool replace(size_t offset,size_t arg_length,const String &to);
 
259
  bool append(const char *s,uint32_t arg_length);
 
260
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
 
261
  bool append(IO_CACHE* file, uint32_t arg_length);
 
262
  bool append_with_prefill(const char *s, uint32_t arg_length, 
 
263
                           uint32_t full_length, char fill_char);
 
264
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
 
265
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
 
266
  bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
 
267
  bool replace(uint32_t offset,uint32_t arg_length,const String &to);
266
268
  inline bool append(char chr)
267
269
  {
268
270
    if (str_length < Alloced_length)
277
279
    }
278
280
    return 0;
279
281
  }
 
282
  bool fill(uint32_t max_length,char fill);
280
283
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
281
284
  friend int stringcmp(const String *a,const String *b);
282
 
  friend String *copy_if_not_alloced(String *a,String *b,size_t arg_length);
283
 
  size_t numchars();
284
 
  int charpos(int i,size_t offset=0);
 
285
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
286
  uint32_t numchars();
 
287
  int charpos(int i,uint32_t offset=0);
285
288
 
286
 
  int reserve(size_t space_needed)
 
289
  int reserve(uint32_t space_needed)
287
290
  {
288
291
    return realloc(str_length + space_needed);
289
292
  }
290
 
  int reserve(size_t space_needed, size_t grow_by);
 
293
  int reserve(uint32_t space_needed, uint32_t grow_by);
291
294
 
292
295
  /*
293
296
    The following append operations do NOT check alloced memory
294
297
    q_*** methods writes values of parameters itself
295
298
    qs_*** methods writes string representation of value
296
299
  */
297
 
  void q_append(const char c);
298
 
  void q_append(const size_t n);
299
 
  void q_append(double d);
300
 
  void q_append(double *d);
301
 
  void q_append(const char *data, size_t data_len);
302
 
  void write_at_position(int position, size_t value);
 
300
  void q_append(const char c)
 
301
  {
 
302
    Ptr[str_length++] = c;
 
303
  }
 
304
  void q_append(const uint32_t n)
 
305
  {
 
306
    int4store(Ptr + str_length, n);
 
307
    str_length += 4;
 
308
  }
 
309
  void q_append(double d)
 
310
  {
 
311
    float8store(Ptr + str_length, d);
 
312
    str_length += 8;
 
313
  }
 
314
  void q_append(double *d)
 
315
  {
 
316
    float8store(Ptr + str_length, *d);
 
317
    str_length += 8;
 
318
  }
 
319
  void q_append(const char *data, uint32_t data_len)
 
320
  {
 
321
    memcpy(Ptr + str_length, data, data_len);
 
322
    str_length += data_len;
 
323
  }
 
324
 
 
325
  void write_at_position(int position, uint32_t value)
 
326
  {
 
327
    int4store(Ptr + position,value);
 
328
  }
 
329
 
 
330
  void qs_append(const char *str, uint32_t len);
 
331
  void qs_append(double d);
 
332
  void qs_append(double *d);
 
333
  inline void qs_append(const char c)
 
334
  {
 
335
     Ptr[str_length]= c;
 
336
     str_length++;
 
337
  }
 
338
  void qs_append(int i);
 
339
  void qs_append(uint i);
303
340
 
304
341
  /* Inline (general) functions used by the protocol functions */
305
342
 
306
 
  inline char *prep_append(size_t arg_length, size_t step_alloc)
 
343
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
307
344
  {
308
 
    size_t new_length= arg_length + str_length;
 
345
    uint32_t new_length= arg_length + str_length;
309
346
    if (new_length > Alloced_length)
310
347
    {
311
348
      if (realloc(new_length + step_alloc))
312
349
        return 0;
313
350
    }
314
 
    size_t old_length= str_length;
 
351
    uint32_t old_length= str_length;
315
352
    str_length+= arg_length;
316
353
    return Ptr+ old_length;                     /* Area to use */
317
354
  }
318
355
 
319
 
  inline bool append(const char *s, size_t arg_length, size_t step_alloc)
 
356
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
320
357
  {
321
 
    size_t new_length= arg_length + str_length;
 
358
    uint32_t new_length= arg_length + str_length;
322
359
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
323
360
      return true;
324
361
    memcpy(Ptr+str_length, s, arg_length);
336
373
  }
337
374
};
338
375
 
339
 
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
340
 
                             char *end);
341
 
 
342
 
} /* namespace drizzled */
343
 
 
344
 
bool operator==(const drizzled::String &s1, const drizzled::String &s2);
345
 
bool operator!=(const drizzled::String &s1, const drizzled::String &s2);
346
 
 
347
 
 
348
 
#endif /* DRIZZLED_SQL_STRING_H */
 
376
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str, 
 
377
                                           char *end)
 
378
{
 
379
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
 
380
}
 
381
 
 
382
inline
 
383
bool operator==(const String &s1, const String &s2)
 
384
{
 
385
  return stringcmp(&s1,&s2) == 0;
 
386
}
 
387
 
 
388
inline
 
389
bool operator!=(const String &s1, const String &s2)
 
390
{
 
391
  return !(s1 == s2);
 
392
}
 
393
 
 
394
#endif /* DRIZZLE_SERVER_SQL_STRING_H */