ibconnection.pp 30 KB

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