~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_strfunc.h

  • Committer: Monty Taylor
  • Date: 2008-11-15 17:58:57 UTC
  • mfrom: (575.5.1 drizzle)
  • mto: (589.1.3 devel)
  • mto: This revision was merged to the branch mainline in revision 586.
  • Revision ID: monty@inaugust.com-20081115175857-kxdeopxzbyyz5bkd
MergedĀ fromĀ David.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#ifndef DRIZZLED_ITEM_STRFUNC_H
21
21
#define DRIZZLED_ITEM_STRFUNC_H
22
22
 
 
23
#include <drizzled/functions/str/sysconst.h>
 
24
#include <drizzled/functions/str/alloc_buffer.h>
 
25
#include <drizzled/functions/str/binary.h>
 
26
#include <drizzled/functions/str/char.h>
 
27
#include <drizzled/functions/str/charset.h>
 
28
#include <drizzled/functions/str/collation.h>
23
29
#include <drizzled/functions/str/concat.h>
24
30
#include <drizzled/functions/str/conv.h>
 
31
#include <drizzled/functions/str/conv_charset.h>
 
32
#include <drizzled/functions/str/database.h>
 
33
#include <drizzled/functions/str/elt.h>
 
34
#include <drizzled/functions/str/export_set.h>
 
35
#include <drizzled/functions/str/format.h>
 
36
#include <drizzled/functions/str/hex.h>
25
37
#include <drizzled/functions/str/insert.h>
26
38
#include <drizzled/functions/str/left.h>
 
39
#include <drizzled/functions/str/make_set.h>
 
40
#include <drizzled/functions/str/pad.h>
 
41
#include <drizzled/functions/str/repeat.h>
27
42
#include <drizzled/functions/str/replace.h>
28
43
#include <drizzled/functions/str/reverse.h>
29
44
#include <drizzled/functions/str/right.h>
30
 
 
31
 
class Item_func_substr :public Item_str_func
32
 
{
33
 
  String tmp_value;
34
 
public:
35
 
  Item_func_substr(Item *a,Item *b) :Item_str_func(a,b) {}
36
 
  Item_func_substr(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
37
 
  String *val_str(String *);
38
 
  void fix_length_and_dec();
39
 
  const char *func_name() const { return "substr"; }
40
 
};
41
 
 
42
 
 
43
 
class Item_func_substr_index :public Item_str_func
44
 
{
45
 
  String tmp_value;
46
 
public:
47
 
  Item_func_substr_index(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
48
 
  String *val_str(String *);
49
 
  void fix_length_and_dec();
50
 
  const char *func_name() const { return "substring_index"; }
51
 
};
52
 
 
53
 
 
54
 
class Item_func_trim :public Item_str_func
55
 
{
56
 
protected:
57
 
  String tmp_value;
58
 
  String remove;
59
 
public:
60
 
  Item_func_trim(Item *a,Item *b) :Item_str_func(a,b) {}
61
 
  Item_func_trim(Item *a) :Item_str_func(a) {}
62
 
  String *val_str(String *);
63
 
  void fix_length_and_dec();
64
 
  const char *func_name() const { return "trim"; }
65
 
  virtual void print(String *str, enum_query_type query_type);
66
 
  virtual const char *mode_name() const { return "both"; }
67
 
};
68
 
 
69
 
 
70
 
class Item_func_ltrim :public Item_func_trim
71
 
{
72
 
public:
73
 
  Item_func_ltrim(Item *a,Item *b) :Item_func_trim(a,b) {}
74
 
  Item_func_ltrim(Item *a) :Item_func_trim(a) {}
75
 
  String *val_str(String *);
76
 
  const char *func_name() const { return "ltrim"; }
77
 
  const char *mode_name() const { return "leading"; }
78
 
};
79
 
 
80
 
 
81
 
class Item_func_rtrim :public Item_func_trim
82
 
{
83
 
public:
84
 
  Item_func_rtrim(Item *a,Item *b) :Item_func_trim(a,b) {}
85
 
  Item_func_rtrim(Item *a) :Item_func_trim(a) {}
86
 
  String *val_str(String *);
87
 
  const char *func_name() const { return "rtrim"; }
88
 
  const char *mode_name() const { return "trailing"; }
89
 
};
90
 
 
91
 
 
92
 
class Item_func_sysconst :public Item_str_func
93
 
{
94
 
public:
95
 
  Item_func_sysconst()
96
 
  { collation.set(system_charset_info,DERIVATION_SYSCONST); }
97
 
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
98
 
  /*
99
 
    Used to create correct Item name in new converted item in
100
 
    safe_charset_converter, return string representation of this function
101
 
    call
102
 
  */
103
 
  virtual const char *fully_qualified_func_name() const = 0;
104
 
  bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
105
 
  { return true; }
106
 
};
107
 
 
108
 
 
109
 
class Item_func_database :public Item_func_sysconst
110
 
{
111
 
public:
112
 
  Item_func_database() :Item_func_sysconst() {}
113
 
  String *val_str(String *);
114
 
  void fix_length_and_dec()
115
 
  {
116
 
    max_length= MAX_FIELD_NAME * system_charset_info->mbmaxlen;
117
 
    maybe_null=1;
118
 
  }
119
 
  const char *func_name() const { return "database"; }
120
 
  const char *fully_qualified_func_name() const { return "database()"; }
121
 
};
122
 
 
123
 
 
124
 
class Item_func_user :public Item_func_sysconst
125
 
{
126
 
protected:
127
 
  bool init (const char *user, const char *host);
128
 
 
129
 
public:
130
 
  Item_func_user()
131
 
  {
132
 
    str_value.set("", 0, system_charset_info);
133
 
  }
134
 
  String *val_str(String *)
135
 
  {
136
 
    assert(fixed == 1);
137
 
    return (null_value ? 0 : &str_value);
138
 
  }
139
 
  bool fix_fields(Session *session, Item **ref);
140
 
  void fix_length_and_dec()
141
 
  {
142
 
    max_length= (USERNAME_CHAR_LENGTH + HOSTNAME_LENGTH + 1) *
143
 
                system_charset_info->mbmaxlen;
144
 
  }
145
 
  const char *func_name() const { return "user"; }
146
 
  const char *fully_qualified_func_name() const { return "user()"; }
147
 
  int save_in_field(Field *field,
148
 
                    bool no_conversions __attribute__((unused)))
149
 
  {
150
 
    return save_str_value_in_field(field, &str_value);
151
 
  }
152
 
};
153
 
 
154
 
 
155
 
class Item_func_current_user :public Item_func_user
156
 
{
157
 
  Name_resolution_context *context;
158
 
 
159
 
public:
160
 
  Item_func_current_user(Name_resolution_context *context_arg)
161
 
    : context(context_arg) {}
162
 
  bool fix_fields(Session *session, Item **ref);
163
 
  const char *func_name() const { return "current_user"; }
164
 
  const char *fully_qualified_func_name() const { return "current_user()"; }
165
 
};
166
 
 
167
 
 
168
 
class Item_func_soundex :public Item_str_func
169
 
{
170
 
  String tmp_value;
171
 
public:
172
 
  Item_func_soundex(Item *a) :Item_str_func(a) {}
173
 
  String *val_str(String *);
174
 
  void fix_length_and_dec();
175
 
  const char *func_name() const { return "soundex"; }
176
 
};
177
 
 
178
 
 
179
 
class Item_func_elt :public Item_str_func
180
 
{
181
 
public:
182
 
  Item_func_elt(List<Item> &list) :Item_str_func(list) {}
183
 
  double val_real();
184
 
  int64_t val_int();
185
 
  String *val_str(String *str);
186
 
  void fix_length_and_dec();
187
 
  const char *func_name() const { return "elt"; }
188
 
};
189
 
 
190
 
 
191
 
class Item_func_make_set :public Item_str_func
192
 
{
193
 
  Item *item;
194
 
  String tmp_str;
195
 
 
196
 
public:
197
 
  Item_func_make_set(Item *a,List<Item> &list) :Item_str_func(list),item(a) {}
198
 
  String *val_str(String *str);
199
 
  bool fix_fields(Session *session, Item **ref)
200
 
  {
201
 
    assert(fixed == 0);
202
 
    return ((!item->fixed && item->fix_fields(session, &item)) ||
203
 
            item->check_cols(1) ||
204
 
            Item_func::fix_fields(session, ref));
205
 
  }
206
 
  void split_sum_func(Session *session, Item **ref_pointer_array, List<Item> &fields);
207
 
  void fix_length_and_dec();
208
 
  void update_used_tables();
209
 
  const char *func_name() const { return "make_set"; }
210
 
 
211
 
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
212
 
  {
213
 
    return item->walk(processor, walk_subquery, arg) ||
214
 
      Item_str_func::walk(processor, walk_subquery, arg);
215
 
  }
216
 
  Item *transform(Item_transformer transformer, unsigned char *arg);
217
 
  virtual void print(String *str, enum_query_type query_type);
218
 
};
219
 
 
220
 
 
221
 
class Item_func_format :public Item_str_func
222
 
{
223
 
  String tmp_str;
224
 
public:
225
 
  Item_func_format(Item *org, Item *dec);
226
 
  String *val_str(String *);
227
 
  void fix_length_and_dec();
228
 
  const char *func_name() const { return "format"; }
229
 
  virtual void print(String *str, enum_query_type query_type);
230
 
};
231
 
 
232
 
 
233
 
class Item_func_char :public Item_str_func
234
 
{
235
 
public:
236
 
  Item_func_char(List<Item> &list) :Item_str_func(list)
237
 
  { collation.set(&my_charset_bin); }
238
 
  Item_func_char(List<Item> &list, const CHARSET_INFO * const cs) :Item_str_func(list)
239
 
  { collation.set(cs); }  
240
 
  String *val_str(String *);
241
 
  void fix_length_and_dec() 
242
 
  {
243
 
    max_length= arg_count * 4;
244
 
  }
245
 
  const char *func_name() const { return "char"; }
246
 
};
247
 
 
248
 
 
249
 
class Item_func_repeat :public Item_str_func
250
 
{
251
 
  String tmp_value;
252
 
public:
253
 
  Item_func_repeat(Item *arg1,Item *arg2) :Item_str_func(arg1,arg2) {}
254
 
  String *val_str(String *);
255
 
  void fix_length_and_dec();
256
 
  const char *func_name() const { return "repeat"; }
257
 
};
258
 
 
259
 
 
260
 
class Item_func_rpad :public Item_str_func
261
 
{
262
 
  String tmp_value, rpad_str;
263
 
public:
264
 
  Item_func_rpad(Item *arg1,Item *arg2,Item *arg3)
265
 
    :Item_str_func(arg1,arg2,arg3) {}
266
 
  String *val_str(String *);
267
 
  void fix_length_and_dec();
268
 
  const char *func_name() const { return "rpad"; }
269
 
};
270
 
 
271
 
 
272
 
class Item_func_lpad :public Item_str_func
273
 
{
274
 
  String tmp_value, lpad_str;
275
 
public:
276
 
  Item_func_lpad(Item *arg1,Item *arg2,Item *arg3)
277
 
    :Item_str_func(arg1,arg2,arg3) {}
278
 
  String *val_str(String *);
279
 
  void fix_length_and_dec();
280
 
  const char *func_name() const { return "lpad"; }
281
 
};
282
 
 
283
 
 
284
 
class Item_func_conv :public Item_str_func
285
 
{
286
 
public:
287
 
  Item_func_conv(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
288
 
  const char *func_name() const { return "conv"; }
289
 
  String *val_str(String *);
290
 
  void fix_length_and_dec()
291
 
  {
292
 
    collation.set(default_charset());
293
 
    max_length=64;
294
 
    maybe_null= 1;
295
 
  }
296
 
};
297
 
 
298
 
 
299
 
class Item_func_hex :public Item_str_func
300
 
{
301
 
  String tmp_value;
302
 
public:
303
 
  Item_func_hex(Item *a) :Item_str_func(a) {}
304
 
  const char *func_name() const { return "hex"; }
305
 
  String *val_str(String *);
306
 
  void fix_length_and_dec()
307
 
  {
308
 
    collation.set(default_charset());
309
 
    decimals=0;
310
 
    max_length=args[0]->max_length*2*collation.collation->mbmaxlen;
311
 
  }
312
 
};
313
 
 
314
 
class Item_func_unhex :public Item_str_func
315
 
{
316
 
  String tmp_value;
317
 
public:
318
 
  Item_func_unhex(Item *a) :Item_str_func(a) 
319
 
  { 
320
 
    /* there can be bad hex strings */
321
 
    maybe_null= 1; 
322
 
  }
323
 
  const char *func_name() const { return "unhex"; }
324
 
  String *val_str(String *);
325
 
  void fix_length_and_dec()
326
 
  {
327
 
    collation.set(&my_charset_bin);
328
 
    decimals=0;
329
 
    max_length=(1+args[0]->max_length)/2;
330
 
  }
331
 
};
332
 
 
333
 
 
334
 
class Item_func_binary :public Item_str_func
335
 
{
336
 
public:
337
 
  Item_func_binary(Item *a) :Item_str_func(a) {}
338
 
  String *val_str(String *a)
339
 
  {
340
 
    assert(fixed == 1);
341
 
    String *tmp=args[0]->val_str(a);
342
 
    null_value=args[0]->null_value;
343
 
    if (tmp)
344
 
      tmp->set_charset(&my_charset_bin);
345
 
    return tmp;
346
 
  }
347
 
  void fix_length_and_dec()
348
 
  {
349
 
    collation.set(&my_charset_bin);
350
 
    max_length=args[0]->max_length;
351
 
  }
352
 
  virtual void print(String *str, enum_query_type query_type);
353
 
  const char *func_name() const { return "cast_as_binary"; }
354
 
};
355
 
 
 
45
#include <drizzled/functions/str/set_collation.h>
 
46
#include <drizzled/functions/str/str_conv.h>
 
47
#include <drizzled/functions/str/substr.h>
 
48
#include <drizzled/functions/str/trim.h>
 
49
#include <drizzled/functions/str/user.h>
 
50
#include <drizzled/functions/str/uuid.h>
 
51
#include <drizzled/functions/str/weight_string.h>
356
52
 
357
53
class Item_load_file :public Item_str_func
358
54
{
371
67
  { return true; }
372
68
};
373
69
 
374
 
 
375
 
class Item_func_export_set: public Item_str_func
376
 
{
377
 
 public:
378
 
  Item_func_export_set(Item *a,Item *b,Item* c) :Item_str_func(a,b,c) {}
379
 
  Item_func_export_set(Item *a,Item *b,Item* c,Item* d) :Item_str_func(a,b,c,d) {}
380
 
  Item_func_export_set(Item *a,Item *b,Item* c,Item* d,Item* e) :Item_str_func(a,b,c,d,e) {}
381
 
  String  *val_str(String *str);
382
 
  void fix_length_and_dec();
383
 
  const char *func_name() const { return "export_set"; }
384
 
};
385
 
 
386
 
class Item_func_quote :public Item_str_func
387
 
{
388
 
  String tmp_value;
389
 
public:
390
 
  Item_func_quote(Item *a) :Item_str_func(a) {}
391
 
  const char *func_name() const { return "quote"; }
392
 
  String *val_str(String *);
393
 
  void fix_length_and_dec()
394
 
  {
395
 
    collation.set(args[0]->collation);
396
 
    max_length= args[0]->max_length * 2 + 2;
397
 
  }
398
 
};
399
 
 
400
 
class Item_func_conv_charset :public Item_str_func
401
 
{
402
 
  bool use_cached_value;
403
 
public:
404
 
  bool safe;
405
 
  const CHARSET_INFO *conv_charset; // keep it public
406
 
  Item_func_conv_charset(Item *a, const CHARSET_INFO * const cs) :Item_str_func(a) 
407
 
  { conv_charset= cs; use_cached_value= 0; safe= 0; }
408
 
  Item_func_conv_charset(Item *a, const CHARSET_INFO * const cs, bool cache_if_const) 
409
 
    :Item_str_func(a) 
410
 
  {
411
 
    assert(args[0]->fixed);
412
 
    conv_charset= cs;
413
 
    if (cache_if_const && args[0]->const_item())
414
 
    {
415
 
      uint32_t errors= 0;
416
 
      String tmp, *str= args[0]->val_str(&tmp);
417
 
      if (!str || str_value.copy(str->ptr(), str->length(),
418
 
                                 str->charset(), conv_charset, &errors))
419
 
        null_value= 1;
420
 
      use_cached_value= 1;
421
 
      str_value.mark_as_const();
422
 
      safe= (errors == 0);
423
 
    }
424
 
    else
425
 
    {
426
 
      use_cached_value= 0;
427
 
      /*
428
 
        Conversion from and to "binary" is safe.
429
 
        Conversion to Unicode is safe.
430
 
        Other kind of conversions are potentially lossy.
431
 
      */
432
 
      safe= (args[0]->collation.collation == &my_charset_bin ||
433
 
             cs == &my_charset_bin ||
434
 
             (cs->state & MY_CS_UNICODE));
435
 
    }
436
 
  }
437
 
  String *val_str(String *);
438
 
  void fix_length_and_dec();
439
 
  const char *func_name() const { return "convert"; }
440
 
  virtual void print(String *str, enum_query_type query_type);
441
 
};
442
 
 
443
 
class Item_func_set_collation :public Item_str_func
444
 
{
445
 
public:
446
 
  Item_func_set_collation(Item *a, Item *b) :Item_str_func(a,b) {};
447
 
  String *val_str(String *);
448
 
  void fix_length_and_dec();
449
 
  bool eq(const Item *item, bool binary_cmp) const;
450
 
  const char *func_name() const { return "collate"; }
451
 
  enum Functype functype() const { return COLLATE_FUNC; }
452
 
  virtual void print(String *str, enum_query_type query_type);
453
 
  Item_field *filed_for_view_update()
454
 
  {
455
 
    /* this function is transparent for view updating */
456
 
    return args[0]->filed_for_view_update();
457
 
  }
458
 
};
459
 
 
460
 
class Item_func_charset :public Item_str_func
461
 
{
462
 
public:
463
 
  Item_func_charset(Item *a) :Item_str_func(a) {}
464
 
  String *val_str(String *);
465
 
  const char *func_name() const { return "charset"; }
466
 
  void fix_length_and_dec()
467
 
  {
468
 
     collation.set(system_charset_info);
469
 
     max_length= 64 * collation.collation->mbmaxlen; // should be enough
470
 
     maybe_null= 0;
471
 
  };
472
 
  table_map not_null_tables() const { return 0; }
473
 
};
474
 
 
475
 
class Item_func_collation :public Item_str_func
476
 
{
477
 
public:
478
 
  Item_func_collation(Item *a) :Item_str_func(a) {}
479
 
  String *val_str(String *);
480
 
  const char *func_name() const { return "collation"; }
481
 
  void fix_length_and_dec()
482
 
  {
483
 
     collation.set(system_charset_info);
484
 
     max_length= 64 * collation.collation->mbmaxlen; // should be enough
485
 
     maybe_null= 0;
486
 
  };
487
 
  table_map not_null_tables() const { return 0; }
488
 
};
489
 
 
490
 
 
491
 
class Item_func_weight_string :public Item_str_func
492
 
{
493
 
  String tmp_value;
494
 
  uint32_t flags;
495
 
  uint32_t nweights;
496
 
public:
497
 
  Item_func_weight_string(Item *a, uint32_t nweights_arg, uint32_t flags_arg)
498
 
  :Item_str_func(a) { nweights= nweights_arg; flags= flags_arg; }
499
 
  const char *func_name() const { return "weight_string"; }
500
 
  String *val_str(String *);
501
 
  void fix_length_and_dec();
502
 
  /*
503
 
    TODO: Currently this Item is not allowed for virtual columns
504
 
    only due to a bug in generating virtual column value.
505
 
  */
506
 
  bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
507
 
  { return true; }
508
 
};
509
 
 
510
 
#define UUID_LENGTH (8+1+4+1+4+1+4+1+12)
511
 
class Item_func_uuid: public Item_str_func
512
 
{
513
 
public:
514
 
  Item_func_uuid(): Item_str_func() {}
515
 
  void fix_length_and_dec() {
516
 
    collation.set(system_charset_info);
517
 
    /*
518
 
       NOTE! uuid() should be changed to use 'ascii'
519
 
       charset when hex(), format(), md5(), etc, and implicit
520
 
       number-to-string conversion will use 'ascii'
521
 
    */
522
 
    max_length= UUID_LENGTH * system_charset_info->mbmaxlen;
523
 
  }
524
 
  const char *func_name() const{ return "uuid"; }
525
 
  String *val_str(String *);
526
 
  bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
527
 
  { return true; }
528
 
};
529
 
 
530
70
#endif /* DRIZZLED_ITEM_STRFUNC_H */