ibconnection.pp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. FFieldFlag : array of shortint;
  18. FinFieldFlag : array of shortint;
  19. SQLDA : PXSQLDA;
  20. in_SQLDA : PXSQLDA;
  21. ParamBinding : array of integer;
  22. end;
  23. TIBTrans = Class(TSQLHandle)
  24. protected
  25. TransactionHandle : pointer;
  26. TPB : string; // Transaction parameter buffer
  27. Status : array [0..19] of ISC_STATUS;
  28. end;
  29. TIBConnection = class (TSQLConnection)
  30. private
  31. FSQLDatabaseHandle : pointer;
  32. FStatus : array [0..19] of ISC_STATUS;
  33. FDialect : integer;
  34. procedure SetDBDialect;
  35. procedure AllocSQLDA(var aSQLDA : PXSQLDA;Count : integer);
  36. procedure TranslateFldType(SQLType, SQLLen, SQLScale : integer; var LensSet : boolean;
  37. var TrType : TFieldType; var TrLen : word);
  38. // conversion methods
  39. procedure GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  40. procedure GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  41. procedure CheckError(ProcName : string; Status : array of ISC_STATUS);
  42. function getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  43. procedure SetParameters(cursor : TSQLCursor;AParams : TParams);
  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 CloseStatement(cursor : TSQLCursor); override;
  52. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); 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. 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. constructor TIBConnection.Create(AOwner : TComponent);
  111. begin
  112. inherited;
  113. FConnOptions := FConnOptions + [sqSupportParams];
  114. end;
  115. function TIBConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  116. begin
  117. Result := (trans as TIBtrans).TransactionHandle;
  118. end;
  119. function TIBConnection.Commit(trans : TSQLHandle) : boolean;
  120. begin
  121. result := false;
  122. with (trans as TIBTrans) do
  123. if isc_commit_transaction(@Status, @TransactionHandle) <> 0 then
  124. CheckError('Commit', Status)
  125. else result := true;
  126. end;
  127. function TIBConnection.RollBack(trans : TSQLHandle) : boolean;
  128. begin
  129. result := false;
  130. if isc_rollback_transaction(@TIBTrans(trans).Status, @TIBTrans(trans).TransactionHandle) <> 0 then
  131. CheckError('Rollback', TIBTrans(trans).Status)
  132. else result := true;
  133. end;
  134. function TIBConnection.StartDBTransaction(trans : TSQLHandle;AParams : String) : boolean;
  135. var
  136. DBHandle : pointer;
  137. tr : TIBTrans;
  138. i : integer;
  139. s : string;
  140. begin
  141. result := false;
  142. DBHandle := GetHandle;
  143. tr := trans as TIBtrans;
  144. with tr do
  145. begin
  146. TPB := chr(isc_tpb_version3);
  147. i := 1;
  148. s := ExtractSubStr(AParams,i,stdWordDelims);
  149. while s <> '' do
  150. begin
  151. if s='isc_tpb_write' then TPB := TPB + chr(isc_tpb_write)
  152. else if s='isc_tpb_read' then TPB := TPB + chr(isc_tpb_read)
  153. else if s='isc_tpb_consistency' then TPB := TPB + chr(isc_tpb_consistency)
  154. else if s='isc_tpb_concurrency' then TPB := TPB + chr(isc_tpb_concurrency)
  155. else if s='isc_tpb_read_committed' then TPB := TPB + chr(isc_tpb_read_committed)
  156. else if s='isc_tpb_rec_version' then TPB := TPB + chr(isc_tpb_rec_version)
  157. else if s='isc_tpb_no_rec_version' then TPB := TPB + chr(isc_tpb_no_rec_version)
  158. else if s='isc_tpb_wait' then TPB := TPB + chr(isc_tpb_wait)
  159. else if s='isc_tpb_nowait' then TPB := TPB + chr(isc_tpb_nowait)
  160. else if s='isc_tpb_shared' then TPB := TPB + chr(isc_tpb_shared)
  161. else if s='isc_tpb_protected' then TPB := TPB + chr(isc_tpb_protected)
  162. else if s='isc_tpb_exclusive' then TPB := TPB + chr(isc_tpb_exclusive)
  163. else if s='isc_tpb_lock_read' then TPB := TPB + chr(isc_tpb_lock_read)
  164. else if s='isc_tpb_lock_write' then TPB := TPB + chr(isc_tpb_lock_write)
  165. else if s='isc_tpb_verb_time' then TPB := TPB + chr(isc_tpb_verb_time)
  166. else if s='isc_tpb_commit_time' then TPB := TPB + chr(isc_tpb_commit_time)
  167. else if s='isc_tpb_ignore_limbo' then TPB := TPB + chr(isc_tpb_ignore_limbo)
  168. else if s='isc_tpb_autocommit' then TPB := TPB + chr(isc_tpb_autocommit)
  169. else if s='isc_tpb_restart_requests' then TPB := TPB + chr(isc_tpb_restart_requests)
  170. else if s='isc_tpb_no_auto_undo' then TPB := TPB + chr(isc_tpb_no_auto_undo);
  171. s := ExtractSubStr(AParams,i,stdWordDelims);
  172. end;
  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(var aSQLDA : PXSQLDA;Count : integer);
  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. procedure TIBConnection.TranslateFldType(SQLType, SQLLen, SQLScale : integer; var LensSet : boolean;
  267. var TrType : TFieldType; var TrLen : word);
  268. begin
  269. LensSet := False;
  270. if (SQLScale >= -4) and (SQLScale <= -1) then //in [-4..-1] then
  271. begin
  272. LensSet := True;
  273. TrLen := SQLLen;
  274. TrType := ftBCD
  275. end
  276. else case (SQLType and not 1) of
  277. SQL_VARYING :
  278. begin
  279. LensSet := True;
  280. TrType := ftString;
  281. TrLen := SQLLen;
  282. end;
  283. SQL_TEXT :
  284. begin
  285. LensSet := True;
  286. TrType := ftString;
  287. TrLen := SQLLen;
  288. end;
  289. SQL_TYPE_DATE :
  290. TrType := ftDate{Time};
  291. SQL_TYPE_TIME :
  292. TrType := ftDateTime;
  293. SQL_TIMESTAMP :
  294. TrType := ftDateTime;
  295. SQL_ARRAY :
  296. begin
  297. TrType := ftArray;
  298. LensSet := true;
  299. TrLen := SQLLen;
  300. end;
  301. SQL_BLOB :
  302. begin
  303. TrType := ftBlob;
  304. LensSet := True;
  305. TrLen := SQLLen;
  306. end;
  307. SQL_SHORT :
  308. TrType := ftInteger;
  309. SQL_LONG :
  310. begin
  311. LensSet := True;
  312. TrLen := 0;
  313. TrType := ftInteger;
  314. end;
  315. SQL_INT64 :
  316. TrType := ftLargeInt;
  317. SQL_DOUBLE :
  318. begin
  319. LensSet := True;
  320. TrLen := SQLLen;
  321. TrType := ftFloat;
  322. end;
  323. SQL_FLOAT :
  324. begin
  325. LensSet := True;
  326. TrLen := SQLLen;
  327. TrType := ftFloat;
  328. end
  329. else
  330. begin
  331. LensSet := True;
  332. TrLen := 0;
  333. TrType := ftUnknown;
  334. end;
  335. end;
  336. end;
  337. Function TIBConnection.AllocateCursorHandle : TSQLCursor;
  338. var curs : TIBCursor;
  339. begin
  340. curs := TIBCursor.create;
  341. curs.sqlda := nil;
  342. curs.statement := nil;
  343. AllocSQLDA(curs.SQLDA,1);
  344. AllocSQLDA(curs.in_SQLDA,1);
  345. result := curs;
  346. end;
  347. procedure TIBConnection.DeAllocateCursorHandle(var cursor : TSQLCursor);
  348. begin
  349. if assigned(cursor) then with cursor as TIBCursor do
  350. begin
  351. reAllocMem(SQLDA,0);
  352. reAllocMem(in_SQLDA,0);
  353. end;
  354. FreeAndNil(cursor);
  355. end;
  356. Function TIBConnection.AllocateTransactionHandle : TSQLHandle;
  357. begin
  358. result := TIBTrans.create;
  359. end;
  360. procedure TIBConnection.CloseStatement(cursor : TSQLCursor);
  361. begin
  362. with cursor as TIBcursor do
  363. begin
  364. if isc_dsql_free_statement(@Status, @Statement, DSQL_Drop) <> 0 then
  365. CheckError('FreeStatement', Status);
  366. Statement := nil;
  367. end;
  368. end;
  369. procedure TIBConnection.PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams);
  370. var dh : pointer;
  371. tr : pointer;
  372. p : pchar;
  373. x : shortint;
  374. i : integer;
  375. begin
  376. // ObtainSQLStatementType(cursor,buf);
  377. with cursor as TIBcursor do
  378. begin
  379. dh := GetHandle;
  380. if isc_dsql_allocate_statement(@Status, @dh, @Statement) <> 0 then
  381. CheckError('PrepareStatement', Status);
  382. tr := aTransaction.Handle;
  383. if assigned(AParams) and (AParams.count > 0) then
  384. begin
  385. SetLength(ParamBinding,0);
  386. i := posex(':',buf);
  387. while i > 0 do
  388. begin
  389. inc(i);
  390. p := @buf[i];
  391. repeat
  392. inc(p);
  393. until (p^ in SQLDelimiterCharacters);
  394. SetLength(ParamBinding,length(ParamBinding)+1);
  395. parambinding[high(parambinding)] := AParams.ParamByName(copy(buf,i,p-@buf[i])).Index;
  396. i := posex(':',buf,i);
  397. end;
  398. for x := 0 to AParams.count-1 do
  399. buf := stringreplace(buf,':'+AParams[x].Name,'?',[rfReplaceAll,rfIgnoreCase]);
  400. end;
  401. if isc_dsql_prepare(@Status, @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  402. CheckError('PrepareStatement', Status);
  403. if assigned(AParams) and (AParams.count > 0) then
  404. begin
  405. AllocSQLDA(in_SQLDA,Length(ParamBinding));
  406. if isc_dsql_describe_bind(@Status, @Statement, 1, in_SQLDA) <> 0 then
  407. CheckError('PrepareStatement', Status);
  408. if in_SQLDA^.SQLD > in_SQLDA^.SQLN then
  409. DatabaseError(SParameterCountIncorrect,self);
  410. {$R-}
  411. SetLength(FinFieldFlag,in_SQLDA^.SQLD);
  412. for x := 0 to in_SQLDA^.SQLD - 1 do with in_SQLDA^.SQLVar[x] do
  413. begin
  414. if ((SQLType and not 1) = SQL_VARYING) then
  415. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen+2)
  416. else
  417. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen);
  418. SQLInd := @FinFieldFlag[x];
  419. end;
  420. {$R+}
  421. end;
  422. if FStatementType = stselect then
  423. begin
  424. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  425. CheckError('PrepareSelect', Status);
  426. if SQLDA^.SQLD > SQLDA^.SQLN then
  427. begin
  428. AllocSQLDA(SQLDA,SQLDA^.SQLD);
  429. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  430. CheckError('PrepareSelect', Status);
  431. end;
  432. {$R-}
  433. SetLength(FFieldFlag,SQLDA^.SQLD);
  434. for x := 0 to SQLDA^.SQLD - 1 do with SQLDA^.SQLVar[x] do
  435. begin
  436. if ((SQLType and not 1) = SQL_VARYING) then
  437. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen+2)
  438. else
  439. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  440. SQLInd := @FFieldFlag[x];
  441. end;
  442. {$R+}
  443. end;
  444. end;
  445. end;
  446. procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor);
  447. var
  448. x : shortint;
  449. begin
  450. {$R-}
  451. with cursor as TIBCursor do
  452. for x := 0 to SQLDA^.SQLD - 1 do
  453. reAllocMem(SQLDA^.SQLVar[x].SQLData,0);
  454. {$R+}
  455. end;
  456. procedure TIBConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams);
  457. var tr : pointer;
  458. begin
  459. tr := aTransaction.Handle;
  460. if Assigned(APArams) and (AParams.count > 0) then SetParameters(cursor, AParams);
  461. with cursor as TIBCursor do
  462. if isc_dsql_execute2(@Status, @tr, @Statement, 1, in_SQLDA, nil) <> 0 then
  463. CheckError('Execute', Status);
  464. end;
  465. procedure TIBConnection.AddFieldDefs(cursor: TSQLCursor;FieldDefs : TfieldDefs);
  466. var
  467. x : integer;
  468. lenset : boolean;
  469. TransLen : word;
  470. TransType : TFieldType;
  471. FD : TFieldDef;
  472. begin
  473. {$R-}
  474. with cursor as TIBCursor do
  475. begin
  476. for x := 0 to SQLDA^.SQLD - 1 do
  477. begin
  478. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].SQLLen, SQLDA^.SQLVar[x].SQLScale,
  479. lenset, TransType, TransLen);
  480. FD := TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].AliasName, TransType,
  481. TransLen, False, (x + 1));
  482. if TransType = ftBCD then FD.precision := SQLDA^.SQLVar[x].SQLLen;
  483. FD.DisplayName := SQLDA^.SQLVar[x].AliasName;
  484. end;
  485. end;
  486. {$R+}
  487. end;
  488. function TIBConnection.GetHandle: pointer;
  489. begin
  490. Result := FSQLDatabaseHandle;
  491. end;
  492. function TIBConnection.Fetch(cursor : TSQLCursor) : boolean;
  493. var
  494. retcode : integer;
  495. begin
  496. with cursor as TIBCursor do
  497. begin
  498. retcode := isc_dsql_fetch(@Status, @Statement, 1, SQLDA);
  499. if (retcode <> 0) and (retcode <> 100) then
  500. CheckError('Fetch', Status);
  501. end;
  502. Result := (retcode <> 100);
  503. end;
  504. procedure TIBConnection.SetParameters(cursor : TSQLCursor;AParams : TParams);
  505. var ParNr,SQLVarNr : integer;
  506. s : string;
  507. i : integer;
  508. currbuff : pchar;
  509. w : word;
  510. begin
  511. {$R-}
  512. with cursor as TIBCursor do for SQLVarNr := 0 to High(ParamBinding){AParams.count-1} do
  513. begin
  514. ParNr := ParamBinding[SQLVarNr];
  515. if AParams[ParNr].IsNull then
  516. in_sqlda^.SQLvar[SQLVarNr].SQLInd^ := -1
  517. else
  518. begin
  519. in_sqlda^.SQLvar[SQLVarNr].SQLInd^ := 0;
  520. case AParams[ParNr].DataType of
  521. ftInteger :
  522. begin
  523. i := AParams[ParNr].AsInteger;
  524. Move(i, in_sqlda^.SQLvar[SQLVarNr].SQLData^, in_SQLDA^.SQLVar[SQLVarNr].SQLLen);
  525. end;
  526. ftString :
  527. begin
  528. {$R-}
  529. s := AParams[ParNr].AsString;
  530. w := length(s);
  531. if ((in_sqlda^.SQLvar[SQLVarNr].SQLType and not 1) = SQL_VARYING) then
  532. begin
  533. in_sqlda^.SQLvar[SQLVarNr].SQLLen := w;
  534. in_sqlda^.SQLvar[SQLVarNr].SQLData := AllocMem(in_SQLDA^.SQLVar[SQLVarNr].SQLLen+2)
  535. end;
  536. CurrBuff := in_sqlda^.SQLvar[SQLVarNr].SQLData;
  537. move(w,CurrBuff^,sizeof(w));
  538. inc(CurrBuff,2);
  539. Move(s[1], CurrBuff^, length(s));
  540. {$R+}
  541. end;
  542. else
  543. begin
  544. DatabaseError('This kind of parameter in not (yet) supported.',self);
  545. end;
  546. end {case}
  547. end;
  548. end;
  549. {$R+}
  550. end;
  551. function TIBConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean;
  552. var
  553. x : integer;
  554. VarcharLen : word;
  555. CurrBuff : pchar;
  556. b : longint;
  557. li : largeint;
  558. c : currency;
  559. begin
  560. with cursor as TIBCursor do
  561. begin
  562. {$R-}
  563. for x := 0 to SQLDA^.SQLD - 1 do
  564. if SQLDA^.SQLVar[x].AliasName = FieldDef.Name then break;
  565. if SQLDA^.SQLVar[x].AliasName <> FieldDef.Name then
  566. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  567. if SQLDA^.SQLVar[x].SQLInd^ = -1 then
  568. result := false
  569. else
  570. begin
  571. with SQLDA^.SQLVar[x] do
  572. if ((SQLType and not 1) = SQL_VARYING) then
  573. begin
  574. Move(SQLData^, VarcharLen, 2);
  575. CurrBuff := SQLData + 2;
  576. end
  577. else
  578. begin
  579. CurrBuff := SQLData;
  580. VarCharLen := SQLDA^.SQLVar[x].SQLLen;
  581. end;
  582. Result := true;
  583. case FieldDef.DataType of
  584. ftBCD :
  585. begin
  586. c := 0;
  587. Move(CurrBuff^, c, SQLDA^.SQLVar[x].SQLLen);
  588. c := c*intpower(10,4+SQLDA^.SQLVar[x].SQLScale);
  589. Move(c, buffer^ , sizeof(c));
  590. end;
  591. ftInteger :
  592. begin
  593. b := 0;
  594. Move(b, Buffer^, sizeof(longint));
  595. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  596. end;
  597. ftLargeint :
  598. begin
  599. li := 0;
  600. Move(CurrBuff^, li, SQLDA^.SQLVar[x].SQLLen);
  601. if SQLDA^.SQLVar[x].SQLScale > 0 then
  602. li := li * trunc(intpower(10, SQLDA^.SQLVar[x].SQLScale))
  603. else if SQLDA^.SQLVar[x].SQLScale < 0 then
  604. li := li div trunc(intpower(10, -SQLDA^.SQLVar[x].SQLScale));
  605. Move(li, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  606. end;
  607. ftDate, ftTime, ftDateTime:
  608. GetDateTime(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLType);
  609. ftString :
  610. begin
  611. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  612. PChar(Buffer + VarCharLen)^ := #0;
  613. end;
  614. ftFloat :
  615. GetFloat(CurrBuff, Buffer, FieldDef);
  616. ftBlob : begin // load the BlobIb in field's buffer
  617. li := 0;
  618. Move(li, Buffer^, sizeof(largeint));
  619. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  620. end
  621. else result := false;
  622. end;
  623. end;
  624. {$R+}
  625. end;
  626. end;
  627. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  628. var
  629. CTime : TTm; // C struct time
  630. STime : TSystemTime; // System time
  631. PTime : TDateTime; // Pascal time
  632. begin
  633. case (AType and not 1) of
  634. SQL_TYPE_DATE :
  635. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  636. SQL_TYPE_TIME :
  637. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  638. SQL_TIMESTAMP :
  639. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  640. end;
  641. STime.Year := CTime.tm_year + 1900;
  642. STime.Month := CTime.tm_mon + 1;
  643. STime.Day := CTime.tm_mday;
  644. STime.Hour := CTime.tm_hour;
  645. STime.Minute := CTime.tm_min;
  646. STime.Second := CTime.tm_sec;
  647. STime.Millisecond := 0;
  648. PTime := SystemTimeToDateTime(STime);
  649. Move(PTime, Buffer^, SizeOf(PTime));
  650. end;
  651. function TIBConnection.GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  652. var s : string;
  653. begin
  654. case SchemaType of
  655. stTables : s := 'select '+
  656. 'rdb$relation_id as recno, '+
  657. '''' + DatabaseName + ''' as catalog_name, '+
  658. ''''' as schema_name, '+
  659. 'rdb$relation_name as table_name, '+
  660. '0 as table_type '+
  661. 'from '+
  662. 'rdb$relations '+
  663. 'where '+
  664. '(rdb$system_flag = 0 or rdb$system_flag is null)'; // and rdb$view_blr is null
  665. stSysTables : s := 'select '+
  666. 'rdb$relation_id as recno, '+
  667. '''' + DatabaseName + ''' as catalog_name, '+
  668. ''''' as schema_name, '+
  669. 'rdb$relation_name as table_name, '+
  670. '0 as table_type '+
  671. 'from '+
  672. 'rdb$relations '+
  673. 'where '+
  674. '(rdb$system_flag > 0)'; // and rdb$view_blr is null
  675. stProcedures : s := 'select '+
  676. 'rdb$procedure_id as recno, '+
  677. '''' + DatabaseName + ''' as catalog_name, '+
  678. ''''' as schema_name, '+
  679. 'rdb$procedure_name as proc_name, '+
  680. '0 as proc_type, '+
  681. 'rdb$procedure_inputs as in_params, '+
  682. 'rdb$procedure_outputs as out_params '+
  683. 'from '+
  684. 'rdb$procedures '+
  685. 'WHERE '+
  686. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  687. stColumns : s := 'select '+
  688. 'rdb$procedure_id as recno, '+
  689. '''' + DatabaseName + ''' as catalog_name, '+
  690. ''''' as schema_name, '+
  691. 'rdb$relation_name as table_name, '+
  692. 'rdb$field_name as column name, '+
  693. 'rdb$field_position as column_position, '+
  694. '0 as column_type, '+
  695. '0 as column_datatype, '+
  696. ''''' as column_typename, '+
  697. '0 as column_subtype, '+
  698. '0 as column_precision, '+
  699. '0 as column_scale, '+
  700. '0 as column_length, '+
  701. '0 as column_nullable '+
  702. 'from '+
  703. 'rdb$relation_fields '+
  704. 'WHERE '+
  705. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  706. else
  707. DatabaseError(SMetadataUnavailable)
  708. end; {case}
  709. result := s;
  710. end;
  711. procedure TIBConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  712. var qry : TSQLQuery;
  713. begin
  714. if not assigned(Transaction) then
  715. DatabaseError(SErrConnTransactionnSet);
  716. qry := tsqlquery.Create(nil);
  717. qry.transaction := Transaction;
  718. qry.database := Self;
  719. with qry do
  720. begin
  721. ReadOnly := True;
  722. sql.clear;
  723. sql.add('select '+
  724. 'ind.rdb$index_name, '+
  725. 'ind.rdb$relation_name, '+
  726. 'ind.rdb$unique_flag, '+
  727. 'ind_seg.rdb$field_name, '+
  728. 'rel_con.rdb$constraint_type '+
  729. 'from '+
  730. 'rdb$index_segments ind_seg, '+
  731. 'rdb$indices ind '+
  732. 'left outer join '+
  733. 'rdb$relation_constraints rel_con '+
  734. 'on '+
  735. 'rel_con.rdb$index_name = ind.rdb$index_name '+
  736. 'where '+
  737. '(ind_seg.rdb$index_name = ind.rdb$index_name) and '+
  738. '(ind.rdb$relation_name=''' + UpperCase(TableName) +''') '+
  739. 'order by '+
  740. 'ind.rdb$index_name;');
  741. open;
  742. end;
  743. while not qry.eof do with IndexDefs.AddIndexDef do
  744. begin
  745. Name := trim(qry.fields[0].asstring);
  746. Fields := trim(qry.Fields[3].asstring);
  747. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  748. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  749. qry.next;
  750. while (name = qry.fields[0].asstring) and (not qry.eof) do
  751. begin
  752. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  753. qry.next;
  754. end;
  755. end;
  756. qry.close;
  757. qry.free;
  758. end;
  759. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  760. var
  761. Ext : extended;
  762. Dbl : double;
  763. Sin : single;
  764. begin
  765. case Field.Size of
  766. 4 :
  767. begin
  768. Move(CurrBuff^, Sin, 4);
  769. Dbl := Sin;
  770. end;
  771. 8 :
  772. begin
  773. Move(CurrBuff^, Dbl, 8);
  774. end;
  775. 10:
  776. begin
  777. Move(CurrBuff^, Ext, 10);
  778. Dbl := double(Ext);
  779. end;
  780. end;
  781. Move(Dbl, Buffer^, 8);
  782. end;
  783. function TIBConnection.getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  784. var
  785. iscInfoBlobMaxSegment : byte = isc_info_blob_max_segment;
  786. blobInfo : array[0..50] of byte;
  787. begin
  788. if isc_blob_info(@Fstatus, @blobHandle, sizeof(iscInfoBlobMaxSegment), @iscInfoBlobMaxSegment, sizeof(blobInfo) - 2, @blobInfo) <> 0 then
  789. CheckError('isc_blob_info', FStatus);
  790. if blobInfo[0] = isc_info_blob_max_segment then
  791. begin
  792. result := isc_vax_integer(pchar(@blobInfo[3]), isc_vax_integer(pchar(@blobInfo[1]), 2));
  793. end
  794. else
  795. CheckError('isc_blob_info', FStatus);
  796. end;
  797. function TIBConnection.CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream;
  798. const
  799. isc_segstr_eof = 335544367; // It's not defined in ibase60 but in ibase40. Would it be better to define in ibase60?
  800. var
  801. mStream : TMemoryStream;
  802. blobHandle : Isc_blob_Handle;
  803. blobSegment : pointer;
  804. blobSegLen : smallint;
  805. maxBlobSize : longInt;
  806. TransactionHandle : pointer;
  807. blobId : ISC_QUAD;
  808. begin
  809. result := nil;
  810. if mode = bmRead then begin
  811. if not field.getData(@blobId) then
  812. exit;
  813. if not assigned(Transaction) then
  814. DatabaseError(SErrConnTransactionnSet);
  815. TransactionHandle := transaction.Handle;
  816. blobHandle := nil;
  817. if isc_open_blob(@FStatus, @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, @blobId) <> 0 then
  818. CheckError('TIBConnection.CreateBlobStream', FStatus);
  819. maxBlobSize := getMaxBlobSize(blobHandle);
  820. blobSegment := AllocMem(maxBlobSize);
  821. mStream := TMemoryStream.create;
  822. while (isc_get_segment(@FStatus, @blobHandle, @blobSegLen, maxBlobSize, blobSegment) = 0) do begin
  823. mStream.writeBuffer(blobSegment^, blobSegLen);
  824. end;
  825. freemem(blobSegment);
  826. mStream.seek(0,soFromBeginning);
  827. if FStatus[1] = isc_segstr_eof then
  828. begin
  829. if isc_close_blob(@FStatus, @blobHandle) <> 0 then
  830. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  831. end
  832. else
  833. CheckError('TIBConnection.CreateBlobStream isc_get_segment', FStatus);
  834. result := mStream;
  835. end;
  836. end;
  837. end.