ibconnection.pp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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 : PByte;
  18. FinFieldFlag : PByte;
  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 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. 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. curs.FPrepared := False;
  344. AllocSQLDA(curs.SQLDA,1);
  345. AllocSQLDA(curs.in_SQLDA,1);
  346. result := curs;
  347. end;
  348. procedure TIBConnection.DeAllocateCursorHandle(var cursor : TSQLCursor);
  349. begin
  350. if assigned(cursor) then with cursor as TIBCursor do
  351. begin
  352. reAllocMem(SQLDA,0);
  353. reAllocMem(in_SQLDA,0);
  354. reAllocMem(FFieldFlag,0);
  355. reAllocMem(FInFieldFlag,0);
  356. end;
  357. FreeAndNil(cursor);
  358. end;
  359. Function TIBConnection.AllocateTransactionHandle : TSQLHandle;
  360. begin
  361. result := TIBTrans.create;
  362. end;
  363. procedure TIBConnection.PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams);
  364. var dh : pointer;
  365. tr : pointer;
  366. p : pchar;
  367. x : shortint;
  368. i : integer;
  369. begin
  370. with cursor as TIBcursor do
  371. begin
  372. dh := GetHandle;
  373. if isc_dsql_allocate_statement(@Status, @dh, @Statement) <> 0 then
  374. CheckError('PrepareStatement', Status);
  375. tr := aTransaction.Handle;
  376. if assigned(AParams) and (AParams.count > 0) then
  377. begin
  378. SetLength(ParamBinding,0);
  379. i := posex(':',buf);
  380. while i > 0 do
  381. begin
  382. inc(i);
  383. p := @buf[i];
  384. repeat
  385. inc(p);
  386. until (p^ in SQLDelimiterCharacters);
  387. SetLength(ParamBinding,length(ParamBinding)+1);
  388. parambinding[high(parambinding)] := AParams.ParamByName(copy(buf,i,p-@buf[i])).Index;
  389. i := posex(':',buf,i);
  390. end;
  391. for x := 0 to AParams.count-1 do
  392. buf := stringreplace(buf,':'+AParams[x].Name,'?',[rfReplaceAll,rfIgnoreCase]);
  393. end;
  394. if isc_dsql_prepare(@Status, @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  395. CheckError('PrepareStatement', Status);
  396. FPrepared := True;
  397. if assigned(AParams) and (AParams.count > 0) then
  398. begin
  399. AllocSQLDA(in_SQLDA,Length(ParamBinding));
  400. if isc_dsql_describe_bind(@Status, @Statement, 1, in_SQLDA) <> 0 then
  401. CheckError('PrepareStatement', Status);
  402. if in_SQLDA^.SQLD > in_SQLDA^.SQLN then
  403. DatabaseError(SParameterCountIncorrect,self);
  404. {$R-}
  405. ReAllocMem(FInFieldFlag,SQLDA^.SQLD+1);
  406. for x := 0 to in_SQLDA^.SQLD - 1 do with in_SQLDA^.SQLVar[x] do
  407. begin
  408. if ((SQLType and not 1) = SQL_VARYING) then
  409. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen+2)
  410. else
  411. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen);
  412. SQLInd := @FinFieldFlag[x];
  413. end;
  414. {$R+}
  415. end;
  416. if FStatementType = stselect then
  417. begin
  418. FPrepared := False;
  419. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  420. CheckError('PrepareSelect', Status);
  421. if SQLDA^.SQLD > SQLDA^.SQLN then
  422. begin
  423. AllocSQLDA(SQLDA,SQLDA^.SQLD);
  424. if isc_dsql_describe(@Status, @Statement, 1, SQLDA) <> 0 then
  425. CheckError('PrepareSelect', Status);
  426. end;
  427. {$R-}
  428. ReAllocMem(FFieldFlag,SQLDA^.SQLD+1);
  429. for x := 0 to SQLDA^.SQLD - 1 do with SQLDA^.SQLVar[x] do
  430. begin
  431. if ((SQLType and not 1) = SQL_VARYING) then
  432. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen+2)
  433. // ReAllocMem(SQLData,SQLDA^.SQLVar[x].SQLLen+2)
  434. else
  435. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  436. // ReAllocMem(SQLData,SQLDA^.SQLVar[x].SQLLen);
  437. SQLInd := @FFieldFlag[x];
  438. end;
  439. {$R+}
  440. end;
  441. end;
  442. end;
  443. procedure TIBConnection.UnPrepareStatement(cursor : TSQLCursor);
  444. begin
  445. with cursor as TIBcursor do
  446. begin
  447. if isc_dsql_free_statement(@Status, @Statement, DSQL_Drop) <> 0 then
  448. CheckError('FreeStatement', Status);
  449. Statement := nil;
  450. FPrepared := False;
  451. end;
  452. end;
  453. procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor);
  454. var
  455. x : shortint;
  456. begin
  457. {$R-}
  458. with cursor as TIBCursor do
  459. for x := 0 to SQLDA^.SQLD - 1 do
  460. reAllocMem(SQLDA^.SQLVar[x].SQLData,0);
  461. {$R+}
  462. end;
  463. procedure TIBConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams);
  464. var tr : pointer;
  465. begin
  466. tr := aTransaction.Handle;
  467. if Assigned(APArams) and (AParams.count > 0) then SetParameters(cursor, AParams);
  468. with cursor as TIBCursor do
  469. if isc_dsql_execute2(@Status, @tr, @Statement, 1, in_SQLDA, nil) <> 0 then
  470. CheckError('Execute', Status);
  471. end;
  472. procedure TIBConnection.AddFieldDefs(cursor: TSQLCursor;FieldDefs : TfieldDefs);
  473. var
  474. x : integer;
  475. lenset : boolean;
  476. TransLen : word;
  477. TransType : TFieldType;
  478. FD : TFieldDef;
  479. begin
  480. {$R-}
  481. with cursor as TIBCursor do
  482. begin
  483. for x := 0 to SQLDA^.SQLD - 1 do
  484. begin
  485. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].SQLLen, SQLDA^.SQLVar[x].SQLScale,
  486. lenset, TransType, TransLen);
  487. FD := TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].AliasName, TransType,
  488. TransLen, False, (x + 1));
  489. if TransType = ftBCD then FD.precision := SQLDA^.SQLVar[x].SQLLen;
  490. FD.DisplayName := SQLDA^.SQLVar[x].AliasName;
  491. end;
  492. end;
  493. {$R+}
  494. end;
  495. function TIBConnection.GetHandle: pointer;
  496. begin
  497. Result := FSQLDatabaseHandle;
  498. end;
  499. function TIBConnection.Fetch(cursor : TSQLCursor) : boolean;
  500. var
  501. retcode : integer;
  502. begin
  503. with cursor as TIBCursor do
  504. begin
  505. retcode := isc_dsql_fetch(@Status, @Statement, 1, SQLDA);
  506. if (retcode <> 0) and (retcode <> 100) then
  507. CheckError('Fetch', Status);
  508. end;
  509. Result := (retcode <> 100);
  510. end;
  511. procedure TIBConnection.SetParameters(cursor : TSQLCursor;AParams : TParams);
  512. var ParNr,SQLVarNr : integer;
  513. s : string;
  514. i : integer;
  515. currbuff : pchar;
  516. w : word;
  517. begin
  518. {$R-}
  519. with cursor as TIBCursor do for SQLVarNr := 0 to High(ParamBinding){AParams.count-1} do
  520. begin
  521. ParNr := ParamBinding[SQLVarNr];
  522. if AParams[ParNr].IsNull then
  523. in_sqlda^.SQLvar[SQLVarNr].SQLInd^ := -1
  524. else
  525. begin
  526. in_sqlda^.SQLvar[SQLVarNr].SQLInd^ := 0;
  527. case AParams[ParNr].DataType of
  528. ftInteger :
  529. begin
  530. i := AParams[ParNr].AsInteger;
  531. Move(i, in_sqlda^.SQLvar[SQLVarNr].SQLData^, in_SQLDA^.SQLVar[SQLVarNr].SQLLen);
  532. end;
  533. ftString :
  534. begin
  535. {$R-}
  536. s := AParams[ParNr].AsString;
  537. w := length(s);
  538. if ((in_sqlda^.SQLvar[SQLVarNr].SQLType and not 1) = SQL_VARYING) then
  539. begin
  540. in_sqlda^.SQLvar[SQLVarNr].SQLLen := w;
  541. in_sqlda^.SQLvar[SQLVarNr].SQLData := AllocMem(in_SQLDA^.SQLVar[SQLVarNr].SQLLen+2)
  542. end;
  543. CurrBuff := in_sqlda^.SQLvar[SQLVarNr].SQLData;
  544. move(w,CurrBuff^,sizeof(w));
  545. inc(CurrBuff,2);
  546. Move(s[1], CurrBuff^, length(s));
  547. {$R+}
  548. end;
  549. else
  550. begin
  551. DatabaseError('This kind of parameter in not (yet) supported.',self);
  552. end;
  553. end {case}
  554. end;
  555. end;
  556. {$R+}
  557. end;
  558. function TIBConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean;
  559. var
  560. x : integer;
  561. VarcharLen : word;
  562. CurrBuff : pchar;
  563. b : longint;
  564. li : largeint;
  565. c : currency;
  566. begin
  567. with cursor as TIBCursor do
  568. begin
  569. {$R-}
  570. for x := 0 to SQLDA^.SQLD - 1 do
  571. if SQLDA^.SQLVar[x].AliasName = FieldDef.Name then break;
  572. if SQLDA^.SQLVar[x].AliasName <> FieldDef.Name then
  573. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  574. if SQLDA^.SQLVar[x].SQLInd^ = -1 then
  575. result := false
  576. else
  577. begin
  578. with SQLDA^.SQLVar[x] do
  579. if ((SQLType and not 1) = SQL_VARYING) then
  580. begin
  581. Move(SQLData^, VarcharLen, 2);
  582. CurrBuff := SQLData + 2;
  583. end
  584. else
  585. begin
  586. CurrBuff := SQLData;
  587. VarCharLen := SQLDA^.SQLVar[x].SQLLen;
  588. end;
  589. Result := true;
  590. case FieldDef.DataType of
  591. ftBCD :
  592. begin
  593. c := 0;
  594. Move(CurrBuff^, c, SQLDA^.SQLVar[x].SQLLen);
  595. c := c*intpower(10,4+SQLDA^.SQLVar[x].SQLScale);
  596. Move(c, buffer^ , sizeof(c));
  597. end;
  598. ftInteger :
  599. begin
  600. b := 0;
  601. Move(b, Buffer^, sizeof(longint));
  602. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  603. end;
  604. ftLargeint :
  605. begin
  606. li := 0;
  607. Move(CurrBuff^, li, SQLDA^.SQLVar[x].SQLLen);
  608. if SQLDA^.SQLVar[x].SQLScale > 0 then
  609. li := li * trunc(intpower(10, SQLDA^.SQLVar[x].SQLScale))
  610. else if SQLDA^.SQLVar[x].SQLScale < 0 then
  611. li := li div trunc(intpower(10, -SQLDA^.SQLVar[x].SQLScale));
  612. Move(li, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  613. end;
  614. ftDate, ftTime, ftDateTime:
  615. GetDateTime(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLType);
  616. ftString :
  617. begin
  618. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  619. PChar(Buffer + VarCharLen)^ := #0;
  620. end;
  621. ftFloat :
  622. GetFloat(CurrBuff, Buffer, FieldDef);
  623. ftBlob : begin // load the BlobIb in field's buffer
  624. li := 0;
  625. Move(li, Buffer^, sizeof(largeint));
  626. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  627. end
  628. else result := false;
  629. end;
  630. end;
  631. {$R+}
  632. end;
  633. end;
  634. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  635. var
  636. CTime : TTm; // C struct time
  637. STime : TSystemTime; // System time
  638. PTime : TDateTime; // Pascal time
  639. begin
  640. case (AType and not 1) of
  641. SQL_TYPE_DATE :
  642. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  643. SQL_TYPE_TIME :
  644. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  645. SQL_TIMESTAMP :
  646. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  647. end;
  648. STime.Year := CTime.tm_year + 1900;
  649. STime.Month := CTime.tm_mon + 1;
  650. STime.Day := CTime.tm_mday;
  651. STime.Hour := CTime.tm_hour;
  652. STime.Minute := CTime.tm_min;
  653. STime.Second := CTime.tm_sec;
  654. STime.Millisecond := 0;
  655. PTime := SystemTimeToDateTime(STime);
  656. Move(PTime, Buffer^, SizeOf(PTime));
  657. end;
  658. function TIBConnection.GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  659. var s : string;
  660. begin
  661. case SchemaType of
  662. stTables : s := 'select '+
  663. 'rdb$relation_id as recno, '+
  664. '''' + DatabaseName + ''' as catalog_name, '+
  665. ''''' as schema_name, '+
  666. 'rdb$relation_name as table_name, '+
  667. '0 as table_type '+
  668. 'from '+
  669. 'rdb$relations '+
  670. 'where '+
  671. '(rdb$system_flag = 0 or rdb$system_flag is null) ' + // and rdb$view_blr is null
  672. 'order by rdb$relation_name';
  673. stSysTables : s := 'select '+
  674. 'rdb$relation_id as recno, '+
  675. '''' + DatabaseName + ''' as catalog_name, '+
  676. ''''' as schema_name, '+
  677. 'rdb$relation_name as table_name, '+
  678. '0 as table_type '+
  679. 'from '+
  680. 'rdb$relations '+
  681. 'where '+
  682. '(rdb$system_flag > 0) ' + // and rdb$view_blr is null
  683. 'order by rdb$relation_name';
  684. stProcedures : s := 'select '+
  685. 'rdb$procedure_id as recno, '+
  686. '''' + DatabaseName + ''' as catalog_name, '+
  687. ''''' as schema_name, '+
  688. 'rdb$procedure_name as proc_name, '+
  689. '0 as proc_type, '+
  690. 'rdb$procedure_inputs as in_params, '+
  691. 'rdb$procedure_outputs as out_params '+
  692. 'from '+
  693. 'rdb$procedures '+
  694. 'WHERE '+
  695. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  696. stColumns : s := 'select '+
  697. 'rdb$field_id as recno, '+
  698. '''' + DatabaseName + ''' as catalog_name, '+
  699. ''''' as schema_name, '+
  700. 'rdb$relation_name as table_name, '+
  701. 'rdb$field_name as column_name, '+
  702. 'rdb$field_position as column_position, '+
  703. '0 as column_type, '+
  704. '0 as column_datatype, '+
  705. ''''' as column_typename, '+
  706. '0 as column_subtype, '+
  707. '0 as column_precision, '+
  708. '0 as column_scale, '+
  709. '0 as column_length, '+
  710. '0 as column_nullable '+
  711. 'from '+
  712. 'rdb$relation_fields '+
  713. 'WHERE '+
  714. '(rdb$system_flag = 0 or rdb$system_flag is null) and (rdb$relation_name = ''' + Uppercase(SchemaObjectName) + ''') ' +
  715. 'order by rdb$field_name';
  716. else
  717. DatabaseError(SMetadataUnavailable)
  718. end; {case}
  719. result := s;
  720. end;
  721. procedure TIBConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  722. var qry : TSQLQuery;
  723. begin
  724. if not assigned(Transaction) then
  725. DatabaseError(SErrConnTransactionnSet);
  726. qry := tsqlquery.Create(nil);
  727. qry.transaction := Transaction;
  728. qry.database := Self;
  729. with qry do
  730. begin
  731. ReadOnly := True;
  732. sql.clear;
  733. sql.add('select '+
  734. 'ind.rdb$index_name, '+
  735. 'ind.rdb$relation_name, '+
  736. 'ind.rdb$unique_flag, '+
  737. 'ind_seg.rdb$field_name, '+
  738. 'rel_con.rdb$constraint_type '+
  739. 'from '+
  740. 'rdb$index_segments ind_seg, '+
  741. 'rdb$indices ind '+
  742. 'left outer join '+
  743. 'rdb$relation_constraints rel_con '+
  744. 'on '+
  745. 'rel_con.rdb$index_name = ind.rdb$index_name '+
  746. 'where '+
  747. '(ind_seg.rdb$index_name = ind.rdb$index_name) and '+
  748. '(ind.rdb$relation_name=''' + UpperCase(TableName) +''') '+
  749. 'order by '+
  750. 'ind.rdb$index_name;');
  751. open;
  752. end;
  753. while not qry.eof do with IndexDefs.AddIndexDef do
  754. begin
  755. Name := trim(qry.fields[0].asstring);
  756. Fields := trim(qry.Fields[3].asstring);
  757. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  758. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  759. qry.next;
  760. while (name = qry.fields[0].asstring) and (not qry.eof) do
  761. begin
  762. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  763. qry.next;
  764. end;
  765. end;
  766. qry.close;
  767. qry.free;
  768. end;
  769. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  770. var
  771. Ext : extended;
  772. Dbl : double;
  773. Sin : single;
  774. begin
  775. case Field.Size of
  776. 4 :
  777. begin
  778. Move(CurrBuff^, Sin, 4);
  779. Dbl := Sin;
  780. end;
  781. 8 :
  782. begin
  783. Move(CurrBuff^, Dbl, 8);
  784. end;
  785. 10:
  786. begin
  787. Move(CurrBuff^, Ext, 10);
  788. Dbl := double(Ext);
  789. end;
  790. end;
  791. Move(Dbl, Buffer^, 8);
  792. end;
  793. function TIBConnection.getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  794. var
  795. iscInfoBlobMaxSegment : byte = isc_info_blob_max_segment;
  796. blobInfo : array[0..50] of byte;
  797. begin
  798. if isc_blob_info(@Fstatus, @blobHandle, sizeof(iscInfoBlobMaxSegment), @iscInfoBlobMaxSegment, sizeof(blobInfo) - 2, @blobInfo) <> 0 then
  799. CheckError('isc_blob_info', FStatus);
  800. if blobInfo[0] = isc_info_blob_max_segment then
  801. begin
  802. result := isc_vax_integer(pchar(@blobInfo[3]), isc_vax_integer(pchar(@blobInfo[1]), 2));
  803. end
  804. else
  805. CheckError('isc_blob_info', FStatus);
  806. end;
  807. function TIBConnection.CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream;
  808. const
  809. isc_segstr_eof = 335544367; // It's not defined in ibase60 but in ibase40. Would it be better to define in ibase60?
  810. var
  811. mStream : TMemoryStream;
  812. blobHandle : Isc_blob_Handle;
  813. blobSegment : pointer;
  814. blobSegLen : smallint;
  815. maxBlobSize : longInt;
  816. TransactionHandle : pointer;
  817. blobId : ISC_QUAD;
  818. begin
  819. result := nil;
  820. if mode = bmRead then begin
  821. if not field.getData(@blobId) then
  822. exit;
  823. if not assigned(Transaction) then
  824. DatabaseError(SErrConnTransactionnSet);
  825. TransactionHandle := transaction.Handle;
  826. blobHandle := nil;
  827. if isc_open_blob(@FStatus, @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, @blobId) <> 0 then
  828. CheckError('TIBConnection.CreateBlobStream', FStatus);
  829. maxBlobSize := getMaxBlobSize(blobHandle);
  830. blobSegment := AllocMem(maxBlobSize);
  831. mStream := TMemoryStream.create;
  832. while (isc_get_segment(@FStatus, @blobHandle, @blobSegLen, maxBlobSize, blobSegment) = 0) do begin
  833. mStream.writeBuffer(blobSegment^, blobSegLen);
  834. end;
  835. freemem(blobSegment);
  836. mStream.seek(0,soFromBeginning);
  837. if FStatus[1] = isc_segstr_eof then
  838. begin
  839. if isc_close_blob(@FStatus, @blobHandle) <> 0 then
  840. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  841. end
  842. else
  843. CheckError('TIBConnection.CreateBlobStream isc_get_segment', FStatus);
  844. result := mStream;
  845. end;
  846. end;
  847. end.