ibconnection.pp 34 KB

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