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