aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/odbc/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/interfaces/odbc/parse.c')
-rw-r--r--src/interfaces/odbc/parse.c573
1 files changed, 347 insertions, 226 deletions
diff --git a/src/interfaces/odbc/parse.c b/src/interfaces/odbc/parse.c
index a5f046f179d..6d018a97017 100644
--- a/src/interfaces/odbc/parse.c
+++ b/src/interfaces/odbc/parse.c
@@ -1,20 +1,20 @@
-/* Module: parse.c
+/* Module: parse.c
*
- * Description: This module contains routines related to parsing SQL statements.
- * This can be useful for two reasons:
+ * Description: This module contains routines related to parsing SQL statements.
+ * This can be useful for two reasons:
*
- * 1. So the query does not actually have to be executed to return data about it
+ * 1. So the query does not actually have to be executed to return data about it
*
- * 2. To be able to return information about precision, nullability, aliases, etc.
- * in the functions SQLDescribeCol and SQLColAttributes. Currently, Postgres
- * doesn't return any information about these things in a query.
+ * 2. To be able to return information about precision, nullability, aliases, etc.
+ * in the functions SQLDescribeCol and SQLColAttributes. Currently, Postgres
+ * doesn't return any information about these things in a query.
*
- * Classes: none
+ * Classes: none
*
- * API functions: none
+ * API functions: none
*
- * Comments: See "notice.txt" for copyright and license information.
+ * Comments: See "notice.txt" for copyright and license information.
*
*/
/* Multibyte support Eiji Tokuya 2001-03-15 */
@@ -38,7 +38,7 @@
#ifndef WIN32
#ifndef HAVE_STRICMP
-#define stricmp(s1,s2) strcasecmp(s1,s2)
+#define stricmp(s1,s2) strcasecmp(s1,s2)
#define strnicmp(s1,s2,n) strncasecmp(s1,s2,n)
#endif
#endif
@@ -47,16 +47,17 @@
#define TAB_INCR 8
#define COL_INCR 16
-char *getNextToken(char *s, char *token, int smax, char *delim, char *quote, char *dquote, char *numeric);
-void getColInfo(COL_INFO *col_info, FIELD_INFO *fi, int k);
-char searchColInfo(COL_INFO *col_info, FIELD_INFO *fi);
+char *getNextToken(char *s, char *token, int smax, char *delim, char *quote, char *dquote, char *numeric);
+void getColInfo(COL_INFO *col_info, FIELD_INFO *fi, int k);
+char searchColInfo(COL_INFO *col_info, FIELD_INFO *fi);
char *
getNextToken(char *s, char *token, int smax, char *delim, char *quote, char *dquote, char *numeric)
{
-int i = 0;
-int out = 0;
-char qc, in_escape = FALSE;
+ int i = 0;
+ int out = 0;
+ char qc,
+ in_escape = FALSE;
if (smax <= 1)
return NULL;
@@ -64,48 +65,62 @@ char qc, in_escape = FALSE;
smax--;
/* skip leading delimiters */
- while (isspace((unsigned char) s[i]) || s[i] == ',') {
+ while (isspace((unsigned char) s[i]) || s[i] == ',')
+ {
/* mylog("skipping '%c'\n", s[i]); */
i++;
}
- if (s[0] == '\0') {
+ if (s[0] == '\0')
+ {
token[0] = '\0';
return NULL;
}
- if (quote) *quote = FALSE;
- if (dquote) *dquote = FALSE;
- if (numeric) *numeric = FALSE;
+ if (quote)
+ *quote = FALSE;
+ if (dquote)
+ *dquote = FALSE;
+ if (numeric)
+ *numeric = FALSE;
/* get the next token */
- while ( ! isspace((unsigned char) s[i]) && s[i] != ',' &&
- s[i] != '\0' && out != smax) {
+ while (!isspace((unsigned char) s[i]) && s[i] != ',' &&
+ s[i] != '\0' && out != smax)
+ {
- /* Handle quoted stuff */
- if ( out == 0 && (s[i] == '\"' || s[i] == '\'')) {
+ /* Handle quoted stuff */
+ if (out == 0 && (s[i] == '\"' || s[i] == '\''))
+ {
qc = s[i];
- if (qc == '\"') {
- if (dquote) *dquote = TRUE;
+ if (qc == '\"')
+ {
+ if (dquote)
+ *dquote = TRUE;
}
- if (qc == '\'') {
- if (quote) *quote = TRUE;
+ if (qc == '\'')
+ {
+ if (quote)
+ *quote = TRUE;
}
- i++; /* dont return the quote */
- while (s[i] != '\0' && out != smax) {
- if (s[i] == qc && ! in_escape) {
+ i++; /* dont return the quote */
+ while (s[i] != '\0' && out != smax)
+ {
+ if (s[i] == qc && !in_escape)
break;
- }
#ifdef MULTIBYTE
- if (multibyte_char_check(s[i]) == 0 && s[i] == '\\' && ! in_escape) {
+ if (multibyte_char_check(s[i]) == 0 && s[i] == '\\' && !in_escape)
+ {
#else
- if (s[i] == '\\' && ! in_escape) {
+ if (s[i] == '\\' && !in_escape)
+ {
#endif
in_escape = TRUE;
}
- else {
- in_escape = FALSE;
+ else
+ {
+ in_escape = FALSE;
token[out++] = s[i];
}
i++;
@@ -115,20 +130,24 @@ char qc, in_escape = FALSE;
break;
}
- /* Check for numeric literals */
- if ( out == 0 && isdigit((unsigned char) s[i])) {
- if (numeric) *numeric = TRUE;
+ /* Check for numeric literals */
+ if (out == 0 && isdigit((unsigned char) s[i]))
+ {
+ if (numeric)
+ *numeric = TRUE;
token[out++] = s[i++];
- while ( isalnum((unsigned char) s[i]) || s[i] == '.')
+ while (isalnum((unsigned char) s[i]) || s[i] == '.')
token[out++] = s[i++];
break;
}
- if ( ispunct((unsigned char) s[i]) && s[i] != '_') {
+ if (ispunct((unsigned char) s[i]) && s[i] != '_')
+ {
mylog("got ispunct: s[%d] = '%c'\n", i, s[i]);
- if (out == 0) {
+ if (out == 0)
+ {
token[out++] = s[i++];
break;
}
@@ -146,25 +165,30 @@ char qc, in_escape = FALSE;
token[out] = '\0';
- /* find the delimiter */
- while ( isspace((unsigned char) s[i]))
+ /* find the delimiter */
+ while (isspace((unsigned char) s[i]))
i++;
- /* return the most priority delimiter */
- if (s[i] == ',') {
- if (delim) *delim = s[i];
+ /* return the most priority delimiter */
+ if (s[i] == ',')
+ {
+ if (delim)
+ *delim = s[i];
}
- else if (s[i] == '\0') {
- if (delim) *delim = '\0';
+ else if (s[i] == '\0')
+ {
+ if (delim)
+ *delim = '\0';
}
- else {
- if (delim) *delim = ' ';
+ else
+ {
+ if (delim)
+ *delim = ' ';
}
/* skip trailing blanks */
- while ( isspace((unsigned char) s[i])) {
+ while (isspace((unsigned char) s[i]))
i++;
- }
return &s[i];
}
@@ -195,23 +219,25 @@ getColInfo(COL_INFO *col_info, FIELD_INFO *fi, int k)
if (fi->name[0] == '\0')
strcpy(fi->name, QR_get_value_manual(col_info->result, k, 3));
- fi->type = atoi( QR_get_value_manual(col_info->result, k, 13));
- fi->precision = atoi( QR_get_value_manual(col_info->result, k, 6));
- fi->length = atoi( QR_get_value_manual(col_info->result, k, 7));
- fi->nullable = atoi( QR_get_value_manual(col_info->result, k, 10));
- fi->display_size = atoi( QR_get_value_manual(col_info->result, k, 12));
+ fi->type = atoi(QR_get_value_manual(col_info->result, k, 13));
+ fi->precision = atoi(QR_get_value_manual(col_info->result, k, 6));
+ fi->length = atoi(QR_get_value_manual(col_info->result, k, 7));
+ fi->nullable = atoi(QR_get_value_manual(col_info->result, k, 10));
+ fi->display_size = atoi(QR_get_value_manual(col_info->result, k, 12));
}
char
searchColInfo(COL_INFO *col_info, FIELD_INFO *fi)
{
-int k;
-char *col;
+ int k;
+ char *col;
- for (k = 0; k < QR_get_num_tuples(col_info->result); k++) {
+ for (k = 0; k < QR_get_num_tuples(col_info->result); k++)
+ {
col = QR_get_value_manual(col_info->result, k, 3);
- if ( ! strcmp(col, fi->name)) {
+ if (!strcmp(col, fi->name))
+ {
getColInfo(col_info, fi, k);
mylog("PARSE: searchColInfo: \n");
@@ -226,20 +252,37 @@ char *col;
char
parse_statement(StatementClass *stmt)
{
-static char *func="parse_statement";
-char token[256];
-char delim, quote, dquote, numeric, unquoted;
-char *ptr;
-char in_select = FALSE, in_distinct = FALSE, in_on = FALSE, in_from = FALSE, in_where = FALSE, in_table = FALSE;
-char in_field = FALSE, in_expr = FALSE, in_func = FALSE, in_dot = FALSE, in_as = FALSE;
-int j, i, k = 0, n, blevel = 0;
-FIELD_INFO **fi;
-TABLE_INFO **ti;
-char parse;
-ConnectionClass *conn = stmt->hdbc;
-HSTMT hcol_stmt;
-StatementClass *col_stmt;
-RETCODE result;
+ static char *func = "parse_statement";
+ char token[256];
+ char delim,
+ quote,
+ dquote,
+ numeric,
+ unquoted;
+ char *ptr;
+ char in_select = FALSE,
+ in_distinct = FALSE,
+ in_on = FALSE,
+ in_from = FALSE,
+ in_where = FALSE,
+ in_table = FALSE;
+ char in_field = FALSE,
+ in_expr = FALSE,
+ in_func = FALSE,
+ in_dot = FALSE,
+ in_as = FALSE;
+ int j,
+ i,
+ k = 0,
+ n,
+ blevel = 0;
+ FIELD_INFO **fi;
+ TABLE_INFO **ti;
+ char parse;
+ ConnectionClass *conn = stmt->hdbc;
+ HSTMT hcol_stmt;
+ StatementClass *col_stmt;
+ RETCODE result;
mylog("%s: entering...\n", func);
@@ -251,34 +294,39 @@ RETCODE result;
stmt->nfld = 0;
stmt->ntab = 0;
- while ((ptr = getNextToken(ptr, token, sizeof(token), &delim, &quote, &dquote, &numeric)) != NULL) {
+ while ((ptr = getNextToken(ptr, token, sizeof(token), &delim, &quote, &dquote, &numeric)) != NULL)
+ {
- unquoted = ! ( quote || dquote );
+ unquoted = !(quote || dquote);
mylog("unquoted=%d, quote=%d, dquote=%d, numeric=%d, delim='%c', token='%s', ptr='%s'\n", unquoted, quote, dquote, numeric, delim, token, ptr);
- if ( unquoted && ! stricmp(token, "select")) {
+ if (unquoted && !stricmp(token, "select"))
+ {
in_select = TRUE;
mylog("SELECT\n");
continue;
}
- if ( unquoted && in_select && ! stricmp(token, "distinct")) {
+ if (unquoted && in_select && !stricmp(token, "distinct"))
+ {
in_distinct = TRUE;
mylog("DISTINCT\n");
continue;
}
- if ( unquoted && ! stricmp(token, "into")) {
+ if (unquoted && !stricmp(token, "into"))
+ {
in_select = FALSE;
mylog("INTO\n");
continue;
}
- if ( unquoted && ! stricmp(token, "from")) {
+ if (unquoted && !stricmp(token, "from"))
+ {
in_select = FALSE;
in_from = TRUE;
@@ -286,11 +334,12 @@ RETCODE result;
continue;
}
- if ( unquoted && (! stricmp(token, "where") ||
- ! stricmp(token, "union") ||
- ! stricmp(token, "order") ||
- ! stricmp(token, "group") ||
- ! stricmp(token, "having"))) {
+ if (unquoted && (!stricmp(token, "where") ||
+ !stricmp(token, "union") ||
+ !stricmp(token, "order") ||
+ !stricmp(token, "group") ||
+ !stricmp(token, "having")))
+ {
in_select = FALSE;
in_from = FALSE;
@@ -300,44 +349,53 @@ RETCODE result;
break;
}
- if (in_select) {
+ if (in_select)
+ {
- if ( in_distinct) {
+ if (in_distinct)
+ {
mylog("in distinct\n");
- if (unquoted && ! stricmp(token, "on")) {
+ if (unquoted && !stricmp(token, "on"))
+ {
in_on = TRUE;
mylog("got on\n");
continue;
}
- if (in_on) {
+ if (in_on)
+ {
in_distinct = FALSE;
in_on = FALSE;
- continue; /* just skip the unique on field */
+ continue; /* just skip the unique on field */
}
mylog("done distinct\n");
in_distinct = FALSE;
}
- if ( in_expr || in_func) { /* just eat the expression */
+ if (in_expr || in_func)
+ { /* just eat the expression */
mylog("in_expr=%d or func=%d\n", in_expr, in_func);
if (quote || dquote)
continue;
- if (in_expr && blevel == 0 && delim == ',') {
+ if (in_expr && blevel == 0 && delim == ',')
+ {
mylog("**** in_expr and Got comma\n");
in_expr = FALSE;
in_field = FALSE;
}
- else if (token[0] == '(') {
+ else if (token[0] == '(')
+ {
blevel++;
mylog("blevel++ = %d\n", blevel);
}
- else if (token[0] == ')') {
+ else if (token[0] == ')')
+ {
blevel--;
mylog("blevel-- = %d\n", blevel);
- if (delim==',') {
+ if (delim == ',')
+ {
in_func = FALSE;
in_expr = FALSE;
in_field = FALSE;
@@ -346,43 +404,50 @@ RETCODE result;
continue;
}
- if ( ! in_field) {
+ if (!in_field)
+ {
- if ( ! token[0])
+ if (!token[0])
continue;
- if ( ! (stmt->nfld % FLD_INCR)) {
+ if (!(stmt->nfld % FLD_INCR))
+ {
mylog("reallocing at nfld=%d\n", stmt->nfld);
fi = (FIELD_INFO **) realloc(fi, (stmt->nfld + FLD_INCR) * sizeof(FIELD_INFO *));
- if ( ! fi) {
+ if (!fi)
+ {
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
stmt->fi = fi;
}
- fi[stmt->nfld] = (FIELD_INFO *) malloc( sizeof(FIELD_INFO));
- if (fi[stmt->nfld] == NULL) {
+ fi[stmt->nfld] = (FIELD_INFO *) malloc(sizeof(FIELD_INFO));
+ if (fi[stmt->nfld] == NULL)
+ {
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
- /* Initialize the field info */
+ /* Initialize the field info */
memset(fi[stmt->nfld], 0, sizeof(FIELD_INFO));
- /* double quotes are for qualifiers */
+ /* double quotes are for qualifiers */
if (dquote)
fi[stmt->nfld]->dquote = TRUE;
- if (quote) {
+ if (quote)
+ {
fi[stmt->nfld++]->quote = TRUE;
continue;
}
- else if (numeric) {
+ else if (numeric)
+ {
mylog("**** got numeric: nfld = %d\n", stmt->nfld);
fi[stmt->nfld]->numeric = TRUE;
}
- else if (token[0] == '(') { /* expression */
+ else if (token[0] == '(')
+ { /* expression */
mylog("got EXPRESSION\n");
fi[stmt->nfld++]->expr = TRUE;
in_expr = TRUE;
@@ -390,33 +455,34 @@ RETCODE result;
continue;
}
- else {
+ else
+ {
strcpy(fi[stmt->nfld]->name, token);
fi[stmt->nfld]->dot[0] = '\0';
}
mylog("got field='%s', dot='%s'\n", fi[stmt->nfld]->name, fi[stmt->nfld]->dot);
- if (delim == ',') {
+ if (delim == ',')
mylog("comma (1)\n");
- }
- else {
+ else
in_field = TRUE;
- }
stmt->nfld++;
continue;
}
/**************************/
- /* We are in a field now */
+ /* We are in a field now */
/**************************/
- if (in_dot) {
+ if (in_dot)
+ {
stmt->nfld--;
strcpy(fi[stmt->nfld]->dot, fi[stmt->nfld]->name);
strcpy(fi[stmt->nfld]->name, token);
stmt->nfld++;
in_dot = FALSE;
- if (delim == ',') {
+ if (delim == ',')
+ {
mylog("in_dot: got comma\n");
in_field = FALSE;
}
@@ -424,7 +490,8 @@ RETCODE result;
continue;
}
- if (in_as) {
+ if (in_as)
+ {
stmt->nfld--;
strcpy(fi[stmt->nfld]->alias, token);
mylog("alias for field '%s' is '%s'\n", fi[stmt->nfld]->name, fi[stmt->nfld]->alias);
@@ -433,58 +500,69 @@ RETCODE result;
stmt->nfld++;
- if (delim == ',') {
+ if (delim == ',')
mylog("comma(2)\n");
- }
continue;
}
- /* Function */
- if (token[0] == '(') {
+ /* Function */
+ if (token[0] == '(')
+ {
in_func = TRUE;
blevel = 1;
- fi[stmt->nfld-1]->func = TRUE;
- /* name will have the function name -- maybe useful some day */
- mylog("**** got function = '%s'\n", fi[stmt->nfld-1]->name);
+ fi[stmt->nfld - 1]->func = TRUE;
+
+ /*
+ * name will have the function name -- maybe useful some
+ * day
+ */
+ mylog("**** got function = '%s'\n", fi[stmt->nfld - 1]->name);
continue;
}
- if (token[0] == '.') {
- in_dot = TRUE;
+ if (token[0] == '.')
+ {
+ in_dot = TRUE;
mylog("got dot\n");
continue;
}
- if ( ! stricmp(token, "as")) {
+ if (!stricmp(token, "as"))
+ {
in_as = TRUE;
mylog("got AS\n");
continue;
}
- /* otherwise, it's probably an expression */
+ /* otherwise, it's probably an expression */
in_expr = TRUE;
- fi[stmt->nfld-1]->expr = TRUE;
- fi[stmt->nfld-1]->name[0] = '\0';
+ fi[stmt->nfld - 1]->expr = TRUE;
+ fi[stmt->nfld - 1]->name[0] = '\0';
mylog("*** setting expression\n");
}
- if (in_from) {
+ if (in_from)
+ {
- if ( ! in_table) {
- if ( ! token[0])
+ if (!in_table)
+ {
+ if (!token[0])
continue;
- if ( ! (stmt->ntab % TAB_INCR)) {
+ if (!(stmt->ntab % TAB_INCR))
+ {
ti = (TABLE_INFO **) realloc(ti, (stmt->ntab + TAB_INCR) * sizeof(TABLE_INFO *));
- if ( ! ti) {
+ if (!ti)
+ {
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
stmt->ti = ti;
}
ti[stmt->ntab] = (TABLE_INFO *) malloc(sizeof(TABLE_INFO));
- if (ti[stmt->ntab] == NULL) {
+ if (ti[stmt->ntab] == NULL)
+ {
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
@@ -494,57 +572,61 @@ RETCODE result;
strcpy(ti[stmt->ntab]->name, token);
mylog("got table = '%s'\n", ti[stmt->ntab]->name);
- if (delim == ',') {
+ if (delim == ',')
mylog("more than 1 tables\n");
- }
- else {
+ else
in_table = TRUE;
- }
stmt->ntab++;
continue;
}
- strcpy(ti[stmt->ntab-1]->alias, token);
- mylog("alias for table '%s' is '%s'\n", ti[stmt->ntab-1]->name, ti[stmt->ntab-1]->alias);
+ strcpy(ti[stmt->ntab - 1]->alias, token);
+ mylog("alias for table '%s' is '%s'\n", ti[stmt->ntab - 1]->name, ti[stmt->ntab - 1]->alias);
in_table = FALSE;
- if (delim == ',') {
+ if (delim == ',')
mylog("more than 1 tables\n");
- }
}
}
/*************************************************/
- /* Resolve any possible field names with tables */
+ /* Resolve any possible field names with tables */
/*************************************************/
parse = TRUE;
- /* Resolve field names with tables */
- for (i = 0; i < stmt->nfld; i++) {
+ /* Resolve field names with tables */
+ for (i = 0; i < stmt->nfld; i++)
+ {
- if (fi[i]->func || fi[i]->expr || fi[i]->numeric) {
+ if (fi[i]->func || fi[i]->expr || fi[i]->numeric)
+ {
fi[i]->ti = NULL;
fi[i]->type = -1;
parse = FALSE;
continue;
}
- else if (fi[i]->quote) { /* handle as text */
+ else if (fi[i]->quote)
+ { /* handle as text */
fi[i]->ti = NULL;
fi[i]->type = PG_TYPE_TEXT;
fi[i]->precision = 0;
continue;
- }
+ }
- /* it's a dot, resolve to table or alias */
- else if (fi[i]->dot[0]) {
- for (k = 0; k < stmt->ntab; k++) {
- if ( ! stricmp(ti[k]->name, fi[i]->dot)) {
+ /* it's a dot, resolve to table or alias */
+ else if (fi[i]->dot[0])
+ {
+ for (k = 0; k < stmt->ntab; k++)
+ {
+ if (!stricmp(ti[k]->name, fi[i]->dot))
+ {
fi[i]->ti = ti[k];
break;
}
- else if ( ! stricmp(ti[k]->alias, fi[i]->dot)) {
+ else if (!stricmp(ti[k]->alias, fi[i]->dot))
+ {
fi[i]->ti = ti[k];
break;
}
@@ -557,41 +639,47 @@ RETCODE result;
mylog("--------------------------------------------\n");
mylog("nfld=%d, ntab=%d\n", stmt->nfld, stmt->ntab);
- for (i=0; i < stmt->nfld; i++) {
+ for (i = 0; i < stmt->nfld; i++)
+ {
mylog("Field %d: expr=%d, func=%d, quote=%d, dquote=%d, numeric=%d, name='%s', alias='%s', dot='%s'\n", i, fi[i]->expr, fi[i]->func, fi[i]->quote, fi[i]->dquote, fi[i]->numeric, fi[i]->name, fi[i]->alias, fi[i]->dot);
if (fi[i]->ti)
- mylog(" ----> table_name='%s', table_alias='%s'\n", fi[i]->ti->name, fi[i]->ti->alias);
+ mylog(" ----> table_name='%s', table_alias='%s'\n", fi[i]->ti->name, fi[i]->ti->alias);
}
- for (i=0; i < stmt->ntab; i++) {
+ for (i = 0; i < stmt->ntab; i++)
mylog("Table %d: name='%s', alias='%s'\n", i, ti[i]->name, ti[i]->alias);
- }
/******************************************************/
- /* Now save the SQLColumns Info for the parse tables */
+ /* Now save the SQLColumns Info for the parse tables */
/******************************************************/
- /* Call SQLColumns for each table and store the result */
- for (i = 0; i < stmt->ntab; i++) {
+ /* Call SQLColumns for each table and store the result */
+ for (i = 0; i < stmt->ntab; i++)
+ {
+
+ /* See if already got it */
+ char found = FALSE;
- /* See if already got it */
- char found = FALSE;
- for (k = 0; k < conn->ntables; k++) {
- if ( ! stricmp(conn->col_info[k]->name, ti[i]->name)) {
+ for (k = 0; k < conn->ntables; k++)
+ {
+ if (!stricmp(conn->col_info[k]->name, ti[i]->name))
+ {
mylog("FOUND col_info table='%s'\n", ti[i]->name);
found = TRUE;
break;
}
}
-
- if ( ! found) {
+
+ if (!found)
+ {
mylog("PARSE: Getting SQLColumns for table[%d]='%s'\n", i, ti[i]->name);
- result = SQLAllocStmt( stmt->hdbc, &hcol_stmt);
- if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) {
+ result = SQLAllocStmt(stmt->hdbc, &hcol_stmt);
+ if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
+ {
stmt->errormsg = "SQLAllocStmt failed in parse_statement for columns.";
stmt->errornumber = STMT_NO_MEMORY_ERROR;
stmt->parse_status = STMT_PARSE_FATAL;
@@ -601,18 +689,21 @@ RETCODE result;
col_stmt = (StatementClass *) hcol_stmt;
col_stmt->internal = TRUE;
- result = SQLColumns(hcol_stmt, "", 0, "", 0,
+ result = SQLColumns(hcol_stmt, "", 0, "", 0,
ti[i]->name, (SWORD) strlen(ti[i]->name), "", 0);
-
+
mylog(" Past SQLColumns\n");
- if (result == SQL_SUCCESS) {
+ if (result == SQL_SUCCESS)
+ {
mylog(" Success\n");
- if ( ! (conn->ntables % COL_INCR)) {
+ if (!(conn->ntables % COL_INCR))
+ {
mylog("PARSE: Allocing col_info at ntables=%d\n", conn->ntables);
conn->col_info = (COL_INFO **) realloc(conn->col_info, (conn->ntables + COL_INCR) * sizeof(COL_INFO *));
- if ( ! conn->col_info) {
+ if (!conn->col_info)
+ {
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
@@ -620,18 +711,23 @@ RETCODE result;
mylog("PARSE: malloc at conn->col_info[%d]\n", conn->ntables);
conn->col_info[conn->ntables] = (COL_INFO *) malloc(sizeof(COL_INFO));
- if ( ! conn->col_info[conn->ntables]) {
+ if (!conn->col_info[conn->ntables])
+ {
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
- /* Store the table name and the SQLColumns result structure */
+ /*
+ * Store the table name and the SQLColumns result
+ * structure
+ */
strcpy(conn->col_info[conn->ntables]->name, ti[i]->name);
conn->col_info[conn->ntables]->result = col_stmt->result;
- /* The connection will now free the result structures, so make
- sure that the statement doesn't free it
- */
+ /*
+ * The connection will now free the result structures, so
+ * make sure that the statement doesn't free it
+ */
col_stmt->result = NULL;
conn->ntables++;
@@ -639,13 +735,14 @@ RETCODE result;
SQLFreeStmt(hcol_stmt, SQL_DROP);
mylog("Created col_info table='%s', ntables=%d\n", ti[i]->name, conn->ntables);
}
- else {
+ else
+ {
SQLFreeStmt(hcol_stmt, SQL_DROP);
break;
}
}
- /* Associate a table from the statement with a SQLColumn info */
+ /* Associate a table from the statement with a SQLColumn info */
ti[i]->col_info = conn->col_info[k];
mylog("associate col_info: i=%d, k=%d\n", i, k);
}
@@ -654,54 +751,64 @@ RETCODE result;
mylog("Done SQLColumns\n");
/******************************************************/
- /* Now resolve the fields to point to column info */
+ /* Now resolve the fields to point to column info */
/******************************************************/
- for (i = 0; i < stmt->nfld;) {
+ for (i = 0; i < stmt->nfld;)
+ {
- /* Dont worry about functions or quotes */
- if (fi[i]->func || fi[i]->quote || fi[i]->numeric) {
+ /* Dont worry about functions or quotes */
+ if (fi[i]->func || fi[i]->quote || fi[i]->numeric)
+ {
i++;
continue;
}
- /* Stars get expanded to all fields in the table */
- else if (fi[i]->name[0] == '*') {
+ /* Stars get expanded to all fields in the table */
+ else if (fi[i]->name[0] == '*')
+ {
- char do_all_tables;
- int total_cols, old_alloc, new_size, cols;
- int increased_cols;
+ char do_all_tables;
+ int total_cols,
+ old_alloc,
+ new_size,
+ cols;
+ int increased_cols;
mylog("expanding field %d\n", i);
- total_cols = 0;
+ total_cols = 0;
- if (fi[i]->ti) /* The star represents only the qualified table */
+ if (fi[i]->ti) /* The star represents only the qualified
+ * table */
total_cols = QR_get_num_tuples(fi[i]->ti->col_info->result);
- else { /* The star represents all tables */
+ else
+ { /* The star represents all tables */
- /* Calculate the total number of columns after expansion */
- for (k = 0; k < stmt->ntab; k++) {
+ /* Calculate the total number of columns after expansion */
+ for (k = 0; k < stmt->ntab; k++)
total_cols += QR_get_num_tuples(ti[k]->col_info->result);
- }
}
increased_cols = total_cols - 1;
- /* Allocate some more field pointers if necessary */
+ /* Allocate some more field pointers if necessary */
/*------------------------------------------------------------- */
- old_alloc = ((stmt->nfld - 1) / FLD_INCR + 1) * FLD_INCR;
+ old_alloc = ((stmt->nfld - 1) / FLD_INCR + 1) * FLD_INCR;
new_size = stmt->nfld + increased_cols;
- mylog("k=%d, increased_cols=%d, old_alloc=%d, new_size=%d\n", k,increased_cols,old_alloc,new_size);
+ mylog("k=%d, increased_cols=%d, old_alloc=%d, new_size=%d\n", k, increased_cols, old_alloc, new_size);
+
+ if (new_size > old_alloc)
+ {
+ int new_alloc = ((new_size / FLD_INCR) + 1) * FLD_INCR;
- if (new_size > old_alloc) {
- int new_alloc = ((new_size / FLD_INCR) + 1) * FLD_INCR;
mylog("need more cols: new_alloc = %d\n", new_alloc);
fi = (FIELD_INFO **) realloc(fi, new_alloc * sizeof(FIELD_INFO *));
- if ( ! fi) {
+ if (!fi)
+ {
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
@@ -709,43 +816,52 @@ RETCODE result;
}
/*------------------------------------------------------------- */
- /* copy any other fields (if there are any) up past the expansion */
- for (j = stmt->nfld - 1; j > i; j--) {
+
+ /*
+ * copy any other fields (if there are any) up past the
+ * expansion
+ */
+ for (j = stmt->nfld - 1; j > i; j--)
+ {
mylog("copying field %d to %d\n", j, increased_cols + j);
fi[increased_cols + j] = fi[j];
}
mylog("done copying fields\n");
/*------------------------------------------------------------- */
- /* Set the new number of fields */
+ /* Set the new number of fields */
stmt->nfld += increased_cols;
mylog("stmt->nfld now at %d\n", stmt->nfld);
/*------------------------------------------------------------- */
- /* copy the new field info */
+ /* copy the new field info */
do_all_tables = (fi[i]->ti ? FALSE : TRUE);
- for (k = 0; k < (do_all_tables ? stmt->ntab : 1); k++) {
+ for (k = 0; k < (do_all_tables ? stmt->ntab : 1); k++)
+ {
TABLE_INFO *the_ti = do_all_tables ? ti[k] : fi[i]->ti;
cols = QR_get_num_tuples(the_ti->col_info->result);
- for (n = 0; n < cols; n++) {
+ for (n = 0; n < cols; n++)
+ {
mylog("creating field info: n=%d\n", n);
/* skip malloc (already did it for the Star) */
- if (k > 0 || n > 0) {
+ if (k > 0 || n > 0)
+ {
mylog("allocating field info at %d\n", n + i);
- fi[n + i] = (FIELD_INFO *) malloc( sizeof(FIELD_INFO));
- if (fi[n + i] == NULL) {
+ fi[n + i] = (FIELD_INFO *) malloc(sizeof(FIELD_INFO));
+ if (fi[n + i] == NULL)
+ {
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
}
- /* Initialize the new space (or the * field) */
+ /* Initialize the new space (or the * field) */
memset(fi[n + i], 0, sizeof(FIELD_INFO));
fi[n + i]->ti = the_ti;
@@ -763,22 +879,28 @@ RETCODE result;
/*------------------------------------------------------------- */
}
- /* We either know which table the field was in because it was qualified
- with a table name or alias -OR- there was only 1 table.
- */
- else if (fi[i]->ti) {
+ /*
+ * We either know which table the field was in because it was
+ * qualified with a table name or alias -OR- there was only 1
+ * table.
+ */
+ else if (fi[i]->ti)
+ {
- if ( ! searchColInfo(fi[i]->ti->col_info, fi[i]))
+ if (!searchColInfo(fi[i]->ti->col_info, fi[i]))
parse = FALSE;
i++;
}
- /* Don't know the table -- search all tables in "from" list */
- else {
+ /* Don't know the table -- search all tables in "from" list */
+ else
+ {
parse = FALSE;
- for (k = 0; k < stmt->ntab; k++) {
- if ( searchColInfo(ti[k]->col_info, fi[i])) {
+ for (k = 0; k < stmt->ntab; k++)
+ {
+ if (searchColInfo(ti[k]->col_info, fi[i]))
+ {
fi[i]->ti = ti[k]; /* now know the table */
parse = TRUE;
break;
@@ -789,7 +911,7 @@ RETCODE result;
}
- if ( ! parse)
+ if (!parse)
stmt->parse_status = STMT_PARSE_INCOMPLETE;
else
stmt->parse_status = STMT_PARSE_COMPLETE;
@@ -798,4 +920,3 @@ RETCODE result;
mylog("done parse_statement: parse=%d, parse_status=%d\n", parse, stmt->parse_status);
return parse;
}
-