ibconnection.pp 30 KB

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