apifull.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program prompts for and executes unknown SQL statements.
  6. * The contents of this file are subject to the Interbase Public
  7. * License Version 1.0 (the "License"); you may not use this file
  8. * except in compliance with the License. You may obtain a copy
  9. * of the License at http://www.Inprise.com/IPL.html
  10. *
  11. * Software distributed under the License is distributed on an
  12. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  13. * or implied. See the License for the specific language governing
  14. * rights and limitations under the License.
  15. *
  16. * The Original Code was created by Inprise Corporation
  17. * and its predecessors. Portions created by Inprise Corporation are
  18. * Copyright (C) Inprise Corporation.
  19. *
  20. * All Rights Reserved.
  21. * Contributor(s): ______________________________________.
  22. */
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #if TIME_WITH_SYS_TIME
  27. # include <sys/time.h>
  28. # include <time.h>
  29. #else
  30. # if HAVE_SYS_TIME_H
  31. # include <sys/time.h>
  32. # else
  33. # include <time.h>
  34. # endif
  35. #endif
  36. #include <ctype.h>
  37. #include <ibase.h>
  38. #include "align.h"
  39. #include "example.h"
  40. #define MAXLEN 1024
  41. process_statement (XSQLDA ** sqlda, char *query);
  42. void print_column (XSQLVAR * var);
  43. int get_statement (char * buf);
  44. /* ibase.h contains PARAMVARY, with the handicap that vary_string is unsigned char*
  45. instead of plain char*.
  46. Maybe we should fix ibase.h for users but without affecting the engine */
  47. /* typedef struct vary2 {
  48. unsigned short vary_length;
  49. char vary_string [1];
  50. } VARY2;
  51. */
  52. typedef PARAMVARY VARY2;
  53. isc_db_handle db = NULL;
  54. isc_tr_handle trans = NULL;
  55. isc_stmt_handle stmt = NULL;
  56. ISC_STATUS_ARRAY status;
  57. int ret;
  58. #ifndef ISC_INT64_FORMAT
  59. /* Define a format string for printf. Printing of 64-bit integers
  60. is not standard between platforms */
  61. #if (defined(_MSC_VER) && defined(WIN32))
  62. #define ISC_INT64_FORMAT "I64"
  63. #else
  64. #define ISC_INT64_FORMAT "ll"
  65. #endif
  66. #endif
  67. int main (int argc, char** argv)
  68. {
  69. int query[MAXLEN];
  70. XSQLDA * sqlda;
  71. char db_name[128];
  72. if (argc < 2)
  73. {
  74. printf("Enter the database name: ");
  75. gets(db_name);
  76. }
  77. else
  78. {
  79. strcpy(db_name, argv[1]);
  80. }
  81. if (isc_attach_database(status, 0, db_name, &db, 0, NULL))
  82. {
  83. printf("Could not open database %s\n", db_name);
  84. ERREXIT(status, 1);
  85. }
  86. /*
  87. * Allocate enough space for 20 fields.
  88. * If more fields get selected, re-allocate SQLDA later.
  89. */
  90. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH (20));
  91. sqlda->sqln = 20;
  92. sqlda->version = 1;
  93. /* Allocate a global statement */
  94. if (isc_dsql_allocate_statement(status, &db, &stmt))
  95. {
  96. free (sqlda);
  97. ERREXIT(status,1)
  98. }
  99. /*
  100. * Process SQL statements.
  101. */
  102. ret = get_statement((char *) query);
  103. /* Use break on error or exit */
  104. while (ret != 1)
  105. {
  106. /* We must pass the address of sqlda, in case it
  107. ** gets re-allocated
  108. */
  109. ret = process_statement(&sqlda,
  110. (char *) query);
  111. if (ret == 1)
  112. break;
  113. ret = get_statement((char *) query);
  114. }
  115. free (sqlda);
  116. if (trans)
  117. if (isc_commit_transaction(status, &trans))
  118. {
  119. ERREXIT(status,1);
  120. }
  121. if (isc_detach_database(status, &db))
  122. {
  123. ERREXIT(status,1);
  124. }
  125. return ret;
  126. }
  127. /*
  128. ** Function: process_statement
  129. ** Process submitted statement. On any fundamental error, return status 1,
  130. ** which will do an isc_print_status and exit the program.
  131. ** On user errors, found in parsing or executing go to status 2,
  132. ** which will print the error and continue.
  133. */
  134. process_statement (XSQLDA **sqldap, char *query)
  135. {
  136. int buffer[MAXLEN];
  137. XSQLDA *sqlda;
  138. XSQLVAR *var;
  139. short num_cols, i;
  140. short length, alignment, type, offset;
  141. int fetch_stat;
  142. static char stmt_info[] = { isc_info_sql_stmt_type };
  143. char info_buffer[20];
  144. short l;
  145. int statement_type;
  146. sqlda = *sqldap;
  147. /* Start a transaction if we are not in one */
  148. if (!trans)
  149. if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  150. {
  151. ERREXIT(status, 1)
  152. }
  153. if (isc_dsql_prepare(status, &trans, &stmt, 0, query, SQL_DIALECT_V6, sqlda))
  154. {
  155. ERREXIT(status,2)
  156. }
  157. /* What is the statement type of this statement?
  158. **
  159. ** stmt_info is a 1 byte info request. info_buffer is a buffer
  160. ** large enough to hold the returned info packet
  161. ** The info_buffer returned contains a isc_info_sql_stmt_type in the first byte,
  162. ** two bytes of length, and a statement_type token.
  163. */
  164. if (!isc_dsql_sql_info(status, &stmt, sizeof (stmt_info), stmt_info,
  165. sizeof (info_buffer), info_buffer))
  166. {
  167. l = (short) isc_vax_integer((char *) info_buffer + 1, 2);
  168. statement_type = isc_vax_integer((char *) info_buffer + 3, l);
  169. }
  170. /*
  171. * Execute a non-select statement.
  172. */
  173. if (!sqlda->sqld)
  174. {
  175. if (isc_dsql_execute(status, &trans, &stmt, SQL_DIALECT_V6, NULL))
  176. {
  177. ERREXIT(status,2)
  178. }
  179. /* Commit DDL statements if that is what sql_info says */
  180. if (trans && (statement_type == isc_info_sql_stmt_ddl))
  181. {
  182. printf ("\tCommitting...\n");
  183. if (isc_commit_transaction(status, &trans))
  184. {
  185. ERREXIT(status, 2)
  186. }
  187. }
  188. return 0;
  189. }
  190. /*
  191. * Process select statements.
  192. */
  193. num_cols = sqlda->sqld;
  194. /* Need more room. */
  195. if (sqlda->sqln < num_cols)
  196. {
  197. *sqldap = sqlda = (XSQLDA *) realloc(sqlda,
  198. XSQLDA_LENGTH (num_cols));
  199. sqlda->sqln = num_cols;
  200. sqlda->version = 1;
  201. if (isc_dsql_describe(status, &stmt, SQL_DIALECT_V6, sqlda))
  202. {
  203. ERREXIT(status,2)
  204. }
  205. num_cols = sqlda->sqld;
  206. }
  207. /*
  208. * Set up SQLDA.
  209. */
  210. for (var = sqlda->sqlvar, offset = 0, i = 0; i < num_cols; var++, i++)
  211. {
  212. length = alignment = var->sqllen;
  213. type = var->sqltype & ~1;
  214. if (type == SQL_TEXT)
  215. alignment = 1;
  216. else if (type == SQL_VARYING)
  217. {
  218. length += sizeof (short) + 1;
  219. alignment = sizeof (short);
  220. }
  221. /* RISC machines are finicky about word alignment
  222. ** So the output buffer values must be placed on
  223. ** word boundaries where appropriate
  224. */
  225. offset = FB_ALIGN(offset, alignment);
  226. var->sqldata = (char *) buffer + offset;
  227. offset += length;
  228. offset = FB_ALIGN(offset, sizeof (short));
  229. var->sqlind = (short*) ((char *) buffer + offset);
  230. offset += sizeof (short);
  231. }
  232. if (isc_dsql_execute(status, &trans, &stmt, SQL_DIALECT_V6, NULL))
  233. {
  234. ERREXIT(status,2)
  235. }
  236. /*
  237. * Print rows.
  238. */
  239. while ((fetch_stat = isc_dsql_fetch(status, &stmt, SQL_DIALECT_V6, sqlda)) == 0)
  240. {
  241. for (i = 0; i < num_cols; i++)
  242. {
  243. print_column((XSQLVAR *) &sqlda->sqlvar[i]);
  244. }
  245. printf("\n");
  246. }
  247. /* Close cursor */
  248. if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  249. {
  250. ERREXIT (status,2);
  251. }
  252. if (fetch_stat != 100L)
  253. {
  254. ERREXIT(status,2)
  255. }
  256. return 0;
  257. }
  258. /*
  259. * Print column's data.
  260. */
  261. void print_column (XSQLVAR *var)
  262. {
  263. short dtype;
  264. char data[MAXLEN], *p;
  265. char blob_s[20], date_s[25];
  266. VARY2 *vary2;
  267. short len;
  268. struct tm times;
  269. ISC_QUAD bid;
  270. dtype = var->sqltype & ~1;
  271. p = data;
  272. /* Null handling. If the column is nullable and null */
  273. if ((var->sqltype & 1) && (*var->sqlind < 0))
  274. {
  275. switch (dtype)
  276. {
  277. case SQL_TEXT:
  278. case SQL_VARYING:
  279. len = var->sqllen;
  280. break;
  281. case SQL_SHORT:
  282. len = 6;
  283. if (var->sqlscale > 0) len += var->sqlscale;
  284. break;
  285. case SQL_LONG:
  286. len = 11;
  287. if (var->sqlscale > 0) len += var->sqlscale;
  288. break;
  289. case SQL_INT64:
  290. len = 21;
  291. if (var->sqlscale > 0) len += var->sqlscale;
  292. break;
  293. case SQL_FLOAT:
  294. len = 15;
  295. break;
  296. case SQL_DOUBLE:
  297. len = 24;
  298. break;
  299. case SQL_TIMESTAMP:
  300. len = 24;
  301. break;
  302. case SQL_TYPE_DATE:
  303. len = 10;
  304. break;
  305. case SQL_TYPE_TIME:
  306. len = 13;
  307. break;
  308. case SQL_BLOB:
  309. case SQL_ARRAY:
  310. default:
  311. len = 17;
  312. break;
  313. }
  314. if ((dtype == SQL_TEXT) || (dtype == SQL_VARYING))
  315. sprintf(p, "%-*s ", len, "NULL");
  316. else
  317. sprintf(p, "%*s ", len, "NULL");
  318. }
  319. else
  320. {
  321. switch (dtype)
  322. {
  323. case SQL_TEXT:
  324. sprintf(p, "%.*s ", var->sqllen, var->sqldata);
  325. break;
  326. case SQL_VARYING:
  327. vary2 = (VARY2*) var->sqldata;
  328. vary2->vary_string[vary2->vary_length] = '\0';
  329. sprintf(p, "%-*s ", var->sqllen, vary2->vary_string);
  330. break;
  331. case SQL_SHORT:
  332. case SQL_LONG:
  333. case SQL_INT64:
  334. {
  335. ISC_INT64 value;
  336. short field_width;
  337. short dscale;
  338. switch (dtype)
  339. {
  340. case SQL_SHORT:
  341. value = (ISC_INT64) *(short *) var->sqldata;
  342. field_width = 6;
  343. break;
  344. case SQL_LONG:
  345. value = (ISC_INT64) *(int *) var->sqldata;
  346. field_width = 11;
  347. break;
  348. case SQL_INT64:
  349. value = (ISC_INT64) *(ISC_INT64 *) var->sqldata;
  350. field_width = 21;
  351. break;
  352. }
  353. dscale = var->sqlscale;
  354. if (dscale < 0)
  355. {
  356. ISC_INT64 tens;
  357. short i;
  358. tens = 1;
  359. for (i = 0; i > dscale; i--)
  360. tens *= 10;
  361. if (value >= 0)
  362. sprintf (p, "%*" ISC_INT64_FORMAT "d.%0*" ISC_INT64_FORMAT "d",
  363. field_width - 1 + dscale,
  364. (ISC_INT64) value / tens,
  365. -dscale,
  366. (ISC_INT64) value % tens);
  367. else if ((value / tens) != 0)
  368. sprintf (p, "%*" ISC_INT64_FORMAT "d.%0*" ISC_INT64_FORMAT "d",
  369. field_width - 1 + dscale,
  370. (ISC_INT64) (value / tens),
  371. -dscale,
  372. (ISC_INT64) -(value % tens));
  373. else
  374. sprintf (p, "%*s.%0*" ISC_INT64_FORMAT "d",
  375. field_width - 1 + dscale,
  376. "-0",
  377. -dscale,
  378. (ISC_INT64) -(value % tens));
  379. }
  380. else if (dscale)
  381. sprintf (p, "%*" ISC_INT64_FORMAT "d%0*d",
  382. field_width,
  383. (ISC_INT64) value,
  384. dscale, 0);
  385. else
  386. sprintf (p, "%*" ISC_INT64_FORMAT "d%",
  387. field_width,
  388. (ISC_INT64) value);
  389. }
  390. break;
  391. case SQL_FLOAT:
  392. sprintf(p, "%15g ", *(float *) (var->sqldata));
  393. break;
  394. case SQL_DOUBLE:
  395. sprintf(p, "%24f ", *(double *) (var->sqldata));
  396. break;
  397. case SQL_TIMESTAMP:
  398. isc_decode_timestamp((ISC_TIMESTAMP *)var->sqldata, &times);
  399. sprintf(date_s, "%04d-%02d-%02d %02d:%02d:%02d.%04d",
  400. times.tm_year + 1900,
  401. times.tm_mon+1,
  402. times.tm_mday,
  403. times.tm_hour,
  404. times.tm_min,
  405. times.tm_sec,
  406. ((ISC_TIMESTAMP *)var->sqldata)->timestamp_time % 10000);
  407. sprintf(p, "%*s ", 24, date_s);
  408. break;
  409. case SQL_TYPE_DATE:
  410. isc_decode_sql_date((ISC_DATE *)var->sqldata, &times);
  411. sprintf(date_s, "%04d-%02d-%02d",
  412. times.tm_year + 1900,
  413. times.tm_mon+1,
  414. times.tm_mday);
  415. sprintf(p, "%*s ", 10, date_s);
  416. break;
  417. case SQL_TYPE_TIME:
  418. isc_decode_sql_time((ISC_TIME *)var->sqldata, &times);
  419. sprintf(date_s, "%02d:%02d:%02d.%04d",
  420. times.tm_hour,
  421. times.tm_min,
  422. times.tm_sec,
  423. (*((ISC_TIME *)var->sqldata)) % 10000);
  424. sprintf(p, "%*s ", 13, date_s);
  425. break;
  426. case SQL_BLOB:
  427. case SQL_ARRAY:
  428. /* Print the blob id on blobs or arrays */
  429. bid = *(ISC_QUAD *) var->sqldata;
  430. sprintf(blob_s, "%08x:%08x", bid.gds_quad_high, bid.gds_quad_low);
  431. sprintf(p, "%17s ", blob_s);
  432. break;
  433. default:
  434. break;
  435. }
  436. }
  437. while (*p)
  438. {
  439. putchar(*p++);
  440. }
  441. }
  442. /*
  443. * Prompt for and get input.
  444. * Statements are terminated by a semicolon.
  445. */
  446. int get_statement (char *buf)
  447. {
  448. short c;
  449. char *p;
  450. int cnt;
  451. p = buf;
  452. cnt = 0;
  453. printf("SQL> ");
  454. for (;;)
  455. {
  456. if ((c = getchar()) == EOF)
  457. return 1;
  458. if (c == '\n')
  459. {
  460. /* accept "quit" or "exit" to terminate application */
  461. if (!strncmp(buf, "exit", 4))
  462. return 1;
  463. if (!strncmp(buf, "quit", 4))
  464. return 1;
  465. /* Search back through white space looking for ';'.*/
  466. while (cnt && isspace(*(p - 1)))
  467. {
  468. p--;
  469. cnt--;
  470. }
  471. if (*(p - 1) == ';')
  472. {
  473. *p++ = '\0';
  474. return 0;
  475. }
  476. *p++ = ' ';
  477. printf("CON> ");
  478. }
  479. else
  480. {
  481. *p++ = (char)c;
  482. }
  483. cnt++;
  484. }
  485. }