ibconnection.pp 32 KB

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