ibconnection.pp 30 KB

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