~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 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
  {
895 by Brian Aker
Completion (?) of uint conversion.
85
    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.
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)
895 by Brian Aker
Completion (?) of uint conversion.
105
  { return (void*) alloc_root(mem_root, (uint32_t) 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);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
270
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
271
  bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
482 by Brian Aker
Remove uint.
272
	    const CHARSET_INFO * const csto, uint32_t *errors);
1 by brian
clean slate
273
  bool append(const String &s);
274
  bool append(const char *s);
205 by Brian Aker
uint32 -> uin32_t
275
  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.
276
  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:
277
  bool append_with_prefill(const char *s, uint32_t arg_length,
205 by Brian Aker
uint32 -> uin32_t
278
			   uint32_t full_length, char fill_char);
279
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
280
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
281
  bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
282
  bool replace(uint32_t offset,uint32_t arg_length,const String &to);
1 by brian
clean slate
283
  inline bool append(char chr)
284
  {
285
    if (str_length < Alloced_length)
286
    {
287
      Ptr[str_length++]=chr;
288
    }
289
    else
290
    {
291
      if (realloc(str_length+1))
292
	return 1;
293
      Ptr[str_length++]=chr;
294
    }
295
    return 0;
296
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
297
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
1 by brian
clean slate
298
  friend int stringcmp(const String *a,const String *b);
205 by Brian Aker
uint32 -> uin32_t
299
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
300
  uint32_t numchars();
301
  int charpos(int i,uint32_t offset=0);
1 by brian
clean slate
302
205 by Brian Aker
uint32 -> uin32_t
303
  int reserve(uint32_t space_needed)
1 by brian
clean slate
304
  {
305
    return realloc(str_length + space_needed);
306
  }
205 by Brian Aker
uint32 -> uin32_t
307
  int reserve(uint32_t space_needed, uint32_t grow_by);
1 by brian
clean slate
308
309
  /*
310
    The following append operations do NOT check alloced memory
311
    q_*** methods writes values of parameters itself
312
    qs_*** methods writes string representation of value
313
  */
314
  void q_append(const char c)
315
  {
316
    Ptr[str_length++] = c;
317
  }
205 by Brian Aker
uint32 -> uin32_t
318
  void q_append(const uint32_t n)
1 by brian
clean slate
319
  {
320
    int4store(Ptr + str_length, n);
321
    str_length += 4;
322
  }
323
  void q_append(double d)
324
  {
325
    float8store(Ptr + str_length, d);
326
    str_length += 8;
327
  }
328
  void q_append(double *d)
329
  {
330
    float8store(Ptr + str_length, *d);
331
    str_length += 8;
332
  }
205 by Brian Aker
uint32 -> uin32_t
333
  void q_append(const char *data, uint32_t data_len)
1 by brian
clean slate
334
  {
335
    memcpy(Ptr + str_length, data, data_len);
336
    str_length += data_len;
337
  }
338
205 by Brian Aker
uint32 -> uin32_t
339
  void write_at_position(int position, uint32_t value)
1 by brian
clean slate
340
  {
341
    int4store(Ptr + position,value);
342
  }
343
344
  /* Inline (general) functions used by the protocol functions */
345
205 by Brian Aker
uint32 -> uin32_t
346
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
347
  {
205 by Brian Aker
uint32 -> uin32_t
348
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
349
    if (new_length > Alloced_length)
350
    {
351
      if (realloc(new_length + step_alloc))
352
        return 0;
353
    }
205 by Brian Aker
uint32 -> uin32_t
354
    uint32_t old_length= str_length;
1 by brian
clean slate
355
    str_length+= arg_length;
356
    return Ptr+ old_length;			/* Area to use */
357
  }
358
205 by Brian Aker
uint32 -> uin32_t
359
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
360
  {
205 by Brian Aker
uint32 -> uin32_t
361
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
362
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
163 by Brian Aker
Merge Monty's code.
363
      return true;
1 by brian
clean slate
364
    memcpy(Ptr+str_length, s, arg_length);
365
    str_length+= arg_length;
163 by Brian Aker
Merge Monty's code.
366
    return false;
1 by brian
clean slate
367
  }
368
  void print(String *print);
369
370
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
371
  void swap(String &s);
372
373
  inline bool uses_buffer_owned_by(const String *s) const
374
  {
375
    return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
376
  }
377
};
378
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
379
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
1 by brian
clean slate
380
                                           char *end)
381
{
382
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
383
}
384
598.1.1 by Super-User
Fixed solaris build crap.
385
extern "C++" {
386
bool operator==(const String &s1, const String &s2);
387
bool operator!=(const String &s1, const String &s2);
602 by Brian Aker
Merge in Monty's work for Solaris Sun Studio
388
}
1 by brian
clean slate
389
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
390
#endif /* DRIZZLE_SERVER_SQL_STRING_H */