~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
20
21
/* This file defines all string functions */
22
23
24
class Item_str_func :public Item_func
25
{
26
public:
27
  Item_str_func() :Item_func() { decimals=NOT_FIXED_DEC; }
28
  Item_str_func(Item *a) :Item_func(a) {decimals=NOT_FIXED_DEC; }
29
  Item_str_func(Item *a,Item *b) :Item_func(a,b) { decimals=NOT_FIXED_DEC; }
30
  Item_str_func(Item *a,Item *b,Item *c) :Item_func(a,b,c) { decimals=NOT_FIXED_DEC; }
31
  Item_str_func(Item *a,Item *b,Item *c,Item *d) :Item_func(a,b,c,d) {decimals=NOT_FIXED_DEC; }
32
  Item_str_func(Item *a,Item *b,Item *c,Item *d, Item* e) :Item_func(a,b,c,d,e) {decimals=NOT_FIXED_DEC; }
33
  Item_str_func(List<Item> &list) :Item_func(list) {decimals=NOT_FIXED_DEC; }
152 by Brian Aker
longlong replacement
34
  int64_t val_int();
1 by brian
clean slate
35
  double val_real();
36
  my_decimal *val_decimal(my_decimal *);
37
  enum Item_result result_type () const { return STRING_RESULT; }
38
  void left_right_max_length();
39
  bool fix_fields(THD *thd, Item **ref);
40
};
41
42
43
class Item_func_concat :public Item_str_func
44
{
45
  String tmp_value;
46
public:
47
  Item_func_concat(List<Item> &list) :Item_str_func(list) {}
48
  Item_func_concat(Item *a,Item *b) :Item_str_func(a,b) {}
49
  String *val_str(String *);
50
  void fix_length_and_dec();
51
  const char *func_name() const { return "concat"; }
52
};
53
54
class Item_func_concat_ws :public Item_str_func
55
{
56
  String tmp_value;
57
public:
58
  Item_func_concat_ws(List<Item> &list) :Item_str_func(list) {}
59
  String *val_str(String *);
60
  void fix_length_and_dec();
61
  const char *func_name() const { return "concat_ws"; }
62
  table_map not_null_tables() const { return 0; }
63
};
64
65
class Item_func_reverse :public Item_str_func
66
{
67
  String tmp_value;
68
public:
69
  Item_func_reverse(Item *a) :Item_str_func(a) {}
70
  String *val_str(String *);
71
  void fix_length_and_dec();
72
  const char *func_name() const { return "reverse"; }
73
};
74
75
76
class Item_func_replace :public Item_str_func
77
{
78
  String tmp_value,tmp_value2;
79
public:
80
  Item_func_replace(Item *org,Item *find,Item *replace)
81
    :Item_str_func(org,find,replace) {}
82
  String *val_str(String *);
83
  void fix_length_and_dec();
84
  const char *func_name() const { return "replace"; }
85
};
86
87
88
class Item_func_insert :public Item_str_func
89
{
90
  String tmp_value;
91
public:
92
  Item_func_insert(Item *org,Item *start,Item *length,Item *new_str)
93
    :Item_str_func(org,start,length,new_str) {}
94
  String *val_str(String *);
95
  void fix_length_and_dec();
96
  const char *func_name() const { return "insert"; }
97
};
98
99
100
class Item_str_conv :public Item_str_func
101
{
102
protected:
482 by Brian Aker
Remove uint.
103
  uint32_t multiply;
1 by brian
clean slate
104
  my_charset_conv_case converter;
105
  String tmp_value;
106
public:
107
  Item_str_conv(Item *item) :Item_str_func(item) {}
108
  String *val_str(String *);
109
};
110
111
112
class Item_func_lcase :public Item_str_conv
113
{
114
public:
115
  Item_func_lcase(Item *item) :Item_str_conv(item) {}
116
  const char *func_name() const { return "lcase"; }
117
  void fix_length_and_dec();
118
};
119
120
class Item_func_ucase :public Item_str_conv
121
{
122
public:
123
  Item_func_ucase(Item *item) :Item_str_conv(item) {}
124
  const char *func_name() const { return "ucase"; }
125
  void fix_length_and_dec();
126
};
127
128
129
class Item_func_left :public Item_str_func
130
{
131
  String tmp_value;
132
public:
133
  Item_func_left(Item *a,Item *b) :Item_str_func(a,b) {}
134
  String *val_str(String *);
135
  void fix_length_and_dec();
136
  const char *func_name() const { return "left"; }
137
};
138
139
140
class Item_func_right :public Item_str_func
141
{
142
  String tmp_value;
143
public:
144
  Item_func_right(Item *a,Item *b) :Item_str_func(a,b) {}
145
  String *val_str(String *);
146
  void fix_length_and_dec();
147
  const char *func_name() const { return "right"; }
148
};
149
150
151
class Item_func_substr :public Item_str_func
152
{
153
  String tmp_value;
154
public:
155
  Item_func_substr(Item *a,Item *b) :Item_str_func(a,b) {}
156
  Item_func_substr(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
157
  String *val_str(String *);
158
  void fix_length_and_dec();
159
  const char *func_name() const { return "substr"; }
160
};
161
162
163
class Item_func_substr_index :public Item_str_func
164
{
165
  String tmp_value;
166
public:
167
  Item_func_substr_index(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
168
  String *val_str(String *);
169
  void fix_length_and_dec();
170
  const char *func_name() const { return "substring_index"; }
171
};
172
173
174
class Item_func_trim :public Item_str_func
175
{
176
protected:
177
  String tmp_value;
178
  String remove;
179
public:
180
  Item_func_trim(Item *a,Item *b) :Item_str_func(a,b) {}
181
  Item_func_trim(Item *a) :Item_str_func(a) {}
182
  String *val_str(String *);
183
  void fix_length_and_dec();
184
  const char *func_name() const { return "trim"; }
185
  virtual void print(String *str, enum_query_type query_type);
186
  virtual const char *mode_name() const { return "both"; }
187
};
188
189
190
class Item_func_ltrim :public Item_func_trim
191
{
192
public:
193
  Item_func_ltrim(Item *a,Item *b) :Item_func_trim(a,b) {}
194
  Item_func_ltrim(Item *a) :Item_func_trim(a) {}
195
  String *val_str(String *);
196
  const char *func_name() const { return "ltrim"; }
197
  const char *mode_name() const { return "leading"; }
198
};
199
200
201
class Item_func_rtrim :public Item_func_trim
202
{
203
public:
204
  Item_func_rtrim(Item *a,Item *b) :Item_func_trim(a,b) {}
205
  Item_func_rtrim(Item *a) :Item_func_trim(a) {}
206
  String *val_str(String *);
207
  const char *func_name() const { return "rtrim"; }
208
  const char *mode_name() const { return "trailing"; }
209
};
210
211
212
class Item_func_sysconst :public Item_str_func
213
{
214
public:
215
  Item_func_sysconst()
216
  { collation.set(system_charset_info,DERIVATION_SYSCONST); }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
217
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
218
  /*
219
    Used to create correct Item name in new converted item in
220
    safe_charset_converter, return string representation of this function
221
    call
222
  */
223
  virtual const char *fully_qualified_func_name() const = 0;
224
};
225
226
227
class Item_func_database :public Item_func_sysconst
228
{
229
public:
230
  Item_func_database() :Item_func_sysconst() {}
231
  String *val_str(String *);
232
  void fix_length_and_dec()
233
  {
234
    max_length= MAX_FIELD_NAME * system_charset_info->mbmaxlen;
235
    maybe_null=1;
236
  }
237
  const char *func_name() const { return "database"; }
238
  const char *fully_qualified_func_name() const { return "database()"; }
239
};
240
241
242
class Item_func_user :public Item_func_sysconst
243
{
244
protected:
245
  bool init (const char *user, const char *host);
246
247
public:
248
  Item_func_user()
249
  {
250
    str_value.set("", 0, system_charset_info);
251
  }
252
  String *val_str(String *)
253
  {
51.1.23 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
254
    assert(fixed == 1);
1 by brian
clean slate
255
    return (null_value ? 0 : &str_value);
256
  }
257
  bool fix_fields(THD *thd, Item **ref);
258
  void fix_length_and_dec()
259
  {
260
    max_length= (USERNAME_CHAR_LENGTH + HOSTNAME_LENGTH + 1) *
261
                system_charset_info->mbmaxlen;
262
  }
263
  const char *func_name() const { return "user"; }
264
  const char *fully_qualified_func_name() const { return "user()"; }
77.1.7 by Monty Taylor
Heap builds clean.
265
  int save_in_field(Field *field,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
266
                    bool no_conversions __attribute__((unused)))
1 by brian
clean slate
267
  {
268
    return save_str_value_in_field(field, &str_value);
269
  }
270
};
271
272
273
class Item_func_current_user :public Item_func_user
274
{
275
  Name_resolution_context *context;
276
277
public:
278
  Item_func_current_user(Name_resolution_context *context_arg)
279
    : context(context_arg) {}
280
  bool fix_fields(THD *thd, Item **ref);
281
  const char *func_name() const { return "current_user"; }
282
  const char *fully_qualified_func_name() const { return "current_user()"; }
283
};
284
285
286
class Item_func_soundex :public Item_str_func
287
{
288
  String tmp_value;
289
public:
290
  Item_func_soundex(Item *a) :Item_str_func(a) {}
291
  String *val_str(String *);
292
  void fix_length_and_dec();
293
  const char *func_name() const { return "soundex"; }
294
};
295
296
297
class Item_func_elt :public Item_str_func
298
{
299
public:
300
  Item_func_elt(List<Item> &list) :Item_str_func(list) {}
301
  double val_real();
152 by Brian Aker
longlong replacement
302
  int64_t val_int();
1 by brian
clean slate
303
  String *val_str(String *str);
304
  void fix_length_and_dec();
305
  const char *func_name() const { return "elt"; }
306
};
307
308
309
class Item_func_make_set :public Item_str_func
310
{
311
  Item *item;
312
  String tmp_str;
313
314
public:
315
  Item_func_make_set(Item *a,List<Item> &list) :Item_str_func(list),item(a) {}
316
  String *val_str(String *str);
317
  bool fix_fields(THD *thd, Item **ref)
318
  {
51.1.23 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
319
    assert(fixed == 0);
1 by brian
clean slate
320
    return ((!item->fixed && item->fix_fields(thd, &item)) ||
321
	    item->check_cols(1) ||
322
	    Item_func::fix_fields(thd, ref));
323
  }
324
  void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
325
  void fix_length_and_dec();
326
  void update_used_tables();
327
  const char *func_name() const { return "make_set"; }
328
481 by Brian Aker
Remove all of uchar.
329
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
1 by brian
clean slate
330
  {
331
    return item->walk(processor, walk_subquery, arg) ||
332
      Item_str_func::walk(processor, walk_subquery, arg);
333
  }
481 by Brian Aker
Remove all of uchar.
334
  Item *transform(Item_transformer transformer, unsigned char *arg);
1 by brian
clean slate
335
  virtual void print(String *str, enum_query_type query_type);
336
};
337
338
339
class Item_func_format :public Item_str_func
340
{
341
  String tmp_str;
342
public:
343
  Item_func_format(Item *org, Item *dec);
344
  String *val_str(String *);
345
  void fix_length_and_dec();
346
  const char *func_name() const { return "format"; }
347
  virtual void print(String *str, enum_query_type query_type);
348
};
349
350
351
class Item_func_char :public Item_str_func
352
{
353
public:
354
  Item_func_char(List<Item> &list) :Item_str_func(list)
355
  { collation.set(&my_charset_bin); }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
356
  Item_func_char(List<Item> &list, const CHARSET_INFO * const cs) :Item_str_func(list)
1 by brian
clean slate
357
  { collation.set(cs); }  
358
  String *val_str(String *);
359
  void fix_length_and_dec() 
360
  {
361
    max_length= arg_count * 4;
362
  }
363
  const char *func_name() const { return "char"; }
364
};
365
366
367
class Item_func_repeat :public Item_str_func
368
{
369
  String tmp_value;
370
public:
371
  Item_func_repeat(Item *arg1,Item *arg2) :Item_str_func(arg1,arg2) {}
372
  String *val_str(String *);
373
  void fix_length_and_dec();
374
  const char *func_name() const { return "repeat"; }
375
};
376
377
378
class Item_func_rpad :public Item_str_func
379
{
380
  String tmp_value, rpad_str;
381
public:
382
  Item_func_rpad(Item *arg1,Item *arg2,Item *arg3)
383
    :Item_str_func(arg1,arg2,arg3) {}
384
  String *val_str(String *);
385
  void fix_length_and_dec();
386
  const char *func_name() const { return "rpad"; }
387
};
388
389
390
class Item_func_lpad :public Item_str_func
391
{
392
  String tmp_value, lpad_str;
393
public:
394
  Item_func_lpad(Item *arg1,Item *arg2,Item *arg3)
395
    :Item_str_func(arg1,arg2,arg3) {}
396
  String *val_str(String *);
397
  void fix_length_and_dec();
398
  const char *func_name() const { return "lpad"; }
399
};
400
401
402
class Item_func_conv :public Item_str_func
403
{
404
public:
405
  Item_func_conv(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
406
  const char *func_name() const { return "conv"; }
407
  String *val_str(String *);
408
  void fix_length_and_dec()
409
  {
410
    collation.set(default_charset());
411
    max_length=64;
412
    maybe_null= 1;
413
  }
414
};
415
416
417
class Item_func_hex :public Item_str_func
418
{
419
  String tmp_value;
420
public:
421
  Item_func_hex(Item *a) :Item_str_func(a) {}
422
  const char *func_name() const { return "hex"; }
423
  String *val_str(String *);
424
  void fix_length_and_dec()
425
  {
426
    collation.set(default_charset());
427
    decimals=0;
428
    max_length=args[0]->max_length*2*collation.collation->mbmaxlen;
429
  }
430
};
431
432
class Item_func_unhex :public Item_str_func
433
{
434
  String tmp_value;
435
public:
436
  Item_func_unhex(Item *a) :Item_str_func(a) 
437
  { 
438
    /* there can be bad hex strings */
439
    maybe_null= 1; 
440
  }
441
  const char *func_name() const { return "unhex"; }
442
  String *val_str(String *);
443
  void fix_length_and_dec()
444
  {
445
    collation.set(&my_charset_bin);
446
    decimals=0;
447
    max_length=(1+args[0]->max_length)/2;
448
  }
449
};
450
451
452
class Item_func_binary :public Item_str_func
453
{
454
public:
455
  Item_func_binary(Item *a) :Item_str_func(a) {}
456
  String *val_str(String *a)
457
  {
51.1.23 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
458
    assert(fixed == 1);
1 by brian
clean slate
459
    String *tmp=args[0]->val_str(a);
460
    null_value=args[0]->null_value;
461
    if (tmp)
462
      tmp->set_charset(&my_charset_bin);
463
    return tmp;
464
  }
465
  void fix_length_and_dec()
466
  {
467
    collation.set(&my_charset_bin);
468
    max_length=args[0]->max_length;
469
  }
470
  virtual void print(String *str, enum_query_type query_type);
471
  const char *func_name() const { return "cast_as_binary"; }
472
};
473
474
475
class Item_load_file :public Item_str_func
476
{
477
  String tmp_value;
478
public:
479
  Item_load_file(Item *a) :Item_str_func(a) {}
480
  String *val_str(String *);
481
  const char *func_name() const { return "load_file"; }
482
  void fix_length_and_dec()
483
  {
484
    collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
485
    maybe_null=1;
486
    max_length=MAX_BLOB_WIDTH;
487
  }
488
};
489
490
491
class Item_func_export_set: public Item_str_func
492
{
493
 public:
494
  Item_func_export_set(Item *a,Item *b,Item* c) :Item_str_func(a,b,c) {}
495
  Item_func_export_set(Item *a,Item *b,Item* c,Item* d) :Item_str_func(a,b,c,d) {}
496
  Item_func_export_set(Item *a,Item *b,Item* c,Item* d,Item* e) :Item_str_func(a,b,c,d,e) {}
497
  String  *val_str(String *str);
498
  void fix_length_and_dec();
499
  const char *func_name() const { return "export_set"; }
500
};
501
502
class Item_func_quote :public Item_str_func
503
{
504
  String tmp_value;
505
public:
506
  Item_func_quote(Item *a) :Item_str_func(a) {}
507
  const char *func_name() const { return "quote"; }
508
  String *val_str(String *);
509
  void fix_length_and_dec()
510
  {
511
    collation.set(args[0]->collation);
512
    max_length= args[0]->max_length * 2 + 2;
513
  }
514
};
515
516
class Item_func_conv_charset :public Item_str_func
517
{
518
  bool use_cached_value;
519
public:
520
  bool safe;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
521
  const CHARSET_INFO *conv_charset; // keep it public
522
  Item_func_conv_charset(Item *a, const CHARSET_INFO * const cs) :Item_str_func(a) 
1 by brian
clean slate
523
  { conv_charset= cs; use_cached_value= 0; safe= 0; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
524
  Item_func_conv_charset(Item *a, const CHARSET_INFO * const cs, bool cache_if_const) 
1 by brian
clean slate
525
    :Item_str_func(a) 
526
  {
51.1.23 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
527
    assert(args[0]->fixed);
1 by brian
clean slate
528
    conv_charset= cs;
529
    if (cache_if_const && args[0]->const_item())
530
    {
482 by Brian Aker
Remove uint.
531
      uint32_t errors= 0;
1 by brian
clean slate
532
      String tmp, *str= args[0]->val_str(&tmp);
533
      if (!str || str_value.copy(str->ptr(), str->length(),
534
                                 str->charset(), conv_charset, &errors))
535
        null_value= 1;
536
      use_cached_value= 1;
537
      str_value.mark_as_const();
538
      safe= (errors == 0);
539
    }
540
    else
541
    {
542
      use_cached_value= 0;
543
      /*
544
        Conversion from and to "binary" is safe.
545
        Conversion to Unicode is safe.
546
        Other kind of conversions are potentially lossy.
547
      */
548
      safe= (args[0]->collation.collation == &my_charset_bin ||
549
             cs == &my_charset_bin ||
550
             (cs->state & MY_CS_UNICODE));
551
    }
552
  }
553
  String *val_str(String *);
554
  void fix_length_and_dec();
555
  const char *func_name() const { return "convert"; }
556
  virtual void print(String *str, enum_query_type query_type);
557
};
558
559
class Item_func_set_collation :public Item_str_func
560
{
561
public:
562
  Item_func_set_collation(Item *a, Item *b) :Item_str_func(a,b) {};
563
  String *val_str(String *);
564
  void fix_length_and_dec();
565
  bool eq(const Item *item, bool binary_cmp) const;
566
  const char *func_name() const { return "collate"; }
567
  enum Functype functype() const { return COLLATE_FUNC; }
568
  virtual void print(String *str, enum_query_type query_type);
569
  Item_field *filed_for_view_update()
570
  {
571
    /* this function is transparent for view updating */
572
    return args[0]->filed_for_view_update();
573
  }
574
};
575
576
class Item_func_charset :public Item_str_func
577
{
578
public:
579
  Item_func_charset(Item *a) :Item_str_func(a) {}
580
  String *val_str(String *);
581
  const char *func_name() const { return "charset"; }
582
  void fix_length_and_dec()
583
  {
584
     collation.set(system_charset_info);
585
     max_length= 64 * collation.collation->mbmaxlen; // should be enough
586
     maybe_null= 0;
587
  };
588
  table_map not_null_tables() const { return 0; }
589
};
590
591
class Item_func_collation :public Item_str_func
592
{
593
public:
594
  Item_func_collation(Item *a) :Item_str_func(a) {}
595
  String *val_str(String *);
596
  const char *func_name() const { return "collation"; }
597
  void fix_length_and_dec()
598
  {
599
     collation.set(system_charset_info);
600
     max_length= 64 * collation.collation->mbmaxlen; // should be enough
601
     maybe_null= 0;
602
  };
603
  table_map not_null_tables() const { return 0; }
604
};
605
606
607
class Item_func_weight_string :public Item_str_func
608
{
609
  String tmp_value;
482 by Brian Aker
Remove uint.
610
  uint32_t flags;
611
  uint32_t nweights;
1 by brian
clean slate
612
public:
482 by Brian Aker
Remove uint.
613
  Item_func_weight_string(Item *a, uint32_t nweights_arg, uint32_t flags_arg)
1 by brian
clean slate
614
  :Item_str_func(a) { nweights= nweights_arg; flags= flags_arg; }
615
  const char *func_name() const { return "weight_string"; }
616
  String *val_str(String *);
617
  void fix_length_and_dec();
618
};
619
620
#define UUID_LENGTH (8+1+4+1+4+1+4+1+12)
621
class Item_func_uuid: public Item_str_func
622
{
623
public:
624
  Item_func_uuid(): Item_str_func() {}
625
  void fix_length_and_dec() {
626
    collation.set(system_charset_info);
627
    /*
628
       NOTE! uuid() should be changed to use 'ascii'
629
       charset when hex(), format(), md5(), etc, and implicit
630
       number-to-string conversion will use 'ascii'
631
    */
632
    max_length= UUID_LENGTH * system_charset_info->mbmaxlen;
633
  }
634
  const char *func_name() const{ return "uuid"; }
635
  String *val_str(String *);
636
};
637