ibconnection.pp 28 KB

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