~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/quote.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 */
19
 
 
20
 
#include "config.h"
21
 
 
22
 
#include <drizzled/function/str/quote.h>
23
 
 
24
 
namespace drizzled
25
 
{
26
 
 
27
 
#define get_esc_bit(mask, num) (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7))
28
 
 
29
 
/**
30
 
  QUOTE() function returns argument string in single quotes suitable for
31
 
  using in a SQL statement.
32
 
 
33
 
  Adds a \\ before all characters that needs to be escaped in a SQL string.
34
 
  We also escape '^Z' (END-OF-FILE in windows) to avoid probelms when
35
 
  running commands from a file in windows.
36
 
 
37
 
  This function is very useful when you want to generate SQL statements.
38
 
 
39
 
  @note
40
 
    QUOTE(NULL) returns the string 'NULL' (4 letters, without quotes).
41
 
 
42
 
  @retval
43
 
    str    Quoted string
44
 
  @retval
45
 
    NULL           Out of memory.
46
 
*/
47
 
 
48
 
String *Item_func_quote::val_str(String *str)
49
 
{
50
 
  assert(fixed == 1);
51
 
  /*
52
 
    Bit mask that has 1 for set for the position of the following characters:
53
 
    0, \, ' and ^Z
54
 
  */
55
 
 
56
 
  static unsigned char escmask[32]=
57
 
  {
58
 
    0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
59
 
    0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
60
 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61
 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
62
 
  };
63
 
 
64
 
  char *from, *to, *end, *start;
65
 
  String *arg= args[0]->val_str(str);
66
 
  uint32_t arg_length, new_length;
67
 
  if (!arg)                                     // Null argument
68
 
  {
69
 
    /* Return the string 'NULL' */
70
 
    str->copy(STRING_WITH_LEN("NULL"), collation.collation);
71
 
    null_value= 0;
72
 
    return str;
73
 
  }
74
 
 
75
 
  arg_length= arg->length();
76
 
  new_length= arg_length+2; /* for beginning and ending ' signs */
77
 
 
78
 
  for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
79
 
    new_length+= get_esc_bit(escmask, (unsigned char) *from);
80
 
 
81
 
  if (tmp_value.alloc(new_length))
82
 
    goto null;
83
 
 
84
 
  /*
85
 
    We replace characters from the end to the beginning
86
 
  */
87
 
  to= (char*) tmp_value.ptr() + new_length - 1;
88
 
  *to--= '\'';
89
 
  for (start= (char*) arg->ptr(),end= start + arg_length; end-- != start; to--)
90
 
  {
91
 
    /*
92
 
      We can't use the bitmask here as we want to replace \O and ^Z with 0
93
 
      and Z
94
 
    */
95
 
    switch (*end)  {
96
 
    case 0:
97
 
      *to--= '0';
98
 
      *to=   '\\';
99
 
      break;
100
 
    case '\032':
101
 
      *to--= 'Z';
102
 
      *to=   '\\';
103
 
      break;
104
 
    case '\'':
105
 
    case '\\':
106
 
      *to--= *end;
107
 
      *to=   '\\';
108
 
      break;
109
 
    default:
110
 
      *to= *end;
111
 
      break;
112
 
    }
113
 
  }
114
 
  *to= '\'';
115
 
  tmp_value.length(new_length);
116
 
  tmp_value.set_charset(collation.collation);
117
 
  null_value= 0;
118
 
  return &tmp_value;
119
 
 
120
 
null:
121
 
  null_value= 1;
122
 
  return 0;
123
 
}
124
 
 
125
 
} /* namespace drizzled */