forked from mariadb-corporation/mariadb-connector-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathma_connection.c
2329 lines (2162 loc) · 84 KB
/
ma_connection.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) 2013,2023 MariaDB Corporation AB
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
#include <ma_odbc.h>
#include <limits.h>
extern const char* DefaultPluginLocation;
static const char* utf8mb3 = "utf8mb3";
static const unsigned int selectedIntOption= 1, unselectedIntOption= 0;
static const my_bool selectedBoolOption= '\1', unselectedBoolOption= '\0';
struct st_madb_isolation MADB_IsolationLevel[] =
{
{SQL_TRANSACTION_REPEATABLE_READ, "REPEATABLE READ", "REPEATABLE-READ"},
{SQL_TRANSACTION_READ_COMMITTED, "READ COMMITTED", "READ-COMMITTED"},
{SQL_TRANSACTION_READ_UNCOMMITTED, "READ UNCOMMITTED", "READ-UNCOMMITTED"},
{SQL_TRANSACTION_SERIALIZABLE, "SERIALIZABLE", "SERIALIZABLE"},
{0, 0}
};
static long TranslateTxIsolation(const char* txIsolation, size_t len)
{
unsigned int i;
for (i = 0; i < 4; i++)
{
if (strncmp(txIsolation, MADB_IsolationLevel[i].StrIsolation, len) == 0 || strncmp(txIsolation, MADB_IsolationLevel[i].TrackStr, len) == 0)
{
return MADB_IsolationLevel[i].SqlIsolation;
break;
}
}
return SQL_TRANSACTION_REPEATABLE_READ;
}
/* used by SQLGetFunctions */
SQLUSMALLINT MADB_supported_api[]=
{
SQL_API_SQLALLOCCONNECT,
SQL_API_SQLALLOCENV,
SQL_API_SQLALLOCHANDLE,
SQL_API_SQLALLOCSTMT,
SQL_API_SQLBINDCOL,
SQL_API_SQLBINDPARAM,
SQL_API_SQLCANCEL,
SQL_API_SQLCLOSECURSOR,
SQL_API_SQLCOLATTRIBUTE,
SQL_API_SQLCOLUMNS,
SQL_API_SQLCONNECT,
SQL_API_SQLCOPYDESC,
SQL_API_SQLDATASOURCES,
SQL_API_SQLDESCRIBECOL,
SQL_API_SQLDISCONNECT,
SQL_API_SQLENDTRAN,
SQL_API_SQLERROR,
SQL_API_SQLEXECDIRECT,
SQL_API_SQLEXECUTE,
SQL_API_SQLFETCH,
SQL_API_SQLFETCHSCROLL,
SQL_API_SQLFREECONNECT,
SQL_API_SQLFREEENV,
SQL_API_SQLFREEHANDLE,
SQL_API_SQLFREESTMT,
SQL_API_SQLGETCONNECTATTR,
SQL_API_SQLGETCONNECTOPTION,
SQL_API_SQLGETCURSORNAME,
SQL_API_SQLGETDATA,
SQL_API_SQLGETDESCFIELD,
SQL_API_SQLGETDESCREC,
SQL_API_SQLGETDIAGFIELD,
SQL_API_SQLGETDIAGREC,
SQL_API_SQLGETENVATTR,
SQL_API_SQLGETFUNCTIONS,
SQL_API_SQLGETINFO,
SQL_API_SQLGETSTMTATTR,
SQL_API_SQLGETSTMTOPTION,
SQL_API_SQLGETTYPEINFO,
SQL_API_SQLNUMRESULTCOLS,
SQL_API_SQLPARAMDATA,
SQL_API_SQLPREPARE,
SQL_API_SQLPUTDATA,
SQL_API_SQLROWCOUNT,
SQL_API_SQLSETCONNECTATTR,
SQL_API_SQLSETCONNECTOPTION,
SQL_API_SQLSETCURSORNAME,
SQL_API_SQLSETDESCFIELD,
SQL_API_SQLSETDESCREC,
SQL_API_SQLSETENVATTR,
SQL_API_SQLSETPARAM,
SQL_API_SQLSETSTMTATTR,
SQL_API_SQLSETSTMTOPTION,
SQL_API_SQLSPECIALCOLUMNS,
SQL_API_SQLSTATISTICS,
SQL_API_SQLTABLES,
SQL_API_SQLTRANSACT,
SQL_API_SQLBULKOPERATIONS,
SQL_API_SQLBINDPARAMETER,
SQL_API_SQLBROWSECONNECT,
SQL_API_SQLCOLATTRIBUTES,
SQL_API_SQLCOLUMNPRIVILEGES ,
SQL_API_SQLDESCRIBEPARAM,
SQL_API_SQLDRIVERCONNECT,
SQL_API_SQLDRIVERS,
SQL_API_SQLEXTENDEDFETCH,
SQL_API_SQLFOREIGNKEYS,
SQL_API_SQLMORERESULTS,
SQL_API_SQLNATIVESQL,
SQL_API_SQLNUMPARAMS,
SQL_API_SQLPARAMOPTIONS,
SQL_API_SQLPRIMARYKEYS,
SQL_API_SQLPROCEDURECOLUMNS,
SQL_API_SQLPROCEDURES,
SQL_API_SQLSETPOS,
SQL_API_SQLSETSCROLLOPTIONS,
SQL_API_SQLTABLES,
SQL_API_SQLTABLEPRIVILEGES,
0
};
struct st_ma_connection_methods MADB_Dbc_Methods; /* declared at the end of file */
SQLRETURN MADB_DbcDummyTrackSession(MADB_Dbc* Dbc);
SQLRETURN MADB_DbcGetServerTxIsolation(MADB_Dbc* Dbc, SQLINTEGER*txIsolation);
/* Stub for stream caching - i.e. cahing is desabled by default, and we just raise the error, if some stmt is streaming atm
* Stmt - the statement, that has requested the operation, and where the error has to be set
*/
int MADB_Dbc_ErrorOnStreaming(MADB_Error *Error)
{
MADB_SetError(Error, MADB_ERR_HY000, "The requested operation is blocked by another streaming operation", 0);
return 1;
}
/* Main method for stream caching - i.e. cahing is desabled by default, and we are capable to store and operate the rest
of the RS.
Stmt - the statement, that has requested the operation, and where the error has to be set */
int MADB_Dbc_CacheRestOfCurrentRsStream(MADB_Dbc* Dbc, MADB_Error *Error)
{
if (Dbc->Streamer != NULL)
{
if (Dbc->Streamer->RsOps->CacheRestOfRs(Dbc->Streamer))
{
return MADB_Dbc_ErrorOnStreaming(Error);
}
Dbc->Streamer= NULL;
return 0;
}
return 0;
}
my_bool CheckConnection(MADB_Dbc *Dbc)
{
if (!Dbc->mariadb)
return FALSE;
if (mysql_get_socket(Dbc->mariadb) == MARIADB_INVALID_SOCKET)
{
/* Check if reconnect option was set */
if (DSN_OPTION(Dbc, MADB_OPT_FLAG_AUTO_RECONNECT))
{
if (!mysql_ping(Dbc->mariadb))
return TRUE;
}
return FALSE;
}
return TRUE;
}
/* {{{ MADB_DbcSetAttr */
SQLRETURN MADB_DbcSetAttr(MADB_Dbc *Dbc, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength, my_bool isWChar)
{
MADB_CLEAR_ERROR(&Dbc->Error);
if (!Dbc)
{
/* Todo: check */
if (Attribute != SQL_ATTR_TRACE &&
Attribute != SQL_ATTR_TRACEFILE)
return SQL_INVALID_HANDLE;
return SQL_SUCCESS;
}
switch(Attribute) {
case SQL_ATTR_ACCESS_MODE:
if ((SQLPOINTER)SQL_MODE_READ_WRITE != ValuePtr)
MADB_SetError(&Dbc->Error, MADB_ERR_01S02, NULL, 0);
Dbc->AccessMode= SQL_MODE_READ_WRITE;
break;
#if (ODBCVER >= 0x0351)
case SQL_ATTR_ANSI_APP:
if (ValuePtr != NULL)
{
Dbc->IsAnsi= 1;
Dbc->ConnOrSrcCharset= &SourceAnsiCs;
CopyClientCharset(&SourceAnsiCs, &Dbc->Charset);
}
else
{
Dbc->IsAnsi= 0;
}
break;
#endif
case SQL_ATTR_ASYNC_ENABLE:
if ((SQLPOINTER)SQL_ASYNC_ENABLE_OFF != ValuePtr)
MADB_SetError(&Dbc->Error, MADB_ERR_01S02, NULL, 0);
Dbc->AsyncEnable= SQL_ASYNC_ENABLE_OFF;
break;
case SQL_ATTR_AUTO_IPD:
/* read only */
MADB_SetError(&Dbc->Error, MADB_ERR_HY092, NULL, 0);
break;
case SQL_ATTR_AUTOCOMMIT:
{
SQLULEN ValidAttrs[]= {2, SQL_AUTOCOMMIT_ON, SQL_AUTOCOMMIT_OFF};
MADB_CHECK_ATTRIBUTE(Dbc, ValuePtr, ValidAttrs);
/* if a connection is open, try to apply setting to the connection */
if (Dbc->mariadb)
{
if (Dbc->EnlistInDtc) {
return MADB_SetError(&Dbc->Error, MADB_ERR_25000, NULL, 0);
}
if (mysql_autocommit(Dbc->mariadb, (my_bool)(size_t)ValuePtr))
{
return MADB_SetError(&Dbc->Error, MADB_ERR_HY001, mysql_error(Dbc->mariadb), mysql_errno(Dbc->mariadb));
}
}
Dbc->AutoCommit= (SQLUINTEGER)(SQLULEN)ValuePtr;
}
break;
case SQL_ATTR_CONNECTION_DEAD:
/* read only! */
return MADB_SetError(&Dbc->Error, MADB_ERR_HY092, NULL, 0);
case SQL_ATTR_CURRENT_CATALOG:
{
MADB_FREE(Dbc->CatalogName);
if (isWChar)
{
/* IsAnsi will be set before this, even if it is set before connection
StringLength from DM here is octets length */
Dbc->CatalogName= MADB_ConvertFromWCharEx((SQLWCHAR *)ValuePtr, StringLength/ sizeof(SQLWCHAR), NULL, Dbc->ConnOrSrcCharset, NULL, TRUE);
}
else
{
if (StringLength == SQL_NTS || *((char*)ValuePtr + StringLength - 1) == '\0')
Dbc->CatalogName= _strdup((char *)ValuePtr);
else
{
if ((Dbc->CatalogName= (char *)MADB_CALLOC(StringLength + 1)))
{
memcpy(Dbc->CatalogName, ValuePtr, StringLength);
Dbc->CatalogName[StringLength]= '\0';
}
}
}
if (Dbc->CatalogName == NULL)
{
MADB_SetError(&Dbc->Error, MADB_ERR_HY001, NULL, 0);
}
if (Dbc->mariadb &&
mysql_select_db(Dbc->mariadb, Dbc->CatalogName))
{
return MADB_SetError(&Dbc->Error, MADB_ERR_HY001, mysql_error(Dbc->mariadb), mysql_errno(Dbc->mariadb));
}
MADB_RESET(Dbc->CurrentSchema, Dbc->CatalogName);
}
break;
case SQL_ATTR_LOGIN_TIMEOUT:
{
long long value= (SQLUINTEGER)(SQLULEN)ValuePtr;
if (value > UINT_MAX) {
MADB_SetError(&Dbc->Error, MADB_ERR_01S02, NULL, 0);
value= UINT_MAX;
}
Dbc->LoginTimeout= (SQLUINTEGER)value;
break;
}
case SQL_ATTR_METADATA_ID:
Dbc->MetadataId= (SQLUINTEGER)(SQLULEN)ValuePtr;
break;
case SQL_ATTR_CONNECTION_TIMEOUT:
return MADB_SetError(&Dbc->Error, MADB_ERR_01S02, NULL, 0);
case SQL_ATTR_ODBC_CURSORS:
{
#pragma warning(disable:4995)
#pragma warning(push)
SQLULEN ValidAttrs[]= {3, SQL_CUR_USE_IF_NEEDED, SQL_CUR_USE_ODBC, SQL_CUR_USE_DRIVER};
MADB_CHECK_ATTRIBUTE(Dbc, ValuePtr, ValidAttrs);
if ((SQLULEN)ValuePtr != SQL_CUR_USE_ODBC)
MADB_SetError(&Dbc->Error, MADB_ERR_01S02, NULL, 0);
Dbc->OdbcCursors= SQL_CUR_USE_ODBC;
#pragma warning(pop)
}
break;
case SQL_ATTR_ENLIST_IN_DTC:
/* MS Distributed Transaction Coordinator not supported */
return MADB_SetError(&Dbc->Error, MADB_ERR_HYC00, NULL, 0);
case SQL_ATTR_PACKET_SIZE:
/* if connection was made, return HY001 */
if (Dbc->mariadb)
{
return MADB_SetError(&Dbc->Error, MADB_ERR_HY001, NULL, 0);
}
Dbc->PacketSize= (SQLUINTEGER)(SQLULEN)ValuePtr;
break;
case SQL_ATTR_QUIET_MODE:
Dbc->QuietMode= (HWND)ValuePtr;
break;
case SQL_ATTR_TRACE:
break;
case SQL_ATTR_TRACEFILE:
break;
case SQL_ATTR_TRANSLATE_LIB:
break;
case SQL_ATTR_TRANSLATE_OPTION:
break;
case SQL_ATTR_TXN_ISOLATION:
if (Dbc->mariadb)
{
my_bool ValidTx= FALSE;
unsigned int i;
for (i=0; i < 4; i++)
{
if (MADB_IsolationLevel[i].SqlIsolation == (SQLLEN)ValuePtr)
{
char StmtStr[128];
int len= _snprintf(StmtStr, sizeof(StmtStr), "SET SESSION TRANSACTION ISOLATION LEVEL %s",
MADB_IsolationLevel[i].StrIsolation);
LOCK_MARIADB(Dbc);
if (mysql_real_query(Dbc->mariadb, StmtStr, (unsigned long)len))
{
UNLOCK_MARIADB(Dbc);
return MADB_SetError(&Dbc->Error, MADB_ERR_HY001, mysql_error(Dbc->mariadb), mysql_errno(Dbc->mariadb));
}
Dbc->Methods->TrackSession(Dbc);
UNLOCK_MARIADB(Dbc);
ValidTx= TRUE;
break;
}
}
if (!ValidTx)
{
return MADB_SetError(&Dbc->Error, MADB_ERR_HY024, NULL, 0);
}
}
Dbc->TxnIsolation= (SQLINTEGER)(SQLLEN)ValuePtr;
break;
default:
break;
}
return Dbc->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_DbcHetAttr */
SQLRETURN MADB_DbcGetAttr(MADB_Dbc *Dbc, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER BufferLength, SQLINTEGER *StringLengthPtr, my_bool isWChar)
{
MADB_CLEAR_ERROR(&Dbc->Error);
if (!Dbc)
return SQL_INVALID_HANDLE;
if (!ValuePtr && Attribute != SQL_ATTR_CURRENT_CATALOG)
return SQL_SUCCESS;
if (Attribute == SQL_ATTR_CURRENT_CATALOG && !StringLengthPtr &&
(!ValuePtr || !BufferLength))
{
return MADB_SetError(&Dbc->Error, MADB_ERR_01004, NULL, 0);
}
switch(Attribute) {
case SQL_ATTR_ACCESS_MODE:
*(SQLUINTEGER *)ValuePtr= SQL_MODE_READ_WRITE;
break;
case SQL_ATTR_ASYNC_ENABLE:
*(SQLULEN *)ValuePtr= SQL_ASYNC_ENABLE_OFF;
break;
case SQL_ATTR_AUTO_IPD:
*(SQLUINTEGER *)ValuePtr= SQL_FALSE;
break;
case SQL_ATTR_AUTOCOMMIT:
/* Not sure why Dbc->AutoCommit is initialized with 4. Probably the idea has been lost in time. But keeping it so far, and workarounding here. Looks like all DMs but iOdbc corrects the value to SQL_AUTOCOMMIT_ON, when returning */
*(SQLUINTEGER *)ValuePtr= (Dbc->AutoCommit != 0 ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF);
break;
case SQL_ATTR_CONNECTION_DEAD:
/* ping may fail if status isn't ready, so we need to check errors */
if (mysql_ping(Dbc->mariadb))
*(SQLUINTEGER *)ValuePtr= (mysql_errno(Dbc->mariadb) == CR_SERVER_GONE_ERROR ||
mysql_errno(Dbc->mariadb) == CR_SERVER_LOST) ? SQL_CD_TRUE : SQL_CD_FALSE;
else
*(SQLUINTEGER *)ValuePtr= SQL_CD_FALSE;
break;
case SQL_ATTR_CURRENT_CATALOG:
{
SQLSMALLINT StrLen;
SQLRETURN ret;
ret= Dbc->Methods->GetCurrentDB(Dbc, ValuePtr, BufferLength, &StrLen, isWChar);
/* if we weren't able to determine the current db, we will return the cached catalog name */
if (!SQL_SUCCEEDED(ret) && Dbc->CatalogName)
{
MADB_CLEAR_ERROR(&Dbc->Error);
StrLen= (SQLSMALLINT)MADB_SetString(isWChar ? &Dbc->Charset : 0, ValuePtr, BUFFER_CHAR_LEN(BufferLength, isWChar),
Dbc->CatalogName, strlen(Dbc->CatalogName), &Dbc->Error);
ret= SQL_SUCCESS;
}
if (StringLengthPtr != NULL)
{
*StringLengthPtr= (SQLINTEGER)StrLen;
}
return ret;
}
case SQL_ATTR_LOGIN_TIMEOUT:
*(SQLUINTEGER *)ValuePtr= Dbc->LoginTimeout;
break;
case SQL_ATTR_CONNECTION_TIMEOUT:
*(SQLUINTEGER *)ValuePtr= 0;
break;
case SQL_ATTR_METADATA_ID:
/* SQL_ATTR_METADATA_ID is SQLUINTEGER attribute on connection level, but SQLULEN on statement level :/ */
*(SQLUINTEGER *)ValuePtr= Dbc->MetadataId;
case SQL_ATTR_ODBC_CURSORS:
*(SQLULEN*)ValuePtr= SQL_CUR_USE_ODBC;
break;
case SQL_ATTR_ENLIST_IN_DTC:
/* MS Distributed Transaction Coordinator not supported */
MADB_SetError(&Dbc->Error, MADB_ERR_HYC00, NULL, 0);
break;
case SQL_ATTR_PACKET_SIZE:
{
unsigned long packet_size= 0;
mysql_get_option(Dbc->mariadb, MYSQL_OPT_NET_BUFFER_LENGTH, &packet_size);
*(SQLUINTEGER *)ValuePtr= (SQLUINTEGER)packet_size;
}
break;
case SQL_ATTR_QUIET_MODE:
#ifdef _WIN32
(HWND)ValuePtr= Dbc->QuietMode;
#endif // _WIN32
break;
case SQL_ATTR_TRACE:
break;
case SQL_ATTR_TRACEFILE:
break;
case SQL_ATTR_TRANSLATE_LIB:
break;
case SQL_ATTR_TRANSLATE_OPTION:
break;
case SQL_ATTR_TXN_ISOLATION:
/* TxnIsolation wasn't set before we retrieve it from open connection or
assume a default of REPETABLE_READ */
if (!Dbc->TxnIsolation)
{
Dbc->TxnIsolation= SQL_TRANSACTION_REPEATABLE_READ;
if (Dbc->mariadb)
{
Dbc->Methods->GetTxIsolation(Dbc, (SQLINTEGER*)ValuePtr);
}
}
else
*(SQLINTEGER*)ValuePtr= Dbc->TxnIsolation;
break;
default:
MADB_SetError(&Dbc->Error, MADB_ERR_HYC00, NULL, 0);
break;
}
return Dbc->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_DbcInit() */
MADB_Dbc *MADB_DbcInit(MADB_Env *Env)
{
MADB_Dbc *Connection= NULL;
MADB_CLEAR_ERROR(&Env->Error);
if (!(Connection = (MADB_Dbc *)MADB_CALLOC(sizeof(MADB_Dbc))))
goto cleanup;
Connection->AutoCommit= 4;
Connection->lcTableNamesMode2= (char)-1; /* -1 means we don't know if lower_case_table_names=2, ie that info has never been requested yet */
Connection->Environment= Env;
Connection->Methods= &MADB_Dbc_Methods;
//CopyClientCharset(&SourceAnsiCs, &Connection->Charset);
InitializeCriticalSection(&Connection->cs);
InitializeCriticalSection(&Connection->ListsCs);
/* Not sure that critical section is really needed here - this init routine is called when
no one has the handle yet */
EnterCriticalSection(&Connection->Environment->cs);
/* Save connection in Environment list */
Connection->ListItem.data= (void *)Connection;
Connection->Environment->Dbcs= MADB_ListAdd(Connection->Environment->Dbcs, &Connection->ListItem);
LeaveCriticalSection(&Connection->Environment->cs);
MADB_PutErrorPrefix(NULL, &Connection->Error);
return Connection;
cleanup:
if (Connection)
free(Connection);
else
MADB_SetError(&Env->Error, MADB_ERR_HY001, NULL, 0);
return NULL;
}
/* }}} */
/* {{{ MADB_DbcFree() */
SQLRETURN MADB_DbcFree(MADB_Dbc *Connection)
{
MADB_Env *Env= NULL;
if (!Connection)
return SQL_ERROR;
MDBUG_C_PRINT(Connection, "%sMADB_DbcFree", "\t->");
MDBUG_C_DUMP(Connection, Connection, 0x);
Env= Connection->Environment;
/* TODO: If somebody uses connection it won't help if lock it here. At least it requires
more fingers movements
LOCK_MARIADB(Dbc);*/
if (Connection->mariadb)
{
mysql_close(Connection->mariadb);
Connection->mariadb= NULL;
}
/*UNLOCK_MARIADB(Dbc);*/
/* todo: delete all descriptors */
EnterCriticalSection(&Env->cs);
Connection->Environment->Dbcs= MADB_ListDelete(Connection->Environment->Dbcs, &Connection->ListItem);
LeaveCriticalSection(&Env->cs);
MADB_FREE(Connection->CatalogName);
CloseClientCharset(&Connection->Charset);
MADB_FREE(Connection->CurrentSchema);
MADB_DSN_Free(Connection->Dsn);
DeleteCriticalSection(&Connection->cs);
free(Connection);
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_DbcGetCurrentDB
Fetches current DB. For use without session tracking
*/
SQLRETURN MADB_DbcGetCurrentDB(MADB_Dbc *Connection, SQLPOINTER CurrentDB, SQLINTEGER CurrentDBLength,
SQLSMALLINT *StringLengthPtr, my_bool isWChar)
{
SQLLEN Size;
MYSQL_RES *res;
MYSQL_ROW row;
MADB_CLEAR_ERROR(&Connection->Error);
if (CheckConnection(Connection) == FALSE)
{
return MADB_SetError(&Connection->Error, MADB_ERR_08003, NULL, 0);
}
LOCK_MARIADB(Connection);
if (mysql_real_query(Connection->mariadb, "SELECT DATABASE()", 17) || (res= mysql_store_result(Connection->mariadb)) == NULL)
{
MADB_SetNativeError(&Connection->Error, SQL_HANDLE_DBC, Connection->mariadb);
goto end;
}
row = mysql_fetch_row(res);
Size= (SQLSMALLINT)MADB_SetString(isWChar ? & Connection->Charset : 0,
(void *)CurrentDB, BUFFER_CHAR_LEN(CurrentDBLength, isWChar), row[0] != NULL ? row[0] : "null",
SQL_NTS, &Connection->Error);
mysql_free_result(res);
if (StringLengthPtr)
*StringLengthPtr= isWChar ? (SQLSMALLINT)Size * sizeof(SQLWCHAR) : (SQLSMALLINT)Size;
end:
UNLOCK_MARIADB(Connection);
return Connection->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_DbcGetTrackedCurrentDB
Fetches current DB. For use without session tracking
*/
SQLRETURN MADB_DbcGetTrackedCurrentDB(MADB_Dbc* Dbc, SQLPOINTER CurrentDB, SQLINTEGER CurrentDBLength,
SQLSMALLINT* StringLengthPtr, my_bool isWChar)
{
SQLLEN Size;
MADB_CLEAR_ERROR(&Dbc->Error);
Size = (SQLSMALLINT)MADB_SetString(isWChar ? &Dbc->Charset : 0,
(void*)CurrentDB, BUFFER_CHAR_LEN(CurrentDBLength, isWChar), Dbc->CurrentSchema != NULL ? Dbc->CurrentSchema : "null",
SQL_NTS, &Dbc->Error);
if (StringLengthPtr)
*StringLengthPtr = isWChar ? (SQLSMALLINT)Size * sizeof(SQLWCHAR) : (SQLSMALLINT)Size;
return Dbc->Error.ReturnValue;
}
/* }}} */
BOOL MADB_SqlMode(MADB_Dbc *Connection, enum enum_madb_sql_mode SqlMode)
{
unsigned int ServerStatus;
mariadb_get_infov(Connection->mariadb, MARIADB_CONNECTION_SERVER_STATUS, (void*)&ServerStatus);
switch (SqlMode)
{
case MADB_NO_BACKSLASH_ESCAPES:
return test(ServerStatus & SERVER_STATUS_NO_BACKSLASH_ESCAPES);
case MADB_ANSI_QUOTES:
return test(ServerStatus & SERVER_STATUS_ANSI_QUOTES);
}
return FALSE;
}
/* }}} */
/* {{{ MADB_DbcEndTran */
SQLRETURN MADB_DbcEndTran(MADB_Dbc *Dbc, SQLSMALLINT CompletionType)
{
MADB_CLEAR_ERROR(&Dbc->Error);
if (!Dbc)
return SQL_INVALID_HANDLE;
LOCK_MARIADB(Dbc);
switch (CompletionType) {
case SQL_ROLLBACK:
if (Dbc->Methods->CacheRestOfCurrentRsStream(Dbc, &Dbc->Error))
{
goto end;
}
if (Dbc->mariadb && mysql_rollback(Dbc->mariadb))
{
MADB_SetNativeError(&Dbc->Error, SQL_HANDLE_DBC, Dbc->mariadb);
}
break;
case SQL_COMMIT:
if (Dbc->Methods->CacheRestOfCurrentRsStream(Dbc, &Dbc->Error))
{
goto end;
}
if (Dbc->mariadb && mysql_commit(Dbc->mariadb))
MADB_SetNativeError(&Dbc->Error, SQL_HANDLE_DBC, Dbc->mariadb);
break;
default:
MADB_SetError(&Dbc->Error, MADB_ERR_HY012, NULL, 0);
}
Dbc->Methods->TrackSession(Dbc);
end:
UNLOCK_MARIADB(Dbc);
return Dbc->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_AddInitCommand
*/
static int MADB_AddInitCommand(MYSQL* mariadb, MADB_DynString *InitCmd, unsigned long MultiStmtAllowed, const char *StmtToAdd)
{
if (MultiStmtAllowed == 0)
{
mysql_optionsv(mariadb, MYSQL_INIT_COMMAND, StmtToAdd);
return 0;
}
else
{
if (InitCmd->length != 0)
{
if (MADB_DynstrAppendMem(InitCmd, ";", 1))
{
return 1;
}
}
return MADB_DynstrAppend(InitCmd, StmtToAdd);
}
}
/* }}} */
static const char* MADB_DbcGetDefaultSchema(MADB_Dbc* this, MADB_Dsn *Dsn)
{
/* set default catalog. If we have value set via SQL_ATTR_CURRENT_CATALOG - it takes priority. Otherwise - DSN */
if (this->CatalogName && this->CatalogName[0])
{
return this->CatalogName;
}
else if (Dsn->Catalog && Dsn->Catalog[0])
{
return Dsn->Catalog;
}
return NULL;
}
/* {{{ MADB_Dbc::CoreConnect
Minimal connect function to re-create functional connection from Dsn without any "extras".
It's mainly to be used by Cancel functions, that need to open additional connection to run query on the server
*/
SQLRETURN MADB_DbcCoreConnect(MADB_Dbc* this, MYSQL* _mariadb, MADB_Dsn *Dsn, MADB_Error* _Error, unsigned long clientFlags)
{
int protocol = MYSQL_PROTOCOL_TCP;
if (!MADB_IS_EMPTY(Dsn->ConnCPluginsDir))
{
mysql_optionsv(_mariadb, MYSQL_PLUGIN_DIR, Dsn->ConnCPluginsDir);
}
else
{
if (DefaultPluginLocation != NULL)
{
mysql_optionsv(_mariadb, MYSQL_PLUGIN_DIR, DefaultPluginLocation);
}
}
if (Dsn->ReadMycnf != '\0')
{
mysql_optionsv(_mariadb, MYSQL_READ_DEFAULT_GROUP, (void *)"odbc");
}
/* Connstring's option value takes precedence*/
if (Dsn->ConnectionTimeout)
{
mysql_optionsv(_mariadb, MYSQL_OPT_CONNECT_TIMEOUT, (const void *)&Dsn->ConnectionTimeout);
}
else if (this->LoginTimeout > 0)
{
mysql_optionsv(_mariadb, MYSQL_OPT_CONNECT_TIMEOUT, (const void *)&this->LoginTimeout);
}
if (Dsn->ReadTimeout)
{
mysql_optionsv(_mariadb, MYSQL_OPT_READ_TIMEOUT, (const void *)&Dsn->ReadTimeout);
}
if (Dsn->WriteTimeout)
{
mysql_optionsv(_mariadb, MYSQL_OPT_WRITE_TIMEOUT, (const void *)&Dsn->WriteTimeout);
}
if (Dsn->IsNamedPipe)
{
mysql_optionsv(_mariadb, MYSQL_OPT_NAMED_PIPE, NULL);
protocol= MYSQL_PROTOCOL_PIPE;
}
else if (Dsn->Socket)
{
#ifdef _WIN32
/* Technically this could break existing application, that has SOCKET in connstring on Windows, and it resulted in TCP connection */
/*protocol= MYSQL_PROTOCOL_PIPE;*/
#else
protocol= MYSQL_PROTOCOL_SOCKET;
#endif
}
else if (Dsn->Port > 0 || Dsn->IsTcpIp)
{
protocol= MYSQL_PROTOCOL_TCP;
if (Dsn->Port == 0)
{
Dsn->Port= 3306;
}
}
mysql_optionsv(_mariadb, MYSQL_OPT_PROTOCOL, (void*)&protocol);
{ //The block is needed because of all goto's
/* I don't think it's possible to have empty strings or only spaces in the string here, but I prefer
to have this paranoid check to make sure we dont' have them */
const char *SslKey= ltrim(Dsn->SslKey);
const char *SslCert= ltrim(Dsn->SslCert);
const char *SslCa= ltrim(Dsn->SslCa);
const char *SslCaPath= ltrim(Dsn->SslCaPath);
const char *SslCipher= ltrim(Dsn->SslCipher);
if (!MADB_IS_EMPTY(SslCa)
|| !MADB_IS_EMPTY(SslCaPath)
|| !MADB_IS_EMPTY(SslCipher)
|| !MADB_IS_EMPTY(SslCert)
|| !MADB_IS_EMPTY(SslKey))
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_ENFORCE, &selectedBoolOption);
if (!MADB_IS_EMPTY(SslKey))
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_KEY, SslKey);
}
if (!MADB_IS_EMPTY(SslCert))
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_CERT, SslCert);
}
if (!MADB_IS_EMPTY(SslCa))
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_CA, SslCa);
}
if (!MADB_IS_EMPTY(SslCaPath))
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_CAPATH, SslCaPath);
}
if (!MADB_IS_EMPTY(SslCipher))
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_CIPHER, SslCipher);
}
}
if (Dsn->TlsVersion > 0)
{
char TlsVersion[sizeof(TlsVersionName) + sizeof(TlsVersionBits) - 1], *Ptr= TlsVersion; /* All names + (n-1) comma */
unsigned int i, NeedComma= 0;
for (i= 0; i < sizeof(TlsVersionBits); ++i)
{
if (Dsn->TlsVersion & TlsVersionBits[i])
{
if (NeedComma != 0)
{
*Ptr++= ',';
}
else
{
NeedComma= 1;
}
strcpy(Ptr, TlsVersionName[i]);
Ptr += strlen(TlsVersionName[i]);
}
}
mysql_optionsv(_mariadb, MARIADB_OPT_TLS_VERSION, (void *)TlsVersion);
}
}
if (Dsn->SslVerify)
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char*)&selectedBoolOption);
}
else
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char*)&unselectedBoolOption);
}
if (Dsn->ForceTls != '\0')
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_ENFORCE, (const char*)&selectedBoolOption);
}
if (!MADB_IS_EMPTY(Dsn->SslCrl))
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_CRL, Dsn->SslCrl);
}
if (!MADB_IS_EMPTY(Dsn->SslCrlPath))
{
mysql_optionsv(_mariadb, MYSQL_OPT_SSL_CRLPATH, Dsn->SslCrlPath);
}
if (!MADB_IS_EMPTY(Dsn->ServerKey))
{
mysql_optionsv(_mariadb, MYSQL_SERVER_PUBLIC_KEY, Dsn->ServerKey);
}
if (!MADB_IS_EMPTY(Dsn->TlsPeerFp))
{
mysql_optionsv(_mariadb, MARIADB_OPT_TLS_PEER_FP, (void*)Dsn->TlsPeerFp);
}
if (!MADB_IS_EMPTY(Dsn->TlsPeerFpList))
{
mysql_optionsv(_mariadb, MARIADB_OPT_TLS_PEER_FP_LIST, (void*)Dsn->TlsPeerFpList);
}
if (!MADB_IS_EMPTY(Dsn->TlsKeyPwd))
{
mysql_optionsv(_mariadb, MARIADB_OPT_TLS_PASSPHRASE, (void*)Dsn->TlsKeyPwd);
}
if (!mysql_real_connect(_mariadb,
Dsn->Socket ? "localhost" : Dsn->ServerName, Dsn->UserName, Dsn->Password, MADB_DbcGetDefaultSchema(this, Dsn), Dsn->Port, Dsn->Socket, clientFlags))
{
MADB_SetNativeError(_Error, SQL_HANDLE_DBC, _mariadb);
if ((this->LoginTimeout > 0 || Dsn->ConnectionTimeout > 0) && strcmp(_Error->SqlState, "08S01") == 0)
{
strcpy_s(_Error->SqlState, SQLSTATE_LENGTH + 1, "HYT00");
}
}
return _Error->ReturnValue;
}
/* }}} */
/* Stub method for old servers - just does nothing */
void ServerCantSetStatement(MADB_QUERY * Query, unsigned long long Timeout)
{}
/* {{{ MADB_Dbc_ConnectDB
Mind that this function is used for establishing connection from the setup lib
*/
SQLRETURN MADB_DbcConnectDB(MADB_Dbc *Connection,
MADB_Dsn *Dsn)
{
char StmtStr[128];
unsigned int i;
unsigned long client_flags= CLIENT_MULTI_RESULTS;
MADB_DynString InitCmd;
const char* defaultSchema= MADB_DbcGetDefaultSchema(Connection, Dsn);
if (!Connection || !Dsn)
return SQL_ERROR;
MADB_CLEAR_ERROR(&Connection->Error);
if (Connection->mariadb == NULL)
{
if (!(Connection->mariadb= mysql_init(NULL)))
{
MADB_SetError(&Connection->Error, MADB_ERR_HY001, NULL, 0);
goto end;
}
}
/* If a client character set was specified in DSN, we will always use it.
Otherwise for ANSI applications we will use the current character set,
for unicode connections we use utf8
*/
{
const char* cs_name= NULL;
if (!MADB_IS_EMPTY(Dsn->CharacterSet))
{
if (strcmp(Dsn->CharacterSet, "utf8") == 0)
{
cs_name= utf8mb3;
}
cs_name= Dsn->CharacterSet;
}
else if (Connection->IsAnsi)
{
MARIADB_CHARSET_INFO *cs= mariadb_get_charset_by_name("auto");
cs_name= cs->csname;
}
if (InitClientCharset(&Connection->Charset, MADB_IS_EMPTY(cs_name) ? "utf8mb4" : cs_name))
{
/* Memory allocation error */
MADB_SetError(&Connection->Error, MADB_ERR_HY001, NULL, 0);
goto end;
}
if (iOdbc() && strcmp(Connection->Charset.cs_info->csname, "swe7") == 0)
{
MADB_SetError(&Connection->Error, MADB_ERR_HY000, "Charset SWE7 is not supported with iODBC", 0);
goto end;
}
if (!Connection->IsAnsi || iOdbc())
{
/* If application is not ansi, we should convert wchar into connection string */
Connection->ConnOrSrcCharset= &Connection->Charset;
}
}
/* todo: error handling */
mysql_optionsv(Connection->mariadb, MYSQL_SET_CHARSET_NAME, Connection->Charset.cs_info->csname);
/* This should go before any DSN_OPTION macro use. I don't know why can't we use Dsn directly, though */
Connection->Options = Dsn->Options;
if (DSN_OPTION(Connection, MADB_OPT_FLAG_MULTI_STATEMENTS))
{
client_flags|= CLIENT_MULTI_STATEMENTS;
MADB_InitDynamicString(&InitCmd, "", 1024, 1024);
}
if (Dsn->InitCommand && Dsn->InitCommand[0])
{
MADB_AddInitCommand(Connection->mariadb, &InitCmd, DSN_OPTION(Connection, MADB_OPT_FLAG_MULTI_STATEMENTS), Dsn->InitCommand);
}
/* Turn sql_auto_is_null behavior off.
For more details see: http://bugs.mysql.com/bug.php?id=47005 */
MADB_AddInitCommand(Connection->mariadb, &InitCmd, DSN_OPTION(Connection, MADB_OPT_FLAG_MULTI_STATEMENTS), "SET SESSION SQL_AUTO_IS_NULL=0");
/* set autocommit behavior */
if (Connection->AutoCommit != 0)
{
MADB_AddInitCommand(Connection->mariadb, &InitCmd, DSN_OPTION(Connection, MADB_OPT_FLAG_MULTI_STATEMENTS), "SET autocommit=1");
}
else
{