pqconnection.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. unit pqconnection;
  2. {$mode objfpc}{$H+}
  3. {$Define LinkDynamically}
  4. interface
  5. uses
  6. Classes, SysUtils, sqldb, db, dbconst,
  7. {$IfDef LinkDynamically}
  8. postgres3dyn;
  9. {$Else}
  10. postgres3;
  11. {$EndIf}
  12. type
  13. TPQTrans = Class(TSQLHandle)
  14. protected
  15. TransactionHandle : PPGConn;
  16. end;
  17. TPQCursor = Class(TSQLCursor)
  18. protected
  19. Statement : string;
  20. tr : Pointer;
  21. nFields : integer;
  22. res : PPGresult;
  23. BaseRes : PPGresult;
  24. Nr : string;
  25. end;
  26. { TPQConnection }
  27. TPQConnection = class (TSQLConnection)
  28. private
  29. FCursorCount : word;
  30. FConnectString : string;
  31. FSQLDatabaseHandle : pointer;
  32. function TranslateFldType(Type_Oid : integer) : TFieldType;
  33. protected
  34. procedure DoInternalConnect; override;
  35. procedure DoInternalDisconnect; override;
  36. function GetHandle : pointer; override;
  37. Function AllocateCursorHandle : TSQLCursor; override;
  38. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  39. Function AllocateTransactionHandle : TSQLHandle; override;
  40. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  41. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  42. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
  43. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
  44. function Fetch(cursor : TSQLCursor) : boolean; override;
  45. procedure UnPrepareStatement(cursor : TSQLCursor); override;
  46. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean; override;
  47. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  48. function RollBack(trans : TSQLHandle) : boolean; override;
  49. function Commit(trans : TSQLHandle) : boolean; override;
  50. procedure CommitRetaining(trans : TSQLHandle); override;
  51. function StartdbTransaction(trans : TSQLHandle; AParams : string) : boolean; override;
  52. procedure RollBackRetaining(trans : TSQLHandle); override;
  53. procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
  54. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  55. public
  56. constructor Create(AOwner : TComponent); override;
  57. published
  58. property DatabaseName;
  59. property KeepConnection;
  60. property LoginPrompt;
  61. property Params;
  62. property OnLogin;
  63. end;
  64. implementation
  65. ResourceString
  66. SErrRollbackFailed = 'Rollback transaction failed';
  67. SErrCommitFailed = 'Commit transaction failed';
  68. SErrConnectionFailed = 'Connection to database failed';
  69. SErrTransactionFailed = 'Start of transacion failed';
  70. SErrClearSelection = 'Clear of selection failed';
  71. SErrExecuteFailed = 'Execution of query failed';
  72. SErrFieldDefsFailed = 'Can not extract field information from query';
  73. SErrFetchFailed = 'Fetch of data failed';
  74. SErrNoDatabaseName = 'Database connect string (DatabaseName) not filled in!';
  75. SErrPrepareFailed = 'Preparation of query failed.';
  76. const Oid_Bool = 16;
  77. Oid_Text = 25;
  78. Oid_Oid = 26;
  79. Oid_Name = 19;
  80. Oid_Int8 = 20;
  81. Oid_int2 = 21;
  82. Oid_Int4 = 23;
  83. Oid_Float4 = 700;
  84. Oid_Float8 = 701;
  85. Oid_Unknown = 705;
  86. Oid_bpchar = 1042;
  87. Oid_varchar = 1043;
  88. Oid_timestamp = 1114;
  89. oid_date = 1082;
  90. oid_time = 1083;
  91. oid_numeric = 1700;
  92. constructor TPQConnection.Create(AOwner : TComponent);
  93. begin
  94. inherited;
  95. FConnOptions := FConnOptions + [sqSupportParams];
  96. end;
  97. function TPQConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  98. begin
  99. Result := (trans as TPQtrans).TransactionHandle;
  100. end;
  101. function TPQConnection.RollBack(trans : TSQLHandle) : boolean;
  102. var
  103. res : PPGresult;
  104. tr : TPQTrans;
  105. begin
  106. result := false;
  107. tr := trans as TPQTrans;
  108. res := PQexec(tr.TransactionHandle, 'ROLLBACK');
  109. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  110. begin
  111. PQclear(res);
  112. result := false;
  113. DatabaseError(SErrRollbackFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  114. end
  115. else
  116. begin
  117. PQclear(res);
  118. PQFinish(tr.TransactionHandle);
  119. result := true;
  120. end;
  121. end;
  122. function TPQConnection.Commit(trans : TSQLHandle) : boolean;
  123. var
  124. res : PPGresult;
  125. tr : TPQTrans;
  126. begin
  127. result := false;
  128. tr := trans as TPQTrans;
  129. res := PQexec(tr.TransactionHandle, 'COMMIT');
  130. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  131. begin
  132. PQclear(res);
  133. result := false;
  134. DatabaseError(SErrCommitFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  135. end
  136. else
  137. begin
  138. PQclear(res);
  139. PQFinish(tr.TransactionHandle);
  140. result := true;
  141. end;
  142. end;
  143. function TPQConnection.StartdbTransaction(trans : TSQLHandle; AParams : string) : boolean;
  144. var
  145. res : PPGresult;
  146. tr : TPQTrans;
  147. msg : string;
  148. begin
  149. result := false;
  150. tr := trans as TPQTrans;
  151. tr.TransactionHandle := PQconnectdb(pchar(FConnectString));
  152. if (PQstatus(tr.TransactionHandle) = CONNECTION_BAD) then
  153. begin
  154. result := false;
  155. PQFinish(tr.TransactionHandle);
  156. DatabaseError(SErrConnectionFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  157. end
  158. else
  159. begin
  160. res := PQexec(tr.TransactionHandle, 'BEGIN');
  161. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  162. begin
  163. result := false;
  164. PQclear(res);
  165. msg := PQerrorMessage(tr.transactionhandle);
  166. PQFinish(tr.TransactionHandle);
  167. DatabaseError(sErrTransactionFailed + ' (PostgreSQL: ' + msg + ')',self);
  168. end
  169. else
  170. begin
  171. PQclear(res);
  172. result := true;
  173. end;
  174. end;
  175. end;
  176. procedure TPQConnection.RollBackRetaining(trans : TSQLHandle);
  177. var
  178. res : PPGresult;
  179. tr : TPQTrans;
  180. msg : string;
  181. begin
  182. tr := trans as TPQTrans;
  183. res := PQexec(tr.TransactionHandle, 'ROLLBACK');
  184. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  185. begin
  186. PQclear(res);
  187. DatabaseError(SErrRollbackFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  188. end
  189. else
  190. begin
  191. PQclear(res);
  192. res := PQexec(tr.TransactionHandle, 'BEGIN');
  193. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  194. begin
  195. PQclear(res);
  196. msg := PQerrorMessage(tr.transactionhandle);
  197. PQFinish(tr.TransactionHandle);
  198. DatabaseError(sErrTransactionFailed + ' (PostgreSQL: ' + msg + ')',self);
  199. end
  200. else
  201. PQclear(res);
  202. end;
  203. end;
  204. procedure TPQConnection.CommitRetaining(trans : TSQLHandle);
  205. var
  206. res : PPGresult;
  207. tr : TPQTrans;
  208. msg : string;
  209. begin
  210. tr := trans as TPQTrans;
  211. res := PQexec(tr.TransactionHandle, 'COMMIT');
  212. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  213. begin
  214. PQclear(res);
  215. DatabaseError(SErrCommitFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  216. end
  217. else
  218. begin
  219. PQclear(res);
  220. res := PQexec(tr.TransactionHandle, 'BEGIN');
  221. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  222. begin
  223. PQclear(res);
  224. msg := PQerrorMessage(tr.transactionhandle);
  225. PQFinish(tr.TransactionHandle);
  226. DatabaseError(sErrTransactionFailed + ' (PostgreSQL: ' + msg + ')',self);
  227. end
  228. else
  229. PQclear(res);
  230. end;
  231. end;
  232. procedure TPQConnection.DoInternalConnect;
  233. var msg : string;
  234. begin
  235. {$IfDef LinkDynamically}
  236. InitialisePostgres3;
  237. {$EndIf}
  238. inherited dointernalconnect;
  239. if (DatabaseName = '') then
  240. DatabaseError(SErrNoDatabaseName,self);
  241. FConnectString := '';
  242. if (UserName <> '') then FConnectString := FConnectString + ' user=''' + UserName + '''';
  243. if (Password <> '') then FConnectString := FConnectString + ' password=''' + Password + '''';
  244. if (HostName <> '') then FConnectString := FConnectString + ' host=''' + HostName + '''';
  245. if (DatabaseName <> '') then FConnectString := FConnectString + ' dbname=''' + DatabaseName + '''';
  246. FSQLDatabaseHandle := PQconnectdb(pchar(FConnectString));
  247. if (PQstatus(FSQLDatabaseHandle) = CONNECTION_BAD) then
  248. begin
  249. msg := PQerrorMessage(FSQLDatabaseHandle);
  250. dointernaldisconnect;
  251. DatabaseError(sErrConnectionFailed + ' (PostgreSQL: ' + msg + ')',self);
  252. end;
  253. end;
  254. procedure TPQConnection.DoInternalDisconnect;
  255. begin
  256. PQfinish(FSQLDatabaseHandle);
  257. {$IfDef LinkDynamically}
  258. ReleasePostgres3;
  259. {$EndIf}
  260. end;
  261. function TPQConnection.TranslateFldType(Type_Oid : integer) : TFieldType;
  262. begin
  263. case Type_Oid of
  264. Oid_varchar,Oid_bpchar,
  265. Oid_name : Result := ftstring;
  266. Oid_text : REsult := ftmemo;
  267. Oid_oid : Result := ftInteger;
  268. Oid_int8 : Result := ftLargeInt;
  269. Oid_int4 : Result := ftInteger;
  270. Oid_int2 : Result := ftSmallInt;
  271. Oid_Float4 : Result := ftFloat;
  272. Oid_Float8 : Result := ftFloat;
  273. Oid_TimeStamp : Result := ftDateTime;
  274. Oid_Date : Result := ftDate;
  275. Oid_Time : Result := ftTime;
  276. Oid_Bool : Result := ftBoolean;
  277. Oid_Numeric : Result := ftBCD;
  278. Oid_Unknown : Result := ftUnknown;
  279. else
  280. Result := ftUnknown;
  281. end;
  282. end;
  283. Function TPQConnection.AllocateCursorHandle : TSQLCursor;
  284. begin
  285. result := TPQCursor.create;
  286. end;
  287. Procedure TPQConnection.DeAllocateCursorHandle(var cursor : TSQLCursor);
  288. begin
  289. FreeAndNil(cursor);
  290. end;
  291. Function TPQConnection.AllocateTransactionHandle : TSQLHandle;
  292. begin
  293. result := TPQTrans.create;
  294. end;
  295. procedure TPQConnection.PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams);
  296. const TypeStrings : array[TFieldType] of string =
  297. (
  298. 'Unknown',
  299. 'text',
  300. 'int',
  301. 'int',
  302. 'int',
  303. 'bool',
  304. 'float',
  305. 'numeric',
  306. 'numeric',
  307. 'date',
  308. 'time',
  309. 'timestamp',
  310. 'Unknown',
  311. 'Unknown',
  312. 'Unknown',
  313. 'Unknown',
  314. 'Unknown',
  315. 'Unknown',
  316. 'Unknown',
  317. 'Unknown',
  318. 'Unknown',
  319. 'Unknown',
  320. 'Unknown',
  321. 'Unknown',
  322. 'Unknown',
  323. 'int',
  324. 'Unknown',
  325. 'Unknown',
  326. 'Unknown',
  327. 'Unknown',
  328. 'Unknown',
  329. 'Unknown',
  330. 'Unknown',
  331. 'Unknown',
  332. 'Unknown',
  333. 'Unknown',
  334. 'Unknown',
  335. 'Unknown'
  336. );
  337. var s : string;
  338. i : integer;
  339. begin
  340. with (cursor as TPQCursor) do
  341. begin
  342. FPrepared := False;
  343. nr := inttostr(FCursorcount);
  344. inc(FCursorCount);
  345. // Prior to v8 there is no support for cursors and parameters.
  346. // So that's not supported.
  347. if FStatementType = stselect then
  348. statement := 'DECLARE slctst' + name + nr +' BINARY CURSOR FOR ' + buf
  349. else if FStatementType in [stInsert,stUpdate,stDelete] then
  350. begin
  351. tr := aTransaction.Handle;
  352. // Only available for pq 8.0, so don't use it...
  353. // Res := pqprepare(tr,'prepst'+name+nr,pchar(buf),params.Count,pchar(''));
  354. s := 'prepare prepst'+nr+' ';
  355. if Assigned(AParams) and (AParams.count > 0) then
  356. begin
  357. s := s + '(';
  358. for i := 0 to AParams.count-1 do
  359. begin
  360. s := s + TypeStrings[AParams[i].DataType] + ',';
  361. buf := stringreplace(buf,':'+AParams[i].Name,'$'+inttostr(i+1),[rfReplaceAll,rfIgnoreCase]);
  362. end;
  363. s[length(s)] := ')';
  364. end;
  365. s := s + ' as ' + buf;
  366. res := pqexec(tr,pchar(s));
  367. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  368. begin
  369. pqclear(res);
  370. DatabaseError(SErrPrepareFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  371. end;
  372. FPrepared := True;
  373. end
  374. else
  375. statement := buf;
  376. end;
  377. end;
  378. procedure TPQConnection.UnPrepareStatement(cursor : TSQLCursor);
  379. begin
  380. with (cursor as TPQCursor) do if FPrepared then
  381. begin
  382. res := pqexec(tr,pchar('deallocate prepst'+nr));
  383. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  384. begin
  385. pqclear(res);
  386. DatabaseError(SErrPrepareFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  387. end;
  388. pqclear(res);
  389. FPrepared := False;
  390. end;
  391. end;
  392. procedure TPQConnection.FreeFldBuffers(cursor : TSQLCursor);
  393. begin
  394. with cursor as TPQCursor do
  395. if (PQresultStatus(res) <> PGRES_FATAL_ERROR) then //Don't try to do anything if the transaction has already encountered an error.
  396. begin
  397. if FStatementType = stselect then
  398. begin
  399. Res := pqexec(tr,pchar('CLOSE slctst' + name + nr));
  400. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  401. begin
  402. pqclear(res);
  403. DatabaseError(SErrClearSelection + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  404. end
  405. end;
  406. pqclear(baseres);
  407. pqclear(res);
  408. end;
  409. end;
  410. procedure TPQConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction;AParams : TParams);
  411. var ar : array of pchar;
  412. i : integer;
  413. s : string;
  414. begin
  415. with cursor as TPQCursor do
  416. begin
  417. if FStatementType in [stInsert,stUpdate,stDelete] then
  418. begin
  419. if Assigned(AParams) and (AParams.count > 0) then
  420. begin
  421. setlength(ar,Aparams.count);
  422. for i := 0 to AParams.count -1 do
  423. case AParams[i].DataType of
  424. ftdatetime : ar[i] := pchar(formatdatetime('YYYY-MM-DD',AParams[i].AsDateTime));
  425. else
  426. ar[i] := pchar(AParams[i].asstring);
  427. writeln(ar[i]);
  428. end;
  429. res := PQexecPrepared(tr,pchar('prepst'+nr),Aparams.count,@Ar[0],nil,nil,0)
  430. end
  431. else
  432. res := PQexecPrepared(tr,pchar('prepst'+nr),0,nil,nil,nil,0);
  433. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  434. begin
  435. pqclear(res);
  436. DatabaseError(SErrExecuteFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self);
  437. end;
  438. end
  439. else
  440. begin
  441. tr := aTransaction.Handle;
  442. s := statement;
  443. //Should be altered, just like in TSQLQuery.ApplyRecUpdate
  444. if assigned(AParams) then for i := 0 to AParams.count-1 do
  445. s := stringreplace(s,':'+AParams[i].Name,AParams[i].asstring,[rfReplaceAll,rfIgnoreCase]);
  446. res := pqexec(tr,pchar(s));
  447. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  448. begin
  449. pqclear(res);
  450. DatabaseError(SErrExecuteFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self);
  451. end;
  452. end;
  453. end;
  454. end;
  455. procedure TPQConnection.AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs);
  456. var
  457. i : integer;
  458. size : integer;
  459. st : string;
  460. fieldtype : tfieldtype;
  461. begin
  462. with cursor as TPQCursor do
  463. begin
  464. // BaseRes := pqexecParams(tr,'FETCH 0 IN selectst' + pchar(name) ,0,nil,nil,nil,nil,1);
  465. st := pchar('FETCH 0 IN slctst' + name+nr);
  466. BaseRes := pqexec(tr,pchar(st));
  467. if (PQresultStatus(BaseRes) <> PGRES_TUPLES_OK) then
  468. begin
  469. pqclear(BaseRes);
  470. DatabaseError(SErrFieldDefsFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  471. end;
  472. nFields := PQnfields(BaseRes);
  473. for i := 0 to nFields-1 do
  474. begin
  475. size := PQfsize(BaseRes, i);
  476. fieldtype := TranslateFldType(PQftype(BaseRes, i));
  477. if (fieldtype = ftstring) and (size = -1) then
  478. begin
  479. size := pqfmod(baseres,i)-3;
  480. if size = -4 then size := dsMaxStringSize;
  481. end;
  482. if fieldtype = ftdate then
  483. size := sizeof(double);
  484. TFieldDef.Create(FieldDefs, PQfname(BaseRes, i), fieldtype,size, False, (i + 1));
  485. end;
  486. end;
  487. end;
  488. function TPQConnection.GetHandle: pointer;
  489. begin
  490. Result := FSQLDatabaseHandle;
  491. end;
  492. function TPQConnection.Fetch(cursor : TSQLCursor) : boolean;
  493. var st : string;
  494. begin
  495. with cursor as TPQCursor do
  496. begin
  497. st := pchar('FETCH NEXT IN slctst' + name+nr);
  498. Res := pqexec(tr,pchar(st));
  499. if (PQresultStatus(res) <> PGRES_TUPLES_OK) then
  500. begin
  501. pqclear(Res);
  502. DatabaseError(SErrfetchFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  503. end;
  504. Result := (PQntuples(res)<>0);
  505. end;
  506. end;
  507. function TPQConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean;
  508. var
  509. x,i : integer;
  510. li : Longint;
  511. CurrBuff : pchar;
  512. tel : byte;
  513. dbl : pdouble;
  514. begin
  515. with cursor as TPQCursor do
  516. begin
  517. for x := 0 to PQnfields(res)-1 do
  518. if PQfname(Res, x) = FieldDef.Name then break;
  519. if PQfname(Res, x) <> FieldDef.Name then
  520. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  521. if pqgetisnull(res,0,x)=1 then
  522. result := false
  523. else
  524. begin
  525. i := PQfsize(res, x);
  526. CurrBuff := pqgetvalue(res,0,x);
  527. case FieldDef.DataType of
  528. ftInteger, ftSmallint, ftLargeInt,ftfloat :
  529. begin
  530. for tel := 1 to i do // postgres returns big-endian numbers
  531. pchar(Buffer)[tel-1] := CurrBuff[i-tel];
  532. end;
  533. ftString :
  534. begin
  535. li := pqgetlength(res,0,x);
  536. Move(CurrBuff^, Buffer^, li);
  537. pchar(Buffer + li)^ := #0;
  538. i := pqfmod(res,x)-3;
  539. end;
  540. ftdate :
  541. begin
  542. li := 0;
  543. for tel := 1 to i do // postgres returns big-endian numbers
  544. pchar(@li)[tel-1] := CurrBuff[i-tel];
  545. // double(buffer^) := x + 36526; This doesn't work, please tell me what is wrong with it?
  546. dbl := pointer(buffer);
  547. dbl^ := li + 36526;
  548. i := sizeof(double);
  549. end;
  550. ftDateTime, fttime :
  551. begin
  552. dbl := pointer(buffer);
  553. dbl^ := 0;
  554. for tel := 1 to i do // postgres returns big-endian numbers
  555. pchar(Buffer)[tel-1] := CurrBuff[i-tel];
  556. dbl^ := (dbl^+3.1558464E+009)/86400; // postgres counts seconds elapsed since 1-1-2000
  557. end;
  558. ftBCD:
  559. begin
  560. // not implemented
  561. end;
  562. ftBoolean:
  563. pchar(buffer)[0] := CurrBuff[0]
  564. end;
  565. result := true;
  566. end;
  567. end;
  568. end;
  569. procedure TPQConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  570. var qry : TSQLQuery;
  571. begin
  572. if not assigned(Transaction) then
  573. DatabaseError(SErrConnTransactionnSet);
  574. qry := tsqlquery.Create(nil);
  575. qry.transaction := Transaction;
  576. qry.database := Self;
  577. with qry do
  578. begin
  579. ReadOnly := True;
  580. sql.clear;
  581. sql.add('select '+
  582. 'ic.relname as indexname, '+
  583. 'tc.relname as tablename, '+
  584. 'ia.attname, '+
  585. 'i.indisprimary, '+
  586. 'i.indisunique '+
  587. 'from '+
  588. 'pg_attribute ta, '+
  589. 'pg_attribute ia, '+
  590. 'pg_class tc, '+
  591. 'pg_class ic, '+
  592. 'pg_index i '+
  593. 'where '+
  594. '(i.indrelid = tc.oid) and '+
  595. '(ta.attrelid = tc.oid) and '+
  596. '(ia.attrelid = i.indexrelid) and '+
  597. '(ic.oid = i.indexrelid) and '+
  598. '(ta.attnum = i.indkey[ia.attnum-1]) and '+
  599. '(upper(tc.relname)=''' + UpperCase(TableName) +''') '+
  600. 'order by '+
  601. 'ic.relname;');
  602. open;
  603. end;
  604. while not qry.eof do with IndexDefs.AddIndexDef do
  605. begin
  606. Name := trim(qry.fields[0].asstring);
  607. Fields := trim(qry.Fields[2].asstring);
  608. If qry.fields[3].asboolean then options := options + [ixPrimary];
  609. If qry.fields[4].asboolean then options := options + [ixUnique];
  610. qry.next;
  611. while (name = qry.fields[0].asstring) and (not qry.eof) do
  612. begin
  613. Fields := Fields + ';' + trim(qry.Fields[2].asstring);
  614. qry.next;
  615. end;
  616. end;
  617. qry.close;
  618. qry.free;
  619. end;
  620. function TPQConnection.GetSchemaInfoSQL(SchemaType: TSchemaType;
  621. SchemaObjectName, SchemaPattern: string): string;
  622. var s : string;
  623. begin
  624. case SchemaType of
  625. stTables : s := 'select '+
  626. 'relfilenode as recno, '+
  627. '''' + DatabaseName + ''' as catalog_name, '+
  628. ''''' as schema_name, '+
  629. 'relname as table_name, '+
  630. '0 as table_type '+
  631. 'from '+
  632. 'pg_class '+
  633. 'where '+
  634. '(relowner > 1) and relkind=''r''' +
  635. 'order by relname';
  636. stSysTables : s := 'select '+
  637. 'relfilenode as recno, '+
  638. '''' + DatabaseName + ''' as catalog_name, '+
  639. ''''' as schema_name, '+
  640. 'relname as table_name, '+
  641. '0 as table_type '+
  642. 'from '+
  643. 'pg_class '+
  644. 'where '+
  645. 'relkind=''r''' +
  646. 'order by relname';
  647. else
  648. DatabaseError(SMetadataUnavailable)
  649. end; {case}
  650. result := s;
  651. end;
  652. end.