1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/*
* Copyright (C) 2008 - 2012 Vivien Malerba <malerba@gnome-db.org>
* Copyright (C) 2009 Bas Driessen <bas.driessen@xobas.com>
* Copyright (C) 2009 Murray Cumming <murrayc@murrayc.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <libgda/sql-parser/gda-statement-struct.h>
/**
* gda_sql_value_stringify
* @value: a #GValue pointer
*
* Simplified version of gda_value_stringify().
*
* Returns: a new string
*/
gchar *
gda_sql_value_stringify (const GValue *value)
{
if (value && !GDA_VALUE_HOLDS_NULL (value)) {
if (g_value_type_transformable (G_VALUE_TYPE (value), G_TYPE_STRING)) {
GValue *string;
gchar *str;
string = g_value_init (g_new0 (GValue, 1), G_TYPE_STRING);
g_value_transform (value, string);
str = g_value_dup_string (string);
gda_value_free (string);
return str;
}
else {
GType type = G_VALUE_TYPE (value);
if (type == G_TYPE_DATE) {
GDate *date;
date = (GDate *) g_value_get_boxed (value);
if (date) {
if (g_date_valid (date))
return g_strdup_printf ("%04u-%02u-%02u",
g_date_get_year (date),
g_date_get_month (date),
g_date_get_day (date));
else
return g_strdup_printf ("%04u-%02u-%02u",
date->year, date->month, date->day);
}
else
return g_strdup ("0000-00-00");
}
else
return g_strdup ("<type not transformable to string>");
}
}
else
return g_strdup ("NULL");
}
/* Returns: @str */
gchar *
_remove_quotes (gchar *str)
{
glong total;
gchar *ptr;
glong offset = 0;
char delim;
if (!str)
return NULL;
delim = *str;
if ((delim != '\'') && (delim != '"'))
return str;
total = strlen (str);
if (str[total-1] == delim) {
/* string is correctly terminated by a double quote */
memmove (str, str+1, total-2);
total -=2;
}
else {
/* string is _not_ correctly terminated by a double quote */
memmove (str, str+1, total-1);
total -=1;
}
str[total] = 0;
ptr = (gchar *) str;
while (offset < total) {
/* we accept the "''" as a synonym of "\'" */
if (*ptr == delim) {
if (*(ptr+1) == delim) {
memmove (ptr+1, ptr+2, total - offset);
offset += 2;
}
else {
*str = 0;
return str;
}
}
else if (*ptr == '"') {
if (*(ptr+1) == '"') {
memmove (ptr+1, ptr+2, total - offset);
offset += 2;
}
else {
*str = 0;
return str;
}
}
else if (*ptr == '\\') {
if (*(ptr+1) == '\\') {
memmove (ptr+1, ptr+2, total - offset);
offset += 2;
}
else {
if (*(ptr+1) == delim) {
*ptr = delim;
memmove (ptr+1, ptr+2, total - offset);
offset += 2;
}
else {
*str = 0;
return str;
}
}
}
else
offset ++;
ptr++;
}
return str;
}
/**
* gda_sql_identifier_force_quotes
* @str: an SQL identifier
*
* Add double quotes around the @str identifier. This function is normally used only by database provider's
* implementation. Any double quote character is replaced by two double quote characters.
*
* For other uses, see gda_sql_identifier_quote().
*
* Since: 5.0
*/
gchar *
gda_sql_identifier_force_quotes (const gchar *str)
{
gchar *retval, *rptr;
const gchar *sptr;
gint len;
if (!str)
return NULL;
len = strlen (str);
retval = g_new (gchar, 2*len + 3);
*retval = '"';
for (rptr = retval+1, sptr = str; *sptr; sptr++, rptr++) {
if (*sptr == '"') {
*rptr = '"';
rptr++;
*rptr = *sptr;
}
else
*rptr = *sptr;
}
*rptr = '"';
rptr++;
*rptr = 0;
return retval;
}
gchar *
_json_quote_string (const gchar *str)
{
gchar *retval, *rptr;
const gchar *sptr;
gint len;
if (!str)
return g_strdup ("null");
len = strlen (str);
retval = g_new (gchar, 2*len + 3);
*retval = '"';
for (rptr = retval+1, sptr = str; *sptr; sptr++, rptr++) {
switch (*sptr) {
case '"':
*rptr = '\\';
rptr++;
*rptr = *sptr;
break;
case '\\':
*rptr = '\\';
rptr++;
*rptr = *sptr;
break;
case '/':
*rptr = '\\';
rptr++;
*rptr = *sptr;
break;
case '\b':
*rptr = '\\';
rptr++;
*rptr = 'b';
break;
case '\f':
*rptr = '\\';
rptr++;
*rptr = 'f';
break;
case '\n':
*rptr = '\\';
rptr++;
*rptr = 'n';
break;
case '\r':
*rptr = '\\';
rptr++;
*rptr = 'r';
break;
case '\t':
*rptr = '\\';
rptr++;
*rptr = 't';
break;
default:
*rptr = *sptr;
break;
}
}
*rptr = '"';
rptr++;
*rptr = 0;
return retval;
}
/*
** If X is a character that can be used in an identifier then
** IdChar(X) will be true. Otherwise it is false.
**
** For ASCII, any character with the high-order bit set is
** allowed in an identifier. For 7-bit characters,
** sqlite3IsIdChar[X] must be 1.
*/
static const char AsciiIdChar[] = {
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
};
#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && AsciiIdChar[c-0x20]))
gboolean
_string_is_identifier (const gchar *str)
{
const gchar *ptr;
gchar *endptr;
gchar c;
if (!str || !(*str))
return FALSE;
if ((*str == '"') || (*str == '`'))
ptr = str + 1;
else
ptr = str;
for (; IdChar(*ptr) ||
(*ptr == '*') ||
(*ptr == '.') ||
(*ptr == '-') ||
((*ptr == '"') && (ptr[1] == '"')) ||
(((*ptr == '"') || (*ptr == '`')) && ptr[1] == 0);
ptr++) {
if ((*ptr == '"') && (ptr[1] == '"'))
ptr++;
}
if (*ptr)
return FALSE;
if (((*str == '"') && (ptr[-1] == '"')) ||
((*str == '`') && (ptr[-1] == '`')))
return TRUE;
/* @str is composed only of character that can be used in an identifier */
g_ascii_strtod (str, &endptr);
if (!*endptr)
/* @str is a number */
return FALSE;
return TRUE;
}
/**
* gda_sql_identifier_prepare_for_compare
* @str: a quoted string
*
* Prepares @str to be compared:
* <itemizedlist>
* <listitem><para>if surrounded by double quotes or single quotes, then just remove the quotes</para></listitem>
* <listitem><para>otherwise convert to lower case</para></listitem>
* </itemizedlist>
*
* The quoted string:
* <itemizedlist>
* <listitem><para>must start and finish with the same single or double quotes character</para></listitem>
* <listitem><para>can contain the delimiter character (the single or double quotes) in the string if every instance
* of it is preceeded with a backslash character or with the delimiter character itself</para></listitem>
* </itemizedlist>
*
* This function is normally used only by database provider's implementation.
*
* WARNING: @str must NOT be a composed identifier (<part1>."<part2>" for example)
*
* Returns: @str
*
* Since: 5.0
*/
gchar *
gda_sql_identifier_prepare_for_compare (gchar *str)
{
if (!str)
return NULL;
if ((*str == '"') || (*str == '\''))
return _remove_quotes (str);
else {
gchar *ptr;
for (ptr = str; *ptr; ptr++)
*ptr = g_ascii_tolower (*ptr);
return str;
}
}
/*
* Reuses @str and fills in @remain and @last
*
* @str "swallowed" by the function and must be allocated memory, and @remain and @last are
* also allocated memory.
*
* Accepted notations for each individual part:
* - aaa
* - "aaa"
*
* So "aaa".bbb, aaa.bbb, "aaa"."bbb", aaa."bbb" are Ok.
*
* Returns TRUE:
* if @str has the <part1>.<part2> form, then @last will contain <part2> and @remain will contain <part1>
* if @str has the <part1> form, then @last will contain <part1> and @remain will contain NULL
*
* Returns FALSE (in any case @str is also freed)
* if @str is NULL:
* if @str is "":
* if @str is malformed:
* @last and @remain will contain NULL
*/
gboolean
_split_identifier_string (gchar *str, gchar **remain, gchar **last)
{
gchar *ptr;
gboolean inq = FALSE;
gint len;
*remain = NULL;
*last = NULL;
if (!str)
return FALSE;
g_strchomp (str);
if (!*str) {
g_free (str);
return FALSE;
}
len = strlen (str) - 1;
if (len > 1) {
if (((str[len] == '"') && (str[len-1] == '.')) ||
(str[len] == '.')) {
g_free (str);
return FALSE;
}
}
if (((str[0] == '"') && (str[1] == '.')) ||
(str[0] == '.')) {
g_free (str);
return FALSE;
}
for (ptr = str + strlen (str) - 1; ptr >= str; ptr--) {
if (*ptr == '"') {
if ((ptr > str) && (ptr[-1] == '"')) {
ptr--;
continue;
}
inq = !inq;
}
else if ((*ptr == '.') && !inq) {
*ptr = 0;
*remain = str;
*last = g_strdup (ptr+1);
break;
}
}
if (!(*last) && !(*remain))
*last = str;
if (*last && !_string_is_identifier (*last)) {
g_free (*last);
*last = NULL;
g_free (*remain);
*remain = NULL;
return FALSE;
}
return TRUE;
}
|