2001-05-23 15:26:42 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Dynamic SQL runtime support
|
2003-10-05 08:37:26 +02:00
|
|
|
* MODULE: preparse.cpp
|
2001-05-23 15:26:42 +02:00
|
|
|
* DESCRIPTION: Dynamic SQL pre parser / parser on client side.
|
|
|
|
* This module will probably change to a YACC parser.
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Interbase Public
|
|
|
|
* License Version 1.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at http://www.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Inprise Corporation
|
|
|
|
* and its predecessors. Portions created by Inprise Corporation are
|
|
|
|
* Copyright (C) Inprise Corporation.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
2001-07-30 01:43:24 +02:00
|
|
|
#include "firebird.h"
|
2001-05-23 15:26:42 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "../jrd/common.h"
|
2003-11-08 00:27:24 +01:00
|
|
|
#include "../jrd/ibase.h"
|
2001-05-23 15:26:42 +02:00
|
|
|
#include "../dsql/chars.h"
|
2003-10-01 12:58:07 +02:00
|
|
|
#include "../dsql/sqlda.h"
|
2001-05-23 15:26:42 +02:00
|
|
|
#include "../dsql/prepa_proto.h"
|
|
|
|
#include "../dsql/utld_proto.h"
|
|
|
|
#include "../jrd/gds_proto.h"
|
|
|
|
|
2003-09-28 02:36:28 +02:00
|
|
|
enum pp_vals {
|
|
|
|
PP_CREATE = 0,
|
|
|
|
PP_DATABASE = 1,
|
|
|
|
PP_SCHEMA = 2,
|
|
|
|
PP_PAGE_SIZE = 3,
|
|
|
|
PP_USER = 4,
|
|
|
|
PP_PASSWORD = 5,
|
|
|
|
PP_PAGESIZE = 6,
|
|
|
|
PP_LENGTH = 7,
|
|
|
|
PP_PAGES = 8,
|
|
|
|
PP_PAGE = 9,
|
|
|
|
PP_SET = 10,
|
|
|
|
PP_NAMES = 11
|
|
|
|
};
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
|
2003-09-28 02:36:28 +02:00
|
|
|
const int MAX_TOKEN_SIZE = 1024;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
|
2003-10-05 08:37:26 +02:00
|
|
|
// use stuff_dpb in place of STUFF to avoid confusion with BLR STUFF
|
|
|
|
// macro defined in dsql.h
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-10-05 08:37:26 +02:00
|
|
|
static inline void stuff_dpb(SCHAR*& dpb, const SCHAR blr)
|
|
|
|
{
|
2003-10-01 20:11:23 +02:00
|
|
|
*dpb++ = blr;
|
|
|
|
}
|
|
|
|
|
2003-10-05 08:37:26 +02:00
|
|
|
static inline void stuff_dpb_long(SCHAR*& dpb, const SLONG blr)
|
|
|
|
{
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, blr);
|
|
|
|
stuff_dpb(dpb, blr >> 8);
|
|
|
|
stuff_dpb(dpb, blr >> 16);
|
|
|
|
stuff_dpb(dpb, blr >> 24);
|
|
|
|
}
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-11-18 08:58:35 +01:00
|
|
|
static void generate_error(ISC_STATUS*, const SCHAR*, SSHORT, SSHORT);
|
2003-11-28 07:48:34 +01:00
|
|
|
static SSHORT get_next_token(const SCHAR**, const SCHAR*, SCHAR*, USHORT*);
|
|
|
|
static SSHORT get_token(ISC_STATUS*, SSHORT, bool, const SCHAR**,
|
|
|
|
const SCHAR* const, SCHAR*, USHORT*);
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-10-01 12:58:07 +02:00
|
|
|
struct pp_table {
|
2001-05-23 15:26:42 +02:00
|
|
|
SCHAR symbol[10];
|
|
|
|
SSHORT length;
|
|
|
|
SSHORT code;
|
2003-10-01 12:58:07 +02:00
|
|
|
};
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-10-01 12:58:07 +02:00
|
|
|
static const pp_table pp_symbols[] = {
|
2002-10-16 10:40:01 +02:00
|
|
|
{"CREATE", 6, PP_CREATE},
|
|
|
|
{"DATABASE", 8, PP_DATABASE},
|
|
|
|
{"SCHEMA", 6, PP_SCHEMA},
|
|
|
|
{"PAGE_SIZE", 9, PP_PAGE_SIZE},
|
|
|
|
{"USER", 4, PP_USER},
|
|
|
|
{"PASSWORD", 8, PP_PASSWORD},
|
|
|
|
{"PAGESIZE", 8, PP_PAGESIZE},
|
|
|
|
{"LENGTH", 6, PP_LENGTH},
|
|
|
|
{"PAGES", 5, PP_PAGES},
|
|
|
|
{"PAGE", 4, PP_PAGE},
|
|
|
|
{"SET", 3, PP_SET},
|
|
|
|
{"NAMES", 5, PP_NAMES},
|
2002-01-04 12:34:22 +01:00
|
|
|
{"", 0, 0}
|
2001-05-23 15:26:42 +02:00
|
|
|
};
|
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
// define the tokens
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-09-28 02:36:28 +02:00
|
|
|
enum token_vals {
|
|
|
|
NO_MORE_TOKENS = -1,
|
|
|
|
TOKEN_TOO_LONG = -2,
|
|
|
|
UNEXPECTED_END_OF_COMMAND = -3,
|
|
|
|
UNEXPECTED_TOKEN = -4,
|
|
|
|
STRING = 257,
|
|
|
|
NUMERIC = 258,
|
|
|
|
SYMBOL = 259
|
|
|
|
};
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-02-15 04:01:51 +01:00
|
|
|
/**
|
|
|
|
|
|
|
|
PREPARSE_execute
|
|
|
|
|
|
|
|
@brief
|
|
|
|
|
|
|
|
@param user_status
|
|
|
|
@param db_handle
|
|
|
|
@param trans_handle
|
|
|
|
@param stmt_length
|
|
|
|
@param stmt
|
|
|
|
@param stmt_eaten
|
|
|
|
@param dialect
|
|
|
|
|
|
|
|
**/
|
2003-11-18 08:58:35 +01:00
|
|
|
bool PREPARSE_execute(
|
2003-10-05 08:37:26 +02:00
|
|
|
ISC_STATUS* user_status,
|
2004-05-03 01:06:37 +02:00
|
|
|
FB_API_HANDLE* db_handle,
|
|
|
|
FB_API_HANDLE* trans_handle,
|
2003-10-03 04:00:40 +02:00
|
|
|
USHORT stmt_length,
|
2003-11-28 07:48:34 +01:00
|
|
|
const SCHAR* stmt,
|
2003-11-18 08:58:35 +01:00
|
|
|
bool* stmt_eaten,
|
2003-10-03 04:00:40 +02:00
|
|
|
USHORT dialect)
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
2003-11-18 08:58:35 +01:00
|
|
|
char* const token = (SCHAR *) gds__alloc((SLONG) MAX_TOKEN_SIZE + 1);
|
2004-03-07 08:58:55 +01:00
|
|
|
// FREE: by return(s) in this procedure
|
2003-11-28 07:48:34 +01:00
|
|
|
if (!token) { // NOMEM:
|
2001-05-23 15:26:42 +02:00
|
|
|
user_status[0] = isc_arg_gds;
|
|
|
|
user_status[1] = isc_virmemexh;
|
|
|
|
user_status[2] = isc_arg_end;
|
2003-11-18 08:58:35 +01:00
|
|
|
return false;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!stmt_length)
|
|
|
|
stmt_length = strlen(stmt);
|
2003-11-18 08:58:35 +01:00
|
|
|
const char* const stmt_end = stmt + stmt_length;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-11-18 08:58:35 +01:00
|
|
|
USHORT token_length;
|
2003-09-04 23:26:15 +02:00
|
|
|
if (get_token(user_status, SYMBOL, false, &stmt, stmt_end, token,
|
2001-05-23 15:26:42 +02:00
|
|
|
&token_length) ||
|
2002-10-16 10:40:01 +02:00
|
|
|
token_length != pp_symbols[PP_CREATE].length ||
|
2003-09-04 23:26:15 +02:00
|
|
|
strcmp(token, pp_symbols[PP_CREATE].symbol))
|
|
|
|
{
|
2004-09-26 13:23:32 +02:00
|
|
|
gds__free(token);
|
2003-11-18 08:58:35 +01:00
|
|
|
return false;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
2003-09-04 23:26:15 +02:00
|
|
|
if (get_token(user_status, SYMBOL, false, &stmt, stmt_end, token,
|
2001-05-23 15:26:42 +02:00
|
|
|
&token_length) ||
|
2002-10-16 10:40:01 +02:00
|
|
|
(token_length != pp_symbols[PP_DATABASE].length &&
|
|
|
|
token_length != pp_symbols[PP_SCHEMA].length) ||
|
|
|
|
(strcmp(token, pp_symbols[PP_DATABASE].symbol) &&
|
2003-09-04 23:26:15 +02:00
|
|
|
strcmp(token, pp_symbols[PP_SCHEMA].symbol)))
|
|
|
|
{
|
2004-09-26 13:23:32 +02:00
|
|
|
gds__free(token);
|
2003-11-18 08:58:35 +01:00
|
|
|
return false;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
2003-09-04 23:26:15 +02:00
|
|
|
if (get_token(user_status, STRING, false, &stmt, stmt_end, token,
|
|
|
|
&token_length))
|
|
|
|
{
|
2004-09-26 13:23:32 +02:00
|
|
|
gds__free(token);
|
2003-11-18 08:58:35 +01:00
|
|
|
return true;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
2003-11-18 08:58:35 +01:00
|
|
|
TEXT file_name[MAX_TOKEN_SIZE + 1];
|
2001-05-23 15:26:42 +02:00
|
|
|
strcpy(file_name, token);
|
2003-11-18 08:58:35 +01:00
|
|
|
*stmt_eaten = false;
|
|
|
|
char* const dpb_array = (SCHAR*) gds__alloc((SLONG) 2 * MAX_TOKEN_SIZE + 25);
|
|
|
|
char* dpb = dpb_array;
|
2004-03-07 08:58:55 +01:00
|
|
|
// FREE: by following return(s) in this procedure
|
2003-11-28 07:48:34 +01:00
|
|
|
if (!dpb_array) { // NOMEM:
|
2001-05-23 15:26:42 +02:00
|
|
|
user_status[0] = isc_arg_gds;
|
|
|
|
user_status[1] = isc_virmemexh;
|
|
|
|
user_status[2] = isc_arg_end;
|
2004-09-26 13:23:32 +02:00
|
|
|
gds__free(token);
|
2003-11-18 08:58:35 +01:00
|
|
|
return true;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
2003-11-08 00:27:24 +01:00
|
|
|
stuff_dpb(dpb, isc_dpb_version1);
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, isc_dpb_overwrite);
|
|
|
|
stuff_dpb(dpb, 1);
|
|
|
|
stuff_dpb(dpb, 0);
|
|
|
|
stuff_dpb(dpb, isc_dpb_sql_dialect);
|
|
|
|
stuff_dpb(dpb, 4);
|
2003-10-05 08:37:26 +02:00
|
|
|
stuff_dpb_long(dpb, dialect);
|
2003-11-18 08:58:35 +01:00
|
|
|
|
|
|
|
SLONG page_size = 0;
|
|
|
|
bool matched;
|
2001-05-23 15:26:42 +02:00
|
|
|
do {
|
2003-11-18 08:58:35 +01:00
|
|
|
const SSHORT result = get_next_token(&stmt, stmt_end, token, &token_length);
|
2001-05-23 15:26:42 +02:00
|
|
|
if (result == NO_MORE_TOKENS) {
|
2003-11-18 08:58:35 +01:00
|
|
|
*stmt_eaten = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (result < 0)
|
|
|
|
break;
|
|
|
|
|
2003-09-04 23:26:15 +02:00
|
|
|
matched = false;
|
2003-11-18 08:58:35 +01:00
|
|
|
for (int i = 3; pp_symbols[i].length && !matched; i++) {
|
2001-05-23 15:26:42 +02:00
|
|
|
if (token_length == pp_symbols[i].length &&
|
2003-11-18 08:58:35 +01:00
|
|
|
!strcmp(token, pp_symbols[i].symbol))
|
|
|
|
{
|
|
|
|
bool get_out = false;
|
2004-11-08 04:14:17 +01:00
|
|
|
const SCHAR* ch;
|
2003-11-18 08:58:35 +01:00
|
|
|
SSHORT l;
|
|
|
|
// CVC: What's strange, this routine doesn't check token_length
|
|
|
|
// but it proceeds blindly, trying to exhaust the token itself.
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
switch (pp_symbols[i].code) {
|
2002-10-16 10:40:01 +02:00
|
|
|
case PP_PAGE_SIZE:
|
|
|
|
case PP_PAGESIZE:
|
2003-09-04 23:26:15 +02:00
|
|
|
if (get_token(user_status, '=', true, &stmt, stmt_end,
|
2001-05-23 15:26:42 +02:00
|
|
|
token, &token_length) ||
|
2003-09-04 23:26:15 +02:00
|
|
|
get_token(user_status, NUMERIC, false, &stmt,
|
|
|
|
stmt_end, token, &token_length))
|
|
|
|
{
|
|
|
|
get_out = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
page_size = atol(token);
|
2003-11-08 00:27:24 +01:00
|
|
|
stuff_dpb(dpb, isc_dpb_page_size);
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, 4);
|
2003-10-05 08:37:26 +02:00
|
|
|
stuff_dpb_long(dpb, page_size);
|
2003-09-04 23:26:15 +02:00
|
|
|
matched = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
2002-10-16 10:40:01 +02:00
|
|
|
case PP_USER:
|
2003-09-04 23:26:15 +02:00
|
|
|
if (get_token(user_status, STRING, false, &stmt, stmt_end,
|
|
|
|
token, &token_length))
|
|
|
|
{
|
|
|
|
get_out = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-11-08 00:27:24 +01:00
|
|
|
stuff_dpb(dpb, isc_dpb_user_name);
|
2001-05-23 15:26:42 +02:00
|
|
|
l = token_length;
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, l);
|
2001-05-23 15:26:42 +02:00
|
|
|
ch = token;
|
|
|
|
while (*ch)
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, *ch++);
|
2003-09-04 23:26:15 +02:00
|
|
|
matched = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
2002-10-16 10:40:01 +02:00
|
|
|
case PP_PASSWORD:
|
2003-09-04 23:26:15 +02:00
|
|
|
if (get_token(user_status, STRING, false, &stmt, stmt_end,
|
|
|
|
token, &token_length))
|
|
|
|
{
|
|
|
|
get_out = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-11-08 00:27:24 +01:00
|
|
|
stuff_dpb(dpb, isc_dpb_password);
|
2001-05-23 15:26:42 +02:00
|
|
|
l = token_length;
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, l);
|
2001-05-23 15:26:42 +02:00
|
|
|
ch = token;
|
|
|
|
while (*ch)
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, *ch++);
|
2003-09-04 23:26:15 +02:00
|
|
|
matched = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
2002-10-16 10:40:01 +02:00
|
|
|
case PP_SET:
|
2003-09-04 23:26:15 +02:00
|
|
|
if (get_token(user_status, SYMBOL, false, &stmt, stmt_end,
|
2001-05-23 15:26:42 +02:00
|
|
|
token, &token_length) ||
|
2002-10-16 10:40:01 +02:00
|
|
|
token_length != pp_symbols[PP_NAMES].length ||
|
|
|
|
strcmp(token, pp_symbols[PP_NAMES].symbol) ||
|
2003-09-04 23:26:15 +02:00
|
|
|
get_token(user_status, STRING, false, &stmt, stmt_end,
|
|
|
|
token, &token_length))
|
|
|
|
{
|
|
|
|
get_out = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-11-08 00:27:24 +01:00
|
|
|
stuff_dpb(dpb, isc_dpb_lc_ctype);
|
2001-05-23 15:26:42 +02:00
|
|
|
l = token_length;
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, l);
|
2001-05-23 15:26:42 +02:00
|
|
|
ch = token;
|
|
|
|
while (*ch)
|
2003-10-01 20:11:23 +02:00
|
|
|
stuff_dpb(dpb, *ch++);
|
2003-09-04 23:26:15 +02:00
|
|
|
matched = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
2002-10-16 10:40:01 +02:00
|
|
|
case PP_LENGTH:
|
2003-11-28 07:48:34 +01:00
|
|
|
// Skip a token for value
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-09-04 23:26:15 +02:00
|
|
|
if (get_token(user_status, '=', true, &stmt, stmt_end,
|
2001-05-23 15:26:42 +02:00
|
|
|
token, &token_length) ||
|
2003-09-04 23:26:15 +02:00
|
|
|
get_token(user_status, NUMERIC, false, &stmt,
|
|
|
|
stmt_end, token, &token_length))
|
|
|
|
{
|
|
|
|
get_out = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-09-04 23:26:15 +02:00
|
|
|
matched = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
2002-10-16 10:40:01 +02:00
|
|
|
case PP_PAGE:
|
|
|
|
case PP_PAGES:
|
2003-09-04 23:26:15 +02:00
|
|
|
matched = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_out) {
|
2004-09-26 13:23:32 +02:00
|
|
|
gds__free(dpb_array);
|
|
|
|
gds__free(token);
|
2003-11-18 08:58:35 +01:00
|
|
|
return true;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (matched);
|
|
|
|
|
2003-11-18 08:58:35 +01:00
|
|
|
const USHORT dpb_len = dpb - dpb_array;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
// This code is because 3.3 server does not recognize isc_dpb_overwrite.
|
2004-05-03 01:06:37 +02:00
|
|
|
FB_API_HANDLE temp_db_handle = 0;
|
2003-08-30 03:54:25 +02:00
|
|
|
if (!isc_attach_database(user_status, 0, file_name, &temp_db_handle,
|
2001-05-23 15:26:42 +02:00
|
|
|
dpb_len, dpb_array) ||
|
2004-09-26 13:23:32 +02:00
|
|
|
user_status[1] != isc_io_error)
|
|
|
|
{
|
2003-11-18 08:58:35 +01:00
|
|
|
if (!user_status[1]) {
|
|
|
|
// Swallow status from detach.
|
|
|
|
ISC_STATUS_ARRAY temp_status;
|
2001-05-23 15:26:42 +02:00
|
|
|
isc_detach_database(temp_status, &temp_db_handle);
|
2003-11-18 08:58:35 +01:00
|
|
|
}
|
2003-11-08 00:27:24 +01:00
|
|
|
if (!user_status[1] || user_status[1] == isc_bad_db_format) {
|
|
|
|
user_status[0] = isc_arg_gds;
|
|
|
|
user_status[1] = isc_io_error;
|
|
|
|
user_status[2] = isc_arg_string;
|
2003-04-10 08:32:58 +02:00
|
|
|
user_status[3] = (ISC_STATUS) "open";
|
2003-11-08 00:27:24 +01:00
|
|
|
user_status[4] = isc_arg_string;
|
2003-04-10 08:32:58 +02:00
|
|
|
user_status[5] = (ISC_STATUS) file_name;
|
2003-11-08 00:27:24 +01:00
|
|
|
user_status[6] = isc_arg_gds;
|
|
|
|
user_status[7] = isc_db_or_file_exists;
|
|
|
|
user_status[8] = isc_arg_end;
|
2001-05-23 15:26:42 +02:00
|
|
|
UTLD_save_status_strings(user_status);
|
|
|
|
}
|
|
|
|
if (dpb_array)
|
2004-09-26 13:23:32 +02:00
|
|
|
gds__free(dpb_array);
|
|
|
|
gds__free(token);
|
2003-11-18 08:58:35 +01:00
|
|
|
return true;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_create_database(user_status, 0, file_name,
|
2002-12-16 16:38:26 +01:00
|
|
|
(db_handle), dpb_len,
|
2001-05-23 15:26:42 +02:00
|
|
|
dpb_array, 0);
|
|
|
|
|
|
|
|
if (dpb_array)
|
2004-09-26 13:23:32 +02:00
|
|
|
gds__free(dpb_array);
|
|
|
|
gds__free(token);
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-11-18 08:58:35 +01:00
|
|
|
return true;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-15 04:01:51 +01:00
|
|
|
/**
|
|
|
|
|
|
|
|
generate_error
|
|
|
|
|
|
|
|
@brief
|
|
|
|
|
|
|
|
@param user_status
|
|
|
|
@param token
|
|
|
|
@param error
|
|
|
|
@param result
|
|
|
|
|
|
|
|
**/
|
2001-05-23 15:26:42 +02:00
|
|
|
static void generate_error(
|
2003-11-18 08:58:35 +01:00
|
|
|
ISC_STATUS* user_status,
|
|
|
|
const SCHAR* token, SSHORT error, SSHORT result)
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
|
|
|
TEXT err_string[MAX_TOKEN_SIZE + 3];
|
|
|
|
|
2003-11-08 00:27:24 +01:00
|
|
|
user_status[0] = isc_arg_gds;
|
|
|
|
user_status[1] = isc_sqlerr;
|
|
|
|
user_status[2] = isc_arg_number;
|
2001-05-23 15:26:42 +02:00
|
|
|
user_status[3] = -104;
|
2003-11-08 00:27:24 +01:00
|
|
|
user_status[4] = isc_arg_gds;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
switch (error) {
|
|
|
|
case UNEXPECTED_END_OF_COMMAND:
|
2003-11-08 00:27:24 +01:00
|
|
|
user_status[5] = isc_command_end_err;
|
|
|
|
user_status[6] = isc_arg_end;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNEXPECTED_TOKEN:
|
|
|
|
case TOKEN_TOO_LONG:
|
|
|
|
if (result) {
|
|
|
|
err_string[0] = (TEXT) result;
|
|
|
|
strcpy(err_string + 1, token);
|
2003-11-18 08:58:35 +01:00
|
|
|
const size_t length = strlen(token);
|
2001-05-23 15:26:42 +02:00
|
|
|
err_string[length + 1] = (TEXT) result;
|
|
|
|
err_string[length + 2] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(err_string, token);
|
2003-11-08 00:27:24 +01:00
|
|
|
user_status[5] = isc_token_err;
|
|
|
|
user_status[6] = isc_arg_gds;
|
|
|
|
user_status[7] = isc_random;
|
|
|
|
user_status[8] = isc_arg_string;
|
2003-04-10 08:32:58 +02:00
|
|
|
user_status[9] = (ISC_STATUS) err_string;
|
2003-11-08 00:27:24 +01:00
|
|
|
user_status[10] = isc_arg_end;
|
2001-05-23 15:26:42 +02:00
|
|
|
UTLD_save_status_strings(user_status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-15 04:01:51 +01:00
|
|
|
/**
|
|
|
|
|
|
|
|
get_next_token
|
|
|
|
|
|
|
|
@brief
|
|
|
|
|
|
|
|
@param stmt
|
|
|
|
@param stmt_end
|
|
|
|
@param token
|
|
|
|
@param token_length
|
|
|
|
|
|
|
|
**/
|
2001-05-23 15:26:42 +02:00
|
|
|
static SSHORT get_next_token(
|
2003-11-28 07:48:34 +01:00
|
|
|
const SCHAR** stmt,
|
2003-11-18 08:58:35 +01:00
|
|
|
const SCHAR* stmt_end,
|
|
|
|
SCHAR* token, USHORT* token_length)
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
2003-11-18 08:58:35 +01:00
|
|
|
UCHAR c, char_class;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
*token_length = 0;
|
2003-11-28 07:48:34 +01:00
|
|
|
const SCHAR* s = *stmt;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (s >= stmt_end) {
|
|
|
|
*stmt = s;
|
|
|
|
return NO_MORE_TOKENS;
|
|
|
|
}
|
|
|
|
c = *s++;
|
|
|
|
if (c == '/' && s < stmt_end && *s == '*') {
|
|
|
|
s++;
|
|
|
|
while (s < stmt_end) {
|
|
|
|
c = *s++;
|
|
|
|
if (c == '*' && s < stmt_end && *s == '/')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
2003-11-18 08:58:35 +01:00
|
|
|
char_class = classes[c];
|
|
|
|
if (!(char_class & CHR_WHITE))
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point c contains character and class contains character class.
|
|
|
|
s is pointing to next character. */
|
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
const SCHAR* const start_of_token = s - 1;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
/* In here we handle only 4 cases, STRING, INTEGER, arbitrary
|
|
|
|
SYMBOL and single character punctuation. */
|
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
// token can be up to MAX_TOKEN_SIZE and 1 is for null termination
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-11-18 08:58:35 +01:00
|
|
|
SCHAR* p = token;
|
|
|
|
const char* const token_end = token + MAX_TOKEN_SIZE + 1;
|
|
|
|
if (char_class & CHR_QUOTE) {
|
2001-05-23 15:26:42 +02:00
|
|
|
for (; p < token_end; p++) {
|
|
|
|
if (s >= stmt_end)
|
|
|
|
return UNEXPECTED_END_OF_COMMAND;
|
|
|
|
|
|
|
|
/* *s is quote - if next != quote we're at the end */
|
|
|
|
|
|
|
|
if ((*s == c) && ((++s == stmt_end) || (*s != c)))
|
|
|
|
break;
|
|
|
|
*p = *s++;
|
|
|
|
}
|
|
|
|
*stmt = s;
|
|
|
|
if (p >= token_end) {
|
2004-03-07 08:58:55 +01:00
|
|
|
// '=' used as then there is no place for null termination
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
*token_length = MAX_TOKEN_SIZE;
|
|
|
|
token[MAX_TOKEN_SIZE] = '\0';
|
|
|
|
return TOKEN_TOO_LONG;
|
|
|
|
}
|
|
|
|
*token_length = (s - start_of_token) - 2;
|
|
|
|
*p = '\0';
|
|
|
|
return STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is it an integer? */
|
|
|
|
|
2003-11-18 08:58:35 +01:00
|
|
|
if (char_class & CHR_DIGIT) {
|
|
|
|
for (; s < stmt_end && (classes[c = *s] & CHR_DIGIT); ++s); // empty body
|
|
|
|
const ptrdiff_t length = (s - start_of_token);
|
2001-05-23 15:26:42 +02:00
|
|
|
*stmt = s;
|
|
|
|
if (length > MAX_TOKEN_SIZE) {
|
|
|
|
memcpy(token, start_of_token, MAX_TOKEN_SIZE);
|
|
|
|
token[MAX_TOKEN_SIZE] = '\0';
|
|
|
|
*token_length = MAX_TOKEN_SIZE;
|
|
|
|
return TOKEN_TOO_LONG;
|
|
|
|
}
|
|
|
|
memcpy(token, start_of_token, length);
|
|
|
|
token[length] = '\0';
|
|
|
|
*token_length = length;
|
|
|
|
return NUMERIC;
|
|
|
|
}
|
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
// Is is a symbol
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-11-18 08:58:35 +01:00
|
|
|
if (char_class & CHR_LETTER) {
|
2001-05-23 15:26:42 +02:00
|
|
|
*p++ = UPPER(c);
|
2004-03-12 08:00:52 +01:00
|
|
|
for (; s < stmt_end && (classes[static_cast<UCHAR>(*s)] & CHR_IDENT) && p < token_end; s++) {
|
2001-05-23 15:26:42 +02:00
|
|
|
*p++ = UPPER(*s);
|
|
|
|
}
|
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
// what happend to token size check. It went to hell
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
*stmt = s;
|
|
|
|
if (p >= token_end) {
|
|
|
|
*token_length = MAX_TOKEN_SIZE;
|
|
|
|
token[MAX_TOKEN_SIZE] = '\0';
|
|
|
|
return TOKEN_TOO_LONG;
|
|
|
|
}
|
|
|
|
*token_length = s - start_of_token;
|
|
|
|
*p = 0;
|
|
|
|
return SYMBOL;
|
|
|
|
}
|
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
// What remains at this point for us is the single character punctuation.
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
*stmt = s;
|
|
|
|
|
|
|
|
return (c == ';' ? NO_MORE_TOKENS : c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-15 04:01:51 +01:00
|
|
|
/**
|
|
|
|
|
|
|
|
get_token
|
|
|
|
|
|
|
|
@brief
|
|
|
|
|
|
|
|
@param status
|
|
|
|
@param token_type
|
|
|
|
@param optional
|
|
|
|
@param stmt
|
|
|
|
@param stmt_end
|
|
|
|
@param token
|
|
|
|
@param token_length
|
|
|
|
|
|
|
|
**/
|
2003-11-18 08:58:35 +01:00
|
|
|
static SSHORT get_token(ISC_STATUS* status,
|
2001-05-23 15:26:42 +02:00
|
|
|
SSHORT token_type,
|
2003-09-04 23:26:15 +02:00
|
|
|
bool optional,
|
2003-11-28 07:48:34 +01:00
|
|
|
const SCHAR** stmt,
|
2003-11-18 08:58:35 +01:00
|
|
|
const SCHAR* const stmt_end,
|
|
|
|
SCHAR* token,
|
|
|
|
USHORT* token_length)
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
2003-11-28 07:48:34 +01:00
|
|
|
const SCHAR* temp_stmt = *stmt;
|
2003-11-18 08:58:35 +01:00
|
|
|
const SSHORT result = get_next_token(&temp_stmt, stmt_end, token, token_length);
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case NO_MORE_TOKENS:
|
|
|
|
*stmt = temp_stmt;
|
|
|
|
generate_error(status, token, UNEXPECTED_END_OF_COMMAND, 0);
|
2002-11-14 09:33:08 +01:00
|
|
|
return FB_FAILURE;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
case UNEXPECTED_END_OF_COMMAND:
|
|
|
|
case TOKEN_TOO_LONG:
|
|
|
|
*stmt = temp_stmt;
|
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
// generate error here
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
generate_error(status, token, result, 0);
|
2002-11-14 09:33:08 +01:00
|
|
|
return FB_FAILURE;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
2003-11-28 07:48:34 +01:00
|
|
|
// Some token was found
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
if (result == token_type) {
|
|
|
|
*stmt = temp_stmt;
|
2002-11-14 09:33:08 +01:00
|
|
|
return FB_SUCCESS;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (optional)
|
2002-11-14 09:33:08 +01:00
|
|
|
return FB_SUCCESS;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
/* generate error here and return failure; */
|
|
|
|
|
|
|
|
*stmt = temp_stmt;
|
|
|
|
generate_error(status, token, UNEXPECTED_TOKEN,
|
|
|
|
(result == STRING) ? *(temp_stmt - 1) : 0);
|
2002-11-14 09:33:08 +01:00
|
|
|
return FB_FAILURE;
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|