~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;
1 by brian
clean slate
71
public:
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
72
  String()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
73
  {
74
    Ptr=0; str_length=Alloced_length=0; alloced=0;
75
    str_charset= &my_charset_bin;
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
76
  }
77
  String(uint32_t length_arg)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
78
  {
79
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
80
    str_charset= &my_charset_bin;
81
  }
82
  String(const char *str, const CHARSET_INFO * const cs)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
83
  {
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
84
    Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
85
    str_charset=cs;
86
  }
87
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
88
  {
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
89
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
90
    str_charset=cs;
91
  }
92
  String(char *str,uint32_t len, const CHARSET_INFO * const cs)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
93
  {
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
94
    Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
95
    str_charset=cs;
96
  }
97
  String(const String &str)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
98
  {
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
99
    Ptr=str.Ptr ; str_length=str.str_length ;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
100
    Alloced_length=str.Alloced_length; alloced=0;
511.1.7 by Monty Taylor
Reverted all changes to sql_string.
101
    str_charset=str.str_charset;
1 by brian
clean slate
102
  }
103
  static void *operator new(size_t size, MEM_ROOT *mem_root)
104
  { return (void*) alloc_root(mem_root, (uint) size); }
644 by Brian Aker
Clean up warnings for Solaris.
105
  static void operator delete(void *, size_t)
1 by brian
clean slate
106
  { TRASH(ptr_arg, size); }
644 by Brian Aker
Clean up warnings for Solaris.
107
  static void operator delete(void *, MEM_ROOT *)
1 by brian
clean slate
108
  { /* never called */ }
109
  ~String() { free(); }
110
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
111
  inline void set_charset(const CHARSET_INFO * const charset_arg)
1 by brian
clean slate
112
  { str_charset= charset_arg; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
113
  inline const CHARSET_INFO *charset() const { return str_charset; }
205 by Brian Aker
uint32 -> uin32_t
114
  inline uint32_t length() const { return str_length;}
115
  inline uint32_t alloced_length() const { return Alloced_length;}
511.2.6 by Monty Taylor
drizzled/ and storage/archive/ are clean.
116
  inline char& operator [] (uint32_t i) const { return Ptr[i]; }
205 by Brian Aker
uint32 -> uin32_t
117
  inline void length(uint32_t len) { str_length=len ; }
1 by brian
clean slate
118
  inline bool is_empty() { return (str_length == 0); }
119
  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().
120
  inline char *ptr() { return Ptr; }
1 by brian
clean slate
121
  inline const char *ptr() const { return Ptr; }
122
  inline char *c_ptr()
123
  {
124
    if (!Ptr || Ptr[str_length])		/* Should be safe */
125
      (void) realloc(str_length);
126
    return Ptr;
127
  }
128
  inline char *c_ptr_quick()
129
  {
130
    if (Ptr && str_length < Alloced_length)
131
      Ptr[str_length]=0;
132
    return Ptr;
133
  }
134
  inline char *c_ptr_safe()
135
  {
136
    if (Ptr && str_length < Alloced_length)
137
      Ptr[str_length]=0;
138
    else
139
      (void) realloc(str_length);
140
    return Ptr;
141
  }
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);
268
  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.
269
		    const CHARSET_INFO * const cs);
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);
205 by Brian Aker
uint32 -> uin32_t
277
  bool append(IO_CACHE* file, uint32_t arg_length);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
278
  bool append_with_prefill(const char *s, uint32_t arg_length,
205 by Brian Aker
uint32 -> uin32_t
279
			   uint32_t full_length, char fill_char);
280
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
281
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
282
  bool replace(uint32_t offset,uint32_t arg_length,const char *to,uint32_t length);
283
  bool replace(uint32_t offset,uint32_t arg_length,const String &to);
1 by brian
clean slate
284
  inline bool append(char chr)
285
  {
286
    if (str_length < Alloced_length)
287
    {
288
      Ptr[str_length++]=chr;
289
    }
290
    else
291
    {
292
      if (realloc(str_length+1))
293
	return 1;
294
      Ptr[str_length++]=chr;
295
    }
296
    return 0;
297
  }
205 by Brian Aker
uint32 -> uin32_t
298
  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.
299
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
1 by brian
clean slate
300
  friend int stringcmp(const String *a,const String *b);
205 by Brian Aker
uint32 -> uin32_t
301
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
302
  uint32_t numchars();
303
  int charpos(int i,uint32_t offset=0);
1 by brian
clean slate
304
205 by Brian Aker
uint32 -> uin32_t
305
  int reserve(uint32_t space_needed)
1 by brian
clean slate
306
  {
307
    return realloc(str_length + space_needed);
308
  }
205 by Brian Aker
uint32 -> uin32_t
309
  int reserve(uint32_t space_needed, uint32_t grow_by);
1 by brian
clean slate
310
311
  /*
312
    The following append operations do NOT check alloced memory
313
    q_*** methods writes values of parameters itself
314
    qs_*** methods writes string representation of value
315
  */
316
  void q_append(const char c)
317
  {
318
    Ptr[str_length++] = c;
319
  }
205 by Brian Aker
uint32 -> uin32_t
320
  void q_append(const uint32_t n)
1 by brian
clean slate
321
  {
322
    int4store(Ptr + str_length, n);
323
    str_length += 4;
324
  }
325
  void q_append(double d)
326
  {
327
    float8store(Ptr + str_length, d);
328
    str_length += 8;
329
  }
330
  void q_append(double *d)
331
  {
332
    float8store(Ptr + str_length, *d);
333
    str_length += 8;
334
  }
205 by Brian Aker
uint32 -> uin32_t
335
  void q_append(const char *data, uint32_t data_len)
1 by brian
clean slate
336
  {
337
    memcpy(Ptr + str_length, data, data_len);
338
    str_length += data_len;
339
  }
340
205 by Brian Aker
uint32 -> uin32_t
341
  void write_at_position(int position, uint32_t value)
1 by brian
clean slate
342
  {
343
    int4store(Ptr + position,value);
344
  }
345
205 by Brian Aker
uint32 -> uin32_t
346
  void qs_append(const char *str, uint32_t len);
1 by brian
clean slate
347
  void qs_append(double d);
348
  void qs_append(double *d);
349
  inline void qs_append(const char c)
350
  {
351
     Ptr[str_length]= c;
352
     str_length++;
353
  }
354
  void qs_append(int i);
482 by Brian Aker
Remove uint.
355
  void qs_append(uint32_t i);
1 by brian
clean slate
356
357
  /* Inline (general) functions used by the protocol functions */
358
205 by Brian Aker
uint32 -> uin32_t
359
  inline char *prep_append(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)
363
    {
364
      if (realloc(new_length + step_alloc))
365
        return 0;
366
    }
205 by Brian Aker
uint32 -> uin32_t
367
    uint32_t old_length= str_length;
1 by brian
clean slate
368
    str_length+= arg_length;
369
    return Ptr+ old_length;			/* Area to use */
370
  }
371
205 by Brian Aker
uint32 -> uin32_t
372
  inline bool append(const char *s, uint32_t arg_length, uint32_t step_alloc)
1 by brian
clean slate
373
  {
205 by Brian Aker
uint32 -> uin32_t
374
    uint32_t new_length= arg_length + str_length;
1 by brian
clean slate
375
    if (new_length > Alloced_length && realloc(new_length + step_alloc))
163 by Brian Aker
Merge Monty's code.
376
      return true;
1 by brian
clean slate
377
    memcpy(Ptr+str_length, s, arg_length);
378
    str_length+= arg_length;
163 by Brian Aker
Merge Monty's code.
379
    return false;
1 by brian
clean slate
380
  }
381
  void print(String *print);
382
383
  /* Swap two string objects. Efficient way to exchange data without memcpy. */
384
  void swap(String &s);
385
386
  inline bool uses_buffer_owned_by(const String *s) const
387
  {
388
    return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
389
  }
390
};
391
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
392
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
1 by brian
clean slate
393
                                           char *end)
394
{
395
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
396
}
397
598.1.1 by Super-User
Fixed solaris build crap.
398
extern "C++" {
399
bool operator==(const String &s1, const String &s2);
400
bool operator!=(const String &s1, const String &s2);
602 by Brian Aker
Merge in Monty's work for Solaris Sun Studio
401
}
1 by brian
clean slate
402
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
403
#endif /* DRIZZLE_SERVER_SQL_STRING_H */