-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gixsql.cpp
1365 lines (1100 loc) · 39.7 KB
/
gixsql.cpp
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) 2021 Marco Ridoni
* Copyright (C) 2013 Tokyo System House Co.,Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3,
* or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING.LIB. If
* not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <cstdbool>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
#include <io.h>
#else
#include <unistd.h>
#endif
#include <math.h>
#include "Connection.h"
#include "ConnectionManager.h"
#include "Cursor.h"
#include "CursorManager.h"
#include "DbInterfaceFactory.h"
#include "DataSourceInfo.h"
#include "SqlVar.h"
#include "SqlVarList.h"
#include "IDbInterface.h"
#include "IConnection.h"
#include "Logger.h"
#include "utils.h"
#include "gixsql.h"
#include "platform.h"
#include "Logger.h"
#include "cobol_var_types.h"
#define FAIL_ON_ERROR(_rc, _st, _dbi, _err) if (_rc != DBERR_NO_ERROR) { \
setStatus(_st, _dbi, _err); \
return RESULT_FAILED; \
}
struct query_info {
char* pname; // default
char* query;
int nParams;
};
static ConnectionManager connection_manager;
static CursorManager cursor_manager;
static void sqlca_initialize(struct sqlca_t*);
static int setStatus(struct sqlca_t* st, IDbInterface* dbi, int err);
static bool get_autocommit(DataSourceInfo*);
static bool get_fixup_params(DataSourceInfo*);
static std::string get_client_encoding(DataSourceInfo*);
static void init_sql_var_list(void);
/* sql var list */
SqlVarList _current_sql_var_list;
SqlVarList _res_sql_var_list;
static int _gixsqlExec(Connection* conn, struct sqlca_t* st, char* _query);
static int _gixsqlExecParams(Connection* conn, struct sqlca_t* st, char* _query, unsigned int nParams);
static int _gixsqlCursorDeclare(struct sqlca_t* st, Connection* conn, std::string connection_name, std::string cursor_name, int with_hold, void* d_query, int query_tl, int nParams);
static int _gixsqlExecPrepared(sqlca_t* st, void* d_connection_id, int connection_id_tl, char* stmt_name, int nParams, IDbInterface** _dbi);
static int _gixsqlConnectReset(struct sqlca_t* st, const std::string& connection_id);
static std::string get_hostref_or_literal(void* data, int connection_id_tl);
int __norec_sqlcode = GIXSQL_DEFAULT_NO_REC_CODE;
static void
sqlca_initialize(struct sqlca_t* sqlca)
{
memcpy((char*)sqlca, (char*)&sqlca_init, sizeof(struct sqlca_t));
}
LIBGIXSQL_API int
GIXSQLConnect(struct sqlca_t* st, void* d_data_source, int data_source_tl, void* d_connection_id, int connection_id_tl,
void* d_dbname, int dbname_tl, void* d_username, int username_tl, void* d_password, int password_tl)
{
spdlog::debug(FMT_FILE_FUNC "GIXSQLConnect start", __FILE__, __func__);
std::string data_source_info;
std::string connection_id;
std::string dbname;
std::string username;
std::string password;
// CONNECT ... AS
connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
dbname = get_hostref_or_literal(d_dbname, dbname_tl);
data_source_info = get_hostref_or_literal(d_data_source, data_source_tl);
username = get_hostref_or_literal(d_username, username_tl);
password = get_hostref_or_literal(d_password, password_tl);
sqlca_initialize(st);
trim(connection_id);
if (!connection_id.empty() && connection_manager.exists(connection_id)) {
spdlog::error("Connection already defined: {}", connection_id);
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_FAILED;
}
DataSourceInfo* data_source = new DataSourceInfo();
int rc = data_source->init(data_source_info, dbname, username, password);
if (rc != 0) {
spdlog::error("Cannot initialize connection parameters, aborting, data source is [{}]", data_source_info);
setStatus(st, NULL, DBERR_CONN_INIT_ERROR);
return RESULT_FAILED;
}
std::string dbtype = data_source->getDbType();
IDbInterface* dbi = DbInterfaceFactory::getInterface(dbtype, gixsql_logger);
if (dbi == NULL) {
spdlog::error("Cannot initialize connection parameters, aborting, data source is [{}]", dbtype);
setStatus(st, NULL, DBERR_CONN_INVALID_DBTYPE);
return RESULT_FAILED;
}
std::shared_ptr<IConnectionOptions> opts(new IConnectionOptions());
opts->autocommit = get_autocommit(data_source);;
opts->fixup_parameters = get_fixup_params(data_source);
opts->client_encoding = get_client_encoding(data_source);
spdlog::trace(FMT_FILE_FUNC "Connection string : {}", __FILE__, __func__, data_source->get());
spdlog::trace(FMT_FILE_FUNC "Data source info : {}", __FILE__, __func__, data_source->dump());
spdlog::trace(FMT_FILE_FUNC "Autocommit : {}", __FILE__, __func__, opts->autocommit);
spdlog::trace(FMT_FILE_FUNC "Fix up parameters : {}", __FILE__, __func__, opts->fixup_parameters);
spdlog::trace(FMT_FILE_FUNC "Client encoding : {}", __FILE__, __func__, opts->client_encoding);
rc = dbi->connect(data_source, opts.get());
if (rc != DBERR_NO_ERROR) {
setStatus(st, dbi, DBERR_CONNECTION_FAILED);
return RESULT_FAILED;
}
if (opts->autocommit) {
rc = dbi->begin_transaction();
if (rc != DBERR_NO_ERROR) {
setStatus(st, dbi, DBERR_BEGIN_TX_FAILED);
dbi->terminate_connection();
return RESULT_FAILED;
}
}
Connection* c = connection_manager.create();
c->setName(connection_id); // it might still be empty, the connection manager will assign a default name
c->setConnectionOptions(opts.get()); // Generic/gobal connection options, separate from driver-specific options that reside only in the data source info
c->setConnectionInfo(data_source);
c->setDbInterface(dbi);
c->setOpened(true);
connection_manager.add(c);
dbi->set_owner((IConnection*)c);
spdlog::debug(FMT_FILE_FUNC "connection success. connection id# = {}, connection id = [{}]", __FILE__, __func__, c->getId(), connection_id);
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
int _gixsqlConnectReset(struct sqlca_t* st, const std::string& connection_id)
{
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
return RESULT_FAILED;
}
cursor_manager.clearConnectionCursors(conn->getId(), true);
IDbInterface* dbi = conn->getDbInterface();
int rc = dbi->reset();
FAIL_ON_ERROR(rc, st, dbi, DBERR_CONN_RESET_FAILED)
connection_manager.remove(conn);
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int
GIXSQLConnectReset(struct sqlca_t* st, void* d_connection_id, int connection_id_tl)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLConnectReset start", __FILE__, __func__);
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
if (connection_id != "*") {
return _gixsqlConnectReset(st, connection_id);
}
else {
spdlog::trace(FMT_FILE_FUNC "GIXSQLConnectReset: resetting all connections", __FILE__, __func__);
for (Connection* c : connection_manager.list()) {
spdlog::trace(FMT_FILE_FUNC "GIXSQLConnectReset: trying to reset: [{}]", __FILE__, __func__, c->getName());
int rc = _gixsqlConnectReset(st, c->getName());
if (rc != RESULT_SUCCESS)
return rc;
}
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
}
LIBGIXSQL_API int
GIXSQLExec(struct sqlca_t* st, void* d_connection_id, int connection_id_tl, char* _query)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLExec start", __FILE__, __func__);
spdlog::trace(FMT_FILE_FUNC "GIXSQLExec SQL: {}", __FILE__, __func__, _query);
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::error("Can't find a connection");
setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
return RESULT_FAILED;
}
sqlca_initialize(st);
if (_query == NULL || strlen(_query) == 0) {
spdlog::error("Empty query");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
return _gixsqlExec(conn, st, _query);
}
LIBGIXSQL_API int
GIXSQLExecImmediate(struct sqlca_t* st, void* d_connection_id, int connection_id_tl, void* d_query, int query_tl)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLExecImmediate start", __FILE__, __func__);
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::error("Can't find a connection");
setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
return RESULT_FAILED;
}
std::string query = get_hostref_or_literal(d_query, query_tl);
spdlog::trace(FMT_FILE_FUNC "GIXSQLExecImmediate SQL: {}", __FILE__, __func__, query);
sqlca_initialize(st);
if (query.empty()) {
spdlog::error("Empty query");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
return _gixsqlExec(conn, st, (char*)query.c_str());
}
static int _gixsqlExec(Connection* conn, struct sqlca_t* st, char* _query)
{
std::string query = _query;
int rc = 0;
IDbInterface* dbi = conn->getDbInterface();
if (is_commit_or_rollback_statement(query)) {
cursor_manager.clearConnectionCursors(conn->getId(), false);
if (conn->getConnectionOptions()->autocommit) {
rc = dbi->end_transaction(query);
FAIL_ON_ERROR(rc, st, dbi, DBERR_END_TX_FAILED)
rc = dbi->begin_transaction();
FAIL_ON_ERROR(rc, st, dbi, DBERR_BEGIN_TX_FAILED)
}
else {
rc = dbi->exec(query);
FAIL_ON_ERROR(rc, st, dbi, DBERR_SQL_ERROR)
}
}
else {
rc = dbi->exec(query);
FAIL_ON_ERROR(rc, st, dbi, DBERR_SQL_ERROR)
if (is_dml_statement(query) && conn->getConnectionOptions()->autocommit) {
rc = dbi->end_transaction("COMMIT");
FAIL_ON_ERROR(rc, st, dbi, DBERR_END_TX_FAILED)
rc = dbi->begin_transaction();
FAIL_ON_ERROR(rc, st, dbi, DBERR_BEGIN_TX_FAILED)
}
}
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int
GIXSQLExecParams(struct sqlca_t* st, void* d_connection_id, int connection_id_tl, char* _query, int nParams)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLExecParams - SQL: {}", __FILE__, __func__, _query);
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::error("Can't find a connection");
setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
return RESULT_FAILED;
}
if (_query == NULL || strlen(_query) == 0) {
spdlog::error("Empty query");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
sqlca_initialize(st);
if (_current_sql_var_list.size() > nParams) {
setStatus(st, NULL, DBERR_TOO_MANY_ARGUMENTS);
return RESULT_FAILED;
}
else if (_current_sql_var_list.size() < nParams) {
setStatus(st, NULL, DBERR_TOO_FEW_ARGUMENTS);
return RESULT_FAILED;
}
return _gixsqlExecParams(conn, st, _query, nParams);
}
static int _gixsqlExecParams(Connection* conn, struct sqlca_t* st, char* _query, unsigned int nParams)
{
std::vector<std::string> params;
std::vector<int> param_types;
std::vector<int> param_lengths;
std::vector<SqlVar*>::iterator it;
// set parameters
for (it = _current_sql_var_list.begin(); it != _current_sql_var_list.end(); it++) {
params.push_back((*it)->getRealData());
param_types.push_back((*it)->getType());
param_lengths.push_back((*it)->getLength());
}
std::string query = _query;
int rc = 0;
IDbInterface* dbi = conn->getDbInterface();
if (!dbi)
FAIL_ON_ERROR(1, st, dbi, DBERR_SQL_ERROR)
if (is_commit_or_rollback_statement(query)) {
cursor_manager.clearConnectionCursors(conn->getId(), false);
if (conn->getConnectionOptions()->autocommit) {
rc = dbi->end_transaction(query);
FAIL_ON_ERROR(rc, st, dbi, DBERR_END_TX_FAILED)
rc = dbi->begin_transaction();
FAIL_ON_ERROR(rc, st, dbi, DBERR_BEGIN_TX_FAILED)
}
else {
rc = dbi->exec_params(query, nParams, param_types, params, param_lengths, param_types);
FAIL_ON_ERROR(rc, st, dbi, DBERR_SQL_ERROR)
}
}
else {
rc = dbi->exec_params(query, nParams, param_types, params, param_lengths, param_types);
FAIL_ON_ERROR(rc, st, dbi, DBERR_SQL_ERROR)
if (is_dml_statement(query) && conn->getConnectionOptions()->autocommit) {
rc = dbi->end_transaction(query);
FAIL_ON_ERROR(rc, st, dbi, DBERR_END_TX_FAILED)
rc = dbi->begin_transaction();
FAIL_ON_ERROR(rc, st, dbi, DBERR_BEGIN_TX_FAILED)
}
}
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int GIXSQLExecPrepared(sqlca_t* st, void* d_connection_id, int connection_id_tl, char* stmt_name, int nParams)
{
IDbInterface* dbi = nullptr; // not used but we need it for the call to the worker function
spdlog::trace(FMT_FILE_FUNC "GIXSQLExecPrepared start", __FILE__, __func__);
return _gixsqlExecPrepared(st, d_connection_id, connection_id_tl, stmt_name, nParams, &dbi);
}
int _gixsqlExecPrepared(sqlca_t* st, void* d_connection_id, int connection_id_tl, char* stmt_name, int nParams, IDbInterface** r_dbi)
{
*r_dbi = nullptr;
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::error("Can't find a connection");
setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
return RESULT_FAILED;
}
if (stmt_name == NULL || strlen(stmt_name) == 0) {
spdlog::error("Empty statement name");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
sqlca_initialize(st);
// TODO: check number of parameters
if (_current_sql_var_list.size() > nParams) {
setStatus(st, NULL, DBERR_TOO_MANY_ARGUMENTS);
return RESULT_FAILED;
}
else if (_current_sql_var_list.size() < nParams) {
setStatus(st, NULL, DBERR_TOO_FEW_ARGUMENTS);
return RESULT_FAILED;
}
std::vector<std::string> params;
std::vector<int> param_lengths;
std::vector<int> param_types;
std::vector<SqlVar*>::iterator it;
// set parameters
for (it = _current_sql_var_list.begin(); it != _current_sql_var_list.end(); it++) {
params.push_back((*it)->getRealData());
param_types.push_back((*it)->getType());
param_lengths.push_back((*it)->getLength());
}
int rc = 0;
IDbInterface* dbi = conn->getDbInterface();
if (!dbi)
FAIL_ON_ERROR(1, st, dbi, DBERR_SQL_ERROR)
rc = dbi->exec_prepared(stmt_name, params, param_lengths, param_types);
FAIL_ON_ERROR(rc, st, dbi, DBERR_SQL_ERROR)
setStatus(st, NULL, DBERR_NO_ERROR);
*r_dbi = dbi;
return RESULT_SUCCESS;
}
LIBGIXSQL_API int GIXSQLExecPreparedInto(sqlca_t* st, void* d_connection_id, int connection_id_tl, char* stmt_name, int nParams, int nResParams)
{
IDbInterface* dbi = nullptr;
spdlog::trace(FMT_FILE_FUNC "GIXSQLExecPreparedInto start", __FILE__, __func__);
int rc = _gixsqlExecPrepared(st, d_connection_id, connection_id_tl, stmt_name, nParams, &dbi);
if (rc != RESULT_SUCCESS)
return rc;
if (!dbi->move_to_first_record(stmt_name)) {
spdlog::error("move_to_first_record failed: {} - {}:", dbi->get_error_code(), dbi->get_state(), dbi->get_error_message());
setStatus(st, dbi, dbi->get_error_code());
return RESULT_FAILED;
}
spdlog::trace(FMT_FILE_FUNC "move_to_first_record successful", __FILE__, __func__);
int nfields = dbi->get_num_fields(nullptr);
if (nfields != nResParams) {
spdlog::error("ResParams({}) and fields({}) are different", nResParams, nfields);
setStatus(st, NULL, DBERR_FIELD_COUNT_MISMATCH);
return RESULT_FAILED;
}
// Some drivers (notably DB2, both natevely and under ODBC, do not support SQLNumRows, so we have to check here
if (dbi->supports_num_rows()) {
// check numtuples
if (dbi->get_num_rows(nullptr) < 1) {
spdlog::trace("No data");
setStatus(st, NULL, DBERR_NO_DATA);
return RESULT_FAILED;
}
if (dbi->get_num_rows(nullptr) > 1) {
spdlog::trace("Too much data");
setStatus(st, NULL, DBERR_TOO_MUCH_DATA);
return RESULT_FAILED;
}
}
// set result params
int datalen = 0;
bool has_invalid_column_data = false;
int bsize = _res_sql_var_list.getMaxLength() + VARLEN_LENGTH_SZ + 1;
char* buffer = (char*)calloc(1, bsize);
for (int i = 0; i < _res_sql_var_list.size(); i++) {
SqlVar* v = _res_sql_var_list.at(i);
if (!dbi->get_resultset_value(ResultSetContextType::PreparedStatement, stmt_name, 0, i, buffer, bsize, &datalen)) {
setStatus(st, dbi, DBERR_INVALID_COLUMN_DATA);
free(buffer);
return RESULT_FAILED;
}
v->createCobolData(buffer, datalen);
spdlog::trace(FMT_FILE_FUNC "result parameter {} - addr: {}", __FILE__, __func__, i + 1, (void*)v->getAddr());
}
free(buffer);
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int
GIXSQLCursorDeclareParams(struct sqlca_t* st, void* d_connection_id, int connection_id_tl, char* cursor_name, int with_hold, void* d_query, int query_tl, int nParams)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLCursorDeclareParams start for cursor [{}]", __FILE__, __func__, cursor_name);
bool is_literal = (query_tl == 0);
std::string query_or_stmt_name = get_hostref_or_literal(d_query, query_tl);
spdlog::trace(FMT_FILE_FUNC "SQL: #{}#", __FILE__, __func__, is_literal ? query_or_stmt_name : "(from field at runtime)");
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::trace(FMT_FILE_FUNC "connection id is not found, cursor initialization will be performed later", __FILE__, __func__);
}
// check argument
if ((is_literal && query_or_stmt_name.empty()) ||
cursor_name == NULL || strlen(cursor_name) == 0) {
spdlog::error("Empty query/cursor");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
// check argument
if (nParams == 0) {
setStatus(st, NULL, DBERR_NO_PARAMETERS);
return RESULT_FAILED;
}
if (_current_sql_var_list.size() > nParams) {
setStatus(st, NULL, DBERR_TOO_MANY_ARGUMENTS);
return RESULT_FAILED;
}
else if (_current_sql_var_list.size() < nParams) {
setStatus(st, NULL, DBERR_TOO_FEW_ARGUMENTS);
return RESULT_FAILED;
}
int rc = _gixsqlCursorDeclare(st, conn, connection_id, cursor_name, with_hold, d_query, query_tl, nParams);
if (!rc && !conn && d_connection_id && connection_id_tl) {
// This means we can try to resolve it later (possibly on open), so we save the COBOL field reference
Cursor* c = cursor_manager.get(cursor_name);
if (c) {
c->setConnectionReference(d_connection_id, connection_id_tl);
}
}
return rc;
}
LIBGIXSQL_API int
GIXSQLCursorDeclare(struct sqlca_t* st, void* d_connection_id, int connection_id_tl, char* cursor_name, int with_hold, void* d_query, int query_tl)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLCursorDeclare start for cursor [{}]", __FILE__, __func__, cursor_name);
bool is_literal = (query_tl == 0);
std::string query_or_stmt_name = get_hostref_or_literal(d_query, query_tl);
spdlog::trace(FMT_FILE_FUNC "SQL: #{}#", __FILE__, __func__, is_literal ? query_or_stmt_name : "(from field at runtime)");
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::trace(FMT_FILE_FUNC "connection id is not found, cursor initialization will be performed later", __FILE__, __func__);
}
// check argument
if ((is_literal && query_or_stmt_name.empty()) ||
cursor_name == NULL || strlen(cursor_name) == 0) {
spdlog::error("Empty query/cursor");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
sqlca_initialize(st);
int rc = _gixsqlCursorDeclare(st, conn, connection_id, cursor_name, with_hold, d_query, query_tl, 0);
if (!rc && !conn && d_connection_id && connection_id_tl) {
// This means we can try to resolve it later (possibly on open), so we save the COBOL field reference
Cursor* c = cursor_manager.get(cursor_name);
if (c) {
c->setConnectionReference(d_connection_id, connection_id_tl);
}
}
return rc;
}
static int _gixsqlCursorDeclare(struct sqlca_t* st, Connection* conn, std::string connection_name, std::string cursor_name, int with_hold, void* d_query, int query_tl, int nParams)
{
if (cursor_manager.exists(cursor_name)) {
spdlog::error("Cursor exists: {}", cursor_name);
setStatus(st, NULL, DBERR_CURSOR_EXISTS);
return RESULT_FAILED;
}
Cursor* c = cursor_manager.create();
c->setConnection((IConnection*)conn);
c->setConnectionName(connection_name);
c->setName(std::string(cursor_name));
if (!query_tl)
c->setQuery(get_hostref_or_literal(d_query, query_tl));
else
c->setQuerySource(d_query, query_tl);
c->setNumParams(nParams);
c->setWithHold(with_hold);
if (nParams > 0) {
c->setParameters(_current_sql_var_list);
}
if (conn) {
IDbInterface* dbi = conn->getDbInterface();
if (dbi->cursor_declare(c, with_hold, nParams)) {
spdlog::error("Invalid cursor data for cursor [{}]", cursor_name);
setStatus(st, NULL, DBERR_DECLARE_CURSOR_FAILED);
delete c;
return RESULT_FAILED;
}
}
cursor_manager.add(c);
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int
GIXSQLCursorOpen(struct sqlca_t* st, char* cname)
{
int rc = 0;
spdlog::trace(FMT_FILE_FUNC "GIXSQLCursorOpen start for cursor [{}]", __FILE__, __func__, cname);
sqlca_initialize(st);
// check argument
if (cname == NULL || strlen(cname) == 0) {
spdlog::error("Empty query/cursor");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
// search cursor
Cursor* cursor = cursor_manager.get(std::string(cname));
if (cursor == NULL) {
spdlog::error("cursor {} not registered", cname);
setStatus(st, NULL, DBERR_NO_SUCH_CURSOR);
return RESULT_FAILED;
}
void* crsr_src_addr = nullptr;
int crsr_src_len = 0;
if (cursor->getQuery().empty()) {
cursor->getQuerySource(&crsr_src_addr, &crsr_src_len);
if (!crsr_src_addr) {
spdlog::error("cursor {} has an empty query", cname);
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
}
// This was not properly declared, we have to set it up
if (!cursor->getConnection()) {
std::string connection_id = cursor->getConnectionName();
IConnection* cc = (IConnection*)connection_manager.get(connection_id);
if (cc)
cursor->setConnection(cc);
else {
// try to resolve the connection id through a reference to a COBOL variable
connection_id = cursor->getConnectionNameFromReference();
cc = (IConnection*)connection_manager.get(connection_id);
if (cc)
cursor->setConnection(cc);
}
if (cursor->getConnection()) {
IDbInterface* cdbi = cursor->getConnection()->getDbInterface();
if (!cdbi || cdbi->cursor_declare(cursor, cursor->isWithHold(), cursor->getNumParams())) {
spdlog::error("Invalid cursor data: {}", cname);
setStatus(st, NULL, DBERR_DECLARE_CURSOR_FAILED);
return RESULT_FAILED;
}
}
}
if (!cursor->getConnection()) {
spdlog::error("Cannot find an active connection for cursor {}", cname);
setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
return RESULT_FAILED;
}
Connection* c = (Connection*)cursor->getConnection();
IDbInterface* dbi = c->getDbInterface();
if (cursor->isOpen()) {
spdlog::error("cursor {} is alredy open", cname);
rc = dbi->close_cursor(cursor);
FAIL_ON_ERROR(rc, st, dbi, DBERR_CLOSE_CURSOR_FAILED)
}
//if (cursor->getConnection() == NULL) { // USE_DEFAULT_CONNECTION
// Connection *conn = connection_manager.current();
// if (conn == NULL) {
// spdlog::error("connection id is not found\n");
// setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
// return RESULT_FAILED;
// }
// cursor->setConnection((IConnection *)conn);
//}
rc = dbi->cursor_open(cursor);
FAIL_ON_ERROR(rc, st, dbi, DBERR_OPEN_CURSOR_FAILED)
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int GIXSQLCursorFetchOne(struct sqlca_t* st, char* cname)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLCursorFetchOne start", __FILE__, __func__);
sqlca_initialize(st);
// check argument
if (cname == NULL || strlen(cname) == 0) {
setStatus(st, NULL, DBERR_NO_SUCH_CURSOR);
return RESULT_FAILED;
}
spdlog::trace(FMT_FILE_FUNC "GIXSQLCursorFetchOne - cursor name: {}", __FILE__, __func__, cname);
Cursor* cursor = cursor_manager.get(cname);
if (cursor == NULL) {
spdlog::error("cursor {} not registered", cname);
setStatus(st, NULL, DBERR_NO_SUCH_CURSOR);
return RESULT_FAILED;
}
if (!cursor->isOpen()) {
spdlog::error("cursor {} closed", cname);
setStatus(st, NULL, DBERR_CURSOR_CLOSED);
return RESULT_FAILED;
}
IDbInterface* dbi = cursor->getConnection()->getDbInterface();
int rc = dbi->fetch_one(cursor, FETCH_NEXT_ROW);
if (rc == DBERR_NO_DATA) {
setStatus(st, dbi, DBERR_NO_DATA);
return DBERR_FETCH_ROW_FAILED;
}
FAIL_ON_ERROR(rc, st, dbi, DBERR_FETCH_ROW_FAILED)
int nResParams = _res_sql_var_list.size();
int nfields = dbi->get_num_fields(cursor);
if (nfields != nResParams) {
spdlog::error("ResParams({}) and fields({}) are different", nResParams, nfields);
setStatus(st, dbi, DBERR_FIELD_COUNT_MISMATCH);
return RESULT_FAILED;
}
int bsize = _res_sql_var_list.getMaxLength() + VARLEN_LENGTH_SZ + 1;
char* buffer = (char*)calloc(1, bsize);
std::vector<SqlVar*>::iterator it;
int i = 0;
int datalen = 0;
for (it = _res_sql_var_list.begin(); it != _res_sql_var_list.end(); it++) {
if (!dbi->get_resultset_value(ResultSetContextType::Cursor, cursor, 0, i++, buffer, bsize, &datalen)) {
setStatus(st, dbi, DBERR_INVALID_COLUMN_DATA);
free(buffer);
return RESULT_FAILED;
}
(*it)->createCobolData(buffer, datalen);
// may add trace code here (or remove from GIXSQLExecSelectIntoOne)
}
free(buffer);
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int
GIXSQLCursorClose(struct sqlca_t* st, char* cname)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLCursorClose start", __FILE__, __func__);
sqlca_initialize(st);
Cursor* cursor = cursor_manager.get(cname);
if (cursor == NULL) {
spdlog::error("cursor {} not registered.\n", cname);
setStatus(st, NULL, DBERR_NO_SUCH_CURSOR);
return RESULT_FAILED;
}
if (!cursor->isOpen()) {
spdlog::error("cursor {} already closed\n", cname);
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
Connection* conn = (Connection*)cursor->getConnection();
if (conn == NULL) {
spdlog::error("No connection assigned, invalid cursor: {}", cname); // warning
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
IDbInterface* dbi = cursor->getConnection()->getDbInterface();
int rc = dbi->close_cursor(cursor);
// See issue #98
//if (cursor_manager.exists(cname))
// cursor_manager.remove(cursor);
FAIL_ON_ERROR(rc, st, dbi, DBERR_CLOSE_CURSOR_FAILED)
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int GIXSQLPrepareStatement(sqlca_t* st, void* d_connection_id, int connection_id_tl, char* stmt_name, void* d_statement_src, int statement_src_tl)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLPrepareStatement start", __FILE__, __func__);
spdlog::trace(FMT_FILE_FUNC "Statement name: {}", __FILE__, __func__, stmt_name);
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::error("Can't find a connection");
setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
return RESULT_FAILED;
}
std::string statement_src = get_hostref_or_literal(d_statement_src, statement_src_tl);
spdlog::trace(FMT_FILE_FUNC "Statement source: {}", __FILE__, __func__, statement_src);
// check argument
if (stmt_name == NULL || strlen(stmt_name) == 0) {
spdlog::error("Empty statement name");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
IDbInterface* dbi = conn->getDbInterface();
if (dbi->prepare(stmt_name, statement_src)) {
spdlog::error("Cannot prepare statement (2)");
setStatus(st, dbi, DBERR_SQL_ERROR);
return RESULT_FAILED;
}
return RESULT_SUCCESS;
}
LIBGIXSQL_API int
GIXSQLExecSelectIntoOne(struct sqlca_t* st, void* d_connection_id, int connection_id_tl, char* _query, int nParams, int nResParams)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLExecSelectIntoOne start", __FILE__, __func__);
spdlog::trace(FMT_FILE_FUNC "SQL: #{}#", __FILE__, __func__, _query);
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::error("Can't find a connection");
setStatus(st, NULL, DBERR_CONN_NOT_FOUND);
return RESULT_FAILED;
}
// check argument
if (_query == NULL || strlen(_query) == 0) {
spdlog::error("Empty query/cursor");
setStatus(st, NULL, DBERR_EMPTY_QUERY);
return RESULT_FAILED;
}
IDbInterface* dbi = conn->getDbInterface();
if (nParams > 0) {
if (_gixsqlExecParams(conn, st, _query, nParams) != RESULT_SUCCESS)
return RESULT_FAILED;
}
else {
if (_gixsqlExec(conn, st, _query) != RESULT_SUCCESS)
return RESULT_FAILED;
}
if (!dbi->move_to_first_record()) {
spdlog::error("move_to_first_record failed: {} - {}: {}", dbi->get_error_code(), dbi->get_state(), dbi->get_error_message());
setStatus(st, dbi, dbi->get_error_code());
return RESULT_FAILED;
}
spdlog::trace(FMT_FILE_FUNC "move_to_first_record successful", __FILE__, __func__);
int nfields = dbi->get_num_fields(nullptr);
if (nfields != nResParams) {
spdlog::error("ResParams({}) and fields({}) are different", nResParams, nfields);
setStatus(st, NULL, DBERR_FIELD_COUNT_MISMATCH);
return RESULT_FAILED;
}
// Some drivers (notably DB2, both natevely and under ODBC, do not support SQLNumRows, so we have to check here
if (dbi->supports_num_rows()) {
// check numtuples
if (dbi->get_num_rows(nullptr) < 1) {
spdlog::trace("No data");
setStatus(st, dbi, DBERR_NO_DATA);
return RESULT_FAILED;
}
if (dbi->get_num_rows(nullptr) > 1) {
spdlog::trace("Too much data");
setStatus(st, dbi, DBERR_TOO_MUCH_DATA);
return RESULT_FAILED;
}
}
// set result params
int datalen = 0;
bool has_invalid_column_data = false;
int bsize = _res_sql_var_list.getMaxLength() + VARLEN_LENGTH_SZ + 1;
char* buffer = (char*)calloc(1, bsize);
for (int i = 0; i < _res_sql_var_list.size(); i++) {
SqlVar* v = _res_sql_var_list.at(i);
if (!dbi->get_resultset_value(ResultSetContextType::CurrentResultSet, NULL, 0, i, buffer, bsize, &datalen)) {
setStatus(st, dbi, DBERR_INVALID_COLUMN_DATA);
free(buffer);
return RESULT_FAILED;
}
v->createCobolData(buffer, datalen);
spdlog::trace(FMT_FILE_FUNC "result parameter {} - addr: {}", __FILE__, __func__, i + 1, (void*)v->getAddr());
}
free(buffer);
setStatus(st, NULL, DBERR_NO_ERROR);
return RESULT_SUCCESS;
}
LIBGIXSQL_API int
GIXSQLDisconnect(struct sqlca_t* st, void* d_connection_id, int connection_id_tl)
{
spdlog::trace(FMT_FILE_FUNC "GIXSQLDisconnect start", __FILE__, __func__);
std::string connection_id = get_hostref_or_literal(d_connection_id, connection_id_tl);
Connection* conn = connection_manager.get(connection_id);
if (conn == NULL) {
spdlog::error("connection is not found\n");
return RESULT_FAILED;
}