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