ibconnection.pp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. function getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  51. protected
  52. procedure DoInternalConnect; override;
  53. procedure DoInternalDisconnect; override;
  54. function GetHandle : pointer; override;
  55. Function AllocateCursorHandle : TSQLHandle; override;
  56. Function AllocateTransactionHandle : TSQLHandle; override;
  57. procedure FreeStatement(cursor : TSQLHandle); override;
  58. procedure PrepareStatement(cursor: TSQLHandle;ATransaction : TSQLTransaction;buf : string); override;
  59. procedure FreeFldBuffers(cursor : TSQLHandle); override;
  60. procedure Execute(cursor: TSQLHandle;atransaction:tSQLtransaction); override;
  61. procedure AddFieldDefs(cursor: TSQLHandle;FieldDefs : TfieldDefs); override;
  62. function Fetch(cursor : TSQLHandle) : boolean; override;
  63. function LoadField(cursor : TSQLHandle;FieldDef : TfieldDef;buffer : pointer) : boolean; override;
  64. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  65. function Commit(trans : TSQLHandle) : boolean; override;
  66. function RollBack(trans : TSQLHandle) : boolean; override;
  67. function StartdbTransaction(trans : TSQLHandle) : boolean; override;
  68. procedure CommitRetaining(trans : TSQLHandle); override;
  69. procedure RollBackRetaining(trans : TSQLHandle); override;
  70. procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
  71. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  72. function CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; override;
  73. published
  74. property Dialect : integer read FDialect write FDialect;
  75. property DatabaseName;
  76. property KeepConnection;
  77. property LoginPrompt;
  78. property Params;
  79. property OnLogin;
  80. end;
  81. implementation
  82. resourcestring
  83. SErrNoDatabaseName = 'Database connect string (DatabaseName) not filled in!';
  84. type
  85. TTm = packed record
  86. tm_sec : longint;
  87. tm_min : longint;
  88. tm_hour : longint;
  89. tm_mday : longint;
  90. tm_mon : longint;
  91. tm_year : longint;
  92. tm_wday : longint;
  93. tm_yday : longint;
  94. tm_isdst : longint;
  95. __tm_gmtoff : longint;
  96. __tm_zone : Pchar;
  97. end;
  98. procedure TIBConnection.CheckError(ProcName : string; Status : array of ISC_STATUS);
  99. var
  100. buf : array [0..1024] of char;
  101. p : pointer;
  102. Msg : string;
  103. begin
  104. if ((Status[0] = 1) and (Status[1] <> 0)) then
  105. begin
  106. p := @Status;
  107. msg := '';
  108. while isc_interprete(Buf, @p) > 0 do
  109. Msg := Msg + #10' -' + StrPas(Buf);
  110. DatabaseError(ProcName + ': ' + Msg,self);
  111. end;
  112. end;
  113. procedure TIBConnection.SetTPB(trans : TIBtrans);
  114. begin
  115. with trans do
  116. begin
  117. TPB := chr(isc_tpb_version3);
  118. case AccessMode of
  119. amReadWrite : TPB := TPB + chr(isc_tpb_write);
  120. amReadOnly : TPB := TPB + chr(isc_tpb_read);
  121. end;
  122. case IsolationLevel of
  123. ilConsistent : TPB := TPB + chr(isc_tpb_consistency);
  124. ilConcurrent : TPB := TPB + chr(isc_tpb_concurrency);
  125. ilReadCommittedRecV : TPB := TPB + chr(isc_tpb_read_committed) +
  126. chr(isc_tpb_rec_version);
  127. ilReadCommitted : TPB := TPB + chr(isc_tpb_read_committed) +
  128. chr(isc_tpb_no_rec_version);
  129. end;
  130. case LockResolution of
  131. lrWait : TPB := TPB + chr(isc_tpb_wait);
  132. lrNoWait : TPB := TPB + chr(isc_tpb_nowait);
  133. end;
  134. case TableReservation of
  135. trSharedLockRead : TPB := TPB + chr(isc_tpb_shared) +
  136. chr(isc_tpb_lock_read);
  137. trSharedLockWrite : TPB := TPB + chr(isc_tpb_shared) +
  138. chr(isc_tpb_lock_write);
  139. trProtectedLockRead : TPB := TPB + chr(isc_tpb_protected) +
  140. chr(isc_tpb_lock_read);
  141. trProtectedLockWrite : TPB := TPB + chr(isc_tpb_protected) +
  142. chr(isc_tpb_lock_write);
  143. end;
  144. end;
  145. end;
  146. function TIBConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  147. begin
  148. Result := (trans as TIBtrans).TransactionHandle;
  149. end;
  150. function TIBConnection.Commit(trans : TSQLHandle) : boolean;
  151. begin
  152. result := false;
  153. with (trans as TIBTrans) do
  154. if isc_commit_transaction(@Status, @TransactionHandle) <> 0 then
  155. CheckError('Commit', Status)
  156. else result := true;
  157. end;
  158. function TIBConnection.RollBack(trans : TSQLHandle) : boolean;
  159. begin
  160. result := false;
  161. if isc_rollback_transaction(@TIBTrans(trans).Status, @TIBTrans(trans).TransactionHandle) <> 0 then
  162. CheckError('Rollback', TIBTrans(trans).Status)
  163. else result := true;
  164. end;
  165. function TIBConnection.StartDBTransaction(trans : TSQLHandle) : boolean;
  166. var
  167. DBHandle : pointer;
  168. tr : TIBTrans;
  169. begin
  170. result := false;
  171. DBHandle := GetHandle;
  172. tr := trans as TIBtrans;
  173. SetTPB(tr);
  174. with tr do
  175. begin
  176. TransactionHandle := nil;
  177. if isc_start_transaction(@Status, @TransactionHandle, 1,
  178. [@DBHandle, Length(TPB), @TPB[1]]) <> 0 then
  179. CheckError('StartTransaction',Status)
  180. else Result := True;
  181. end;
  182. end;
  183. procedure TIBConnection.CommitRetaining(trans : TSQLHandle);
  184. begin
  185. with trans as TIBtrans do
  186. if isc_commit_retaining(@Status, @TransactionHandle) <> 0 then
  187. CheckError('CommitRetaining', Status);
  188. end;
  189. procedure TIBConnection.RollBackRetaining(trans : TSQLHandle);
  190. begin
  191. with trans as TIBtrans do
  192. if isc_rollback_retaining(@Status, @TransactionHandle) <> 0 then
  193. CheckError('RollBackRetaining', Status);
  194. end;
  195. procedure TIBConnection.DoInternalConnect;
  196. var
  197. DPB : string;
  198. begin
  199. {$IfDef LinkDynamically}
  200. InitialiseIBase60;
  201. {$EndIf}
  202. inherited dointernalconnect;
  203. DPB := chr(isc_dpb_version1);
  204. if (UserName <> '') then
  205. begin
  206. DPB := DPB + chr(isc_dpb_user_name) + chr(Length(UserName)) + UserName;
  207. if (Password <> '') then
  208. DPB := DPB + chr(isc_dpb_password) + chr(Length(Password)) + Password;
  209. end;
  210. if (Role <> '') then
  211. DPB := DPB + chr(isc_dpb_sql_role_name) + chr(Length(Role)) + Role;
  212. if Length(CharSet) > 0 then
  213. DPB := DPB + Chr(isc_dpb_lc_ctype) + Chr(Length(CharSet)) + CharSet;
  214. if (DatabaseName = '') then
  215. DatabaseError(SErrNoDatabaseName,self);
  216. FSQLDatabaseHandle := nil;
  217. if isc_attach_database(@FStatus, Length(DatabaseName), @DatabaseName[1], @FSQLDatabaseHandle,
  218. Length(DPB), @DPB[1]) <> 0 then
  219. CheckError('DoInternalConnect', FStatus);
  220. SetDBDialect;
  221. end;
  222. procedure TIBConnection.DoInternalDisconnect;
  223. begin
  224. if not Connected then
  225. begin
  226. FSQLDatabaseHandle := nil;
  227. Exit;
  228. end;
  229. isc_detach_database(@FStatus[0], @FSQLDatabaseHandle);
  230. CheckError('Close', FStatus);
  231. {$IfDef LinkDynamically}
  232. ReleaseIBase60;
  233. {$EndIf}
  234. end;
  235. procedure TIBConnection.SetDBDialect;
  236. var
  237. x : integer;
  238. Len : integer;
  239. Buffer : string;
  240. ResBuf : array [0..39] of byte;
  241. begin
  242. Buffer := Chr(isc_info_db_sql_dialect) + Chr(isc_info_end);
  243. if isc_database_info(@FStatus, @FSQLDatabaseHandle, Length(Buffer),
  244. @Buffer[1], SizeOf(ResBuf), @ResBuf) <> 0 then
  245. CheckError('SetDBDialect', FStatus);
  246. x := 0;
  247. while x < 40 do
  248. case ResBuf[x] of
  249. isc_info_db_sql_dialect :
  250. begin
  251. Inc(x);
  252. Len := isc_vax_integer(@ResBuf[x], 2);
  253. Inc(x, 2);
  254. FDialect := isc_vax_integer(@ResBuf[x], Len);
  255. Inc(x, Len);
  256. end;
  257. isc_info_end : Break;
  258. end;
  259. end;
  260. procedure TIBConnection.AllocSQLDA(Cursor : TIBcursor;Count : integer);
  261. begin
  262. with cursor as TIBCursor do
  263. begin
  264. reAllocMem(SQLDA, XSQLDA_Length(Count));
  265. { Zero out the memory block to avoid problems with exceptions within the
  266. constructor of this class. }
  267. FillChar(SQLDA^, XSQLDA_Length(Count), 0);
  268. SQLDA^.Version := sqlda_version1;
  269. SQLDA^.SQLN := Count;
  270. end;
  271. end;
  272. procedure TIBConnection.TranslateFldType(SQLType, SQLLen, SQLScale : integer; var LensSet : boolean;
  273. var TrType : TFieldType; var TrLen : word);
  274. begin
  275. LensSet := False;
  276. if (SQLScale >= -4) and (SQLScale <= -1) then //in [-4..-1] then
  277. begin
  278. LensSet := True;
  279. TrLen := SQLLen;
  280. TrType := ftBCD
  281. end
  282. else case (SQLType and not 1) of
  283. SQL_VARYING :
  284. begin
  285. LensSet := True;
  286. TrType := ftString;
  287. TrLen := SQLLen;
  288. end;
  289. SQL_TEXT :
  290. begin
  291. LensSet := True;
  292. TrType := ftString;
  293. TrLen := SQLLen;
  294. end;
  295. SQL_TYPE_DATE :
  296. TrType := ftDate{Time};
  297. SQL_TYPE_TIME :
  298. TrType := ftDateTime;
  299. SQL_TIMESTAMP :
  300. TrType := ftDateTime;
  301. SQL_ARRAY :
  302. begin
  303. TrType := ftArray;
  304. LensSet := true;
  305. TrLen := SQLLen;
  306. end;
  307. SQL_BLOB :
  308. begin
  309. TrType := ftBlob;
  310. LensSet := True;
  311. TrLen := SQLLen;
  312. end;
  313. SQL_SHORT :
  314. TrType := ftInteger;
  315. SQL_LONG :
  316. begin
  317. LensSet := True;
  318. TrLen := 0;
  319. TrType := ftInteger;
  320. end;
  321. SQL_INT64 :
  322. TrType := ftLargeInt;
  323. SQL_DOUBLE :
  324. begin
  325. LensSet := True;
  326. TrLen := SQLLen;
  327. TrType := ftFloat;
  328. end;
  329. SQL_FLOAT :
  330. begin
  331. LensSet := True;
  332. TrLen := SQLLen;
  333. TrType := ftFloat;
  334. end
  335. else
  336. begin
  337. LensSet := True;
  338. TrLen := 0;
  339. TrType := ftUnknown;
  340. end;
  341. end;
  342. end;
  343. Function TIBConnection.AllocateCursorHandle : TSQLHandle;
  344. var curs : TIBCursor;
  345. begin
  346. curs := TIBCursor.create;
  347. curs.sqlda := nil;
  348. curs.statement := nil;
  349. AllocSQLDA(curs,1);
  350. result := curs;
  351. end;
  352. Function TIBConnection.AllocateTransactionHandle : TSQLHandle;
  353. begin
  354. result := TIBTrans.create;
  355. end;
  356. procedure TIBConnection.FreeStatement(cursor : TSQLHandle);
  357. begin
  358. with cursor as TIBcursor do
  359. begin
  360. if isc_dsql_free_statement(@Status, @Statement, DSQL_Drop) <> 0 then
  361. CheckError('FreeStatement', Status);
  362. Statement := nil;
  363. end;
  364. reAllocMem((cursor as tibcursor).SQLDA,0);
  365. end;
  366. procedure TIBConnection.PrepareStatement(cursor: TSQLHandle;ATransaction : TSQLTransaction;buf : string);
  367. var dh : pointer;
  368. tr : pointer;
  369. x : shortint;
  370. begin
  371. with cursor as TIBcursor do
  372. begin
  373. dh := GetHandle;
  374. if isc_dsql_allocate_statement(@Status, @dh, @Statement) <> 0 then
  375. CheckError('PrepareStatement', Status);
  376. tr := aTransaction.Handle;
  377. if isc_dsql_prepare(@Status, @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  378. CheckError('PrepareStatement', Status);
  379. if StatementType = stselect then
  380. begin
  381. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  382. CheckError('PrepareSelect', Status);
  383. if SQLDA^.SQLD > SQLDA^.SQLN then
  384. begin
  385. AllocSQLDA((cursor as TIBCursor),SQLDA^.SQLD);
  386. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  387. CheckError('PrepareSelect', Status);
  388. end;
  389. {$R-}
  390. SetLength(FFieldFlag,SQLDA^.SQLD);
  391. for x := 0 to SQLDA^.SQLD - 1 do with SQLDA^.SQLVar[x] do
  392. begin
  393. if ((SQLType and not 1) = SQL_VARYING) then
  394. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen+2)
  395. else
  396. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  397. SQLInd := @FFieldFlag[x];
  398. end;
  399. {$R+}
  400. end;
  401. end;
  402. end;
  403. procedure TIBConnection.FreeFldBuffers(cursor : TSQLHandle);
  404. var
  405. x : shortint;
  406. begin
  407. {$R-}
  408. with cursor as TIBCursor do
  409. for x := 0 to SQLDA^.SQLD - 1 do
  410. reAllocMem(SQLDA^.SQLVar[x].SQLData,0);
  411. {$R+}
  412. end;
  413. procedure TIBConnection.Execute(cursor: TSQLHandle;atransaction:tSQLtransaction);
  414. var tr : pointer;
  415. begin
  416. tr := aTransaction.Handle;
  417. with cursor as TIBCursor do
  418. if isc_dsql_execute(@Status, @tr, @Statement, 1, nil) <> 0 then
  419. CheckError('Execute', Status);
  420. end;
  421. procedure TIBConnection.AddFieldDefs(cursor: TSQLHandle;FieldDefs : TfieldDefs);
  422. var
  423. x : integer;
  424. lenset : boolean;
  425. TransLen : word;
  426. TransType : TFieldType;
  427. FD : TFieldDef;
  428. begin
  429. {$R-}
  430. with cursor as TIBCursor do
  431. begin
  432. for x := 0 to SQLDA^.SQLD - 1 do
  433. begin
  434. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].SQLLen, SQLDA^.SQLVar[x].SQLScale,
  435. lenset, TransType, TransLen);
  436. FD := TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].AliasName, TransType,
  437. TransLen, False, (x + 1));
  438. if TransType = ftBCD then FD.precision := SQLDA^.SQLVar[x].SQLLen;
  439. FD.DisplayName := SQLDA^.SQLVar[x].AliasName;
  440. end;
  441. end;
  442. {$R+}
  443. end;
  444. function TIBConnection.GetHandle: pointer;
  445. begin
  446. Result := FSQLDatabaseHandle;
  447. end;
  448. function TIBConnection.Fetch(cursor : TSQLHandle) : boolean;
  449. var
  450. retcode : integer;
  451. begin
  452. with cursor as TIBCursor do
  453. begin
  454. retcode := isc_dsql_fetch(@Status, @Statement, 1, SQLDA);
  455. if (retcode <> 0) and (retcode <> 100) then
  456. CheckError('Fetch', Status);
  457. end;
  458. Result := (retcode <> 100);
  459. end;
  460. function TIBConnection.LoadField(cursor : TSQLHandle;FieldDef : TfieldDef;buffer : pointer) : boolean;
  461. var
  462. x : integer;
  463. VarcharLen : word;
  464. CurrBuff : pchar;
  465. b : longint;
  466. li : largeint;
  467. c : currency;
  468. begin
  469. with cursor as TIBCursor do
  470. begin
  471. {$R-}
  472. for x := 0 to SQLDA^.SQLD - 1 do
  473. if SQLDA^.SQLVar[x].AliasName = FieldDef.Name then break;
  474. if SQLDA^.SQLVar[x].AliasName <> FieldDef.Name then
  475. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  476. if SQLDA^.SQLVar[x].SQLInd^ = -1 then
  477. result := false
  478. else
  479. begin
  480. with SQLDA^.SQLVar[x] do
  481. if ((SQLType and not 1) = SQL_VARYING) then
  482. begin
  483. Move(SQLData^, VarcharLen, 2);
  484. CurrBuff := SQLData + 2;
  485. end
  486. else
  487. begin
  488. CurrBuff := SQLData;
  489. VarCharLen := SQLDA^.SQLVar[x].SQLLen;
  490. end;
  491. Result := true;
  492. case FieldDef.DataType of
  493. ftBCD :
  494. begin
  495. c := 0;
  496. Move(CurrBuff^, c, SQLDA^.SQLVar[x].SQLLen);
  497. c := c*intpower(10,4+SQLDA^.SQLVar[x].SQLScale);
  498. Move(c, buffer^ , sizeof(c));
  499. end;
  500. ftInteger :
  501. begin
  502. b := 0;
  503. Move(b, Buffer^, sizeof(longint));
  504. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  505. end;
  506. ftLargeint :
  507. begin
  508. li := 0;
  509. Move(CurrBuff^, li, SQLDA^.SQLVar[x].SQLLen);
  510. if SQLDA^.SQLVar[x].SQLScale > 0 then
  511. li := li * trunc(intpower(10, SQLDA^.SQLVar[x].SQLScale))
  512. else if SQLDA^.SQLVar[x].SQLScale < 0 then
  513. li := li div trunc(intpower(10, -SQLDA^.SQLVar[x].SQLScale));
  514. Move(li, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  515. end;
  516. ftDate, ftTime, ftDateTime:
  517. GetDateTime(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLType);
  518. ftString :
  519. begin
  520. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  521. PChar(Buffer + VarCharLen)^ := #0;
  522. end;
  523. ftFloat :
  524. GetFloat(CurrBuff, Buffer, FieldDef);
  525. ftBlob : begin // load the BlobIb in field's buffer
  526. li := 0;
  527. Move(li, Buffer^, sizeof(largeint));
  528. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  529. end
  530. else result := false;
  531. end;
  532. end;
  533. {$R+}
  534. end;
  535. end;
  536. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  537. var
  538. CTime : TTm; // C struct time
  539. STime : TSystemTime; // System time
  540. PTime : TDateTime; // Pascal time
  541. begin
  542. case (AType and not 1) of
  543. SQL_TYPE_DATE :
  544. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  545. SQL_TYPE_TIME :
  546. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  547. SQL_TIMESTAMP :
  548. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  549. end;
  550. STime.Year := CTime.tm_year + 1900;
  551. STime.Month := CTime.tm_mon + 1;
  552. STime.Day := CTime.tm_mday;
  553. STime.Hour := CTime.tm_hour;
  554. STime.Minute := CTime.tm_min;
  555. STime.Second := CTime.tm_sec;
  556. STime.Millisecond := 0;
  557. PTime := SystemTimeToDateTime(STime);
  558. Move(PTime, Buffer^, SizeOf(PTime));
  559. end;
  560. function TIBConnection.GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  561. var s : string;
  562. begin
  563. case SchemaType of
  564. stTables : s := 'select '+
  565. 'rdb$relation_id as recno, '+
  566. '''' + DatabaseName + ''' as catalog_name, '+
  567. ''''' as schema_name, '+
  568. 'rdb$relation_name as table_name, '+
  569. '0 as table_type '+
  570. 'from '+
  571. 'rdb$relations '+
  572. 'where '+
  573. '(rdb$system_flag = 0 or rdb$system_flag is null)'; // and rdb$view_blr is null
  574. stSysTables : s := 'select '+
  575. 'rdb$relation_id as recno, '+
  576. '''' + DatabaseName + ''' as catalog_name, '+
  577. ''''' as schema_name, '+
  578. 'rdb$relation_name as table_name, '+
  579. '0 as table_type '+
  580. 'from '+
  581. 'rdb$relations '+
  582. 'where '+
  583. '(rdb$system_flag > 0)'; // and rdb$view_blr is null
  584. stProcedures : s := 'select '+
  585. 'rdb$procedure_id as recno, '+
  586. '''' + DatabaseName + ''' as catalog_name, '+
  587. ''''' as schema_name, '+
  588. 'rdb$procedure_name as proc_name, '+
  589. '0 as proc_type, '+
  590. 'rdb$procedure_inputs as in_params, '+
  591. 'rdb$procedure_outputs as out_params '+
  592. 'from '+
  593. 'rdb$procedures '+
  594. 'WHERE '+
  595. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  596. stColumns : s := 'select '+
  597. 'rdb$procedure_id as recno, '+
  598. '''' + DatabaseName + ''' as catalog_name, '+
  599. ''''' as schema_name, '+
  600. 'rdb$relation_name as table_name, '+
  601. 'rdb$field_name as column name, '+
  602. 'rdb$field_position as column_position, '+
  603. '0 as column_type, '+
  604. '0 as column_datatype, '+
  605. ''''' as column_typename, '+
  606. '0 as column_subtype, '+
  607. '0 as column_precision, '+
  608. '0 as column_scale, '+
  609. '0 as column_length, '+
  610. '0 as column_nullable '+
  611. 'from '+
  612. 'rdb$relation_fields '+
  613. 'WHERE '+
  614. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  615. else
  616. DatabaseError(SMetadataUnavailable)
  617. end; {case}
  618. result := s;
  619. end;
  620. procedure TIBConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  621. var qry : TSQLQuery;
  622. begin
  623. if not assigned(Transaction) then
  624. DatabaseError(SErrConnTransactionnSet);
  625. qry := tsqlquery.Create(nil);
  626. qry.transaction := Transaction;
  627. qry.database := Self;
  628. with qry do
  629. begin
  630. ReadOnly := True;
  631. sql.clear;
  632. sql.add('select '+
  633. 'ind.rdb$index_name, '+
  634. 'ind.rdb$relation_name, '+
  635. 'ind.rdb$unique_flag, '+
  636. 'ind_seg.rdb$field_name, '+
  637. 'rel_con.rdb$constraint_type '+
  638. 'from '+
  639. 'rdb$index_segments ind_seg, '+
  640. 'rdb$indices ind '+
  641. 'left outer join '+
  642. 'rdb$relation_constraints rel_con '+
  643. 'on '+
  644. 'rel_con.rdb$index_name = ind.rdb$index_name '+
  645. 'where '+
  646. '(ind_seg.rdb$index_name = ind.rdb$index_name) and '+
  647. '(ind.rdb$relation_name=''' + UpperCase(TableName) +''') '+
  648. 'order by '+
  649. 'ind.rdb$index_name;');
  650. open;
  651. end;
  652. while not qry.eof do with IndexDefs.AddIndexDef do
  653. begin
  654. Name := trim(qry.fields[0].asstring);
  655. Fields := trim(qry.Fields[3].asstring);
  656. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  657. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  658. qry.next;
  659. while (name = qry.fields[0].asstring) and (not qry.eof) do
  660. begin
  661. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  662. qry.next;
  663. end;
  664. end;
  665. qry.close;
  666. qry.free;
  667. end;
  668. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  669. var
  670. Ext : extended;
  671. Dbl : double;
  672. Sin : single;
  673. begin
  674. case Field.Size of
  675. 4 :
  676. begin
  677. Move(CurrBuff^, Sin, 4);
  678. Dbl := Sin;
  679. end;
  680. 8 :
  681. begin
  682. Move(CurrBuff^, Dbl, 8);
  683. end;
  684. 10:
  685. begin
  686. Move(CurrBuff^, Ext, 10);
  687. Dbl := double(Ext);
  688. end;
  689. end;
  690. Move(Dbl, Buffer^, 8);
  691. end;
  692. function TIBConnection.getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  693. var
  694. iscInfoBlobMaxSegment : byte = isc_info_blob_max_segment;
  695. blobInfo : array[0..50] of byte;
  696. begin
  697. if isc_blob_info(@Fstatus, @blobHandle, sizeof(iscInfoBlobMaxSegment), @iscInfoBlobMaxSegment, sizeof(blobInfo) - 2, @blobInfo) <> 0 then
  698. CheckError('isc_blob_info', FStatus);
  699. if blobInfo[0] = isc_info_blob_max_segment then
  700. begin
  701. result := isc_vax_integer(pchar(@blobInfo[3]), isc_vax_integer(pchar(@blobInfo[1]), 2));
  702. end
  703. else
  704. CheckError('isc_blob_info', FStatus);
  705. end;
  706. function TIBConnection.CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream;
  707. const
  708. isc_segstr_eof = 335544367; // It's not defined in ibase60 but in ibase40. Would it be better to define in ibase60?
  709. var
  710. mStream : TMemoryStream;
  711. blobHandle : Isc_blob_Handle;
  712. blobSegment : pointer;
  713. blobSegLen : smallint;
  714. maxBlobSize : longInt;
  715. TransactionHandle : pointer;
  716. blobId : ISC_QUAD;
  717. begin
  718. result := nil;
  719. if mode = bmRead then begin
  720. if not field.getData(@blobId) then
  721. exit;
  722. TransactionHandle := transaction.Handle;
  723. blobHandle := nil;
  724. if isc_open_blob(@FStatus, @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, @blobId) <> 0 then
  725. CheckError('TIBConnection.CreateBlobStream', FStatus);
  726. maxBlobSize := getMaxBlobSize(blobHandle);
  727. blobSegment := AllocMem(maxBlobSize);
  728. mStream := TMemoryStream.create;
  729. while (isc_get_segment(@FStatus, @blobHandle, @blobSegLen, maxBlobSize, blobSegment) = 0) do begin
  730. mStream.writeBuffer(blobSegment^, blobSegLen);
  731. end;
  732. freemem(blobSegment);
  733. mStream.seek(0,soFromBeginning);
  734. if FStatus[1] = isc_segstr_eof then
  735. begin
  736. if isc_close_blob(@FStatus, @blobHandle) <> 0 then
  737. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  738. end
  739. else
  740. CheckError('TIBConnection.CreateBlobStream isc_get_segment', FStatus);
  741. result := mStream;
  742. end;
  743. end;
  744. end.