ibconnection.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. unit IBConnection;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, IBase60, sqldb, db;
  6. type
  7. TAccessMode = (amReadWrite, amReadOnly);
  8. TIsolationLevel = (ilConcurrent, ilConsistent, ilReadCommittedRecV,
  9. ilReadCommitted);
  10. TLockResolution = (lrWait, lrNoWait);
  11. TTableReservation = (trNone, trSharedLockRead, trSharedLockWrite,
  12. trProtectedLockRead, trProtectedLockWrite);
  13. TIBCursor = Class(TSQLHandle)
  14. protected
  15. Status : array [0..19] of ISC_STATUS;
  16. Statement : pointer;
  17. SQLDA : PXSQLDA;
  18. end;
  19. TIBTrans = Class(TSQLHandle)
  20. protected
  21. TransactionHandle : pointer;
  22. TPB : string; // Transaction parameter buffer
  23. Status : array [0..19] of ISC_STATUS;
  24. AccessMode : TAccessMode;
  25. IsolationLevel : TIsolationLevel;
  26. LockResolution : TLockResolution;
  27. TableReservation : TTableReservation;
  28. end;
  29. TIBConnection = class (TSQLConnection)
  30. private
  31. FSQLDAAllocated : integer;
  32. FSQLDatabaseHandle : pointer;
  33. FStatus : array [0..19] of ISC_STATUS;
  34. FFieldFlag : array [0..1023] of shortint;
  35. FDialect : integer;
  36. procedure SetDBDialect;
  37. procedure AllocSQLDA(Cursor : TIBCursor;Count : integer);
  38. procedure TranslateFldType(SQLType, SQLLen : integer; var LensSet : boolean;
  39. var TrType : TFieldType; var TrLen : word);
  40. procedure SetTPB(trans : TIBtrans);
  41. // conversion methods
  42. procedure GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  43. procedure GetFloat(CurrBuff, Buffer : pointer; Field : TField);
  44. procedure CheckError(ProcName : string; Status : array of ISC_STATUS);
  45. protected
  46. procedure DoInternalConnect; override;
  47. procedure DoInternalDisconnect; override;
  48. function GetHandle : pointer; override;
  49. Function AllocateCursorHandle : TSQLHandle; override;
  50. Function AllocateTransactionHandle : TSQLHandle; override;
  51. procedure FreeStatement(cursor : TSQLHandle); override;
  52. procedure PrepareStatement(cursor: TSQLHandle;ATransaction : TSQLTransaction;buf : string); override;
  53. procedure FreeFldBuffers(cursor : TSQLHandle); override;
  54. procedure Execute(cursor: TSQLHandle;atransaction:tSQLtransaction); override;
  55. procedure AddFieldDefs(cursor: TSQLHandle; FieldDefs : TfieldDefs); override;
  56. function GetFieldSizes(cursor : TSQLHandle) : integer; override;
  57. function Fetch(cursor : TSQLHandle) : boolean; override;
  58. procedure LoadFieldsFromBuffer(cursor : TSQLHandle;buffer: pchar); override;
  59. function GetFieldData(Cursor : TSQLHandle;Field: TField; FieldDefs : TfieldDefs; Buffer: Pointer;currbuff : pchar): Boolean; override;
  60. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  61. function Commit(trans : TSQLHandle) : boolean; override;
  62. function RollBack(trans : TSQLHandle) : boolean; override;
  63. function StartdbTransaction(trans : TSQLHandle) : boolean; override;
  64. procedure CommitRetaining(trans : TSQLHandle); override;
  65. procedure RollBackRetaining(trans : TSQLHandle); override;
  66. published
  67. property Dialect : integer read FDialect write FDialect;
  68. property DatabaseName;
  69. property KeepConnection;
  70. property LoginPrompt;
  71. property Params;
  72. property OnLogin;
  73. end;
  74. implementation
  75. resourcestring
  76. SErrNoDatabaseName = 'Database connect string (DatabaseName) not filled in!';
  77. type
  78. TTm = packed record
  79. tm_sec : longint;
  80. tm_min : longint;
  81. tm_hour : longint;
  82. tm_mday : longint;
  83. tm_mon : longint;
  84. tm_year : longint;
  85. tm_wday : longint;
  86. tm_yday : longint;
  87. tm_isdst : longint;
  88. __tm_gmtoff : longint;
  89. __tm_zone : Pchar;
  90. end;
  91. procedure TIBConnection.CheckError(ProcName : string; Status : array of ISC_STATUS);
  92. var
  93. buf : array [0..1024] of char;
  94. p : pointer;
  95. Msg : string;
  96. begin
  97. if ((Status[0] = 1) and (Status[1] <> 0)) then
  98. begin
  99. p := @Status;
  100. while isc_interprete(Buf, @p) > 0 do
  101. Msg := Msg + #10' -' + StrPas(Buf);
  102. DatabaseError(ProcName + ': ' + Msg,self);
  103. end;
  104. end;
  105. procedure TIBConnection.SetTPB(trans : TIBtrans);
  106. begin
  107. with trans do
  108. begin
  109. TPB := chr(isc_tpb_version3);
  110. case AccessMode of
  111. amReadWrite : TPB := TPB + chr(isc_tpb_write);
  112. amReadOnly : TPB := TPB + chr(isc_tpb_read);
  113. end;
  114. case IsolationLevel of
  115. ilConsistent : TPB := TPB + chr(isc_tpb_consistency);
  116. ilConcurrent : TPB := TPB + chr(isc_tpb_concurrency);
  117. ilReadCommittedRecV : TPB := TPB + chr(isc_tpb_read_committed) +
  118. chr(isc_tpb_rec_version);
  119. ilReadCommitted : TPB := TPB + chr(isc_tpb_read_committed) +
  120. chr(isc_tpb_no_rec_version);
  121. end;
  122. case LockResolution of
  123. lrWait : TPB := TPB + chr(isc_tpb_wait);
  124. lrNoWait : TPB := TPB + chr(isc_tpb_nowait);
  125. end;
  126. case TableReservation of
  127. trSharedLockRead : TPB := TPB + chr(isc_tpb_shared) +
  128. chr(isc_tpb_lock_read);
  129. trSharedLockWrite : TPB := TPB + chr(isc_tpb_shared) +
  130. chr(isc_tpb_lock_write);
  131. trProtectedLockRead : TPB := TPB + chr(isc_tpb_protected) +
  132. chr(isc_tpb_lock_read);
  133. trProtectedLockWrite : TPB := TPB + chr(isc_tpb_protected) +
  134. chr(isc_tpb_lock_write);
  135. end;
  136. end;
  137. end;
  138. function TIBConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  139. begin
  140. Result := (trans as TIBtrans).TransactionHandle;
  141. end;
  142. function TIBConnection.Commit(trans : TSQLHandle) : boolean;
  143. begin
  144. result := false;
  145. with (trans as TIBTrans) do
  146. if isc_commit_transaction(@Status, @TransactionHandle) <> 0 then
  147. CheckError('Commit', Status)
  148. else result := true;
  149. end;
  150. function TIBConnection.RollBack(trans : TSQLHandle) : boolean;
  151. begin
  152. result := false;
  153. if isc_rollback_transaction(@TIBTrans(trans).Status, @TIBTrans(trans).TransactionHandle) <> 0 then
  154. CheckError('Rollback', TIBTrans(trans).Status)
  155. else result := true;
  156. end;
  157. function TIBConnection.StartDBTransaction(trans : TSQLHandle) : boolean;
  158. var
  159. DBHandle : pointer;
  160. tr : TIBTrans;
  161. begin
  162. result := false;
  163. DBHandle := GetHandle;
  164. tr := trans as TIBtrans;
  165. SetTPB(tr);
  166. with tr do
  167. begin
  168. TransactionHandle := nil;
  169. if isc_start_transaction(@Status, @TransactionHandle, 1,
  170. [@DBHandle, Length(TPB), @TPB[1]]) <> 0 then
  171. CheckError('StartTransaction',Status)
  172. else Result := True;
  173. end;
  174. end;
  175. procedure TIBConnection.CommitRetaining(trans : TSQLHandle);
  176. begin
  177. with trans as TIBtrans do
  178. if isc_commit_retaining(@Status, @TransactionHandle) <> 0 then
  179. CheckError('CommitRetaining', Status);
  180. end;
  181. procedure TIBConnection.RollBackRetaining(trans : TSQLHandle);
  182. begin
  183. with trans as TIBtrans do
  184. if isc_rollback_retaining(@Status, @TransactionHandle) <> 0 then
  185. CheckError('RollBackRetaining', Status);
  186. end;
  187. procedure TIBConnection.DoInternalConnect;
  188. var
  189. DPB : string;
  190. begin
  191. inherited dointernalconnect;
  192. DPB := chr(isc_dpb_version1);
  193. if (UserName <> '') then
  194. begin
  195. DPB := DPB + chr(isc_dpb_user_name) + chr(Length(UserName)) + UserName;
  196. if (Password <> '') then
  197. DPB := DPB + chr(isc_dpb_password) + chr(Length(Password)) + Password;
  198. end;
  199. if (Role <> '') then
  200. DPB := DPB + chr(isc_dpb_sql_role_name) + chr(Length(Role)) + Role;
  201. if Length(CharSet) > 0 then
  202. DPB := DPB + Chr(isc_dpb_lc_ctype) + Chr(Length(CharSet)) + CharSet;
  203. if (DatabaseName = '') then
  204. DatabaseError(SErrNoDatabaseName,self);
  205. FSQLDatabaseHandle := nil;
  206. if isc_attach_database(@FStatus, Length(DatabaseName), @DatabaseName[1], @FSQLDatabaseHandle,
  207. Length(DPB), @DPB[1]) <> 0 then
  208. CheckError('DoInternalConnect', FStatus);
  209. SetDBDialect;
  210. end;
  211. procedure TIBConnection.DoInternalDisconnect;
  212. begin
  213. if not Connected then
  214. begin
  215. FSQLDatabaseHandle := nil;
  216. Exit;
  217. end;
  218. isc_detach_database(@FStatus[0], @FSQLDatabaseHandle);
  219. CheckError('Close', FStatus);
  220. end;
  221. procedure TIBConnection.SetDBDialect;
  222. var
  223. x : integer;
  224. Len : integer;
  225. Buffer : string;
  226. ResBuf : array [0..39] of byte;
  227. begin
  228. Buffer := Chr(isc_info_db_sql_dialect) + Chr(isc_info_end);
  229. if isc_database_info(@FStatus, @FSQLDatabaseHandle, Length(Buffer),
  230. @Buffer[1], SizeOf(ResBuf), @ResBuf) <> 0 then
  231. CheckError('SetDBDialect', FStatus);
  232. x := 0;
  233. while x < 40 do
  234. case ResBuf[x] of
  235. isc_info_db_sql_dialect :
  236. begin
  237. Inc(x);
  238. Len := isc_vax_integer(@ResBuf[x], 2);
  239. Inc(x, 2);
  240. FDialect := isc_vax_integer(@ResBuf[x], Len);
  241. Inc(x, Len);
  242. end;
  243. isc_info_end : Break;
  244. end;
  245. end;
  246. procedure TIBConnection.AllocSQLDA(Cursor : TIBcursor;Count : integer);
  247. begin
  248. with cursor as TIBCursor do
  249. begin
  250. if FSQLDAAllocated > 0 then
  251. FreeMem(SQLDA);
  252. GetMem(SQLDA, XSQLDA_Length(Count));
  253. { Zero out the memory block to avoid problems with exceptions within the
  254. constructor of this class. }
  255. FillChar(SQLDA^, XSQLDA_Length(Count), 0);
  256. FSQLDAAllocated := Count;
  257. SQLDA^.Version := sqlda_version1;
  258. SQLDA^.SQLN := Count;
  259. end;
  260. end;
  261. procedure TIBConnection.TranslateFldType(SQLType, SQLLen : integer; var LensSet : boolean;
  262. var TrType : TFieldType; var TrLen : word);
  263. begin
  264. LensSet := False;
  265. case (SQLType and not 1) of
  266. SQL_VARYING :
  267. begin
  268. LensSet := True;
  269. TrType := ftString;
  270. TrLen := SQLLen;
  271. end;
  272. SQL_TEXT :
  273. begin
  274. LensSet := True;
  275. TrType := ftString;
  276. TrLen := SQLLen;
  277. end;
  278. SQL_TYPE_DATE :
  279. TrType := ftDateTime;
  280. SQL_TYPE_TIME :
  281. TrType := ftDateTime;
  282. SQL_TIMESTAMP :
  283. TrType := ftDateTime;
  284. SQL_ARRAY :
  285. begin
  286. end;
  287. SQL_BLOB :
  288. begin
  289. end;
  290. SQL_SHORT :
  291. begin
  292. LensSet := True;
  293. TrLen := SQLLen;
  294. TrType := ftInteger;
  295. end;
  296. SQL_LONG :
  297. begin
  298. LensSet := True;
  299. TrLen := SQLLen;
  300. TrType := ftInteger;
  301. end;
  302. SQL_INT64 :
  303. {TrType := ftInt64};
  304. SQL_DOUBLE :
  305. begin
  306. LensSet := True;
  307. TrLen := SQLLen;
  308. TrType := ftFloat;
  309. end;
  310. SQL_FLOAT :
  311. begin
  312. LensSet := True;
  313. TrLen := SQLLen;
  314. TrType := ftFloat;
  315. end;
  316. end;
  317. end;
  318. Function TIBConnection.AllocateCursorHandle : TSQLHandle;
  319. var curs : TIBCursor;
  320. begin
  321. curs := TIBCursor.create;
  322. AllocSQLDA(curs,10);
  323. result := curs;
  324. end;
  325. Function TIBConnection.AllocateTransactionHandle : TSQLHandle;
  326. begin
  327. result := TIBTrans.create;
  328. end;
  329. procedure TIBConnection.FreeStatement(cursor : TSQLHandle);
  330. begin
  331. with cursor as TIBcursor do
  332. begin
  333. if isc_dsql_free_statement(@Status, @Statement, DSQL_Drop) <> 0 then
  334. CheckError('FreeStatement', Status);
  335. Statement := nil;
  336. end;
  337. end;
  338. procedure TIBConnection.PrepareStatement(cursor: TSQLHandle;ATransaction : TSQLTransaction;buf : string);
  339. var dh : pointer;
  340. tr : pointer;
  341. x : shortint;
  342. begin
  343. with cursor as TIBcursor do
  344. begin
  345. dh := GetHandle;
  346. if isc_dsql_allocate_statement(@Status, @dh, @Statement) <> 0 then
  347. CheckError('PrepareStatement', Status);
  348. tr := aTransaction.Handle;
  349. if isc_dsql_prepare(@Status, @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  350. CheckError('PrepareStatement', Status);
  351. if StatementType = stselect then
  352. begin
  353. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  354. CheckError('PrepareSelect', Status);
  355. if SQLDA^.SQLD > SQLDA^.SQLN then
  356. begin
  357. AllocSQLDA((cursor as TIBCursor),SQLDA^.SQLD);
  358. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  359. CheckError('PrepareSelect', Status);
  360. end;
  361. {$R-}
  362. for x := 0 to SQLDA^.SQLD - 1 do
  363. begin
  364. SQLDA^.SQLVar[x].SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  365. SQLDA^.SQLVar[x].SQLInd := @FFieldFlag[x];
  366. end;
  367. {$R+}
  368. end;
  369. end;
  370. end;
  371. procedure TIBConnection.FreeFldBuffers(cursor : TSQLHandle);
  372. var
  373. x : shortint;
  374. begin
  375. {$R-}
  376. with cursor as TIBCursor do
  377. for x := 0 to SQLDA^.SQLD - 1 do
  378. begin
  379. if SQLDA^.SQLVar[x].SQLData <> nil then
  380. begin
  381. FreeMem(SQLDA^.SQLVar[x].SQLData);
  382. SQLDA^.SQLVar[x].SQLData := nil;
  383. end;
  384. end;
  385. {$R+}
  386. end;
  387. procedure TIBConnection.Execute(cursor: TSQLHandle;atransaction:tSQLtransaction);
  388. var tr : pointer;
  389. begin
  390. tr := aTransaction.Handle;
  391. with cursor as TIBCursor do
  392. if isc_dsql_execute(@Status, @tr, @Statement, 1, nil) <> 0 then
  393. CheckError('Execute', Status);
  394. end;
  395. procedure TIBConnection.AddFieldDefs(cursor: TSQLHandle; FieldDefs : TfieldDefs);
  396. var
  397. x : integer;
  398. lenset : boolean;
  399. TransLen : word;
  400. TransType : TFieldType;
  401. begin
  402. {$R-}
  403. with cursor as TIBCursor do
  404. begin
  405. for x := 0 to SQLDA^.SQLD - 1 do
  406. begin
  407. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].SQLLen, lenset,
  408. TransType, TransLen);
  409. TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].SQLName, TransType,
  410. TransLen, False, (x + 1));
  411. end;
  412. end;
  413. {$R+}
  414. end;
  415. function TIBConnection.GetFieldSizes(cursor : TSQLHandle) : integer;
  416. var
  417. x,recsize : integer;
  418. begin
  419. recsize := 0;
  420. {$R-}
  421. with cursor as TIBCursor do
  422. for x := 0 to SQLDA^.SQLD - 1 do
  423. Inc(recsize, SQLDA^.SQLVar[x].SQLLen);
  424. {$R+}
  425. result := recsize;
  426. end;
  427. function TIBConnection.GetHandle: pointer;
  428. begin
  429. Result := FSQLDatabaseHandle;
  430. end;
  431. function TIBConnection.Fetch(cursor : TSQLHandle) : boolean;
  432. var
  433. retcode : integer;
  434. begin
  435. with cursor as TIBCursor do
  436. begin
  437. retcode := isc_dsql_fetch(@Status, @Statement, 1, SQLDA);
  438. if (retcode <> 0) and (retcode <> 100) then
  439. CheckError('Fetch', Status);
  440. end;
  441. Result := (retcode <> 100);
  442. end;
  443. procedure TIBConnection.LoadFieldsFromBuffer(cursor : TSQLHandle;buffer : pchar);
  444. var
  445. x : integer;
  446. VarcharLen : word;
  447. begin
  448. {$R-}
  449. with cursor as TIBCursor do for x := 0 to SQLDA^.SQLD - 1 do
  450. begin
  451. with SQLDA^.SQLVar[x] do
  452. begin
  453. if ((SQLType and not 1) = SQL_VARYING) then
  454. begin
  455. Move(SQLData^, VarcharLen, 2);
  456. Move((SQLData + 2)^, Buffer^, VarcharLen);
  457. PChar(Buffer + VarcharLen)^ := #0;
  458. end
  459. else Move(SQLData^, Buffer^, SQLLen);
  460. Inc(Buffer, SQLLen);
  461. end;
  462. end;
  463. {$R+}
  464. end;
  465. function TIBConnection.GetFieldData(Cursor : TSQLHandle;Field: TField; FieldDefs : TfieldDefs; Buffer: Pointer;currbuff : pchar): Boolean;
  466. var
  467. x : longint;
  468. b : longint;
  469. begin
  470. Result := False;
  471. with cursor as TIBCursor do for x := 0 to SQLDA^.SQLD - 1 do
  472. begin
  473. {$R-}
  474. if (Field.FieldName = SQLDA^.SQLVar[x].SQLName) then
  475. begin
  476. case Field.DataType of
  477. ftInteger :
  478. begin
  479. b := 0;
  480. Move(b, Buffer^, 4);
  481. Move(CurrBuff^, Buffer^, Field.Size);
  482. end;
  483. ftDate, ftTime, ftDateTime:
  484. GetDateTime(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLType);
  485. ftString :
  486. begin
  487. Move(CurrBuff^, Buffer^, Field.Size);
  488. PChar(Buffer + Field.Size)^ := #0;
  489. end;
  490. ftFloat :
  491. GetFloat(CurrBuff, Buffer, Field);
  492. end;
  493. Result := True;
  494. Break;
  495. end
  496. else Inc(CurrBuff, SQLDA^.SQLVar[x].SQLLen);
  497. {$R+}
  498. end;
  499. end;
  500. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  501. var
  502. CTime : TTm; // C struct time
  503. STime : TSystemTime; // System time
  504. PTime : TDateTime; // Pascal time
  505. begin
  506. case (AType and not 1) of
  507. SQL_TYPE_DATE :
  508. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  509. SQL_TYPE_TIME :
  510. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  511. SQL_TIMESTAMP :
  512. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  513. end;
  514. STime.Year := CTime.tm_year + 1900;
  515. STime.Month := CTime.tm_mon + 1;
  516. STime.Day := CTime.tm_mday;
  517. STime.Hour := CTime.tm_hour;
  518. STime.Minute := CTime.tm_min;
  519. STime.Second := CTime.tm_sec;
  520. STime.Millisecond := 0;
  521. PTime := SystemTimeToDateTime(STime);
  522. Move(PTime, Buffer^, SizeOf(PTime));
  523. end;
  524. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Field : TField);
  525. var
  526. Ext : extended;
  527. Dbl : double;
  528. Sin : single;
  529. begin
  530. case Field.Size of
  531. 4 :
  532. begin
  533. Move(CurrBuff^, Sin, 4);
  534. Dbl := Sin;
  535. end;
  536. 8 :
  537. begin
  538. Move(CurrBuff^, Dbl, 8);
  539. end;
  540. 10:
  541. begin
  542. Move(CurrBuff^, Ext, 10);
  543. Dbl := Ext;
  544. end;
  545. end;
  546. Move(Dbl, Buffer^, 8);
  547. end;
  548. end.