mysqlconn.inc 18 KB

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