mysqldb4.pp 22 KB

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