ibconnection.pp 32 KB

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