forked from oleksiivorobiov/oracle_oci_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdemo2.c
509 lines (437 loc) · 12.7 KB
/
cdemo2.c
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
/* Copyright (c) 1991, 2006, Oracle. All rights reserved.
*/
/*
NAME
cdemo2.c - C demo Program # 2
MODIFIED (MM/DD/YY)
kmohan 03/28/06 - change hda to size_t
mjaeger 07/14/99 - bug 808870: OCCS: convert tabs, no long lines
ehayes 05/29/97 - Fix olint errors
echen 03/11/97 - fix a test bug
dchatter 01/05/96 - Creation of the Solaris version with hda as
ub4 array of HDA_SIZE/sizeof(ub4) for alignment
reasons.
*/
/* This program accepts arbitrary SQL statements from the user,
and processes the statement. Statements may be entered on
multiple lines, and must be terminated by a semi-colon.
If a query, the results are printed.
Statements are entered at the OCISQL prompt.
To quit the program, type EXIT at the OCISQL prompt.
The size of the HDA is defined by the HDA_SIZE constant,
which is declared in ocidem.h to be 256 bytes for 32-
bit architectures and 512 bytes for 64-bit architectures.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
/* Include OCI-specific headers. */
#include <oratypes.h>
#include <ocidfn.h>
#ifdef __STDC__
#include <ociapr.h>
#else
#include <ocikpr.h>
#endif
#include <ocidem.h>
/* Constants used in this program. */
#define MAX_BINDS 12
#define MAX_ITEM_BUFFER_SIZE 33
#define MAX_SELECT_LIST_SIZE 12
#define MAX_SQL_IDENTIFIER 31
#define PARSE_NO_DEFER 0
#define PARSE_V7_LNG 2
/* Define one logon data area and one cursor data area
Also define a host data area for olog.
(See ocidfn.h for declarations). */
Lda_Def lda;
Cda_Def cda;
size_t hda[HDA_SIZE/sizeof(size_t)];
/* Declare an array of bind values. */
text bind_values[MAX_BINDS][MAX_ITEM_BUFFER_SIZE];
/* Declare structures for query information. */
struct describe
{
sb4 dbsize;
sb2 dbtype;
sb1 buf[MAX_ITEM_BUFFER_SIZE];
sb4 buflen;
sb4 dsize;
sb2 precision;
sb2 scale;
sb2 nullok;
};
struct define
{
ub1 buf[MAX_ITEM_BUFFER_SIZE];
float flt_buf;
sword int_buf;
sb2 indp;
ub2 col_retlen, col_retcode;
};
/* Define arrays of describe and define structs. */
struct describe desc[MAX_SELECT_LIST_SIZE];
struct define def[MAX_SELECT_LIST_SIZE];
/* Declare this programs functions. */
sword connect_user();
sword describe_define();
sword do_binds();
void do_exit();
void oci_error();
sword get_sql_statement();
void print_header();
void print_rows();
/* Globals */
static text sql_statement[2048];
static sword sql_function;
static sword numwidth = 8;
main()
{
sword col, errno, n, ncols;
text *cp;
/* Connect to ORACLE. */
if (connect_user())
exit(-1);
/* Open a cursor, exit on error (unrecoverable). */
if (oopen(&cda, &lda, (text *) 0, -1, -1, (text *) 0, -1))
{
printf("Error opening cursor. Exiting...\n");
ologof(&lda);
exit(-1);
}
/* Process user's SQL statements. */
for (;;)
{
/* Get the statement, exit on "exit". */
if (get_sql_statement()) {
printf("\n");
do_exit(0);
}
/* Parse the statement; do not defer the parse,
so that errors come back right away. */
if (oparse(&cda, (text *) sql_statement, (sb4) -1,
(sword) PARSE_NO_DEFER, (ub4) PARSE_V7_LNG))
{
oci_error(&cda);
continue;
}
/* Save the SQL function code right after parse. */
sql_function = cda.ft;
/* Bind any input variables. */
if ((ncols = do_binds(&cda, sql_statement)) == -1)
continue;
/* If the statement is a query, describe and define
all select-list items before doing the oexec. */
if (sql_function == FT_SELECT)
if ((ncols = describe_define(&cda)) == -1)
continue;
/* Execute the statement. */
if (oexec(&cda))
{
oci_error(&cda);
continue;
}
/* Fetch and display the rows for the query. */
if (sql_function == FT_SELECT)
{
print_header(ncols);
print_rows(&cda, ncols);
}
/* Print the rows-processed count. */
if (sql_function == FT_SELECT ||
sql_function == FT_UPDATE ||
sql_function == FT_DELETE ||
sql_function == FT_INSERT)
printf("\n%d row%c processed.\n", cda.rpc,
cda.rpc == 1 ? ' ' : 's');
else
printf("\nStatement processed.\n");
} /* end for (;;) */
} /* end main() */
sword
connect_user()
{
text username[132];
text password[132];
sword n;
/* Three tries to connect. */
for (n = 3; --n >= 0; )
{
printf("Username: ");
gets((char *) username);
printf("Password: ");
gets((char *) password);
if (olog(&lda, (ub1 *)hda, username, -1, password, -1,
(text *) 0, -1, (ub4)OCI_LM_DEF))
{
printf("Cannot connect as %s.\n", username);
printf("Try again.\n\n");
}
else
{
return 0;
}
}
printf("Connection failed. Exiting...\n");
return -1;
}
/* Describe select-list items. */
sword
describe_define(cda)
Cda_Def *cda;
{
sword col, deflen, deftyp;
static ub1 *defptr;
/* Describe the select-list items. */
for (col = 0; col < MAX_SELECT_LIST_SIZE; col++)
{
desc[col].buflen = MAX_ITEM_BUFFER_SIZE;
if (odescr(cda, col + 1, &desc[col].dbsize,
&desc[col].dbtype, &desc[col].buf[0],
&desc[col].buflen, &desc[col].dsize,
&desc[col].precision, &desc[col].scale,
&desc[col].nullok))
{
/* Break on end of select list. */
if (cda->rc == VAR_NOT_IN_LIST)
break;
else
{
oci_error(cda);
return -1;
}
}
/* adjust sizes and types for display */
switch (desc[col].dbtype)
{
case NUMBER_TYPE:
desc[col].dbsize = numwidth;
/* Handle NUMBER with scale as float. */
if (desc[col].scale != 0)
{
defptr = (ub1 *) &def[col].flt_buf;
deflen = (sword) sizeof(float);
deftyp = FLOAT_TYPE;
desc[col].dbtype = FLOAT_TYPE;
}
else
{
defptr = (ub1 *) &def[col].int_buf;
deflen = (sword) sizeof(sword);
deftyp = INT_TYPE;
desc[col].dbtype = INT_TYPE;
}
break;
default:
if (desc[col].dbtype == DATE_TYPE)
desc[col].dbsize = 9;
if (desc[col].dbtype == ROWID_TYPE)
desc[col].dbsize = 18;
defptr = def[col].buf;
deflen = desc[col].dbsize > MAX_ITEM_BUFFER_SIZE ?
MAX_ITEM_BUFFER_SIZE : desc[col].dbsize + 1;
deftyp = STRING_TYPE;
break;
}
if (odefin(cda, col + 1,
defptr, deflen, deftyp,
-1, &def[col].indp, (text *) 0, -1, -1,
&def[col].col_retlen,
&def[col].col_retcode))
{
oci_error(cda);
return -1;
}
}
return col;
}
/* Bind input variables. */
sword
do_binds(cda, stmt_buf)
Cda_Def *cda;
text *stmt_buf;
{
sword i, in_literal, n;
text *cp, *ph;
/* Find and bind input variables for placeholders. */
for (i = 0, in_literal = FALSE, cp = stmt_buf;
*cp && i < MAX_BINDS; cp++)
{
if (*cp == '\'')
in_literal = ~in_literal;
if (*cp == ':' && !in_literal)
{
for (ph = ++cp, n = 0;
*cp && (isalnum(*cp) || *cp == '_')
&& n < MAX_SQL_IDENTIFIER;
cp++, n++
)
;
*cp = '\0';
printf("Enter value for %s: ", ph);
gets((char *) &bind_values[i][0]);
/* Do the bind, using obndrv().
NOTE: the bind variable address must be static.
This would not work if bind_values were an
auto on the do_binds stack. */
if (obndrv(cda, ph, -1, (ub1 *)&bind_values[i][0], -1,
VARCHAR2_TYPE,
-1, (sb2 *) 0, (text *) 0, -1, -1))
{
oci_error(cda);
return -1;
}
i++;
} /* end if (*cp == ...) */
} /* end for () */
return i;
}
/* Clean up and exit. LDA and CDA are
global. */
void
do_exit(rv)
sword rv;
{
if (oclose(&cda))
fputs("Error closing cursor!\n", stdout);
if (ologof(&lda))
fputs("Error logging off!\n", stdout);
exit(rv);
}
void
oci_error(cda)
Cda_Def *cda;
{
text msg[512];
sword n;
fputs("\n-- ORACLE ERROR --\n", stderr);
n = oerhms(&lda, cda->rc, msg, (sword) sizeof (msg));
fprintf(stderr, "%.*s", n, msg);
fprintf(stderr, "Processing OCI function %s\n",
oci_func_tab[cda->fc]);
fprintf(stderr, "Do you want to continue? [yn]: ");
fgets((char *) msg, (int) sizeof (msg), stdin);
if (*msg != '\n' && *msg != 'y' && *msg != 'Y')
do_exit(1);
fputc('\n', stdout);
}
sword
get_sql_statement()
{
text cbuf[1024];
text *cp;
sword stmt_level;
for (stmt_level = 1; ;)
{
if (stmt_level == 1)
{
/* Init statement buffer and print prompt. */
*sql_statement = '\0';
fputs("\nOCISQL> ", stdout);
}
else
{
printf("%3d ", stmt_level);
}
/* Get (part of) a SQL statement. */
gets((char *) cbuf);
if (*cbuf == '\0')
continue;
if (strncmp((char *) cbuf, "exit", 4) == 0)
return -1;
/* Concatenate to statement buffer. */
if (stmt_level > 1)
strcat((char *) sql_statement, " ");
strcat((char *) sql_statement, (char *) cbuf);
/* Check for possible terminator. */
cp = &sql_statement[strlen((char *) sql_statement) - 1];
while (isspace(*cp))
cp--;
if (*cp == ';')
{
*cp = '\0';
break;
}
stmt_level++;
}
return 0;
}
void
print_header(ncols)
sword ncols;
{
sword col, n;
fputc('\n', stdout);
for (col = 0; col < ncols; col++)
{
n = desc[col].dbsize - desc[col].buflen;
if (desc[col].dbtype == FLOAT_TYPE ||
desc[col].dbtype == INT_TYPE)
{
printf("%*c", n, ' ');
printf("%*.*s", desc[col].buflen,
desc[col].buflen, desc[col].buf);
}
else
{
printf("%*.*s", desc[col].buflen,
desc[col].buflen, desc[col].buf);
printf("%*c", n, ' ');
}
fputc(' ', stdout);
}
fputc('\n', stdout);
for (col = 0; col < ncols; col++)
{
for (n = desc[col].dbsize; --n >= 0; )
fputc('-', stdout);
fputc(' ', stdout);
}
fputc('\n', stdout);
}
void
print_rows(cda, ncols)
Cda_Def *cda;
sword ncols;
{
sword col, n;
for (;;)
{
fputc('\n', stdout);
/* Fetch a row. Break on end of fetch,
disregard null fetch "error". */
if (ofetch(cda))
{
if (cda->rc == NO_DATA_FOUND)
break;
if (cda->rc != NULL_VALUE_RETURNED)
oci_error(cda);
}
for (col = 0; col < ncols ; col++)
{
/* Check col. return code for null. If
null, print n spaces, else print value. */
if (def[col].indp < 0)
printf("%*c", desc[col].dbsize, ' ');
else
{
switch (desc[col].dbtype)
{
case FLOAT_TYPE:
printf("%*.*f", numwidth, 2, def[col].flt_buf);
break;
case INT_TYPE:
printf("%*d", numwidth, def[col].int_buf);
break;
default:
printf("%s", def[col].buf);
n = desc[col].dbsize - strlen((char *) def[col].buf);
if (n > 0)
printf("%*c", n, ' ');
break;
}
}
fputc(' ', stdout);
}
} /* end for (;;) */
}