mysqlconn.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. {$mode objfpc}{$H+}
  2. {$MACRO on}
  3. interface
  4. uses
  5. Classes, SysUtils,bufdataset,sqldb,db,dynlibs,
  6. {$IfDef mysql50}
  7. mysql50dyn;
  8. {$DEFINE TConnectionName:=TMySQL50Connection}
  9. {$DEFINE TTransactionName:=TMySQL50Transaction}
  10. {$DEFINE TCursorName:=TMySQL50Cursor}
  11. {$ELSE}
  12. {$IfDef mysql41}
  13. mysql41dyn;
  14. {$DEFINE TConnectionName:=TMySQL41Connection}
  15. {$DEFINE TTransactionName:=TMySQL41Transaction}
  16. {$DEFINE TCursorName:=TMySQL41Cursor}
  17. {$ELSE}
  18. {$IFDEF mysql4} // temporary backwards compatibility for Lazarus
  19. mysql40dyn;
  20. {$DEFINE TConnectionName:=TMySQLConnection}
  21. {$DEFINE TTransactionName:=TMySQLTransaction}
  22. {$DEFINE TCursorName:=TMySQLCursor}
  23. {$ELSE}
  24. mysql40dyn;
  25. {$DEFINE TConnectionName:=TMySQL40Connection}
  26. {$DEFINE TTransactionName:=TMySQL40Transaction}
  27. {$DEFINE TCursorName:=TMySQL40Cursor}
  28. {$EndIf}
  29. {$EndIf}
  30. {$EndIf}
  31. Type
  32. TTransactionName = Class(TSQLHandle)
  33. protected
  34. end;
  35. { TCursorName }
  36. TCursorName = Class(TSQLCursor)
  37. protected
  38. FRes: PMYSQL_RES; { Record pointer }
  39. FNeedData : Boolean;
  40. FStatement : String;
  41. Row : MYSQL_ROW;
  42. RowsAffected : QWord;
  43. LastInsertID : QWord;
  44. ParamBinding : TParamBinding;
  45. ParamReplaceString : String;
  46. MapDSRowToMSQLRow : array of integer;
  47. end;
  48. { TConnectionName }
  49. TConnectionName = class (TSQLConnection)
  50. private
  51. FDialect: integer;
  52. FHostInfo: String;
  53. FServerInfo: String;
  54. FMySQL : PMySQL;
  55. FDidConnect : Boolean;
  56. function GetClientInfo: string;
  57. function GetServerStatus: String;
  58. procedure ConnectMySQL(var HMySQL : PMySQL;H,U,P : pchar);
  59. procedure ExecuteDirectMySQL(const query : string);
  60. protected
  61. function StrToStatementType(s : string) : TStatementType; override;
  62. Procedure ConnectToServer; virtual;
  63. Procedure SelectDatabase; virtual;
  64. function MySQLDataType(AType: enum_field_types; ASize, ADecimals: Integer; var NewType: TFieldType; var NewSize: Integer): Boolean;
  65. function MySQLWriteData(AType: enum_field_types;ASize: Integer; AFieldType: TFieldType; Source, Dest: PChar; out CreateBlob : boolean): Boolean;
  66. // SQLConnection methods
  67. procedure DoInternalConnect; override;
  68. procedure DoInternalDisconnect; override;
  69. function GetHandle : pointer; override;
  70. function GetAsSQLText(Field : TField) : string; overload; override;
  71. function GetAsSQLText(Param : TParam) : string; overload; override;
  72. Function AllocateCursorHandle : TSQLCursor; override;
  73. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  74. Function AllocateTransactionHandle : TSQLHandle; override;
  75. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  76. procedure UnPrepareStatement(cursor:TSQLCursor); override;
  77. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  78. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction;AParams : TParams); override;
  79. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
  80. function Fetch(cursor : TSQLCursor) : boolean; override;
  81. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; override;
  82. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor;ATransaction : TSQLTransaction); override;
  83. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  84. function Commit(trans : TSQLHandle) : boolean; override;
  85. function RollBack(trans : TSQLHandle) : boolean; override;
  86. function StartdbTransaction(trans : TSQLHandle; AParams : string) : boolean; override;
  87. procedure CommitRetaining(trans : TSQLHandle); override;
  88. procedure RollBackRetaining(trans : TSQLHandle); override;
  89. procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
  90. Public
  91. constructor Create(AOwner : TComponent); override;
  92. procedure CreateDB; override;
  93. procedure DropDB; override;
  94. Property ServerInfo : String Read FServerInfo;
  95. Property HostInfo : String Read FHostInfo;
  96. property ClientInfo: string read GetClientInfo;
  97. property ServerStatus : String read GetServerStatus;
  98. published
  99. property Dialect : integer read FDialect write FDialect;
  100. property DatabaseName;
  101. property HostName;
  102. property KeepConnection;
  103. property LoginPrompt;
  104. property Params;
  105. property OnLogin;
  106. end;
  107. EMySQLError = Class(Exception);
  108. implementation
  109. uses dbconst;
  110. { TConnectionName }
  111. Resourcestring
  112. SErrServerConnectFailed = 'Server connect failed.';
  113. SErrDatabaseSelectFailed = 'failed to select database: %s';
  114. SErrDatabaseCreate = 'Failed to create database: %s';
  115. SErrDatabaseDrop = 'Failed to drop database: %s';
  116. SErrNoData = 'No data for record';
  117. SErrExecuting = 'Error executing query: %s';
  118. SErrFetchingdata = 'Error fetching row data: %s';
  119. SErrGettingResult = 'Error getting result set: %s';
  120. SErrNoQueryResult = 'No result from query.';
  121. SErrNotversion50 = 'TMySQL50Connection can not work with the installed MySQL client version (%s).';
  122. SErrNotversion41 = 'TMySQL41Connection can not work with the installed MySQL client version (%s).';
  123. SErrNotversion40 = 'TMySQL40Connection can not work with the installed MySQL client version (%s).';
  124. Procedure MySQlError(R : PMySQL;Msg: String;Comp : TComponent);
  125. Var
  126. MySQLMsg : String;
  127. begin
  128. If (R<>Nil) then
  129. begin
  130. MySQLMsg:=Strpas(mysql_error(R));
  131. DatabaseErrorFmt(Msg,[MySQLMsg],Comp);
  132. end
  133. else
  134. DatabaseError(Msg,Comp);
  135. end;
  136. function TConnectionName.StrToStatementType(s : string) : TStatementType;
  137. begin
  138. S:=Lowercase(s);
  139. if s = 'show' then exit(stSelect);
  140. result := inherited StrToStatementType(s);
  141. end;
  142. function TConnectionName.GetClientInfo: string;
  143. Var
  144. B : Boolean;
  145. begin
  146. // To make it possible to call this if there's no connection yet
  147. B:=(MysqlLibraryHandle=Nilhandle);
  148. If B then
  149. InitialiseMysql;
  150. Try
  151. Result:=strpas(mysql_get_client_info());
  152. Finally
  153. if B then
  154. ReleaseMysql;
  155. end;
  156. end;
  157. function TConnectionName.GetServerStatus: String;
  158. begin
  159. CheckConnected;
  160. Result := mysql_stat(FMYSQL);
  161. end;
  162. procedure TConnectionName.ConnectMySQL(var HMySQL : PMySQL;H,U,P : pchar);
  163. begin
  164. HMySQL := mysql_init(HMySQL);
  165. HMySQL:=mysql_real_connect(HMySQL,PChar(H),PChar(U),Pchar(P),Nil,0,Nil,0);
  166. If (HMySQL=Nil) then
  167. MySQlError(Nil,SErrServerConnectFailed,Self);
  168. end;
  169. function TConnectionName.GetAsSQLText(Field : TField) : string;
  170. var esc_str : pchar;
  171. begin
  172. if (not assigned(field)) or field.IsNull then Result := 'Null'
  173. else if field.DataType = ftString then
  174. begin
  175. Getmem(esc_str,sizeof(field.asstring)*2+1);
  176. mysql_real_escape_string(FMySQL,esc_str,pchar(field.asstring),length(field.asstring));
  177. Result := '''' + esc_str + '''';
  178. Freemem(esc_str);
  179. end
  180. else Result := inherited GetAsSqlText(field);
  181. end;
  182. function TConnectionName.GetAsSQLText(Param: TParam) : string;
  183. var esc_str : pchar;
  184. begin
  185. if (not assigned(param)) or param.IsNull then Result := 'Null'
  186. else if param.DataType in [ftString,ftBlob] then
  187. begin
  188. Getmem(esc_str,length(param.asstring)*2+1);
  189. mysql_real_escape_string(FMySQL,esc_str,pchar(param.asstring),length(param.asstring));
  190. Result := '''' + esc_str + '''';
  191. Freemem(esc_str);
  192. end
  193. else Result := inherited GetAsSqlText(Param);
  194. end;
  195. procedure TConnectionName.ConnectToServer;
  196. Var
  197. H,U,P : String;
  198. begin
  199. H:=HostName;
  200. U:=UserName;
  201. P:=Password;
  202. ConnectMySQL(FMySQL,pchar(H),pchar(U),pchar(P));
  203. FServerInfo := strpas(mysql_get_server_info(FMYSQL));
  204. FHostInfo := strpas(mysql_get_host_info(FMYSQL));
  205. end;
  206. procedure TConnectionName.SelectDatabase;
  207. begin
  208. if mysql_select_db(FMySQL,pchar(DatabaseName))<>0 then
  209. MySQLError(FMySQL,SErrDatabaseSelectFailed,Self);
  210. end;
  211. procedure TConnectionName.CreateDB;
  212. begin
  213. ExecuteDirectMySQL('CREATE DATABASE ' +DatabaseName);
  214. end;
  215. procedure TConnectionName.DropDB;
  216. begin
  217. ExecuteDirectMySQL('DROP DATABASE ' +DatabaseName);
  218. end;
  219. procedure TConnectionName.ExecuteDirectMySQL(const query : string);
  220. var ADidConnect : boolean;
  221. H,U,P : String;
  222. AMySQL : PMySQL;
  223. begin
  224. CheckDisConnected;
  225. ADidConnect:=(MySQLLibraryHandle=NilHandle);
  226. if ADidConnect then
  227. InitialiseMysql;
  228. H:=HostName;
  229. U:=UserName;
  230. P:=Password;
  231. AMySQL := nil;
  232. ConnectMySQL(AMySQL,pchar(H),pchar(U),pchar(P));
  233. if mysql_query(AMySQL,pchar(query))<>0 then
  234. MySQLError(AMySQL,Format(SErrExecuting,[StrPas(mysql_error(AMySQL))]),Self);
  235. mysql_close(AMySQL);
  236. if ADidConnect then
  237. ReleaseMysql;
  238. end;
  239. procedure TConnectionName.DoInternalConnect;
  240. begin
  241. FDidConnect:=(MySQLLibraryHandle=NilHandle);
  242. if FDidConnect then
  243. InitialiseMysql;
  244. {$IFDEF mysql50}
  245. if copy(strpas(mysql_get_client_info()),1,3)<>'5.0' then
  246. Raise EInOutError.CreateFmt(SErrNotversion50,[strpas(mysql_get_client_info())]);
  247. {$ELSE}
  248. {$IFDEF mysql41}
  249. if copy(strpas(mysql_get_client_info()),1,3)<>'4.1' then
  250. Raise EInOutError.CreateFmt(SErrNotversion41,[strpas(mysql_get_client_info())]);
  251. {$ELSE}
  252. if copy(strpas(mysql_get_client_info()),1,3)<>'4.0' then
  253. Raise EInOutError.CreateFmt(SErrNotversion40,[strpas(mysql_get_client_info())]);
  254. {$ENDIF}
  255. {$ENDIF}
  256. inherited DoInternalConnect;
  257. ConnectToServer;
  258. SelectDatabase;
  259. end;
  260. procedure TConnectionName.DoInternalDisconnect;
  261. begin
  262. inherited DoInternalDisconnect;
  263. mysql_close(FMySQL);
  264. FMySQL:=Nil;
  265. if FDidConnect then
  266. ReleaseMysql;
  267. end;
  268. function TConnectionName.GetHandle: pointer;
  269. begin
  270. Result:=FMySQL;
  271. end;
  272. function TConnectionName.AllocateCursorHandle: TSQLCursor;
  273. begin
  274. Result:=TCursorName.Create;
  275. end;
  276. Procedure TConnectionName.DeAllocateCursorHandle(var cursor : TSQLCursor);
  277. begin
  278. FreeAndNil(cursor);
  279. end;
  280. function TConnectionName.AllocateTransactionHandle: TSQLHandle;
  281. begin
  282. // Result:=TTransactionName.Create;
  283. Result := nil;
  284. end;
  285. procedure TConnectionName.PrepareStatement(cursor: TSQLCursor;
  286. ATransaction: TSQLTransaction; buf: string;AParams : TParams);
  287. begin
  288. // if assigned(AParams) and (AParams.count > 0) then
  289. // DatabaseError('Parameters (not) yet supported for the MySQL SqlDB connection.',self);
  290. With Cursor as TCursorName do
  291. begin
  292. FStatement:=Buf;
  293. if assigned(AParams) and (AParams.count > 0) then
  294. FStatement := AParams.ParseSQL(FStatement,false,sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions,psSimulated,paramBinding,ParamReplaceString);
  295. if FStatementType=stSelect then
  296. FNeedData:=True;
  297. end
  298. end;
  299. procedure TConnectionName.UnPrepareStatement(cursor: TSQLCursor);
  300. begin
  301. // do nothing
  302. end;
  303. procedure TConnectionName.FreeFldBuffers(cursor: TSQLCursor);
  304. Var
  305. C : TCursorName;
  306. begin
  307. C:=Cursor as TCursorName;
  308. if c.FStatementType=stSelect then
  309. c.FNeedData:=False;
  310. If (C.FRes<>Nil) then
  311. begin
  312. Mysql_free_result(C.FRes);
  313. C.FRes:=Nil;
  314. end;
  315. SetLength(c.MapDSRowToMSQLRow,0);
  316. inherited;
  317. end;
  318. procedure TConnectionName.Execute(cursor: TSQLCursor;
  319. atransaction: tSQLtransaction;AParams : TParams);
  320. Var
  321. C : TCursorName;
  322. i : integer;
  323. begin
  324. C:=Cursor as TCursorName;
  325. If (C.FRes=Nil) then
  326. begin
  327. if Assigned(AParams) and (AParams.count > 0) then
  328. for i := 0 to AParams.count -1 do
  329. C.FStatement := stringreplace(C.FStatement,C.ParamReplaceString+inttostr(AParams[i].Index+1),GetAsSQLText(AParams[i]),[rfReplaceAll,rfIgnoreCase]);
  330. if mysql_query(FMySQL,Pchar(C.FStatement))<>0 then
  331. MySQLError(FMYSQL,Format(SErrExecuting,[StrPas(mysql_error(FMySQL))]),Self)
  332. else
  333. begin
  334. C.RowsAffected := mysql_affected_rows(FMYSQL);
  335. C.LastInsertID := mysql_insert_id(FMYSQL);
  336. if C.FNeedData then
  337. C.FRes:=mysql_store_result(FMySQL);
  338. end;
  339. end;
  340. end;
  341. function TConnectionName.MySQLDataType(AType: enum_field_types; ASize, ADecimals: Integer;
  342. var NewType: TFieldType; var NewSize: Integer): Boolean;
  343. begin
  344. Result := True;
  345. case AType of
  346. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG,
  347. FIELD_TYPE_INT24:
  348. begin
  349. NewType := ftInteger;
  350. NewSize := 0;
  351. end;
  352. {$ifdef mysql50}
  353. FIELD_TYPE_NEWDECIMAL,
  354. {$endif}
  355. FIELD_TYPE_DECIMAL: if ADecimals < 5 then
  356. begin
  357. NewType := ftBCD;
  358. NewSize := 0;
  359. end
  360. else
  361. begin
  362. NewType := ftFloat;
  363. NewSize := 0;
  364. end;
  365. FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  366. begin
  367. NewType := ftFloat;
  368. NewSize := 0;
  369. end;
  370. FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATETIME:
  371. begin
  372. NewType := ftDateTime;
  373. NewSize := 0;
  374. end;
  375. FIELD_TYPE_DATE:
  376. begin
  377. NewType := ftDate;
  378. NewSize := 0;
  379. end;
  380. FIELD_TYPE_TIME:
  381. begin
  382. NewType := ftTime;
  383. NewSize := 0;
  384. end;
  385. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  386. begin
  387. NewType := ftString;
  388. NewSize := ASize;
  389. end;
  390. FIELD_TYPE_BLOB:
  391. begin
  392. NewType := ftBlob;
  393. NewSize := 0;
  394. end
  395. else
  396. Result := False;
  397. end;
  398. end;
  399. procedure TConnectionName.AddFieldDefs(cursor: TSQLCursor;
  400. FieldDefs: TfieldDefs);
  401. var
  402. C : TCursorName;
  403. I, TF, FC: Integer;
  404. field: PMYSQL_FIELD;
  405. DFT: TFieldType;
  406. DFS: Integer;
  407. begin
  408. // Writeln('MySQL: Adding fielddefs');
  409. C:=(Cursor as TCursorName);
  410. If (C.FRes=Nil) then
  411. begin
  412. // Writeln('res is nil');
  413. MySQLError(FMySQL,SErrNoQueryResult,Self);
  414. end;
  415. // Writeln('MySQL: have result');
  416. FC:=mysql_num_fields(C.FRes);
  417. SetLength(c.MapDSRowToMSQLRow,FC);
  418. TF := 1;
  419. For I:= 0 to FC-1 do
  420. begin
  421. field := mysql_fetch_field_direct(C.FRES, I);
  422. // Writeln('MySQL: creating fielddef ',I+1);
  423. if MySQLDataType(field^.ftype, field^.length, field^.decimals, DFT, DFS) then
  424. begin
  425. TFieldDef.Create(FieldDefs, field^.name, DFT, DFS, False, TF);
  426. c.MapDSRowToMSQLRow[TF-1] := I;
  427. inc(TF);
  428. end
  429. end;
  430. // Writeln('MySQL: Finished adding fielddefs');
  431. end;
  432. function TConnectionName.Fetch(cursor: TSQLCursor): boolean;
  433. Var
  434. C : TCursorName;
  435. begin
  436. C:=Cursor as TCursorName;
  437. C.Row:=MySQL_Fetch_row(C.FRes);
  438. Result:=(C.Row<>Nil);
  439. end;
  440. function TConnectionName.LoadField(cursor : TSQLCursor;
  441. FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
  442. var
  443. field: PMYSQL_FIELD;
  444. row : MYSQL_ROW;
  445. C : TCursorName;
  446. begin
  447. // Writeln('LoadFieldsFromBuffer');
  448. C:=Cursor as TCursorName;
  449. if C.Row=nil then
  450. begin
  451. // Writeln('LoadFieldsFromBuffer: row=nil');
  452. MySQLError(FMySQL,SErrFetchingData,Self);
  453. end;
  454. Row:=C.Row;
  455. inc(Row,c.MapDSRowToMSQLRow[FieldDef.FieldNo-1]);
  456. field := mysql_fetch_field_direct(C.FRES, c.MapDSRowToMSQLRow[FieldDef.FieldNo-1]);
  457. Result := MySQLWriteData(field^.ftype, field^.length, FieldDef.DataType, Row^, Buffer, CreateBlob);
  458. end;
  459. procedure TConnectionName.LoadBlobIntoBuffer(FieldDef: TFieldDef;
  460. ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction: TSQLTransaction);
  461. var
  462. row : MYSQL_ROW;
  463. C : TCursorName;
  464. li : longint;
  465. begin
  466. C:=Cursor as TCursorName;
  467. if C.Row=nil then
  468. MySQLError(FMySQL,SErrFetchingData,Self);
  469. Row:=C.Row;
  470. inc(Row,c.MapDSRowToMSQLRow[FieldDef.FieldNo-1]);
  471. li := strlen(pchar(row^));
  472. ReAllocMem(ABlobBuf^.BlobBuffer^.Buffer,li);
  473. Move(pchar(row^)^, ABlobBuf^.BlobBuffer^.Buffer^, li);
  474. ABlobBuf^.BlobBuffer^.Size := li;
  475. end;
  476. function InternalStrToFloat(S: string): Extended;
  477. var
  478. I: Integer;
  479. Tmp: string;
  480. begin
  481. Tmp := '';
  482. for I := 1 to Length(S) do
  483. begin
  484. if not (S[I] in ['0'..'9', '+', '-', 'E', 'e']) then
  485. Tmp := Tmp + DecimalSeparator
  486. else
  487. Tmp := Tmp + S[I];
  488. end;
  489. Result := StrToFloat(Tmp);
  490. end;
  491. function InternalStrToCurrency(S: string): Extended;
  492. var
  493. I: Integer;
  494. Tmp: string;
  495. begin
  496. Tmp := '';
  497. for I := 1 to Length(S) do
  498. begin
  499. if not (S[I] in ['0'..'9', '+', '-', 'E', 'e']) then
  500. Tmp := Tmp + DecimalSeparator
  501. else
  502. Tmp := Tmp + S[I];
  503. end;
  504. Result := StrToCurr(Tmp);
  505. end;
  506. function InternalStrToDate(S: string): TDateTime;
  507. var
  508. EY, EM, ED: Word;
  509. begin
  510. EY := StrToInt(Copy(S,1,4));
  511. EM := StrToInt(Copy(S,6,2));
  512. ED := StrToInt(Copy(S,9,2));
  513. if (EY = 0) or (EM = 0) or (ED = 0) then
  514. Result:=0
  515. else
  516. Result:=EncodeDate(EY, EM, ED);
  517. end;
  518. function InternalStrToDateTime(S: string): TDateTime;
  519. var
  520. EY, EM, ED: Word;
  521. EH, EN, ES: Word;
  522. begin
  523. EY := StrToInt(Copy(S, 1, 4));
  524. EM := StrToInt(Copy(S, 6, 2));
  525. ED := StrToInt(Copy(S, 9, 2));
  526. EH := StrToInt(Copy(S, 12, 2));
  527. EN := StrToInt(Copy(S, 15, 2));
  528. ES := StrToInt(Copy(S, 18, 2));
  529. if (EY = 0) or (EM = 0) or (ED = 0) then
  530. Result := 0
  531. else
  532. Result := EncodeDate(EY, EM, ED);
  533. Result := Result + EncodeTime(EH, EN, ES, 0);
  534. end;
  535. function InternalStrToTime(S: string): TDateTime;
  536. var
  537. EH, EM, ES: Word;
  538. begin
  539. EH := StrToInt(Copy(S, 1, 2));
  540. EM := StrToInt(Copy(S, 4, 2));
  541. ES := StrToInt(Copy(S, 7, 2));
  542. Result := EncodeTime(EH, EM, ES, 0);
  543. end;
  544. function InternalStrToTimeStamp(S: string): TDateTime;
  545. var
  546. EY, EM, ED: Word;
  547. EH, EN, ES: Word;
  548. begin
  549. {$IFNDEF mysql40}
  550. EY := StrToInt(Copy(S, 1, 4));
  551. EM := StrToInt(Copy(S, 6, 2));
  552. ED := StrToInt(Copy(S, 9, 2));
  553. EH := StrToInt(Copy(S, 12, 2));
  554. EN := StrToInt(Copy(S, 15, 2));
  555. ES := StrToInt(Copy(S, 18, 2));
  556. {$ELSE}
  557. EY := StrToInt(Copy(S, 1, 4));
  558. EM := StrToInt(Copy(S, 5, 2));
  559. ED := StrToInt(Copy(S, 7, 2));
  560. EH := StrToInt(Copy(S, 9, 2));
  561. EN := StrToInt(Copy(S, 11, 2));
  562. ES := StrToInt(Copy(S, 13, 2));
  563. {$ENDIF}
  564. if (EY = 0) or (EM = 0) or (ED = 0) then
  565. Result := 0
  566. else
  567. Result := EncodeDate(EY, EM, ED);
  568. Result := Result + EncodeTime(EH, EN, ES, 0);;
  569. end;
  570. function TConnectionName.MySQLWriteData(AType: enum_field_types;ASize: Integer; AFieldType: TFieldType; Source, Dest: PChar; out CreateBlob : boolean): Boolean;
  571. var
  572. VI: Integer;
  573. VF: Double;
  574. VC: Currency;
  575. VD: TDateTime;
  576. Src : String;
  577. begin
  578. Result := False;
  579. CreateBlob := False;
  580. if Source = Nil then
  581. exit;
  582. Src:=StrPas(Source);
  583. case AType of
  584. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG,
  585. FIELD_TYPE_INT24:
  586. begin
  587. if (Src<>'') then
  588. VI := StrToInt(Src)
  589. else
  590. VI := 0;
  591. Move(VI, Dest^, SizeOf(Integer));
  592. end;
  593. FIELD_TYPE_LONGLONG:
  594. begin
  595. if (Src<>'') then
  596. VI := StrToInt64(Src)
  597. else
  598. VI := 0;
  599. Move(VI, Dest^, SizeOf(LargeInt));
  600. end;
  601. {$ifdef mysql50}
  602. FIELD_TYPE_NEWDECIMAL,
  603. {$endif}
  604. FIELD_TYPE_DECIMAL, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  605. if AFieldType = ftBCD then
  606. begin
  607. VC := InternalStrToCurrency(Src);
  608. Move(VC, Dest^, SizeOf(Currency));
  609. end
  610. else
  611. begin
  612. if Src <> '' then
  613. VF := InternalStrToFloat(Src)
  614. else
  615. VF := 0;
  616. Move(VF, Dest^, SizeOf(Double));
  617. end;
  618. FIELD_TYPE_TIMESTAMP:
  619. begin
  620. if Src <> '' then
  621. VD := InternalStrToTimeStamp(Src)
  622. else
  623. VD := 0;
  624. Move(VD, Dest^, SizeOf(TDateTime));
  625. end;
  626. FIELD_TYPE_DATETIME:
  627. begin
  628. if Src <> '' then
  629. VD := InternalStrToDateTime(Src)
  630. else
  631. VD := 0;
  632. Move(VD, Dest^, SizeOf(TDateTime));
  633. end;
  634. FIELD_TYPE_DATE:
  635. begin
  636. if Src <> '' then
  637. VD := InternalStrToDate(Src)
  638. else
  639. VD := 0;
  640. Move(VD, Dest^, SizeOf(TDateTime));
  641. end;
  642. FIELD_TYPE_TIME:
  643. begin
  644. if Src <> '' then
  645. VD := InternalStrToTime(Src)
  646. else
  647. VD := 0;
  648. Move(VD, Dest^, SizeOf(TDateTime));
  649. end;
  650. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  651. begin
  652. { Write('Moving string of size ',asize,' : ');
  653. P:=Source;
  654. If (P<>nil) then
  655. While P[0]<>#0 do
  656. begin
  657. Write(p[0]);
  658. inc(p);
  659. end;
  660. Writeln;
  661. } if Src<> '' then
  662. Move(Source^, Dest^, ASize)
  663. else
  664. Dest^ := #0;
  665. end;
  666. FIELD_TYPE_BLOB:
  667. CreateBlob := True;
  668. end;
  669. Result := True;
  670. end;
  671. procedure TConnectionName.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  672. var qry : TSQLQuery;
  673. begin
  674. if not assigned(Transaction) then
  675. DatabaseError(SErrConnTransactionnSet);
  676. qry := tsqlquery.Create(nil);
  677. qry.transaction := Transaction;
  678. qry.database := Self;
  679. with qry do
  680. begin
  681. ReadOnly := True;
  682. sql.clear;
  683. sql.add('show index from ' + TableName);
  684. open;
  685. end;
  686. IndexDefs.Clear;
  687. while not qry.eof do with IndexDefs.AddIndexDef do
  688. begin
  689. Name := trim(qry.fieldbyname('Key_name').asstring);
  690. Fields := trim(qry.fieldbyname('Column_name').asstring);
  691. If Name = 'PRIMARY' then options := options + [ixPrimary];
  692. If qry.fieldbyname('Non_unique').asinteger = 0 then options := options + [ixUnique];
  693. qry.next;
  694. { while (name = qry.fields[0].asstring) and (not qry.eof) do
  695. begin
  696. Fields := Fields + ';' + trim(qry.Fields[2].asstring);
  697. qry.next;
  698. end;}
  699. end;
  700. qry.close;
  701. qry.free;
  702. end;
  703. constructor TConnectionName.Create(AOwner: TComponent);
  704. begin
  705. inherited Create(AOwner);
  706. FConnOptions := FConnOptions + [sqEscapeRepeat] + [sqEscapeSlash];
  707. end;
  708. function TConnectionName.GetTransactionHandle(trans: TSQLHandle): pointer;
  709. begin
  710. Result:=Nil;
  711. end;
  712. function TConnectionName.Commit(trans: TSQLHandle): boolean;
  713. begin
  714. // Do nothing.
  715. end;
  716. function TConnectionName.RollBack(trans: TSQLHandle): boolean;
  717. begin
  718. // Do nothing
  719. end;
  720. function TConnectionName.StartdbTransaction(trans: TSQLHandle; AParams : string): boolean;
  721. begin
  722. // Do nothing
  723. end;
  724. procedure TConnectionName.CommitRetaining(trans: TSQLHandle);
  725. begin
  726. // Do nothing
  727. end;
  728. procedure TConnectionName.RollBackRetaining(trans: TSQLHandle);
  729. begin
  730. // Do nothing
  731. end;
  732. end.