ibconnection.pp 17 KB

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