ibconnection.pp 40 KB

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