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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
/*
* Copyright (C) 2007 Vivien Malerba
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, 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 && G_IS_VALUE (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);
g_value_unset (string);
g_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 correclty terminated by a double quote */
g_memmove (str, str+1, total-2);
total -=2;
}
else {
/* string is _not_ correclty terminated by a double quote */
g_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) {
g_memmove (ptr+1, ptr+2, total - offset);
offset += 2;
}
else {
*str = 0;
return str;
}
}
if (*ptr == '\\') {
if (*(ptr+1) == '\\') {
g_memmove (ptr+1, ptr+2, total - offset);
offset += 2;
}
else {
if (*(ptr+1) == delim) {
*ptr = delim;
g_memmove (ptr+1, ptr+2, total - offset);
offset += 2;
}
else {
*str = 0;
return str;
}
}
}
else
offset ++;
ptr++;
}
return str;
}
/**
* gda_sql_identifier_add_quotes
* @str: an SQL identifier
*
* Add double quotes around the @str identifier. Use the gda_sql_identifier_needs_quotes()
* function to tell if an identifier needs to be quoted.
*
* Returns: a new string
*
* Deprecated: 4.0.3: Use gda_sql_identifier_quote() instead.
*/
gchar *
gda_sql_identifier_add_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;
gdouble d;
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] == 0);
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 */
d = g_ascii_strtod (str, &endptr);
if (!*endptr)
/* @str is a number */
return FALSE;
return TRUE;
}
/**
* gda_sql_identifier_needs_quotes
* @str: an SQL identifier
*
* Tells if @str needs to be quoted before using it in an SQL statement. To actually add quotes,
* use gda_sql_identifier_add_quotes().
*
* To determine if quotes are needed: the following rules are applied:
* <itemizedlist>
* <listitem><para>If the 1st character is a digit, then %TRUE is returned</para></listitem>
* <listitem><para>If there are mixed lower and upper case letters, then %TRUE is returned</para></listitem>
* <listitem><para>If there are other characters than digits, letters and the '_', '$' and '#', then %TRUE is returned</para></listitem>
* <listitem><para>Otherwise %FALSE is returned</para></listitem>
* </itemizedlist>
*
* Returns: TRUE if @str needs some quotes
*
* Deprecated: 4.0.3: Not needed anymore because of the gda_sql_identifier_quote()
*/
gboolean
gda_sql_identifier_needs_quotes (const gchar *str)
{
const gchar *ptr;
gchar icase = 0;
g_return_val_if_fail (str, FALSE);
for (ptr = str; *ptr; ptr++) {
/* quote if 1st char is a number */
if ((*ptr <= '9') && (*ptr >= '0')) {
if (ptr == str)
return TRUE;
continue;
}
if ((*ptr >= 'A') && (*ptr <= 'Z')) {
if (icase == 0) /* first alpha char encountered */
icase = 'U';
else if (icase == 'L') /* @str has mixed case */
return TRUE;
continue;
}
if ((*ptr >= 'a') && (*ptr <= 'z')) {
if (icase == 0) /* first alpha char encountered */
icase = 'L';
else if (icase == 'U')
return TRUE; /* @str has mixed case */
continue;
}
if ((*ptr != '$') && (*ptr != '_') && (*ptr != '#'))
return TRUE;
}
return FALSE;
}
/**
* gda_sql_identifier_remove_quotes
* @str: a quoted string
*
* Prepares @str to be compared:
* - if surrounded by double quotes or single quotes, then just remove the quotes
* - otherwise convert to lower case
*
* 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>
*
* WARNING: @str must NOT be a composed identifier (<part1>."<part2>" for example)
*
* Returns: @str
*
* Deprecated: 4.0.3: Not needed anymore because of the gda_sql_identifier_quote()
*/
gchar *
gda_sql_identifier_remove_quotes (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 == '"')
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;
}
|