ibconnection.pp 37 KB

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