ibconnection.pp 19 KB

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