forked from lunarmodules/luasql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathls_odbc.c
1269 lines (1098 loc) · 28.8 KB
/
ls_odbc.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
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
/*
** LuaSQL, ODBC driver
** Authors: Pedro Rabinovitch, Roberto Ierusalimschy, Diego Nehab,
** Tomas Guisasola
** See Copyright Notice in license.html
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SQL_NOUNICODEMAP
#if defined(_WIN32)
#include <windows.h>
#include <sqlext.h>
#elif defined(INFORMIX)
#include "infxcli.h"
#else
#include "sql.h"
#include "sqltypes.h"
#include "sqlext.h"
#endif
#include "lua.h"
#include "lauxlib.h"
#include "luasql.h"
#define LUASQL_ENVIRONMENT_ODBC "ODBC environment"
#define LUASQL_CONNECTION_ODBC "ODBC connection"
#define LUASQL_STATEMENT_ODBC "ODBC statement"
#define LUASQL_CURSOR_ODBC "ODBC cursor"
/* holds data for parameter binding */
typedef struct {
SQLPOINTER buf;
SQLLEN len;
SQLLEN type;
} param_data;
/* general form of the driver objects */
typedef struct {
short closed;
int lock;
} obj_data;
typedef struct {
short closed;
int lock; /* lock count for open connections */
SQLHENV henv; /* environment handle */
} env_data;
typedef struct {
short closed;
int lock; /* lock count for open statements */
env_data *env; /* the connection's environment */
SQLHDBC hdbc; /* database connection handle */
} conn_data;
typedef struct {
short closed;
int lock; /* lock count for open cursors */
unsigned char hidden; /* these statement was created indirectly */
conn_data *conn; /* the statement's connection */
SQLHSTMT hstmt; /* statement handle */
SQLSMALLINT numparams; /* number of input parameters */
int paramtypes; /* reference to param type table */
param_data *params; /* array of parameter data */
} stmt_data;
typedef struct {
short closed;
stmt_data *stmt; /* the cursor's statement */
int numcols; /* number of columns */
int coltypes, colnames; /* reference to column information tables */
} cur_data;
/* we are lazy */
#define hENV SQL_HANDLE_ENV
#define hSTMT SQL_HANDLE_STMT
#define hDBC SQL_HANDLE_DBC
static int error(SQLRETURN a)
{
return (a != SQL_SUCCESS) && (a != SQL_SUCCESS_WITH_INFO) && (a != SQL_NO_DATA);
}
/*
** Registers a given C object in the registry to avoid GC
*/
static void luasql_registerobj(lua_State *L, int index, void *obj)
{
lua_pushvalue(L, index);
lua_pushlightuserdata(L, obj);
lua_pushvalue(L, -2);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pop(L, 1);
}
/*
** Unregisters a given C object from the registry
*/
static void luasql_unregisterobj(lua_State *L, void *obj)
{
lua_pushlightuserdata(L, obj);
lua_pushnil(L);
lua_settable(L, LUA_REGISTRYINDEX);
}
static int lock_obj(lua_State *L, int indx, void *ptr)
{
obj_data *obj = (obj_data *)ptr;
luasql_registerobj(L, indx, obj);
return ++obj->lock;
}
static int unlock_obj(lua_State *L, void *ptr)
{
obj_data *obj = (obj_data *)ptr;
if(--obj->lock == 0) {
luasql_unregisterobj(L, obj);
}
return obj->lock;
}
/*
** Check for valid environment.
*/
static env_data *getenvironment (lua_State *L, int i)
{
env_data *env = (env_data *)luaL_checkudata (L, i, LUASQL_ENVIRONMENT_ODBC);
luaL_argcheck (L, env != NULL, i, LUASQL_PREFIX"environment expected");
luaL_argcheck (L, !env->closed, i, LUASQL_PREFIX"environment is closed");
return env;
}
/*
** Check for valid connection.
*/
static conn_data *getconnection (lua_State *L, int i)
{
conn_data *conn = (conn_data *)luaL_checkudata (L, i, LUASQL_CONNECTION_ODBC);
luaL_argcheck (L, conn != NULL, i, LUASQL_PREFIX"connection expected");
luaL_argcheck (L, !conn->closed, i, LUASQL_PREFIX"connection is closed");
return conn;
}
/*
** Check for valid connection.
*/
static stmt_data *getstatement (lua_State *L, int i)
{
stmt_data *stmt = (stmt_data *)luaL_checkudata (L, i, LUASQL_STATEMENT_ODBC);
luaL_argcheck (L, stmt != NULL, i, LUASQL_PREFIX"statement expected");
luaL_argcheck (L, !stmt->closed, i, LUASQL_PREFIX"statement is closed");
return stmt;
}
/*
** Check for valid cursor.
*/
static cur_data *getcursor (lua_State *L, int i)
{
cur_data *cursor = (cur_data *)luaL_checkudata (L, i, LUASQL_CURSOR_ODBC);
luaL_argcheck (L, cursor != NULL, i, LUASQL_PREFIX"cursor expected");
luaL_argcheck (L, !cursor->closed, i, LUASQL_PREFIX"cursor is closed");
return cursor;
}
/*
** Pushes true and returns 1
*/
static int pass(lua_State *L) {
lua_pushboolean (L, 1);
return 1;
}
/*
** Fails with error message from ODBC
** Inputs:
** type: type of handle used in operation
** handle: handle used in operation
*/
static int fail(lua_State *L, const SQLSMALLINT type, const SQLHANDLE handle) {
SQLCHAR State[6];
SQLINTEGER NativeError;
SQLSMALLINT MsgSize, i;
SQLRETURN ret;
SQLCHAR Msg[SQL_MAX_MESSAGE_LENGTH];
luaL_Buffer b;
lua_pushnil(L);
luaL_buffinit(L, &b);
i = 1;
while (1) {
ret = SQLGetDiagRec(type, handle, i, State, &NativeError, Msg,
sizeof(Msg), &MsgSize);
if (ret == SQL_NO_DATA) break;
luaL_addlstring(&b, (char*)Msg, MsgSize);
luaL_addchar(&b, '\n');
i++;
}
luaL_pushresult(&b);
return 2;
}
static param_data *malloc_stmt_params(SQLSMALLINT c)
{
param_data *p = (param_data *)malloc(sizeof(param_data)*c);
memset(p, 0, sizeof(param_data)*c);
return p;
}
static param_data *free_stmt_params(param_data *data, SQLSMALLINT c)
{
if(data != NULL) {
param_data *p = data;
for(; c>0; ++p, --c) {
free(p->buf);
}
free(data);
}
return NULL;
}
/*
** Shuts a statement
** Returns non-zero on error
*/
static int stmt_shut(lua_State *L, stmt_data *stmt)
{
SQLRETURN ret;
unlock_obj(L, stmt->conn);
stmt->closed = 1;
luaL_unref (L, LUA_REGISTRYINDEX, stmt->paramtypes);
stmt->paramtypes = LUA_NOREF;
stmt->params = free_stmt_params(stmt->params, stmt->numparams);
ret = SQLFreeHandle(hSTMT, stmt->hstmt);
if (error(ret)) {
return 1;
}
return 0;
}
/*
** Closes a cursor directly
** Returns non-zero on error
*/
static int cur_shut(lua_State *L, cur_data *cur)
{
/* Nullify structure fields. */
cur->closed = 1;
if (error(SQLCloseCursor(cur->stmt->hstmt))) {
return fail(L, hSTMT, cur->stmt->hstmt);
}
/* release col tables */
luaL_unref (L, LUA_REGISTRYINDEX, cur->colnames);
luaL_unref (L, LUA_REGISTRYINDEX, cur->coltypes);
/* release statement and, if hidden, shut it */
if(unlock_obj(L, cur->stmt) == 0) {
if(cur->stmt->hidden) {
return stmt_shut(L, cur->stmt);
}
}
return 0;
}
/*
** Returns the name of an equivalent lua type for a SQL type.
*/
static const char *sqltypetolua (const SQLSMALLINT type) {
switch (type) {
case SQL_BIGINT: case SQL_TINYINT:
case SQL_INTEGER: case SQL_SMALLINT:
#if LUA_VERSION_NUM>=503
return "integer";
#endif
case SQL_NUMERIC: case SQL_DECIMAL:
case SQL_FLOAT: case SQL_REAL: case SQL_DOUBLE:
return "number";
case SQL_BINARY: case SQL_VARBINARY: case SQL_LONGVARBINARY:
return "binary"; /* !!!!!! nao seria string? */
case SQL_BIT:
return "boolean";
default:
return "string";
}
}
/*
** Retrieves data from the i_th column in the current row
** Input
** types: index in stack of column types table
** hstmt: statement handle
** i: column number
** Returns:
** 0 if successful, non-zero otherwise;
*/
static int push_column(lua_State *L, int coltypes, const SQLHSTMT hstmt,
SQLUSMALLINT i) {
const char *tname;
char type;
/* get column type from types table */
lua_rawgeti (L, LUA_REGISTRYINDEX, coltypes);
lua_rawgeti (L, -1, i); /* typename of the column */
tname = lua_tostring(L, -1);
if (!tname)
return luasql_faildirect(L, "invalid type in table.");
type = tname[1];
lua_pop(L, 2); /* pops type name and coltypes table */
/* deal with data according to type */
switch (type) {
/* nUmber */
case 'u': {
SQLDOUBLE num;
SQLLEN got;
SQLRETURN rc = SQLGetData(hstmt, i, SQL_C_DOUBLE, &num, 0, &got);
if (error(rc))
return fail(L, hSTMT, hstmt);
if (got == SQL_NULL_DATA)
lua_pushnil(L);
else
lua_pushnumber(L, num);
return 0;
}
#if LUA_VERSION_NUM>=503
/* iNteger */
case 'n': {
SQLINTEGER num;
SQLLEN got;
SQLRETURN rc = SQLGetData(hstmt, i, SQL_C_SLONG, &num, 0, &got);
if (error(rc))
return fail(L, hSTMT, hstmt);
if (got == SQL_NULL_DATA)
lua_pushnil(L);
else
lua_pushinteger(L, num);
return 0;
}
#endif
/* bOol */
case 'o': {
SQLCHAR b;
SQLLEN got;
SQLRETURN rc = SQLGetData(hstmt, i, SQL_C_BIT, &b, 0, &got);
if (error(rc))
return fail(L, hSTMT, hstmt);
if (got == SQL_NULL_DATA)
lua_pushnil(L);
else
lua_pushboolean(L, b);
return 0;
}
/* sTring */
case 't':
/* bInary */
case 'i': {
SQLSMALLINT stype = (type == 't') ? SQL_C_CHAR : SQL_C_BINARY;
SQLLEN got;
char *buffer;
luaL_Buffer b;
SQLRETURN rc;
luaL_buffinit(L, &b);
buffer = luaL_prepbuffer(&b);
rc = SQLGetData(hstmt, i, stype, buffer, LUAL_BUFFERSIZE, &got);
if (got == SQL_NULL_DATA) {
lua_pushnil(L);
return 0;
}
/* concat intermediary chunks */
while (rc == SQL_SUCCESS_WITH_INFO) {
if (got >= LUAL_BUFFERSIZE || got == SQL_NO_TOTAL) {
got = LUAL_BUFFERSIZE;
/* get rid of null termination in string block */
if (stype == SQL_C_CHAR) got--;
}
luaL_addsize(&b, got);
buffer = luaL_prepbuffer(&b);
rc = SQLGetData(hstmt, i, stype, buffer,
LUAL_BUFFERSIZE, &got);
}
/* concat last chunk */
if (rc == SQL_SUCCESS) {
if (got >= LUAL_BUFFERSIZE || got == SQL_NO_TOTAL) {
got = LUAL_BUFFERSIZE;
/* get rid of null termination in string block */
if (stype == SQL_C_CHAR) got--;
}
luaL_addsize(&b, got);
}
if (rc == SQL_ERROR) return fail(L, hSTMT, hstmt);
/* return everything we got */
luaL_pushresult(&b);
return 0;
}
}
return 0;
}
/*
** Get another row of the given cursor.
*/
static int cur_fetch (lua_State *L)
{
cur_data *cur = getcursor (L, 1);
SQLHSTMT hstmt = cur->stmt->hstmt;
int ret;
SQLRETURN rc = SQLFetch(hstmt);
if (rc == SQL_NO_DATA) {
/* automatically close cursor when end of resultset is reached */
if((ret = cur_shut(L, cur)) != 0) {
return ret;
}
lua_pushnil(L);
return 1;
} else {
if (error(rc)) {
return fail(L, hSTMT, hstmt);
}
}
if (lua_istable (L, 2)) {
SQLUSMALLINT i;
const char *opts = luaL_optstring (L, 3, "n");
int num = strchr (opts, 'n') != NULL;
int alpha = strchr (opts, 'a') != NULL;
for (i = 1; i <= cur->numcols; i++) {
ret = push_column (L, cur->coltypes, hstmt, i);
if (ret) {
return ret;
}
if (alpha) {
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->colnames);
lua_rawgeti (L, -1, i); /* gets column name */
lua_pushvalue (L, -3); /* duplicates column value */
lua_rawset (L, 2); /* table[name] = value */
lua_pop (L, 1); /* pops colnames table */
}
if (num) {
lua_rawseti (L, 2, i);
} else {
lua_pop (L, 1); /* pops value */
}
}
lua_pushvalue (L, 2);
return 1; /* return table */
} else {
SQLUSMALLINT i;
luaL_checkstack (L, cur->numcols, LUASQL_PREFIX"too many columns");
for (i = 1; i <= cur->numcols; i++) {
ret = push_column (L, cur->coltypes, hstmt, i);
if (ret) {
return ret;
}
}
return cur->numcols;
}
}
/*
** Closes a cursor.
*/
static int cur_close (lua_State *L)
{
int res;
cur_data *cur = (cur_data *) luaL_checkudata (L, 1, LUASQL_CURSOR_ODBC);
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;
}
if((res = cur_shut(L, cur)) != 0) {
return res;
}
return pass(L);
}
/*
** Returns the table with column names.
*/
static int cur_colnames (lua_State *L) {
cur_data *cur = (cur_data *) getcursor (L, 1);
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->colnames);
return 1;
}
/*
** Returns the table with column types.
*/
static int cur_coltypes (lua_State *L) {
cur_data *cur = (cur_data *) getcursor (L, 1);
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->coltypes);
return 1;
}
/*
** Creates two tables with the names and the types of the columns.
*/
static int create_colinfo (lua_State *L, cur_data *cur)
{
SQLCHAR buffer[256];
SQLSMALLINT namelen, datatype, i;
SQLRETURN ret;
int types, names;
lua_newtable(L);
types = lua_gettop (L);
lua_newtable(L);
names = lua_gettop (L);
for (i = 1; i <= cur->numcols; i++) {
ret = SQLDescribeCol(cur->stmt->hstmt, i, buffer, sizeof(buffer),
&namelen, &datatype, NULL, NULL, NULL);
if (ret == SQL_ERROR) {
lua_pop(L, 2);
return -1;
}
lua_pushstring (L, (char *)buffer);
lua_rawseti (L, names, i);
lua_pushstring(L, sqltypetolua(datatype));
lua_rawseti (L, types, i);
}
cur->colnames = luaL_ref (L, LUA_REGISTRYINDEX);
cur->coltypes = luaL_ref (L, LUA_REGISTRYINDEX);
return 0;
}
/*
** Creates a cursor table and leave it on the top of the stack.
*/
static int create_cursor (lua_State *L, int stmt_i, stmt_data *stmt,
const SQLSMALLINT numcols)
{
cur_data *cur;
lock_obj(L, stmt_i, stmt);
cur = (cur_data *) LUASQL_NEWUD(L, sizeof(cur_data));
luasql_setmeta (L, LUASQL_CURSOR_ODBC);
/* fill in structure */
cur->closed = 0;
cur->stmt = stmt;
cur->numcols = numcols;
cur->colnames = LUA_NOREF;
cur->coltypes = LUA_NOREF;
/* make and store column information table */
if(create_colinfo (L, cur) < 0) {
lua_pop(L, 1);
return fail(L, hSTMT, cur->stmt->hstmt);
}
return 1;
}
/*
** Returns the table with statement params.
*/
static int stmt_paramtypes (lua_State *L)
{
stmt_data *stmt = getstatement(L, 1);
lua_rawgeti (L, LUA_REGISTRYINDEX, stmt->paramtypes);
return 1;
}
static int stmt_close(lua_State *L)
{
stmt_data *stmt = (stmt_data *) luaL_checkudata (L, 1, LUASQL_STATEMENT_ODBC);
luaL_argcheck (L, stmt != NULL, 1, LUASQL_PREFIX"statement expected");
luaL_argcheck (L, stmt->lock == 0, 1,
LUASQL_PREFIX"there are still open cursors");
if (stmt->closed) {
lua_pushboolean (L, 0);
return 1;
}
if(stmt_shut(L, stmt)) {
return fail(L, hSTMT, stmt->hstmt);
}
lua_pushboolean(L, 1);
return 1;
}
/*
** Closes a connection.
*/
static int conn_close (lua_State *L)
{
SQLRETURN ret;
conn_data *conn = (conn_data *)luaL_checkudata(L,1,LUASQL_CONNECTION_ODBC);
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->lock > 0) {
lua_pushboolean(L, 0);
lua_pushstring(L, "There are open statements/cursors");
return 2;
}
/* Decrement connection counter on environment object */
unlock_obj(L, conn->env);
/* Nullify structure fields. */
conn->closed = 1;
ret = SQLDisconnect(conn->hdbc);
if (error(ret)) {
return fail(L, hDBC, conn->hdbc);
}
ret = SQLFreeHandle(hDBC, conn->hdbc);
if (error(ret)) {
return fail(L, hDBC, conn->hdbc);
}
return pass(L);
}
/*
** Executes the given statement
** Takes:
** * istmt : location of the statement object on the stack
*/
static int raw_execute(lua_State *L, int istmt)
{
SQLSMALLINT numcols;
stmt_data *stmt = getstatement(L, istmt);
/* execute the statement */
if (error(SQLExecute(stmt->hstmt))) {
return fail(L, hSTMT, stmt->hstmt);
}
/* determine the number of result columns */
if (error(SQLNumResultCols(stmt->hstmt, &numcols))) {
return fail(L, hSTMT, stmt->hstmt);
}
if (numcols > 0) {
/* if there is a results table (e.g., SELECT) */
return create_cursor(L, -1, stmt, numcols);
} else {
/* if action has no results (e.g., UPDATE) */
SQLLEN numrows;
if(error(SQLRowCount(stmt->hstmt, &numrows))) {
return fail(L, hSTMT, stmt->hstmt);
}
#if LUA_VERSION_NUM >= 503
lua_pushinteger(L, (lua_Integer)numrows);
#else
lua_pushnumber(L, (lua_Number)numrows);
#endif
return 1;
}
}
static int set_param(lua_State *L, stmt_data *stmt, int i, param_data *data)
{
static SQLLEN cbNull = SQL_NULL_DATA;
switch(lua_type(L, -1)) {
case LUA_TNIL: {
lua_pop(L, 1);
if(error(SQLBindParameter(stmt->hstmt, i, SQL_PARAM_INPUT, SQL_C_DEFAULT,
SQL_DOUBLE, 0, 0, NULL, 0, &cbNull))) {
return fail(L, hSTMT, stmt->hstmt);
}
}
break;
case LUA_TNUMBER: {
data->buf = malloc(sizeof(double));
*(double *)data->buf = (double)lua_tonumber(L, -1);
data->len = sizeof(double);
data->type = 0;
lua_pop(L, 1);
if(error(SQLBindParameter(stmt->hstmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE,
SQL_DOUBLE, 0, 0, data->buf, data->len,
&data->type))) {
return fail(L, hSTMT, stmt->hstmt);
}
}
break;
case LUA_TSTRING: {
const char *str = lua_tostring(L, -1);
size_t len = strlen(str);
data->buf = malloc(len+1);
memcpy((char *)data->buf, str, len+1);
data->len = len;
data->type = SQL_NTS;
lua_pop(L, 1);
if(error(SQLBindParameter(stmt->hstmt, i, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_CHAR, len, 0, data->buf, data->len,
&data->type))) {
return fail(L, hSTMT, stmt->hstmt);
}
}
break;
case LUA_TBOOLEAN: {
data->buf = malloc(sizeof(SQLCHAR));
*(SQLCHAR *)data->buf = (SQLCHAR)lua_toboolean(L, -1);
data->len = 0;
data->type = 0;
lua_pop(L, 1);
if(error(SQLBindParameter(stmt->hstmt, i, SQL_PARAM_INPUT, SQL_C_BIT,
SQL_BIT, 0, 0, data->buf, data->len,
&data->type))) {
return fail(L, hSTMT, stmt->hstmt);
}
}
break;
default:
lua_pop(L, 1);
return luasql_faildirect(L, "unsupported parameter type");
}
return 0;
}
/*
** Reads a param table into a statement
*/
static int raw_readparams_table(lua_State *L, stmt_data *stmt, int iparams)
{
SQLSMALLINT i;
param_data *data;
int res = 0;
free_stmt_params(stmt->params, stmt->numparams);
stmt->params = malloc_stmt_params(stmt->numparams);
data = stmt->params;
for(i=1; i<=stmt->numparams; ++i, ++data) {
/* not using lua_geti for backwards compat with Lua 5.1/LuaJIT */
lua_pushnumber(L, i);
lua_gettable(L, iparams);
res = set_param(L, stmt, i, data);
if(res != 0) {
return res;
}
}
return 0;
}
/*
** Reads a param table into a statement
*/
static int raw_readparams_args(lua_State *L, stmt_data *stmt, int arg, int ltop)
{
SQLSMALLINT i;
param_data *data;
int res = 0;
free_stmt_params(stmt->params, stmt->numparams);
stmt->params = malloc_stmt_params(stmt->numparams);
data = stmt->params;
for(i=1; i<=stmt->numparams; ++i, ++data, ++arg) {
if(arg > ltop) {
lua_pushnil(L);
} else {
lua_pushvalue(L, arg);
}
res = set_param(L, stmt, i, data);
if(res != 0) {
return res;
}
}
return 0;
}
/*
** Executes the prepared statement
** Lua Input: [params]
** params: A table of parameters to use in the statement
** Lua Returns
** cursor object: if there are results or
** row count: number of rows affected by statement if no results
*/
static int stmt_execute(lua_State *L)
{
int ltop = lua_gettop(L);
int res;
/* any parameters to use */
if(ltop > 1) {
stmt_data *stmt = getstatement(L, 1);
if(lua_type(L, 2) == LUA_TTABLE) {
res = raw_readparams_table(L, stmt, 2);
} else {
res = raw_readparams_args(L, stmt, 2, ltop);
}
if(res != 0) {
return res;
}
}
return raw_execute(L, 1);
}
/*
** creates a table of parameter types (maybe)
** Returns: the reference key of the table (noref if unable to build the table)
*/
static int desc_params(lua_State *L, stmt_data *stmt)
{
SQLSMALLINT i;
lua_newtable(L);
for(i=1; i <= stmt->numparams; ++i) {
SQLSMALLINT type, digits, nullable;
SQLULEN len;
/* fun fact: most ODBC drivers don't support this function (MS Access for
example), so we can't get a param type table */
if(error(SQLDescribeParam(stmt->hstmt, i, &type, &len, &digits, &nullable))) {
lua_pop(L,1);
return LUA_NOREF;
}
lua_pushstring(L, sqltypetolua(type));
lua_rawseti(L, -2, i);
}
return luaL_ref(L, LUA_REGISTRYINDEX);
}
/*
** Prepares a statement
** Lua Input: sql
** sql: The SQL statement to prepare
** Lua Returns:
** Statement object
*/
static int conn_prepare(lua_State *L)
{
conn_data *conn = getconnection(L, 1);
SQLCHAR *statement = (SQLCHAR *)luaL_checkstring(L, 2);
SQLHDBC hdbc = conn->hdbc;
SQLHSTMT hstmt;
SQLRETURN ret;
stmt_data *stmt;
ret = SQLAllocHandle(hSTMT, hdbc, &hstmt);
if (error(ret)) {
return fail(L, hDBC, hdbc);
}
ret = SQLPrepare(hstmt, statement, SQL_NTS);
if (error(ret)) {
ret = fail(L, hSTMT, hstmt);
SQLFreeHandle(hSTMT, hstmt);
return ret;
}
stmt = (stmt_data *)LUASQL_NEWUD(L, sizeof(stmt_data));
memset(stmt, 0, sizeof(stmt_data));
stmt->closed = 0;
stmt->lock = 0;
stmt->hidden = 0;
stmt->conn = conn;
stmt->hstmt = hstmt;
if(error(SQLNumParams(hstmt, &stmt->numparams))) {
int res;
lua_pop(L, 1);
res = fail(L, hSTMT, hstmt);
SQLFreeHandle(hSTMT, hstmt);
return res;
}
stmt->paramtypes = desc_params(L, stmt);
stmt->params = NULL;
/* activate statement object */
luasql_setmeta(L, LUASQL_STATEMENT_ODBC);
lock_obj(L, 1, conn);
return 1;
}
/*
** Executes a SQL statement directly
** Lua Input: sql, [params]
** sql: The SQL statement to execute
** params: A table of parameters to use in the SQL
** Lua Returns
** cursor object: if there are results or
** row count: number of rows affected by statement if no results
*/
static int conn_execute (lua_State *L)
{
stmt_data *stmt;
int res, istmt;
int ltop = lua_gettop(L);
/* prepare statement */
if((res = conn_prepare(L)) != 1) {
return res;
}
istmt = lua_gettop(L);
stmt = getstatement(L, istmt);
/* because this is a direct execute, statement is hidden from user */
stmt->hidden = 1;
/* do we have any parameters */
if(ltop > 2) {
if(lua_type(L, 3) == LUA_TTABLE) {
res = raw_readparams_table(L, stmt, 3);
} else {
res = raw_readparams_args(L, stmt, 3, ltop);
}
if(res != 0) {
return res;
}
}
/* do it */
res = raw_execute(L, istmt);
/* anything but a cursor, close the statement directly */
if(!lua_isuserdata(L, -res)) {
stmt_shut(L, stmt);
}
/* tidy up */
lua_remove(L, istmt);
return res;
}
/*
** Rolls back a transaction.
*/
static int conn_commit (lua_State *L) {
conn_data *conn = (conn_data *) getconnection (L, 1);
SQLRETURN ret = SQLEndTran(hDBC, conn->hdbc, SQL_COMMIT);
if (error(ret))
return fail(L, hSTMT, conn->hdbc);
else
return pass(L);
}