ibconnection.pp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. TIBCursor = Class(TSQLCursor)
  14. protected
  15. Status : array [0..19] of ISC_STATUS;
  16. Statement : pointer;
  17. SQLDA : PXSQLDA;
  18. in_SQLDA : PXSQLDA;
  19. ParamBinding : array of integer;
  20. end;
  21. TIBTrans = Class(TSQLHandle)
  22. protected
  23. TransactionHandle : pointer;
  24. TPB : string; // Transaction parameter buffer
  25. Status : array [0..19] of ISC_STATUS;
  26. end;
  27. TIBConnection = class (TSQLConnection)
  28. private
  29. FSQLDatabaseHandle : pointer;
  30. FStatus : array [0..19] of ISC_STATUS;
  31. FDialect : integer;
  32. procedure SetDBDialect;
  33. procedure AllocSQLDA(var aSQLDA : PXSQLDA;Count : integer);
  34. procedure TranslateFldType(SQLType, SQLLen, SQLScale : integer; var LensSet : boolean;
  35. var TrType : TFieldType; var TrLen : word);
  36. // conversion methods
  37. procedure GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  38. procedure SetDateTime(CurrBuff: pointer; PTime : TDateTime; AType : integer);
  39. procedure GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  40. procedure CheckError(ProcName : string; Status : array of ISC_STATUS);
  41. function getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  42. procedure SetParameters(cursor : TSQLCursor;AParams : TParams);
  43. procedure FreeSQLDABuffer(var aSQLDA : PXSQLDA);
  44. protected
  45. procedure DoInternalConnect; override;
  46. procedure DoInternalDisconnect; override;
  47. function GetHandle : pointer; override;
  48. Function AllocateCursorHandle : TSQLCursor; override;
  49. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  50. Function AllocateTransactionHandle : TSQLHandle; override;
  51. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  52. procedure UnPrepareStatement(cursor : TSQLCursor); override;
  53. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  54. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
  55. procedure AddFieldDefs(cursor: TSQLCursor;FieldDefs : TfieldDefs); override;
  56. function Fetch(cursor : TSQLCursor) : boolean; override;
  57. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean; override;
  58. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  59. function Commit(trans : TSQLHandle) : boolean; override;
  60. function RollBack(trans : TSQLHandle) : boolean; override;
  61. function StartdbTransaction(trans : TSQLHandle; AParams : string) : boolean; override;
  62. procedure CommitRetaining(trans : TSQLHandle); override;
  63. procedure RollBackRetaining(trans : TSQLHandle); override;
  64. procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
  65. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  66. function CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; override;
  67. public
  68. constructor Create(AOwner : TComponent); override;
  69. published
  70. property Dialect : integer read FDialect write FDialect;
  71. property DatabaseName;
  72. property KeepConnection;
  73. property LoginPrompt;
  74. property Params;
  75. property OnLogin;
  76. end;
  77. implementation
  78. uses strutils;
  79. type
  80. TTm = packed record
  81. tm_sec : longint;
  82. tm_min : longint;
  83. tm_hour : longint;
  84. tm_mday : longint;
  85. tm_mon : longint;
  86. tm_year : longint;
  87. tm_wday : longint;
  88. tm_yday : longint;
  89. tm_isdst : longint;
  90. __tm_gmtoff : longint;
  91. __tm_zone : Pchar;
  92. end;
  93. procedure TIBConnection.CheckError(ProcName : string; Status : array of ISC_STATUS);
  94. var
  95. buf : array [0..1024] of char;
  96. p : pointer;
  97. Msg : string;
  98. begin
  99. if ((Status[0] = 1) and (Status[1] <> 0)) then
  100. begin
  101. p := @Status;
  102. msg := '';
  103. while isc_interprete(Buf, @p) > 0 do
  104. Msg := Msg + #10' -' + StrPas(Buf);
  105. DatabaseError(ProcName + ': ' + Msg,self);
  106. end;
  107. end;
  108. constructor TIBConnection.Create(AOwner : TComponent);
  109. begin
  110. inherited;
  111. FConnOptions := FConnOptions + [sqSupportParams];
  112. end;
  113. function TIBConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  114. begin
  115. Result := (trans as TIBtrans).TransactionHandle;
  116. end;
  117. function TIBConnection.Commit(trans : TSQLHandle) : boolean;
  118. begin
  119. result := false;
  120. with (trans as TIBTrans) do
  121. if isc_commit_transaction(@Status, @TransactionHandle) <> 0 then
  122. CheckError('Commit', Status)
  123. else result := true;
  124. end;
  125. function TIBConnection.RollBack(trans : TSQLHandle) : boolean;
  126. begin
  127. result := false;
  128. if isc_rollback_transaction(@TIBTrans(trans).Status, @TIBTrans(trans).TransactionHandle) <> 0 then
  129. CheckError('Rollback', TIBTrans(trans).Status)
  130. else result := true;
  131. end;
  132. function TIBConnection.StartDBTransaction(trans : TSQLHandle;AParams : String) : boolean;
  133. var
  134. DBHandle : pointer;
  135. tr : TIBTrans;
  136. i : integer;
  137. s : string;
  138. begin
  139. result := false;
  140. DBHandle := GetHandle;
  141. tr := trans as TIBtrans;
  142. with tr do
  143. begin
  144. TPB := chr(isc_tpb_version3);
  145. i := 1;
  146. s := ExtractSubStr(AParams,i,stdWordDelims);
  147. while s <> '' do
  148. begin
  149. if s='isc_tpb_write' then TPB := TPB + chr(isc_tpb_write)
  150. else if s='isc_tpb_read' then TPB := TPB + chr(isc_tpb_read)
  151. else if s='isc_tpb_consistency' then TPB := TPB + chr(isc_tpb_consistency)
  152. else if s='isc_tpb_concurrency' then TPB := TPB + chr(isc_tpb_concurrency)
  153. else if s='isc_tpb_read_committed' then TPB := TPB + chr(isc_tpb_read_committed)
  154. else if s='isc_tpb_rec_version' then TPB := TPB + chr(isc_tpb_rec_version)
  155. else if s='isc_tpb_no_rec_version' then TPB := TPB + chr(isc_tpb_no_rec_version)
  156. else if s='isc_tpb_wait' then TPB := TPB + chr(isc_tpb_wait)
  157. else if s='isc_tpb_nowait' then TPB := TPB + chr(isc_tpb_nowait)
  158. else if s='isc_tpb_shared' then TPB := TPB + chr(isc_tpb_shared)
  159. else if s='isc_tpb_protected' then TPB := TPB + chr(isc_tpb_protected)
  160. else if s='isc_tpb_exclusive' then TPB := TPB + chr(isc_tpb_exclusive)
  161. else if s='isc_tpb_lock_read' then TPB := TPB + chr(isc_tpb_lock_read)
  162. else if s='isc_tpb_lock_write' then TPB := TPB + chr(isc_tpb_lock_write)
  163. else if s='isc_tpb_verb_time' then TPB := TPB + chr(isc_tpb_verb_time)
  164. else if s='isc_tpb_commit_time' then TPB := TPB + chr(isc_tpb_commit_time)
  165. else if s='isc_tpb_ignore_limbo' then TPB := TPB + chr(isc_tpb_ignore_limbo)
  166. else if s='isc_tpb_autocommit' then TPB := TPB + chr(isc_tpb_autocommit)
  167. else if s='isc_tpb_restart_requests' then TPB := TPB + chr(isc_tpb_restart_requests)
  168. else if s='isc_tpb_no_auto_undo' then TPB := TPB + chr(isc_tpb_no_auto_undo);
  169. s := ExtractSubStr(AParams,i,stdWordDelims);
  170. end;
  171. TransactionHandle := nil;
  172. if isc_start_transaction(@Status, @TransactionHandle, 1,
  173. [@DBHandle, Length(TPB), @TPB[1]]) <> 0 then
  174. CheckError('StartTransaction',Status)
  175. else Result := True;
  176. end;
  177. end;
  178. procedure TIBConnection.CommitRetaining(trans : TSQLHandle);
  179. begin
  180. with trans as TIBtrans do
  181. if isc_commit_retaining(@Status, @TransactionHandle) <> 0 then
  182. CheckError('CommitRetaining', Status);
  183. end;
  184. procedure TIBConnection.RollBackRetaining(trans : TSQLHandle);
  185. begin
  186. with trans as TIBtrans do
  187. if isc_rollback_retaining(@Status, @TransactionHandle) <> 0 then
  188. CheckError('RollBackRetaining', Status);
  189. end;
  190. procedure TIBConnection.DoInternalConnect;
  191. var
  192. DPB : string;
  193. begin
  194. {$IfDef LinkDynamically}
  195. InitialiseIBase60;
  196. {$EndIf}
  197. inherited dointernalconnect;
  198. DPB := chr(isc_dpb_version1);
  199. if (UserName <> '') then
  200. begin
  201. DPB := DPB + chr(isc_dpb_user_name) + chr(Length(UserName)) + UserName;
  202. if (Password <> '') then
  203. DPB := DPB + chr(isc_dpb_password) + chr(Length(Password)) + Password;
  204. end;
  205. if (Role <> '') then
  206. DPB := DPB + chr(isc_dpb_sql_role_name) + chr(Length(Role)) + Role;
  207. if Length(CharSet) > 0 then
  208. DPB := DPB + Chr(isc_dpb_lc_ctype) + Chr(Length(CharSet)) + CharSet;
  209. FSQLDatabaseHandle := nil;
  210. if isc_attach_database(@FStatus, Length(DatabaseName), @DatabaseName[1], @FSQLDatabaseHandle,
  211. Length(DPB), @DPB[1]) <> 0 then
  212. CheckError('DoInternalConnect', FStatus);
  213. SetDBDialect;
  214. end;
  215. procedure TIBConnection.DoInternalDisconnect;
  216. begin
  217. if not Connected then
  218. begin
  219. FSQLDatabaseHandle := nil;
  220. Exit;
  221. end;
  222. isc_detach_database(@FStatus[0], @FSQLDatabaseHandle);
  223. CheckError('Close', FStatus);
  224. {$IfDef LinkDynamically}
  225. ReleaseIBase60;
  226. {$EndIf}
  227. end;
  228. procedure TIBConnection.SetDBDialect;
  229. var
  230. x : integer;
  231. Len : integer;
  232. Buffer : string;
  233. ResBuf : array [0..39] of byte;
  234. begin
  235. Buffer := Chr(isc_info_db_sql_dialect) + Chr(isc_info_end);
  236. if isc_database_info(@FStatus, @FSQLDatabaseHandle, Length(Buffer),
  237. @Buffer[1], SizeOf(ResBuf), @ResBuf) <> 0 then
  238. CheckError('SetDBDialect', FStatus);
  239. x := 0;
  240. while x < 40 do
  241. case ResBuf[x] of
  242. isc_info_db_sql_dialect :
  243. begin
  244. Inc(x);
  245. Len := isc_vax_integer(@ResBuf[x], 2);
  246. Inc(x, 2);
  247. FDialect := isc_vax_integer(@ResBuf[x], Len);
  248. Inc(x, Len);
  249. end;
  250. isc_info_end : Break;
  251. end;
  252. end;
  253. procedure TIBConnection.AllocSQLDA(var aSQLDA : PXSQLDA;Count : integer);
  254. var x : shortint;
  255. begin
  256. FreeSQLDABuffer(aSQLDA);
  257. if count > -1 then
  258. begin
  259. reAllocMem(aSQLDA, XSQLDA_Length(Count));
  260. { Zero out the memory block to avoid problems with exceptions within the
  261. constructor of this class. }
  262. FillChar(aSQLDA^, XSQLDA_Length(Count), 0);
  263. aSQLDA^.Version := sqlda_version1;
  264. aSQLDA^.SQLN := Count;
  265. end
  266. else
  267. reAllocMem(aSQLDA,0);
  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 := SQLLen;
  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 := SQLLen;
  324. TrType := ftFloat;
  325. end;
  326. SQL_FLOAT :
  327. begin
  328. LensSet := True;
  329. TrLen := SQLLen;
  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 : TSQLCursor;
  341. var curs : TIBCursor;
  342. begin
  343. curs := TIBCursor.create;
  344. curs.sqlda := nil;
  345. curs.statement := nil;
  346. curs.FPrepared := False;
  347. AllocSQLDA(curs.SQLDA,0);
  348. AllocSQLDA(curs.in_SQLDA,0);
  349. result := curs;
  350. end;
  351. procedure TIBConnection.DeAllocateCursorHandle(var cursor : TSQLCursor);
  352. begin
  353. if assigned(cursor) then with cursor as TIBCursor do
  354. begin
  355. AllocSQLDA(SQLDA,-1);
  356. AllocSQLDA(in_SQLDA,-1);
  357. end;
  358. FreeAndNil(cursor);
  359. end;
  360. Function TIBConnection.AllocateTransactionHandle : TSQLHandle;
  361. begin
  362. result := TIBTrans.create;
  363. end;
  364. procedure TIBConnection.PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams);
  365. var dh : pointer;
  366. tr : pointer;
  367. p : pchar;
  368. x : shortint;
  369. i : integer;
  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 assigned(AParams) and (AParams.count > 0) then
  378. buf := AParams.ParseSQL(buf,false,psInterbase,paramBinding);
  379. if isc_dsql_prepare(@Status, @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  380. CheckError('PrepareStatement', Status);
  381. FPrepared := True;
  382. if assigned(AParams) and (AParams.count > 0) then
  383. begin
  384. AllocSQLDA(in_SQLDA,Length(ParamBinding));
  385. if isc_dsql_describe_bind(@Status, @Statement, 1, in_SQLDA) <> 0 then
  386. CheckError('PrepareStatement', Status);
  387. if in_SQLDA^.SQLD > in_SQLDA^.SQLN then
  388. DatabaseError(SParameterCountIncorrect,self);
  389. {$R-}
  390. for x := 0 to in_SQLDA^.SQLD - 1 do with in_SQLDA^.SQLVar[x] do
  391. begin
  392. if ((SQLType and not 1) = SQL_VARYING) then
  393. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen+2)
  394. else
  395. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen);
  396. if (sqltype and 1) = 1 then New(SQLInd);
  397. end;
  398. {$R+}
  399. end;
  400. if FStatementType = stselect then
  401. begin
  402. FPrepared := False;
  403. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  404. CheckError('PrepareSelect', Status);
  405. if SQLDA^.SQLD > SQLDA^.SQLN then
  406. begin
  407. AllocSQLDA(SQLDA,SQLDA^.SQLD);
  408. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  409. CheckError('PrepareSelect', Status);
  410. end;
  411. {$R-}
  412. for x := 0 to SQLDA^.SQLD - 1 do with SQLDA^.SQLVar[x] do
  413. begin
  414. if ((SQLType and not 1) = SQL_VARYING) then
  415. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen+2)
  416. else
  417. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  418. if (SQLType and 1) = 1 then New(SQLInd);
  419. end;
  420. {$R+}
  421. end;
  422. end;
  423. end;
  424. procedure TIBConnection.UnPrepareStatement(cursor : TSQLCursor);
  425. begin
  426. with cursor as TIBcursor do
  427. begin
  428. if isc_dsql_free_statement(@Status, @Statement, DSQL_Drop) <> 0 then
  429. CheckError('FreeStatement', Status);
  430. Statement := nil;
  431. FPrepared := False;
  432. end;
  433. end;
  434. procedure TIBConnection.FreeSQLDABuffer(var aSQLDA : PXSQLDA);
  435. var x : shortint;
  436. begin
  437. {$R-}
  438. if assigned(aSQLDA) then
  439. for x := 0 to aSQLDA^.SQLN - 1 do
  440. begin
  441. reAllocMem(aSQLDA^.SQLVar[x].SQLData,0);
  442. if assigned(aSQLDA^.SQLVar[x].sqlind) then
  443. begin
  444. Dispose(aSQLDA^.SQLVar[x].sqlind);
  445. aSQLDA^.SQLVar[x].sqlind := nil;
  446. end
  447. end;
  448. {$R+}
  449. end;
  450. procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor);
  451. begin
  452. with cursor as TIBCursor do
  453. begin
  454. FreeSQLDABuffer(SQLDA);
  455. FreeSQLDABuffer(in_SQLDA);
  456. end;
  457. end;
  458. procedure TIBConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams);
  459. var tr : pointer;
  460. begin
  461. tr := aTransaction.Handle;
  462. if Assigned(APArams) and (AParams.count > 0) then SetParameters(cursor, AParams);
  463. with cursor as TIBCursor do
  464. if isc_dsql_execute2(@Status, @tr, @Statement, 1, in_SQLDA, nil) <> 0 then
  465. CheckError('Execute', Status);
  466. end;
  467. procedure TIBConnection.AddFieldDefs(cursor: TSQLCursor;FieldDefs : TfieldDefs);
  468. var
  469. x : integer;
  470. lenset : boolean;
  471. TransLen : word;
  472. TransType : TFieldType;
  473. FD : TFieldDef;
  474. begin
  475. {$R-}
  476. with cursor as TIBCursor do
  477. begin
  478. for x := 0 to SQLDA^.SQLD - 1 do
  479. begin
  480. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].SQLLen, SQLDA^.SQLVar[x].SQLScale,
  481. lenset, TransType, TransLen);
  482. FD := TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].AliasName, TransType,
  483. TransLen, False, (x + 1));
  484. if TransType = ftBCD then FD.precision := SQLDA^.SQLVar[x].SQLLen;
  485. FD.DisplayName := SQLDA^.SQLVar[x].AliasName;
  486. end;
  487. end;
  488. {$R+}
  489. end;
  490. function TIBConnection.GetHandle: pointer;
  491. begin
  492. Result := FSQLDatabaseHandle;
  493. end;
  494. function TIBConnection.Fetch(cursor : TSQLCursor) : boolean;
  495. var
  496. retcode : integer;
  497. begin
  498. with cursor as TIBCursor do
  499. begin
  500. retcode := isc_dsql_fetch(@Status, @Statement, 1, SQLDA);
  501. if (retcode <> 0) and (retcode <> 100) then
  502. CheckError('Fetch', Status);
  503. end;
  504. Result := (retcode <> 100);
  505. end;
  506. procedure TIBConnection.SetParameters(cursor : TSQLCursor;AParams : TParams);
  507. var ParNr,SQLVarNr : integer;
  508. s : string;
  509. i : integer;
  510. currbuff : pchar;
  511. w : word;
  512. begin
  513. {$R-}
  514. with cursor as TIBCursor do for SQLVarNr := 0 to High(ParamBinding){AParams.count-1} do
  515. begin
  516. ParNr := ParamBinding[SQLVarNr];
  517. if AParams[ParNr].IsNull then
  518. in_sqlda^.SQLvar[SQLVarNr].SQLInd^ := -1
  519. else
  520. begin
  521. if assigned(in_sqlda^.SQLvar[SQLVarNr].SQLInd) then in_sqlda^.SQLvar[SQLVarNr].SQLInd^ := 0;
  522. case AParams[ParNr].DataType of
  523. ftInteger :
  524. begin
  525. i := AParams[ParNr].AsInteger;
  526. {$R-}
  527. Move(i, in_sqlda^.SQLvar[SQLVarNr].SQLData^, in_SQLDA^.SQLVar[SQLVarNr].SQLLen);
  528. {$R+}
  529. end;
  530. ftString :
  531. begin
  532. {$R-}
  533. s := AParams[ParNr].AsString;
  534. w := length(s);
  535. if ((in_sqlda^.SQLvar[SQLVarNr].SQLType and not 1) = SQL_VARYING) then
  536. begin
  537. in_sqlda^.SQLvar[SQLVarNr].SQLLen := w;
  538. ReAllocMem(in_sqlda^.SQLvar[SQLVarNr].SQLData,in_SQLDA^.SQLVar[SQLVarNr].SQLLen+2);
  539. CurrBuff := in_sqlda^.SQLvar[SQLVarNr].SQLData;
  540. move(w,CurrBuff^,sizeof(w));
  541. inc(CurrBuff,2);
  542. end
  543. else
  544. CurrBuff := in_sqlda^.SQLvar[SQLVarNr].SQLData;
  545. Move(s[1], CurrBuff^, length(s));
  546. {$R+}
  547. end;
  548. ftDate, ftTime, ftDateTime:
  549. {$R-}
  550. SetDateTime(in_sqlda^.SQLvar[SQLVarNr].SQLData, AParams[ParNr].AsDateTime, in_SQLDA^.SQLVar[SQLVarNr].SQLType);
  551. {$R+}
  552. else
  553. begin
  554. DatabaseError('This kind of parameter in not (yet) supported.',self);
  555. end;
  556. end {case}
  557. end;
  558. end;
  559. {$R+}
  560. end;
  561. function TIBConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean;
  562. var
  563. x : integer;
  564. VarcharLen : word;
  565. CurrBuff : pchar;
  566. b : longint;
  567. li : largeint;
  568. c : currency;
  569. begin
  570. with cursor as TIBCursor do
  571. begin
  572. {$R-}
  573. for x := 0 to SQLDA^.SQLD - 1 do
  574. if SQLDA^.SQLVar[x].AliasName = FieldDef.Name then break;
  575. if SQLDA^.SQLVar[x].AliasName <> FieldDef.Name then
  576. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  577. if assigned(SQLDA^.SQLVar[x].SQLInd) and (SQLDA^.SQLVar[x].SQLInd^ = -1) then
  578. result := false
  579. else
  580. begin
  581. with SQLDA^.SQLVar[x] do
  582. if ((SQLType and not 1) = SQL_VARYING) then
  583. begin
  584. Move(SQLData^, VarcharLen, 2);
  585. CurrBuff := SQLData + 2;
  586. end
  587. else
  588. begin
  589. CurrBuff := SQLData;
  590. VarCharLen := SQLDA^.SQLVar[x].SQLLen;
  591. end;
  592. Result := true;
  593. case FieldDef.DataType of
  594. ftBCD :
  595. begin
  596. c := 0;
  597. Move(CurrBuff^, c, SQLDA^.SQLVar[x].SQLLen);
  598. c := c*intpower(10,4+SQLDA^.SQLVar[x].SQLScale);
  599. Move(c, buffer^ , sizeof(c));
  600. end;
  601. ftInteger :
  602. begin
  603. b := 0;
  604. Move(b, Buffer^, sizeof(longint));
  605. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  606. end;
  607. ftLargeint :
  608. begin
  609. li := 0;
  610. Move(CurrBuff^, li, SQLDA^.SQLVar[x].SQLLen);
  611. if SQLDA^.SQLVar[x].SQLScale > 0 then
  612. li := li * trunc(intpower(10, SQLDA^.SQLVar[x].SQLScale))
  613. else if SQLDA^.SQLVar[x].SQLScale < 0 then
  614. li := li div trunc(intpower(10, -SQLDA^.SQLVar[x].SQLScale));
  615. Move(li, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  616. end;
  617. ftDate, ftTime, ftDateTime:
  618. GetDateTime(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLType);
  619. ftString :
  620. begin
  621. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  622. PChar(Buffer + VarCharLen)^ := #0;
  623. end;
  624. ftFloat :
  625. GetFloat(CurrBuff, Buffer, FieldDef);
  626. ftBlob : begin // load the BlobIb in field's buffer
  627. li := 0;
  628. Move(li, Buffer^, sizeof(largeint));
  629. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  630. end
  631. else result := false;
  632. end;
  633. end;
  634. {$R+}
  635. end;
  636. end;
  637. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  638. var
  639. CTime : TTm; // C struct time
  640. STime : TSystemTime; // System time
  641. PTime : TDateTime; // Pascal time
  642. begin
  643. case (AType and not 1) of
  644. SQL_TYPE_DATE :
  645. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  646. SQL_TYPE_TIME :
  647. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  648. SQL_TIMESTAMP :
  649. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  650. end;
  651. STime.Year := CTime.tm_year + 1900;
  652. STime.Month := CTime.tm_mon + 1;
  653. STime.Day := CTime.tm_mday;
  654. STime.Hour := CTime.tm_hour;
  655. STime.Minute := CTime.tm_min;
  656. STime.Second := CTime.tm_sec;
  657. STime.Millisecond := 0;
  658. PTime := SystemTimeToDateTime(STime);
  659. Move(PTime, Buffer^, SizeOf(PTime));
  660. end;
  661. procedure TIBConnection.SetDateTime(CurrBuff: pointer; PTime : TDateTime; AType : integer);
  662. var
  663. CTime : TTm; // C struct time
  664. STime : TSystemTime; // System time
  665. begin
  666. DateTimeToSystemTime(PTime,STime);
  667. CTime.tm_year := STime.Year - 1900;
  668. CTime.tm_mon := STime.Month -1;
  669. CTime.tm_mday := STime.Day;
  670. CTime.tm_hour := STime.Hour;
  671. CTime.tm_min := STime.Minute;
  672. CTime.tm_sec := STime.Second;
  673. case (AType and not 1) of
  674. SQL_TYPE_DATE :
  675. isc_encode_sql_date(@CTime, PISC_DATE(CurrBuff));
  676. SQL_TYPE_TIME :
  677. isc_encode_sql_time(@CTime, PISC_TIME(CurrBuff));
  678. SQL_TIMESTAMP :
  679. isc_encode_timestamp(@CTime, PISC_TIMESTAMP(CurrBuff));
  680. end;
  681. end;
  682. function TIBConnection.GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  683. var s : string;
  684. begin
  685. case SchemaType of
  686. stTables : s := 'select '+
  687. 'rdb$relation_id as recno, '+
  688. '''' + DatabaseName + ''' as catalog_name, '+
  689. ''''' as schema_name, '+
  690. 'rdb$relation_name as table_name, '+
  691. '0 as table_type '+
  692. 'from '+
  693. 'rdb$relations '+
  694. 'where '+
  695. '(rdb$system_flag = 0 or rdb$system_flag is null) ' + // and rdb$view_blr is null
  696. 'order by rdb$relation_name';
  697. stSysTables : s := 'select '+
  698. 'rdb$relation_id as recno, '+
  699. '''' + DatabaseName + ''' as catalog_name, '+
  700. ''''' as schema_name, '+
  701. 'rdb$relation_name as table_name, '+
  702. '0 as table_type '+
  703. 'from '+
  704. 'rdb$relations '+
  705. 'where '+
  706. '(rdb$system_flag > 0) ' + // and rdb$view_blr is null
  707. 'order by rdb$relation_name';
  708. stProcedures : s := 'select '+
  709. 'rdb$procedure_id as recno, '+
  710. '''' + DatabaseName + ''' as catalog_name, '+
  711. ''''' as schema_name, '+
  712. 'rdb$procedure_name as proc_name, '+
  713. '0 as proc_type, '+
  714. 'rdb$procedure_inputs as in_params, '+
  715. 'rdb$procedure_outputs as out_params '+
  716. 'from '+
  717. 'rdb$procedures '+
  718. 'WHERE '+
  719. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  720. stColumns : s := 'select '+
  721. 'rdb$field_id as recno, '+
  722. '''' + DatabaseName + ''' as catalog_name, '+
  723. ''''' as schema_name, '+
  724. 'rdb$relation_name as table_name, '+
  725. 'rdb$field_name as column_name, '+
  726. 'rdb$field_position as column_position, '+
  727. '0 as column_type, '+
  728. '0 as column_datatype, '+
  729. ''''' as column_typename, '+
  730. '0 as column_subtype, '+
  731. '0 as column_precision, '+
  732. '0 as column_scale, '+
  733. '0 as column_length, '+
  734. '0 as column_nullable '+
  735. 'from '+
  736. 'rdb$relation_fields '+
  737. 'WHERE '+
  738. '(rdb$system_flag = 0 or rdb$system_flag is null) and (rdb$relation_name = ''' + Uppercase(SchemaObjectName) + ''') ' +
  739. 'order by rdb$field_name';
  740. else
  741. DatabaseError(SMetadataUnavailable)
  742. end; {case}
  743. result := s;
  744. end;
  745. procedure TIBConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  746. var qry : TSQLQuery;
  747. begin
  748. if not assigned(Transaction) then
  749. DatabaseError(SErrConnTransactionnSet);
  750. qry := tsqlquery.Create(nil);
  751. qry.transaction := Transaction;
  752. qry.database := Self;
  753. with qry do
  754. begin
  755. ReadOnly := True;
  756. sql.clear;
  757. sql.add('select '+
  758. 'ind.rdb$index_name, '+
  759. 'ind.rdb$relation_name, '+
  760. 'ind.rdb$unique_flag, '+
  761. 'ind_seg.rdb$field_name, '+
  762. 'rel_con.rdb$constraint_type '+
  763. 'from '+
  764. 'rdb$index_segments ind_seg, '+
  765. 'rdb$indices ind '+
  766. 'left outer join '+
  767. 'rdb$relation_constraints rel_con '+
  768. 'on '+
  769. 'rel_con.rdb$index_name = ind.rdb$index_name '+
  770. 'where '+
  771. '(ind_seg.rdb$index_name = ind.rdb$index_name) and '+
  772. '(ind.rdb$relation_name=''' + UpperCase(TableName) +''') '+
  773. 'order by '+
  774. 'ind.rdb$index_name;');
  775. open;
  776. end;
  777. while not qry.eof do with IndexDefs.AddIndexDef do
  778. begin
  779. Name := trim(qry.fields[0].asstring);
  780. Fields := trim(qry.Fields[3].asstring);
  781. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  782. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  783. qry.next;
  784. while (name = qry.fields[0].asstring) and (not qry.eof) do
  785. begin
  786. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  787. qry.next;
  788. end;
  789. end;
  790. qry.close;
  791. qry.free;
  792. end;
  793. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  794. var
  795. Ext : extended;
  796. Dbl : double;
  797. Sin : single;
  798. begin
  799. case Field.Size of
  800. 4 :
  801. begin
  802. Move(CurrBuff^, Sin, 4);
  803. Dbl := Sin;
  804. end;
  805. 8 :
  806. begin
  807. Move(CurrBuff^, Dbl, 8);
  808. end;
  809. 10:
  810. begin
  811. Move(CurrBuff^, Ext, 10);
  812. Dbl := double(Ext);
  813. end;
  814. end;
  815. Move(Dbl, Buffer^, 8);
  816. end;
  817. function TIBConnection.getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  818. var
  819. iscInfoBlobMaxSegment : byte = isc_info_blob_max_segment;
  820. blobInfo : array[0..50] of byte;
  821. begin
  822. if isc_blob_info(@Fstatus, @blobHandle, sizeof(iscInfoBlobMaxSegment), @iscInfoBlobMaxSegment, sizeof(blobInfo) - 2, @blobInfo) <> 0 then
  823. CheckError('isc_blob_info', FStatus);
  824. if blobInfo[0] = isc_info_blob_max_segment then
  825. begin
  826. result := isc_vax_integer(pchar(@blobInfo[3]), isc_vax_integer(pchar(@blobInfo[1]), 2));
  827. end
  828. else
  829. CheckError('isc_blob_info', FStatus);
  830. end;
  831. function TIBConnection.CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream;
  832. const
  833. isc_segstr_eof = 335544367; // It's not defined in ibase60 but in ibase40. Would it be better to define in ibase60?
  834. var
  835. mStream : TMemoryStream;
  836. blobHandle : Isc_blob_Handle;
  837. blobSegment : pointer;
  838. blobSegLen : smallint;
  839. maxBlobSize : longInt;
  840. TransactionHandle : pointer;
  841. blobId : ISC_QUAD;
  842. begin
  843. result := nil;
  844. if mode = bmRead then begin
  845. if not field.getData(@blobId) then
  846. exit;
  847. if not assigned(Transaction) then
  848. DatabaseError(SErrConnTransactionnSet);
  849. TransactionHandle := transaction.Handle;
  850. blobHandle := nil;
  851. if isc_open_blob(@FStatus, @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, @blobId) <> 0 then
  852. CheckError('TIBConnection.CreateBlobStream', FStatus);
  853. maxBlobSize := getMaxBlobSize(blobHandle);
  854. blobSegment := AllocMem(maxBlobSize);
  855. mStream := TMemoryStream.create;
  856. while (isc_get_segment(@FStatus, @blobHandle, @blobSegLen, maxBlobSize, blobSegment) = 0) do begin
  857. mStream.writeBuffer(blobSegment^, blobSegLen);
  858. end;
  859. freemem(blobSegment);
  860. mStream.seek(0,soFromBeginning);
  861. if FStatus[1] = isc_segstr_eof then
  862. begin
  863. if isc_close_blob(@FStatus, @blobHandle) <> 0 then
  864. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  865. end
  866. else
  867. CheckError('TIBConnection.CreateBlobStream isc_get_segment', FStatus);
  868. result := mStream;
  869. end;
  870. end;
  871. end.