~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Monty Taylor
  • Date: 2008-11-16 05:36:13 UTC
  • mto: (584.1.9 devel)
  • mto: This revision was merged to the branch mainline in revision 589.
  • Revision ID: monty@inaugust.com-20081116053613-bld4rqxhlkb49c02
Split out cache_row and type_holder.

Show diffs side-by-side

added added

removed removed

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