ibconnection.pp 32 KB

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