~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

Moved base64.h to mysys.

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