574.3.9
by Lee
moving functions from item_strfunc to functions/str directory |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
1999.6.1
by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file |
4 |
* Copyright (C) 2008 Sun Microsystems, Inc.
|
574.3.9
by Lee
moving functions from item_strfunc to functions/str directory |
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 |
||
2173.2.1
by Monty Taylor
Fixes incorrect usage of include |
20 |
#include <config.h> |
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
21 |
|
1759.3.23
by Stewart Smith
move QUOTE() function to string_functions plugin |
22 |
#include "quote.h" |
574.3.9
by Lee
moving functions from item_strfunc to functions/str directory |
23 |
|
2154.2.4
by Brian Aker
This fixes 716459 |
24 |
#include <drizzled/lex_string.h> |
25 |
||
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
26 |
namespace drizzled |
27 |
{
|
|
574.3.9
by Lee
moving functions from item_strfunc to functions/str directory |
28 |
|
1507.1.1
by Sanders King
Changes to quote.cc and mf_iocache.cc: convert macros to inline functions; reformat comments to suit Doxygen |
29 |
inline
|
30 |
static uint32_t get_esc_bit(unsigned char *mask, unsigned char num) |
|
31 |
{
|
|
32 |
return (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7)); |
|
33 |
}
|
|
574.3.9
by Lee
moving functions from item_strfunc to functions/str directory |
34 |
|
35 |
/**
|
|
1507.1.1
by Sanders King
Changes to quote.cc and mf_iocache.cc: convert macros to inline functions; reformat comments to suit Doxygen |
36 |
* @brief
|
37 |
* Returns the argument string in single quotes suitable for using in a SQL statement.
|
|
38 |
*
|
|
39 |
* @detail
|
|
40 |
* Adds a \\ before all characters that needs to be escaped in a SQL string.
|
|
41 |
* We also escape '^Z' (END-OF-FILE in windows) to avoid problems when
|
|
42 |
* running commands from a file in windows.
|
|
43 |
*
|
|
44 |
* This function is very useful when you want to generate SQL statements.
|
|
45 |
*
|
|
46 |
* @note
|
|
47 |
* val_str(NULL) returns the string 'NULL' (4 letters, without quotes).
|
|
48 |
*
|
|
49 |
* @retval str Quoted string
|
|
50 |
* @retval NULL Out of memory.
|
|
51 |
*/
|
|
574.3.9
by Lee
moving functions from item_strfunc to functions/str directory |
52 |
String *Item_func_quote::val_str(String *str) |
53 |
{
|
|
54 |
assert(fixed == 1); |
|
55 |
/*
|
|
56 |
Bit mask that has 1 for set for the position of the following characters:
|
|
57 |
0, \, ' and ^Z
|
|
58 |
*/
|
|
59 |
||
60 |
static unsigned char escmask[32]= |
|
61 |
{
|
|
62 |
0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00, |
|
63 |
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, |
|
64 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
65 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
66 |
};
|
|
67 |
||
68 |
char *from, *to, *end, *start; |
|
69 |
String *arg= args[0]->val_str(str); |
|
70 |
uint32_t arg_length, new_length; |
|
71 |
if (!arg) // Null argument |
|
72 |
{
|
|
73 |
/* Return the string 'NULL' */
|
|
74 |
str->copy(STRING_WITH_LEN("NULL"), collation.collation); |
|
75 |
null_value= 0; |
|
76 |
return str; |
|
77 |
}
|
|
78 |
||
79 |
arg_length= arg->length(); |
|
80 |
new_length= arg_length+2; /* for beginning and ending ' signs */ |
|
81 |
||
82 |
for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++) |
|
83 |
new_length+= get_esc_bit(escmask, (unsigned char) *from); |
|
84 |
||
85 |
if (tmp_value.alloc(new_length)) |
|
86 |
goto null; |
|
87 |
||
88 |
/*
|
|
89 |
We replace characters from the end to the beginning
|
|
90 |
*/
|
|
91 |
to= (char*) tmp_value.ptr() + new_length - 1; |
|
92 |
*to--= '\''; |
|
93 |
for (start= (char*) arg->ptr(),end= start + arg_length; end-- != start; to--) |
|
94 |
{
|
|
95 |
/*
|
|
96 |
We can't use the bitmask here as we want to replace \O and ^Z with 0
|
|
97 |
and Z
|
|
98 |
*/
|
|
99 |
switch (*end) { |
|
100 |
case 0: |
|
101 |
*to--= '0'; |
|
102 |
*to= '\\'; |
|
103 |
break; |
|
104 |
case '\032': |
|
105 |
*to--= 'Z'; |
|
106 |
*to= '\\'; |
|
107 |
break; |
|
108 |
case '\'': |
|
109 |
case '\\': |
|
110 |
*to--= *end; |
|
111 |
*to= '\\'; |
|
112 |
break; |
|
113 |
default: |
|
114 |
*to= *end; |
|
115 |
break; |
|
116 |
}
|
|
117 |
}
|
|
118 |
*to= '\''; |
|
119 |
tmp_value.length(new_length); |
|
120 |
tmp_value.set_charset(collation.collation); |
|
121 |
null_value= 0; |
|
122 |
return &tmp_value; |
|
123 |
||
124 |
null: |
|
125 |
null_value= 1; |
|
126 |
return 0; |
|
127 |
}
|
|
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
128 |
|
129 |
} /* namespace drizzled */ |