pqconnection.pp 21 KB

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