~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/string_functions/quote.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

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