ibconnection.pp 36 KB

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