ibconnection.pp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  71. published
  72. property Dialect : integer read FDialect write FDialect;
  73. property DatabaseName;
  74. property KeepConnection;
  75. property LoginPrompt;
  76. property Params;
  77. property OnLogin;
  78. end;
  79. implementation
  80. resourcestring
  81. SErrNoDatabaseName = 'Database connect string (DatabaseName) not filled in!';
  82. type
  83. TTm = packed record
  84. tm_sec : longint;
  85. tm_min : longint;
  86. tm_hour : longint;
  87. tm_mday : longint;
  88. tm_mon : longint;
  89. tm_year : longint;
  90. tm_wday : longint;
  91. tm_yday : longint;
  92. tm_isdst : longint;
  93. __tm_gmtoff : longint;
  94. __tm_zone : Pchar;
  95. end;
  96. procedure TIBConnection.CheckError(ProcName : string; Status : array of ISC_STATUS);
  97. var
  98. buf : array [0..1024] of char;
  99. p : pointer;
  100. Msg : string;
  101. begin
  102. if ((Status[0] = 1) and (Status[1] <> 0)) then
  103. begin
  104. p := @Status;
  105. msg := '';
  106. while isc_interprete(Buf, @p) > 0 do
  107. Msg := Msg + #10' -' + StrPas(Buf);
  108. DatabaseError(ProcName + ': ' + Msg,self);
  109. end;
  110. end;
  111. procedure TIBConnection.SetTPB(trans : TIBtrans);
  112. begin
  113. with trans do
  114. begin
  115. TPB := chr(isc_tpb_version3);
  116. case AccessMode of
  117. amReadWrite : TPB := TPB + chr(isc_tpb_write);
  118. amReadOnly : TPB := TPB + chr(isc_tpb_read);
  119. end;
  120. case IsolationLevel of
  121. ilConsistent : TPB := TPB + chr(isc_tpb_consistency);
  122. ilConcurrent : TPB := TPB + chr(isc_tpb_concurrency);
  123. ilReadCommittedRecV : TPB := TPB + chr(isc_tpb_read_committed) +
  124. chr(isc_tpb_rec_version);
  125. ilReadCommitted : TPB := TPB + chr(isc_tpb_read_committed) +
  126. chr(isc_tpb_no_rec_version);
  127. end;
  128. case LockResolution of
  129. lrWait : TPB := TPB + chr(isc_tpb_wait);
  130. lrNoWait : TPB := TPB + chr(isc_tpb_nowait);
  131. end;
  132. case TableReservation of
  133. trSharedLockRead : TPB := TPB + chr(isc_tpb_shared) +
  134. chr(isc_tpb_lock_read);
  135. trSharedLockWrite : TPB := TPB + chr(isc_tpb_shared) +
  136. chr(isc_tpb_lock_write);
  137. trProtectedLockRead : TPB := TPB + chr(isc_tpb_protected) +
  138. chr(isc_tpb_lock_read);
  139. trProtectedLockWrite : TPB := TPB + chr(isc_tpb_protected) +
  140. chr(isc_tpb_lock_write);
  141. end;
  142. end;
  143. end;
  144. function TIBConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  145. begin
  146. Result := (trans as TIBtrans).TransactionHandle;
  147. end;
  148. function TIBConnection.Commit(trans : TSQLHandle) : boolean;
  149. begin
  150. result := false;
  151. with (trans as TIBTrans) do
  152. if isc_commit_transaction(@Status, @TransactionHandle) <> 0 then
  153. CheckError('Commit', Status)
  154. else result := true;
  155. end;
  156. function TIBConnection.RollBack(trans : TSQLHandle) : boolean;
  157. begin
  158. result := false;
  159. if isc_rollback_transaction(@TIBTrans(trans).Status, @TIBTrans(trans).TransactionHandle) <> 0 then
  160. CheckError('Rollback', TIBTrans(trans).Status)
  161. else result := true;
  162. end;
  163. function TIBConnection.StartDBTransaction(trans : TSQLHandle) : boolean;
  164. var
  165. DBHandle : pointer;
  166. tr : TIBTrans;
  167. begin
  168. result := false;
  169. DBHandle := GetHandle;
  170. tr := trans as TIBtrans;
  171. SetTPB(tr);
  172. with tr do
  173. begin
  174. TransactionHandle := nil;
  175. if isc_start_transaction(@Status, @TransactionHandle, 1,
  176. [@DBHandle, Length(TPB), @TPB[1]]) <> 0 then
  177. CheckError('StartTransaction',Status)
  178. else Result := True;
  179. end;
  180. end;
  181. procedure TIBConnection.CommitRetaining(trans : TSQLHandle);
  182. begin
  183. with trans as TIBtrans do
  184. if isc_commit_retaining(@Status, @TransactionHandle) <> 0 then
  185. CheckError('CommitRetaining', Status);
  186. end;
  187. procedure TIBConnection.RollBackRetaining(trans : TSQLHandle);
  188. begin
  189. with trans as TIBtrans do
  190. if isc_rollback_retaining(@Status, @TransactionHandle) <> 0 then
  191. CheckError('RollBackRetaining', Status);
  192. end;
  193. procedure TIBConnection.DoInternalConnect;
  194. var
  195. DPB : string;
  196. begin
  197. {$IfDef LinkDynamically}
  198. InitialiseIBase60;
  199. {$EndIf}
  200. inherited dointernalconnect;
  201. DPB := chr(isc_dpb_version1);
  202. if (UserName <> '') then
  203. begin
  204. DPB := DPB + chr(isc_dpb_user_name) + chr(Length(UserName)) + UserName;
  205. if (Password <> '') then
  206. DPB := DPB + chr(isc_dpb_password) + chr(Length(Password)) + Password;
  207. end;
  208. if (Role <> '') then
  209. DPB := DPB + chr(isc_dpb_sql_role_name) + chr(Length(Role)) + Role;
  210. if Length(CharSet) > 0 then
  211. DPB := DPB + Chr(isc_dpb_lc_ctype) + Chr(Length(CharSet)) + CharSet;
  212. if (DatabaseName = '') then
  213. DatabaseError(SErrNoDatabaseName,self);
  214. FSQLDatabaseHandle := nil;
  215. if isc_attach_database(@FStatus, Length(DatabaseName), @DatabaseName[1], @FSQLDatabaseHandle,
  216. Length(DPB), @DPB[1]) <> 0 then
  217. CheckError('DoInternalConnect', FStatus);
  218. SetDBDialect;
  219. end;
  220. procedure TIBConnection.DoInternalDisconnect;
  221. begin
  222. if not Connected then
  223. begin
  224. FSQLDatabaseHandle := nil;
  225. Exit;
  226. end;
  227. isc_detach_database(@FStatus[0], @FSQLDatabaseHandle);
  228. CheckError('Close', FStatus);
  229. {$IfDef LinkDynamically}
  230. ReleaseIBase60;
  231. {$EndIf}
  232. end;
  233. procedure TIBConnection.SetDBDialect;
  234. var
  235. x : integer;
  236. Len : integer;
  237. Buffer : string;
  238. ResBuf : array [0..39] of byte;
  239. begin
  240. Buffer := Chr(isc_info_db_sql_dialect) + Chr(isc_info_end);
  241. if isc_database_info(@FStatus, @FSQLDatabaseHandle, Length(Buffer),
  242. @Buffer[1], SizeOf(ResBuf), @ResBuf) <> 0 then
  243. CheckError('SetDBDialect', FStatus);
  244. x := 0;
  245. while x < 40 do
  246. case ResBuf[x] of
  247. isc_info_db_sql_dialect :
  248. begin
  249. Inc(x);
  250. Len := isc_vax_integer(@ResBuf[x], 2);
  251. Inc(x, 2);
  252. FDialect := isc_vax_integer(@ResBuf[x], Len);
  253. Inc(x, Len);
  254. end;
  255. isc_info_end : Break;
  256. end;
  257. end;
  258. procedure TIBConnection.AllocSQLDA(Cursor : TIBcursor;Count : integer);
  259. begin
  260. with cursor as TIBCursor do
  261. begin
  262. reAllocMem(SQLDA, XSQLDA_Length(Count));
  263. { Zero out the memory block to avoid problems with exceptions within the
  264. constructor of this class. }
  265. FillChar(SQLDA^, XSQLDA_Length(Count), 0);
  266. SQLDA^.Version := sqlda_version1;
  267. SQLDA^.SQLN := Count;
  268. end;
  269. end;
  270. procedure TIBConnection.TranslateFldType(SQLType, SQLLen, SQLScale : integer; var LensSet : boolean;
  271. var TrType : TFieldType; var TrLen : word);
  272. begin
  273. LensSet := False;
  274. if (SQLScale >= -4) and (SQLScale <= -1) then //in [-4..-1] then
  275. begin
  276. LensSet := True;
  277. TrLen := SQLScale;
  278. TrType := ftBCD
  279. end
  280. else case (SQLType and not 1) of
  281. SQL_VARYING :
  282. begin
  283. LensSet := True;
  284. TrType := ftString;
  285. TrLen := SQLLen;
  286. end;
  287. SQL_TEXT :
  288. begin
  289. LensSet := True;
  290. TrType := ftString;
  291. TrLen := SQLLen;
  292. end;
  293. SQL_TYPE_DATE :
  294. TrType := ftDate{Time};
  295. SQL_TYPE_TIME :
  296. TrType := ftDateTime;
  297. SQL_TIMESTAMP :
  298. TrType := ftDateTime;
  299. SQL_ARRAY :
  300. begin
  301. TrType := ftArray;
  302. LensSet := true;
  303. TrLen := SQLLen;
  304. end;
  305. SQL_BLOB :
  306. begin
  307. TrType := ftBlob;
  308. LensSet := True;
  309. TrLen := SQLLen;
  310. end;
  311. SQL_SHORT :
  312. TrType := ftInteger;
  313. SQL_LONG :
  314. begin
  315. LensSet := True;
  316. TrLen := 0;
  317. TrType := ftInteger;
  318. end;
  319. SQL_INT64 :
  320. TrType := ftLargeInt;
  321. SQL_DOUBLE :
  322. begin
  323. LensSet := True;
  324. TrLen := 0;
  325. TrType := ftFloat;
  326. end;
  327. SQL_FLOAT :
  328. begin
  329. LensSet := True;
  330. TrLen := 0;
  331. TrType := ftFloat;
  332. end
  333. else
  334. begin
  335. LensSet := True;
  336. TrLen := 0;
  337. TrType := ftUnknown;
  338. end;
  339. end;
  340. end;
  341. Function TIBConnection.AllocateCursorHandle : TSQLHandle;
  342. var curs : TIBCursor;
  343. begin
  344. curs := TIBCursor.create;
  345. curs.sqlda := nil;
  346. curs.statement := nil;
  347. AllocSQLDA(curs,1);
  348. result := curs;
  349. end;
  350. Function TIBConnection.AllocateTransactionHandle : TSQLHandle;
  351. begin
  352. result := TIBTrans.create;
  353. end;
  354. procedure TIBConnection.FreeStatement(cursor : TSQLHandle);
  355. begin
  356. with cursor as TIBcursor do
  357. begin
  358. if isc_dsql_free_statement(@Status, @Statement, DSQL_Drop) <> 0 then
  359. CheckError('FreeStatement', Status);
  360. Statement := nil;
  361. end;
  362. reAllocMem((cursor as tibcursor).SQLDA,0);
  363. end;
  364. procedure TIBConnection.PrepareStatement(cursor: TSQLHandle;ATransaction : TSQLTransaction;buf : string);
  365. var dh : pointer;
  366. tr : pointer;
  367. x : shortint;
  368. begin
  369. with cursor as TIBcursor do
  370. begin
  371. dh := GetHandle;
  372. if isc_dsql_allocate_statement(@Status, @dh, @Statement) <> 0 then
  373. CheckError('PrepareStatement', Status);
  374. tr := aTransaction.Handle;
  375. if isc_dsql_prepare(@Status, @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  376. CheckError('PrepareStatement', Status);
  377. if StatementType = stselect then
  378. begin
  379. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  380. CheckError('PrepareSelect', Status);
  381. if SQLDA^.SQLD > SQLDA^.SQLN then
  382. begin
  383. AllocSQLDA((cursor as TIBCursor),SQLDA^.SQLD);
  384. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  385. CheckError('PrepareSelect', Status);
  386. end;
  387. {$R-}
  388. SetLength(FFieldFlag,SQLDA^.SQLD);
  389. for x := 0 to SQLDA^.SQLD - 1 do with SQLDA^.SQLVar[x] do
  390. begin
  391. if ((SQLType and not 1) = SQL_VARYING) then
  392. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen+2)
  393. else
  394. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  395. SQLInd := @FFieldFlag[x];
  396. end;
  397. {$R+}
  398. end;
  399. end;
  400. end;
  401. procedure TIBConnection.FreeFldBuffers(cursor : TSQLHandle);
  402. var
  403. x : shortint;
  404. begin
  405. {$R-}
  406. with cursor as TIBCursor do
  407. for x := 0 to SQLDA^.SQLD - 1 do
  408. reAllocMem(SQLDA^.SQLVar[x].SQLData,0);
  409. {$R+}
  410. end;
  411. procedure TIBConnection.Execute(cursor: TSQLHandle;atransaction:tSQLtransaction);
  412. var tr : pointer;
  413. begin
  414. tr := aTransaction.Handle;
  415. with cursor as TIBCursor do
  416. if isc_dsql_execute(@Status, @tr, @Statement, 1, nil) <> 0 then
  417. CheckError('Execute', Status);
  418. end;
  419. procedure TIBConnection.AddFieldDefs(cursor: TSQLHandle;FieldDefs : TfieldDefs);
  420. var
  421. x : integer;
  422. lenset : boolean;
  423. TransLen : word;
  424. TransType : TFieldType;
  425. FD : TFieldDef;
  426. begin
  427. {$R-}
  428. with cursor as TIBCursor do
  429. begin
  430. for x := 0 to SQLDA^.SQLD - 1 do
  431. begin
  432. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].SQLLen, SQLDA^.SQLVar[x].SQLScale,
  433. lenset, TransType, TransLen);
  434. FD := TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].SQLName, TransType,
  435. TransLen, False, (x + 1));
  436. if TransType = ftBCD then FD.precision := SQLDA^.SQLVar[x].SQLLen;
  437. FD.DisplayName := SQLDA^.SQLVar[x].AliasName;
  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].AliasName = FieldDef.Name then break;
  472. if SQLDA^.SQLVar[x].AliasName <> 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. function TIBConnection.GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  550. var s : string;
  551. begin
  552. case SchemaType of
  553. stTables : s := 'select '+
  554. 'rdb$relation_id as recno, '+
  555. '''' + DatabaseName + ''' as catalog_name, '+
  556. ''''' as schema_name, '+
  557. 'rdb$relation_name as table_name, '+
  558. '0 as table_type '+
  559. 'from '+
  560. 'rdb$relations '+
  561. 'where '+
  562. '(rdb$system_flag = 0 or rdb$system_flag is null)'; // and rdb$view_blr is null
  563. stSysTables : s := 'select '+
  564. 'rdb$relation_id as recno, '+
  565. '''' + DatabaseName + ''' as catalog_name, '+
  566. ''''' as schema_name, '+
  567. 'rdb$relation_name as table_name, '+
  568. '0 as table_type '+
  569. 'from '+
  570. 'rdb$relations '+
  571. 'where '+
  572. '(rdb$system_flag > 0)'; // and rdb$view_blr is null
  573. stProcedures : s := 'select '+
  574. 'rdb$procedure_id as recno, '+
  575. '''' + DatabaseName + ''' as catalog_name, '+
  576. ''''' as schema_name, '+
  577. 'rdb$procedure_name as proc_name, '+
  578. '0 as proc_type, '+
  579. 'rdb$procedure_inputs as in_params, '+
  580. 'rdb$procedure_outputs as out_params '+
  581. 'from '+
  582. 'rdb$procedures '+
  583. 'WHERE '+
  584. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  585. stColumns : s := 'select '+
  586. 'rdb$procedure_id as recno, '+
  587. '''' + DatabaseName + ''' as catalog_name, '+
  588. ''''' as schema_name, '+
  589. 'rdb$relation_name as table_name, '+
  590. 'rdb$field_name as column name, '+
  591. 'rdb$field_position as column_position, '+
  592. '0 as column_type, '+
  593. '0 as column_datatype, '+
  594. ''''' as column_typename, '+
  595. '0 as column_subtype, '+
  596. '0 as column_precision, '+
  597. '0 as column_scale, '+
  598. '0 as column_length, '+
  599. '0 as column_nullable '+
  600. 'from '+
  601. 'rdb$relation_fields '+
  602. 'WHERE '+
  603. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  604. else
  605. DatabaseError(SMetadataUnavailable)
  606. end; {case}
  607. result := s;
  608. end;
  609. procedure TIBConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  610. var qry : TSQLQuery;
  611. begin
  612. if not assigned(Transaction) then
  613. DatabaseError(SErrConnTransactionnSet);
  614. qry := tsqlquery.Create(nil);
  615. qry.transaction := Transaction;
  616. qry.database := Self;
  617. with qry do
  618. begin
  619. ReadOnly := True;
  620. sql.clear;
  621. sql.add('select '+
  622. 'ind.rdb$index_name, '+
  623. 'ind.rdb$relation_name, '+
  624. 'ind.rdb$unique_flag, '+
  625. 'ind_seg.rdb$field_name, '+
  626. 'rel_con.rdb$constraint_type '+
  627. 'from '+
  628. 'rdb$index_segments ind_seg, '+
  629. 'rdb$indices ind '+
  630. 'left outer join '+
  631. 'rdb$relation_constraints rel_con '+
  632. 'on '+
  633. 'rel_con.rdb$index_name = ind.rdb$index_name '+
  634. 'where '+
  635. '(ind_seg.rdb$index_name = ind.rdb$index_name) and '+
  636. '(ind.rdb$relation_name=''' + UpperCase(TableName) +''') '+
  637. 'order by '+
  638. 'ind.rdb$index_name;');
  639. open;
  640. end;
  641. while not qry.eof do with IndexDefs.AddIndexDef do
  642. begin
  643. Name := trim(qry.fields[0].asstring);
  644. Fields := trim(qry.Fields[3].asstring);
  645. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  646. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  647. qry.next;
  648. while (name = qry.fields[0].asstring) and (not qry.eof) do
  649. begin
  650. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  651. qry.next;
  652. end;
  653. end;
  654. qry.close;
  655. qry.free;
  656. end;
  657. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  658. var
  659. Ext : extended;
  660. Dbl : double;
  661. Sin : single;
  662. begin
  663. case Field.Size of
  664. 4 :
  665. begin
  666. Move(CurrBuff^, Sin, 4);
  667. Dbl := Sin;
  668. end;
  669. 8 :
  670. begin
  671. Move(CurrBuff^, Dbl, 8);
  672. end;
  673. 10:
  674. begin
  675. Move(CurrBuff^, Ext, 10);
  676. Dbl := double(Ext);
  677. end;
  678. end;
  679. Move(Dbl, Buffer^, 8);
  680. end;
  681. end.