ibconnection.pp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. IndexDefs.Clear;
  908. while not qry.eof do with IndexDefs.AddIndexDef do
  909. begin
  910. Name := trim(qry.fields[0].asstring);
  911. Fields := trim(qry.Fields[3].asstring);
  912. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  913. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  914. If qry.fields[5].asInteger = 1 then options:=options+[ixDescending];
  915. qry.next;
  916. while (name = trim(qry.fields[0].asstring)) and (not qry.eof) do
  917. begin
  918. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  919. qry.next;
  920. end;
  921. end;
  922. qry.close;
  923. qry.free;
  924. end;
  925. procedure TIBConnection.SetFloat(CurrBuff: pointer; Dbl: Double; Size: integer);
  926. var
  927. Ext : extended;
  928. Sin : single;
  929. begin
  930. case Size of
  931. 4 :
  932. begin
  933. Sin := Dbl;
  934. Move(Sin, CurrBuff^, 4);
  935. end;
  936. 8 :
  937. begin
  938. Move(Dbl, CurrBuff^, 8);
  939. end;
  940. 10:
  941. begin
  942. Ext := Dbl;
  943. Move(Ext, CurrBuff^, 10);
  944. end;
  945. end;
  946. end;
  947. procedure TIBConnection.GetFloat(CurrBuff, Buffer : pointer; Size : byte);
  948. var
  949. Ext : extended;
  950. Dbl : double;
  951. Sin : single;
  952. begin
  953. case Size of
  954. 4 :
  955. begin
  956. Move(CurrBuff^, Sin, 4);
  957. Dbl := Sin;
  958. end;
  959. 8 :
  960. begin
  961. Move(CurrBuff^, Dbl, 8);
  962. end;
  963. 10:
  964. begin
  965. Move(CurrBuff^, Ext, 10);
  966. Dbl := double(Ext);
  967. end;
  968. end;
  969. Move(Dbl, Buffer^, 8);
  970. end;
  971. function TIBConnection.getMaxBlobSize(blobHandle : TIsc_Blob_Handle) : longInt;
  972. var
  973. iscInfoBlobMaxSegment : byte = isc_info_blob_max_segment;
  974. blobInfo : array[0..50] of byte;
  975. begin
  976. if isc_blob_info(@Fstatus[0], @blobHandle, sizeof(iscInfoBlobMaxSegment), pchar(@iscInfoBlobMaxSegment), sizeof(blobInfo) - 2, pchar(@blobInfo[0])) <> 0 then
  977. CheckError('isc_blob_info', FStatus);
  978. if blobInfo[0] = isc_info_blob_max_segment then
  979. begin
  980. result := isc_vax_integer(pchar(@blobInfo[3]), isc_vax_integer(pchar(@blobInfo[1]), 2));
  981. end
  982. else
  983. CheckError('isc_blob_info', FStatus);
  984. end;
  985. procedure TIBConnection.LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction);
  986. const
  987. isc_segstr_eof = 335544367; // It's not defined in ibase60 but in ibase40. Would it be better to define in ibase60?
  988. var
  989. blobHandle : Isc_blob_Handle;
  990. blobSegment : pointer;
  991. blobSegLen : word;
  992. maxBlobSize : longInt;
  993. TransactionHandle : pointer;
  994. blobId : PISC_QUAD;
  995. ptr : Pointer;
  996. begin
  997. blobId := PISC_QUAD(@(ABlobBuf^.ConnBlobBuffer));
  998. TransactionHandle := Atransaction.Handle;
  999. blobHandle := nil;
  1000. if isc_open_blob(@FStatus[0], @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, blobId) <> 0 then
  1001. CheckError('TIBConnection.CreateBlobStream', FStatus);
  1002. maxBlobSize := getMaxBlobSize(blobHandle);
  1003. blobSegment := AllocMem(maxBlobSize);
  1004. with ABlobBuf^.BlobBuffer^ do
  1005. begin
  1006. Size := 0;
  1007. while (isc_get_segment(@FStatus[0], @blobHandle, @blobSegLen, maxBlobSize, blobSegment) = 0) do
  1008. begin
  1009. ReAllocMem(Buffer,Size+blobSegLen);
  1010. ptr := Buffer+Size;
  1011. move(blobSegment^,ptr^,blobSegLen);
  1012. inc(Size,blobSegLen);
  1013. end;
  1014. end;
  1015. freemem(blobSegment);
  1016. if FStatus[1] = isc_segstr_eof then
  1017. begin
  1018. if isc_close_blob(@FStatus[0], @blobHandle) <> 0 then
  1019. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  1020. end
  1021. else
  1022. CheckError('TIBConnection.CreateBlobStream isc_get_segment', FStatus);
  1023. end;
  1024. function TIBConnection.RowsAffected(cursor: TSQLCursor): TRowsCount;
  1025. var info_request : string;
  1026. resbuf : array[0..63] of byte;
  1027. i : integer;
  1028. BlockSize,
  1029. subBlockSize : integer;
  1030. SelectedRows,
  1031. InsertedRows : integer;
  1032. begin
  1033. SelectedRows:=-1;
  1034. InsertedRows:=-1;
  1035. if assigned(cursor) then with cursor as TIBCursor do
  1036. if assigned(statement) then
  1037. begin
  1038. info_request := chr(isc_info_sql_records);
  1039. if isc_dsql_sql_info(@Status[0],@Statement,Length(info_request), @info_request[1],sizeof(resbuf),@resbuf) <> 0 then
  1040. CheckError('RowsAffected', Status);
  1041. i := 0;
  1042. while not (byte(resbuf[i]) in [isc_info_end,isc_info_truncated]) do
  1043. begin
  1044. BlockSize:=isc_vax_integer(@resbuf[i+1],2);
  1045. if resbuf[i]=isc_info_sql_records then
  1046. begin
  1047. inc(i,3);
  1048. BlockSize:=BlockSize+i;
  1049. while (resbuf[i] <> isc_info_end) and (i < BlockSize) do
  1050. begin
  1051. subBlockSize:=isc_vax_integer(@resbuf[i+1],2);
  1052. if resbuf[i] = isc_info_req_select_count then
  1053. SelectedRows := isc_vax_integer(@resbuf[i+3],subBlockSize)
  1054. else if resbuf[i] = isc_info_req_insert_count then
  1055. InsertedRows := isc_vax_integer(@resbuf[i+3],subBlockSize);
  1056. inc(i,subBlockSize+3);
  1057. end;
  1058. end
  1059. else
  1060. inc(i,BlockSize+3);
  1061. end;
  1062. end;
  1063. if SelectedRows>0 then result:=SelectedRows
  1064. else Result:=InsertedRows;
  1065. end;
  1066. { TIBConnectionDef }
  1067. class function TIBConnectionDef.TypeName: String;
  1068. begin
  1069. Result:='Firebird';
  1070. end;
  1071. class function TIBConnectionDef.ConnectionClass: TSQLConnectionClass;
  1072. begin
  1073. Result:=TIBConnection;
  1074. end;
  1075. class function TIBConnectionDef.Description: String;
  1076. begin
  1077. Result:='Connect to Firebird/Interbase directly via the client library';
  1078. end;
  1079. initialization
  1080. RegisterConnection(TIBConnectionDef);
  1081. finalization
  1082. UnRegisterConnection(TIBConnectionDef);
  1083. end.