mysqlconn.inc 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. {$mode objfpc}{$H+}
  2. interface
  3. uses
  4. Classes, SysUtils,bufdataset,sqldb,db,dynlibs,ctypes,
  5. {$IFDEF mysql55}
  6. mysql55dyn;
  7. {$ELSE}
  8. {$IFDEF mysql51}
  9. mysql51dyn;
  10. {$ELSE}
  11. {$IfDef mysql50}
  12. mysql50dyn;
  13. {$ELSE}
  14. {$IfDef mysql41}
  15. mysql41dyn;
  16. {$ELSE}
  17. {$IFDEF mysql4} // temporary backwards compatibility for Lazarus
  18. mysql40dyn;
  19. {$ELSE}
  20. mysql40dyn;
  21. {$EndIf}
  22. {$EndIf}
  23. {$EndIf}
  24. {$endif}
  25. {$endif}
  26. Const
  27. {$IFDEF mysql55}
  28. MySQLVersion = '5.5';
  29. {$else}
  30. {$IFDEF mysql51}
  31. MySQLVersion = '5.1';
  32. {$else}
  33. {$IfDef mysql50}
  34. MySQLVersion = '5.0';
  35. {$ELSE}
  36. {$IfDef mysql41}
  37. MySQLVersion = '4.1';
  38. {$ELSE}
  39. {$IFDEF mysql4} // temporary backwards compatibility for Lazarus
  40. MySQLVersion = '4.0';
  41. {$ELSE}
  42. MySQLVersion = '4.0';
  43. {$EndIf}
  44. {$EndIf}
  45. {$EndIf}
  46. {$endif}
  47. {$endif}
  48. Type
  49. TTransactionName = Class(TSQLHandle)
  50. protected
  51. end;
  52. { TCursorName }
  53. TCursorName = Class(TSQLCursor)
  54. protected
  55. FRes: PMYSQL_RES; { Record pointer }
  56. FNeedData : Boolean;
  57. FStatement : String;
  58. Row : MYSQL_ROW;
  59. Lengths : pculong; { Lengths of the columns of the current row }
  60. RowsAffected : QWord;
  61. LastInsertID : QWord;
  62. ParamBinding : TParamBinding;
  63. ParamReplaceString : String;
  64. MapDSRowToMSQLRow : array of integer;
  65. end;
  66. { TConnectionName }
  67. TConnectionName = class (TSQLConnection)
  68. private
  69. FHostInfo: String;
  70. FServerInfo: String;
  71. FMySQL : PMySQL;
  72. function GetClientInfo: string;
  73. function GetServerStatus: String;
  74. procedure ConnectMySQL(var HMySQL : PMySQL;H,U,P : pchar);
  75. procedure ExecuteDirectMySQL(const query : string);
  76. function EscapeString(const Str : string) : string;
  77. protected
  78. function StrToStatementType(s : string) : TStatementType; override;
  79. Procedure ConnectToServer; virtual;
  80. Procedure SelectDatabase; virtual;
  81. function MySQLDataType(AField: PMYSQL_FIELD; var NewType: TFieldType; var NewSize: Integer): Boolean;
  82. function MySQLWriteData(AField: PMYSQL_FIELD; FieldDef: TFieldDef; Source, Dest: PChar; Len: integer; out CreateBlob : boolean): Boolean;
  83. // SQLConnection methods
  84. procedure DoInternalConnect; override;
  85. procedure DoInternalDisconnect; override;
  86. function GetHandle : pointer; override;
  87. function GetAsSQLText(Field : TField) : string; overload; override;
  88. function GetAsSQLText(Param : TParam) : string; overload; override;
  89. Function AllocateCursorHandle : TSQLCursor; override;
  90. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  91. Function AllocateTransactionHandle : TSQLHandle; override;
  92. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  93. procedure UnPrepareStatement(cursor:TSQLCursor); override;
  94. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  95. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction;AParams : TParams); override;
  96. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
  97. function Fetch(cursor : TSQLCursor) : boolean; override;
  98. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; override;
  99. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor;ATransaction : TSQLTransaction); override;
  100. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  101. function Commit(trans : TSQLHandle) : boolean; override;
  102. function RollBack(trans : TSQLHandle) : boolean; override;
  103. function StartdbTransaction(trans : TSQLHandle; AParams : string) : boolean; override;
  104. procedure CommitRetaining(trans : TSQLHandle); override;
  105. procedure RollBackRetaining(trans : TSQLHandle); override;
  106. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  107. procedure UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string); override;
  108. function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
  109. Public
  110. constructor Create(AOwner : TComponent); override;
  111. procedure GetFieldNames(const TableName : string; List : TStrings); override;
  112. procedure GetTableNames(List : TStrings; SystemTables : Boolean = false); override;
  113. procedure CreateDB; override;
  114. procedure DropDB; override;
  115. Property ServerInfo : String Read FServerInfo;
  116. Property HostInfo : String Read FHostInfo;
  117. property ClientInfo: string read GetClientInfo;
  118. property ServerStatus : String read GetServerStatus;
  119. published
  120. property DatabaseName;
  121. property HostName;
  122. property KeepConnection;
  123. property LoginPrompt;
  124. property Params;
  125. property Port stored false;
  126. property OnLogin;
  127. end;
  128. { TMySQLConnectionDef }
  129. TMySQLConnectionDef = Class(TConnectionDef)
  130. Class Function TypeName : String; override;
  131. Class Function ConnectionClass : TSQLConnectionClass; override;
  132. Class Function Description : String; override;
  133. Class Function DefaultLibraryName : String; override;
  134. Class Function LoadFunction : TLibraryLoadFunction; override;
  135. Class Function UnLoadFunction : TLibraryUnLoadFunction; override;
  136. end;
  137. EMySQLError = Class(Exception);
  138. {$ifdef mysql55}
  139. TMySQL55Connection = Class(TConnectionName);
  140. TMySQL55ConnectionDef = Class(TMySQLConnectionDef);
  141. TMySQL55Transaction = Class(TTransactionName);
  142. TMySQL55Cursor = Class(TCursorName);
  143. {$else}
  144. {$IfDef mysql51}
  145. TMySQL51Connection = Class(TConnectionName);
  146. TMySQL51ConnectionDef = Class(TMySQLConnectionDef);
  147. TMySQL51Transaction = Class(TTransactionName);
  148. TMySQL51Cursor = Class(TCursorName);
  149. {$ELSE}
  150. {$IfDef mysql50}
  151. TMySQL50Connection = Class(TConnectionName);
  152. TMySQL50ConnectionDef = Class(TMySQLConnectionDef);
  153. TMySQL50Transaction = Class(TTransactionName);
  154. TMySQL50Cursor = Class(TCursorName);
  155. {$ELSE}
  156. {$IfDef mysql41}
  157. TMySQL41Connection = Class(TConnectionName);
  158. TMySQL41ConnectionDef = Class(TMySQLConnectionDef);
  159. TMySQL41Transaction = Class(TTransactionName);
  160. TMySQL41Cursor = Class(TCursorName);
  161. {$ELSE}
  162. {$IFDEF mysql4} // temporary backwards compatibility for Lazarus
  163. TMySQLConnection = Class(TConnectionName);
  164. TMySQL40ConnectionDef = Class(TMySQLConnectionDef);
  165. TMySQLTransaction = Class(TTransactionName);
  166. TMySQLCursor = Class(TCursorName);
  167. {$ELSE}
  168. TMySQL40Connection = Class(TConnectionName);
  169. TMySQL40ConnectionDef = Class(TMySQLConnectionDef);
  170. TMySQL40Transaction = Class(TTransactionName);
  171. TMySQL40Cursor = Class(TCursorName);
  172. {$EndIf}
  173. {$EndIf}
  174. {$endif}
  175. {$EndIf}
  176. {$ENDIF}
  177. implementation
  178. uses
  179. dbconst,
  180. strutils,
  181. dateutils,
  182. FmtBCD;
  183. const
  184. Mysql_Option_Names : array[mysql_option] of string = ('MYSQL_OPT_CONNECT_TIMEOUT','MYSQL_OPT_COMPRESS',
  185. 'MYSQL_OPT_NAMED_PIPE','MYSQL_INIT_COMMAND',
  186. 'MYSQL_READ_DEFAULT_FILE','MYSQL_READ_DEFAULT_GROUP',
  187. 'MYSQL_SET_CHARSET_DIR','MYSQL_SET_CHARSET_NAME',
  188. 'MYSQL_OPT_LOCAL_INFILE','MYSQL_OPT_PROTOCOL',
  189. 'MYSQL_SHARED_MEMORY_BASE_NAME','MYSQL_OPT_READ_TIMEOUT',
  190. 'MYSQL_OPT_WRITE_TIMEOUT','MYSQL_OPT_USE_RESULT',
  191. 'MYSQL_OPT_USE_REMOTE_CONNECTION','MYSQL_OPT_USE_EMBEDDED_CONNECTION',
  192. 'MYSQL_OPT_GUESS_CONNECTION','MYSQL_SET_CLIENT_IP',
  193. 'MYSQL_SECURE_AUTH'
  194. {$IFDEF MYSQL50_UP}
  195. ,'MYSQL_REPORT_DATA_TRUNCATION', 'MYSQL_OPT_RECONNECT'
  196. {$IFDEF mysql51_UP}
  197. ,'MYSQL_OPT_SSL_VERIFY_SERVER_CERT'
  198. {$IFDEF mysql55}
  199. ,'MYSQL_PLUGIN_DIR', 'MYSQL_DEFAULT_AUTH'
  200. {$ENDIF}
  201. {$ENDIF}
  202. {$ENDIF}
  203. );
  204. Resourcestring
  205. SErrServerConnectFailed = 'Server connect failed.';
  206. SErrDatabaseSelectFailed = 'failed to select database: %s';
  207. SErrDatabaseCreate = 'Failed to create database: %s';
  208. SErrDatabaseDrop = 'Failed to drop database: %s';
  209. SErrNoData = 'No data for record';
  210. SErrExecuting = 'Error executing query: %s';
  211. SErrFetchingdata = 'Error fetching row data: %s';
  212. SErrGettingResult = 'Error getting result set: %s';
  213. SErrNoQueryResult = 'No result from query.';
  214. SErrVersionMismatch = '%s can not work with the installed MySQL client version: Expected (%s), got (%s).';
  215. SErrSettingParameter = 'Error setting parameter "%s"';
  216. Procedure MySQLError(R : PMySQL;Msg: String;Comp : TComponent);
  217. Var
  218. MySQLMsg : String;
  219. begin
  220. If (R<>Nil) then
  221. begin
  222. MySQLMsg:=Strpas(mysql_error(R));
  223. DatabaseErrorFmt(Msg,[MySQLMsg],Comp);
  224. end
  225. else
  226. DatabaseError(Msg,Comp);
  227. end;
  228. function MysqlOption(const OptionName: string; out AMysql_Option: mysql_option) : boolean;
  229. var AMysql_Option_i: mysql_option;
  230. begin
  231. result := false;
  232. for AMysql_Option_i:=low(AMysql_Option) to high(AMysql_Option) do
  233. if sametext(Mysql_Option_Names[AMysql_Option_i],OptionName) then
  234. begin
  235. result := true;
  236. AMysql_Option:=AMysql_Option_i;
  237. break;
  238. end;
  239. end;
  240. { TConnectionName }
  241. function TConnectionName.StrToStatementType(s : string) : TStatementType;
  242. begin
  243. s:=Lowercase(s);
  244. if (s='analyze') or (s='check') or (s='checksum') or (s='optimize') or (s='repair') or (s='show') then
  245. exit(stSelect)
  246. else if s='call' then
  247. exit(stExecProcedure)
  248. else
  249. Result := inherited StrToStatementType(s);
  250. end;
  251. function TConnectionName.GetClientInfo: string;
  252. begin
  253. // To make it possible to call this if there's no connection yet
  254. InitialiseMysql;
  255. Try
  256. Result:=strpas(mysql_get_client_info());
  257. Finally
  258. ReleaseMysql;
  259. end;
  260. end;
  261. function TConnectionName.GetServerStatus: String;
  262. begin
  263. CheckConnected;
  264. Result := mysql_stat(FMYSQL);
  265. end;
  266. procedure TConnectionName.ConnectMySQL(var HMySQL : PMySQL;H,U,P : pchar);
  267. Var
  268. APort : Cardinal;
  269. i,e: integer;
  270. AMysql_Option: mysql_option;
  271. OptStr: string;
  272. OptInt: cuint;
  273. Opt: pointer;
  274. begin
  275. HMySQL := mysql_init(HMySQL);
  276. APort:=Abs(StrToIntDef(Params.Values['Port'],0));
  277. for i := 0 to Params.Count-1 do
  278. begin
  279. if MysqlOption(params.Names[i],AMysql_Option) then
  280. begin
  281. OptStr:=params.ValueFromIndex[i];
  282. val(OptStr,OptInt,e);
  283. if e=0 then
  284. Opt := @OptInt
  285. else
  286. Opt := pchar(OptStr);
  287. if mysql_options(HMySQL,AMysql_Option,Opt) <> 0 then
  288. MySQLError(HMySQL,Format(SErrSettingParameter,[params.Names[i]]),Self);
  289. end;
  290. end;
  291. HMySQL:=mysql_real_connect(HMySQL,PChar(H),PChar(U),Pchar(P),Nil,APort,Nil,CLIENT_MULTI_RESULTS); //CLIENT_MULTI_RESULTS is required by CALL SQL statement(executes stored procedure), that produces result sets
  292. If (HMySQL=Nil) then
  293. MySQLError(Nil,SErrServerConnectFailed,Self);
  294. // MySQL _Server_ version 4.1 and later
  295. // major_version*10000 + minor_version *100 + sub_version
  296. if (trim(CharSet) <> '') and (4*10000 + 1*100 <= mysql_get_server_version(HMySQL)) then
  297. begin
  298. // Only available for mysql 5.0.7 and later...
  299. // if (mysql_set_character_set(HMySQL, PChar(CharSet)) <> 0) then
  300. if mysql_query(FMySQL,PChar('SET CHARACTER SET ''' + EscapeString(CharSet) +''''))<>0 then
  301. MySQLError(HMySQL,SErrExecuting,Self);
  302. end;
  303. end;
  304. function TConnectionName.GetAsSQLText(Field : TField) : string;
  305. var esc_str : pchar;
  306. begin
  307. if (not assigned(field)) or field.IsNull then Result := 'Null'
  308. else if field.DataType = ftString then
  309. Result := '''' + EscapeString(field.AsString) + ''''
  310. else Result := inherited GetAsSqlText(field);
  311. end;
  312. function TConnectionName.GetAsSQLText(Param: TParam) : string;
  313. var esc_str : pchar;
  314. begin
  315. if (not assigned(param)) or param.IsNull then Result := 'Null'
  316. else if param.DataType in [ftString,ftFixedChar,ftBlob,ftMemo,ftBytes,ftVarBytes] then
  317. Result := '''' + EscapeString(Param.AsString) + ''''
  318. else Result := inherited GetAsSqlText(Param);
  319. end;
  320. procedure TConnectionName.ConnectToServer;
  321. Var
  322. H,U,P : String;
  323. begin
  324. H:=HostName;
  325. U:=UserName;
  326. P:=Password;
  327. ConnectMySQL(FMySQL,pchar(H),pchar(U),pchar(P));
  328. FServerInfo := strpas(mysql_get_server_info(FMYSQL));
  329. FHostInfo := strpas(mysql_get_host_info(FMYSQL));
  330. end;
  331. procedure TConnectionName.SelectDatabase;
  332. begin
  333. if mysql_select_db(FMySQL,pchar(DatabaseName))<>0 then
  334. MySQLError(FMySQL,SErrDatabaseSelectFailed,Self);
  335. end;
  336. procedure TConnectionName.CreateDB;
  337. begin
  338. ExecuteDirectMySQL('CREATE DATABASE ' +DatabaseName);
  339. end;
  340. procedure TConnectionName.DropDB;
  341. begin
  342. ExecuteDirectMySQL('DROP DATABASE ' +DatabaseName);
  343. end;
  344. procedure TConnectionName.ExecuteDirectMySQL(const query : string);
  345. var H,U,P : String;
  346. AMySQL : PMySQL;
  347. begin
  348. CheckDisConnected;
  349. InitialiseMysql;
  350. try
  351. H:=HostName;
  352. U:=UserName;
  353. P:=Password;
  354. AMySQL := nil;
  355. ConnectMySQL(AMySQL,pchar(H),pchar(U),pchar(P));
  356. try
  357. if mysql_query(AMySQL,pchar(query))<>0 then
  358. MySQLError(AMySQL,SErrExecuting,Self);
  359. finally
  360. mysql_close(AMySQL);
  361. end;
  362. finally
  363. ReleaseMysql;
  364. end;
  365. end;
  366. function TConnectionName.EscapeString(const Str: string): string;
  367. var Len : integer;
  368. begin
  369. SetLength(result,length(str)*2+1);
  370. Len := mysql_real_escape_string(FMySQL,pchar(Result),pchar(Str),length(Str));
  371. SetLength(result,Len);
  372. end;
  373. procedure TConnectionName.DoInternalConnect;
  374. var
  375. FullVersion,
  376. ClientVerStr: string;
  377. begin
  378. InitialiseMysql;
  379. Fullversion:=strpas(mysql_get_client_info());
  380. ClientVerStr := copy(FullVersion,1,3);
  381. If (ClientVerStr<>MySQLVersion) then
  382. Raise EInOutError.CreateFmt(SErrVersionMisMatch,[ClassName,MySQLVersion,FullVersion]);
  383. inherited DoInternalConnect;
  384. ConnectToServer;
  385. SelectDatabase;
  386. end;
  387. procedure TConnectionName.DoInternalDisconnect;
  388. begin
  389. inherited DoInternalDisconnect;
  390. mysql_close(FMySQL);
  391. FMySQL:=Nil;
  392. ReleaseMysql;
  393. end;
  394. function TConnectionName.GetHandle: pointer;
  395. begin
  396. Result:=FMySQL;
  397. end;
  398. function TConnectionName.AllocateCursorHandle: TSQLCursor;
  399. begin
  400. {$IfDef mysql55}
  401. Result:=TMySQL55Cursor.Create;
  402. {$ELSE}
  403. {$IfDef mysql51}
  404. Result:=TMySQL51Cursor.Create;
  405. {$ELSE}
  406. {$IfDef mysql50}
  407. Result:=TMySQL50Cursor.Create;
  408. {$ELSE}
  409. {$IfDef mysql41}
  410. Result:=TMySQL41Cursor.Create;
  411. {$ELSE}
  412. {$IFDEF mysql4} // temporary backwards compatibility for Lazarus
  413. Result:=TMySQLCursor.Create;
  414. {$ELSE}
  415. Result:=TMySQL40Cursor.Create;
  416. {$EndIf}
  417. {$EndIf}
  418. {$EndIf}
  419. {$EndIf}
  420. {$endif}
  421. end;
  422. Procedure TConnectionName.DeAllocateCursorHandle(var cursor : TSQLCursor);
  423. begin
  424. FreeAndNil(cursor);
  425. end;
  426. function TConnectionName.AllocateTransactionHandle: TSQLHandle;
  427. begin
  428. // Result:=TTransactionName.Create;
  429. Result := nil;
  430. end;
  431. procedure TConnectionName.PrepareStatement(cursor: TSQLCursor;
  432. ATransaction: TSQLTransaction; buf: string;AParams : TParams);
  433. begin
  434. // if assigned(AParams) and (AParams.count > 0) then
  435. // DatabaseError('Parameters (not) yet supported for the MySQL SqlDB connection.',self);
  436. With Cursor as TCursorName do
  437. begin
  438. FStatement:=Buf;
  439. if assigned(AParams) and (AParams.count > 0) then
  440. FStatement := AParams.ParseSQL(FStatement,false,sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions,psSimulated,paramBinding,ParamReplaceString);
  441. if FStatementType in [stSelect,stExecProcedure] then
  442. FNeedData:=True;
  443. end
  444. end;
  445. procedure TConnectionName.UnPrepareStatement(cursor: TSQLCursor);
  446. Var
  447. C : TCursorName;
  448. begin
  449. C:=Cursor as TCursorName;
  450. if assigned(C.FRes) then //ExecSQL with dataset returned
  451. begin
  452. mysql_free_result(C.FRes);
  453. C.FRes:=nil;
  454. end;
  455. end;
  456. procedure TConnectionName.FreeFldBuffers(cursor: TSQLCursor);
  457. Var
  458. C : TCursorName;
  459. begin
  460. C:=Cursor as TCursorName;
  461. if c.FStatementType in [stSelect,stExecProcedure] then
  462. c.FNeedData:=False;
  463. if assigned(C.FRes) then
  464. begin
  465. mysql_free_result(C.FRes);
  466. C.FRes:=Nil;
  467. end;
  468. SetLength(c.MapDSRowToMSQLRow,0);
  469. inherited;
  470. end;
  471. procedure TConnectionName.Execute(cursor: TSQLCursor;
  472. atransaction: tSQLtransaction;AParams : TParams);
  473. Var
  474. C : TCursorName;
  475. i : integer;
  476. ParamNames,ParamValues : array of string;
  477. Res: PMYSQL_RES;
  478. begin
  479. C:=Cursor as TCursorName;
  480. If (C.FRes=Nil) then
  481. begin
  482. if Assigned(AParams) and (AParams.count > 0) then
  483. begin
  484. setlength(ParamNames,AParams.Count);
  485. setlength(ParamValues,AParams.Count);
  486. for i := 0 to AParams.count -1 do
  487. begin
  488. ParamNames[AParams.count-i-1] := C.ParamReplaceString+inttostr(AParams[i].Index+1);
  489. ParamValues[AParams.count-i-1] := GetAsSQLText(AParams[i]);
  490. end;
  491. // paramreplacestring kan een probleem geven bij postgres als hij niet meer gewoon $ is?
  492. C.FStatement := stringsreplace(C.FStatement,ParamNames,ParamValues,[rfReplaceAll]);
  493. end;
  494. if mysql_query(FMySQL,Pchar(C.FStatement))<>0 then
  495. MySQLError(FMYSQL,SErrExecuting,Self)
  496. else
  497. begin
  498. C.RowsAffected := mysql_affected_rows(FMYSQL);
  499. C.LastInsertID := mysql_insert_id(FMYSQL);
  500. if C.FNeedData then
  501. repeat
  502. Res:=mysql_store_result(FMySQL); //returns a null pointer if the statement didn't return a result set
  503. if Res<>nil then
  504. begin
  505. mysql_free_result(C.FRes);
  506. C.FRes:=Res;
  507. end;
  508. until mysql_next_result(FMySQL)<>0;
  509. end;
  510. end;
  511. end;
  512. function TConnectionName.MySQLDataType(AField: PMYSQL_FIELD; var NewType: TFieldType; var NewSize: Integer): Boolean;
  513. var ASize, ADecimals: integer;
  514. begin
  515. Result := True;
  516. ASize := AField^.length;
  517. NewSize := 0;
  518. case AField^.ftype of
  519. FIELD_TYPE_LONGLONG:
  520. begin
  521. NewType := ftLargeint;
  522. end;
  523. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_YEAR:
  524. begin
  525. NewType := ftSmallint;
  526. end;
  527. FIELD_TYPE_LONG, FIELD_TYPE_INT24:
  528. begin
  529. if AField^.flags and AUTO_INCREMENT_FLAG <> 0 then
  530. NewType := ftAutoInc
  531. else
  532. NewType := ftInteger;
  533. end;
  534. {$ifdef mysql50_up}
  535. FIELD_TYPE_NEWDECIMAL,
  536. {$endif}
  537. FIELD_TYPE_DECIMAL:
  538. begin
  539. ADecimals:=AField^.decimals;
  540. if (ADecimals < 5) and (ASize-2-ADecimals < 15) then //ASize is display size i.e. with sign and decimal point
  541. NewType := ftBCD
  542. else if (ADecimals = 0) and (ASize < 20) then
  543. NewType := ftLargeInt
  544. else
  545. NewType := ftFmtBCD;
  546. NewSize := ADecimals;
  547. end;
  548. FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  549. begin
  550. NewType := ftFloat;
  551. end;
  552. FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATETIME:
  553. begin
  554. NewType := ftDateTime;
  555. end;
  556. FIELD_TYPE_DATE:
  557. begin
  558. NewType := ftDate;
  559. end;
  560. FIELD_TYPE_TIME:
  561. begin
  562. NewType := ftTime;
  563. end;
  564. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  565. begin
  566. // Since mysql server version 5.0.3 string-fields with a length of more
  567. // then 256 characters are suported
  568. if ASize>dsMaxStringSize then
  569. NewType := ftMemo
  570. else
  571. begin
  572. if AField^.ftype = FIELD_TYPE_STRING then
  573. NewType := ftFixedChar
  574. else
  575. NewType := ftString;
  576. {$IFDEF MYSQL50_UP}
  577. if AField^.charsetnr = 63 then //BINARY vs. CHAR, VARBINARY vs. VARCHAR
  578. if NewType = ftFixedChar then
  579. NewType := ftBytes
  580. else
  581. NewType := ftVarBytes;
  582. {$ENDIF}
  583. NewSize := ASize;
  584. end;
  585. end;
  586. FIELD_TYPE_BLOB:
  587. begin
  588. {$IFDEF MYSQL50_UP}
  589. if AField^.charsetnr = 63 then //character set is binary
  590. NewType := ftBlob
  591. else
  592. NewType := ftMemo;
  593. {$ELSE}
  594. NewType := ftBlob;
  595. {$ENDIF}
  596. end;
  597. {$IFDEF MYSQL50_UP}
  598. FIELD_TYPE_BIT:
  599. NewType := ftLargeInt;
  600. {$ENDIF}
  601. else
  602. Result := False;
  603. end;
  604. end;
  605. procedure TConnectionName.AddFieldDefs(cursor: TSQLCursor;
  606. FieldDefs: TfieldDefs);
  607. var
  608. C : TCursorName;
  609. I, TF, FC: Integer;
  610. field: PMYSQL_FIELD;
  611. DFT: TFieldType;
  612. DFS: Integer;
  613. begin
  614. // Writeln('MySQL: Adding fielddefs');
  615. C:=(Cursor as TCursorName);
  616. If (C.FRes=Nil) then
  617. begin
  618. // Writeln('res is nil');
  619. MySQLError(FMySQL,SErrNoQueryResult,Self);
  620. end;
  621. // Writeln('MySQL: have result');
  622. FC:=mysql_num_fields(C.FRes);
  623. SetLength(c.MapDSRowToMSQLRow,FC);
  624. TF := 1;
  625. For I:= 0 to FC-1 do
  626. begin
  627. field := mysql_fetch_field_direct(C.FRES, I);
  628. // Writeln('MySQL: creating fielddef ',I+1);
  629. if MySQLDataType(field, DFT, DFS) then
  630. begin
  631. TFieldDef.Create(FieldDefs, FieldDefs.MakeNameUnique(field^.name), DFT, DFS,
  632. (field^.flags and (AUTO_INCREMENT_FLAG or NOT_NULL_FLAG {$IFDEF MYSQL50_UP}or NO_DEFAULT_VALUE_FLAG{$ENDIF})) = (NOT_NULL_FLAG {$IFDEF MYSQL50_UP}or NO_DEFAULT_VALUE_FLAG{$ENDIF}),
  633. TF);
  634. c.MapDSRowToMSQLRow[TF-1] := I;
  635. inc(TF);
  636. end
  637. end;
  638. // Writeln('MySQL: Finished adding fielddefs');
  639. end;
  640. function TConnectionName.Fetch(cursor: TSQLCursor): boolean;
  641. Var
  642. C : TCursorName;
  643. begin
  644. C:=Cursor as TCursorName;
  645. C.Row:=MySQL_Fetch_row(C.FRes);
  646. Result:=(C.Row<>Nil);
  647. if Result then
  648. C.Lengths := mysql_fetch_lengths(C.FRes)
  649. else
  650. C.Lengths := nil;
  651. end;
  652. function TConnectionName.LoadField(cursor : TSQLCursor;
  653. FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
  654. var
  655. field: PMYSQL_FIELD;
  656. C : TCursorName;
  657. i : integer;
  658. begin
  659. // Writeln('LoadFieldsFromBuffer');
  660. C:=Cursor as TCursorName;
  661. if (C.Row=nil) or (C.Lengths=nil) then
  662. begin
  663. // Writeln('LoadFieldsFromBuffer: row=nil');
  664. MySQLError(FMySQL,SErrFetchingData,Self);
  665. end;
  666. i := c.MapDSRowToMSQLRow[FieldDef.FieldNo-1];
  667. field := mysql_fetch_field_direct(C.FRES, i);
  668. Result := MySQLWriteData(field, FieldDef, C.Row[i], Buffer, C.Lengths[i], CreateBlob);
  669. end;
  670. procedure TConnectionName.LoadBlobIntoBuffer(FieldDef: TFieldDef;
  671. ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction: TSQLTransaction);
  672. var
  673. C : TCursorName;
  674. i : integer;
  675. len : longint;
  676. begin
  677. C:=Cursor as TCursorName;
  678. if (C.Row=nil) or (C.Lengths=nil) then
  679. MySQLError(FMySQL,SErrFetchingData,Self);
  680. i := c.MapDSRowToMSQLRow[FieldDef.FieldNo-1];
  681. len := C.Lengths[i];
  682. ReAllocMem(ABlobBuf^.BlobBuffer^.Buffer, len);
  683. Move(C.Row[i]^, ABlobBuf^.BlobBuffer^.Buffer^, len);
  684. ABlobBuf^.BlobBuffer^.Size := len;
  685. end;
  686. function InternalStrToFloat(S: string): Extended;
  687. var
  688. I: Integer;
  689. Tmp: string;
  690. begin
  691. Tmp := '';
  692. for I := 1 to Length(S) do
  693. begin
  694. if not (S[I] in ['0'..'9', '+', '-', 'E', 'e']) then
  695. Tmp := Tmp + DecimalSeparator
  696. else
  697. Tmp := Tmp + S[I];
  698. end;
  699. Result := StrToFloat(Tmp);
  700. end;
  701. function InternalStrToCurrency(S: string): Extended;
  702. var
  703. I: Integer;
  704. Tmp: string;
  705. begin
  706. Tmp := '';
  707. for I := 1 to Length(S) do
  708. begin
  709. if not (S[I] in ['0'..'9', '+', '-', 'E', 'e']) then
  710. Tmp := Tmp + DecimalSeparator
  711. else
  712. Tmp := Tmp + S[I];
  713. end;
  714. Result := StrToCurr(Tmp);
  715. end;
  716. function InternalStrToDate(S: string): TDateTime;
  717. var
  718. EY, EM, ED: Word;
  719. begin
  720. EY := StrToInt(Copy(S,1,4));
  721. EM := StrToInt(Copy(S,6,2));
  722. ED := StrToInt(Copy(S,9,2));
  723. if (EY = 0) or (EM = 0) or (ED = 0) then
  724. Result:=0
  725. else
  726. Result:=EncodeDate(EY, EM, ED);
  727. end;
  728. function InternalStrToDateTime(S: string): TDateTime;
  729. var
  730. EY, EM, ED: Word;
  731. EH, EN, ES: Word;
  732. begin
  733. EY := StrToInt(Copy(S, 1, 4));
  734. EM := StrToInt(Copy(S, 6, 2));
  735. ED := StrToInt(Copy(S, 9, 2));
  736. EH := StrToInt(Copy(S, 12, 2));
  737. EN := StrToInt(Copy(S, 15, 2));
  738. ES := StrToInt(Copy(S, 18, 2));
  739. if (EY = 0) or (EM = 0) or (ED = 0) then
  740. Result := 0
  741. else
  742. Result := EncodeDate(EY, EM, ED);
  743. Result := ComposeDateTime(Result,EncodeTime(EH, EN, ES, 0));
  744. end;
  745. function InternalStrToTime(S: string): TDateTime;
  746. var
  747. EH, EM, ES: Word;
  748. p: integer;
  749. begin
  750. p := 1;
  751. EH := StrToInt(ExtractSubstr(S, p, [':'])); //hours can be 2 or 3 digits
  752. EM := StrToInt(ExtractSubstr(S, p, [':']));
  753. ES := StrToInt(ExtractSubstr(S, p, ['.']));
  754. Result := EncodeTimeInterval(EH, EM, ES, 0);
  755. end;
  756. function InternalStrToTimeStamp(S: string): TDateTime;
  757. var
  758. EY, EM, ED: Word;
  759. EH, EN, ES: Word;
  760. begin
  761. {$IFNDEF mysql40}
  762. EY := StrToInt(Copy(S, 1, 4));
  763. EM := StrToInt(Copy(S, 6, 2));
  764. ED := StrToInt(Copy(S, 9, 2));
  765. EH := StrToInt(Copy(S, 12, 2));
  766. EN := StrToInt(Copy(S, 15, 2));
  767. ES := StrToInt(Copy(S, 18, 2));
  768. {$ELSE}
  769. EY := StrToInt(Copy(S, 1, 4));
  770. EM := StrToInt(Copy(S, 5, 2));
  771. ED := StrToInt(Copy(S, 7, 2));
  772. EH := StrToInt(Copy(S, 9, 2));
  773. EN := StrToInt(Copy(S, 11, 2));
  774. ES := StrToInt(Copy(S, 13, 2));
  775. {$ENDIF}
  776. if (EY = 0) or (EM = 0) or (ED = 0) then
  777. Result := 0
  778. else
  779. Result := EncodeDate(EY, EM, ED);
  780. Result := Result + EncodeTime(EH, EN, ES, 0);
  781. end;
  782. function TConnectionName.MySQLWriteData(AField: PMYSQL_FIELD; FieldDef: TFieldDef; Source, Dest: PChar; Len: integer; out CreateBlob : boolean): Boolean;
  783. var
  784. VI: Integer;
  785. VL: LargeInt;
  786. VS: Smallint;
  787. VF: Double;
  788. VC: Currency;
  789. VD: TDateTime;
  790. VB: TBCD;
  791. Src : String;
  792. begin
  793. Result := False;
  794. CreateBlob := False;
  795. if Source = Nil then // If the pointer is NULL, the field is NULL
  796. exit;
  797. SetString(Src, Source, Len);
  798. case AField^.ftype of
  799. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_YEAR:
  800. begin
  801. if (Src<>'') then
  802. VS := StrToInt(Src)
  803. else
  804. VS := 0;
  805. Move(VS, Dest^, SizeOf(smallint));
  806. end;
  807. FIELD_TYPE_LONG, FIELD_TYPE_INT24:
  808. begin
  809. if (Src<>'') then
  810. VI := StrToInt(Src)
  811. else
  812. VI := 0;
  813. Move(VI, Dest^, SizeOf(Integer));
  814. end;
  815. FIELD_TYPE_LONGLONG:
  816. begin
  817. if (Src<>'') then
  818. VL := StrToInt64(Src)
  819. else
  820. VL := 0;
  821. Move(VL, Dest^, SizeOf(LargeInt));
  822. end;
  823. {$ifdef mysql50_up}
  824. FIELD_TYPE_NEWDECIMAL,
  825. {$endif}
  826. FIELD_TYPE_DECIMAL, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  827. case FieldDef.DataType of
  828. ftBCD:
  829. begin
  830. VC := InternalStrToCurrency(Src);
  831. Move(VC, Dest^, SizeOf(Currency));
  832. end;
  833. ftFmtBCD:
  834. begin
  835. VB := StrToBCD(Src, FSQLFormatSettings);
  836. Move(VB, Dest^, SizeOf(TBCD));
  837. end
  838. else
  839. begin
  840. if Src <> '' then
  841. VF := InternalStrToFloat(Src)
  842. else
  843. VF := 0;
  844. Move(VF, Dest^, SizeOf(Double));
  845. end;
  846. end;
  847. FIELD_TYPE_TIMESTAMP:
  848. begin
  849. if Src <> '' then
  850. VD := InternalStrToTimeStamp(Src)
  851. else
  852. VD := 0;
  853. Move(VD, Dest^, SizeOf(TDateTime));
  854. end;
  855. FIELD_TYPE_DATETIME:
  856. begin
  857. if Src <> '' then
  858. VD := InternalStrToDateTime(Src)
  859. else
  860. VD := 0;
  861. Move(VD, Dest^, SizeOf(TDateTime));
  862. end;
  863. FIELD_TYPE_DATE:
  864. begin
  865. if Src <> '' then
  866. VD := InternalStrToDate(Src)
  867. else
  868. VD := 0;
  869. Move(VD, Dest^, SizeOf(TDateTime));
  870. end;
  871. FIELD_TYPE_TIME:
  872. begin
  873. if Src <> '' then
  874. VD := InternalStrToTime(Src)
  875. else
  876. VD := 0;
  877. Move(VD, Dest^, SizeOf(TDateTime));
  878. end;
  879. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  880. begin
  881. { Write('Moving string of size ',asize,' : ');
  882. P:=Source;
  883. If (P<>nil) then
  884. While P[0]<>#0 do
  885. begin
  886. Write(p[0]);
  887. inc(p);
  888. end;
  889. Writeln;
  890. }
  891. if Len > FieldDef.Size then
  892. Len := FieldDef.Size;
  893. case FieldDef.DataType of
  894. // String-fields which can contain more then dsMaxStringSize characters
  895. // are mapped to ftBlob fields, while their mysql-datatype is FIELD_TYPE_BLOB
  896. ftBlob, ftMemo:
  897. CreateBlob := True;
  898. ftVarBytes:
  899. begin
  900. PWord(Dest)^ := Len;
  901. Move(Source^, (Dest+sizeof(Word))^, Len);
  902. end;
  903. ftBytes:
  904. Move(Source^, Dest^, Len);
  905. else // ftString, ftFixedChar
  906. begin
  907. Move(Source^, Dest^, Len);
  908. (Dest+Len)^ := #0;
  909. end;
  910. end;
  911. end;
  912. FIELD_TYPE_BLOB:
  913. CreateBlob := True;
  914. {$IFDEF MYSQL50_UP}
  915. FIELD_TYPE_BIT:
  916. begin
  917. VL := 0;
  918. for VI := 0 to Len-1 do
  919. VL := VL * 256 + PByte(Source+VI)^;
  920. move(VL, Dest^, sizeof(LargeInt));
  921. end;
  922. {$ENDIF}
  923. end;
  924. Result := True;
  925. end;
  926. procedure TConnectionName.UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string);
  927. var qry : TSQLQuery;
  928. begin
  929. if not assigned(Transaction) then
  930. DatabaseError(SErrConnTransactionnSet);
  931. qry := tsqlquery.Create(nil);
  932. qry.transaction := Transaction;
  933. qry.database := Self;
  934. with qry do
  935. begin
  936. ParseSQL := False;
  937. sql.clear;
  938. sql.add('show index from ' + TableName);
  939. open;
  940. end;
  941. while not qry.eof do with IndexDefs.AddIndexDef do
  942. begin
  943. Name := trim(qry.fieldbyname('Key_name').asstring);
  944. Fields := trim(qry.fieldbyname('Column_name').asstring);
  945. If Name = 'PRIMARY' then options := options + [ixPrimary];
  946. If qry.fieldbyname('Non_unique').asinteger = 0 then options := options + [ixUnique];
  947. qry.next;
  948. while (name = trim(qry.fieldbyname('Key_name').asstring)) and (not qry.eof) do
  949. begin
  950. Fields := Fields + ';' + trim(qry.fieldbyname('Column_name').asstring);
  951. qry.next;
  952. end;
  953. end;
  954. qry.close;
  955. qry.free;
  956. end;
  957. function TConnectionName.RowsAffected(cursor: TSQLCursor): TRowsCount;
  958. begin
  959. if assigned(cursor) then
  960. // Compile this without range-checking. RowsAffected can be -1, although
  961. // it's an unsigned integer. (small joke from the mysql-guys)
  962. // Without range-checking this goes ok. If Range is turned on, this results
  963. // in range-check errors.
  964. Result := (cursor as TCursorName).RowsAffected
  965. else
  966. Result := -1;
  967. end;
  968. constructor TConnectionName.Create(AOwner: TComponent);
  969. const SingleBackQoutes: TQuoteChars = ('`','`');
  970. begin
  971. inherited Create(AOwner);
  972. FConnOptions := FConnOptions + [sqEscapeRepeat] + [sqEscapeSlash];
  973. FieldNameQuoteChars:=SingleBackQoutes;
  974. FMySQL := Nil;
  975. end;
  976. procedure TConnectionName.GetFieldNames(const TableName: string; List: TStrings);
  977. begin
  978. GetDBInfo(stColumns,TableName,'field',List);
  979. end;
  980. procedure TConnectionName.GetTableNames(List: TStrings; SystemTables: Boolean);
  981. begin
  982. GetDBInfo(stTables,'','tables_in_'+DatabaseName,List)
  983. end;
  984. function TConnectionName.GetTransactionHandle(trans: TSQLHandle): pointer;
  985. begin
  986. Result:=Nil;
  987. end;
  988. function TConnectionName.Commit(trans: TSQLHandle): boolean;
  989. begin
  990. // Do nothing.
  991. end;
  992. function TConnectionName.RollBack(trans: TSQLHandle): boolean;
  993. begin
  994. // Do nothing
  995. end;
  996. function TConnectionName.StartdbTransaction(trans: TSQLHandle; AParams : string): boolean;
  997. begin
  998. // Do nothing
  999. end;
  1000. procedure TConnectionName.CommitRetaining(trans: TSQLHandle);
  1001. begin
  1002. // Do nothing
  1003. end;
  1004. procedure TConnectionName.RollBackRetaining(trans: TSQLHandle);
  1005. begin
  1006. // Do nothing
  1007. end;
  1008. function TConnectionName.GetSchemaInfoSQL(SchemaType: TSchemaType;
  1009. SchemaObjectName, SchemaPattern: string): string;
  1010. begin
  1011. case SchemaType of
  1012. stTables : result := 'show tables';
  1013. stColumns : result := 'show columns from ' + EscapeString(SchemaObjectName);
  1014. else
  1015. DatabaseError(SMetadataUnavailable)
  1016. end; {case}
  1017. end;
  1018. { TMySQLConnectionDef }
  1019. class function TMySQLConnectionDef.TypeName: String;
  1020. begin
  1021. Result:='MySQL '+MySQLVersion;
  1022. end;
  1023. class function TMySQLConnectionDef.ConnectionClass: TSQLConnectionClass;
  1024. begin
  1025. {$IfDef mysql55}
  1026. Result:=TMySQL55Connection;
  1027. {$ELSE}
  1028. {$IfDef mysql51}
  1029. Result:=TMySQL51Connection;
  1030. {$ELSE}
  1031. {$IfDef mysql50}
  1032. Result:=TMySQL50Connection;
  1033. {$ELSE}
  1034. {$IfDef mysql41}
  1035. Result:=TMySQL41Connection;
  1036. {$ELSE}
  1037. {$IFDEF mysql4} // temporary backwards compatibility for Lazarus
  1038. Result:=TMySQLConnection;
  1039. {$ELSE}
  1040. Result:=TMySQL40Connection;
  1041. {$EndIf}
  1042. {$EndIf}
  1043. {$EndIf}
  1044. {$endif}
  1045. {$endif}
  1046. end;
  1047. class function TMySQLConnectionDef.Description: String;
  1048. begin
  1049. Result:='Connect to a MySQL '+MySQLVersion+'database directly via the client library';
  1050. end;
  1051. class function TMySQLConnectionDef.DefaultLibraryName: String;
  1052. begin
  1053. Result:=mysqlvlib;
  1054. end;
  1055. class function TMySQLConnectionDef.LoadFunction: TLibraryLoadFunction;
  1056. begin
  1057. Result:=@initialisemysql;
  1058. end;
  1059. class function TMySQLConnectionDef.UnLoadFunction: TLibraryUnLoadFunction;
  1060. begin
  1061. Result:=@ReleaseMySQL;
  1062. end;
  1063. {$IfDef mysql55}
  1064. initialization
  1065. RegisterConnection(TMySQL55ConnectionDef);
  1066. finalization
  1067. UnRegisterConnection(TMySQL55ConnectionDef);
  1068. {$else}
  1069. {$IfDef mysql51}
  1070. initialization
  1071. RegisterConnection(TMySQL51ConnectionDef);
  1072. finalization
  1073. UnRegisterConnection(TMySQL51ConnectionDef);
  1074. {$ELSE}
  1075. {$IfDef mysql50}
  1076. initialization
  1077. RegisterConnection(TMySQL50ConnectionDef);
  1078. finalization
  1079. UnRegisterConnection(TMySQL50ConnectionDef);
  1080. {$ELSE}
  1081. {$IfDef mysql41}
  1082. initialization
  1083. RegisterConnection(TMySQL41ConnectionDef);
  1084. finalization
  1085. UnRegisterConnection(TMySQL41ConnectionDef);
  1086. {$ELSE}
  1087. {$IFDEF mysql4} // temporary backwards compatibility for Lazarus
  1088. initialization
  1089. RegisterConnection(TMySQL40ConnectionDef);
  1090. finalization
  1091. UnRegisterConnection(TMySQL40ConnectionDef);
  1092. {$ELSE}
  1093. initialization
  1094. RegisterConnection(TMySQL40ConnectionDef);
  1095. finalization
  1096. UnRegisterConnection(TMySQL40ConnectionDef);
  1097. {$EndIf}
  1098. {$EndIf}
  1099. {$EndIf}
  1100. {$ENDIF}
  1101. {$endif}
  1102. end.