ibconnection.pp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. in_sqlda^.SQLvar[SQLVarNr].SQLInd^ := -1
  535. else
  536. begin
  537. if assigned(in_sqlda^.SQLvar[SQLVarNr].SQLInd) then in_sqlda^.SQLvar[SQLVarNr].SQLInd^ := 0;
  538. case AParams[ParNr].DataType of
  539. ftInteger :
  540. begin
  541. i := AParams[ParNr].AsInteger;
  542. {$R-}
  543. Move(i, in_sqlda^.SQLvar[SQLVarNr].SQLData^, in_SQLDA^.SQLVar[SQLVarNr].SQLLen);
  544. {$R+}
  545. end;
  546. ftString :
  547. begin
  548. {$R-}
  549. s := AParams[ParNr].AsString;
  550. w := length(s);
  551. if ((in_sqlda^.SQLvar[SQLVarNr].SQLType and not 1) = SQL_VARYING) then
  552. begin
  553. in_sqlda^.SQLvar[SQLVarNr].SQLLen := w;
  554. ReAllocMem(in_sqlda^.SQLvar[SQLVarNr].SQLData,in_SQLDA^.SQLVar[SQLVarNr].SQLLen+2);
  555. CurrBuff := in_sqlda^.SQLvar[SQLVarNr].SQLData;
  556. move(w,CurrBuff^,sizeof(w));
  557. inc(CurrBuff,2);
  558. end
  559. else
  560. CurrBuff := in_sqlda^.SQLvar[SQLVarNr].SQLData;
  561. Move(s[1], CurrBuff^, length(s));
  562. {$R+}
  563. end;
  564. ftDate, ftTime, ftDateTime:
  565. {$R-}
  566. SetDateTime(in_sqlda^.SQLvar[SQLVarNr].SQLData, AParams[ParNr].AsDateTime, in_SQLDA^.SQLVar[SQLVarNr].SQLType);
  567. {$R+}
  568. ftLargeInt:
  569. begin
  570. li := AParams[ParNr].AsLargeInt;
  571. {$R-}
  572. Move(li, in_sqlda^.SQLvar[SQLVarNr].SQLData^, in_SQLDA^.SQLVar[SQLVarNr].SQLLen);
  573. {$R+}
  574. end;
  575. else
  576. begin
  577. DatabaseError('This kind of parameter in not (yet) supported.',self);
  578. end;
  579. end {case}
  580. end;
  581. end;
  582. {$R+}
  583. end;
  584. function TIBConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean;
  585. var
  586. x : integer;
  587. VarcharLen : word;
  588. CurrBuff : pchar;
  589. b : longint;
  590. c : currency;
  591. begin
  592. with cursor as TIBCursor do
  593. begin
  594. {$R-}
  595. for x := 0 to SQLDA^.SQLD - 1 do
  596. if SQLDA^.SQLVar[x].AliasName = FieldDef.Name then break;
  597. if SQLDA^.SQLVar[x].AliasName <> FieldDef.Name then
  598. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  599. if assigned(SQLDA^.SQLVar[x].SQLInd) and (SQLDA^.SQLVar[x].SQLInd^ = -1) then
  600. result := false
  601. else
  602. begin
  603. with SQLDA^.SQLVar[x] do
  604. if ((SQLType and not 1) = SQL_VARYING) then
  605. begin
  606. Move(SQLData^, VarcharLen, 2);
  607. CurrBuff := SQLData + 2;
  608. end
  609. else
  610. begin
  611. CurrBuff := SQLData;
  612. VarCharLen := SQLDA^.SQLVar[x].SQLLen;
  613. end;
  614. Result := true;
  615. case FieldDef.DataType of
  616. ftBCD :
  617. begin
  618. c := 0;
  619. Move(CurrBuff^, c, SQLDA^.SQLVar[x].SQLLen);
  620. c := c*intpower(10,4+SQLDA^.SQLVar[x].SQLScale);
  621. Move(c, buffer^ , sizeof(c));
  622. end;
  623. ftInteger :
  624. begin
  625. b := 0;
  626. Move(b, Buffer^, sizeof(longint));
  627. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  628. end;
  629. ftLargeint :
  630. begin
  631. FillByte(buffer^,sizeof(LargeInt),0);
  632. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  633. end;
  634. ftDate, ftTime, ftDateTime:
  635. GetDateTime(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLType);
  636. ftString :
  637. begin
  638. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  639. PChar(Buffer + VarCharLen)^ := #0;
  640. end;
  641. ftFloat :
  642. GetFloat(CurrBuff, Buffer, FieldDef);
  643. ftBlob : begin // load the BlobIb in field's buffer
  644. FillByte(buffer^,sizeof(LargeInt),0);
  645. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  646. end
  647. else result := false;
  648. end;
  649. end;
  650. {$R+}
  651. end;
  652. end;
  653. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  654. var
  655. CTime : TTm; // C struct time
  656. STime : TSystemTime; // System time
  657. PTime : TDateTime; // Pascal time
  658. begin
  659. case (AType and not 1) of
  660. SQL_TYPE_DATE :
  661. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  662. SQL_TYPE_TIME :
  663. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  664. SQL_TIMESTAMP :
  665. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  666. end;
  667. STime.Year := CTime.tm_year + 1900;
  668. STime.Month := CTime.tm_mon + 1;
  669. STime.Day := CTime.tm_mday;
  670. STime.Hour := CTime.tm_hour;
  671. STime.Minute := CTime.tm_min;
  672. STime.Second := CTime.tm_sec;
  673. STime.Millisecond := 0;
  674. PTime := SystemTimeToDateTime(STime);
  675. Move(PTime, Buffer^, SizeOf(PTime));
  676. end;
  677. procedure TIBConnection.SetDateTime(CurrBuff: pointer; PTime : TDateTime; AType : integer);
  678. var
  679. CTime : TTm; // C struct time
  680. STime : TSystemTime; // System time
  681. begin
  682. DateTimeToSystemTime(PTime,STime);
  683. CTime.tm_year := STime.Year - 1900;
  684. CTime.tm_mon := STime.Month -1;
  685. CTime.tm_mday := STime.Day;
  686. CTime.tm_hour := STime.Hour;
  687. CTime.tm_min := STime.Minute;
  688. CTime.tm_sec := STime.Second;
  689. case (AType and not 1) of
  690. SQL_TYPE_DATE :
  691. isc_encode_sql_date(@CTime, PISC_DATE(CurrBuff));
  692. SQL_TYPE_TIME :
  693. isc_encode_sql_time(@CTime, PISC_TIME(CurrBuff));
  694. SQL_TIMESTAMP :
  695. isc_encode_timestamp(@CTime, PISC_TIMESTAMP(CurrBuff));
  696. end;
  697. end;
  698. function TIBConnection.GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  699. var s : string;
  700. begin
  701. case SchemaType of
  702. stTables : s := 'select '+
  703. 'rdb$relation_id as recno, '+
  704. '''' + DatabaseName + ''' as catalog_name, '+
  705. ''''' as schema_name, '+
  706. 'rdb$relation_name as table_name, '+
  707. '0 as table_type '+
  708. 'from '+
  709. 'rdb$relations '+
  710. 'where '+
  711. '(rdb$system_flag = 0 or rdb$system_flag is null) ' + // and rdb$view_blr is null
  712. 'order by rdb$relation_name';
  713. stSysTables : s := 'select '+
  714. 'rdb$relation_id as recno, '+
  715. '''' + DatabaseName + ''' as catalog_name, '+
  716. ''''' as schema_name, '+
  717. 'rdb$relation_name as table_name, '+
  718. '0 as table_type '+
  719. 'from '+
  720. 'rdb$relations '+
  721. 'where '+
  722. '(rdb$system_flag > 0) ' + // and rdb$view_blr is null
  723. 'order by rdb$relation_name';
  724. stProcedures : s := 'select '+
  725. 'rdb$procedure_id as recno, '+
  726. '''' + DatabaseName + ''' as catalog_name, '+
  727. ''''' as schema_name, '+
  728. 'rdb$procedure_name as proc_name, '+
  729. '0 as proc_type, '+
  730. 'rdb$procedure_inputs as in_params, '+
  731. 'rdb$procedure_outputs as out_params '+
  732. 'from '+
  733. 'rdb$procedures '+
  734. 'WHERE '+
  735. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  736. stColumns : s := 'select '+
  737. 'rdb$field_id as recno, '+
  738. '''' + DatabaseName + ''' as catalog_name, '+
  739. ''''' as schema_name, '+
  740. 'rdb$relation_name as table_name, '+
  741. 'rdb$field_name as column_name, '+
  742. 'rdb$field_position as column_position, '+
  743. '0 as column_type, '+
  744. '0 as column_datatype, '+
  745. ''''' as column_typename, '+
  746. '0 as column_subtype, '+
  747. '0 as column_precision, '+
  748. '0 as column_scale, '+
  749. '0 as column_length, '+
  750. '0 as column_nullable '+
  751. 'from '+
  752. 'rdb$relation_fields '+
  753. 'WHERE '+
  754. '(rdb$system_flag = 0 or rdb$system_flag is null) and (rdb$relation_name = ''' + Uppercase(SchemaObjectName) + ''') ' +
  755. 'order by rdb$field_name';
  756. else
  757. DatabaseError(SMetadataUnavailable)
  758. end; {case}
  759. result := s;
  760. end;
  761. procedure TIBConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  762. var qry : TSQLQuery;
  763. begin
  764. if not assigned(Transaction) then
  765. DatabaseError(SErrConnTransactionnSet);
  766. qry := tsqlquery.Create(nil);
  767. qry.transaction := Transaction;
  768. qry.database := Self;
  769. with qry do
  770. begin
  771. ReadOnly := True;
  772. sql.clear;
  773. sql.add('select '+
  774. 'ind.rdb$index_name, '+
  775. 'ind.rdb$relation_name, '+
  776. 'ind.rdb$unique_flag, '+
  777. 'ind_seg.rdb$field_name, '+
  778. 'rel_con.rdb$constraint_type '+
  779. 'from '+
  780. 'rdb$index_segments ind_seg, '+
  781. 'rdb$indices ind '+
  782. 'left outer join '+
  783. 'rdb$relation_constraints rel_con '+
  784. 'on '+
  785. 'rel_con.rdb$index_name = ind.rdb$index_name '+
  786. 'where '+
  787. '(ind_seg.rdb$index_name = ind.rdb$index_name) and '+
  788. '(ind.rdb$relation_name=''' + UpperCase(TableName) +''') '+
  789. 'order by '+
  790. 'ind.rdb$index_name;');
  791. open;
  792. end;
  793. while not qry.eof do with IndexDefs.AddIndexDef do
  794. begin
  795. Name := trim(qry.fields[0].asstring);
  796. Fields := trim(qry.Fields[3].asstring);
  797. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  798. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  799. qry.next;
  800. while (name = qry.fields[0].asstring) and (not qry.eof) do
  801. begin
  802. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  803. qry.next;
  804. end;
  805. end;
  806. qry.close;
  807. qry.free;
  808. end;
  809. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Field : TFieldDef);
  810. var
  811. Ext : extended;
  812. Dbl : double;
  813. Sin : single;
  814. begin
  815. case Field.Size of
  816. 4 :
  817. begin
  818. Move(CurrBuff^, Sin, 4);
  819. Dbl := Sin;
  820. end;
  821. 8 :
  822. begin
  823. Move(CurrBuff^, Dbl, 8);
  824. end;
  825. 10:
  826. begin
  827. Move(CurrBuff^, Ext, 10);
  828. Dbl := double(Ext);
  829. end;
  830. end;
  831. Move(Dbl, Buffer^, 8);
  832. end;
  833. function TIBConnection.getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  834. var
  835. iscInfoBlobMaxSegment : byte = isc_info_blob_max_segment;
  836. blobInfo : array[0..50] of byte;
  837. begin
  838. if isc_blob_info(@Fstatus, @blobHandle, sizeof(iscInfoBlobMaxSegment), @iscInfoBlobMaxSegment, sizeof(blobInfo) - 2, @blobInfo) <> 0 then
  839. CheckError('isc_blob_info', FStatus);
  840. if blobInfo[0] = isc_info_blob_max_segment then
  841. begin
  842. result := isc_vax_integer(pchar(@blobInfo[3]), isc_vax_integer(pchar(@blobInfo[1]), 2));
  843. end
  844. else
  845. CheckError('isc_blob_info', FStatus);
  846. end;
  847. function TIBConnection.CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream;
  848. const
  849. isc_segstr_eof = 335544367; // It's not defined in ibase60 but in ibase40. Would it be better to define in ibase60?
  850. var
  851. mStream : TMemoryStream;
  852. blobHandle : Isc_blob_Handle;
  853. blobSegment : pointer;
  854. blobSegLen : smallint;
  855. maxBlobSize : longInt;
  856. TransactionHandle : pointer;
  857. blobId : ISC_QUAD;
  858. begin
  859. result := nil;
  860. if mode = bmRead then begin
  861. if not field.getData(@blobId) then
  862. exit;
  863. if not assigned(Transaction) then
  864. DatabaseError(SErrConnTransactionnSet);
  865. TransactionHandle := transaction.Handle;
  866. blobHandle := nil;
  867. if isc_open_blob(@FStatus, @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, @blobId) <> 0 then
  868. CheckError('TIBConnection.CreateBlobStream', FStatus);
  869. maxBlobSize := getMaxBlobSize(blobHandle);
  870. blobSegment := AllocMem(maxBlobSize);
  871. mStream := TMemoryStream.create;
  872. while (isc_get_segment(@FStatus, @blobHandle, @blobSegLen, maxBlobSize, blobSegment) = 0) do begin
  873. mStream.writeBuffer(blobSegment^, blobSegLen);
  874. end;
  875. freemem(blobSegment);
  876. mStream.seek(0,soFromBeginning);
  877. if FStatus[1] = isc_segstr_eof then
  878. begin
  879. if isc_close_blob(@FStatus, @blobHandle) <> 0 then
  880. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  881. end
  882. else
  883. CheckError('TIBConnection.CreateBlobStream isc_get_segment', FStatus);
  884. result := mStream;
  885. end;
  886. end;
  887. end.