mysqlconn.inc 35 KB

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