-
Notifications
You must be signed in to change notification settings - Fork 2
/
ma_connection.c
2537 lines (2409 loc) · 136 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,2019 MariaDB Corporation AB
2021 SingleStore, Inc.
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 <keywords/keywords.hpp>
#include <ma_odbc.h>
extern const char* DefaultPluginLocation;
struct st_madb_isolation MADB_IsolationLevel[] =
{
{SQL_TRANSACTION_REPEATABLE_READ, "REPEATABLE READ"},
{SQL_TRANSACTION_READ_COMMITTED, "READ COMMITTED"},
{SQL_TRANSACTION_READ_UNCOMMITTED, "READ UNCOMMITTED"},
{SQL_TRANSACTION_SERIALIZABLE, "SERIALIZABLE"},
{0, 0}
};
/* 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, TODO PLAT-5516
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, TODO PLAT-5515
SQL_API_SQLCOLATTRIBUTES,
SQL_API_SQLCOLUMNPRIVILEGES ,
SQL_API_SQLDESCRIBEPARAM,
SQL_API_SQLDRIVERCONNECT,
SQL_API_SQLDRIVERS,
SQL_API_SQLEXTENDEDFETCH,
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_SQLTABLEPRIVILEGES,
// SQL_API_SQLCANCELHANDLE TODO PLAT-5514
};
struct st_ma_connection_methods MADB_Dbc_Methods; /* declared at the end of file */
extern Client_Charset utf8;
#define CHARSET_PTR(isWChar, Dbc) isWChar ? (Dbc->Charset.cs_info ? &Dbc->Charset : &utf8 ): NULL
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 */
Dbc->CatalogName= MADB_ConvertFromWChar((SQLWCHAR *)ValuePtr, StringLength, NULL, Dbc->ConnOrSrcCharset, NULL);
}
else {
ADJUST_LENGTH(ValuePtr, StringLength);
Dbc->CatalogName= strndup((char *)ValuePtr, StringLength);
}
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));
}
}
break;
case SQL_ATTR_LOGIN_TIMEOUT:
Dbc->LoginTimeout= (SQLUINTEGER)(SQLULEN)ValuePtr;
break;
case SQL_ATTR_METADATA_ID:
Dbc->MetadataId= (SQLUINTEGER)(SQLULEN)ValuePtr;
break;
case SQL_ATTR_ODBC_CURSORS:
{
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;
}
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];
_snprintf(StmtStr, sizeof(StmtStr), "SET SESSION TRANSACTION ISOLATION LEVEL %s",
MADB_IsolationLevel[i].StrIsolation);
LOCK_MARIADB(Dbc);
if (mysql_query(Dbc->mariadb, StmtStr))
{
UNLOCK_MARIADB(Dbc);
return MADB_SetError(&Dbc->Error, MADB_ERR_HY001, mysql_error(Dbc->mariadb), mysql_errno(Dbc->mariadb));
}
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:
*(SQLUINTEGER *)ValuePtr= Dbc->AutoCommit;
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= MADB_Dbc_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(CHARSET_PTR(isWChar, Dbc), ValuePtr, BufferLength,
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:
*(SQLUINTEGER *)ValuePtr= Dbc->MetadataId;
case SQL_ATTR_ODBC_CURSORS:
*(SQLINTEGER *)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);
*(SQLINTEGER *)ValuePtr= (SQLINTEGER)packet_size;
}
break;
case SQL_ATTR_QUIET_MODE:
Dbc->QuietMode= (HWND)ValuePtr;
break;
case SQL_ATTR_TRACE:
break;
case SQL_ATTR_TRACEFILE:
break;
case SQL_ATTR_ANSI_APP:
*(SQLUINTEGER *)ValuePtr= SQL_SUCCESS;
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)
{
*(SQLULEN *)ValuePtr= SQL_TRANSACTION_REPEATABLE_READ;
if (Dbc->mariadb)
{
MYSQL_RES *result;
MYSQL_ROW row;
const char *StmtString= "SELECT VARIABLE_VALUE FROM INFORMATION_SCHEMA.SESSION_VARIABLES WHERE VARIABLE_NAME='TX_ISOLATION'";
LOCK_MARIADB(Dbc);
if (mysql_query(Dbc->mariadb, StmtString))
{
UNLOCK_MARIADB(Dbc);
MADB_SetNativeError(&Dbc->Error, SQL_HANDLE_DBC, Dbc->mariadb);
return Dbc->Error.ReturnValue;
}
result= mysql_store_result(Dbc->mariadb);
UNLOCK_MARIADB(Dbc);
if ((row = mysql_fetch_row(result)))
{
unsigned int i;
for (i=0; i < 4; i++)
if (!strcmp(row[0], MADB_IsolationLevel[i].StrIsolation))
{
*(SQLULEN *)ValuePtr= MADB_IsolationLevel[i].SqlIsolation;
break;
}
}
mysql_free_result(result);
}
}
else
*(SQLULEN *)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->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->DataBase);
MADB_DSN_Free(Connection->Dsn);
DeleteCriticalSection(&Connection->cs);
free(Connection);
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_Dbc_GetCurrentDB */
SQLRETURN MADB_Dbc_GetCurrentDB(MADB_Dbc *Connection, SQLPOINTER CurrentDB, SQLINTEGER CurrentDBLength,
SQLSMALLINT *StringLengthPtr, my_bool isWChar)
{
MADB_Stmt *Stmt;
SQLRETURN ret;
SQLLEN Size;
char Buffer[65 * sizeof(SQLWCHAR)];
MADB_CLEAR_ERROR(&Connection->Error);
ret= MA_SQLAllocHandle(SQL_HANDLE_STMT, (SQLHANDLE) Connection, (SQLHANDLE*)&Stmt);
if (!SQL_SUCCEEDED(ret))
return ret;
// Currently we have to check if DATABASE() != '' because SingleStore returns an empty string (rather than NULL)
// for a connection with no database.
char* currentDbQuery = "SELECT IF(DATABASE() IS NOT NULL AND DATABASE() != '',DATABASE(),'null')";
if (!SQL_SUCCEEDED(Stmt->Methods->ExecDirect(Stmt, currentDbQuery, SQL_NTS)) ||
!SQL_SUCCEEDED(Stmt->Methods->Fetch(Stmt)))
{
MADB_CopyError(&Connection->Error, &Stmt->Error);
goto end;
}
ret= Stmt->Methods->GetData(Stmt, 1, SQL_CHAR, Buffer, 65, &Size, TRUE);
Size= (SQLSMALLINT)MADB_SetString(isWChar ? & Connection->Charset : 0,
(void *)CurrentDB, BUFFER_CHAR_LEN(CurrentDBLength, isWChar), Buffer,
SQL_NTS, &Connection->Error);
if (StringLengthPtr)
*StringLengthPtr= isWChar ? (SQLSMALLINT)Size * sizeof(SQLWCHAR) : (SQLSMALLINT)Size;
end:
MA_SQLFreeStmt(Stmt, SQL_DROP);
return Connection->Error.ReturnValue;
}
/* }}} */
/* {{{ 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->mariadb && mysql_rollback(Dbc->mariadb))
MADB_SetNativeError(&Dbc->Error, SQL_HANDLE_DBC, Dbc->mariadb);
break;
case SQL_COMMIT:
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);
}
UNLOCK_MARIADB(Dbc);
return Dbc->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_Dbc_Connect
Mind that this function is used for establishing connection from the setup lib
*/
SQLRETURN MADB_DbcConnect(MADB_Dbc *Connection,
MADB_Dsn *Dsn)
{
char StmtStr[128];
unsigned ReportDataTruncation= 1;
unsigned int i;
unsigned long client_flags= 0L;
my_bool my_reconnect= 1;
if (!Connection || !Dsn)
return SQL_ERROR;
char *emailForSSO = Dsn->UserName;
if (Dsn->JWT)
{
// we will send JWT as the password
Dsn->Password = Dsn->JWT;
if (!Dsn->UserName || !Dsn->UserName[0])
{
Dsn->UserName = MADB_ALLOC(2*sizeof(char));
strcpy(Dsn->UserName, "*");
}
Dsn->JWT = NULL;
}
if (Dsn->IsBrowserAuth)
{
if (GetCredentialsBrowserSSO(Connection, Dsn, emailForSSO, TRUE /*readCached*/))
{
goto end;
}
}
// connectionTried indicates if mysql_real_connect has already been called, i.e.
// if we already issued a connection request to the server
my_bool connectionTried = FALSE;
real_connect:
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( !MADB_IS_EMPTY(Dsn->ConnCPluginsDir))
{
mysql_optionsv(Connection->mariadb, MYSQL_PLUGIN_DIR, Dsn->ConnCPluginsDir);
}
else
{
if (DefaultPluginLocation != NULL)
{
mysql_optionsv(Connection->mariadb, MYSQL_PLUGIN_DIR, DefaultPluginLocation);
}
}
if (Dsn->ReadMycnf != '\0')
{
mysql_optionsv(Connection->mariadb, MYSQL_READ_DEFAULT_GROUP, (void *)"odbc");
}
/* 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))
{
cs_name= Dsn->CharacterSet;
}
else if (Connection->IsAnsi)
{
MARIADB_CHARSET_INFO *cs= mariadb_get_charset_by_name("auto");
cs_name= cs->csname;
}
// Use utf8mb3 by default if no specific charset was provided by the client.
if (InitClientCharset(&Connection->Charset, MADB_IS_EMPTY(cs_name) ? "utf8" : cs_name))
{
/* Memory allocation error */
MADB_SetError(&Connection->Error, MADB_ERR_HY001, "Charset is not supported", 0);
goto end;
}
if (iOdbc() && strcmp(Connection->Charset.cs_info->csname, "swe7") == 0)
{
MADB_SetError(&Connection->Error, MADB_ERR_HY001, "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);
if (Dsn->InitCommand && Dsn->InitCommand[0])
mysql_optionsv(Connection->mariadb, MYSQL_INIT_COMMAND, Dsn->InitCommand);
if (Dsn->ConnectionTimeout)
mysql_optionsv(Connection->mariadb, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&Dsn->ConnectionTimeout);
Connection->Options= Dsn->Options;
if (DSN_OPTION(Connection, MADB_OPT_FLAG_AUTO_RECONNECT))
mysql_optionsv(Connection->mariadb, MYSQL_OPT_RECONNECT, &my_reconnect);
if (Dsn->IsNamedPipe) /* DSN_OPTION(Connection, MADB_OPT_FLAG_NAMED_PIPE) */
mysql_optionsv(Connection->mariadb, MYSQL_OPT_NAMED_PIPE, (void *)Dsn->ServerName);
if (DSN_OPTION(Connection, MADB_OPT_FLAG_NO_SCHEMA))
client_flags|= CLIENT_NO_SCHEMA;
if (DSN_OPTION(Connection, MADB_OPT_FLAG_IGNORE_SPACE))
client_flags|= CLIENT_IGNORE_SPACE;
if (DSN_OPTION(Connection, MADB_OPT_FLAG_FOUND_ROWS))
client_flags|= CLIENT_FOUND_ROWS;
if (DSN_OPTION(Connection, MADB_OPT_FLAG_COMPRESSED_PROTO))
client_flags|= CLIENT_COMPRESS;
if (DSN_OPTION(Connection, MADB_OPT_FLAG_MULTI_STATEMENTS))
client_flags|= CLIENT_MULTI_STATEMENTS;
if (Dsn->InteractiveClient)
{
mysql_optionsv(Connection->mariadb, MARIADB_OPT_INTERACTIVE, 1);
}
/* enable truncation reporting */
mysql_optionsv(Connection->mariadb, MYSQL_REPORT_DATA_TRUNCATION, &ReportDataTruncation);
if (Dsn->Socket)
{
int protocol= MYSQL_PROTOCOL_SOCKET;
mysql_optionsv(Connection->mariadb, MYSQL_OPT_PROTOCOL, (void*)&protocol);
}
{
/* 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' 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))
{
char Enable= 1;
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_ENFORCE, &Enable);
if (!MADB_IS_EMPTY(SslKey))
{
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_KEY, SslKey);
}
if (!MADB_IS_EMPTY(SslCert))
{
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_CERT, SslCert);
}
if (!MADB_IS_EMPTY(SslCa))
{
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_CA, SslCa);
}
if (!MADB_IS_EMPTY(SslCaPath))
{
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_CAPATH, SslCaPath);
}
if (!MADB_IS_EMPTY(SslCipher))
{
mysql_optionsv(Connection->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(Connection->mariadb, MARIADB_OPT_TLS_VERSION, (void *)TlsVersion);
}
}
if (Dsn->SslVerify)
{
const unsigned int verify= 0x01010101;
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char*)&verify);
}
else
{
const unsigned int verify= 0;
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char*)&verify);
}
}
if (Dsn->ForceTls != '\0')
{
const unsigned int ForceTls= 0x01010101;
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_ENFORCE, (const char*)&ForceTls);
}
if (!MADB_IS_EMPTY(Dsn->SslCrl))
{
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_CRL, Dsn->SslCrl);
}
if (!MADB_IS_EMPTY(Dsn->SslCrlPath))
{
mysql_optionsv(Connection->mariadb, MYSQL_OPT_SSL_CRLPATH, Dsn->SslCrlPath);
}
if (!MADB_IS_EMPTY(Dsn->ServerKey))
{
mysql_optionsv(Connection->mariadb, MYSQL_SERVER_PUBLIC_KEY, Dsn->ServerKey);
}
if (!MADB_IS_EMPTY(Dsn->TlsPeerFp))
{
mysql_optionsv(Connection->mariadb, MARIADB_OPT_TLS_PEER_FP, (void*)Dsn->TlsPeerFp);
}
if (!MADB_IS_EMPTY(Dsn->TlsPeerFpList))
{
mysql_optionsv(Connection->mariadb, MARIADB_OPT_TLS_PEER_FP_LIST, (void*)Dsn->TlsPeerFpList);
}
if (!MADB_IS_EMPTY(Dsn->TlsKeyPwd))
{
mysql_optionsv(Connection->mariadb, MARIADB_OPT_TLS_PASSPHRASE, (void*)Dsn->TlsKeyPwd);
}
#ifdef MAODBC_UNICODEDRIVER
mysql_options4(Connection->mariadb, MYSQL_OPT_CONNECT_ATTR_ADD, "_driver_name", "SingleStore ODBC Unicode Driver");
#else
mysql_options4(Connection->mariadb, MYSQL_OPT_CONNECT_ATTR_ADD, "_driver_name", "SingleStore ODBC ANSI Driver");
#endif
mysql_options4(Connection->mariadb, MYSQL_OPT_CONNECT_ATTR_ADD, "_driver_version", MARIADB_ODBC_VERSION);
if (!MADB_IS_EMPTY(Dsn->App))
{
mysql_options4(Connection->mariadb, MYSQL_OPT_CONNECT_ATTR_ADD, "program_name", Dsn->App);
}
// Dsn->ConnectAttrs is a comma-separated list of key-value pairs in the form key:value
if (!MADB_IS_EMPTY(Dsn->ConnAttrs))
{
char *connect_attrs = (char*)MADB_ALLOC(strlen(Dsn->ConnAttrs) + 1);
strcpy(connect_attrs, Dsn->ConnAttrs);
char *attr = strtok(connect_attrs, ",:");
char *key, *value;
int attr_idx = 0;
while(attr != NULL)
{
if (attr_idx % 2 == 0)
{
key = attr;
}
else
{
value = attr;
mysql_options4(Connection->mariadb, MYSQL_OPT_CONNECT_ATTR_ADD, key, value);
}
attr = strtok(NULL, ",:");
++attr_idx;
}
MADB_FREE(connect_attrs);
}
if (!mysql_real_connect(Connection->mariadb,
Dsn->Socket ? "localhost" : Dsn->ServerName, Dsn->UserName, Dsn->Password,
Dsn->Catalog && Dsn->Catalog[0] ? Dsn->Catalog : NULL, Dsn->Port, Dsn->Socket, client_flags))
{
// in case of Browser Auth we try to get credentials from the browser
// without reading the keyring
if (Dsn->IsBrowserAuth && !connectionTried)
{
connectionTried = TRUE;
// we've already tried to run GetCredentialsBrowserSSO above, so
// we need to cleanup the old credentials first
MADB_FREE(Dsn->UserName);
MADB_FREE(Dsn->Password);
if (GetCredentialsBrowserSSO(Connection, Dsn, emailForSSO, FALSE /*readCached*/))
{
goto end;
}
// if we get credentials successfully, we retry connection
else
{
goto real_connect;
}
}
goto mysql_native_err;
}
/* set default catalog */
if (Connection->CatalogName && Connection->CatalogName[0])
{
if (mysql_select_db(Connection->mariadb, Connection->CatalogName))
goto mysql_native_err;
}
// mysql_set_character_set in the form of the mysql_optionsv call is a no-op:
// mysql_optionsv(*, MYSQL_SET_CHARSET_NAME, *) doesn't set database/connection charset
mysql_set_character_set(Connection->mariadb, Connection->Charset.cs_info->csname);
/* set autocommit behavior */
if (mysql_autocommit(Connection->mariadb, (my_bool)Connection->AutoCommit))
goto mysql_native_err;
if (!Dsn->CompatMode) {
MYSQL_RES *result;
MYSQL_ROW row;
const char *StmtString= "SELECT @@memsql_version, @@aggregator_id";
LOCK_MARIADB(Connection);
if (mysql_query(Connection->mariadb, StmtString))
{
UNLOCK_MARIADB(Connection);
return MADB_SetNativeError(&Connection->Error, SQL_HANDLE_DBC, Connection->mariadb);
}
result= mysql_store_result(Connection->mariadb);
UNLOCK_MARIADB(Connection);
if ((row = mysql_fetch_row(result)))
{
char* ss_version = (char*) row[0];
int aggregator_id = atoi((char*)(row[1]));
mysql_optionsv(Connection->mariadb, MYSQL_SS_VERSION, ss_version);
mysql_optionsv(Connection->mariadb, SS_AGGREGATOR_ID, &aggregator_id);
}
mysql_free_result(result);
}
SetDBCharsetnr(Connection);
/* Set isolation level */
if (Connection->IsolationLevel)
for (i=0; i < 4; i++)
{
if (MADB_IsolationLevel[i].SqlIsolation == Connection->IsolationLevel)
{
_snprintf(StmtStr, 128, "SET SESSION TRANSACTION ISOLATION LEVEL %s",
MADB_IsolationLevel[i].StrIsolation);
if (mysql_query(Connection->mariadb, StmtStr))
goto mysql_native_err;
break;
}
}
goto end;
mysql_native_err:
MADB_SetNativeError(&Connection->Error, SQL_HANDLE_DBC, Connection->mariadb);
end:
if (Connection->Error.ReturnValue == SQL_ERROR && Connection->mariadb)
{
mysql_close(Connection->mariadb);
Connection->mariadb= NULL;
}
return Connection->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_DbcGetFunctions */
SQLRETURN MADB_DbcGetFunctions(MADB_Dbc *Dbc, SQLUSMALLINT FunctionId, SQLUSMALLINT *SupportedPtr)
{
unsigned int i, Elements= sizeof(MADB_supported_api) / sizeof(SQLUSMALLINT);
switch(FunctionId) {
case SQL_API_ODBC3_ALL_FUNCTIONS:
/* clear ptr */
memset(SupportedPtr, 0, sizeof(SQLUSMALLINT) * SQL_API_ODBC3_ALL_FUNCTIONS_SIZE);
for (i=0; i < Elements; ++i)
{
SQLUSMALLINT function= MADB_supported_api[i];
SupportedPtr[function >> 4]|= (1 << (function & 0x000F));
}
return SQL_SUCCESS;
case SQL_API_ALL_FUNCTIONS:
/* Set all to SQL_FALSE (0) */
memset(SupportedPtr, 0, sizeof(SQLUSMALLINT) * 100);
for (i=0; i < Elements; i++)
if (MADB_supported_api[i] < 100)
SupportedPtr[MADB_supported_api[i]]= SQL_TRUE;
return SQL_SUCCESS;
default:
*SupportedPtr= SQL_FALSE;
for (i=0; i < Elements; i++)
if (MADB_supported_api[i] == FunctionId)
{
*SupportedPtr= SQL_TRUE;
return SQL_SUCCESS;
}
break;
}
return MADB_SetError(&Dbc->Error, MADB_ERR_HY095, "Function type out of range",
mysql_errno(Dbc->mariadb));
}
/* }}} */
/* {{{ IsStringInfoType */