~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_string.h

  • Committer: Jay Pipes
  • Date: 2008-07-17 18:48:58 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717184858-2mbouxl8xi41gcge
Removed DBUG from CSV and Blackhole storage engines

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