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