mysqldb.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. unit MySQLDB;
  2. {$H+}
  3. interface
  4. uses
  5. SysUtils, Classes, db, mysql,mysql_com;
  6. type
  7. PMySQLDatasetBookmark = ^TMySQLDatasetBookmark;
  8. TMySQLDatasetBookmark = record
  9. BookmarkData: Integer;
  10. BookmarkFlag: TBookmarkFlag;
  11. end;
  12. Pinteger = ^Integer;
  13. TMySQLDataset = class(TDataSet)
  14. private
  15. FSQL: TStrings;
  16. FDatabase: string;
  17. FHost: string;
  18. FPort: Integer;
  19. FUser: string;
  20. FPassword: string;
  21. FRecordSize: Integer;
  22. FBufferSize: Integer;
  23. // MySQL data
  24. FMYSQL: PMYSQL;
  25. FMYSQLRES: PMYSQL_RES;
  26. FCurrentRecord: Integer; { Record pointer }
  27. FServerInfo: string;
  28. FHostInfo: string;
  29. FAffectedRows: Integer;
  30. FLastInsertID: Integer;
  31. FLoadingFieldDefs: Boolean;
  32. procedure DoOpen;
  33. procedure DoClose;
  34. procedure DoQuery;
  35. procedure DoGetResult;
  36. procedure CalculateSizes;
  37. procedure LoadBufferFromData(Buffer: PChar);
  38. function GetServerStatus: string;
  39. protected
  40. procedure SetDatabase(const Value: string);
  41. procedure SetSQL(const Value: TStrings);
  42. function GetClientInfo: string;
  43. function InternalStrToFloat(S: string): Extended;
  44. function InternalStrToDate(S: string): TDateTime;
  45. function InternalStrToTime(S: string): TDateTime;
  46. function InternalStrToDateTime(S: string): TDateTime;
  47. function InternalStrToTimeStamp(S: string): TDateTime;
  48. function MySQLFieldToFieldType(AType: enum_field_types; ASize: Integer;
  49. var NewType: TFieldType; var NewSize: Integer): Boolean;
  50. function MySQLDataSize(AType: enum_field_types; ASize: Integer): Integer;
  51. function MySQLWriteFieldData(AType: enum_field_types; ASize: Integer; Source: PChar;
  52. Dest: PChar): Integer;
  53. function GetCanModify: Boolean; override;
  54. { Mandatory overrides }
  55. // Record buffer methods:
  56. function AllocRecordBuffer: PChar; override;
  57. procedure FreeRecordBuffer(var Buffer: PChar); override;
  58. procedure InternalInitRecord(Buffer: PChar); override;
  59. function GetRecord(Buffer: PChar; GetMode: TGetMode; DoCheck: Boolean): TGetResult; override;
  60. function GetRecordSize: Word; override;
  61. procedure SetFieldData(Field: TField; Buffer: Pointer); override;
  62. // Bookmark methods:
  63. procedure GetBookmarkData(Buffer: PChar; Data: Pointer); override;
  64. function GetBookmarkFlag(Buffer: PChar): TBookmarkFlag; override;
  65. procedure InternalGotoBookmark(ABookmark: Pointer); override;
  66. procedure InternalSetToRecord(Buffer: PChar); override;
  67. procedure SetBookmarkFlag(Buffer: PChar; Value: TBookmarkFlag); override;
  68. procedure SetBookmarkData(Buffer: PChar; Data: Pointer); override;
  69. // Navigational methods:
  70. procedure InternalFirst; override;
  71. procedure InternalLast; override;
  72. // Editing methods:
  73. procedure InternalAddRecord(Buffer: Pointer; DoAppend: Boolean); override;
  74. procedure InternalDelete; override;
  75. procedure InternalPost; override;
  76. // Misc methods:
  77. procedure InternalClose; override;
  78. procedure InternalHandleException; override;
  79. procedure InternalInitFieldDefs; override;
  80. procedure InternalOpen; override;
  81. function IsCursorOpen: Boolean; override;
  82. { Optional overrides }
  83. function GetRecordCount: Integer; override;
  84. function GetRecNo: Integer; override;
  85. procedure SetRecNo(Value: Integer); override;
  86. public
  87. constructor Create(AOwner: TComponent); override;
  88. destructor Destroy; override;
  89. procedure ExecSQL;
  90. // TDataset method
  91. function GetFieldData(Field: TField; Buffer: Pointer): Boolean; override;
  92. property ServerInfo: string read FServerInfo;
  93. property ClientInfo: string read GetClientInfo;
  94. property HostInfo: string read FHostInfo;
  95. property AffectedRows: Integer read FAffectedRows;
  96. property LastInsertID: Integer read FLastInsertID;
  97. property ServerStatus: string read GetServerStatus;
  98. published
  99. property Active;
  100. property SQL: TStrings read FSQL write SetSQL;
  101. property Database: string read FDatabase write SetDatabase;
  102. property Host: string read FHost write FHost;
  103. property Port: Integer read FPort write FPort;
  104. property User: string read FUser write FUser;
  105. property Password: string read FPassword write FPassword;
  106. property BeforeOpen;
  107. property AfterOpen;
  108. property BeforeClose;
  109. property AfterClose;
  110. property BeforeInsert;
  111. property AfterInsert;
  112. property BeforeEdit;
  113. property AfterEdit;
  114. property BeforePost;
  115. property AfterPost;
  116. property BeforeCancel;
  117. property AfterCancel;
  118. property BeforeDelete;
  119. property AfterDelete;
  120. property BeforeScroll;
  121. property AfterScroll;
  122. property OnDeleteError;
  123. property OnEditError;
  124. end;
  125. implementation
  126. { TMySQLDataset }
  127. constructor TMySQLDataset.Create(AOwner: TComponent);
  128. begin
  129. inherited Create(AOwner);
  130. FSQL := TStringList.Create;
  131. FHost := '';
  132. FPort := 0;
  133. FUser := '';
  134. FPassword := '';
  135. FBufferSize := 0;
  136. FRecordSize := 0;
  137. FCurrentRecord := -1;
  138. FLoadingFieldDefs := False;
  139. FAffectedRows := 0;
  140. FLastInsertID := -1;
  141. FMYSQL := nil;
  142. FMYSQLRES := nil;
  143. end;
  144. destructor TMySQLDataset.Destroy;
  145. begin
  146. FSQL.Free;
  147. inherited destroy;
  148. end;
  149. function TMySQLDataset.AllocRecordBuffer: PChar;
  150. begin
  151. Result := AllocMem(FBufferSize);
  152. end;
  153. procedure TMySQLDataset.FreeRecordBuffer(var Buffer: PChar);
  154. begin
  155. FreeMem(Buffer);
  156. end;
  157. procedure TMySQLDataset.GetBookmarkData(Buffer: PChar; Data: Pointer);
  158. begin
  159. PInteger(Data)^ := PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkData;
  160. end;
  161. function TMySQLDataset.GetBookmarkFlag(Buffer: PChar): TBookmarkFlag;
  162. begin
  163. Result := PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkFlag;
  164. end;
  165. function TMySQLDataset.GetFieldData(Field: TField; Buffer: Pointer): Boolean;
  166. var
  167. I, FC: Integer;
  168. fld: TMYSQL_FIELD;
  169. CurBuf: PChar;
  170. begin
  171. Result := False;
  172. CurBuf := ActiveBuffer;
  173. FC := mysql_num_fields(FMYSQLRES);
  174. for I := 0 to FC-1 do
  175. begin
  176. fld := mysql_fetch_field_direct(FMYSQLRES, I);
  177. //if Field.FieldNo = I+1 then
  178. if Field.FieldName = fld.name then
  179. begin
  180. Move(CurBuf^, PChar(Buffer)^, MySQLDataSize(fld.ftype, fld.length));
  181. if Field.DataType in [ftString{, ftWideString}] then
  182. Result := PChar(buffer)^ <> #0
  183. else
  184. Result := True;
  185. break;
  186. end
  187. else
  188. Inc(CurBuf, MySQLDataSize(fld.ftype, fld.length));
  189. end;
  190. end;
  191. function TMySQLDataset.GetRecNo: Integer;
  192. begin
  193. UpdateCursorPos;
  194. if (FCurrentRecord = -1) and (RecordCount > 0) then
  195. Result := 1
  196. else
  197. Result := FCurrentRecord + 1;
  198. end;
  199. function TMySQLDataset.GetRecord(Buffer: PChar; GetMode: TGetMode;
  200. DoCheck: Boolean): TGetResult;
  201. begin
  202. if RecordCount < 1 then
  203. Result := grEOF
  204. else
  205. begin
  206. Result := grOk;
  207. case GetMode of
  208. gmPrior:
  209. if FCurrentRecord <= 0 then
  210. begin
  211. Result := grBOF;
  212. FCurrentRecord := -1;
  213. end
  214. else
  215. Dec(FCurrentRecord);
  216. gmCurrent:
  217. if (FCurrentRecord < 0) or (FCurrentRecord >= RecordCount) then
  218. Result := grError;
  219. gmNext:
  220. if FCurrentRecord >= RecordCount-1 then
  221. Result := grEOF
  222. else
  223. Inc(FCurrentRecord);
  224. end;
  225. if Result = grOK then
  226. begin
  227. LoadBufferFromData(Buffer);
  228. with PMySQLDatasetBookmark(Buffer + FRecordSize)^ do
  229. begin
  230. BookmarkData := FCurrentRecord;
  231. BookmarkFlag := bfCurrent;
  232. end;
  233. end
  234. else
  235. if (Result = grError) and (DoCheck) then
  236. DatabaseError('No record');
  237. end;
  238. end;
  239. function TMySQLDataset.GetRecordCount: Integer;
  240. begin
  241. Result := mysql_num_rows(FMYSQLRES);
  242. end;
  243. function TMySQLDataset.GetRecordSize: Word;
  244. begin
  245. Result := FRecordSize;
  246. end;
  247. procedure TMySQLDataset.InternalAddRecord(Buffer: Pointer; DoAppend: Boolean);
  248. begin
  249. end;
  250. procedure TMySQLDataset.InternalClose;
  251. begin
  252. FCurrentRecord := -1;
  253. DoClose;
  254. if DefaultFields then
  255. DestroyFields;
  256. end;
  257. procedure TMySQLDataset.InternalDelete;
  258. begin
  259. end;
  260. procedure TMySQLDataset.InternalFirst;
  261. begin
  262. FCurrentRecord := -1;
  263. end;
  264. procedure TMySQLDataset.InternalGotoBookmark(ABookmark: Pointer);
  265. begin
  266. FCurrentRecord := PInteger(ABookmark)^;
  267. end;
  268. procedure TMySQLDataset.InternalHandleException;
  269. begin
  270. // Application.HandleException(self);
  271. end;
  272. procedure TMySQLDataset.InternalInitFieldDefs;
  273. var
  274. I, FC: Integer;
  275. field: TMYSQL_FIELD;
  276. DFT: TFieldType;
  277. DFS: Integer;
  278. WasClosed: Boolean;
  279. begin
  280. if FLoadingFieldDefs then Exit;
  281. FLoadingFieldDefs := True;
  282. try
  283. WasClosed := not IsCursorOpen;
  284. if WasClosed then
  285. begin
  286. DoOpen;
  287. DoQuery;
  288. DoGetResult;
  289. end;
  290. try
  291. FieldDefs.Clear;
  292. FC := mysql_num_fields(FMYSQLRES);
  293. for I := 0 to FC-1 do
  294. begin
  295. field := mysql_fetch_field_direct(FMYSQLRES, I);
  296. if MySQLFieldToFieldType(field.ftype, field.length, DFT, DFS) then
  297. TFieldDef.Create(FieldDefs, field.name, DFT, DFS, False, I+1);
  298. end;
  299. finally
  300. if WasClosed then
  301. begin
  302. DoClose;
  303. end;
  304. end;
  305. finally
  306. FLoadingFieldDefs := False;
  307. end;
  308. end;
  309. procedure TMySQLDataset.InternalInitRecord(Buffer: PChar);
  310. begin
  311. FillChar(Buffer^, FBufferSize, 0);
  312. end;
  313. procedure TMySQLDataset.InternalLast;
  314. begin
  315. FCurrentRecord := RecordCount;
  316. end;
  317. procedure TMySQLDataset.InternalOpen;
  318. begin
  319. FMYSQL := nil;
  320. FMYSQLRES := nil;
  321. try
  322. DoOpen;
  323. DoQuery;
  324. DoGetResult;
  325. FCurrentRecord := -1;
  326. InternalInitFieldDefs;
  327. if DefaultFields then
  328. CreateFields;
  329. CalculateSizes;
  330. BindFields(True);
  331. except
  332. DoClose;
  333. FMYSQL := nil;
  334. FMYSQLRES := nil;
  335. raise;
  336. end;
  337. FServerInfo := mysql_get_server_info(FMYSQL);
  338. FHostInfo := mysql_get_host_info(FMYSQL);
  339. BookMarkSize:=SizeOf(Longint);
  340. end;
  341. procedure TMySQLDataset.InternalSetToRecord(Buffer: PChar);
  342. begin
  343. FCurrentRecord := PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkData;
  344. end;
  345. function TMySQLDataset.IsCursorOpen: Boolean;
  346. begin
  347. Result := FMYSQL <> nil;
  348. end;
  349. procedure TMySQLDataset.SetBookmarkData(Buffer: PChar; Data: Pointer);
  350. begin
  351. PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkData := PInteger(Data)^;
  352. end;
  353. procedure TMySQLDataset.SetBookmarkFlag(Buffer: PChar;
  354. Value: TBookmarkFlag);
  355. begin
  356. PMySQLDatasetBookmark(Buffer + FRecordSize)^.BookmarkFlag := Value;
  357. end;
  358. procedure TMySQLDataset.SetFieldData(Field: TField; Buffer: Pointer);
  359. begin
  360. end;
  361. procedure TMySQLDataset.SetRecNo(Value: Integer);
  362. begin
  363. if (Value >= 0) and (Value <= RecordCount-1) then
  364. begin
  365. FCurrentRecord := Value-1;
  366. Resync([]);
  367. end;
  368. end;
  369. procedure TMySQLDataset.SetSQL(const Value: TStrings);
  370. begin
  371. FSQL.Assign(Value);
  372. FieldDefs.Clear;
  373. end;
  374. procedure TMySQLDataset.ExecSQL;
  375. begin
  376. try
  377. DoOpen;
  378. try
  379. DoQuery;
  380. finally
  381. DoClose;
  382. end;
  383. finally
  384. FMYSQLRES := nil;
  385. FMYSQL := nil;
  386. end;
  387. end;
  388. procedure TMySQLDataset.SetDatabase(const Value: string);
  389. begin
  390. FDatabase := Value;
  391. end;
  392. procedure TMySQLDataset.InternalPost;
  393. begin
  394. end;
  395. function TMySQLDataset.GetClientInfo: string;
  396. begin
  397. Result := mysql_get_client_info;
  398. end;
  399. function TMySQLDataset.MySQLFieldToFieldType(AType: enum_field_types; ASize: Integer;
  400. var NewType: TFieldType; var NewSize: Integer): Boolean;
  401. begin
  402. Result := True;
  403. case AType of
  404. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG,
  405. FIELD_TYPE_INT24:
  406. begin
  407. NewType := ftInteger;
  408. NewSize := 0;
  409. end;
  410. FIELD_TYPE_DECIMAL, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  411. begin
  412. NewType := ftFloat;
  413. NewSize := 0;
  414. end;
  415. FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATETIME:
  416. begin
  417. NewType := ftDateTime;
  418. NewSize := 0;
  419. end;
  420. FIELD_TYPE_DATE:
  421. begin
  422. NewType := ftDate;
  423. NewSize := 0;
  424. end;
  425. FIELD_TYPE_TIME:
  426. begin
  427. NewType := ftTime;
  428. NewSize := 0;
  429. end;
  430. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  431. begin
  432. NewType := ftString;
  433. NewSize := ASize;
  434. end;
  435. else
  436. Result := False;
  437. end;
  438. end;
  439. procedure TMySQLDataset.CalculateSizes;
  440. var
  441. I, FC: Integer;
  442. field: TMYSQL_FIELD;
  443. begin
  444. FRecordSize := 0;
  445. FC := mysql_num_fields(FMYSQLRES);
  446. for I := 0 to FC-1 do
  447. begin
  448. field := mysql_fetch_field_direct(FMYSQLRES, I);
  449. FRecordSize := FRecordSize + MySQLDataSize(field.ftype, field.length);
  450. end;
  451. FBufferSize := FRecordSize + SizeOf(TMySQLDatasetBookmark);
  452. end;
  453. procedure TMySQLDataset.LoadBufferFromData(Buffer: PChar);
  454. var
  455. I, FC, CT: Integer;
  456. field: TMYSQL_FIELD;
  457. row: TMYSQL_ROW;
  458. begin
  459. mysql_data_seek(FMYSQLRES, FCurrentRecord);
  460. row := mysql_fetch_row(FMYSQLRES);
  461. if row = nil then
  462. DatabaseError(mysql_error(FMYSQL));
  463. FC := mysql_num_fields(FMYSQLRES);
  464. for I := 0 to FC-1 do
  465. begin
  466. field := mysql_fetch_field_direct(FMYSQLRES, I);
  467. CT := MySQLWriteFieldData(field.ftype, field.length, row^, Buffer);
  468. Inc(Buffer, CT);
  469. Inc(row);
  470. end;
  471. end;
  472. function TMySQLDataset.MySQLDataSize(AType: enum_field_types;
  473. ASize: Integer): Integer;
  474. begin
  475. Result := 0;
  476. case AType of
  477. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG,
  478. FIELD_TYPE_INT24:
  479. begin
  480. Result := SizeOf(Integer);
  481. end;
  482. FIELD_TYPE_DECIMAL, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  483. begin
  484. Result := SizeOf(Double);
  485. end;
  486. FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATE, FIELD_TYPE_TIME, FIELD_TYPE_DATETIME:
  487. begin
  488. Result := SizeOf(TDateTime);
  489. end;
  490. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  491. begin
  492. Result := ASize;
  493. end;
  494. end;
  495. end;
  496. function TMySQLDataset.MySQLWriteFieldData(AType: enum_field_types;
  497. ASize: Integer; Source, Dest: PChar): Integer;
  498. var
  499. VI: Integer;
  500. VF: Double;
  501. VD: TDateTime;
  502. begin
  503. Result := 0;
  504. case AType of
  505. FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG,
  506. FIELD_TYPE_INT24:
  507. begin
  508. Result := SizeOf(Integer);
  509. if Source <> '' then
  510. VI := StrToInt(Source)
  511. else
  512. VI := 0;
  513. Move(VI, Dest^, Result);
  514. end;
  515. FIELD_TYPE_DECIMAL, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
  516. begin
  517. Result := SizeOf(Double);
  518. if Source <> '' then
  519. VF := InternalStrToFloat(Source)
  520. else
  521. VF := 0;
  522. Move(VF, Dest^, Result);
  523. end;
  524. FIELD_TYPE_TIMESTAMP:
  525. begin
  526. Result := SizeOf(TDateTime);
  527. if Source <> '' then
  528. VD := InternalStrToTimeStamp(Source)
  529. else
  530. VD := 0;
  531. Move(VD, Dest^, Result);
  532. end;
  533. FIELD_TYPE_DATETIME:
  534. begin
  535. Result := SizeOf(TDateTime);
  536. if Source <> '' then
  537. VD := InternalStrToDateTime(Source)
  538. else
  539. VD := 0;
  540. Move(VD, Dest^, Result);
  541. end;
  542. FIELD_TYPE_DATE:
  543. begin
  544. Result := SizeOf(TDateTime);
  545. if Source <> '' then
  546. VD := InternalStrToDate(Source)
  547. else
  548. VD := 0;
  549. Move(VD, Dest^, Result);
  550. end;
  551. FIELD_TYPE_TIME:
  552. begin
  553. Result := SizeOf(TDateTime);
  554. if Source <> '' then
  555. VD := InternalStrToTime(Source)
  556. else
  557. VD := 0;
  558. Move(VD, Dest^, Result);
  559. end;
  560. FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_ENUM, FIELD_TYPE_SET:
  561. begin
  562. Result := ASize;
  563. if Source <> '' then
  564. Move(Source^, Dest^, Result)
  565. else
  566. Dest^ := #0;
  567. end;
  568. end;
  569. end;
  570. function TMySQLDataset.InternalStrToFloat(S: string): Extended;
  571. var
  572. I: Integer;
  573. Tmp: string;
  574. begin
  575. Tmp := '';
  576. for I := 1 to Length(S) do
  577. begin
  578. if not (S[I] in ['0'..'9', '+', '-', 'E', 'e']) then
  579. Tmp := Tmp + DecimalSeparator
  580. else
  581. Tmp := Tmp + S[I];
  582. end;
  583. Result := StrToFloat(Tmp);
  584. end;
  585. function TMySQLDataset.InternalStrToDate(S: string): TDateTime;
  586. var
  587. EY, EM, ED: Word;
  588. begin
  589. EY := StrToInt(Copy(S, 1, 4));
  590. EM := StrToInt(Copy(S, 6, 2));
  591. ED := StrToInt(Copy(S, 9, 2));
  592. if (EY = 0) or (EM = 0) or (ED = 0) then
  593. Result := 0
  594. else
  595. Result := EncodeDate(EY, EM, ED);
  596. end;
  597. function TMySQLDataset.InternalStrToDateTime(S: string): TDateTime;
  598. var
  599. EY, EM, ED: Word;
  600. EH, EN, ES: Word;
  601. begin
  602. EY := StrToInt(Copy(S, 1, 4));
  603. EM := StrToInt(Copy(S, 6, 2));
  604. ED := StrToInt(Copy(S, 9, 2));
  605. EH := StrToInt(Copy(S, 11, 2));
  606. EN := StrToInt(Copy(S, 14, 2));
  607. ES := StrToInt(Copy(S, 17, 2));
  608. if (EY = 0) or (EM = 0) or (ED = 0) then
  609. Result := 0
  610. else
  611. Result := EncodeDate(EY, EM, ED);
  612. Result := Result + EncodeTime(EH, EN, ES, 0);
  613. end;
  614. function TMySQLDataset.InternalStrToTime(S: string): TDateTime;
  615. var
  616. EH, EM, ES: Word;
  617. begin
  618. EH := StrToInt(Copy(S, 1, 2));
  619. EM := StrToInt(Copy(S, 4, 2));
  620. ES := StrToInt(Copy(S, 7, 2));
  621. Result := EncodeTime(EH, EM, ES, 0);
  622. end;
  623. function TMySQLDataset.InternalStrToTimeStamp(S: string): TDateTime;
  624. var
  625. EY, EM, ED: Word;
  626. EH, EN, ES: Word;
  627. begin
  628. EY := StrToInt(Copy(S, 1, 4));
  629. EM := StrToInt(Copy(S, 5, 2));
  630. ED := StrToInt(Copy(S, 7, 2));
  631. EH := StrToInt(Copy(S, 9, 2));
  632. EN := StrToInt(Copy(S, 11, 2));
  633. ES := StrToInt(Copy(S, 13, 2));
  634. if (EY = 0) or (EM = 0) or (ED = 0) then
  635. Result := 0
  636. else
  637. Result := EncodeDate(EY, EM, ED);
  638. Result := Result + EncodeTime(EH, EN, ES, 0);;
  639. end;
  640. procedure TMySQLDataset.DoClose;
  641. begin
  642. try
  643. if FMYSQLRES <> nil then
  644. mysql_free_result(FMYSQLRES);
  645. if FMYSQL <> nil then
  646. mysql_close(FMYSQL);
  647. finally
  648. FMYSQLRES := nil;
  649. FMYSQL := nil;
  650. end;
  651. end;
  652. procedure TMySQLDataset.DoOpen;
  653. begin
  654. FMYSQL := mysql_connect(nil, PChar(FHost), PChar(FUser), PChar(FPassword));
  655. if FMYSQL = nil then
  656. DatabaseError('Error connecting to MySQL server');
  657. if FDatabase <> '' then
  658. if mysql_select_db(FMYSQL, PChar(FDatabase)) <> 0 then
  659. DatabaseError(mysql_error(FMYSQL));
  660. end;
  661. procedure TMySQLDataset.DoQuery;
  662. var
  663. Query: string;
  664. begin
  665. Query := FSQL.GetText;
  666. if mysql_query(FMYSQL, PChar(Query)) <> 0 then
  667. DatabaseError(mysql_error(FMYSQL));
  668. FAffectedRows := mysql_affected_rows(FMYSQL);
  669. FLastInsertID := mysql_insert_id(FMYSQL);
  670. end;
  671. function TMySQLDataset.GetCanModify: Boolean;
  672. begin
  673. Result := False;
  674. end;
  675. procedure TMySQLDataset.DoGetResult;
  676. begin
  677. FMYSQLRES := mysql_store_result(FMYSQL);
  678. if FMYSQLRES = nil then
  679. DatabaseError(mysql_error(FMYSQL));
  680. FAffectedRows := mysql_affected_rows(FMYSQL);
  681. end;
  682. function TMySQLDataset.GetServerStatus: string;
  683. begin
  684. CheckActive;
  685. Result := mysql_stat(FMYSQL);
  686. end;
  687. end.
  688. $Log$
  689. Revision 1.2 2000-07-13 11:32:56 michael
  690. + removed logs
  691. }