ibconnection.pp 40 KB

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