File: MySQLConnection.pas

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (1502 lines) | stat: -rw-r--r-- 49,608 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
unit MySQLConnection;

// Copyright (C) 2003 - 2004 MySQL AB, 2008 Sun Microsystems, Inc.

interface

uses
  gnugettext, Windows, Messages, SysUtils, Classes, ComCtrls, ImgList,
  Controls, TntComCtrls, TntClasses, Forms, Contnrs,
  ExtCtrls, AuxFuncs, PNGImage, StdCtrls,
  SyncObjs, myx_util_public_interface, myx_public_interface, MyxError,
  Options, myx_sql_parser_public_interface,
  LexicalTools, StringContainers;

const
  WM_CONNECTION_LOST        = WM_USER + 101; // The currently connected server got lost. Reconnection might be possible.
  WM_CONNECTION_ESTABLISHED = WM_USER + 102; // A lost server connection could be established again.
  WM_CONNECTED              = WM_USER + 103; // A new server connection was created (new MySQL structure etc.).
  WM_DISCONNECTED           = WM_USER + 104; // A server connection was completely cut. No reconnection possible.
  WM_DefaultSchemaChanged   = WM_USER + 105;
  WM_SchemaListChanged      = WM_USER + 106;

  CR_SERVER_GONE_ERROR = 2006;
  CR_SERVER_LOST = 2013;
  CR_CONN_HOST_ERROR = 2003;

type
  TFetchDataThread = class;

  // Indicates what kind of data is being fetched currently.
  TFetchingDataKind = 
  (
    dkCatalogSchema,
    dkUserNames,
    dkProcessList,
    dkSchemaTables,
    dkStatusVars,
    djServerVars,
    dkUserData,
    dkSchemaTableStatus,
    dkTableAction,
    dkSchemaIndices,
    dkSchemaViewStatus,
    dkSchemaSPStatus,
    dkSchemaProcBody,
    dkSchemaProcDrop,
    dkSchemaViewDef,
    dkSchemaViewDrop
  );

  TFetchingDataKinds = set of TFetchingDataKind;
  
  TThreadExecMethod = procedure(Sender: TObject) of object;

  TConnectionLock = class(TCriticalSection)
  public
    function TryEnter: Boolean; virtual;
  end;

  IMySQLConnTransactionStatusChangeListener = interface
    procedure TransactionStatusChanged;
    procedure TransactionQueryExecuted(SQLCmd: WideString);
    procedure TransactionStarted;
  end;

  TConnectionState =
  (
    csConnected,
    csUnclear,
    csDisconnected
  );

  TMySQLConn = class(TObject)
  private
    FCurrentDataThread: TFetchDataThread;        // Set by each thread as soon as it starts working. Access is serialized
                                                 // via the critical section.
    FMySQL: Pointer;                             // Main MySQL server connection data.
    FDefaultSchema: WideString;
    FInTransaction: Boolean;
    FTransactionStatusChangeListeners: TList;
    FQuoteChar: WideString;
    FWorkList: TThreadList;
    FStatusBar: TTntStatusBar;
    FEmbeddedConnection: Boolean;
    FFetchDataLock: TConnectionLock;

    FMajorVersion,
    FMinorVersion: Integer;
    FUserConnection: TMYX_USER_CONNECTION;

    FParameters: TTntStringList;
    FDoParameterSubstitution: Boolean;

    FQueryHookMultiReadSync: TMultiReadExclusiveWriteSynchronizer;
    FQueryHookSQL: WideString;

    FDataBeingFetched: TFetchingDataKinds;

    FConnected: Boolean;
    FIsLocalServer: Boolean;

    FAllowConnectionSkip: Boolean;
    FKeepAlive: Boolean;

    FErrorCode: MYX_LIB_ERROR;
    FKeepAliveTimer: TTimer;
    FLastConnectionState: TConnectionState;

    procedure KeepAliveTrigger(Sender: TObject);
    procedure SetConnected(const Value: Boolean);
    procedure SetKeepAlive(const Value: Boolean);
  protected
    function ShowConnectToInstanceForm(var User_connection: TMYX_USER_CONNECTION; var MySQL: Pointer;
      MySQLConnectionsOnly: Boolean; SelectSchemata: Boolean; Create2: Boolean): Integer;
    function ConnectUsingUserConnection(Connection: WideString; var User_connection: TMYX_USER_CONNECTION;
      SelectSchemata: Boolean): Integer;
    function ConnectUsingCommandlineConnectionParams(var User_connection: TMYX_USER_CONNECTION; var FMySQL: Pointer;
      SelectSchemata: Boolean): Integer;
    function Connect(user_conn: TMYX_USER_CONNECTION; SelectSchemata: Boolean): Integer;

    function InternalStartsTransaction(Lexer: TSQLLexer): Boolean;
    function ExecuteDirect(SQLCmd: WideString; WaitTime: Integer; DisplayErrorDialog: Boolean; AllowMultiQueries: Boolean): Boolean; overload;

    procedure SetDefaultSchema(const Value: WideString);
    procedure SetInTransaction(InTransaction: Boolean);
    function GetQuoteChar: WideString;
    function GetFetchingData: Boolean;
  public
    constructor Create(StatusBar: TTntStatusBar);
    destructor Destroy; override;

    procedure ClearWorkList;
    procedure CheckConnectionStatusChange(SQLCmd: WideString);
    procedure TransactionQueryExecuted;
    function ConnectToEmbedded: Integer;
    function ConnectToServer(AutoConnect: Boolean; MySQLConnectionsOnly: Boolean = True; SelectSchemata:
      Boolean = False; Create2: Boolean = False): Integer;
    function Reconnect(RefreshOnly: Boolean = False): Boolean;
    procedure Disconnect(DoNotification: Boolean);
    procedure FetchData(KindOfData: TFetchingDataKind; FetchMethod: TThreadExecMethod; RefreshMethod: TThreadExecMethod;
      Target: TObject; StatusBarText: WideString; AllowParallelExecution: Boolean = False;
      AllowDuplicatedKindOfData: Boolean = False; ShowWaitCursor: Boolean = True; ControlThread: TThread = nil);
    procedure StopCurrentThread(Timeout: Integer);

    function ExecuteDirect(SQLCmd: WideString; WaitTime: Integer = 0; DisplayErrorDialog: Boolean = True): Boolean; overload;
    function ExecuteDirectMulti(SQLCmd: WideString; WaitTime: Integer = 0; DisplayErrorDialog: Boolean = True): Boolean;
    function ExecuteDirectQuery(SQLCmd: WideString; FetchMethod: TThreadExecMethod;
      WaitTime: Integer = 0; DisplayErrorDialog: Boolean = True): Boolean;

    procedure SchemaListChanged;

    procedure AddTransactionStatusListener(Listener: IMySQLConnTransactionStatusChangeListener);
    procedure RemoveTransactionStatusListener(Listener: IMySQLConnTransactionStatusChangeListener);

    function GetConnectionCaption(IncludeSchema: Boolean = True): WideString;
    function Ping: Integer;
    procedure UseStatusBar(StatusBar: TTntStatusBar);
    function StartsTransaction(const SQL: WideString): Boolean;

    property AllowConnectionSkip: Boolean read FAllowConnectionSkip write FAllowConnectionSkip default True;
    property Connected: Boolean read FConnected write SetConnected;
    property DataBeingFetched: TFetchingDataKinds read FDataBeingFetched;
    property DefaultSchema: WideString read FDefaultSchema write SetDefaultSchema;
    property EmbeddedConnection: Boolean read FEmbeddedConnection;
    property FetchingData: Boolean read GetFetchingData;
    property InTransaction: Boolean read FInTransaction write SetInTransaction;
    property IsLocalServer: Boolean read FIsLocalServer write FIsLocalServer;
    property KeepAlive: Boolean read FKeepAlive write SetKeepAlive;
    property Lock: TConnectionLock read FFetchDataLock;
    property MajorVersion: Integer read FMajorVersion;
    property MinorVersion: Integer read FMinorVersion;
    property MySQL: Pointer read FMySQL;
    property QuoteChar: WideString read GetQuoteChar;
    property UserConnection: TMYX_USER_CONNECTION read FUserConnection;
  end;

  TFetchDataThread = class(TThread)
  private
    FConnection: TMySQLConn;
    FErrorMessage: WideString;
    FFetchMethod: TThreadExecMethod;
    FRefreshMethod: TThreadExecMethod;
    FSynchronizedMethod: TThreadExecMethod;
    FKindOfData: TFetchingDataKind;
    FTarget: TObject;
    FStatusBarText: WideString;
    FAllowParallelExecution: Boolean;
    FShowWaitCursor: Boolean;
  protected
    procedure Execute; override;
    procedure SetStatusBar(StatusBarText: WideString);
  public
    constructor Create(AMySQLConn: TMySQLConn; AKindOfData: TFetchingDataKind; AFetchMethod: TThreadExecMethod;
      ARefreshMethod: TThreadExecMethod; ATarget: TObject; AStatusBarText: WideString; AAllowParallelExecution,
      ShowWaitCursor: Boolean);
    procedure UpdateStatusBar;
    procedure ShowError;
    procedure ExecuteSynchronized(SynchronizedMethod: TThreadExecMethod);
    procedure ExecuteSynchronizedMethod;

    property Connection: TMySQLConn read FConnection;
    property Target: TObject read FTarget;
    property StatusBarText: WideString write SetStatusBar;
  end;

function ExecuteMultiCmdSQL(sql: PChar; user_data: Pointer): Integer cdecl;

//----------------------------------------------------------------------------------------------------------------------

implementation

uses
  ConnectToInstance;

var
  DataThreadCount: Integer;
  
//----------------------------------------------------------------------------------------------------------------------

constructor TMySQLConn.Create(StatusBar: TTntStatusBar);

begin
  inherited Create;

  FStatusBar := StatusBar;

  FWorkList := TThreadList.Create;
  FMySQL := nil;
  FUserConnection := nil;
  FTransactionStatusChangeListeners := nil;

  FDefaultSchema := '';

  FConnected := False;
  FIsLocalServer := False;

  FEmbeddedConnection := False;

  //Initialize components for thread handling
  FFetchDataLock := TConnectionLock.Create;
  FDataBeingFetched := [];

  FParameters := TTntStringList.Create;
  FDoParameterSubstitution := False;

  FQueryHookMultiReadSync := TMultiReadExclusiveWriteSynchronizer.Create;

  FAllowConnectionSkip := True;
  FKeepAlive := True;
  FKeepAliveTimer := TTimer.Create(nil);
  with FKeepAliveTimer do
  begin
    OnTimer := KeepAliveTrigger;
    Interval := 5000;
    Enabled := True;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

destructor TMySQLConn.Destroy;

begin
  FKeepAliveTimer.Free;
  ClearWorkList;
  FWorkList.Free;
  FFetchDataLock.Free;
  FQueryHookMultiReadSync.Free;
  FUserConnection.Free;
  FParameters.Free;

  // Close connection and free memory for MySQL struct
  if Assigned(FMySQL) then
    myx_mysql_close(FMySQL);

  inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.ClearWorkList;

// Goes through the work list and tells all waiting threads to terminate. Before that however the current thread must
// be stopped. It is given a timeout of 2 seconds to finish its work otherwise it is killed.

var
  I: Integer;
  CurrentThread: TFetchDataThread;

begin
  CurrentThread := nil;
  with FWorkList.LockList do
  try
    for I := 0 to Count - 1 do
      TFetchDataThread(Items[I]).Terminate;

    // Now that all threads are marked to terminate they will not start any work but immediately return
    // as soon as the fetch lock is free.
    // Now finish the currently active thread if there is one.
    // Keep the lock on the work list so the current thread does not interfere in our manipulation here.
    if Assigned(FCurrentDataThread) then
    begin
      FCurrentDataThread.FreeOnTerminate := False;
      FCurrentDataThread.Terminate;
      CurrentThread := FCurrentDataThread;
    end;

    // Finally empty work list. The dangling threads will do nothing, just awake and finish their
    // thread proc without calling application code, so we don't need to care any further.
    Clear;
  finally
    FWorkList.UnlockList;
  end;

  if Assigned(CurrentThread) then
  begin
    // Wait 2 seconds and kill the thread if it did not stop working within that time.
    if WaitForSingleObject(CurrentThread.Handle, 2000) = WAIT_TIMEOUT then
    begin
      TerminateThread(CurrentThread.Handle, 999);

      // Restore default cursor and adjust current connection state.
      // The thread had no opportunity to do it.
      if not CurrentThread.FAllowParallelExecution then
        FFetchDataLock.Release;
      InterlockedDecrement(DataThreadCount);
      if DataThreadCount = 0 then
        Screen.Cursor := crDefault;
      FCurrentDataThread := nil;
    end;
    FreeAndNil(CurrentThread);
  end;

  FDataBeingFetched := [];
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ConnectToServer(AutoConnect: Boolean; MySQLConnectionsOnly: Boolean = True;
  SelectSchemata: Boolean = False; Create2: Boolean = False): Integer;

begin
  // If AutoConnect and a connection was specified
  if (AutoConnect) and (MYXCommonOptions.ConnectionToUse <> '') then
    Result := ConnectUsingUserConnection(MYXCommonOptions.ConnectionToUse, FUserConnection, SelectSchemata)
  else
    // If AutoConnect and username was specified
    if (AutoConnect) and (MYXCommonOptions.ConnectionUsername <> '') then
      Result := ConnectUsingCommandlineConnectionParams(FUserConnection, FMySQL, SelectSchemata)
    else
      Result := ShowConnectToInstanceForm(FUserConnection, FMySQL, MySQLConnectionsOnly, SelectSchemata, Create2);

  if Result = 1 then
  begin
    FMajorVersion := myx_get_mysql_major_version(FMySQL);
    FMinorVersion := myx_get_mysql_minor_version(FMySQL);

    FConnected := True;
    FIsLocalServer := myx_is_localhost(FUserConnection.hostname) = 1;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ShowConnectToInstanceForm(var User_connection: TMYX_USER_CONNECTION; var MySQL: Pointer;
  MySQLConnectionsOnly: Boolean; SelectSchemata: Boolean; Create2: Boolean): Integer;

var
  ConnectToInstanceForm: TConnectToInstanceForm;
  ModalRes: Integer;

begin
  ConnectToInstanceForm := TConnectToInstanceForm.Create(nil, MySQLConnectionsOnly, SelectSchemata);

  try
    ConnectToInstanceForm.AllowConnectionSkip := FAllowConnectionSkip;
    ModalRes := ConnectToInstanceForm.ShowModal;
    if ModalRes = mrOK then
    begin
      // Terminate old connection if still connected.
      Disconnect(False);

      // Initialize Variables.
      User_connection := TMYX_USER_CONNECTION.create(ConnectToInstanceForm.User_Connection.get_record_pointer);
      MySQL := ConnectToInstanceForm.PMySQL;
      DefaultSchema := myx_get_default_schema(FMySQL);

      Result := 1;
    end
    else
      if (ModalRes = mrAbort) then
        Result := -1
      else
        Result := 0;

  finally
    ConnectToInstanceForm.Free;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ConnectUsingUserConnection(Connection: WideString; var User_connection: TMYX_USER_CONNECTION;
  SelectSchemata: Boolean): Integer;

var
  user_conns: PMYX_USER_CONNECTIONS;
  stored_conns: TMYX_USER_CONNECTIONS;
  user_conn: TMYX_USER_CONNECTION;
  error: MYX_LIB_ERROR;
  i: Integer;

begin
  Result := -1;

  //Get connection
  //Fetch connections from library
  user_conns := myx_load_user_connections(
    MYXCommonOptions.UserDataDir + 'mysqlx_user_connections.xml', @error);
  if (error <> MYX_NO_ERROR) then
    raise EMyxCommonLibraryError.Create(_('Error while loading stored connections.'),
      Ord(error), MYXCommonOptions.UserDataDir + 'mysqlx_user_connections.xml');

  try
    stored_conns := TMYX_USER_CONNECTIONS.create(user_conns);
    try
      user_conn := nil;
      for i := 0 to stored_conns.user_connections.Count - 1 do
      begin
        if (CompareText(stored_conns.user_connections[i].connection_name,
          MYXCommonOptions.ConnectionToUse) = 0) then
        begin
          user_conn := stored_conns.user_connections[i];
          break;
        end;
      end;

      //If the connection is found, connect
      if (user_conn <> nil) then
      begin
        //Copy connection
        User_connection := TMYX_USER_CONNECTION.Create(user_conn.get_record_pointer);

        Result := Connect(User_connection, SelectSchemata);
      end
      else
        ShowModalDialog(Application.Title + ' - ' + _('Connection not found'),
          Format(_('The connection %s cannot be found in the list of stored connections.'),
          [MYXCommonOptions.ConnectionToUse]), myx_mtError, _('OK'));
    finally
      stored_conns.Free;
    end;
  finally
    myx_free_user_connections(user_conns);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ConnectUsingCommandlineConnectionParams(var User_connection: TMYX_USER_CONNECTION;
  var FMySQL: Pointer; SelectSchemata: Boolean): Integer;
  
begin
  //Create new connection form command line parameters
  User_connection := TMYX_USER_CONNECTION.Create(
    '',
    MYXCommonOptions.ConnectionUsername,
    MYXCommonOptions.ConnectionPassword,
    MYXCommonOptions.ConnectionHost,
    MYXCommonOptions.ConnectionPort,
    MYXCommonOptions.ConnectionSchema,
    '', '', MYX_MYSQL_CONN, MYX_FAVORITE_USER_CONNECTION);

  Result := Connect(User_connection, SelectSchemata);
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.Connect(user_conn: TMYX_USER_CONNECTION; SelectSchemata: Boolean): Integer;

var
  ConnectionResult: Integer;

begin
  FMySQL := myx_mysql_init();
  if FMySQL = nil then
  begin
    ShowModalDialog(Application.Title + ' ' + _('Error'), _('Error while allocating memory for MySQL Struct.'),
      myx_mtError, 'OK');

    Result := -1;
    Exit;
  end;

  ConnectionResult := myx_connect_to_instance(user_conn.get_record_pointer, FMySQL);
  if ConnectionResult = 0 then
  begin
    if SelectSchemata and (user_conn.schema <> '') then
    begin
      ConnectionResult := myx_use_schema(FMySQL, user_conn.schema);
      if ConnectionResult <> 0 then
      begin
        ShowModalDialog(Application.Title + ' ' + _('Error'), Format(_('The schema %s cannot be selected.'),
          [user_conn.schema]), myx_mtError, 'OK');
        Result := -1;
        Exit;
      end;

      DefaultSchema := myx_get_default_schema(FMySQL);
    end;

    Result := 1;
  end
  else
  begin
    ShowModalDialog(Application.Title + ' ' + _('Error'),
      _('Could not connect to the specified host.') + ' ' + #13#10 + #13#10 +
      Format(_('MySQL Error Number %d' + #13#10 + '%s'), [myx_mysql_errno(FMySQL), myx_mysql_error(FMySQL)]),
      myx_mtError, 'OK');

    Result := -1;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.GetConnectionCaption(IncludeSchema: Boolean): WideString;

begin
  if (FUserConnection <> nil) then
  begin
    if (FUserConnection.username = '') and (FUserConnection.hostname = '') and FConnected then
      Result := 'Embedded'
    else
    begin
      if FUserConnection.storage_type = MYX_FAVORITE_USER_CONNECTION then
        Result := 'Connection: ' + FUserConnection.connection_name
      else
        Result := 'Connection: ' + FUserConnection.username + '@' + FUserConnection.hostname + ':' + IntToStr(FUserConnection.port);
      if (FDefaultSchema <> '') and (IncludeSchema) then
        Result := Result + ' / ' + FDefaultSchema;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

type
  TCastThread = class(TThread);
  
procedure TMySQLConn.FetchData(KindOfData: TFetchingDataKind; FetchMethod: TThreadExecMethod;
  RefreshMethod: TThreadExecMethod; Target: TObject; StatusBarText: WideString; AllowParallelExecution:
  Boolean; AllowDuplicatedKindOfData, ShowWaitCursor: Boolean; ControlThread: TThread);

// This method starts retrieval of data by creating a separate thread and using the provided fetch and refresh methods.
// KindOfData determines what must be retrieved and is used in conjunction with AllowDuplicateKindOfData to allow or
// deny certain data retrieval actions.
// ControlThread has a special meaning as it is used to cancel long lasting operations. If it is assigned this method
// waits for the background to return unless the control thread gets terminated. In this case the data thread
// is killed and this method immediately returns to the caller.

var
  WaitResult: Cardinal;
  Thread: TFetchDataThread;
  
begin
  if not (KindOfData in FDataBeingFetched) or AllowDuplicatedKindOfData then
  begin
    //Add KindOfData to DataBeingFetched Set
    if not (KindOfData in FDataBeingFetched) then
      Include(FDataBeingFetched, KindOfData);
    try
      // Create a new thread.
      Thread := TFetchDataThread.Create(Self, KindOfData, FetchMethod, RefreshMethod, Target, StatusBarText,
        AllowParallelExecution, ShowWaitCursor);
      FWorkList.Add(Thread);
      try
        // Thread will be freed after execution if no control thread is given.
        if ControlThread = nil then
          Thread.FreeOnTerminate := True;

        Thread.Resume;

        if Assigned(ControlThread) then
        begin
          // Wait for the data thread to finish but don't deadlock. Instead poll the thread until it is finished
          // or the control thread is terminated.
          WaitResult := WAIT_OBJECT_0;
          while not TCastThread(ControlThread).Terminated do
          begin
            WaitResult := WaitForSingleObject(Thread.Handle, 100);
            if WaitResult = WAIT_OBJECT_0 then
              Break;
          end;
          // Kill the fetch thread if the control thread has been told to finish but the fetch thread is still working.
          if WaitResult = WAIT_TIMEOUT then
          begin
            TerminateThread(Thread.Handle, 999);

            // Close the connection if we have to kill the last query.
            Reconnect;
           end;
          Thread.Free;
        end;
      except
        FWorkList.Remove(Thread);
        Thread.Free;

        raise;
      end;
    except
      FDataBeingFetched := FDataBeingFetched - [KindOfData];

      raise;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.StartsTransaction(const SQL: WideString): Boolean;

// Checks if the given SQL string contains a START TRANSACTION command.
// Actually this function only sets up the Lexer. The actual check is done in an internal function
// which plays nicely with other parsing stuff here.

var
  Input: TWideStringStream;
  Lexer: TSQLLexer;

begin
  Input := TWideStringStream.Create(SQL);
  Lexer := TSQLLexer.Create(Input);
  try
    Result := InternalStartsTransaction(Lexer);
  finally
    Lexer.Free;
    Input.Free;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.StopCurrentThread(Timeout: Integer);

var
  CurrentThread: TFetchDataThread;
  
begin
  CurrentThread := nil;
  with FWorkList.LockList do
  try
    if Assigned(FCurrentDataThread) then
    begin
      FCurrentDataThread.FreeOnTerminate := False;
      FCurrentDataThread.Terminate;
      CurrentThread := FCurrentDataThread;
    end;
  finally
    FWorkList.UnlockList;
  end;

  if Assigned(CurrentThread) then
  begin
    if WaitForSingleObject(CurrentThread.Handle, Timeout) = WAIT_TIMEOUT then
    begin
      TerminateThread(CurrentThread.Handle, 999);

      // Restore default cursor and adjust current connection state.
      // The thread had no opportunity to do it.
      FDataBeingFetched := FDataBeingFetched - [CurrentThread.FKindOfData];
      if not CurrentThread.FAllowParallelExecution then
        FFetchDataLock.Release;
      InterlockedDecrement(DataThreadCount);
      if DataThreadCount = 0 then
        Screen.Cursor := crDefault;
      FCurrentDataThread := nil;
    end;
    FreeAndNil(CurrentThread);

    if Assigned(FStatusBar) then
    begin
      FStatusBar.Panels[1].Text := '';
      FStatusBar.Invalidate;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.Reconnect(RefreshOnly: Boolean): Boolean;

var
  ConnectionResult: Integer;

begin
  Result := False;

  if Assigned(FUserConnection) then
  begin
    if not RefreshOnly then
    begin
      if Assigned(FMySQL) then
        Disconnect(False);

      if FEmbeddedConnection then
        FMySQL := myx_mysql_embedded_init()
      else
        FMySQL := myx_mysql_init();
      if FMySQL = nil then
        raise EMyxError.Create('Error while allocating memory for MySQL Struct.');
    end;

    ConnectionResult := myx_connect_to_instance(FUserConnection.get_record_pointer, FMySQL);
    if ConnectionResult = 0 then
    begin
      FConnected := True;
      MessageToAllForms(WM_CONNECTED, 0, 0);
      Result := True;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.Disconnect(DoNotification: Boolean);

begin
  if FConnected then
  begin
    FConnected := False;

    // Close connection and free memory for MySQL struct.
    myx_mysql_close(FMySQL);
    FMySQL := nil;

    if DoNotification then
      MessageToAllForms(WM_DISCONNECTED, 0, 0);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.CheckConnectionStatusChange(SQLCmd: WideString);

var
  FoundTransactionCommand: Boolean;
  Lexer: TSQLLexer;
  Input: TWideStringStream;
  Token: WideString;

begin
  // Catch transaction status change.
  Input := TWideStringStream.Create(SQLCmd);
  Lexer := TSQLLexer.Create(Input);
  try
    FoundTransactionCommand := InternalStartsTransaction(Lexer);

    if FoundTransactionCommand then
      InTransaction := True
    else
    begin
      // Start over examining input.
      Lexer.Reset;
      if Lexer.NextToken = toSymbol then
      begin
        if Lexer.TokenSymbolIs('commit') then
        begin
          InTransaction := False;
          FoundTransactionCommand := True;
        end
        else
          if Lexer.TokenSymbolIs('rollback') then
          begin
            // Full rollback or only to a named savepoint?
            // The latter does not stop the current transaction.
            Token := Lexer.NextToken;
            if Token = toSymbol then
            begin
              // Skip optional token.
              if Lexer.TokenSymbolIs('work') then
                Token := Lexer.NextToken;
              if not Lexer.TokenSymbolIs('to') then
              begin
                InTransaction := False;
                FoundTransactionCommand := True;
              end;
            end
            else
            begin
              InTransaction := False;
              FoundTransactionCommand := True;
            end;
          end;
      end;
    end;

    // When in transaction, check if the executed command auto-commits the Trx
    if not FoundTransactionCommand and InTransaction then
      InTransaction := (myx_check_whether_commits_transaction(FMySQL, SQLCmd) = 0);
  finally
    Lexer.Free;
    Input.Free;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function ExecuteMultiCmdSQL(sql: PChar; user_data: Pointer): Integer;

var
  Conn: TMySQLConn;
  ErrorCode: MYX_LIB_ERROR;
  AffectedRows: Int64;

begin
  Conn := TMySQLConn(user_data);

  myx_query_execute_direct(Conn.FMySQL, Utf8Decode(sql), @ErrorCode, @AffectedRows);

  if (Conn.FErrorCode = MYX_NO_ERROR) and (ErrorCode <> MYX_NO_ERROR) then
    Conn.FErrorCode := ErrorCode;

  Result := 0;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ExecuteDirect(SQLCmd: WideString; WaitTime: Integer; DisplayErrorDialog: Boolean): Boolean;

begin
  Result := ExecuteDirect(SQLCmd, WaitTime, DisplayErrorDialog, False);
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ExecuteDirectMulti(SQLCmd: WideString; WaitTime: Integer; DisplayErrorDialog: Boolean): Boolean;

begin
  Result := ExecuteDirect(SQLCmd, WaitTime, DisplayErrorDialog, True);
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ExecuteDirect(SQLCmd: WideString; WaitTime: Integer; DisplayErrorDialog: Boolean; AllowMultiQueries: Boolean): Boolean;

// Directly executes a query. This method might run in parallel to an already running fetch thread.
// So make sure we wait while the other thread is running but don't block totally if the caller thread is the main thread.
// Note: This method is not reentrant! You cannot call it while it is already in progress.

var
  MySQLErrorNr: Integer;
  AffectedRows: Int64;

begin

  Result := False;
  
  if MainThreadID = GetCurrentThreadId then
  begin
    // Called by the main thread so check if there is already a data fetch in progress.
    // Wait for it a limited amount of time.
    repeat
      // Try to get the lock. If that succeeds continue normally.
      if FFetchDataLock.TryEnter then
      begin
        WaitTime := 10; // Give it a value > 0 to avoid the exit call below if the caller did not give a wait time.
        Break;
      end;

      // If we could not get the lock then handle all pending messages (e.g. to finish synchronized calls).
      Application.ProcessMessages;

      // Do this every 100 ms...
      Sleep(100);
      Dec(WaitTime, 100);

      // ...until no more wait time is left.
    until WaitTime <= 0;

    if WaitTime <= 0 then
      Exit;
  end
  else
    FFetchDataLock.Acquire;
    
  try
    Result := False;
    try
      FErrorCode := MYX_NO_ERROR;

      if AllowMultiQueries then
        myx_process_sql_statements(SQLCmd, @ExecuteMultiCmdSQL, self, 0)
      else
        myx_query_execute_direct(FMySQL, SQLCmd, @FErrorCode, @AffectedRows);

      if FErrorCode = MYX_NO_ERROR then
      begin
        Result := True;

        // Check if command causes a transaction state change.
        CheckConnectionStatusChange(SQLCmd);
      end
      else
      begin
        MySQLErrorNr := myx_mysql_errno(FMySQL);

        if DisplayErrorDialog then
          ShowModalDialog(_('Execution Error'), _('Error while executing query.') + #13#10#13#10 + SQLCmd + #13#10#13#10 +
            _('MySQL Error Number ') + IntToStr(MySQLErrorNr) + #13#10 + myx_mysql_error(FMySQL), myx_mtError, 'OK');
      end;
    except
      on x: Exception do
        if DisplayErrorDialog then
          ShowModalDialog(_('Execution Error'),  _('The following error occured while executing the query.') + #13#10#13#10 +
            SQLCmd + #13#10#13#10 + x.Message, myx_mtError, 'OK');
    end;
  finally
    FFetchDataLock.Release;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ExecuteDirectQuery(SQLCmd: WideString; FetchMethod: TThreadExecMethod;
  WaitTime: Integer; DisplayErrorDialog: Boolean): Boolean;

// Directly executes a query. This method might run in parallel to an already running fetch thread.
// So make sure we wait until the other thread is running but don't block totally if the caller thread is the main thread.
// Note: This method is not reentrant! You cannot call it while it is already in progress.

var
  MySQLErrorNr: Integer;

begin
  Result := False;
  
  if MainThreadID = GetCurrentThreadId then
  begin
    // Called by the main thread so check if there is already a data fetch in progress.
    // Wait for it a limited amount of time.
    repeat
      // Try to get the lock. If that succeeds continue normally.
      if FFetchDataLock.TryEnter then
      begin
        WaitTime := 10; // Give it a value > 0 to avoid the exit call below if the caller did not give a wait time.
        Break;
      end;

      // If we could not get the lock then handle all pending messages (e.g. to finish synchronized calls).
      Application.ProcessMessages;

      // Do this every 100 ms...
      Sleep(100);
      Dec(WaitTime, 100);

      // ...until no more wait time is left.
    until WaitTime <= 0;

    if WaitTime <= 0 then
      Exit;
  end
  else
    FFetchDataLock.Acquire;
    
  try
    Result := False;
    try
      if myx_mysql_query(FMySQL, SQLCmd) = 0 then
      begin
        Result := True;
        FetchMethod(self);

        // Check if command causes a transaction state change.
        CheckConnectionStatusChange(SQLCmd);
      end
      else
      begin
        MySQLErrorNr := myx_mysql_errno(FMySQL);

        if DisplayErrorDialog then
          ShowModalDialog('Execution Error',
            'Error while executing query.' + #13#10#13#10 +
            SQLCmd + #13#10#13#10 +
            'MySQL Error Number ' + IntToStr(MySQLErrorNr) + #13#10 +
            myx_mysql_error(FMySQL),
            myx_mtError, 'OK');
      end;
    except
      on x: Exception do
        if (DisplayErrorDialog) then
          ShowModalDialog('Execution Error',
            'The following error occured while executing the query.' + #13#10#13#10 +
            SQLCmd + #13#10#13#10 +
            x.Message, myx_mtError, 'OK');
    end;
  finally
    FFetchDataLock.Release;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.KeepAliveTrigger(Sender: TObject);

var
  Notify: Boolean;
  
begin
  case Ping of
    0:
      begin
        // Have connection to server. Notify application if this is the first contact after we lost it previously.
        FConnected := True;
        Notify := FLastConnectionState = csDisconnected;
        FLastConnectionState := csConnected;
        if Notify then
          MessageToAllForms(WM_CONNECTION_ESTABLISHED, 0, 0);
      end;
    1:
      begin
        // Don't see the server. Notify application if this happend first time since last check.
        case FLastConnectionState of
          csConnected:
            FLastConnectionState := csUnclear; // One ping went wrong, give it a last chance.
          csUnclear:
            begin
              FConnected := False;
              FLastConnectionState := csDisconnected;
              MessageToAllForms(WM_CONNECTION_LOST, 0, 0);
            end;
        end;
      end;
    2:
      begin
        // Ping could not check because the connection is still in use. Stay in uncertain mode until we know for sure.
        FLastConnectionState := csUnclear;
      end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.SetKeepAlive(const Value: Boolean);

begin
  if FKeepAlive <> Value then
  begin
    FKeepAlive := Value;
    FKeepAliveTimer.Enabled := FKeepAlive;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.SetConnected(const Value: Boolean);

begin
  if FConnected <> Value then
  begin
    if Value then
      Reconnect
    else
      Disconnect(True);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.SetDefaultSchema(const Value: WideString);

var
  error: Integer;

begin
  if FMySQL <> nil then
  begin
    if Value <> '' then
    begin
      Lock.Acquire;
      try
        error := myx_use_schema(FMySQL, Value);
        if (error = 0) then
          FDefaultSchema := myx_get_default_schema(FMySQL)
        else
        begin
          // Server connect might have gone, try automatic reconnection in this case.
          error := myx_mysql_errno(FMySQL);
          if (error = CR_SERVER_GONE_ERROR) or (error = CR_SERVER_LOST) or (error = CR_CONN_HOST_ERROR) then
          begin
            Reconnect(True);

            // Now try again. If this fails again then we show the error.
            error := myx_use_schema(FMySQL, Value);
          end;

          if (error = 0) then
            FDefaultSchema := myx_get_default_schema(FMySQL)
          else
            raise EMyxSQLError.Create(Format(_('The default schema cannot be changed to %s'), [Value]),
              myx_mysql_errno(FMySQL), myx_mysql_error(FMySQL));
        end;
      finally
        Lock.Release;
      end;
    end
    else
      FDefaultSchema := '';

    FUserConnection.schema := Value;

    MessageToAllForms(WM_DefaultSchemaChanged, 0, 0);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.SchemaListChanged;

begin
  MessageToAllForms(WM_SchemaListChanged, 0, 0);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure MySQLQueryPostHook(mysql: Pointer; cdata: Pointer; query: PChar; length: Integer); cdecl;

var
  PSender: TMySQLConn;

begin
  PSender := cdata;

  PSender.FQueryHookMultiReadSync.BeginWrite;
  try
    PSender.FQueryHookSQL := UTF8Decode(query);
  finally
    PSender.FQueryHookMultiReadSync.EndWrite;
  end;

  TThread.Synchronize(nil, PSender.TransactionQueryExecuted);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.SetInTransaction(InTransaction: Boolean);

var
  I: Integer;
  TransactionStarted: Boolean;

begin
  if FInTransaction <> InTransaction then
  begin
    TransactionStarted := ((Not (FInTransaction)) and (InTransaction));

    FInTransaction := InTransaction;

    if Assigned(FTransactionStatusChangeListeners) then
      for I := 0 to FTransactionStatusChangeListeners.Count - 1 do
      begin
        if (TransactionStarted) then
          IMySQLConnTransactionStatusChangeListener(FTransactionStatusChangeListeners[I]).TransactionStarted;
          
        IMySQLConnTransactionStatusChangeListener(FTransactionStatusChangeListeners[I]).TransactionStatusChanged;
      end;

    if FInTransaction then
      myx_mysql_set_query_hooks(FMySQL, nil, @MySQLQueryPostHook, Self)
    else
      myx_mysql_set_query_hooks(FMySQL, nil, nil, nil);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.GetQuoteChar: WideString;

begin
  if (FQuoteChar='') then
    FQuoteChar := Chr(myx_get_mysql_quote_char(FMySQL, 0));

  Result := FQuoteChar;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.InternalStartsTransaction(Lexer: TSQLLexer): Boolean;

// Checks if the current tokens in the Lexer comprise a command that starts a transaction.

begin
  Result := False;
  if Lexer.NextToken = toSymbol then
    if Lexer.TokenSymbolIs('start') then
    begin
      if (Lexer.NextToken = toSymbol) and Lexer.TokenSymbolIs('transaction') then
        Result := True;
    end
    else
      if Lexer.TokenSymbolIs('begin') then
        Result := True;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.Ping: Integer;

// Pings the server to see if it is still alive. If the connection has been dropped an attempt is made to
// reconnect.
// Returns 0 if the server is reachable, 1 if not and 2 if we cannot get exclusive access to the connection
// (perhaps because there is a data fetch thread still running).

begin
  if Assigned(FMySQL) then
  begin
    // If we cannot get the lock then the connection is currently working,
    // so there is no need to ping the server.
    if FFetchDataLock.TryEnter then
    try
      Result := myx_ping_server(FMySQL);
    finally
      FFetchDataLock.Release;
    end
    else
      Result := 2;
  end
  else
    Result := 1;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.GetFetchingData: Boolean;

begin
  Result := FDataBeingFetched <> [];
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.AddTransactionStatusListener(Listener: IMySQLConnTransactionStatusChangeListener);

begin
  if FTransactionStatusChangeListeners = nil then
    FTransactionStatusChangeListeners := TList.Create;
  if FTransactionStatusChangeListeners.IndexOf(Pointer(Listener)) = -1 then
    FTransactionStatusChangeListeners.Add(Pointer(Listener));
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.RemoveTransactionStatusListener(Listener: IMySQLConnTransactionStatusChangeListener);

begin
  if FTransactionStatusChangeListeners <> nil then
  begin
    FTransactionStatusChangeListeners.Remove(Pointer(Listener));

    if FTransactionStatusChangeListeners.Count = 0 then
    begin
      FTransactionStatusChangeListeners.Free;
      FTransactionStatusChangeListeners := nil;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.TransactionQueryExecuted;

var
  I: Integer;

begin
  FQueryHookMultiReadSync.BeginRead;
  try
    if Assigned(FTransactionStatusChangeListeners) then
      for I := 0 to FTransactionStatusChangeListeners.Count - 1 do
        IMySQLConnTransactionStatusChangeListener(
          FTransactionStatusChangeListeners[I]).TransactionQueryExecuted(FQueryHookSQL);
  finally
    FQueryHookMultiReadSync.EndRead;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMySQLConn.UseStatusBar(StatusBar: TTntStatusBar);

begin
  FStatusBar := StatusBar;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMySQLConn.ConnectToEmbedded: Integer;

var
  ConnectionResult: Integer;

begin
  Result := -1;

  FEmbeddedConnection := True;

  if (myx_mysql_embedded_start() <> 0) then
    Exit;

  FMySQL := myx_mysql_embedded_init();
  if (FMySQL = nil) then
    Exit;

  FUserConnection := TMYX_USER_CONNECTION.Create('', '', '', '', 0, '', '', '', MYX_MYSQL_CONN,
    MYX_FAVORITE_USER_CONNECTION);

  //Connect to instance
  ConnectionResult := myx_connect_to_instance(FUserConnection.get_record_pointer, FMySQL);
  if ConnectionResult = 0 then
  begin
    FConnected := True;
    Result := 1;
  end
  else
  begin
    ShowModalDialog(Application.Title + ' ' + _('Error'),
      _('Could not connect to embedded server.') + ' ' + #13#10 + #13#10 +
      Format(_('MySQL Error Number %d' + #13#10 + '%s'),
      [myx_mysql_errno(FMySQL), myx_mysql_error(FMySQL)]), myx_mtError, 'OK');
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

constructor TFetchDataThread.Create(AMySQLConn: TMySQLConn; AKindOfData: TFetchingDataKind;
  AFetchMethod: TThreadExecMethod; ARefreshMethod: TThreadExecMethod; ATarget: TObject;
  AStatusBarText: WideString; AAllowParallelExecution, ShowWaitCursor: Boolean);

begin
  inherited Create(True);

  FConnection := AMySQLConn;
  FKindOfData := AKindOfData;
  FFetchMethod := AFetchMethod;
  FRefreshMethod := ARefreshMethod;
  FTarget := ATarget;
  StatusBarText := AStatusBarText;
  FSynchronizedMethod := nil;
  FAllowParallelExecution := AAllowParallelExecution;
  FShowWaitCursor := ShowWaitCursor;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TFetchDataThread.Execute;

begin
  if not FAllowParallelExecution then
    FConnection.FFetchDataLock.Acquire;

  InterlockedIncrement(DataThreadCount);

  if FShowWaitCursor then
    Screen.Cursor := crAppStart;

  try
    try
      with FConnection.FWorkList.LockList do
      try
        Remove(Self);
        FConnection.FCurrentDataThread := Self;
      finally
        FConnection.FWorkList.UnlockList;
      end;

      if not Terminated and (FStatusBarText <> '') then
        Synchronize(UpdateStatusBar);

      if not Terminated and Assigned(FFetchMethod) then
        FFetchMethod(self);

    except
      on x: EMyxSQLError do
        if not Terminated then
        begin
          FErrorMessage := x.FormattedMessage;
          Synchronize(ShowError);
        end;
      on x: Exception do
        if not Terminated then
        begin
          FErrorMessage := x.Message;
          Synchronize(ShowError);
        end;
    end;
  finally
    try
      with FConnection.FWorkList.LockList do
      try
        FConnection.FCurrentDataThread := nil;
        if Count = 0 then
          FConnection.FDataBeingFetched := []
        else
          FConnection.FDataBeingFetched := FConnection.FDataBeingFetched - [FKindOfData];
      finally
        FConnection.FWorkList.UnlockList;
      end;

      if not FAllowParallelExecution then
        FConnection.FFetchDataLock.Release;

      StatusBarText := '';
      if not Terminated then
        Synchronize(UpdateStatusBar);

      if DataThreadCount > 0 then
        if (InterlockedDecrement(DataThreadCount) = 0) and FShowWaitCursor then
          Screen.Cursor := crDefault;

      if not Terminated and Assigned(FRefreshMethod) then
        ExecuteSynchronized(FRefreshMethod);
    except
      // Capture clean-up exceptions too. Don't let exceptions escape the thread context.
      // However, if there was already an exception then don't overwrite their error message.
      on x: EMyxSQLError do
        if not Terminated and (FErrorMessage = '') then
        begin
          FErrorMessage := x.FormattedMessage;
          Synchronize(ShowError);
        end;
      on x: Exception do
        if not Terminated and (FErrorMessage = '') then
        begin
          FErrorMessage := x.Message;
          Synchronize(ShowError);
        end;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TFetchDataThread.UpdateStatusBar;

begin
  if Assigned(FConnection.FStatusBar) and not Application.Terminated then
  begin
    if FConnection.FStatusBar.Panels.Count > 1 then
      FConnection.FStatusBar.Panels[1].Text := FStatusBarText;
    FConnection.FStatusBar.Repaint;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TFetchDataThread.ShowError;

begin
  ShowModalDialog(Application.Title + ' ' + _('Error'), FErrorMessage, myx_mtError, _('OK'));
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TFetchDataThread.ExecuteSynchronized(SynchronizedMethod: TThreadExecMethod);

begin
  self.FSynchronizedMethod := SynchronizedMethod;

  Synchronize(ExecuteSynchronizedMethod);
 end;

//----------------------------------------------------------------------------------------------------------------------

procedure TFetchDataThread.ExecuteSynchronizedMethod;

begin
  if Assigned(FSynchronizedMethod) then
    FSynchronizedMethod(self);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TFetchDataThread.SetStatusBar(StatusBarText: WideString);

begin
  FStatusBarText := StatusBarText;

  UpdateStatusBar;
end;

//----------------- TConnectionLock ------------------------------------------------------------------------------------

function TConnectionLock.TryEnter: Boolean;

begin
  Result := TryEnterCriticalSection(FSection);
end;

//----------------------------------------------------------------------------------------------------------------------

end.