forked from CUBRID/cubrid-perl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbdimp.c
1970 lines (1694 loc) · 53.2 KB
/
dbdimp.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
/*
* Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the <ORGANIZATION> nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
*/
#include "cubrid.h"
#include <fcntl.h>
#ifdef WIN32
#define open(file, flag, mode) PerlLIO_open3(file, flag, mode)
#endif
/* Loading dynamic cascci library needs this header. */
#ifdef WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#define CUBRID_ER_MSG_LEN 1024
#define CUBRID_BUFFER_LEN 4096
static struct _error_message {
int err_code;
char *err_msg;
} cubrid_err_msgs[] = {
{CUBRID_ER_CANNOT_GET_COLUMN_INFO, "Cannot get column info"},
{CUBRID_ER_CANNOT_FETCH_DATA, "Cannot fetch data"},
{CUBRID_ER_WRITE_FILE, "Cannot write file"},
{CUBRID_ER_READ_FILE, "Cannot read file"},
{CUBRID_ER_NOT_LOB_TYPE, "Not a lob type, can only support SQL_BLOB or SQL_CLOB"},
{CUBRID_ER_INVALID_PARAM, "Invalid parameter"},
{CUBRID_ER_ROW_INDEX_EXCEEDED, "Row index exceeds the allowed range(1 ~ the number of affected rows)"},
{CUBRID_ER_EXPORT_NULL_LOB_INVALID, "Exporting NULL LOB is invalid"},
{0, ""}
};
static void *libcascci = NULL;
typedef int (*CCI_GET_LAST_INSERT_ID) (int con, void *buff,
T_CCI_ERROR * err_buf);
static CCI_GET_LAST_INSERT_ID cci_get_last_insert_id_fp = NULL;
/***************************************************************************
* Private function prototypes
**************************************************************************/
static int _dbd_db_end_tran (SV *dbh, imp_dbh_t *imp_dbh, int type);
static int _cubrid_lob_bind (SV *sv,
int index,
IV sql_type,
char *buf,
T_CCI_ERROR *error);
static int _cubrid_lob_new (int conn,
T_CCI_LOB *lob,
T_CCI_U_TYPE type,
T_CCI_ERROR *error);
static long long _cubrid_lob_size( T_CCI_LOB lob, T_CCI_U_TYPE type );
static int _cubrid_lob_write (int conn,
T_CCI_LOB lob,
T_CCI_U_TYPE type,
long long start_pos,
int length,
const char *buf,
T_CCI_ERROR *error);
static int _cubrid_lob_read (int conn,
T_CCI_LOB lob,
T_CCI_U_TYPE type,
long long start_pos,
int length,
char *buf,
T_CCI_ERROR *error);
static int _cubrid_lob_free (T_CCI_LOB lob, T_CCI_U_TYPE type);
static int _cubrid_fetch_schema (AV *rows_av,
int req_handle,
int col_count,
T_CCI_COL_INFO *col_info,
T_CCI_ERROR *error);
static int _cubrid_fetch_row (AV *av,
int req_handle,
int col_count,
T_CCI_COL_INFO *col_info,
T_CCI_ERROR *error);
/***************************************************************************
*
* Name: dbd_init
*
* Purpose: Called when the driver is installed by DBI
*
* Input: dbistate - pointer to the DBIS variable, used for some
* DBI internal things
*
* Returns: Nothing
*
**************************************************************************/
void
dbd_init( dbistate_t *dbistate )
{
DBIS = dbistate;
cci_init();
/*
* use cci_get_last_id if possible
* early distributed libraries don't include it
*/
#ifdef WIN32
libcascci = LoadLibrary ("cascci.dll");
if (libcascci != NULL) {
cci_get_last_insert_id_fp =
(CCI_GET_LAST_INSERT_ID) GetProcAddress (libcascci,
"cci_get_last_insert_id");
}
#else
libcascci = dlopen ("libcascci.so", RTLD_LAZY);
if (libcascci != NULL) {
cci_get_last_insert_id_fp = dlsym (libcascci, "cci_get_last_insert_id");
}
#endif
}
/***************************************************************************
*
* Name: dbd_discon_all
*
* Purpose: Disconnect all database handles at shutdown time
*
* Input: dbh - database handle being disconnecte
* imp_dbh - drivers private database handle data
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_discon_all( SV *drh, imp_drh_t *imp_drh )
{
cci_end ();
if (!PL_dirty && !SvTRUE(perl_get_sv("DBI::PERL_ENDING",0))) {
sv_setiv(DBIc_ERR(imp_drh), (IV)1);
sv_setpv(DBIc_ERRSTR(imp_drh), (char*)"disconnect_all not implemented");
return FALSE;
}
if (libcascci != NULL) {
#ifdef WIN32
FreeLibrary (libcascci);
#else
dlclose (libcascci);
#endif
}
return FALSE;
}
/***************************************************************************
*
* Name: handle_error
*
* Purpose: Called to associate an error code and an error message
* to some handle
*
* Input: h - the handle in error condition
* e - the error code
* error - the error message
*
* Returns: Nothing
*
**************************************************************************/
static int
get_error_msg( int err_code, char *err_msg )
{
int i;
for (i = 0; ; i++) {
if (!cubrid_err_msgs[i].err_code)
break;
if (cubrid_err_msgs[i].err_code == err_code) {
snprintf (err_msg, CUBRID_ER_MSG_LEN,
"ERROR: CLIENT, %d, %s",
err_code, cubrid_err_msgs[i].err_msg);
return 0;
}
}
return -1;
}
static void
handle_error( SV* h, int e, T_CCI_ERROR *error )
{
D_imp_xxh (h);
char msg[CUBRID_ER_MSG_LEN] = {'\0'};
SV *errstr;
if (DBIc_TRACE_LEVEL (imp_xxh) >= 2)
PerlIO_printf (DBILOGFP, "\t\t--> handle_error\n");
errstr = DBIc_ERRSTR (imp_xxh);
sv_setiv (DBIc_ERR (imp_xxh), (IV)e);
if (e < CUBRID_ER_START) {
get_error_msg (e, msg);
} else if (cci_get_error_msg (e, error, msg, CUBRID_ER_MSG_LEN) < 0) {
snprintf (msg, CUBRID_ER_MSG_LEN, "Unknown Error");
}
sv_setpv (errstr, msg);
if (DBIc_TRACE_LEVEL (imp_xxh) >= 2)
PerlIO_printf (DBIc_LOGPIO (imp_xxh), "%s\n", msg);
if (DBIc_TRACE_LEVEL (imp_xxh) >= 2)
PerlIO_printf (DBILOGFP, "\t\t<-- handle_error\n");
return;
}
/***************************************************************************
*
* Name: dbd_db_login6
*
* Purpose: Called for connecting to a database and logging in.
*
* Input: dbh - database handle being initialized
* imp_dbh - drivers private database handle data
* dbname - the database we want to log into; may be like
* "dbname:host" or "dbname:host:port"
* user - user name to connect as
* password - passwort to connect with
*
* Returns: Nothing
*
**************************************************************************/
int
dbd_db_login6( SV *dbh, imp_dbh_t *imp_dbh,
char *dbname, char *uid, char *pwd, SV *attr )
{
int con, res;
T_CCI_ERROR error;
if ((con = cci_connect_with_url_ex (dbname, uid, pwd, &error)) < 0)
{
handle_error (dbh, con, &error);
return FALSE;
}
imp_dbh->handle = con;
if ((res = cci_end_tran (con, CCI_TRAN_COMMIT, &error)) < 0)
{
handle_error (dbh, res, &error);
return FALSE;
}
DBIc_IMPSET_on(imp_dbh);
DBIc_ACTIVE_on(imp_dbh);
return TRUE;
}
/***************************************************************************
*
* Name: dbd_db_commit
* dbd_db_rollback
*
* Purpose: Commit/Rollback any pending transaction to the database.
*
* Input: dbh - database handle being commit/rollback
* imp_dbh - drivers private database handle data
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_db_commit( SV *dbh, imp_dbh_t *imp_dbh )
{
return _dbd_db_end_tran (dbh, imp_dbh, CCI_TRAN_COMMIT);
}
int
dbd_db_rollback( SV *dbh, imp_dbh_t *imp_dbh )
{
return _dbd_db_end_tran (dbh, imp_dbh, CCI_TRAN_ROLLBACK);
}
static int
_dbd_db_end_tran( SV *dbh, imp_dbh_t *imp_dbh, int type )
{
int res;
T_CCI_ERROR error;
if ((res = cci_end_tran (imp_dbh->handle, type, &error)) < 0) {
handle_error (dbh, res, &error);
return FALSE;
}
return TRUE;
}
/***************************************************************************
*
* Name: dbd_db_destroy
*
* Purpose: Our part of the dbh destructor
*
* Input: dbh - database handle being disconnected
* imp_dbh - drivers private database handle data
*
* Returns: Nothing
*
**************************************************************************/
void
dbd_db_destroy( SV *dbh, imp_dbh_t *imp_dbh )
{
if (DBIc_ACTIVE(imp_dbh)) {
(void)dbd_db_disconnect(dbh, imp_dbh);
}
DBIc_IMPSET_off(imp_dbh);
}
/***************************************************************************
*
* Name: dbd_db_disconnect
*
* Purpose: Disconnect a database handle from its database
*
* Input: dbh - database handle being disconnected
* imp_dbh - drivers private database handle data
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_db_disconnect( SV *dbh, imp_dbh_t *imp_dbh )
{
int res;
T_CCI_ERROR error;
DBIc_ACTIVE_off(imp_dbh);
if ((res = cci_disconnect (imp_dbh->handle, &error)) < 0) {
handle_error (dbh, res, &error);
return FALSE;
}
return TRUE;
}
/***************************************************************************
*
* Name: dbd_db_STORE_attrib
*
* Purpose: Function for storing dbh attributes
*
* Input: dbh - database handle being disconnected
* imp_dbh - drivers private database handle data
* keysv - the attribute name
* valuesv - the attribute value
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_db_STORE_attrib( SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv )
{
STRLEN kl;
char *key = SvPV (keysv,kl);
int on = SvTRUE (valuesv);
switch (kl) {
case 10:
if (strEQ("AutoCommit", key))
{
if (on)
{
cci_set_autocommit (imp_dbh->handle, CCI_AUTOCOMMIT_TRUE);
}
else
{
cci_set_autocommit (imp_dbh->handle, CCI_AUTOCOMMIT_FALSE);
}
DBIc_set (imp_dbh, DBIcf_AutoCommit, on);
return TRUE;
}
}
return FALSE;
}
/***************************************************************************
*
* Name: dbd_db_FETCH_attrib
*
* Purpose: Function for fetching dbh attributes
*
* Input: dbh - database handle being disconnected
* imp_dbh - drivers private database handle data
* keysv - the attribute name
* valuesv - the attribute value
*
* Returns: An SV*, if sucessfull; NULL otherwise
*
**************************************************************************/
SV *
dbd_db_FETCH_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv)
{
STRLEN kl;
char *key = SvPV (keysv, kl);
SV *retsv = Nullsv;
switch (kl) {
case 10:
if (strEQ("AutoCommit", key)) {
retsv = boolSV(DBIc_has(imp_dbh,DBIcf_AutoCommit));
}
break;
}
return sv_2mortal(retsv);
}
/**************************************************************************
*
* Name: dbd_db_last_insert_id
*
* Purpose: Returns a value identifying the row just inserted
*
* Input: dbh - database handle
* imp_dbh - drivers private database handle data
* catalog - NULL
* schema - NULL
* table - NULL
* field - NULL
* attr - NULL
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
SV *
dbd_db_last_insert_id( SV *dbh, imp_dbh_t *imp_dbh,
SV *catalog, SV *schema, SV *table, SV *field, SV *attr )
{
SV *sv;
char *name = NULL;
int res;
T_CCI_ERROR error;
if (cci_get_last_insert_id_fp != NULL) {
/* cci_get_last_id set last_id as con_handle's static buffer */
res = cci_get_last_insert_id (imp_dbh->handle, &name, &error);
} else {
/* cci_last_id set last_id as allocated string */
res = cci_last_insert_id (imp_dbh->handle, &name, &error);
}
if (res < 0) {
handle_error (dbh, res, &error);
return Nullsv;
}
if (!name) {
return Nullsv;
} else {
sv = newSVpvn (name, strlen(name));
/* free last_id which is allocated in cci_last_insert_id */
if (cci_get_last_insert_id_fp == NULL) {
#ifndef WIN32
free(name);
#endif
}
}
return sv_2mortal (sv);
}
/**************************************************************************
*
* Name: dbd_db_ping
*
* Purpose: Check whether the database server is still running and the
* connection to it is still working.
*
* Input: Nothing
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_db_ping( SV *dbh )
{
int res;
T_CCI_ERROR error;
char *query = "SELECT 1+1 from db_root /*+ shard_id(0) */";
int req_handle = 0, result = 0, ind = 0;
D_imp_dbh (dbh);
if ((res = cci_prepare (imp_dbh->handle, query, 0, &error)) < 0) {
goto ER_DB_PING;
}
req_handle = res;
if ((res = cci_execute (req_handle, 0, 0, &error)) < 0) {
goto ER_DB_PING;
}
while (1) {
res = cci_cursor (req_handle, 1, CCI_CURSOR_CURRENT, &error);
if (res == CCI_ER_NO_MORE_DATA) {
break;
}
if (res < 0) {
goto ER_DB_PING;
}
if ((res = cci_fetch (req_handle, &error)) < 0) {
goto ER_DB_PING;
}
if ((res = cci_get_data (req_handle, 1, CCI_A_TYPE_INT, &result, &ind)) < 0) {
goto ER_DB_PING;
}
if (result == 2) {
cci_close_req_handle (req_handle);
return TRUE;
}
}
cci_close_req_handle (req_handle);
ER_DB_PING:
handle_error (dbh, res, &error);
return FALSE;
}
/***************************************************************************
*
* Name: dbd_st_prepare
*
* Purpose: Called for preparing an SQL statement; our part of the
* statement handle constructor
*
* Input: sth - statement handle being initialized
* imp_sth - drivers private statement handle data
* statement - pointer to string with SQL statement
* attribs - statement attributes, currently not in use
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_st_prepare( SV *sth, imp_sth_t *imp_sth, char *statement, SV *attribs )
{
int res;
T_CCI_ERROR error;
D_imp_dbh_from_sth;
if ('\0' == *statement)
croak ("Cannot preapre empty statement");
imp_sth->conn = imp_dbh->handle;
imp_sth->col_count = -1;
imp_sth->sql_type = 0;
imp_sth->affected_rows = -1;
imp_sth->lob = NULL;
if ((res = cci_prepare (imp_sth->conn, statement, 0, &error)) < 0) {
handle_error (sth, res, &error);
return FALSE;
}
imp_sth->handle = res;
DBIc_NUM_PARAMS(imp_sth) = cci_get_bind_num (res);
DBIc_IMPSET_on(imp_sth);
return TRUE;
}
/***************************************************************************
*
* Name: dbd_st_execute
*
* Purpose: Called for execute the prepared SQL statement; our part of
* the statement handle constructor
*
* Input: sth - statement handle
* imp_sth - drivers private statement handle data
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_st_execute( SV *sth, imp_sth_t *imp_sth )
{
int res, option = 0, max_col_size = 0;
T_CCI_ERROR error;
T_CCI_COL_INFO *col_info;
T_CCI_SQLX_CMD sql_type;
int col_count;
if ((res = cci_execute (imp_sth->handle, option,
max_col_size, &error)) < 0) {
handle_error (sth, res, &error);
return -2;
}
col_info = cci_get_result_info (imp_sth->handle, &sql_type, &col_count);
if (sql_type == SQLX_CMD_SELECT && !col_info) {
handle_error(sth, CUBRID_ER_CANNOT_GET_COLUMN_INFO, NULL);
return -2;
}
imp_sth->col_info = col_info;
imp_sth->sql_type = sql_type;
imp_sth->col_count = col_count;
switch (sql_type) {
case SQLX_CMD_INSERT:
case SQLX_CMD_UPDATE:
case SQLX_CMD_DELETE:
case SQLX_CMD_CALL:
case SQLX_CMD_SELECT:
imp_sth->affected_rows = res;
break;
default:
imp_sth->affected_rows = -1;
}
if (sql_type == SQLX_CMD_SELECT) {
res = cci_cursor (imp_sth->handle, 1, CCI_CURSOR_CURRENT, &error);
if (res < 0 && res != CCI_ER_NO_MORE_DATA) {
handle_error (sth, res, &error);
return -2;
}
DBIc_NUM_FIELDS (imp_sth) = col_count;
DBIc_ACTIVE_on (imp_sth);
}
return imp_sth->affected_rows;
}
/***************************************************************************
*
* Name: dbd_st_fetch
*
* Purpose: Called for execute the prepared SQL statement; our part of
* the statement handle constructor
*
* Input: sth - statement handle being initialized
* imp_sth - drivers private statement handle data
*
* Returns: array of columns; the array is allocated by DBI via
* DBIS->get_fbav(imp_sth), even the values of the array
* are prepared, we just need to modify them appropriately
*
**************************************************************************/
AV *
dbd_st_fetch( SV *sth, imp_sth_t *imp_sth )
{
AV *av;
int res;
T_CCI_ERROR error;
if (DBIc_ACTIVE(imp_sth)) {
DBIc_ACTIVE_off(imp_sth);
}
res = cci_cursor (imp_sth->handle, 0, CCI_CURSOR_CURRENT, &error);
if (res == CCI_ER_NO_MORE_DATA) {
return Nullav;
} else if (res < 0) {
goto ERR_ST_FETCH;
}
if ((res = cci_fetch (imp_sth->handle, &error)) < 0) {
goto ERR_ST_FETCH;
}
av = DBIS->get_fbav(imp_sth);
if ((res = _cubrid_fetch_row (av,
imp_sth->handle,
imp_sth->col_count,
imp_sth->col_info,
&error)) < 0) {
goto ERR_ST_FETCH;
}
res = cci_cursor (imp_sth->handle, 1, CCI_CURSOR_CURRENT, &error);
if (res < 0 && res != CCI_ER_NO_MORE_DATA) {
goto ERR_ST_FETCH;
}
return av;
ERR_ST_FETCH:
handle_error (sth, res, &error);
return Nullav;
}
/***************************************************************************
*
* Name: dbd_st_finish
*
* Purpose: Called for freeing a CUBRID result
*
* Input: sth - statement handle being finished
* imp_sth - drivers private statement handle data
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_st_finish( SV *sth, imp_sth_t *imp_sth )
{
if (!DBIc_ACTIVE(imp_sth))
return TRUE;
DBIc_ACTIVE_off(imp_sth);
return TRUE;
}
/***************************************************************************
*
* Name: dbd_st_destroy
*
* Purpose: Our part of the statement handles destructor
*
* Input: sth - statement handle being destroyed
* imp_sth - drivers private statement handle data
*
* Returns: Nothing
*
**************************************************************************/
void
dbd_st_destroy( SV *sth, imp_sth_t *imp_sth )
{
if (imp_sth->handle) {
if (imp_sth->lob) {
int i;
for (i = 0; i < imp_sth->affected_rows; i++) {
_cubrid_lob_free (imp_sth->lob[i].lob, imp_sth->lob[i].type);
}
free (imp_sth->lob);
imp_sth->lob = NULL;
}
cci_close_req_handle (imp_sth->handle);
imp_sth->handle = 0;
imp_sth->col_count = -1;
imp_sth->sql_type = 0;
imp_sth->affected_rows = -1;
}
DBIc_IMPSET_off(imp_sth);
return;
}
/***************************************************************************
*
* Name: dbd_st_STORE_attrib
*
* Purpose: Modifies a statement handles attributes
*
* Input: sth - statement handle being initialized
* imp_sth - drivers private statement handle data
* keysv - attribute name
* valuesv - attribute value
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_st_STORE_attrib( SV *sth, imp_sth_t *imp_sth, SV *keysv, SV *valuesv )
{
return TRUE;
}
/***************************************************************************
*
* Name: dbd_st_FETCH_attrib
*
* Purpose: Retrieves a statement handles attributes
*
* Input: sth - statement handle
* imp_sth - drivers private statement handle data
* keysv - attribute name
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
SV *
dbd_st_FETCH_attrib( SV *sth, imp_sth_t *imp_sth, SV *keysv )
{
SV *retsv = Nullsv;
STRLEN kl;
char *key = SvPV (keysv, kl);
int i;
char col_name[128] = {'\0'};
switch (kl) {
case 4:
if (strEQ ("NAME", key)) {
AV *av = newAV ();
retsv = newRV_inc (sv_2mortal ((SV *)av));
for (i = 1; i<= imp_sth->col_count; i++) {
strcpy (col_name,
CCI_GET_RESULT_INFO_NAME (imp_sth->col_info, i));
av_store (av, i-1, newSVpv (col_name, 0));
}
}
else if (strEQ("TYPE", key)) {
int type;
AV *av = newAV ();
retsv = newRV_inc (sv_2mortal ((SV *)av));
for (i = 1; i<= imp_sth->col_count; i++) {
type = CCI_GET_RESULT_INFO_TYPE (imp_sth->col_info, i);
av_store (av, i-1, newSViv (type));
}
}
break;
case 5:
if (strEQ ("SCALE", key)) {
AV *av = newAV ();
int scale;
retsv = newRV_inc (sv_2mortal ((SV *)av));
for (i = 1; i<= imp_sth->col_count; i++) {
scale = CCI_GET_RESULT_INFO_SCALE (imp_sth->col_info, i);
av_store (av, i-1, newSViv (scale));
}
}
break;
case 8:
if (strEQ ("NULLABLE", key)) {
AV *av = newAV ();
int not_null;
retsv = newRV_inc (sv_2mortal ((SV *)av));
for (i = 1; i<= imp_sth->col_count; i++) {
not_null = CCI_GET_RESULT_INFO_IS_NON_NULL (imp_sth->col_info, i) ? 0 : 1;
av_store (av, i-1, newSViv (not_null));
}
}
break;
case 9:
if (strEQ ("PRECISION", key)) {
AV *av = newAV ();
int precision;
retsv = newRV_inc (sv_2mortal ((SV *)av));
for (i = 1; i<= imp_sth->col_count; i++) {
precision = CCI_GET_RESULT_INFO_PRECISION (imp_sth->col_info, i);
av_store (av, i-1, newSViv (precision));
}
}
break;
}
return sv_2mortal (retsv);
}
/***************************************************************************
*
* Name: dbd_st_blob_read (Not implement now)
*
* Purpose: Used for blob reads if the statement handles blob
*
* Input: sth - statement handle from which a blob will be
* fetched (currently not supported by DBD::cubrid)
* imp_sth - drivers private statement handle data
* field - field number of the blob
* offset - the offset of the field, where to start reading
* len - maximum number of bytes to read
* destrv - RV* that tells us where to store
* destoffset - destination offset
*
* Returns: TRUE for success, FALSE otherwise
*
**************************************************************************/
int
dbd_st_blob_read(SV *sth, imp_sth_t *imp_sth,
int field, long offset, long len, SV *destrv, long destoffset)
{
return FALSE;
}
/***************************************************************************
*
* Name: dbd_st_rows
*
* Purpose: used to get the number of rows affected by the SQL statement
* (INSERT, DELETE, UPDATE).
*
* Input: sth - statement handle
* imp_sth - drivers private statement handle data
*
* Returns: Number of rows affected by the SQL statement for success.
* -1, when SQL statement is not INSERT, DELETE or UPDATE.
*
**************************************************************************/
int
dbd_st_rows( SV * sth, imp_sth_t * imp_sth )
{
return imp_sth->affected_rows;
}
/***************************************************************************
*
* Name: cubrid_str2bit
*
* Purpose: convert string to bit
*
**************************************************************************/
static char* cubrid_str2bit(char* str)
{
int i=0,len=0,t=0;
char* buf=NULL;
int shift = 8;
if(str == NULL)
return NULL;
len = strlen(str);
if(0 == len%shift)
t =1;
buf = (char*)malloc(len/shift+1+1);
memset(buf,0,len/shift+1+1);
for(i=0;i<len;i++)
{
if(str[len-i-1] == '1')
{
buf[len/shift - i/shift-t] |= (1<<(i%shift));
}
else if(str[len-i-1] == '0')
{
//nothing
}
else
{
return NULL;
}
}
return buf;
}