~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Monty Taylor
  • Date: 2009-05-09 22:13:47 UTC
  • mto: This revision was merged to the branch mainline in revision 1009.
  • Revision ID: mordred@inaugust.com-20090509221347-l712szviusbobro0
Re-added bitset<> as a replacement for Bitmap<>

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
/* This file is originally from the mysql distribution. Coded by monty */
24
24
 
25
 
#ifdef USE_PRAGMA_INTERFACE
26
 
#pragma interface                       /* gcc class implementation */
27
 
#endif
28
25
 
29
26
#ifndef NOT_FIXED_DEC
30
 
#define NOT_FIXED_DEC                   31
 
27
#define NOT_FIXED_DEC                   (uint8_t)31
31
28
#endif
32
29
 
33
 
#include <libdrizzle/drizzle_com.h>
 
30
#include <drizzled/common.h>
 
31
#include <mysys/iocache.h>
 
32
#include <stdlib.h>
 
33
#include <string.h>
34
34
 
35
35
class String;
36
 
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
37
 
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
38
 
uint32_t copy_and_convert(char *to, uint32_t to_length, const CHARSET_INFO * const to_cs,
39
 
                        const char *from, uint32_t from_length,
40
 
                        const CHARSET_INFO * const from_cs, uint32_t *errors);
41
 
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
42
 
                               char *to, uint32_t to_length,
43
 
                               const CHARSET_INFO * const from_cs,
44
 
                               const char *from, uint32_t from_length,
45
 
                               uint32_t nchars,
46
 
                               const char **well_formed_error_pos,
47
 
                               const char **cannot_convert_error_pos,
48
 
                               const char **from_end_pos);
49
 
size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
50
 
                                 char *dst, size_t dstlen,
51
 
                                 const char *src, size_t srclen);
 
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
52
64
 
53
65
class String
54
66
{
56
68
  uint32_t str_length,Alloced_length;
57
69
  bool alloced;
58
70
  const CHARSET_INFO *str_charset;
 
71
 
59
72
public:
60
73
  String()
61
 
  { 
62
 
    Ptr=0; str_length=Alloced_length=0; alloced=0; 
63
 
    str_charset= &my_charset_bin; 
 
74
  {
 
75
    Ptr=0; str_length=Alloced_length=0; alloced=0;
 
76
    str_charset= &my_charset_bin;
64
77
  }
65
78
  String(uint32_t length_arg)
66
 
  { 
67
 
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg); 
 
79
  {
 
80
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
68
81
    str_charset= &my_charset_bin;
69
82
  }
70
83
  String(const char *str, const CHARSET_INFO * const cs)
71
 
  { 
72
 
    Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
 
84
  {
 
85
    Ptr=(char*) str; str_length=(uint32_t) strlen(str); Alloced_length=0; alloced=0;
73
86
    str_charset=cs;
74
87
  }
75
88
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
76
 
  { 
 
89
  {
77
90
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
78
91
    str_charset=cs;
79
92
  }
80
93
  String(char *str,uint32_t len, const CHARSET_INFO * const cs)
81
 
  { 
 
94
  {
82
95
    Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
83
96
    str_charset=cs;
84
97
  }
85
98
  String(const String &str)
86
 
  { 
 
99
  {
87
100
    Ptr=str.Ptr ; str_length=str.str_length ;
88
 
    Alloced_length=str.Alloced_length; alloced=0; 
 
101
    Alloced_length=str.Alloced_length; alloced=0;
89
102
    str_charset=str.str_charset;
90
103
  }
91
104
  static void *operator new(size_t size, MEM_ROOT *mem_root)
92
 
  { return (void*) alloc_root(mem_root, (uint) size); }
93
 
  static void operator delete(void *ptr_arg __attribute__((unused)),
94
 
                              size_t size __attribute__((unused)))
 
105
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
 
106
  static void operator delete(void *, size_t)
95
107
  { TRASH(ptr_arg, size); }
96
 
  static void operator delete(void *ptr_arg __attribute__((unused)),
97
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
108
  static void operator delete(void *, MEM_ROOT *)
98
109
  { /* never called */ }
99
110
  ~String() { free(); }
100
111
 
129
140
      (void) realloc(str_length);
130
141
    return Ptr;
131
142
  }
 
143
  void append_identifier(const char *name, uint32_t length);
132
144
 
133
145
  void set(String &str,uint32_t offset,uint32_t arg_length)
134
146
  {
178
190
    statement to be run on the remote server, and have a comma after each.
179
191
    When the list is complete, I "chop" off the trailing comma
180
192
 
181
 
    ex. 
182
 
      String stringobj; 
 
193
    ex.
 
194
      String stringobj;
183
195
      stringobj.append("VALUES ('foo', 'fi', 'fo',");
184
196
      stringobj.chop();
185
197
      stringobj.append(")");
189
201
    VALUES ('foo', 'fi', 'fo',
190
202
    VALUES ('foo', 'fi', 'fo'
191
203
    VALUES ('foo', 'fi', 'fo')
192
 
      
 
204
 
193
205
  */
194
206
  inline void chop()
195
207
  {
196
 
    Ptr[str_length--]= '\0'; 
 
208
    Ptr[str_length--]= '\0';
197
209
  }
198
210
 
199
211
  inline void free()
220
232
    if (arg_length < Alloced_length)
221
233
    {
222
234
      char *new_ptr;
223
 
      if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
 
235
      if (!(new_ptr=(char*) ::realloc(Ptr,arg_length)))
224
236
      {
225
237
        Alloced_length = 0;
226
238
        real_alloc(arg_length);
238
250
    if (&s != this)
239
251
    {
240
252
      /*
241
 
        It is forbidden to do assignments like 
 
253
        It is forbidden to do assignments like
242
254
        some_string = substring_of_that_string
243
255
       */
244
256
      assert(!s.uses_buffer_owned_by(this));
255
267
  static bool needs_conversion(uint32_t arg_length,
256
268
                               const CHARSET_INFO * const cs_from, const CHARSET_INFO * const cs_to,
257
269
                               uint32_t *offset);
258
 
  bool copy_aligned(const char *s, uint32_t arg_length, uint32_t offset,
259
 
                    const CHARSET_INFO * const cs);
260
270
  bool set_or_copy_aligned(const char *s, uint32_t arg_length, const CHARSET_INFO * const cs);
261
271
  bool copy(const char*s,uint32_t arg_length, const CHARSET_INFO * const csfrom,
262
272
            const CHARSET_INFO * const csto, uint32_t *errors);
264
274
  bool append(const char *s);
265
275
  bool append(const char *s,uint32_t arg_length);
266
276
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
267
 
  bool append(IO_CACHE* file, uint32_t arg_length);
268
 
  bool append_with_prefill(const char *s, uint32_t arg_length, 
 
277
  bool append_with_prefill(const char *s, uint32_t arg_length,
269
278
                           uint32_t full_length, char fill_char);
270
279
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
271
280
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
285
294
    }
286
295
    return 0;
287
296
  }
288
 
  bool fill(uint32_t max_length,char fill);
289
297
  friend int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
290
298
  friend int stringcmp(const String *a,const String *b);
291
299
  friend String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
333
341
    int4store(Ptr + position,value);
334
342
  }
335
343
 
336
 
  void qs_append(const char *str, uint32_t len);
337
 
  void qs_append(double d);
338
 
  void qs_append(double *d);
339
 
  inline void qs_append(const char c)
340
 
  {
341
 
     Ptr[str_length]= c;
342
 
     str_length++;
343
 
  }
344
 
  void qs_append(int i);
345
 
  void qs_append(uint32_t i);
346
 
 
347
344
  /* Inline (general) functions used by the protocol functions */
348
345
 
349
346
  inline char *prep_append(uint32_t arg_length, uint32_t step_alloc)
379
376
  }
380
377
};
381
378
 
382
 
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str, 
 
379
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
383
380
                                           char *end)
384
381
{
385
382
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
386
383
}
387
384
 
388
 
inline
389
 
bool operator==(const String &s1, const String &s2)
390
 
{
391
 
  return stringcmp(&s1,&s2) == 0;
392
 
}
393
 
 
394
 
inline
395
 
bool operator!=(const String &s1, const String &s2)
396
 
{
397
 
  return !(s1 == s2);
 
385
extern "C++" {
 
386
bool operator==(const String &s1, const String &s2);
 
387
bool operator!=(const String &s1, const String &s2);
398
388
}
399
389
 
400
390
#endif /* DRIZZLE_SERVER_SQL_STRING_H */