~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
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
 */
1 by brian
clean slate
19
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
20
#ifndef DRIZZLE_SERVER_SQL_STRING_H
21
#define DRIZZLE_SERVER_SQL_STRING_H
22
1 by brian
clean slate
23
/* This file is originally from the mysql distribution. Coded by monty */
24
25
26
#ifndef NOT_FIXED_DEC
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
27
#define NOT_FIXED_DEC			(uint8_t)31
1 by brian
clean slate
28
#endif
29
543 by Monty Taylor
Renamed drizzle_common again. Removed sql_common. (empty)
30
#include <drizzled/common.h>
481.1.16 by Monty Taylor
Merged iocache.h addition.
31
#include <mysys/iocache.h>
677.1.5 by Monty Taylor
Fixed a couple of build issues.
32
#include <stdlib.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
33
#include <string.h>
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
34
1 by brian
clean slate
35
class String;
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
36
37
#if defined(__cplusplus)
38
extern "C" {
39
#endif
40
41
  int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
42
  int stringcmp(const String *a,const String *b);
43
  String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
44
  uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
45
                                   char *to, uint32_t to_length,
46
                                   const CHARSET_INFO * const from_cs,
47
                                   const char *from, uint32_t from_length,
48
                                   uint32_t nchars,
49
                                   const char **well_formed_error_pos,
50
                                   const char **cannot_convert_error_pos,
51
                                   const char **from_end_pos);
52
  size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
53
                                   char *dst, size_t dstlen,
54
                                   const char *src, size_t srclen);
55
56
#if defined(__cplusplus)
57
}
58
#endif
1 by brian
clean slate
59
60
class String
61
{
511.2.4 by Monty Taylor
Fixed warnings before we got to sql_yacc.
62
  char *Ptr;
205 by Brian Aker
uint32 -> uin32_t
63
  uint32_t str_length,Alloced_length;
1 by brian
clean slate
64
  bool alloced;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
65
  const CHARSET_INFO *str_charset;
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
66
1 by brian
clean slate
67
public:
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
68
  String()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
69
  {
70
    Ptr=0; str_length=Alloced_length=0; alloced=0;
71
    str_charset= &my_charset_bin;
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
72
  }
73
  String(uint32_t length_arg)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
74
  {
75
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
76
    str_charset= &my_charset_bin;
77
  }
78
  String(const char *str, const CHARSET_INFO * const cs)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
79
  {
895 by Brian Aker
Completion (?) of uint conversion.
80
    Ptr=(char*) str; str_length=(uint32_t) strlen(str); Alloced_length=0; alloced=0;
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
81
    str_charset=cs;
82
  }
83
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
84
  {
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
85
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
86
    str_charset=cs;
87
  }
88
  String(char *str,uint32_t len, const CHARSET_INFO * const cs)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
89
  {
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
90
    Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
91
    str_charset=cs;
92
  }
93
  String(const String &str)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
94
  {
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
95
    Ptr=str.Ptr ; str_length=str.str_length ;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
96
    Alloced_length=str.Alloced_length; alloced=0;
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
97
    str_charset=str.str_charset;
1 by brian
clean slate
98
  }
99
  static void *operator new(size_t size, MEM_ROOT *mem_root)
1022.4.1 by Monty Taylor
First attempt at supporting gcc-4.4.
100
  { return (void*) alloc_root(mem_root, (uint32_t) size); } 
644 by Brian Aker
Clean up warnings for Solaris.
101
  static void operator delete(void *, size_t)
1 by brian
clean slate
102
  { TRASH(ptr_arg, size); }
644 by Brian Aker
Clean up warnings for Solaris.
103
  static void operator delete(void *, MEM_ROOT *)
1 by brian
clean slate
104
  { /* never called */ }
1022.2.29 by Monty Taylor
Fixed some no-inline warnings.
105
  ~String();
1 by brian
clean slate
106
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
107
  inline void set_charset(const CHARSET_INFO * const charset_arg)
1 by brian
clean slate
108
  { str_charset= charset_arg; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
109
  inline const CHARSET_INFO *charset() const { return str_charset; }
205 by Brian Aker
uint32 -> uin32_t
110
  inline uint32_t length() const { return str_length;}
111
  inline uint32_t alloced_length() const { return Alloced_length;}
511.2.6 by Monty Taylor
drizzled/ and storage/archive/ are clean.
112
  inline char& operator [] (uint32_t i) const { return Ptr[i]; }
205 by Brian Aker
uint32 -> uin32_t
113
  inline void length(uint32_t len) { str_length=len ; }
1 by brian
clean slate
114
  inline bool is_empty() { return (str_length == 0); }
115
  inline void mark_as_const() { Alloced_length= 0;}
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
116
  inline char *ptr() { return Ptr; }
1 by brian
clean slate
117
  inline const char *ptr() const { return Ptr; }
118
  inline char *c_ptr()
119
  {
1022.5.1 by nlw
Fix buffer overrun in string::c_ptr, now handles all cases properly
120
    if (str_length == Alloced_length)
1 by brian
clean slate
121
      (void) realloc(str_length);
1022.5.1 by nlw
Fix buffer overrun in string::c_ptr, now handles all cases properly
122
    else
123
      Ptr[str_length]= 0;
124
    
1 by brian
clean slate
125
    return Ptr;
126
  }
127
  inline char *c_ptr_quick()
128
  {
129
    if (Ptr && str_length < Alloced_length)
130
      Ptr[str_length]=0;
131
    return Ptr;
132
  }
133
  inline char *c_ptr_safe()
134
  {
135
    if (Ptr && str_length < Alloced_length)
136
      Ptr[str_length]=0;
137
    else
138
      (void) realloc(str_length);
139
    return Ptr;
140
  }
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
141
  void append_identifier(const char *name, uint32_t length);
1 by brian
clean slate
142
205 by Brian Aker
uint32 -> uin32_t
143
  void set(String &str,uint32_t offset,uint32_t arg_length)
1 by brian
clean slate
144
  {
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
145
    assert(&str != this);
1 by brian
clean slate
146
    free();
147
    Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
148
    if (str.Alloced_length)
149
      Alloced_length=str.Alloced_length-offset;
150
    else
151
      Alloced_length=0;
152
    str_charset=str.str_charset;
153
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
154
  inline void set(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
155
  {
156
    free();
157
    Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
158
    str_charset=cs;
159
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
160
  inline void set(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
161
  {
162
    free();
163
    Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
164
    str_charset=cs;
165
  }
205 by Brian Aker
uint32 -> uin32_t
166
  bool set_ascii(const char *str, uint32_t arg_length);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
167
  inline void set_quick(char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
1 by brian
clean slate
168
  {
169
    if (!alloced)
170
    {
171
      Ptr=(char*) str; str_length=Alloced_length=arg_length;
172
    }
173
    str_charset=cs;
174
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
175
  bool set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs);
176
  bool set(int64_t num, const CHARSET_INFO * const cs)
1 by brian
clean slate
177
  { return set_int(num, false, cs); }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
178
  bool set(uint64_t num, const CHARSET_INFO * const cs)
152 by Brian Aker
longlong replacement
179
  { return set_int((int64_t)num, true, cs); }
482 by Brian Aker
Remove uint.
180
  bool set_real(double num,uint32_t decimals, const CHARSET_INFO * const cs);
1 by brian
clean slate
181
182
  /*
183
    PMG 2004.11.12
184
    This is a method that works the same as perl's "chop". It simply
185
    drops the last character of a string. This is useful in the case
186
    of the federated storage handler where I'm building a unknown
187
    number, list of values and fields to be used in a sql insert
188
    statement to be run on the remote server, and have a comma after each.
189
    When the list is complete, I "chop" off the trailing comma
190
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
191
    ex.
192
      String stringobj;
1 by brian
clean slate
193
      stringobj.append("VALUES ('foo', 'fi', 'fo',");
194
      stringobj.chop();
195
      stringobj.append(")");
196
197
    In this case, the value of string was:
198
199
    VALUES ('foo', 'fi', 'fo',
200
    VALUES ('foo', 'fi', 'fo'
201
    VALUES ('foo', 'fi', 'fo')
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
202
1 by brian
clean slate
203
  */
204
  inline void chop()
205
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
206
    Ptr[str_length--]= '\0';
1 by brian
clean slate
207
  }
208
209
  inline void free()
210
  {
211
    if (alloced)
212
    {
213
      alloced=0;
214
      Alloced_length=0;
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
215
      ::free(Ptr);
1 by brian
clean slate
216
      Ptr=0;
217
      str_length=0;				/* Safety */
218
    }
219
  }
205 by Brian Aker
uint32 -> uin32_t
220
  inline bool alloc(uint32_t arg_length)
1 by brian
clean slate
221
  {
222
    if (arg_length < Alloced_length)
223
      return 0;
224
    return real_alloc(arg_length);
225
  }
205 by Brian Aker
uint32 -> uin32_t
226
  bool real_alloc(uint32_t arg_length);			// Empties old string
227
  bool realloc(uint32_t arg_length);
228
  inline void shrink(uint32_t arg_length)		// Shrink buffer
1 by brian
clean slate
229
  {
230
    if (arg_length < Alloced_length)
231
    {
232
      char *new_ptr;
656.1.26 by Monty Taylor
Finally removed all of the my_malloc stuff.
233
      if (!(new_ptr=(char*) ::realloc(Ptr,arg_length)))
1 by brian
clean slate
234
      {
235
	Alloced_length = 0;
236
	real_alloc(arg_length);
237
      }
238
      else
239
      {
240
	Ptr=new_ptr;
241
	Alloced_length=arg_length;
242
      }
243
    }
244
  }
245
  bool is_alloced() { return alloced; }
246
  inline String& operator = (const String &s)
247
  {
248
    if (&s != this)
249
    {
250
      /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
251
        It is forbidden to do assignments like
1 by brian
clean slate
252
        some_string = substring_of_that_string
253
       */
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
254
      assert(!s.uses_buffer_owned_by(this));
1 by brian
clean slate
255
      free();
256
      Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
257
      alloced=0;
258
    }
259
    return *this;
260
  }
261
262
  bool copy();					// Alloc string if not alloced
263
  bool copy(const String &s);			// Allocate new string
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
264
  bool copy(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);	// Allocate new string
205 by Brian Aker
uint32 -> uin32_t
265
  static bool needs_conversion(uint32_t arg_length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
266
  			       const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
205 by Brian Aker
uint32 -> uin32_t
267
			       uint32_t *offset);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
268
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
269
  bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
482 by Brian Aker
Remove uint.
270
	    const CHARSET_INFO * const csto, uint32_t *errors);
1 by brian
clean slate
271
  bool append(const String &s);
272
  bool append(const char *s);
205 by Brian Aker
uint32 -> uin32_t
273
  bool append(const char *s,uint32_t arg_length);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
274
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
275
  bool append_with_prefill(const char *s, uint32_t arg_length,
205 by Brian Aker
uint32 -> uin32_t
276
			   uint32_t full_length, char fill_char);
277
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
278
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
279
  bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
280
  bool replace(uint32_t offset,uint32_t arg_length,const String &to);
1 by brian
clean slate
281
  inline bool append(char chr)
282
  {
283
    if (str_length < Alloced_length)
284
    {
285
      Ptr[str_length++]=chr;
286
    }
287
    else
288
    {
289
      if (realloc(str_length+1))
290
	return 1;
291
      Ptr[str_length++]=chr;
292
    }
293
    return 0;
294
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
295
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
1 by brian
clean slate
296
  friend int stringcmp(const String *a,const String *b);
205 by Brian Aker
uint32 -> uin32_t
297
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
298
  uint32_t numchars();
299
  int charpos(int i,uint32_t offset=0);
1 by brian
clean slate
300
205 by Brian Aker
uint32 -> uin32_t
301
  int reserve(uint32_t space_needed)
1 by brian
clean slate
302
  {
303
    return realloc(str_length + space_needed);
304
  }
205 by Brian Aker
uint32 -> uin32_t
305
  int reserve(uint32_t space_needed, uint32_t grow_by);
1 by brian
clean slate
306
307
  /*
308
    The following append operations do NOT check alloced memory
309
    q_*** methods writes values of parameters itself
310
    qs_*** methods writes string representation of value
311
  */
312
  void q_append(const char c)
313
  {
314
    Ptr[str_length++] = c;
315
  }
205 by Brian Aker
uint32 -> uin32_t
316
  void q_append(const uint32_t n)
1 by brian
clean slate
317
  {
318
    int4store(Ptr + str_length, n);
319
    str_length += 4;
320
  }
321
  void q_append(double d)
322
  {
323
    float8store(Ptr + str_length, d);
324
    str_length += 8;
325
  }
326
  void q_append(double *d)
327
  {
328
    float8store(Ptr + str_length, *d);
329
    str_length += 8;
330
  }
205 by Brian Aker
uint32 -> uin32_t
331
  void q_append(const char *data, uint32_t data_len)
1 by brian
clean slate
332
  {
333
    memcpy(Ptr + str_length, data, data_len);
334
    str_length += data_len;
335
  }
336
205 by Brian Aker
uint32 -> uin32_t
337
  void write_at_position(int position, uint32_t value)
1 by brian
clean slate
338
  {
339
    int4store(Ptr + position,value);
340
  }
341
342
  /* Inline (general) functions used by the protocol functions */
343
205 by Brian Aker
uint32 -> uin32_t
344
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
345
  {
205 by Brian Aker
uint32 -> uin32_t
346
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
347
    if (new_length > Alloced_length)
348
    {
349
      if (realloc(new_length + step_alloc))
350
        return 0;
351
    }
205 by Brian Aker
uint32 -> uin32_t
352
    uint32_t old_length= str_length;
1 by brian
clean slate
353
    str_length+= arg_length;
354
    return Ptr+ old_length;			/* Area to use */
355
  }
356
205 by Brian Aker
uint32 -> uin32_t
357
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
358
  {
205 by Brian Aker
uint32 -> uin32_t
359
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
360
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
163 by Brian Aker
Merge Monty's code.
361
      return true;
1 by brian
clean slate
362
    memcpy(Ptr+str_length, s, arg_length);
363
    str_length+= arg_length;
163 by Brian Aker
Merge Monty's code.
364
    return false;
1 by brian
clean slate
365
  }
366
  void print(String *print);
367
368
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
369
  void swap(String &s);
370
371
  inline bool uses_buffer_owned_by(const String *s) const
372
  {
373
    return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
374
  }
375
};
376
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
377
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
1 by brian
clean slate
378
                                           char *end)
379
{
380
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
381
}
382
598.1.1 by Super-User
Fixed solaris build crap.
383
extern "C++" {
384
bool operator==(const String &s1, const String &s2);
385
bool operator!=(const String &s1, const String &s2);
602 by Brian Aker
Merge in Monty's work for Solaris Sun Studio
386
}
1 by brian
clean slate
387
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
388
#endif /* DRIZZLE_SERVER_SQL_STRING_H */