ibconnection.pp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. unit IBConnection;
  2. {$mode objfpc}{$H+}
  3. {$Define LinkDynamically}
  4. interface
  5. uses
  6. Classes, SysUtils, sqldb, db, math, dbconst, bufdataset,
  7. {$IfDef LinkDynamically}
  8. ibase60dyn;
  9. {$Else}
  10. ibase60;
  11. {$EndIf}
  12. const
  13. DEFDIALECT = 3;
  14. MAXBLOBSEGMENTSIZE = 65535; //Maximum number of bytes that fit in a blob segment.
  15. type
  16. EIBDatabaseError = class(EDatabaseError)
  17. public
  18. GDSErrorCode : Longint;
  19. end;
  20. { TIBCursor }
  21. TIBCursor = Class(TSQLCursor)
  22. protected
  23. Status : array [0..19] of ISC_STATUS;
  24. Statement : pointer;
  25. SQLDA : PXSQLDA;
  26. in_SQLDA : PXSQLDA;
  27. ParamBinding : array of integer;
  28. FieldBinding : array of integer;
  29. end;
  30. TIBTrans = Class(TSQLHandle)
  31. protected
  32. TransactionHandle : pointer;
  33. TPB : string; // Transaction parameter buffer
  34. Status : array [0..19] of ISC_STATUS;
  35. end;
  36. { TIBConnection }
  37. TIBConnection = class (TSQLConnection)
  38. private
  39. FSQLDatabaseHandle : pointer;
  40. FStatus : array [0..19] of ISC_STATUS;
  41. FDialect : integer;
  42. FDBDialect : integer;
  43. FBLobSegmentSize : word; //required for backward compatibilty; not used
  44. procedure ConnectFB;
  45. function GetDialect: integer;
  46. procedure AllocSQLDA(var aSQLDA : PXSQLDA;Count : integer);
  47. procedure TranslateFldType(SQLType, SQLSubType, SQLLen, SQLScale : integer;
  48. var TrType : TFieldType; var TrLen : word);
  49. // conversion methods
  50. procedure GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  51. procedure SetDateTime(CurrBuff: pointer; PTime : TDateTime; AType : integer);
  52. procedure GetFloat(CurrBuff, Buffer : pointer; Size : Byte);
  53. procedure SetFloat(CurrBuff: pointer; Dbl: Double; Size: integer);
  54. procedure CheckError(ProcName : string; Status : PISC_STATUS);
  55. procedure SetParameters(cursor : TSQLCursor; aTransation : TSQLTransaction; AParams : TParams);
  56. procedure FreeSQLDABuffer(var aSQLDA : PXSQLDA);
  57. function IsDialectStored: boolean;
  58. protected
  59. procedure DoConnect; override;
  60. procedure DoInternalConnect; override;
  61. procedure DoInternalDisconnect; override;
  62. function GetHandle : pointer; override;
  63. Function AllocateCursorHandle : TSQLCursor; override;
  64. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  65. Function AllocateTransactionHandle : TSQLHandle; override;
  66. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  67. procedure UnPrepareStatement(cursor : TSQLCursor); override;
  68. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  69. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
  70. procedure AddFieldDefs(cursor: TSQLCursor;FieldDefs : TfieldDefs); override;
  71. function Fetch(cursor : TSQLCursor) : boolean; override;
  72. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; override;
  73. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  74. function Commit(trans : TSQLHandle) : boolean; override;
  75. function RollBack(trans : TSQLHandle) : boolean; override;
  76. function StartdbTransaction(trans : TSQLHandle; AParams : string) : boolean; override;
  77. procedure CommitRetaining(trans : TSQLHandle); override;
  78. procedure RollBackRetaining(trans : TSQLHandle); override;
  79. procedure UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string); override;
  80. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  81. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); override;
  82. function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
  83. public
  84. constructor Create(AOwner : TComponent); override;
  85. procedure CreateDB; override;
  86. procedure DropDB; override;
  87. //Segment size is not used in the code; property kept for backward compatibility
  88. property BlobSegmentSize : word read FBlobSegmentSize write FBlobSegmentSize; deprecated;
  89. function GetDBDialect: integer;
  90. published
  91. property DatabaseName;
  92. property Dialect : integer read GetDialect write FDialect stored IsDialectStored default DEFDIALECT;
  93. property KeepConnection;
  94. property LoginPrompt;
  95. property Params;
  96. property OnLogin;
  97. end;
  98. { TIBConnectionDef }
  99. TIBConnectionDef = Class(TConnectionDef)
  100. Class Function TypeName : String; override;
  101. Class Function ConnectionClass : TSQLConnectionClass; override;
  102. Class Function Description : String; override;
  103. end;
  104. implementation
  105. uses
  106. strutils, FmtBCD;
  107. type
  108. TTm = packed record
  109. tm_sec : longint;
  110. tm_min : longint;
  111. tm_hour : longint;
  112. tm_mday : longint;
  113. tm_mon : longint;
  114. tm_year : longint;
  115. tm_wday : longint;
  116. tm_yday : longint;
  117. tm_isdst : longint;
  118. __tm_gmtoff : longint;
  119. __tm_zone : Pchar;
  120. end;
  121. procedure TIBConnection.CheckError(ProcName : string; Status : PISC_STATUS);
  122. var
  123. buf : array [0..1023] of char;
  124. Msg : string;
  125. E : EIBDatabaseError;
  126. Err : longint;
  127. begin
  128. if ((Status[0] = 1) and (Status[1] <> 0)) then
  129. begin
  130. Err := Status[1];
  131. msg := '';
  132. while isc_interprete(Buf, @Status) > 0 do
  133. Msg := Msg + LineEnding +' -' + StrPas(Buf);
  134. E := EIBDatabaseError.CreateFmt('%s : %s : %s',[self.Name,ProcName,Msg]);
  135. E.GDSErrorCode := Err;
  136. Raise E;
  137. end;
  138. end;
  139. constructor TIBConnection.Create(AOwner : TComponent);
  140. begin
  141. inherited;
  142. FConnOptions := FConnOptions + [sqSupportParams] + [sqEscapeRepeat];
  143. FieldNameQuoteChars:=DoubleQuotes;
  144. FBLobSegmentSize := 65535; //Shows we're using the maximum segment size
  145. FDialect := -1;
  146. FDBDialect := -1;
  147. end;
  148. function TIBConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  149. begin
  150. Result := (trans as TIBtrans).TransactionHandle;
  151. end;
  152. function TIBConnection.Commit(trans : TSQLHandle) : boolean;
  153. begin
  154. result := false;
  155. with (trans as TIBTrans) do
  156. if isc_commit_transaction(@Status[0], @TransactionHandle) <> 0 then
  157. CheckError('Commit', Status)
  158. else result := true;
  159. end;
  160. function TIBConnection.RollBack(trans : TSQLHandle) : boolean;
  161. begin
  162. result := false;
  163. if isc_rollback_transaction(@TIBTrans(trans).Status[0], @TIBTrans(trans).TransactionHandle) <> 0 then
  164. CheckError('Rollback', TIBTrans(trans).Status)
  165. else result := true;
  166. end;
  167. function TIBConnection.StartDBTransaction(trans : TSQLHandle;AParams : String) : boolean;
  168. var
  169. DBHandle : pointer;
  170. tr : TIBTrans;
  171. i : integer;
  172. s : string;
  173. begin
  174. result := false;
  175. DBHandle := GetHandle;
  176. tr := trans as TIBtrans;
  177. with tr do
  178. begin
  179. TPB := chr(isc_tpb_version3);
  180. i := 1;
  181. s := ExtractSubStr(AParams,i,stdWordDelims);
  182. while s <> '' do
  183. begin
  184. if s='isc_tpb_write' then TPB := TPB + chr(isc_tpb_write)
  185. else if s='isc_tpb_read' then TPB := TPB + chr(isc_tpb_read)
  186. else if s='isc_tpb_consistency' then TPB := TPB + chr(isc_tpb_consistency)
  187. else if s='isc_tpb_concurrency' then TPB := TPB + chr(isc_tpb_concurrency)
  188. else if s='isc_tpb_read_committed' then TPB := TPB + chr(isc_tpb_read_committed)
  189. else if s='isc_tpb_rec_version' then TPB := TPB + chr(isc_tpb_rec_version)
  190. else if s='isc_tpb_no_rec_version' then TPB := TPB + chr(isc_tpb_no_rec_version)
  191. else if s='isc_tpb_wait' then TPB := TPB + chr(isc_tpb_wait)
  192. else if s='isc_tpb_nowait' then TPB := TPB + chr(isc_tpb_nowait)
  193. else if s='isc_tpb_shared' then TPB := TPB + chr(isc_tpb_shared)
  194. else if s='isc_tpb_protected' then TPB := TPB + chr(isc_tpb_protected)
  195. else if s='isc_tpb_exclusive' then TPB := TPB + chr(isc_tpb_exclusive)
  196. else if s='isc_tpb_lock_read' then TPB := TPB + chr(isc_tpb_lock_read)
  197. else if s='isc_tpb_lock_write' then TPB := TPB + chr(isc_tpb_lock_write)
  198. else if s='isc_tpb_verb_time' then TPB := TPB + chr(isc_tpb_verb_time)
  199. else if s='isc_tpb_commit_time' then TPB := TPB + chr(isc_tpb_commit_time)
  200. else if s='isc_tpb_ignore_limbo' then TPB := TPB + chr(isc_tpb_ignore_limbo)
  201. else if s='isc_tpb_autocommit' then TPB := TPB + chr(isc_tpb_autocommit)
  202. else if s='isc_tpb_restart_requests' then TPB := TPB + chr(isc_tpb_restart_requests)
  203. else if s='isc_tpb_no_auto_undo' then TPB := TPB + chr(isc_tpb_no_auto_undo);
  204. s := ExtractSubStr(AParams,i,stdWordDelims);
  205. end;
  206. TransactionHandle := nil;
  207. if isc_start_transaction(@Status[0], @TransactionHandle, 1,
  208. [@DBHandle, Length(TPB), @TPB[1]]) <> 0 then
  209. CheckError('StartTransaction',Status)
  210. else Result := True;
  211. end;
  212. end;
  213. procedure TIBConnection.CommitRetaining(trans : TSQLHandle);
  214. begin
  215. with trans as TIBtrans do
  216. if isc_commit_retaining(@Status[0], @TransactionHandle) <> 0 then
  217. CheckError('CommitRetaining', Status);
  218. end;
  219. procedure TIBConnection.RollBackRetaining(trans : TSQLHandle);
  220. begin
  221. with trans as TIBtrans do
  222. if isc_rollback_retaining(@Status[0], @TransactionHandle) <> 0 then
  223. CheckError('RollBackRetaining', Status);
  224. end;
  225. procedure TIBConnection.DropDB;
  226. begin
  227. CheckDisConnected;
  228. {$IfDef LinkDynamically}
  229. InitialiseIBase60;
  230. {$EndIf}
  231. ConnectFB;
  232. if isc_drop_database(@FStatus[0], @FSQLDatabaseHandle) <> 0 then
  233. CheckError('DropDB', FStatus);
  234. {$IfDef LinkDynamically}
  235. ReleaseIBase60;
  236. {$EndIf}
  237. end;
  238. procedure TIBConnection.CreateDB;
  239. var ASQLDatabaseHandle,
  240. ASQLTransactionHandle : pointer;
  241. CreateSQL : String;
  242. pagesize : String;
  243. begin
  244. CheckDisConnected;
  245. {$IfDef LinkDynamically}
  246. InitialiseIBase60;
  247. {$EndIf}
  248. ASQLDatabaseHandle := nil;
  249. ASQLTransactionHandle := nil;
  250. CreateSQL := 'CREATE DATABASE ';
  251. if HostName <> '' then CreateSQL := CreateSQL + ''''+ HostName+':'+DatabaseName + ''''
  252. else CreateSQL := CreateSQL + '''' + DatabaseName + '''';
  253. if UserName <> '' then
  254. CreateSQL := CreateSQL + ' USER ''' + Username + '''';
  255. if Password <> '' then
  256. CreateSQL := CreateSQL + ' PASSWORD ''' + Password + '''';
  257. pagesize := params.Values['PAGE_SIZE'];
  258. if pagesize <> '' then
  259. CreateSQL := CreateSQL + ' PAGE_SIZE '+pagesize;
  260. if CharSet <> '' then
  261. CreateSQL := CreateSQL + ' DEFAULT CHARACTER SET ' + CharSet;
  262. if isc_dsql_execute_immediate(@FStatus[0],@ASQLDatabaseHandle,@ASQLTransactionHandle,length(CreateSQL),@CreateSQL[1],Dialect,nil) <> 0 then
  263. CheckError('CreateDB', FStatus);
  264. if isc_detach_database(@FStatus[0], @ASQLDatabaseHandle) <> 0 then
  265. CheckError('CreateDB', FStatus);
  266. {$IfDef LinkDynamically}
  267. ReleaseIBase60;
  268. {$EndIf}
  269. end;
  270. procedure TIBConnection.DoInternalConnect;
  271. begin
  272. {$IfDef LinkDynamically}
  273. InitialiseIBase60;
  274. {$EndIf}
  275. inherited dointernalconnect;
  276. ConnectFB;
  277. end;
  278. procedure TIBConnection.DoInternalDisconnect;
  279. begin
  280. FDialect := -1;
  281. FDBDialect := -1;
  282. if not Connected then
  283. begin
  284. FSQLDatabaseHandle := nil;
  285. Exit;
  286. end;
  287. if isc_detach_database(@FStatus[0], @FSQLDatabaseHandle) <> 0 then
  288. CheckError('Close', FStatus);
  289. {$IfDef LinkDynamically}
  290. ReleaseIBase60;
  291. {$EndIf}
  292. end;
  293. function TIBConnection.GetDBDialect: integer;
  294. var
  295. x : integer;
  296. Len : integer;
  297. Buffer : array [0..1] of byte;
  298. ResBuf : array [0..39] of byte;
  299. begin
  300. result := -1;
  301. if Connected then
  302. begin
  303. Buffer[0] := isc_info_db_sql_dialect;
  304. Buffer[1] := isc_info_end;
  305. if isc_database_info(@FStatus[0], @FSQLDatabaseHandle, Length(Buffer),
  306. pchar(@Buffer[0]), SizeOf(ResBuf), pchar(@ResBuf[0])) <> 0 then
  307. CheckError('SetDBDialect', FStatus);
  308. x := 0;
  309. while x < 40 do
  310. case ResBuf[x] of
  311. isc_info_db_sql_dialect :
  312. begin
  313. Inc(x);
  314. Len := isc_vax_integer(pchar(@ResBuf[x]), 2);
  315. Inc(x, 2);
  316. Result := isc_vax_integer(pchar(@ResBuf[x]), Len);
  317. Inc(x, Len);
  318. end;
  319. isc_info_end : Break;
  320. else
  321. inc(x);
  322. end;
  323. end;
  324. end;
  325. procedure TIBConnection.ConnectFB;
  326. var
  327. ADatabaseName: String;
  328. DPB: string;
  329. begin
  330. DPB := chr(isc_dpb_version1);
  331. if (UserName <> '') then
  332. begin
  333. DPB := DPB + chr(isc_dpb_user_name) + chr(Length(UserName)) + UserName;
  334. if (Password <> '') then
  335. DPB := DPB + chr(isc_dpb_password) + chr(Length(Password)) + Password;
  336. end;
  337. if (Role <> '') then
  338. DPB := DPB + chr(isc_dpb_sql_role_name) + chr(Length(Role)) + Role;
  339. if Length(CharSet) > 0 then
  340. DPB := DPB + Chr(isc_dpb_lc_ctype) + Chr(Length(CharSet)) + CharSet;
  341. FSQLDatabaseHandle := nil;
  342. if HostName <> '' then ADatabaseName := HostName+':'+DatabaseName
  343. else ADatabaseName := DatabaseName;
  344. if isc_attach_database(@FStatus[0], Length(ADatabaseName), @ADatabaseName[1],
  345. @FSQLDatabaseHandle,
  346. Length(DPB), @DPB[1]) <> 0 then
  347. CheckError('DoInternalConnect', FStatus);
  348. end;
  349. function TIBConnection.GetDialect: integer;
  350. begin
  351. if FDialect = -1 then
  352. begin
  353. if FDBDialect = -1 then
  354. Result := DEFDIALECT
  355. else
  356. Result := FDBDialect;
  357. end else
  358. Result := FDialect;
  359. end;
  360. procedure TIBConnection.AllocSQLDA(var aSQLDA : PXSQLDA;Count : integer);
  361. begin
  362. FreeSQLDABuffer(aSQLDA);
  363. if count > -1 then
  364. begin
  365. reAllocMem(aSQLDA, XSQLDA_Length(Count));
  366. { Zero out the memory block to avoid problems with exceptions within the
  367. constructor of this class. }
  368. FillChar(aSQLDA^, XSQLDA_Length(Count), 0);
  369. aSQLDA^.Version := sqlda_version1;
  370. aSQLDA^.SQLN := Count;
  371. end
  372. else
  373. reAllocMem(aSQLDA,0);
  374. end;
  375. procedure TIBConnection.TranslateFldType(SQLType, SQLSubType, SQLLen, SQLScale : integer;
  376. var TrType : TFieldType; var TrLen : word);
  377. begin
  378. TrLen := 0;
  379. if SQLScale < 0 then
  380. begin
  381. TrLen := abs(SQLScale);
  382. if (TrLen <= MaxBCDScale) then //Note: NUMERIC(18,3) or (17,2) must be mapped to ftFmtBCD, but we do not know Precision
  383. TrType := ftBCD
  384. else
  385. TrType := ftFMTBcd;
  386. end
  387. else case (SQLType and not 1) of
  388. SQL_VARYING :
  389. begin
  390. TrType := ftString;
  391. TrLen := SQLLen;
  392. end;
  393. SQL_TEXT :
  394. begin
  395. TrType := ftFixedChar;
  396. TrLen := SQLLen;
  397. end;
  398. SQL_TYPE_DATE :
  399. TrType := ftDate;
  400. SQL_TYPE_TIME :
  401. TrType := ftTime;
  402. SQL_TIMESTAMP :
  403. TrType := ftDateTime;
  404. SQL_ARRAY :
  405. begin
  406. TrType := ftArray;
  407. TrLen := SQLLen;
  408. end;
  409. SQL_BLOB :
  410. begin
  411. if SQLSubType = 1 then
  412. TrType := ftMemo
  413. else
  414. TrType := ftBlob;
  415. TrLen := SQLLen;
  416. end;
  417. SQL_SHORT :
  418. TrType := ftSmallint;
  419. SQL_LONG :
  420. TrType := ftInteger;
  421. SQL_INT64 :
  422. TrType := ftLargeInt;
  423. SQL_DOUBLE :
  424. TrType := ftFloat;
  425. SQL_FLOAT :
  426. TrType := ftFloat;
  427. else
  428. TrType := ftUnknown;
  429. end;
  430. end;
  431. Function TIBConnection.AllocateCursorHandle : TSQLCursor;
  432. var curs : TIBCursor;
  433. begin
  434. curs := TIBCursor.create;
  435. curs.sqlda := nil;
  436. curs.statement := nil;
  437. curs.FPrepared := False;
  438. AllocSQLDA(curs.SQLDA,0);
  439. result := curs;
  440. end;
  441. procedure TIBConnection.DeAllocateCursorHandle(var cursor : TSQLCursor);
  442. begin
  443. if assigned(cursor) then with cursor as TIBCursor do
  444. begin
  445. AllocSQLDA(SQLDA,-1);
  446. AllocSQLDA(in_SQLDA,-1);
  447. end;
  448. FreeAndNil(cursor);
  449. end;
  450. Function TIBConnection.AllocateTransactionHandle : TSQLHandle;
  451. begin
  452. result := TIBTrans.create;
  453. end;
  454. procedure TIBConnection.PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams);
  455. var dh : pointer;
  456. tr : pointer;
  457. x : Smallint;
  458. info_request : string;
  459. resbuf : array[0..7] of byte;
  460. blockSize : integer;
  461. IBStatementType: integer;
  462. begin
  463. with cursor as TIBcursor do
  464. begin
  465. dh := GetHandle;
  466. if isc_dsql_allocate_statement(@Status[0], @dh, @Statement) <> 0 then
  467. CheckError('PrepareStatement', Status);
  468. tr := aTransaction.Handle;
  469. if assigned(AParams) and (AParams.count > 0) then
  470. buf := AParams.ParseSQL(buf,false,sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions,psInterbase,paramBinding);
  471. if isc_dsql_prepare(@Status[0], @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  472. CheckError('PrepareStatement', Status);
  473. if assigned(AParams) and (AParams.count > 0) then
  474. begin
  475. AllocSQLDA(in_SQLDA,Length(ParamBinding));
  476. if isc_dsql_describe_bind(@Status[0], @Statement, 1, in_SQLDA) <> 0 then
  477. CheckError('PrepareStatement', Status);
  478. if in_SQLDA^.SQLD > in_SQLDA^.SQLN then
  479. DatabaseError(SParameterCountIncorrect,self);
  480. {$R-}
  481. for x := 0 to in_SQLDA^.SQLD - 1 do with in_SQLDA^.SQLVar[x] do
  482. begin
  483. if ((SQLType and not 1) = SQL_VARYING) then
  484. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen+2)
  485. else
  486. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen);
  487. // Always force the creation of slqind for parameters. It could be
  488. // that a database-trigger takes care of inserting null-values, so
  489. // it should always be possible to pass null-parameters. If that fails,
  490. // the database-server will generate the appropiate error.
  491. sqltype := sqltype or 1;
  492. new(sqlind);
  493. end;
  494. {$R+}
  495. end
  496. else
  497. AllocSQLDA(in_SQLDA,0);
  498. // Get the statement type from firebird/interbase
  499. info_request := chr(isc_info_sql_stmt_type);
  500. if isc_dsql_sql_info(@Status[0],@Statement,Length(info_request), @info_request[1],sizeof(resbuf),@resbuf) <> 0 then
  501. CheckError('PrepareStatement', Status);
  502. assert(resbuf[0]=isc_info_sql_stmt_type);
  503. BlockSize:=isc_vax_integer(@resbuf[1],2);
  504. IBStatementType:=isc_vax_integer(@resbuf[3],blockSize);
  505. assert(resbuf[3+blockSize]=isc_info_end);
  506. // If the statementtype is isc_info_sql_stmt_exec_procedure then
  507. // override the statement type derrived by parsing the query.
  508. // This to recognize statements like 'insert into .. returning' correctly
  509. if IBStatementType = isc_info_sql_stmt_exec_procedure then
  510. FStatementType := stExecProcedure;
  511. if FStatementType in [stSelect,stExecProcedure] then
  512. begin
  513. if isc_dsql_describe(@Status[0], @Statement, 1, SQLDA) <> 0 then
  514. CheckError('PrepareSelect', Status);
  515. if SQLDA^.SQLD > SQLDA^.SQLN then
  516. begin
  517. AllocSQLDA(SQLDA,SQLDA^.SQLD);
  518. if isc_dsql_describe(@Status[0], @Statement, 1, SQLDA) <> 0 then
  519. CheckError('PrepareSelect', Status);
  520. end;
  521. {$R-}
  522. for x := 0 to SQLDA^.SQLD - 1 do with SQLDA^.SQLVar[x] do
  523. begin
  524. if ((SQLType and not 1) = SQL_VARYING) then
  525. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen+2)
  526. else
  527. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  528. if (SQLType and 1) = 1 then New(SQLInd);
  529. end;
  530. {$R+}
  531. end;
  532. FPrepared := True;
  533. end;
  534. end;
  535. procedure TIBConnection.UnPrepareStatement(cursor : TSQLCursor);
  536. begin
  537. with cursor as TIBcursor do
  538. if assigned(Statement) Then
  539. begin
  540. if isc_dsql_free_statement(@Status[0], @Statement, DSQL_Drop) <> 0 then
  541. CheckError('FreeStatement', Status);
  542. Statement := nil;
  543. FPrepared := False;
  544. end;
  545. end;
  546. procedure TIBConnection.FreeSQLDABuffer(var aSQLDA : PXSQLDA);
  547. var x : Smallint;
  548. begin
  549. {$R-}
  550. if assigned(aSQLDA) then
  551. for x := 0 to aSQLDA^.SQLN - 1 do
  552. begin
  553. reAllocMem(aSQLDA^.SQLVar[x].SQLData,0);
  554. if assigned(aSQLDA^.SQLVar[x].sqlind) then
  555. begin
  556. Dispose(aSQLDA^.SQLVar[x].sqlind);
  557. aSQLDA^.SQLVar[x].sqlind := nil;
  558. end
  559. end;
  560. {$R+}
  561. end;
  562. function TIBConnection.IsDialectStored: boolean;
  563. begin
  564. result := (FDialect<>-1);
  565. end;
  566. procedure TIBConnection.DoConnect;
  567. begin
  568. inherited DoConnect;
  569. FDbDialect := GetDBDialect;
  570. end;
  571. procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor);
  572. begin
  573. with cursor as TIBCursor do
  574. begin
  575. FreeSQLDABuffer(SQLDA);
  576. FreeSQLDABuffer(in_SQLDA);
  577. SetLength(FieldBinding,0);
  578. end;
  579. end;
  580. procedure TIBConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams);
  581. var tr : pointer;
  582. out_SQLDA : PXSQLDA;
  583. begin
  584. tr := aTransaction.Handle;
  585. if Assigned(APArams) and (AParams.count > 0) then SetParameters(cursor, atransaction, AParams);
  586. with cursor as TIBCursor do
  587. begin
  588. if FStatementType = stExecProcedure then
  589. out_SQLDA := SQLDA
  590. else
  591. out_SQLDA := nil;
  592. if isc_dsql_execute2(@Status[0], @tr, @Statement, 1, in_SQLDA, out_SQLDA) <> 0 then
  593. CheckError('Execute', Status);
  594. end;
  595. end;
  596. procedure TIBConnection.AddFieldDefs(cursor: TSQLCursor;FieldDefs : TfieldDefs);
  597. var
  598. x : integer;
  599. TransLen : word;
  600. TransType : TFieldType;
  601. FD : TFieldDef;
  602. begin
  603. {$R-}
  604. with cursor as TIBCursor do
  605. begin
  606. setlength(FieldBinding,SQLDA^.SQLD);
  607. for x := 0 to SQLDA^.SQLD - 1 do
  608. begin
  609. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].sqlsubtype, SQLDA^.SQLVar[x].SQLLen, SQLDA^.SQLVar[x].SQLScale,
  610. TransType, TransLen);
  611. FD := TFieldDef.Create(FieldDefs, FieldDefs.MakeNameUnique(SQLDA^.SQLVar[x].AliasName), TransType,
  612. TransLen, (SQLDA^.SQLVar[x].sqltype and 1)=0, (x + 1));
  613. if TransType = ftBCD then
  614. case (SQLDA^.SQLVar[x].sqltype and not 1) of
  615. SQL_SHORT : FD.precision := 4;
  616. SQL_LONG : FD.precision := 9;
  617. SQL_INT64 : FD.precision := 18;
  618. else FD.precision := SQLDA^.SQLVar[x].SQLLen;
  619. end;
  620. // FD.DisplayName := SQLDA^.SQLVar[x].AliasName;
  621. FieldBinding[FD.FieldNo-1] := x;
  622. end;
  623. end;
  624. {$R+}
  625. end;
  626. function TIBConnection.GetHandle: pointer;
  627. begin
  628. Result := FSQLDatabaseHandle;
  629. end;
  630. function TIBConnection.Fetch(cursor : TSQLCursor) : boolean;
  631. var
  632. retcode : integer;
  633. begin
  634. with cursor as TIBCursor do
  635. begin
  636. if FStatementType = stExecProcedure then
  637. //it is not recommended fetch from non-select statement, i.e. statement which have no cursor
  638. //starting from Firebird 2.5 it leads to error 'Invalid cursor reference'
  639. if SQLDA^.SQLD = 0 then
  640. retcode := 100 //no more rows to retrieve
  641. else
  642. begin
  643. retcode := 0;
  644. SQLDA^.SQLD := 0; //hack: mark after first fetch
  645. end
  646. else
  647. retcode := isc_dsql_fetch(@Status[0], @Statement, 1, SQLDA);
  648. if (retcode <> 0) and (retcode <> 100) then
  649. CheckError('Fetch', Status);
  650. end;
  651. Result := (retcode = 0);
  652. end;
  653. procedure TIBConnection.SetParameters(cursor : TSQLCursor; aTransation : TSQLTransaction; AParams : TParams);
  654. var ParNr,SQLVarNr : integer;
  655. s : string;
  656. i : integer;
  657. si : smallint;
  658. li : LargeInt;
  659. currbuff : pchar;
  660. w : word;
  661. TransactionHandle : pointer;
  662. blobId : ISC_QUAD;
  663. blobHandle : Isc_blob_Handle;
  664. BlobSize,
  665. BlobBytesWritten : longint;
  666. procedure SetBlobParam;
  667. begin
  668. {$R-}
  669. with cursor as TIBCursor do
  670. begin
  671. TransactionHandle := aTransation.Handle;
  672. blobhandle := nil;
  673. if isc_create_blob(@FStatus[0], @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, @blobId) <> 0 then
  674. CheckError('TIBConnection.CreateBlobStream', FStatus);
  675. s := AParams[ParNr].AsString;
  676. BlobSize := length(s);
  677. BlobBytesWritten := 0;
  678. i := 0;
  679. // Write in segments of MAXBLOBSEGMENTSIZE, as that is the fastest.
  680. // We ignore BlobSegmentSize property.
  681. while BlobBytesWritten < (BlobSize-MAXBLOBSEGMENTSIZE) do
  682. begin
  683. isc_put_segment(@FStatus[0], @blobHandle, MAXBLOBSEGMENTSIZE, @s[(i*MAXBLOBSEGMENTSIZE)+1]);
  684. inc(BlobBytesWritten,MAXBLOBSEGMENTSIZE);
  685. inc(i);
  686. end;
  687. if BlobBytesWritten <> BlobSize then
  688. isc_put_segment(@FStatus[0], @blobHandle, BlobSize-BlobBytesWritten, @s[(i*MAXBLOBSEGMENTSIZE)+1]);
  689. if isc_close_blob(@FStatus[0], @blobHandle) <> 0 then
  690. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  691. Move(blobId, in_sqlda^.SQLvar[SQLVarNr].SQLData^, in_SQLDA^.SQLVar[SQLVarNr].SQLLen);
  692. end;
  693. {$R+}
  694. end;
  695. var
  696. // This should be a pointer, because the ORIGINAL variables must
  697. // be modified.
  698. VSQLVar: ^XSQLVAR;
  699. d : double;
  700. begin
  701. {$R-}
  702. with cursor as TIBCursor do for SQLVarNr := 0 to High(ParamBinding){AParams.count-1} do
  703. begin
  704. ParNr := ParamBinding[SQLVarNr];
  705. VSQLVar := @in_sqlda^.SQLvar[SQLVarNr];
  706. if AParams[ParNr].IsNull then
  707. VSQLVar^.SQLInd^ := -1
  708. else
  709. begin
  710. VSQLVar^.SQLInd^ := 0;
  711. case (VSQLVar^.sqltype and not 1) of
  712. SQL_LONG :
  713. begin
  714. if VSQLVar^.sqlscale = 0 then
  715. i := AParams[ParNr].AsInteger
  716. else
  717. i := Round(AParams[ParNr].AsCurrency * IntPower(10, -VSQLVar^.sqlscale));
  718. Move(i, VSQLVar^.SQLData^, VSQLVar^.SQLLen);
  719. end;
  720. SQL_SHORT :
  721. begin
  722. if VSQLVar^.sqlscale = 0 then
  723. si := AParams[ParNr].AsSmallint
  724. else
  725. si := Round(AParams[ParNr].AsCurrency * IntPower(10, -VSQLVar^.sqlscale));
  726. i := si;
  727. Move(i, VSQLVar^.SQLData^, VSQLVar^.SQLLen);
  728. end;
  729. SQL_BLOB :
  730. SetBlobParam;
  731. SQL_VARYING, SQL_TEXT :
  732. begin
  733. s := AParams[ParNr].AsString;
  734. w := length(s); // a word is enough, since the max-length of a string in interbase is 32k
  735. if ((VSQLVar^.SQLType and not 1) = SQL_VARYING) then
  736. begin
  737. VSQLVar^.SQLLen := w;
  738. ReAllocMem(VSQLVar^.SQLData, VSQLVar^.SQLLen+2);
  739. CurrBuff := VSQLVar^.SQLData;
  740. move(w,CurrBuff^,sizeof(w));
  741. inc(CurrBuff,2);
  742. end
  743. else
  744. begin
  745. // The buffer-length is always VSQLVar^.sqllen, nothing more, nothing less
  746. // so fill the complete buffer with valid data. Adding #0 will lead
  747. // to problems, because the #0 will be seen as a part of the (binary) string
  748. CurrBuff := VSQLVar^.SQLData;
  749. w := VSQLVar^.sqllen;
  750. s := PadRight(s,w);
  751. end;
  752. Move(s[1], CurrBuff^, w);
  753. end;
  754. SQL_TYPE_DATE, SQL_TYPE_TIME, SQL_TIMESTAMP :
  755. SetDateTime(VSQLVar^.SQLData, AParams[ParNr].AsDateTime, VSQLVar^.SQLType);
  756. SQL_INT64:
  757. begin
  758. if VSQLVar^.sqlscale = 0 then
  759. li := AParams[ParNr].AsLargeInt
  760. else if AParams[ParNr].DataType = ftFMTBcd then
  761. begin
  762. d:=AParams[ParNr].AsFMTBCD * IntPower(10, -VSQLVar^.sqlscale);
  763. li := Round(d)
  764. end
  765. else
  766. li := Round(AParams[ParNr].AsCurrency * IntPower(10, -VSQLVar^.sqlscale));
  767. Move(li, VSQLVar^.SQLData^, VSQLVar^.SQLLen);
  768. end;
  769. SQL_DOUBLE, SQL_FLOAT:
  770. SetFloat(VSQLVar^.SQLData, AParams[ParNr].AsFloat, VSQLVar^.SQLLen);
  771. else
  772. DatabaseErrorFmt(SUnsupportedParameter,[Fieldtypenames[AParams[ParNr].DataType]],self);
  773. end {case}
  774. end;
  775. end;
  776. {$R+}
  777. end;
  778. function TIBConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
  779. var
  780. x : integer;
  781. VarcharLen : word;
  782. CurrBuff : pchar;
  783. c : currency;
  784. AFmtBcd : tBCD;
  785. smalli : smallint;
  786. longi : longint;
  787. largei : largeint;
  788. begin
  789. CreateBlob := False;
  790. with cursor as TIBCursor do
  791. begin
  792. {$R-}
  793. x := FieldBinding[FieldDef.FieldNo-1];
  794. // Joost, 5 jan 2006: I disabled the following, since it's useful for
  795. // debugging, but it also slows things down. In principle things can only go
  796. // wrong when FieldDefs is changed while the dataset is opened. A user just
  797. // shoudn't do that. ;) (The same is done in PQConnection)
  798. // if SQLDA^.SQLVar[x].AliasName <> FieldDef.Name then
  799. // DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  800. if assigned(SQLDA^.SQLVar[x].SQLInd) and (SQLDA^.SQLVar[x].SQLInd^ = -1) then
  801. result := false
  802. else
  803. begin
  804. with SQLDA^.SQLVar[x] do
  805. if ((SQLType and not 1) = SQL_VARYING) then
  806. begin
  807. Move(SQLData^, VarcharLen, 2);
  808. CurrBuff := SQLData + 2;
  809. end
  810. else
  811. begin
  812. CurrBuff := SQLData;
  813. VarCharLen := FieldDef.Size;
  814. end;
  815. Result := true;
  816. case FieldDef.DataType of
  817. ftBCD :
  818. begin
  819. case SQLDA^.SQLVar[x].SQLLen of
  820. 2 : begin
  821. Move(CurrBuff^, smalli, 2);
  822. c := smalli*intpower(10,SQLDA^.SQLVar[x].SQLScale);
  823. end;
  824. 4 : begin
  825. Move(CurrBuff^, longi, 4);
  826. c := longi*intpower(10,SQLDA^.SQLVar[x].SQLScale);
  827. end;
  828. 8 : begin
  829. Move(CurrBuff^, largei, 8);
  830. c := largei*intpower(10,SQLDA^.SQLVar[x].SQLScale);
  831. end;
  832. else
  833. Result := False; // Just to be sure, in principle this will never happen
  834. end; {case}
  835. Move(c, buffer^ , sizeof(c));
  836. end;
  837. ftFMTBcd :
  838. begin
  839. case SQLDA^.SQLVar[x].SQLLen of
  840. 2 : begin
  841. Move(CurrBuff^, smalli, 2);
  842. AFmtBCD:= smalli*intpower(10,SQLDA^.SQLVar[x].SQLScale);
  843. end;
  844. 4 : begin
  845. Move(CurrBuff^, longi, 4);
  846. AFmtBcd := longi*intpower(10,SQLDA^.SQLVar[x].SQLScale);
  847. end;
  848. 8 : begin
  849. Move(CurrBuff^, largei, 8);
  850. AFmtBcd := largei*intpower(10,SQLDA^.SQLVar[x].SQLScale);
  851. end;
  852. else
  853. Result := False; // Just to be sure, in principle this will never happen
  854. end; {case}
  855. Move(AFmtBcd, buffer^ , sizeof(AFmtBcd));
  856. end;
  857. ftInteger :
  858. begin
  859. FillByte(buffer^,sizeof(Longint),0);
  860. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  861. end;
  862. ftLargeint :
  863. begin
  864. FillByte(buffer^,sizeof(LargeInt),0);
  865. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  866. end;
  867. ftSmallint :
  868. begin
  869. FillByte(buffer^,sizeof(Smallint),0);
  870. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  871. end;
  872. ftDate, ftTime, ftDateTime:
  873. GetDateTime(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLType);
  874. ftString, ftFixedChar :
  875. begin
  876. Move(CurrBuff^, Buffer^, VarCharLen);
  877. PChar(Buffer + VarCharLen)^ := #0;
  878. end;
  879. ftFloat :
  880. GetFloat(CurrBuff, Buffer, SQLDA^.SQLVar[x].SQLLen);
  881. ftBlob,
  882. ftMemo :
  883. begin // load the BlobIb in field's buffer
  884. FillByte(buffer^,sizeof(TBufBlobField),0);
  885. Move(CurrBuff^, Buffer^, SQLDA^.SQLVar[x].SQLLen);
  886. end;
  887. else
  888. begin
  889. result := false;
  890. databaseerrorfmt(SUnsupportedFieldType, [Fieldtypenames[FieldDef.DataType], Self]);
  891. end
  892. end; { case }
  893. end; { if/else }
  894. {$R+}
  895. end; { with cursor }
  896. end;
  897. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  898. var
  899. CTime : TTm; // C struct time
  900. STime : TSystemTime; // System time
  901. PTime : TDateTime; // Pascal time
  902. begin
  903. case (AType and not 1) of
  904. SQL_TYPE_DATE :
  905. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  906. SQL_TYPE_TIME :
  907. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  908. SQL_TIMESTAMP :
  909. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  910. else
  911. Raise EIBDatabaseError.CreateFmt('Invalid parameter type for date Decode : %d',[(AType and not 1)]);
  912. end;
  913. STime.Year := CTime.tm_year + 1900;
  914. STime.Month := CTime.tm_mon + 1;
  915. STime.Day := CTime.tm_mday;
  916. STime.Hour := CTime.tm_hour;
  917. STime.Minute := CTime.tm_min;
  918. STime.Second := CTime.tm_sec;
  919. STime.Millisecond := 0;
  920. PTime := SystemTimeToDateTime(STime);
  921. Move(PTime, Buffer^, SizeOf(PTime));
  922. end;
  923. procedure TIBConnection.SetDateTime(CurrBuff: pointer; PTime : TDateTime; AType : integer);
  924. var
  925. CTime : TTm; // C struct time
  926. STime : TSystemTime; // System time
  927. begin
  928. DateTimeToSystemTime(PTime,STime);
  929. CTime.tm_year := STime.Year - 1900;
  930. CTime.tm_mon := STime.Month -1;
  931. CTime.tm_mday := STime.Day;
  932. CTime.tm_hour := STime.Hour;
  933. CTime.tm_min := STime.Minute;
  934. CTime.tm_sec := STime.Second;
  935. case (AType and not 1) of
  936. SQL_TYPE_DATE :
  937. isc_encode_sql_date(@CTime, PISC_DATE(CurrBuff));
  938. SQL_TYPE_TIME :
  939. isc_encode_sql_time(@CTime, PISC_TIME(CurrBuff));
  940. SQL_TIMESTAMP :
  941. isc_encode_timestamp(@CTime, PISC_TIMESTAMP(CurrBuff));
  942. else
  943. Raise EIBDatabaseError.CreateFmt('Invalid parameter type for date encode : %d',[(AType and not 1)]);
  944. end;
  945. end;
  946. function TIBConnection.GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  947. var s : string;
  948. begin
  949. case SchemaType of
  950. stTables : s := 'select '+
  951. 'rdb$relation_id as recno, '+
  952. '''' + DatabaseName + ''' as catalog_name, '+
  953. ''''' as schema_name, '+
  954. 'rdb$relation_name as table_name, '+
  955. '0 as table_type '+
  956. 'from '+
  957. 'rdb$relations '+
  958. 'where '+
  959. '(rdb$system_flag = 0 or rdb$system_flag is null) ' + // and rdb$view_blr is null
  960. 'order by rdb$relation_name';
  961. stSysTables : s := 'select '+
  962. 'rdb$relation_id as recno, '+
  963. '''' + DatabaseName + ''' as catalog_name, '+
  964. ''''' as schema_name, '+
  965. 'rdb$relation_name as table_name, '+
  966. '0 as table_type '+
  967. 'from '+
  968. 'rdb$relations '+
  969. 'where '+
  970. '(rdb$system_flag > 0) ' + // and rdb$view_blr is null
  971. 'order by rdb$relation_name';
  972. stProcedures : s := 'select '+
  973. 'rdb$procedure_id as recno, '+
  974. '''' + DatabaseName + ''' as catalog_name, '+
  975. ''''' as schema_name, '+
  976. 'rdb$procedure_name as proc_name, '+
  977. '0 as proc_type, '+
  978. 'rdb$procedure_inputs as in_params, '+
  979. 'rdb$procedure_outputs as out_params '+
  980. 'from '+
  981. 'rdb$procedures '+
  982. 'WHERE '+
  983. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  984. stColumns : s := 'select '+
  985. 'rdb$field_id as recno, '+
  986. '''' + DatabaseName + ''' as catalog_name, '+
  987. ''''' as schema_name, '+
  988. 'rdb$relation_name as table_name, '+
  989. 'rdb$field_name as column_name, '+
  990. 'rdb$field_position as column_position, '+
  991. '0 as column_type, '+
  992. '0 as column_datatype, '+
  993. ''''' as column_typename, '+
  994. '0 as column_subtype, '+
  995. '0 as column_precision, '+
  996. '0 as column_scale, '+
  997. '0 as column_length, '+
  998. '0 as column_nullable '+
  999. 'from '+
  1000. 'rdb$relation_fields '+
  1001. 'WHERE '+
  1002. '(rdb$system_flag = 0 or rdb$system_flag is null) and (rdb$relation_name = ''' + Uppercase(SchemaObjectName) + ''') ' +
  1003. 'order by rdb$field_name';
  1004. else
  1005. DatabaseError(SMetadataUnavailable)
  1006. end; {case}
  1007. result := s;
  1008. end;
  1009. procedure TIBConnection.UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string);
  1010. var qry : TSQLQuery;
  1011. begin
  1012. if not assigned(Transaction) then
  1013. DatabaseError(SErrConnTransactionnSet);
  1014. qry := tsqlquery.Create(nil);
  1015. qry.transaction := Transaction;
  1016. qry.database := Self;
  1017. with qry do
  1018. begin
  1019. ReadOnly := True;
  1020. sql.clear;
  1021. sql.add('select '+
  1022. 'ind.rdb$index_name, '+
  1023. 'ind.rdb$relation_name, '+
  1024. 'ind.rdb$unique_flag, '+
  1025. 'ind_seg.rdb$field_name, '+
  1026. 'rel_con.rdb$constraint_type, '+
  1027. 'ind.rdb$index_type '+
  1028. 'from '+
  1029. 'rdb$index_segments ind_seg, '+
  1030. 'rdb$indices ind '+
  1031. 'left outer join '+
  1032. 'rdb$relation_constraints rel_con '+
  1033. 'on '+
  1034. 'rel_con.rdb$index_name = ind.rdb$index_name '+
  1035. 'where '+
  1036. '(ind_seg.rdb$index_name = ind.rdb$index_name) and '+
  1037. '(ind.rdb$relation_name=''' + UpperCase(TableName) +''') '+
  1038. 'order by '+
  1039. 'ind.rdb$index_name;');
  1040. open;
  1041. end;
  1042. while not qry.eof do with IndexDefs.AddIndexDef do
  1043. begin
  1044. Name := trim(qry.fields[0].asstring);
  1045. Fields := trim(qry.Fields[3].asstring);
  1046. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  1047. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  1048. If qry.fields[5].asInteger = 1 then options:=options+[ixDescending];
  1049. qry.next;
  1050. while (name = trim(qry.fields[0].asstring)) and (not qry.eof) do
  1051. begin
  1052. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  1053. qry.next;
  1054. end;
  1055. end;
  1056. qry.close;
  1057. qry.free;
  1058. end;
  1059. procedure TIBConnection.SetFloat(CurrBuff: pointer; Dbl: Double; Size: integer);
  1060. var
  1061. Ext : extended;
  1062. Sin : single;
  1063. begin
  1064. case Size of
  1065. 4 :
  1066. begin
  1067. Sin := Dbl;
  1068. Move(Sin, CurrBuff^, 4);
  1069. end;
  1070. 8 :
  1071. begin
  1072. Move(Dbl, CurrBuff^, 8);
  1073. end;
  1074. 10:
  1075. begin
  1076. Ext := Dbl;
  1077. Move(Ext, CurrBuff^, 10);
  1078. end;
  1079. else
  1080. Raise EIBDatabaseError.CreateFmt('Invalid float size for float encode : %d',[Size]);
  1081. end;
  1082. end;
  1083. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Size : byte);
  1084. var
  1085. Ext : extended;
  1086. Dbl : double;
  1087. Sin : single;
  1088. begin
  1089. case Size of
  1090. 4 :
  1091. begin
  1092. Move(CurrBuff^, Sin, 4);
  1093. Dbl := Sin;
  1094. end;
  1095. 8 :
  1096. begin
  1097. Move(CurrBuff^, Dbl, 8);
  1098. end;
  1099. 10:
  1100. begin
  1101. Move(CurrBuff^, Ext, 10);
  1102. Dbl := double(Ext);
  1103. end;
  1104. else
  1105. Raise EIBDatabaseError.CreateFmt('Invalid float size for float Decode : %d',[Size]);
  1106. end;
  1107. Move(Dbl, Buffer^, 8);
  1108. end;
  1109. procedure TIBConnection.LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction);
  1110. const
  1111. isc_segstr_eof = 335544367; // It's not defined in ibase60 but in ibase40. Would it be better to define in ibase60?
  1112. var
  1113. blobHandle : Isc_blob_Handle;
  1114. blobSegment : pointer;
  1115. blobSegLen : word;
  1116. TransactionHandle : pointer;
  1117. blobId : PISC_QUAD;
  1118. ptr : Pointer;
  1119. begin
  1120. blobId := PISC_QUAD(@(ABlobBuf^.ConnBlobBuffer));
  1121. TransactionHandle := Atransaction.Handle;
  1122. blobHandle := nil;
  1123. if isc_open_blob(@FStatus[0], @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, blobId) <> 0 then
  1124. CheckError('TIBConnection.CreateBlobStream', FStatus);
  1125. //For performance, read as much as we can, regardless of any segment size set in database.
  1126. blobSegment := AllocMem(MAXBLOBSEGMENTSIZE);
  1127. with ABlobBuf^.BlobBuffer^ do
  1128. begin
  1129. Size := 0;
  1130. while (isc_get_segment(@FStatus[0], @blobHandle, @blobSegLen, MAXBLOBSEGMENTSIZE, blobSegment) = 0) do
  1131. begin
  1132. ReAllocMem(Buffer,Size+blobSegLen);
  1133. ptr := Buffer+Size;
  1134. move(blobSegment^,ptr^,blobSegLen);
  1135. inc(Size,blobSegLen);
  1136. end;
  1137. end;
  1138. freemem(blobSegment);
  1139. if FStatus[1] = isc_segstr_eof then
  1140. begin
  1141. if isc_close_blob(@FStatus[0], @blobHandle) <> 0 then
  1142. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  1143. end
  1144. else
  1145. CheckError('TIBConnection.CreateBlobStream isc_get_segment', FStatus);
  1146. end;
  1147. function TIBConnection.RowsAffected(cursor: TSQLCursor): TRowsCount;
  1148. var info_request : string;
  1149. resbuf : array[0..63] of byte;
  1150. i : integer;
  1151. BlockSize,
  1152. subBlockSize : integer;
  1153. SelectedRows,
  1154. InsertedRows : integer;
  1155. begin
  1156. SelectedRows:=-1;
  1157. InsertedRows:=-1;
  1158. if assigned(cursor) then with cursor as TIBCursor do
  1159. if assigned(statement) then
  1160. begin
  1161. info_request := chr(isc_info_sql_records);
  1162. if isc_dsql_sql_info(@Status[0],@Statement,Length(info_request), @info_request[1],sizeof(resbuf),@resbuf) <> 0 then
  1163. CheckError('RowsAffected', Status);
  1164. i := 0;
  1165. while not (byte(resbuf[i]) in [isc_info_end,isc_info_truncated]) do
  1166. begin
  1167. BlockSize:=isc_vax_integer(@resbuf[i+1],2);
  1168. if resbuf[i]=isc_info_sql_records then
  1169. begin
  1170. inc(i,3);
  1171. BlockSize:=BlockSize+i;
  1172. while (resbuf[i] <> isc_info_end) and (i < BlockSize) do
  1173. begin
  1174. subBlockSize:=isc_vax_integer(@resbuf[i+1],2);
  1175. if resbuf[i] = isc_info_req_select_count then
  1176. SelectedRows := isc_vax_integer(@resbuf[i+3],subBlockSize)
  1177. else if resbuf[i] = isc_info_req_insert_count then
  1178. InsertedRows := isc_vax_integer(@resbuf[i+3],subBlockSize);
  1179. inc(i,subBlockSize+3);
  1180. end;
  1181. end
  1182. else
  1183. inc(i,BlockSize+3);
  1184. end;
  1185. end;
  1186. if SelectedRows>0 then result:=SelectedRows
  1187. else Result:=InsertedRows;
  1188. end;
  1189. { TIBConnectionDef }
  1190. class function TIBConnectionDef.TypeName: String;
  1191. begin
  1192. Result:='Firebird';
  1193. end;
  1194. class function TIBConnectionDef.ConnectionClass: TSQLConnectionClass;
  1195. begin
  1196. Result:=TIBConnection;
  1197. end;
  1198. class function TIBConnectionDef.Description: String;
  1199. begin
  1200. Result:='Connect to Firebird/Interbase directly via the client library';
  1201. end;
  1202. initialization
  1203. RegisterConnection(TIBConnectionDef);
  1204. finalization
  1205. UnRegisterConnection(TIBConnectionDef);
  1206. end.