forked from lunarmodules/luasql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathls_oci8.c
876 lines (792 loc) · 22.8 KB
/
ls_oci8.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
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
/*
** LuaSQL, Oracle driver
** Authors: Tomas Guisasola, Leonardo Godinho
** See Copyright Notice in license.html
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "oci.h"
#include "oratypes.h"
#include "ociapr.h"
#include "ocidem.h"
#include "lua.h"
#include "lauxlib.h"
#include "luasql.h"
#define LUASQL_ENVIRONMENT_OCI8 "Oracle environment"
#define LUASQL_CONNECTION_OCI8 "Oracle connection"
#define LUASQL_CURSOR_OCI8 "Oracle cursor"
typedef struct {
short closed;
int conn_counter;
OCIEnv *envhp;
OCIError *errhp; /* !!! */
} env_data;
typedef struct {
short closed;
short loggedon;
short auto_commit; /* 0 for manual commit */
int cur_counter;
int env; /* reference to environment */
OCISvcCtx *svchp; /* service handle */
OCIError *errhp; /* !!! */
} conn_data;
typedef union {
int i;
char *s;
double d;
} column_value;
typedef struct {
ub2 type; /* database type */
text *name; /* column name */
ub4 namelen; /* column name length */
ub2 max; /* maximum size */
sb2 null; /* is null? */
OCIDefine *define; /* define handle */
column_value val;
} column_data;
typedef struct {
short closed;
int conn; /* reference to connection */
int numcols; /* number of columns */
int colnames, coltypes; /* reference to column info tables */
int curr_tuple; /* next tuple to be read */
char *text; /* text of SQL statement */
OCIStmt *stmthp; /* statement handle */
OCIError *errhp; /* !!! */
column_data *cols; /* array of columns */
} cur_data;
int checkerr (lua_State *L, sword status, OCIError *errhp);
#define ASSERT(L,exp,err) {sword s = exp; if (s) return checkerr (L, s, err);}
/*
** Check for valid environment.
*/
static env_data *getenvironment (lua_State *L) {
env_data *env = (env_data *)luaL_checkudata (L, 1, LUASQL_ENVIRONMENT_OCI8);
luaL_argcheck (L, env != NULL, 1, LUASQL_PREFIX"environment expected");
luaL_argcheck (L, !env->closed, 1, LUASQL_PREFIX"environment is closed");
return env;
}
/*
** Check for valid connection.
*/
static conn_data *getconnection (lua_State *L) {
conn_data *conn = (conn_data *)luaL_checkudata (L, 1, LUASQL_CONNECTION_OCI8);
luaL_argcheck (L, conn != NULL, 1, LUASQL_PREFIX"connection expected");
luaL_argcheck (L, !conn->closed, 1, LUASQL_PREFIX"connection is closed");
return conn;
}
/*
** Check for valid cursor.
*/
static cur_data *getcursor (lua_State *L) {
cur_data *cur = (cur_data *)luaL_checkudata (L, 1, LUASQL_CURSOR_OCI8);
luaL_argcheck (L, cur != NULL, 1, LUASQL_PREFIX"cursor expected");
luaL_argcheck (L, !cur->closed, 1, LUASQL_PREFIX"cursor is closed");
return cur;
}
/*
** Push nil plus an error message.
*/
int checkerr (lua_State *L, sword status, OCIError *errhp) {
lua_pushnil (L);
switch (status) {
case OCI_SUCCESS:
lua_pushnil (L); /* !!!!!!!!!!!!! */
break;
case OCI_SUCCESS_WITH_INFO:
lua_pushstring (L, LUASQL_PREFIX"Success with info!");
break;
case OCI_NEED_DATA:
lua_pushstring (L, LUASQL_PREFIX"OCI_NEED_DATA");
break;
case OCI_NO_DATA:
lua_pushstring (L, LUASQL_PREFIX"OCI_NODATA");
break;
case OCI_ERROR: {
text errbuf[512];
sb4 errcode = 0;
OCIErrorGet (errhp, (ub4) 1, (text *) NULL, &errcode,
errbuf, (ub4) sizeof (errbuf), OCI_HTYPE_ERROR);
lua_pushstring (L, LUASQL_PREFIX);
lua_pushstring (L, errbuf);
lua_concat (L, 2);
break;
}
case OCI_INVALID_HANDLE:
lua_pushstring (L, LUASQL_PREFIX"OCI_INVALID_HANDLE");
break;
case OCI_STILL_EXECUTING:
lua_pushstring (L, LUASQL_PREFIX"OCI_STILL_EXECUTE");
break;
case OCI_CONTINUE:
lua_pushstring (L, LUASQL_PREFIX"OCI_CONTINUE");
break;
}
return 2;
}
/*
** Copy the column name to the column structure and convert it to lower case.
*/
static void copy_column_name (column_data *col, text *name) {
unsigned int i;
col->name = (text *)malloc (col->namelen);
memcpy (col->name, name, col->namelen);
for (i = 0; i < col->namelen; i++)
col->name[i] = tolower (col->name[i]);
}
/*
** Alloc buffers for column values.
*/
static int alloc_column_buffer (lua_State *L, cur_data *cur, int i) {
/* column index ranges from 1 to numcols */
/* C array index ranges from 0 to numcols-1 */
column_data *col = &(cur->cols[i-1]);
OCIParam *param;
text *name;
ASSERT (L, OCIParamGet (cur->stmthp, OCI_HTYPE_STMT, cur->errhp,
(dvoid **)¶m, i), cur->errhp);
ASSERT (L, OCIAttrGet (param, OCI_DTYPE_PARAM,
(dvoid *)&(name), (ub4 *)&(col->namelen),
OCI_ATTR_NAME, cur->errhp), cur->errhp);
copy_column_name (col, name);
ASSERT (L, OCIAttrGet (param, OCI_DTYPE_PARAM,
(dvoid *)&(col->type), (ub4 *)0, OCI_ATTR_DATA_TYPE,
cur->errhp), cur->errhp);
switch (col->type) {
case SQLT_VCS:
case SQLT_AFC:
case SQLT_AVC:
ASSERT (L, OCIAttrGet (param, OCI_DTYPE_PARAM,
(dvoid *)&(col->max), 0, OCI_ATTR_DATA_SIZE,
cur->errhp), cur->errhp);
col->val.s = calloc (col->max + 1, sizeof(col->val.s));
ASSERT (L, OCIDefineByPos (cur->stmthp, &(col->define),
cur->errhp, (ub4)i, col->val.s, col->max+1,
SQLT_STR /*col->type*/, (dvoid *)&(col->null), (ub2 *)0,
(ub2 *)0, (ub4) OCI_DEFAULT), cur->errhp);
break;
case SQLT_CHR:
case SQLT_STR:
ASSERT (L, OCIAttrGet (param, OCI_DTYPE_PARAM,
(dvoid *)&(col->max), 0, OCI_ATTR_DATA_SIZE,
cur->errhp), cur->errhp);
col->val.s = calloc (col->max * 2 + 1, sizeof(col->val.s));
ASSERT (L, OCIDefineByPos (cur->stmthp, &(col->define),
cur->errhp, (ub4)i, col->val.s, col->max * 2 + 1,
SQLT_STR /*col->type*/, (dvoid *)&(col->null), (ub2 *)0,
(ub2 *)0, (ub4) OCI_DEFAULT), cur->errhp);
break;
case SQLT_NUM:
case SQLT_FLT:
case SQLT_INT:
/* case SQLT_UIN: */
ASSERT (L, OCIDefineByPos (cur->stmthp, &(col->define),
cur->errhp, (ub4)i, &(col->val.d), sizeof(col->val.d),
SQLT_FLT, (dvoid *)&(col->null), (ub2 *)0,
(ub2 *)0, (ub4) OCI_DEFAULT), cur->errhp);
break;
case SQLT_CLOB: {
env_data *env;
conn_data *conn;
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->conn);
conn = (conn_data *)lua_touserdata (L, -1);
lua_rawgeti (L, LUA_REGISTRYINDEX, conn->env);
env = (env_data *)lua_touserdata (L, -1);
lua_pop (L, 2);
ASSERT (L, OCIDescriptorAlloc (env->envhp, (dvoid *)&(col->val.s),
OCI_DTYPE_LOB, (size_t)0, (dvoid **)0), cur->errhp);
ASSERT (L, OCIDefineByPos (cur->stmthp, &(col->define),
cur->errhp, (ub4)i, &(col->val.s), (sb4)sizeof(dvoid *),
SQLT_CLOB, (dvoid *)&(col->null), (ub2 *)0, (ub2 *)0,
OCI_DEFAULT), cur->errhp);
break;
}
default:
luaL_error (L, LUASQL_PREFIX"invalid type %d #%d", col->type, i);
break;
}
return 0;
}
/*
** Deallocate column buffers.
*/
static int free_column_buffers (lua_State *L, cur_data *cur, int i) {
/* column index ranges from 1 to numcols */
/* C array index ranges from 0 to numcols-1 */
column_data *col = &(cur->cols[i-1]);
free (col->name);
switch (col->type) {
case SQLT_INT:
case SQLT_FLT:
case SQLT_NUM:
break;
case SQLT_CHR:
case SQLT_STR:
case SQLT_VCS:
case SQLT_AFC:
case SQLT_AVC:
free(col->val.s);
break;
case SQLT_CLOB:
ASSERT (L, OCIDescriptorFree (col->val.s,
OCI_DTYPE_LOB), cur->errhp);
break;
default:
luaL_error (L, LUASQL_PREFIX"unknown type");
/*printf("free_buffers(): Unknown Type: %d count: %d\n",cols.item[count].type, count );*/
break;
}
return 0;
}
/*
** Push a value on top of the stack.
*/
static int pushvalue (lua_State *L, cur_data *cur, int i) {
/* column index ranges from 1 to numcols */
/* C array index ranges from 0 to numcols-1 */
column_data *col = &(cur->cols[i-1]);
if (col->null) {
/* Oracle NULL => Lua nil */
lua_pushnil (L);
return 1;
}
switch (col->type) {
case SQLT_NUM:
case SQLT_INT:
case SQLT_FLT:
lua_pushnumber (L, col->val.d);
break;
case SQLT_CHR:
case SQLT_STR:
case SQLT_VCS:
case SQLT_AFC:
case SQLT_AVC:
lua_pushstring (L, (char *)(col->val.s));
break;
case SQLT_CLOB: {
ub4 lob_len;
conn_data *conn;
env_data *env;
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->conn);
conn = lua_touserdata (L, -1);
lua_rawgeti (L, LUA_REGISTRYINDEX, conn->env);
env = lua_touserdata (L, -1);
lua_pop (L, 2);
ASSERT (L, OCILobGetLength (conn->svchp, cur->errhp,
(OCILobLocator *)col->val.s, &lob_len), cur->errhp);
if (lob_len > 0) {
char *lob_buffer=malloc(lob_len);
ub4 amount = lob_len;
ASSERT(L, OCILobRead(conn->svchp, cur->errhp,
(OCILobLocator *) col->val.s, &amount, (ub4) 1,
(dvoid *) lob_buffer, (ub4) lob_len, (dvoid *)0,
(sb4 (*) (dvoid *, CONST dvoid *, ub4, ub1)) 0,
(ub2) 0, (ub1) SQLCS_IMPLICIT), cur->errhp);
lua_pushlstring (L, lob_buffer, amount);
free(lob_buffer);
} else {
lua_pushstring (L, "");
}
break;
}
default:
luaL_error (L, LUASQL_PREFIX"unexpected error");
}
return 1;
}
/*
** Get another row of the given cursor.
*/
static int cur_fetch (lua_State *L) {
cur_data *cur = getcursor (L);
sword status = OCIStmtFetch (cur->stmthp, cur->errhp, 1,
OCI_FETCH_NEXT, OCI_DEFAULT);
if (status == OCI_NO_DATA) {
/* No more rows */
lua_pushnil (L);
return 1;
} else if (status != OCI_SUCCESS) {
/* Error */
return checkerr (L, status, cur->errhp);
}
if (lua_istable (L, 2)) {
int i;
const char *opts = luaL_optstring (L, 3, "n");
if (strchr (opts, 'n') != NULL)
/* Copy values to numerical indices */
for (i = 1; i <= cur->numcols; i++) {
int ret = pushvalue (L, cur, i);
if (ret != 1)
return ret;
lua_rawseti (L, 2, i);
}
if (strchr (opts, 'a') != NULL)
/* Copy values to alphanumerical indices */
for (i = 1; i <= cur->numcols; i++) {
column_data *col = &(cur->cols[i-1]);
int ret;
lua_pushlstring (L, col->name, col->namelen);
if ((ret = pushvalue (L, cur, i)) != 1)
return ret;
lua_rawset (L, 2);
}
lua_pushvalue(L, 2);
return 1; /* return table */
}
else {
int i;
luaL_checkstack (L, cur->numcols, LUASQL_PREFIX"too many columns");
for (i = 1; i <= cur->numcols; i++) {
int ret = pushvalue (L, cur, i);
if (ret != 1)
return ret;
}
return cur->numcols; /* return #numcols values */
}
}
/*
** Close the cursor on top of the stack.
** Return 1
*/
static int cur_close (lua_State *L) {
int i;
conn_data *conn;
cur_data *cur = (cur_data *)luaL_checkudata (L, 1, LUASQL_CURSOR_OCI8);
luaL_argcheck (L, cur != NULL, 1, LUASQL_PREFIX"cursor expected");
if (cur->closed) {
lua_pushboolean (L, 0);
lua_pushstring(L, "cursor is already closed");
return 2;
}
/* Deallocate buffers. */
for (i = 1; i <= cur->numcols; i++) {
int ret = free_column_buffers (L, cur, i);
if (ret)
return ret;
}
free (cur->cols);
free (cur->text);
/* Nullify structure fields. */
cur->closed = 1;
if (cur->stmthp)
OCIHandleFree ((dvoid *)cur->stmthp, OCI_HTYPE_STMT);
if (cur->errhp)
OCIHandleFree ((dvoid *)cur->errhp, OCI_HTYPE_ERROR);
/* Decrement cursor counter on connection object */
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->conn);
conn = lua_touserdata (L, -1);
conn->cur_counter--;
luaL_unref (L, LUA_REGISTRYINDEX, cur->conn);
luaL_unref (L, LUA_REGISTRYINDEX, cur->colnames);
luaL_unref (L, LUA_REGISTRYINDEX, cur->coltypes);
lua_pushboolean (L, 1);
return 1;
}
/*
** Return the list of field names as a table on top of the stack.
*/
static int cur_getcolnames (lua_State *L) {
cur_data *cur = getcursor (L);
if (cur->colnames != LUA_NOREF)
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->colnames);
else {
int i;
lua_newtable (L);
for (i = 1; i <= cur->numcols; i++) {
column_data *col = &(cur->cols[i-1]);
lua_pushlstring (L, col->name, col->namelen);
lua_rawseti (L, -2, i);
}
lua_pushvalue (L, -1);
cur->colnames = luaL_ref (L, LUA_REGISTRYINDEX);
}
return 1;
}
/*
**
*/
static char *getcolumntype (column_data *col) {
switch (col->type) {
case SQLT_CHR:
case SQLT_STR:
case SQLT_VCS:
case SQLT_AFC:
case SQLT_AVC:
return "string";
case SQLT_NUM:
case SQLT_FLT:
case SQLT_INT:
/* case SQLT_UIN: */
return "number";
case SQLT_CLOB:
return "string";
default:
return "";
}
}
/*
** Return the list of field types as a table on top of the stack.
*/
static int cur_getcoltypes (lua_State *L) {
cur_data *cur = getcursor (L);
if (cur->coltypes != LUA_NOREF)
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->coltypes);
else {
int i;
lua_newtable (L);
for (i = 1; i <= cur->numcols; i++) {
column_data *col = &(cur->cols[i-1]);
lua_pushnumber (L, i);
lua_pushstring (L, getcolumntype (col));
lua_rawset (L, -3);
}
lua_pushvalue (L, -1);
cur->coltypes = luaL_ref (L, LUA_REGISTRYINDEX);
}
return 1;
}
/*
** Push the number of rows.
*/
static int cur_numrows (lua_State *L) {
int n;
cur_data *cur = getcursor (L);
ASSERT (L, OCIAttrGet ((dvoid *) cur->stmthp, OCI_HTYPE_STMT, (dvoid *)&n,
(ub4)0, OCI_ATTR_NUM_ROWS, cur->errhp), cur->errhp);
lua_pushnumber (L, n);
return 1;
}
/*
** Close a Connection object.
*/
static int conn_close (lua_State *L) {
env_data *env;
conn_data *conn = (conn_data *)luaL_checkudata (L, 1, LUASQL_CONNECTION_OCI8);
luaL_argcheck (L, conn != NULL, 1, LUASQL_PREFIX"connection expected");
if (conn->closed) {
lua_pushboolean(L, 0);
lua_pushstring(L, "Connection is already closed");
return 2;
}
if (conn->cur_counter > 0){
lua_pushboolean(L, 0);
lua_pushstring(L, "There are open cursors");
return 2;
}
/* Nullify structure fields. */
conn->closed = 1;
if (conn->svchp) {
if (conn->loggedon)
OCILogoff (conn->svchp, conn->errhp);
else
OCIHandleFree ((dvoid *)conn->svchp, OCI_HTYPE_SVCCTX);
}
if (conn->errhp)
OCIHandleFree ((dvoid *)conn->errhp, OCI_HTYPE_ERROR);
/* Decrement connection counter on environment object */
lua_rawgeti (L, LUA_REGISTRYINDEX, conn->env);
env = lua_touserdata (L, -1);
env->conn_counter--;
luaL_unref (L, LUA_REGISTRYINDEX, conn->env);
lua_pushboolean (L, 1);
return 1;
}
/*
** Create a new Cursor object and push it on top of the stack.
*/
static int create_cursor (lua_State *L, int o, conn_data *conn, OCIStmt *stmt, const char *text) {
int i;
env_data *env;
cur_data *cur = (cur_data *)LUASQL_NEWUD(L, sizeof(cur_data));
luasql_setmeta (L, LUASQL_CURSOR_OCI8);
conn->cur_counter++;
/* fill in structure */
cur->closed = 0;
cur->numcols = 0;
cur->colnames = LUA_NOREF;
cur->coltypes = LUA_NOREF;
cur->curr_tuple = 0;
cur->stmthp = stmt;
cur->errhp = NULL;
cur->cols = NULL;
cur->text = strdup (text);
lua_pushvalue (L, o);
cur->conn = luaL_ref (L, LUA_REGISTRYINDEX);
/* error handler */
lua_rawgeti (L, LUA_REGISTRYINDEX, conn->env);
env = lua_touserdata (L, -1);
lua_pop (L, 1);
ASSERT (L, OCIHandleAlloc((dvoid *) env->envhp,
(dvoid **) &(cur->errhp), (ub4) OCI_HTYPE_ERROR, (size_t) 0,
(dvoid **) 0), conn->errhp);
/* get number of columns */
ASSERT (L, OCIAttrGet ((dvoid *)stmt, (ub4)OCI_HTYPE_STMT,
(dvoid *)&cur->numcols, (ub4 *)0, (ub4)OCI_ATTR_PARAM_COUNT,
cur->errhp), cur->errhp);
cur->cols = (column_data *)malloc (sizeof(column_data) * cur->numcols);
/* define output variables */
/* Oracle and Lua column indices ranges from 1 to numcols */
/* C array indices ranges from 0 to numcols-1 */
for (i = 1; i <= cur->numcols; i++) {
int ret = alloc_column_buffer (L, cur, i);
if (ret)
return ret;
}
return 1;
}
/*
** Execute an SQL statement.
** Return a Cursor object if the statement is a query, otherwise
** return the number of tuples affected by the statement.
*/
static int conn_execute (lua_State *L) {
env_data *env;
conn_data *conn = getconnection (L);
const char *statement = luaL_checkstring (L, 2);
sword status;
ub4 prefetch = 0;
ub4 iters;
ub4 mode;
ub2 type;
OCIStmt *stmthp;
/* get environment */
lua_rawgeti (L, LUA_REGISTRYINDEX, conn->env);
if (!lua_isuserdata (L, -1))
luaL_error(L,LUASQL_PREFIX"invalid environment in connection!");
env = (env_data *)lua_touserdata (L, -1);
/* statement handle */
ASSERT (L, OCIHandleAlloc ((dvoid *)env->envhp, (dvoid **)&stmthp,
OCI_HTYPE_STMT, (size_t)0, (dvoid **)0), conn->errhp);
ASSERT (L, OCIAttrSet ((dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
(dvoid *)&prefetch, (ub4)0, (ub4)OCI_ATTR_PREFETCH_ROWS,
conn->errhp), conn->errhp);
ASSERT (L, OCIStmtPrepare (stmthp, conn->errhp, (text *)statement,
(ub4) strlen(statement), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT),
conn->errhp);
/* statement type */
ASSERT (L, OCIAttrGet ((dvoid *)stmthp, (ub4) OCI_HTYPE_STMT,
(dvoid *)&type, (ub4 *)0, (ub4)OCI_ATTR_STMT_TYPE, conn->errhp),
conn->errhp);
if (type == OCI_STMT_SELECT)
iters = 0;
else
iters = 1;
if (conn->auto_commit)
mode = OCI_COMMIT_ON_SUCCESS;
else
mode = OCI_DEFAULT;
/* execute statement */
status = OCIStmtExecute (conn->svchp, stmthp, conn->errhp, iters,
(ub4)0, (CONST OCISnapshot *)NULL, (OCISnapshot *)NULL, mode);
if (status && (status != OCI_NO_DATA)) {
OCIHandleFree ((dvoid *)stmthp, OCI_HTYPE_STMT);
return checkerr (L, status, conn->errhp);
}
if (type == OCI_STMT_SELECT) {
/* create cursor */
return create_cursor (L, 1, conn, stmthp, statement);
} else {
/* return number of rows */
int rows_affected;
ASSERT (L, OCIAttrGet ((dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
(dvoid *)&rows_affected, (ub4 *)0,
(ub4)OCI_ATTR_ROW_COUNT, conn->errhp), conn->errhp);
OCIHandleFree ((dvoid *)stmthp, OCI_HTYPE_STMT);
lua_pushnumber (L, rows_affected);
return 1;
}
}
/*
** Commit the current transaction.
*/
static int conn_commit (lua_State *L) {
conn_data *conn = getconnection (L);
ASSERT (L, OCITransCommit (conn->svchp, conn->errhp, OCI_DEFAULT),
conn->errhp);
/*
if (conn->auto_commit == 0)
ASSERT (L, OCITransStart (conn->svchp, conn->errhp...
*/
return 0;
}
/*
** Rollback the current transaction.
*/
static int conn_rollback (lua_State *L) {
conn_data *conn = getconnection (L);
ASSERT (L, OCITransRollback (conn->svchp, conn->errhp, OCI_DEFAULT),
conn->errhp);
/*
if (conn->auto_commit == 0)
sql_begin(conn);
*/
return 0;
}
/*
** Set "auto commit" property of the connection.
** If 'true', then rollback current transaction.
** If 'false', then start a new transaction.
*/
static int conn_setautocommit (lua_State *L) {
conn_data *conn = getconnection (L);
if (lua_toboolean (L, 2)) {
conn->auto_commit = 1;
/* Undo active transaction. */
ASSERT (L, OCITransRollback (conn->svchp, conn->errhp,
OCI_DEFAULT), conn->errhp);
}
else {
conn->auto_commit = 0;
/* sql_begin(conn);*/
}
lua_pushboolean(L, 1);
return 1;
}
/*
** Connects to a data source.
*/
static int env_connect (lua_State *L) {
env_data *env = getenvironment (L);
const char *sourcename = luaL_checkstring(L, 2);
const char *username = luaL_optstring(L, 3, NULL);
const char *password = luaL_optstring(L, 4, NULL);
/* Sizes of strings */
size_t snlen = strlen(sourcename);
size_t userlen = (username) ? strlen(username) : 0;
size_t passlen = (password) ? strlen(password) : 0;
/* Alloc connection object */
conn_data *conn = (conn_data *)LUASQL_NEWUD(L, sizeof(conn_data));
/* fill in structure */
luasql_setmeta (L, LUASQL_CONNECTION_OCI8);
conn->env = LUA_NOREF;
conn->closed = 1;
conn->auto_commit = 1;
conn->cur_counter = 0;
conn->loggedon = 0;
conn->svchp = NULL;
conn->errhp = NULL;
lua_pushvalue (L, 1);
conn->env = luaL_ref (L, LUA_REGISTRYINDEX);
/* error handler */
ASSERT (L, OCIHandleAlloc((dvoid *) env->envhp,
(dvoid **) &(conn->errhp), /* !!! */
(ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0), env->errhp);
/* service handler */
/*ASSERT (L, OCIHandleAlloc((dvoid *) env->envhp,
(dvoid **) &(conn->svchp),
(ub4) OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0), conn->errhp);
*/
/* login */
ASSERT (L, OCILogon(env->envhp, conn->errhp, &(conn->svchp),
(CONST text*)username, userlen,
(CONST text*)password, passlen,
(CONST text*)sourcename, snlen), conn->errhp);
conn->closed = 0;
env->conn_counter++;
conn->loggedon = 1;
return 1;
}
/*
** Close environment object.
*/
static int env_close (lua_State *L) {
env_data *env = (env_data *)luaL_checkudata (L, 1, LUASQL_ENVIRONMENT_OCI8);
luaL_argcheck (L, env != NULL, 1, LUASQL_PREFIX"environment expected");
if (env->closed) {
lua_pushboolean (L, 0);
lua_pushstring(L, "env is already closed");
return 2;
}
if (env->conn_counter > 0){
lua_pushboolean(L, 0);
lua_pushstring(L, "There are open connections");
return 2;
}
env->closed = 1;
/* desalocar: env->errhp e env->envhp */
if (env->envhp)
OCIHandleFree ((dvoid *)env->envhp, OCI_HTYPE_ENV);
if (env->errhp)
OCIHandleFree ((dvoid *)env->errhp, OCI_HTYPE_ERROR);
lua_pushboolean (L, 1);
return 1;
}
/*
** Creates an Environment and returns it.
*/
static int create_environment (lua_State *L) {
env_data *env = (env_data *)LUASQL_NEWUD(L, sizeof(env_data));
luasql_setmeta (L, LUASQL_ENVIRONMENT_OCI8);
/* fill in structure */
env->closed = 0;
env->conn_counter = 0;
env->envhp = NULL;
env->errhp = NULL;
/* maybe OCI_SHARED and OCI_THREADED ??? */
if (OCIEnvCreate ( &(env->envhp), (ub4)OCI_DEFAULT, (dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
(void (*)(dvoid *, dvoid *)) 0,
(size_t) 0,
(dvoid **) 0))
luasql_faildirect (L, "couldn't create environment");
/* error handler */
ASSERT (L, OCIHandleAlloc((dvoid *) env->envhp,
(dvoid **) &(env->errhp), /* !!! */
(ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0), env->errhp);
return 1;
}
/*
** Create metatables for each class of object.
*/
static void create_metatables (lua_State *L) {
struct luaL_Reg environment_methods[] = {
{"__gc", env_close}, /* Should this method be changed? */
{"__close", env_close},
{"close", env_close},
{"connect", env_connect},
{NULL, NULL},
};
struct luaL_Reg connection_methods[] = {
{"__gc", conn_close}, /* Should this method be changed? */
{"__close", conn_close},
{"close", conn_close},
{"execute", conn_execute},
{"commit", conn_commit},
{"rollback", conn_rollback},
{"setautocommit", conn_setautocommit},
{NULL, NULL},
};
struct luaL_Reg cursor_methods[] = {
{"__gc", cur_close}, /* Should this method be changed? */
{"__close", cur_close},
{"close", cur_close},
{"getcolnames", cur_getcolnames},
{"getcoltypes", cur_getcoltypes},
{"fetch", cur_fetch},
{"numrows", cur_numrows},
{NULL, NULL},
};
luasql_createmeta (L, LUASQL_ENVIRONMENT_OCI8, environment_methods);
luasql_createmeta (L, LUASQL_CONNECTION_OCI8, connection_methods);
luasql_createmeta (L, LUASQL_CURSOR_OCI8, cursor_methods);
lua_pop (L, 3);
}
/*
** Creates the metatables for the objects and registers the
** driver open method.
*/
LUASQL_API int luaopen_luasql_oci8 (lua_State *L) {
struct luaL_Reg driver[] = {
{"oci8", create_environment},
{NULL, NULL},
};
create_metatables (L);
lua_newtable (L);
luaL_setfuncs (L, driver, 0);
luasql_set_info (L);
return 1;
}