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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
|
/*--------
* Module: parse.c
*
* 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
*
* 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
*
* API functions: none
*
* Comments: See "notice.txt" for copyright and license information.
*--------
*/
/* Multibyte support Eiji Tokuya 2001-03-15 */
#include "psqlodbc.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "statement.h"
#include "connection.h"
#include "qresult.h"
#include "pgtypes.h"
#include "pgapifunc.h"
#include "multibyte.h"
#define FLD_INCR 32
#define TAB_INCR 8
#define COL_INCR 16
char *getNextToken(int ccsc, 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);
Int4 FI_precision(const FIELD_INFO *fi)
{
if (!fi) return -1;
switch (fi->type)
{
case PG_TYPE_NUMERIC:
return fi->column_size;
case PG_TYPE_DATETIME:
case PG_TYPE_TIMESTAMP_NO_TMZONE:
return fi->decimal_digits;
}
return 0;
}
Int4 FI_scale(const FIELD_INFO *fi)
{
if (!fi) return -1;
switch (fi->type)
{
case PG_TYPE_NUMERIC:
return fi->decimal_digits;
}
return 0;
}
char *
getNextToken(
int ccsc, /* client encoding */
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;
encoded_str encstr;
if (smax <= 1)
return NULL;
smax--;
/* skip leading delimiters */
while (isspace((UCHAR) s[i]) || s[i] == ',')
{
/* mylog("skipping '%c'\n", s[i]); */
i++;
}
if (s[i] == '\0')
{
token[0] = '\0';
return NULL;
}
if (quote)
*quote = FALSE;
if (dquote)
*dquote = FALSE;
if (numeric)
*numeric = FALSE;
encoded_str_constr(&encstr, ccsc, &s[i]);
/* get the next token */
while (s[i] != '\0' && out < smax)
{
encoded_nextchar(&encstr);
if (ENCODE_STATUS(encstr) != 0)
{
token[out++] = s[i++];
continue;
}
if (isspace((UCHAR) s[i]) || s[i] == ',')
break;
/* Handle quoted stuff */
if (out == 0 && (s[i] == '\"' || s[i] == '\''))
{
qc = s[i];
if (qc == '\"')
{
if (dquote)
*dquote = TRUE;
}
if (qc == '\'')
{
if (quote)
*quote = TRUE;
}
i++; /* dont return the quote */
while (s[i] != '\0' && out != smax)
{
encoded_nextchar(&encstr);
if (ENCODE_STATUS(encstr) != 0)
{
token[out++] = s[i++];
continue;
}
if (s[i] == qc && !in_escape)
break;
if (s[i] == '\\' && !in_escape)
in_escape = TRUE;
else
{
in_escape = FALSE;
token[out++] = s[i];
}
i++;
}
if (s[i] == qc)
i++;
break;
}
/* Check for numeric literals */
if (out == 0 && isdigit((UCHAR) s[i]))
{
if (numeric)
*numeric = TRUE;
token[out++] = s[i++];
while (isalnum((UCHAR) s[i]) || s[i] == '.')
token[out++] = s[i++];
break;
}
if (ispunct((UCHAR) s[i]) && s[i] != '_')
{
mylog("got ispunct: s[%d] = '%c'\n", i, s[i]);
if (out == 0)
{
token[out++] = s[i++];
break;
}
else
break;
}
if (out != smax)
token[out++] = s[i];
i++;
}
/* mylog("done -- s[%d] = '%c'\n", i, s[i]); */
token[out] = '\0';
/* find the delimiter */
while (isspace((UCHAR) s[i]))
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 (delim)
*delim = ' ';
}
/* skip trailing blanks */
while (isspace((UCHAR) s[i]))
i++;
return &s[i];
}
#if 0
QR_set_num_fields(SC_get_Curres(stmt), 14);
QR_set_field_info(SC_get_Curres(stmt), 0, "TABLE_QUALIFIER", PG_TYPE_TEXT, MAX_INFO_STRING);
QR_set_field_info(SC_get_Curres(stmt), 1, "TABLE_OWNER", PG_TYPE_TEXT, MAX_INFO_STRING);
QR_set_field_info(SC_get_Curres(stmt), 2, "TABLE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
QR_set_field_info(SC_get_Curres(stmt), 3, "COLUMN_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
QR_set_field_info(SC_get_Curres(stmt), 4, "DATA_TYPE", PG_TYPE_INT2, 2);
QR_set_field_info(SC_get_Curres(stmt), 5, "TYPE_NAME", PG_TYPE_TEXT, MAX_INFO_STRING);
QR_set_field_info(SC_get_Curres(stmt), 6, "PRECISION", PG_TYPE_INT4, 4);
QR_set_field_info(SC_get_Curres(stmt), 7, "LENGTH", PG_TYPE_INT4, 4);
QR_set_field_info(SC_get_Curres(stmt), 8, "SCALE", PG_TYPE_INT2, 2);
QR_set_field_info(SC_get_Curres(stmt), 9, "RADIX", PG_TYPE_INT2, 2);
QR_set_field_info(SC_get_Curres(stmt), 10, "NULLABLE", PG_TYPE_INT2, 2);
QR_set_field_info(SC_get_Curres(stmt), 11, "REMARKS", PG_TYPE_TEXT, 254);
/* User defined fields */
QR_set_field_info(SC_get_Curres(stmt), 12, "DISPLAY_SIZE", PG_TYPE_INT4, 4);
QR_set_field_info(SC_get_Curres(stmt), 13, "FIELD_TYPE", PG_TYPE_INT4, 4);
#endif
void
getColInfo(COL_INFO *col_info, FIELD_INFO *fi, int k)
{
char *str;
Int2 reserved_cols;
reserved_cols = 18;
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, (Int2)(reserved_cols + 1)));
fi->column_size = atoi(QR_get_value_manual(col_info->result, k, 6));
fi->length = atoi(QR_get_value_manual(col_info->result, k, 7));
if (str = QR_get_value_manual(col_info->result, k, 8), str)
fi->decimal_digits = atoi(str);
else
fi->decimal_digits = -1;
fi->nullable = atoi(QR_get_value_manual(col_info->result, k, 10));
fi->display_size = atoi(QR_get_value_manual(col_info->result, k, reserved_cols));
}
char
searchColInfo(COL_INFO *col_info, FIELD_INFO *fi)
{
int k,
cmp;
char *col;
for (k = 0; k < QR_get_num_backend_tuples(col_info->result); k++)
{
col = QR_get_value_manual(col_info->result, k, 3);
if (fi->dquote)
cmp = strcmp(col, fi->name);
else
cmp = stricmp(col, fi->name);
if (!cmp)
{
if (!fi->dquote)
strcpy(fi->name, col);
getColInfo(col_info, fi, k);
mylog("PARSE: searchColInfo: \n");
return TRUE;
}
}
return FALSE;
}
/*
* lower the unquoted name
*/
static
void lower_the_name(char *name, ConnectionClass *conn, BOOL dquote)
{
if (!dquote)
{
char *ptr;
encoded_str encstr;
make_encoded_str(&encstr, conn, name);
/* lower case table name */
for (ptr = name; *ptr; ptr++)
{
encoded_nextchar(&encstr);
if (ENCODE_STATUS(encstr) == 0)
*ptr = tolower((UCHAR) *ptr);
}
}
}
char
parse_statement(StatementClass *stmt)
{
CSTR func = "parse_statement";
char token[256], stoken[256];
char delim,
quote,
dquote,
numeric,
unquoted;
char *ptr,
*pptr = NULL;
char in_select = FALSE,
in_distinct = FALSE,
in_on = FALSE,
in_from = FALSE,
in_where = FALSE,
in_table = FALSE,
out_table = TRUE;
char in_field = FALSE,
in_expr = FALSE,
in_func = FALSE,
out_func = FALSE,
in_dot = FALSE,
in_as = FALSE;
int j,
i,
k = 0,
n,
blevel = 0, old_blevel, subqlevel = 0;
FIELD_INFO **fi;
TABLE_INFO **ti;
char parse;
ConnectionClass *conn = stmt->hdbc;
HSTMT hcol_stmt;
StatementClass *col_stmt;
IRDFields *irdflds = SC_get_IRDF(stmt);
RETCODE result;
BOOL updatable = TRUE;
QResultClass* resultClass;
mylog("%s: entering...\n", func);
ptr = stmt->statement;
fi = irdflds->fi;
ti = stmt->ti;
irdflds->nfields = 0;
stmt->ntab = 0;
stmt->from_pos = -1;
stmt->where_pos = -1;
while (pptr = ptr, (ptr = getNextToken(conn->ccsc, pptr, token, sizeof(token), &delim, "e, &dquote, &numeric)) != NULL)
{
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);
old_blevel = blevel;
if (unquoted && blevel == 0)
{
if (in_select)
{
if (!stricmp(token, "distinct"))
{
in_distinct = TRUE;
updatable = FALSE;
mylog("DISTINCT\n");
continue;
}
else if (!stricmp(token, "into"))
{
in_select = FALSE;
mylog("INTO\n");
stmt->statement_type = STMT_TYPE_CREATE;
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
else if (!stricmp(token, "from"))
{
in_select = FALSE;
in_from = TRUE;
if (stmt->from_pos < 0 &&
(!strnicmp(pptr, "from", 4)))
{
mylog("First ");
stmt->from_pos = pptr - stmt->statement;
}
mylog("FROM\n");
continue;
}
} /* in_select && unquoted && blevel == 0 */
else if ((!stricmp(token, "where") ||
!stricmp(token, "union") ||
!stricmp(token, "intersect") ||
!stricmp(token, "except") ||
!stricmp(token, "order") ||
!stricmp(token, "group") ||
!stricmp(token, "having")))
{
in_from = FALSE;
in_where = TRUE;
if (stmt->where_pos < 0)
stmt->where_pos = pptr - stmt->statement;
mylog("%s...\n", token);
if (stricmp(token, "where") &&
stricmp(token, "order"))
{
updatable = FALSE;
break;
}
continue;
}
} /* unquoted && blevel == 0 */
/* check the change of blevel etc */
if (unquoted)
{
if (!stricmp(token, "select"))
{
stoken[0] = '\0';
if (0 == blevel)
{
in_select = TRUE;
mylog("SELECT\n");
continue;
}
else
{
mylog("SUBSELECT\n");
if (0 == subqlevel)
subqlevel = blevel;
}
}
else if (token[0] == '(')
{
blevel++;
mylog("blevel++ = %d\n", blevel);
/* aggregate function ? */
if (stoken[0] && updatable && 0 == subqlevel)
{
if (stricmp(stoken, "count") == 0 ||
stricmp(stoken, "sum") == 0 ||
stricmp(stoken, "avg") == 0 ||
stricmp(stoken, "max") == 0 ||
stricmp(stoken, "min") == 0 ||
stricmp(stoken, "variance") == 0 ||
stricmp(stoken, "stddev") == 0)
updatable = FALSE;
}
}
else if (token[0] == ')')
{
blevel--;
mylog("blevel-- = %d\n", blevel);
if (blevel < subqlevel)
subqlevel = 0;
}
if (blevel >= old_blevel && ',' != delim)
strcpy(stoken, token);
else
stoken[0] = '\0';
}
if (in_select)
{
if (in_expr || in_func)
{
/* just eat the expression */
mylog("in_expr=%d or func=%d\n", in_expr, in_func);
if (blevel == 0)
{
if (delim == ',')
{
mylog("**** Got comma in_expr/func\n");
in_func = FALSE;
in_expr = FALSE;
in_field = FALSE;
}
else if (unquoted && !stricmp(token, "as"))
{
mylog("got AS in_expr\n");
in_func = FALSE;
in_expr = FALSE;
in_as = TRUE;
in_field = TRUE;
}
}
continue;
} /* (in_expr || in_func) && in_select */
if (in_distinct)
{
mylog("in distinct\n");
if (unquoted && !stricmp(token, "on"))
{
in_on = TRUE;
mylog("got on\n");
continue;
}
if (in_on)
{
in_distinct = FALSE;
in_on = FALSE;
continue; /* just skip the unique on field */
}
mylog("done distinct\n");
in_distinct = FALSE;
} /* in_distinct */
if (!in_field)
{
if (!token[0])
continue;
if (!(irdflds->nfields % FLD_INCR))
{
mylog("reallocing at nfld=%d\n", irdflds->nfields);
fi = (FIELD_INFO **) realloc(fi, (irdflds->nfields + FLD_INCR) * sizeof(FIELD_INFO *));
if (!fi)
{
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
irdflds->fi = fi;
}
fi[irdflds->nfields] = (FIELD_INFO *) malloc(sizeof(FIELD_INFO));
if (fi[irdflds->nfields] == NULL)
{
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
/* Initialize the field info */
memset(fi[irdflds->nfields], 0, sizeof(FIELD_INFO));
/* double quotes are for qualifiers */
if (dquote)
fi[irdflds->nfields]->dquote = TRUE;
if (quote)
{
fi[irdflds->nfields]->quote = TRUE;
fi[irdflds->nfields]->column_size = strlen(token);
}
else if (numeric)
{
mylog("**** got numeric: nfld = %d\n", irdflds->nfields);
fi[irdflds->nfields]->numeric = TRUE;
}
else if (0 == old_blevel && blevel > 0)
{ /* expression */
mylog("got EXPRESSION\n");
fi[irdflds->nfields++]->expr = TRUE;
in_expr = TRUE;
continue;
}
else
{
strcpy(fi[irdflds->nfields]->name, token);
fi[irdflds->nfields]->dot[0] = '\0';
}
mylog("got field='%s', dot='%s'\n", fi[irdflds->nfields]->name, fi[irdflds->nfields]->dot);
if (delim == ',')
mylog("comma (1)\n");
else
in_field = TRUE;
irdflds->nfields++;
continue;
} /* !in_field */
/*
* We are in a field now
*/
if (in_dot)
{
int ifld = irdflds->nfields - 1;
if (fi[ifld]->dot[0])
{
fi[ifld]->schema = strdup(fi[ifld]->dot);
}
strcpy(fi[ifld]->dot, fi[ifld]->name);
strcpy(fi[ifld]->name, token);
if (delim == ',')
{
mylog("in_dot: got comma\n");
in_field = FALSE;
}
in_dot = FALSE;
continue;
}
if (in_as)
{
irdflds->nfields--;
strcpy(fi[irdflds->nfields]->alias, token);
mylog("alias for field '%s' is '%s'\n", fi[irdflds->nfields]->name, fi[irdflds->nfields]->alias);
in_as = FALSE;
in_field = FALSE;
irdflds->nfields++;
if (delim == ',')
mylog("comma(2)\n");
continue;
}
/* Function */
if (0 == old_blevel && blevel > 0)
{
in_dot = FALSE;
in_func = TRUE;
fi[irdflds->nfields - 1]->func = TRUE;
/*
* name will have the function name -- maybe useful some
* day
*/
mylog("**** got function = '%s'\n", fi[irdflds->nfields - 1]->name);
continue;
}
if (token[0] == '.')
{
in_dot = TRUE;
mylog("got dot\n");
continue;
}
in_dot = FALSE;
if (!stricmp(token, "as"))
{
in_as = TRUE;
mylog("got AS\n");
continue;
}
/* otherwise, it's probably an expression */
in_expr = TRUE;
fi[irdflds->nfields - 1]->expr = TRUE;
fi[irdflds->nfields - 1]->name[0] = '\0';
fi[irdflds->nfields - 1]->column_size = 0;
mylog("*** setting expression\n");
} /* in_select end */
if (in_from)
{
if (token[0] == ';')
{
in_from = FALSE;
break;
}
switch (token[0])
{
case '\0':
continue;
case ',':
out_table = TRUE;
continue;
}
if (out_table && !in_table && !in_func) /* new table */
{
if (!dquote)
{
if (token[0] == '(' ||
token[0] == ')')
continue;
/*
* Detect a function call that looks like a table, eg.
* SELECT * FROM version()
*
* This needs work to properly handle functions found in the from
* clause, but this at least prevents nasty errors for now.
*
* DJP, 2005-01-08
*/
if (ptr[0] == '(')
{
in_func = TRUE;
mylog("**** got function = '%s'\n", token);
mylog("FIXME: functions in the FROM clause are currently ignored!!\n");
continue;
}
}
if (!(stmt->ntab % TAB_INCR))
{
ti = (TABLE_INFO **) realloc(ti, (stmt->ntab + TAB_INCR) * sizeof(TABLE_INFO *));
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)
{
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
ti[stmt->ntab]->schema[0] = '\0';
ti[stmt->ntab]->alias[0] = '\0';
ti[stmt->ntab]->updatable = 1;
strcpy(ti[stmt->ntab]->name, token);
lower_the_name(ti[stmt->ntab]->name, conn, dquote);
mylog("got table = '%s'\n", ti[stmt->ntab]->name);
if (delim == ',')
{
out_table = TRUE;
mylog("more than 1 tables\n");
}
else
{
out_table = FALSE;
in_table = TRUE;
}
stmt->ntab++;
in_dot = FALSE;
continue;
}
if (out_table && !in_table && in_func) /* old function */
{
/* Just skipped a function alias */
if (out_func)
{
in_func = FALSE;
out_func = FALSE;
mylog("leaving func (and ignoring alias)\n");
continue;
}
/* Normal function, no alias */
if (token[0] == ')' && (ptr[0] == ',' || strlen(ptr) == 0))
{
in_func = FALSE;
/*
* TODO: shouldn't we cache the metadata somewhere?
*/
SC_pre_execute(stmt);
resultClass = SC_get_Curres(stmt);
mylog("PGAPI_NumResultCols: result = %u, status = %d, numcols = %d\n", resultClass, stmt->status, resultClass!= NULL ? QR_NumResultCols(resultClass) : -1);
if ((!resultClass) || ((stmt->status != STMT_FINISHED) && (stmt->status != STMT_PREMATURE)))
{
/* no query has been executed on this statement */
SC_set_error(stmt, STMT_EXEC_ERROR, "No query has been executed with that handle");
SC_log_error(func, "", stmt);
return SQL_ERROR;
}
else if (!QR_command_maybe_successful(resultClass))
{
SC_set_errornumber(stmt, STMT_EXEC_ERROR);
SC_log_error(func, "", stmt);
return SQL_ERROR;
}
/*
* If the parse_statement is called to get the number of
* result columns throught SQLColAttribute.
*/
irdflds->nfields = QR_NumPublicResultCols(resultClass);
mylog("leaving func.\n");
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
/* Function with following alias */
if (token[0] == ')')
{
out_func = TRUE;
continue;
}
}
if (!dquote && stricmp(token, "JOIN") == 0)
{
in_table = FALSE;
out_table = TRUE;
continue;
}
if (in_table)
{
if (in_dot)
{
strcpy(ti[stmt->ntab - 1]->schema, ti[stmt->ntab - 1]->name);
strcpy(ti[stmt->ntab - 1]->name, token);
lower_the_name(ti[stmt->ntab - 1]->name, conn, dquote);
in_dot = FALSE;
continue;
}
if (strcmp(token, ".") == 0)
{
in_dot = TRUE;
continue;
}
if (dquote || stricmp(token, "as"))
{
if (stricmp(token, "LEFT") == 0 ||
stricmp(token, "RIGHT") == 0 ||
stricmp(token, "OUTER") == 0 ||
stricmp(token, "FULL") == 0 ||
stricmp(token, "ON") == 0)
{
if (!dquote)
{
in_table = FALSE;
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);
in_table = FALSE;
if (delim == ',')
{
out_table = TRUE;
mylog("more than 1 tables\n");
}
}
}
} /* in_from */
}
/*
* Resolve any possible field names with tables
*/
parse = TRUE;
/* Resolve field names with tables */
for (i = 0; i < (int) irdflds->nfields; i++)
{
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 */
fi[i]->ti = NULL;
/*
* fi[i]->type = PG_TYPE_TEXT; fi[i]->column_size = 0; the
* following may be better
*/
fi[i]->type = PG_TYPE_UNKNOWN;
if (fi[i]->column_size == 0)
{
fi[i]->type = PG_TYPE_VARCHAR;
fi[i]->column_size = 254;
}
fi[i]->length = fi[i]->column_size;
continue;
}
/* field name contains the schema name */
else if (fi[i]->schema)
{
int matchidx = -1;
for (k = 0; k < stmt->ntab; k++)
{
if (!stricmp(ti[k]->name, fi[i]->dot))
{
if (!stricmp(ti[k]->schema, fi[i]->schema))
{
fi[i]->ti = ti[k];
break;
}
else if (!ti[k]->schema[0])
{
if (matchidx < 0)
matchidx = k;
else
{
stmt->parse_status = STMT_PARSE_FATAL;
SC_set_error(stmt, STMT_EXEC_ERROR, "duplicated Table name");
stmt->updatable = FALSE;
return FALSE;
}
}
}
}
if (matchidx >= 0)
fi[i]->ti = ti[matchidx];
}
/* 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]->alias, fi[i]->dot))
{
fi[i]->ti = ti[k];
break;
}
else if (!stricmp(ti[k]->name, fi[i]->dot))
{
fi[i]->ti = ti[k];
break;
}
}
}
else if (stmt->ntab == 1)
fi[i]->ti = ti[0];
}
mylog("--------------------------------------------\n");
mylog("nfld=%d, ntab=%d\n", irdflds->nfields, stmt->ntab);
if (0 == stmt->ntab)
{
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
for (i = 0; i < (int) irdflds->nfields; 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);
}
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
*/
/* Call SQLColumns for each table and store the result */
if (stmt->ntab > 1)
updatable = FALSE;
else if (stmt->from_pos < 0)
updatable = FALSE;
for (i = 0; i < stmt->ntab; i++)
{
/* See if already got it */
char found = FALSE;
if (conn->schema_support)
{
if (!ti[i]->schema[0])
{
const char *curschema = CC_get_current_schema(conn);
/*
* Though current_schema() doesn't have
* much sense in PostgreSQL, we first
* check the current_schema() when no
* explicit schema name was specified.
*/
for (k = 0; k < conn->ntables; k++)
{
if (!stricmp(conn->col_info[k]->name, ti[i]->name) &&
!stricmp(conn->col_info[k]->schema, curschema))
{
mylog("FOUND col_info table='%s' current schema='%s'\n", ti[i]->name, curschema);
found = TRUE;
strcpy(ti[i]->schema, curschema);
break;
}
}
if (!found)
{
QResultClass *res;
BOOL tblFound = FALSE;
/*
* We also have to check as follows.
*/
sprintf(token, "select nspname from pg_namespace n, pg_class c"
" where c.relnamespace=n.oid and c.oid='\"%s\"'::regclass", ti[i]->name);
res = CC_send_query(conn, token, NULL, CLEAR_RESULT_ON_ABORT);
if (res)
{
if (QR_get_num_total_tuples(res) == 1)
{
tblFound = TRUE;
strcpy(ti[i]->schema, QR_get_value_backend_row(res, 0, 0));
}
QR_Destructor(res);
}
else
CC_abort(conn);
if (!tblFound)
{
stmt->parse_status = STMT_PARSE_FATAL;
SC_set_error(stmt, STMT_EXEC_ERROR, "Table not found");
stmt->updatable = FALSE;
return FALSE;
}
}
}
if (!found && ti[i]->schema[0])
{
for (k = 0; k < conn->ntables; k++)
{
if (!stricmp(conn->col_info[k]->name, ti[i]->name) &&
!stricmp(conn->col_info[k]->schema, ti[i]->schema))
{
mylog("FOUND col_info table='%s' schema='%s'\n", ti[i]->name, ti[i]->schema);
found = TRUE;
break;
}
}
}
}
else
{
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)
{
mylog("PARSE: Getting PG_Columns for table[%d]='%s'\n", i, ti[i]->name);
result = PGAPI_AllocStmt(stmt->hdbc, &hcol_stmt);
if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
{
SC_set_error(stmt, STMT_NO_MEMORY_ERROR, "PGAPI_AllocStmt failed in parse_statement for columns.");
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
col_stmt = (StatementClass *) hcol_stmt;
col_stmt->internal = TRUE;
result = PGAPI_Columns(hcol_stmt, "", 0, ti[i]->schema,
SQL_NTS, ti[i]->name, SQL_NTS, "", 0, PODBC_NOT_SEARCH_PATTERN);
mylog(" Past PG_Columns\n");
if (result == SQL_SUCCESS)
{
mylog(" Success\n");
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)
{
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
}
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])
{
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
/*
* Store the table name and the SQLColumns result
* structure
*/
if (ti[i]->schema[0])
conn->col_info[conn->ntables]->schema = strdup(ti[i]->schema);
else
conn->col_info[conn->ntables]->schema = NULL;
strcpy(conn->col_info[conn->ntables]->name, ti[i]->name);
conn->col_info[conn->ntables]->result = SC_get_Curres(col_stmt);
/*
* The connection will now free the result structures, so
* make sure that the statement doesn't free it
*/
SC_set_Result(col_stmt, NULL);
conn->ntables++;
PGAPI_FreeStmt(hcol_stmt, SQL_DROP);
mylog("Created col_info table='%s', ntables=%d\n", ti[i]->name, conn->ntables);
}
else
{
PGAPI_FreeStmt(hcol_stmt, SQL_DROP);
break;
}
}
/* 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);
}
mylog("Done PG_Columns\n");
/*
* Now resolve the fields to point to column info
*/
if (updatable && 1 == stmt->ntab)
updatable = stmt->ti[0]->updatable;
for (i = 0; i < (int) irdflds->nfields;)
{
fi[i]->updatable = updatable;
/* Dont worry about functions or quotes */
if (fi[i]->func || fi[i]->quote || fi[i]->numeric)
{
fi[i]->updatable = FALSE;
i++;
continue;
}
/* 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;
mylog("expanding field %d\n", i);
total_cols = 0;
if (fi[i]->ti) /* The star represents only the qualified
* table */
total_cols = QR_get_num_backend_tuples(fi[i]->ti->col_info->result);
else
{ /* The star represents all tables */
/* Calculate the total number of columns after expansion */
for (k = 0; k < stmt->ntab; k++)
total_cols += QR_get_num_backend_tuples(ti[k]->col_info->result);
}
increased_cols = total_cols - 1;
/* Allocate some more field pointers if necessary */
old_alloc = ((irdflds->nfields - 1) / FLD_INCR + 1) * FLD_INCR;
new_size = irdflds->nfields + increased_cols;
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;
mylog("need more cols: new_alloc = %d\n", new_alloc);
fi = (FIELD_INFO **) realloc(fi, new_alloc * sizeof(FIELD_INFO *));
if (!fi)
{
stmt->parse_status = STMT_PARSE_FATAL;
return FALSE;
}
irdflds->fi = fi;
}
/*
* copy any other fields (if there are any) up past the
* expansion
*/
for (j = irdflds->nfields - 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 */
irdflds->nfields += increased_cols;
mylog("irdflds->nfields now at %d\n", irdflds->nfields);
/* copy the new field info */
do_all_tables = (fi[i]->ti ? FALSE : TRUE);
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_backend_tuples(the_ti->col_info->result);
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)
{
mylog("allocating field info at %d\n", n + i);
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) */
memset(fi[n + i], 0, sizeof(FIELD_INFO));
fi[n + i]->ti = the_ti;
mylog("about to copy at %d\n", n + i);
getColInfo(the_ti->col_info, fi[n + i], n);
fi[n + i]->updatable = updatable;
mylog("done copying\n");
}
i += cols;
mylog("i now at %d\n", i);
}
}
/*
* 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]))
{
parse = FALSE;
fi[i]->updatable = FALSE;
}
i++;
}
/* Don't know the table -- search all tables in "from" list */
else
{
for (k = 0; k < stmt->ntab; k++)
{
if (searchColInfo(ti[k]->col_info, fi[i]))
{
fi[i]->ti = ti[k]; /* now know the table */
break;
}
}
if (k >= stmt->ntab)
{
parse = FALSE;
fi[i]->updatable = FALSE;
}
i++;
}
}
if (!parse)
stmt->parse_status = STMT_PARSE_INCOMPLETE;
else
stmt->parse_status = STMT_PARSE_COMPLETE;
stmt->updatable = updatable;
mylog("done parse_statement: parse=%d, parse_status=%d\n", parse, stmt->parse_status);
return parse;
}
|