ibconnection.pp 33 KB

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