ibconnection.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 [0..1023] 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 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. end;
  288. SQL_BLOB :
  289. begin
  290. end;
  291. SQL_SHORT :
  292. TrType := ftInteger;
  293. SQL_LONG :
  294. begin
  295. LensSet := True;
  296. TrLen := 0;
  297. TrType := ftInteger;
  298. end;
  299. SQL_INT64 :
  300. {TrType := ftInt64};
  301. SQL_DOUBLE :
  302. begin
  303. LensSet := True;
  304. TrLen := 0;
  305. TrType := ftFloat;
  306. end;
  307. SQL_FLOAT :
  308. begin
  309. LensSet := True;
  310. TrLen := 0;
  311. TrType := ftFloat;
  312. end;
  313. end;
  314. end;
  315. Function TIBConnection.AllocateCursorHandle : TSQLHandle;
  316. var curs : TIBCursor;
  317. begin
  318. curs := TIBCursor.create;
  319. curs.sqlda := nil;
  320. curs.statement := nil;
  321. AllocSQLDA(curs,10);
  322. result := curs;
  323. end;
  324. Function TIBConnection.AllocateTransactionHandle : TSQLHandle;
  325. begin
  326. result := TIBTrans.create;
  327. end;
  328. procedure TIBConnection.FreeStatement(cursor : TSQLHandle);
  329. begin
  330. with cursor as TIBcursor do
  331. begin
  332. if isc_dsql_free_statement(@Status, @Statement, DSQL_Drop) <> 0 then
  333. CheckError('FreeStatement', Status);
  334. Statement := nil;
  335. end;
  336. end;
  337. procedure TIBConnection.PrepareStatement(cursor: TSQLHandle;ATransaction : TSQLTransaction;buf : string);
  338. var dh : pointer;
  339. tr : pointer;
  340. x : shortint;
  341. begin
  342. with cursor as TIBcursor do
  343. begin
  344. dh := GetHandle;
  345. if isc_dsql_allocate_statement(@Status, @dh, @Statement) <> 0 then
  346. CheckError('PrepareStatement', Status);
  347. tr := aTransaction.Handle;
  348. if isc_dsql_prepare(@Status, @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  349. CheckError('PrepareStatement', Status);
  350. if StatementType = stselect then
  351. begin
  352. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  353. CheckError('PrepareSelect', Status);
  354. if SQLDA^.SQLD > SQLDA^.SQLN then
  355. begin
  356. AllocSQLDA((cursor as TIBCursor),SQLDA^.SQLD);
  357. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  358. CheckError('PrepareSelect', Status);
  359. end;
  360. {$R-}
  361. for x := 0 to SQLDA^.SQLD - 1 do
  362. begin
  363. SQLDA^.SQLVar[x].SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  364. SQLDA^.SQLVar[x].SQLInd := @FFieldFlag[x];
  365. end;
  366. {$R+}
  367. end;
  368. end;
  369. end;
  370. procedure TIBConnection.FreeFldBuffers(cursor : TSQLHandle);
  371. var
  372. x : shortint;
  373. begin
  374. {$R-}
  375. with cursor as TIBCursor do
  376. for x := 0 to SQLDA^.SQLD - 1 do
  377. if SQLDA^.SQLVar[x].SQLData <> nil then reAllocMem(SQLDA^.SQLVar[x].SQLData,0);
  378. {$R+}
  379. end;
  380. procedure TIBConnection.Execute(cursor: TSQLHandle;atransaction:tSQLtransaction);
  381. var tr : pointer;
  382. begin
  383. tr := aTransaction.Handle;
  384. with cursor as TIBCursor do
  385. if isc_dsql_execute(@Status, @tr, @Statement, 1, nil) <> 0 then
  386. CheckError('Execute', Status);
  387. end;
  388. procedure TIBConnection.AddFieldDefs(cursor: TSQLHandle;FieldDefs : TfieldDefs);
  389. var
  390. x : integer;
  391. lenset : boolean;
  392. TransLen : word;
  393. TransType : TFieldType;
  394. begin
  395. {$R-}
  396. with cursor as TIBCursor do
  397. begin
  398. for x := 0 to SQLDA^.SQLD - 1 do
  399. begin
  400. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].SQLLen, SQLDA^.SQLVar[x].SQLScale,
  401. lenset, TransType, TransLen);
  402. TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].SQLName, TransType,
  403. TransLen, False, (x + 1)).precision := SQLDA^.SQLVar[x].SQLLen
  404. end;
  405. end;
  406. {$R+}
  407. end;
  408. {function TIBConnection.GetFieldSizes(cursor : TSQLHandle) : integer;
  409. var
  410. x,recsize : integer;
  411. begin
  412. recsize := sizeof(longint); // size of the NullMask
  413. with cursor as TIBCursor do
  414. for x := 0 to SQLDA^.SQLD - 1 do
  415. if (SQLDA^.SQLVar[x].SQLType and not 1) in [SQL_VARYING,SQL_TEXT] then
  416. Inc(recsize, SQLDA^.SQLVar[x].SQLLen+1)
  417. else
  418. Inc(recsize, SQLDA^.SQLVar[x].SQLLen);
  419. result := recsize;
  420. end;}
  421. function TIBConnection.GetHandle: pointer;
  422. begin
  423. Result := FSQLDatabaseHandle;
  424. end;
  425. function TIBConnection.Fetch(cursor : TSQLHandle) : boolean;
  426. var
  427. retcode : integer;
  428. begin
  429. with cursor as TIBCursor do
  430. begin
  431. retcode := isc_dsql_fetch(@Status, @Statement, 1, SQLDA);
  432. if (retcode <> 0) and (retcode <> 100) then
  433. CheckError('Fetch', Status);
  434. end;
  435. Result := (retcode <> 100);
  436. end;
  437. function TIBConnection.LoadField(cursor : TSQLHandle;FieldDef : TfieldDef;buffer : pointer) : boolean;
  438. var
  439. x : integer;
  440. VarcharLen : word;
  441. CurrBuff : pchar;
  442. b : longint;
  443. c : currency;
  444. begin
  445. with cursor as TIBCursor do
  446. begin
  447. {$R-}
  448. for x := 0 to SQLDA^.SQLD - 1 do
  449. if SQLDA^.SQLVar[x].SQLName = FieldDef.Name then break;
  450. if SQLDA^.SQLVar[x].SQLName <> FieldDef.Name then
  451. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  452. if SQLDA^.SQLVar[x].SQLInd^ = -1 then
  453. result := false
  454. else
  455. begin
  456. with SQLDA^.SQLVar[x] do
  457. if ((SQLType and not 1) = SQL_VARYING) then
  458. begin
  459. Move(SQLData^, VarcharLen, 2);
  460. CurrBuff := SQLData + 2;
  461. PChar(CurrBuff + Varcharlen)^ := #0;
  462. end
  463. else CurrBuff := SQLData;
  464. Result := true;
  465. case FieldDef.DataType of
  466. ftBCD :
  467. begin
  468. c := 0;
  469. Move(CurrBuff^, c, SQLDA^.SQLVar[x].SQLLen);
  470. c := c*intpower(10,4+SQLDA^.SQLVar[x].SQLScale);
  471. Move(c, buffer^ , sizeof(c));
  472. end;
  473. ftInteger :
  474. begin
  475. b := 0;
  476. Move(b, Buffer^, 4);
  477. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  478. end;
  479. ftDate, ftTime, ftDateTime:
  480. GetDateTime(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLType);
  481. ftString :
  482. begin
  483. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  484. PChar(Buffer + SQLDA^.SQLVar[x].SQLLen)^ := #0;
  485. end;
  486. ftFloat :
  487. GetFloat(CurrBuff, Buffer, FieldDef)
  488. else result := false;
  489. end;
  490. end;
  491. {$R+}
  492. end;
  493. end;
  494. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  495. var
  496. CTime : TTm; // C struct time
  497. STime : TSystemTime; // System time
  498. PTime : TDateTime; // Pascal time
  499. begin
  500. case (AType and not 1) of
  501. SQL_TYPE_DATE :
  502. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  503. SQL_TYPE_TIME :
  504. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  505. SQL_TIMESTAMP :
  506. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  507. end;
  508. STime.Year := CTime.tm_year + 1900;
  509. STime.Month := CTime.tm_mon + 1;
  510. STime.Day := CTime.tm_mday;
  511. STime.Hour := CTime.tm_hour;
  512. STime.Minute := CTime.tm_min;
  513. STime.Second := CTime.tm_sec;
  514. STime.Millisecond := 0;
  515. PTime := SystemTimeToDateTime(STime);
  516. Move(PTime, Buffer^, SizeOf(PTime));
  517. end;
  518. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  519. var
  520. Ext : extended;
  521. Dbl : double;
  522. Sin : single;
  523. begin
  524. case Field.Size of
  525. 4 :
  526. begin
  527. Move(CurrBuff^, Sin, 4);
  528. Dbl := Sin;
  529. end;
  530. 8 :
  531. begin
  532. Move(CurrBuff^, Dbl, 8);
  533. end;
  534. 10:
  535. begin
  536. Move(CurrBuff^, Ext, 10);
  537. Dbl := double(Ext);
  538. end;
  539. end;
  540. Move(Dbl, Buffer^, 8);
  541. end;
  542. end.