mysqldb3.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. unit MySQLDB3;
  2. {$H+}
  3. interface
  4. uses
  5. SysUtils, Classes, db, mysql3,mysql3_com;
  6. type
  7. PMySQLDatasetBookmark = ^TMySQLDatasetBookmark;
  8. TMySQLDatasetBookmark = record
  9. BookmarkData: Integer;
  10. BookmarkFlag: TBookmarkFlag;
  11. end;
  12. Pinteger = ^Integer;
  13. TMySQLDatabase = class(TDatabase)
  14. Private
  15. FMYSQL: PMYSQL;
  16. FServerInfo: string;
  17. FHostInfo: string;
  18. function GetHostName: String;
  19. Function GetUserName : String;
  20. procedure SetHostName(const AValue: String);
  21. Procedure SetUserName (Value : String);
  22. Procedure SetPassword (Value : String);
  23. Function GetPassword : String;
  24. Function GetClientInfo : String;
  25. Protected
  26. Procedure ConnectToServer;
  27. Procedure SelectDatabase;
  28. Procedure DoInternalConnect; override;
  29. Procedure DoInternalDisConnect; override;
  30. procedure StartTransaction; override;
  31. procedure EndTransaction; override;
  32. function GetServerStatus: string;
  33. Public
  34. Procedure CreateDatabase;
  35. Procedure DropDatabase;
  36. Property ServerInfo : String Read FServerInfo;
  37. Property HostInfo : String Read FHostInfo;
  38. property ClientInfo: string read GetClientInfo;
  39. property ServerStatus : String read GetServerStatus;
  40. Published
  41. Property UserName : String Read GetUserName Write SetUserName;
  42. Property HostName : String Read GetHostName Write SetHostName;
  43. Property Password : String Read GetPassword Write SetPassword;
  44. end;
  45. TMySQLDataset = class(TDBDataSet)
  46. private
  47. FSQL: TStrings;
  48. FRecordSize: Integer;
  49. FBufferSize: Integer;
  50. // MySQL data
  51. FMYSQLRES: PMYSQL_RES;
  52. FCurrentRecord: Integer; { Record pointer }
  53. FAffectedRows: QWord;
  54. FLastInsertID: Integer;
  55. FLoadingFieldDefs: Boolean;
  56. procedure DoClose;
  57. procedure DoQuery;
  58. procedure DoGetResult;
  59. procedure CalculateSizes;
  60. procedure LoadBufferFromData(Buffer: PChar);
  61. protected
  62. Function FMySQL : PMySQL;
  63. procedure SetSQL(const Value: TStrings);
  64. function InternalStrToFloat(S: string): Extended;
  65. function InternalStrToDate(S: string): TDateTime;
  66. function InternalStrToTime(S: string): TDateTime;
  67. function InternalStrToDateTime(S: string): TDateTime;
  68. function InternalStrToTimeStamp(S: string): TDateTime;
  69. function MySQLFieldToFieldType(AType: enum_field_types; ASize: Integer;
  70. var NewType: TFieldType; var NewSize: Integer): Boolean;
  71. function MySQLDataSize(AType: enum_field_types; ASize: Integer): Integer;
  72. function MySQLWriteFieldData(AType: enum_field_types; ASize: Integer; Source: PChar;
  73. Dest: PChar): Integer;
  74. function GetCanModify: Boolean; override;
  75. { Mandatory overrides }
  76. // Record buffer methods:
  77. function AllocRecordBuffer: PChar; override;
  78. procedure FreeRecordBuffer(var Buffer: PChar); override;
  79. procedure InternalInitRecord(Buffer: PChar); override;
  80. function GetRecord(Buffer: PChar; GetMode: TGetMode; DoCheck: Boolean): TGetResult; override;
  81. function GetRecordSize: Word; override;
  82. procedure SetFieldData(Field: TField; Buffer: Pointer); override;
  83. // Bookmark methods:
  84. procedure GetBookmarkData(Buffer: PChar; Data: Pointer); override;
  85. function GetBookmarkFlag(Buffer: PChar): TBookmarkFlag; override;
  86. procedure InternalGotoBookmark(ABookmark: Pointer); override;
  87. procedure InternalSetToRecord(Buffer: PChar); override;
  88. procedure SetBookmarkFlag(Buffer: PChar; Value: TBookmarkFlag); override;
  89. procedure SetBookmarkData(Buffer: PChar; Data: Pointer); override;
  90. // Navigational methods:
  91. procedure InternalFirst; override;
  92. procedure InternalLast; override;
  93. // Editing methods:
  94. procedure InternalAddRecord(Buffer: Pointer; DoAppend: Boolean); override;
  95. procedure InternalDelete; override;
  96. procedure InternalPost; override;
  97. // Misc methods:
  98. procedure InternalClose; override;
  99. procedure InternalInitFieldDefs; override;
  100. procedure InternalOpen; override;
  101. function IsCursorOpen: Boolean; override;
  102. { Optional overrides }
  103. function GetRecordCount: Integer; override;
  104. function GetRecNo: Integer; override;
  105. procedure SetRecNo(Value: Integer); override;
  106. public
  107. constructor Create(AOwner: TComponent); override;
  108. destructor Destroy; override;
  109. procedure ExecSQL;
  110. // TDataset method
  111. function GetFieldData(Field: TField; Buffer: Pointer): Boolean; override;
  112. function GetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean): Boolean; overload; override;
  113. procedure SetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean); overload; override;
  114. property AffectedRows: QWord read FAffectedRows;
  115. property LastInsertID: Integer read FLastInsertID;
  116. published
  117. property Active;
  118. property Database;
  119. property SQL: TStrings read FSQL write SetSQL;
  120. property BeforeOpen;
  121. property AfterOpen;
  122. property BeforeClose;
  123. property AfterClose;
  124. property BeforeInsert;
  125. property AfterInsert;
  126. property BeforeEdit;
  127. property AfterEdit;
  128. property BeforePost;
  129. property AfterPost;
  130. property BeforeCancel;
  131. property AfterCancel;
  132. property BeforeDelete;
  133. property AfterDelete;
  134. property BeforeScroll;
  135. property AfterScroll;
  136. property OnDeleteError;
  137. property OnEditError;
  138. end;
  139. EMySQLError = Class(Exception);
  140. implementation
  141. Resourcestring
  142. SErrServerConnectFailed = 'Server connect failed.';
  143. SErrDatabaseSelectFailed = 'failed to select database: %s';
  144. SErrDatabaseCreate = 'Failed to create database: %s';
  145. SErrDatabaseDrop = 'Failed to drop database: %s';
  146. SErrNoData = 'No data for record';
  147. SErrExecuting = 'Error executing query: %s';
  148. SErrFetchingdata = 'Error fetching row data: %s';
  149. SErrGettingResult = 'Error getting result set: %s';
  150. Procedure MySQlError(R : PMySQL;Msg: String;Comp : TComponent);
  151. Var
  152. MySQLMsg : String;
  153. begin
  154. If (R<>Nil) then
  155. begin
  156. MySQLMsg:=Strpas(mysql_error(R));
  157. DatabaseErrorFmt(Msg,[MySQLMsg],Comp);
  158. end
  159. else
  160. DatabaseError(Msg,Comp);
  161. end;
  162. { TMySQLDataset }
  163. constructor TMySQLDataset.Create(AOwner: TComponent);
  164. begin
  165. inherited Create(AOwner);
  166. FSQL := TStringList.Create;
  167. FBufferSize := 0;
  168. FRecordSize := 0;
  169. FCurrentRecord := -1;
  170. FLoadingFieldDefs := False;
  171. FAffectedRows := 0;
  172. FLastInsertID := -1;
  173. FMYSQLRES := nil;
  174. end;
  175. destructor TMySQLDataset.Destroy;
  176. begin
  177. Close;
  178. FSQL.Free;
  179. inherited destroy;
  180. end;
  181. function TMySQLDataset.AllocRecordBuffer: PChar;
  182. begin
  183. Result := AllocMem(FBufferSize);
  184. end;
  185. procedure TMySQLDataset.FreeRecordBuffer(var Buffer: PChar);
  186. begin
  187. If (@Buffer<>nil) then
  188. FreeMem(Buffer);
  189. end;
  190. procedure TMySQLDataset.GetBookmarkData(Buffer: PChar; Data: Pointer);
  191. begin
  192. PInteger(Data)^ := PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkData;
  193. end;
  194. function TMySQLDataset.GetBookmarkFlag(Buffer: PChar): TBookmarkFlag;
  195. begin
  196. Result:=PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkFlag;
  197. end;
  198. function TMySQLDataset.GetFieldData(Field: TField; Buffer: Pointer): Boolean;
  199. var
  200. I, FC: Integer;
  201. fld: PMYSQL_FIELD;
  202. CurBuf: PChar;
  203. begin
  204. Result := False;
  205. CurBuf := ActiveBuffer;
  206. FC := mysql_num_fields(FMYSQLRES);
  207. for I := 0 to FC-1 do
  208. begin
  209. fld := mysql_fetch_field_direct(FMYSQLRES, I);
  210. if Field.FieldName = fld^.name then
  211. begin
  212. Move(CurBuf^, PChar(Buffer)^, MySQLDataSize(fld^.ftype, fld^.length));
  213. if Field.DataType in [ftString{, ftWideString}] then
  214. begin
  215. Result := PChar(buffer)^ <> #0;
  216. if Result then
  217. // Terminate string (necessary for enum fields)
  218. PChar(buffer)[fld^.length] := #0;
  219. end
  220. else
  221. Result := True;
  222. break;
  223. end
  224. else
  225. Inc(CurBuf, MySQLDataSize(fld^.ftype, fld^.length));
  226. end;
  227. end;
  228. function TMySQLDataset.GetFieldData(Field: TField; Buffer: Pointer;
  229. NativeFormat: Boolean): Boolean;
  230. begin
  231. Result:=GetFieldData(Field, Buffer);
  232. end;
  233. procedure TMySQLDataset.SetFieldData(Field: TField; Buffer: Pointer;
  234. NativeFormat: Boolean);
  235. begin
  236. SetFieldData(Field, Buffer);
  237. end;
  238. function TMySQLDataset.GetRecNo: Integer;
  239. begin
  240. UpdateCursorPos;
  241. if (FCurrentRecord=-1) and (RecordCount > 0) then
  242. Result:=1
  243. else
  244. Result:=FCurrentRecord+1;
  245. end;
  246. function TMySQLDataset.GetRecord(Buffer: PChar; GetMode: TGetMode;
  247. DoCheck: Boolean): TGetResult;
  248. begin
  249. if RecordCount < 1 then
  250. Result := grEOF
  251. else
  252. begin
  253. Result := grOk;
  254. case GetMode of
  255. gmPrior:
  256. if FCurrentRecord <= 0 then
  257. begin
  258. Result := grBOF;
  259. FCurrentRecord := -1;
  260. end
  261. else
  262. Dec(FCurrentRecord);
  263. gmCurrent:
  264. if (FCurrentRecord<0) or (FCurrentRecord>=RecordCount) then
  265. Result := grError;
  266. gmNext:
  267. if FCurrentRecord>=RecordCount-1 then
  268. Result := grEOF
  269. else
  270. Inc(FCurrentRecord);
  271. end;
  272. if (Result=grOK) then
  273. begin
  274. LoadBufferFromData(Buffer);
  275. with PMySQLDatasetBookmark(Buffer + FRecordSize)^ do
  276. begin
  277. BookmarkData := FCurrentRecord;
  278. BookmarkFlag := bfCurrent;
  279. end;
  280. end
  281. else
  282. if (Result=grError) and (DoCheck) then
  283. DatabaseError(SerrNoData,Self);
  284. end;
  285. end;
  286. function TMySQLDataset.GetRecordCount: Integer;
  287. begin
  288. Result:=mysql_num_rows(FMYSQLRES);
  289. end;
  290. function TMySQLDataset.GetRecordSize: Word;
  291. begin
  292. Result:=FRecordSize;
  293. end;
  294. procedure TMySQLDataset.InternalAddRecord(Buffer: Pointer; DoAppend: Boolean);
  295. begin
  296. end;
  297. procedure TMySQLDataset.InternalClose;
  298. begin
  299. FCurrentRecord := -1;
  300. DoClose;
  301. if DefaultFields then
  302. DestroyFields;
  303. end;
  304. procedure TMySQLDataset.InternalDelete;
  305. begin
  306. end;
  307. procedure TMySQLDataset.InternalFirst;
  308. begin
  309. FCurrentRecord := -1;
  310. end;
  311. procedure TMySQLDataset.InternalGotoBookmark(ABookmark: Pointer);
  312. begin
  313. FCurrentRecord := PInteger(ABookmark)^;
  314. end;
  315. procedure TMySQLDataset.InternalInitFieldDefs;
  316. var
  317. I, FC: Integer;
  318. field: PMYSQL_FIELD;
  319. DFT: TFieldType;
  320. DFS: Integer;
  321. WasClosed: Boolean;
  322. begin
  323. if FLoadingFieldDefs then Exit;
  324. FLoadingFieldDefs := True;
  325. try
  326. WasClosed := not IsCursorOpen;
  327. if WasClosed then
  328. begin
  329. DoQuery;
  330. DoGetResult;
  331. end;
  332. try
  333. FieldDefs.Clear;
  334. FC := mysql_num_fields(FMYSQLRES);
  335. for I := 0 to FC-1 do
  336. begin
  337. field := mysql_fetch_field_direct(FMYSQLRES, I);
  338. if MySQLFieldToFieldType(field^.ftype, field^.length, DFT, DFS) then
  339. TFieldDef.Create(FieldDefs, field^.name, DFT, DFS, False, I+1);
  340. end;
  341. finally
  342. if WasClosed then
  343. DoClose;
  344. end;
  345. finally
  346. FLoadingFieldDefs := False;
  347. end;
  348. end;
  349. procedure TMySQLDataset.InternalInitRecord(Buffer: PChar);
  350. begin
  351. FillChar(Buffer^, FBufferSize, 0);
  352. end;
  353. procedure TMySQLDataset.InternalLast;
  354. begin
  355. FCurrentRecord := RecordCount;
  356. end;
  357. procedure TMySQLDataset.InternalOpen;
  358. begin
  359. CheckDatabase;
  360. FMYSQLRES := nil;
  361. try
  362. DoQuery;
  363. DoGetResult;
  364. FCurrentRecord := -1;
  365. InternalInitFieldDefs;
  366. if DefaultFields then
  367. CreateFields;
  368. CalculateSizes;
  369. BindFields(True);
  370. except
  371. DoClose;
  372. raise;
  373. end;
  374. BookMarkSize:=SizeOf(Longint);
  375. end;
  376. procedure TMySQLDataset.InternalSetToRecord(Buffer: PChar);
  377. begin
  378. FCurrentRecord := PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkData;
  379. end;
  380. function TMySQLDataset.IsCursorOpen: Boolean;
  381. begin
  382. Result:=(FMYSQLRES<>nil);
  383. end;
  384. procedure TMySQLDataset.SetBookmarkData(Buffer: PChar; Data: Pointer);
  385. begin
  386. PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkData := PInteger(Data)^;
  387. end;
  388. procedure TMySQLDataset.SetBookmarkFlag(Buffer: PChar;
  389. Value: TBookmarkFlag);
  390. begin
  391. PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkFlag := Value;
  392. end;
  393. procedure TMySQLDataset.SetFieldData(Field: TField; Buffer: Pointer);
  394. begin
  395. end;
  396. procedure TMySQLDataset.SetRecNo(Value: Integer);
  397. begin
  398. if (Value >= 0) and (Value <= RecordCount-1) then
  399. begin
  400. FCurrentRecord := Value-1;
  401. Resync([]);
  402. end;
  403. end;
  404. procedure TMySQLDataset.SetSQL(const Value: TStrings);
  405. begin
  406. FSQL.Assign(Value);
  407. FieldDefs.Clear;
  408. end;
  409. procedure TMySQLDataset.ExecSQL;
  410. begin
  411. try
  412. DoQuery;
  413. finally
  414. DoClose;
  415. end;
  416. end;
  417. procedure TMySQLDataset.InternalPost;
  418. begin
  419. end;
  420. function TMySQLDataset.MySQLFieldToFieldType(AType: enum_field_types; ASize: Integer;
  421. var NewType: TFieldType; var NewSize: Integer): Boolean;
  422. begin
  423. Result := True;
  424. case AType of
  425. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG,
  426. FIELD_TYPE_INT24:
  427. begin
  428. NewType := ftInteger;
  429. NewSize := 0;
  430. end;
  431. FIELD_TYPE_DECIMAL, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  432. begin
  433. NewType := ftFloat;
  434. NewSize := 0;
  435. end;
  436. FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATETIME:
  437. begin
  438. NewType := ftDateTime;
  439. NewSize := 0;
  440. end;
  441. FIELD_TYPE_DATE:
  442. begin
  443. NewType := ftDate;
  444. NewSize := 0;
  445. end;
  446. FIELD_TYPE_TIME:
  447. begin
  448. NewType := ftTime;
  449. NewSize := 0;
  450. end;
  451. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  452. begin
  453. NewType := ftString;
  454. NewSize := ASize;
  455. end;
  456. else
  457. Result := False;
  458. end;
  459. end;
  460. procedure TMySQLDataset.CalculateSizes;
  461. var
  462. I, FC: Integer;
  463. field: PMYSQL_FIELD;
  464. begin
  465. FRecordSize := 0;
  466. FC := mysql_num_fields(FMYSQLRES);
  467. for I := 0 to FC-1 do
  468. begin
  469. field := mysql_fetch_field_direct(FMYSQLRES, I);
  470. FRecordSize := FRecordSize + MySQLDataSize(field^.ftype, field^.length);
  471. end;
  472. FBufferSize := FRecordSize + SizeOf(TMySQLDatasetBookmark);
  473. end;
  474. procedure TMySQLDataset.LoadBufferFromData(Buffer: PChar);
  475. var
  476. I, FC, CT: Integer;
  477. field: PMYSQL_FIELD;
  478. row: TMYSQL_ROW;
  479. begin
  480. mysql_data_seek(FMYSQLRES, FCurrentRecord);
  481. row := mysql_fetch_row(FMYSQLRES);
  482. if row = nil then
  483. MySQLError(FMySQL,SErrFetchingData,Self);
  484. FC := mysql_num_fields(FMYSQLRES);
  485. for I := 0 to FC-1 do
  486. begin
  487. field := mysql_fetch_field_direct(FMYSQLRES, I);
  488. CT := MySQLWriteFieldData(field^.ftype, field^.length, row^, Buffer);
  489. Inc(Buffer, CT);
  490. Inc(row);
  491. end;
  492. end;
  493. function TMySQLDataset.MySQLDataSize(AType: enum_field_types;
  494. ASize: Integer): Integer;
  495. begin
  496. Result := 0;
  497. case AType of
  498. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG,
  499. FIELD_TYPE_INT24:
  500. begin
  501. Result := SizeOf(Integer);
  502. end;
  503. FIELD_TYPE_DECIMAL, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  504. begin
  505. Result := SizeOf(Double);
  506. end;
  507. FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATE, FIELD_TYPE_TIME, FIELD_TYPE_DATETIME:
  508. begin
  509. Result := SizeOf(TDateTime);
  510. end;
  511. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  512. begin
  513. Result := ASize;
  514. end;
  515. end;
  516. end;
  517. function TMySQLDataset.MySQLWriteFieldData(AType: enum_field_types;
  518. ASize: Integer; Source, Dest: PChar): Integer;
  519. var
  520. VI: Integer;
  521. VF: Double;
  522. VD: TDateTime;
  523. begin
  524. Result := 0;
  525. case AType of
  526. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG,
  527. FIELD_TYPE_INT24:
  528. begin
  529. Result := SizeOf(Integer);
  530. if Source <> '' then
  531. VI := StrToInt(Source)
  532. else
  533. VI := 0;
  534. Move(VI, Dest^, Result);
  535. end;
  536. FIELD_TYPE_DECIMAL, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  537. begin
  538. Result := SizeOf(Double);
  539. if Source <> '' then
  540. VF := InternalStrToFloat(Source)
  541. else
  542. VF := 0;
  543. Move(VF, Dest^, Result);
  544. end;
  545. FIELD_TYPE_TIMESTAMP:
  546. begin
  547. Result := SizeOf(TDateTime);
  548. if Source <> '' then
  549. VD := InternalStrToTimeStamp(Source)
  550. else
  551. VD := 0;
  552. Move(VD, Dest^, Result);
  553. end;
  554. FIELD_TYPE_DATETIME:
  555. begin
  556. Result := SizeOf(TDateTime);
  557. if Source <> '' then
  558. VD := InternalStrToDateTime(Source)
  559. else
  560. VD := 0;
  561. Move(VD, Dest^, Result);
  562. end;
  563. FIELD_TYPE_DATE:
  564. begin
  565. Result := SizeOf(TDateTime);
  566. if Source <> '' then
  567. VD := InternalStrToDate(Source)
  568. else
  569. VD := 0;
  570. Move(VD, Dest^, Result);
  571. end;
  572. FIELD_TYPE_TIME:
  573. begin
  574. Result := SizeOf(TDateTime);
  575. if Source <> '' then
  576. VD := InternalStrToTime(Source)
  577. else
  578. VD := 0;
  579. Move(VD, Dest^, Result);
  580. end;
  581. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  582. begin
  583. Result := ASize;
  584. if Source <> '' then
  585. Move(Source^, Dest^, Result)
  586. else
  587. Dest^ := #0;
  588. end;
  589. end;
  590. end;
  591. function TMySQLDataset.InternalStrToFloat(S: string): Extended;
  592. var
  593. I: Integer;
  594. Tmp: string;
  595. begin
  596. Tmp := '';
  597. for I := 1 to Length(S) do
  598. begin
  599. if not (S[I] in ['0'..'9', '+', '-', 'E', 'e']) then
  600. Tmp := Tmp + DecimalSeparator
  601. else
  602. Tmp := Tmp + S[I];
  603. end;
  604. Result := StrToFloat(Tmp);
  605. end;
  606. function TMySQLDataset.InternalStrToDate(S: string): TDateTime;
  607. var
  608. EY, EM, ED: Word;
  609. begin
  610. EY := StrToInt(Copy(S,1,4));
  611. EM := StrToInt(Copy(S,6,2));
  612. ED := StrToInt(Copy(S,9,2));
  613. if (EY = 0) or (EM = 0) or (ED = 0) then
  614. Result:=0
  615. else
  616. Result:=EncodeDate(EY, EM, ED);
  617. end;
  618. function TMySQLDataset.InternalStrToDateTime(S: string): TDateTime;
  619. var
  620. EY, EM, ED: Word;
  621. EH, EN, ES: Word;
  622. begin
  623. EY := StrToInt(Copy(S, 1, 4));
  624. EM := StrToInt(Copy(S, 6, 2));
  625. ED := StrToInt(Copy(S, 9, 2));
  626. EH := StrToInt(Copy(S, 11, 2));
  627. EN := StrToInt(Copy(S, 14, 2));
  628. ES := StrToInt(Copy(S, 17, 2));
  629. if (EY = 0) or (EM = 0) or (ED = 0) then
  630. Result := 0
  631. else
  632. Result := EncodeDate(EY, EM, ED);
  633. Result := Result + EncodeTime(EH, EN, ES, 0);
  634. end;
  635. function TMySQLDataset.InternalStrToTime(S: string): TDateTime;
  636. var
  637. EH, EM, ES: Word;
  638. begin
  639. EH := StrToInt(Copy(S, 1, 2));
  640. EM := StrToInt(Copy(S, 4, 2));
  641. ES := StrToInt(Copy(S, 7, 2));
  642. Result := EncodeTime(EH, EM, ES, 0);
  643. end;
  644. function TMySQLDataset.InternalStrToTimeStamp(S: string): TDateTime;
  645. var
  646. EY, EM, ED: Word;
  647. EH, EN, ES: Word;
  648. begin
  649. EY := StrToInt(Copy(S, 1, 4));
  650. EM := StrToInt(Copy(S, 5, 2));
  651. ED := StrToInt(Copy(S, 7, 2));
  652. EH := StrToInt(Copy(S, 9, 2));
  653. EN := StrToInt(Copy(S, 11, 2));
  654. ES := StrToInt(Copy(S, 13, 2));
  655. if (EY = 0) or (EM = 0) or (ED = 0) then
  656. Result := 0
  657. else
  658. Result := EncodeDate(EY, EM, ED);
  659. Result := Result + EncodeTime(EH, EN, ES, 0);;
  660. end;
  661. procedure TMySQLDataset.DoClose;
  662. begin
  663. try
  664. if FMYSQLRES <> nil then
  665. mysql_free_result(FMYSQLRES);
  666. finally
  667. FMYSQLRES := nil;
  668. end;
  669. end;
  670. procedure TMySQLDataset.DoQuery;
  671. var
  672. Query: PChar;
  673. begin
  674. Query := FSQL.GetText;
  675. try
  676. if mysql_query(FMySQL,Query) <> 0 then
  677. MySQLError(FMYSQL,SErrExecuting,Self);
  678. finally
  679. StrDispose(Query);
  680. end;
  681. FAffectedRows := mysql_affected_rows(FMYSQL);
  682. FLastInsertID := mysql_insert_id(FMYSQL);
  683. end;
  684. function TMySQLDataset.GetCanModify: Boolean;
  685. begin
  686. Result := False;
  687. end;
  688. procedure TMySQLDataset.DoGetResult;
  689. begin
  690. FMYSQLRES := mysql_store_result(FMYSQL);
  691. if (FMYSQLRES=nil) then
  692. MySQLError(FMYSQL,SErrGettingResult,Self);
  693. FAffectedRows := mysql_affected_rows(FMYSQL);
  694. end;
  695. function TMySQLDataset.FMySQL: PMySQL;
  696. begin
  697. Result:=(Database as TMySQLDatabase).FMySQL;
  698. end;
  699. { TMySQLDatabase }
  700. function TMySQLDatabase.GetUserName: String;
  701. begin
  702. result:=Params.values['UserName'];
  703. end;
  704. function TMySQLDatabase.GetHostName: String;
  705. begin
  706. Result:=Params.Values['HostName'];
  707. end;
  708. procedure TMySQLDatabase.SetHostName(const AValue: String);
  709. begin
  710. Params.Values['HostName']:=AValue;
  711. end;
  712. procedure TMySQLDatabase.SetUserName(Value: String);
  713. begin
  714. Params.Values['UserName']:=Value;
  715. end;
  716. procedure TMySQLDatabase.SetPassword(Value: String);
  717. begin
  718. Params.Values['Password']:=Value;
  719. end;
  720. function TMySQLDatabase.GetPassword: String;
  721. begin
  722. Result:=Params.Values['Password'];
  723. end;
  724. function TMySQLDatabase.GetClientInfo: String;
  725. begin
  726. Result:=strpas(mysql_get_client_info);
  727. end;
  728. procedure TMySQLDatabase.ConnectToServer;
  729. Var
  730. H,U,P : String;
  731. begin
  732. if (FMySQL=Nil) then
  733. New(FMySQL);
  734. H:=HostName;
  735. U:=UserName;
  736. P:=Password;
  737. FMySQL:=mysql_connect(FMySQL,PChar(H),PChar(U),Pchar(P));
  738. If (FMySQL=Nil) then
  739. MySQlError(Nil,SErrServerConnectFailed,Self);
  740. FServerInfo := strpas(mysql_get_server_info(FMYSQL));
  741. FHostInfo := strpas(mysql_get_host_info(FMYSQL));
  742. end;
  743. procedure TMySQLDatabase.SelectDatabase;
  744. begin
  745. if mysql_select_db(FMySQL,pchar(DatabaseName))<>0 then
  746. MySQLError(FMySQL,SErrDatabaseSelectFailed,Self);
  747. end;
  748. procedure TMySQLDatabase.DoInternalConnect;
  749. begin
  750. if (FMySQL<>nil) then
  751. DoInternalDisconnect;
  752. ConnectToServer;
  753. SelectDatabase;
  754. end;
  755. procedure TMySQLDatabase.DoInternalDisConnect;
  756. begin
  757. mysql_close(FMySQL);
  758. FMySQL:=Nil;
  759. FServerInfo:='';
  760. FHostInfo:='';
  761. end;
  762. procedure TMySQLDatabase.StartTransaction;
  763. begin
  764. // Nothing yet
  765. end;
  766. procedure TMySQLDatabase.EndTransaction;
  767. begin
  768. // Nothing yet
  769. end;
  770. procedure TMySQLDatabase.CreateDatabase;
  771. Var
  772. Disconnect : Boolean;
  773. begin
  774. Disconnect:=(FMySQL=Nil);
  775. if Disconnect then
  776. ConnectToServer;
  777. try
  778. if mysql_create_db(FMySQL,Pchar(DatabaseName))<>0 then
  779. MySQLError(FMySQL,SErrDatabaseCreate,Self);
  780. Finally
  781. If Disconnect then
  782. DoInternalDisconnect;
  783. end;
  784. end;
  785. procedure TMySQLDatabase.DropDatabase;
  786. Var
  787. Disconnect : Boolean;
  788. begin
  789. Disconnect:=(FMySQL=Nil);
  790. if Disconnect then
  791. ConnectToServer;
  792. If (FMySQL=Nil) then
  793. ConnectToServer;
  794. try
  795. if mysql_drop_db(FMySQL,Pchar(DatabaseName))<>0 then
  796. MySQLError(FMySQL,SErrDatabaseDrop,Self);
  797. Finally
  798. If Disconnect then
  799. DoInternalDisconnect;
  800. end;
  801. end;
  802. function TMySQLDatabase.GetServerStatus: string;
  803. begin
  804. CheckConnected;
  805. Result := mysql_stat(FMYSQL);
  806. end;
  807. end.