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. res : PPGresult;
  22. CurTuple : integer;
  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. uses math;
  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. 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. FConnectString := '';
  239. if (UserName <> '') then FConnectString := FConnectString + ' user=''' + UserName + '''';
  240. if (Password <> '') then FConnectString := FConnectString + ' password=''' + Password + '''';
  241. if (HostName <> '') then FConnectString := FConnectString + ' host=''' + HostName + '''';
  242. if (DatabaseName <> '') then FConnectString := FConnectString + ' dbname=''' + DatabaseName + '''';
  243. if (Params.Text <> '') then FConnectString := FConnectString + ' '+Params.Text;
  244. FSQLDatabaseHandle := PQconnectdb(pchar(FConnectString));
  245. if (PQstatus(FSQLDatabaseHandle) = CONNECTION_BAD) then
  246. begin
  247. msg := PQerrorMessage(FSQLDatabaseHandle);
  248. dointernaldisconnect;
  249. DatabaseError(sErrConnectionFailed + ' (PostgreSQL: ' + msg + ')',self);
  250. end;
  251. end;
  252. procedure TPQConnection.DoInternalDisconnect;
  253. begin
  254. PQfinish(FSQLDatabaseHandle);
  255. {$IfDef LinkDynamically}
  256. ReleasePostgres3;
  257. {$EndIf}
  258. end;
  259. function TPQConnection.TranslateFldType(Type_Oid : integer) : TFieldType;
  260. begin
  261. case Type_Oid of
  262. Oid_varchar,Oid_bpchar,
  263. Oid_name : Result := ftstring;
  264. Oid_text : Result := ftstring;
  265. Oid_oid : Result := ftInteger;
  266. Oid_int8 : Result := ftLargeInt;
  267. Oid_int4 : Result := ftInteger;
  268. Oid_int2 : Result := ftSmallInt;
  269. Oid_Float4 : Result := ftFloat;
  270. Oid_Float8 : Result := ftFloat;
  271. Oid_TimeStamp : Result := ftDateTime;
  272. Oid_Date : Result := ftDate;
  273. Oid_Time : Result := ftTime;
  274. Oid_Bool : Result := ftBoolean;
  275. Oid_Numeric : Result := ftBCD;
  276. Oid_Unknown : Result := ftUnknown;
  277. else
  278. Result := ftUnknown;
  279. end;
  280. end;
  281. Function TPQConnection.AllocateCursorHandle : TSQLCursor;
  282. begin
  283. result := TPQCursor.create;
  284. end;
  285. Procedure TPQConnection.DeAllocateCursorHandle(var cursor : TSQLCursor);
  286. begin
  287. FreeAndNil(cursor);
  288. end;
  289. Function TPQConnection.AllocateTransactionHandle : TSQLHandle;
  290. begin
  291. result := TPQTrans.create;
  292. end;
  293. procedure TPQConnection.PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams);
  294. const TypeStrings : array[TFieldType] of string =
  295. (
  296. 'Unknown',
  297. 'text',
  298. 'int',
  299. 'int',
  300. 'int',
  301. 'bool',
  302. 'float',
  303. 'numeric',
  304. 'numeric',
  305. 'date',
  306. 'time',
  307. 'timestamp',
  308. 'Unknown',
  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. 'int',
  322. 'Unknown',
  323. 'Unknown',
  324. 'Unknown',
  325. 'Unknown',
  326. 'Unknown',
  327. 'Unknown',
  328. 'Unknown',
  329. 'Unknown',
  330. 'Unknown',
  331. 'Unknown',
  332. 'Unknown',
  333. 'Unknown'
  334. );
  335. var s : string;
  336. i : integer;
  337. begin
  338. with (cursor as TPQCursor) do
  339. begin
  340. FPrepared := False;
  341. nr := inttostr(FCursorcount);
  342. inc(FCursorCount);
  343. // Prior to v8 there is no support for cursors and parameters.
  344. // So that's not supported.
  345. if FStatementType in [stInsert,stUpdate,stDelete, stSelect] then
  346. begin
  347. tr := aTransaction.Handle;
  348. // Only available for pq 8.0, so don't use it...
  349. // Res := pqprepare(tr,'prepst'+name+nr,pchar(buf),params.Count,pchar(''));
  350. s := 'prepare prepst'+nr+' ';
  351. if Assigned(AParams) and (AParams.count > 0) then
  352. begin
  353. s := s + '(';
  354. for i := 0 to AParams.count-1 do if TypeStrings[AParams[i].DataType] <> 'Unknown' then
  355. s := s + TypeStrings[AParams[i].DataType] + ','
  356. else DatabaseErrorFmt(SUnsupportedParameter,[Fieldtypenames[AParams[i].DataType]],self);
  357. s[length(s)] := ')';
  358. buf := AParams.ParseSQL(buf,false,psPostgreSQL);
  359. end;
  360. s := s + ' as ' + buf;
  361. res := pqexec(tr,pchar(s));
  362. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  363. begin
  364. pqclear(res);
  365. DatabaseError(SErrPrepareFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  366. end;
  367. FPrepared := True;
  368. end
  369. else
  370. statement := buf;
  371. end;
  372. end;
  373. procedure TPQConnection.UnPrepareStatement(cursor : TSQLCursor);
  374. begin
  375. with (cursor as TPQCursor) do if FPrepared then
  376. begin
  377. res := pqexec(tr,pchar('deallocate prepst'+nr));
  378. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  379. begin
  380. pqclear(res);
  381. DatabaseError(SErrPrepareFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  382. end;
  383. pqclear(res);
  384. FPrepared := False;
  385. end;
  386. end;
  387. procedure TPQConnection.FreeFldBuffers(cursor : TSQLCursor);
  388. begin
  389. // Do nothing
  390. end;
  391. procedure TPQConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction;AParams : TParams);
  392. var ar : array of pointer;
  393. i : integer;
  394. s : string;
  395. begin
  396. with cursor as TPQCursor do
  397. begin
  398. if FStatementType in [stInsert,stUpdate,stDelete,stSelect] then
  399. begin
  400. if Assigned(AParams) and (AParams.count > 0) then
  401. begin
  402. setlength(ar,Aparams.count);
  403. for i := 0 to AParams.count -1 do if not AParams[i].IsNull then
  404. begin
  405. case AParams[i].DataType of
  406. ftdatetime : s := formatdatetime('YYYY-MM-DD',AParams[i].AsDateTime);
  407. else
  408. s := AParams[i].asstring;
  409. end; {case}
  410. GetMem(ar[i],length(s)+1);
  411. StrMove(PChar(ar[i]),Pchar(s),Length(S)+1);
  412. end
  413. else
  414. FreeAndNil(ar[i]);
  415. res := PQexecPrepared(tr,pchar('prepst'+nr),Aparams.count,@Ar[0],nil,nil,0);
  416. for i := 0 to AParams.count -1 do
  417. FreeMem(ar[i]);
  418. end
  419. else
  420. res := PQexecPrepared(tr,pchar('prepst'+nr),0,nil,nil,nil,1);
  421. end
  422. else
  423. begin
  424. tr := aTransaction.Handle;
  425. s := statement;
  426. //Should be altered, just like in TSQLQuery.ApplyRecUpdate
  427. if assigned(AParams) then for i := 0 to AParams.count-1 do
  428. s := stringreplace(s,':'+AParams[i].Name,AParams[i].asstring,[rfReplaceAll,rfIgnoreCase]);
  429. res := pqexec(tr,pchar(s));
  430. end;
  431. if not (PQresultStatus(res) in [PGRES_COMMAND_OK,PGRES_TUPLES_OK]) then
  432. begin
  433. pqclear(res);
  434. DatabaseError(SErrExecuteFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self);
  435. end;
  436. end;
  437. end;
  438. procedure TPQConnection.AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs);
  439. var
  440. i : integer;
  441. size : integer;
  442. st : string;
  443. fieldtype : tfieldtype;
  444. nFields : integer;
  445. begin
  446. with cursor as TPQCursor do
  447. begin
  448. nFields := PQnfields(Res);
  449. for i := 0 to nFields-1 do
  450. begin
  451. size := PQfsize(Res, i);
  452. fieldtype := TranslateFldType(PQftype(Res, i));
  453. if (fieldtype = ftstring) and (size = -1) then
  454. begin
  455. size := pqfmod(res,i)-4;
  456. if size = -5 then size := dsMaxStringSize;
  457. end;
  458. if fieldtype = ftdate then
  459. size := sizeof(double);
  460. TFieldDef.Create(FieldDefs, PQfname(Res, i), fieldtype,size, False, (i + 1));
  461. end;
  462. CurTuple := -1;
  463. end;
  464. end;
  465. function TPQConnection.GetHandle: pointer;
  466. begin
  467. Result := FSQLDatabaseHandle;
  468. end;
  469. function TPQConnection.Fetch(cursor : TSQLCursor) : boolean;
  470. var st : string;
  471. begin
  472. with cursor as TPQCursor do
  473. begin
  474. inc(CurTuple);
  475. Result := (PQntuples(res)>CurTuple);
  476. end;
  477. end;
  478. function TPQConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean;
  479. type TNumericRecord = record
  480. Digits : SmallInt;
  481. Weight : SmallInt;
  482. Sign : SmallInt;
  483. Scale : Smallint;
  484. end;
  485. var
  486. x,i : integer;
  487. li : Longint;
  488. CurrBuff : pchar;
  489. tel : byte;
  490. dbl : pdouble;
  491. cur : currency;
  492. NumericRecord : ^TNumericRecord;
  493. begin
  494. with cursor as TPQCursor do
  495. begin
  496. for x := 0 to PQnfields(res)-1 do
  497. if PQfname(Res, x) = FieldDef.Name then break;
  498. if PQfname(Res, x) <> FieldDef.Name then
  499. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  500. if pqgetisnull(res,CurTuple,x)=1 then
  501. result := false
  502. else
  503. begin
  504. i := PQfsize(res, x);
  505. CurrBuff := pqgetvalue(res,CurTuple,x);
  506. result := true;
  507. case FieldDef.DataType of
  508. ftInteger, ftSmallint, ftLargeInt,ftfloat :
  509. begin
  510. for tel := 1 to i do // postgres returns big-endian numbers
  511. pchar(Buffer)[tel-1] := CurrBuff[i-tel];
  512. end;
  513. ftString :
  514. begin
  515. li := pqgetlength(res,curtuple,x);
  516. Move(CurrBuff^, Buffer^, li);
  517. pchar(Buffer + li)^ := #0;
  518. i := pqfmod(res,x)-3;
  519. end;
  520. ftdate :
  521. begin
  522. li := 0;
  523. for tel := 1 to i do // postgres returns big-endian numbers
  524. pchar(@li)[tel-1] := CurrBuff[i-tel];
  525. // double(buffer^) := x + 36526; This doesn't work, please tell me what is wrong with it?
  526. dbl := pointer(buffer);
  527. dbl^ := li + 36526;
  528. i := sizeof(double);
  529. end;
  530. ftDateTime, fttime :
  531. begin
  532. dbl := pointer(buffer);
  533. dbl^ := 0;
  534. for tel := 1 to i do // postgres returns big-endian numbers
  535. pchar(Buffer)[tel-1] := CurrBuff[i-tel];
  536. dbl^ := (dbl^+3.1558464E+009)/86400; // postgres counts seconds elapsed since 1-1-2000
  537. // Now convert the mathematically-correct datetime to the
  538. // illogical windows/delphi/fpc TDateTime:
  539. if (dbl^ <= 0) and (frac(dbl^)<0) then
  540. dbl^ := trunc(dbl^)-2-frac(dbl^);
  541. end;
  542. ftBCD:
  543. begin
  544. NumericRecord := pointer(CurrBuff);
  545. NumericRecord^.Digits := BEtoN(NumericRecord^.Digits);
  546. NumericRecord^.Scale := BEtoN(NumericRecord^.Scale);
  547. NumericRecord^.Weight := BEtoN(NumericRecord^.Weight);
  548. inc(pointer(currbuff),sizeof(TNumericRecord));
  549. cur := 0;
  550. if (NumericRecord^.Digits = 0) and (NumericRecord^.Scale = 0) then // = NaN, which is not supported by Currency-type, so we return NULL
  551. result := false
  552. else
  553. begin
  554. for tel := 1 to NumericRecord^.Digits do
  555. begin
  556. cur := cur + beton(pword(currbuff)^) * intpower(10000,-(tel-1)+NumericRecord^.weight);
  557. inc(pointer(currbuff),2);
  558. end;
  559. if BEtoN(NumericRecord^.Sign) <> 0 then cur := -cur;
  560. Move(Cur, Buffer^, sizeof(currency));
  561. end;
  562. end;
  563. ftBoolean:
  564. pchar(buffer)[0] := CurrBuff[0]
  565. else
  566. result := false;
  567. end;
  568. end;
  569. end;
  570. end;
  571. procedure TPQConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  572. var qry : TSQLQuery;
  573. begin
  574. if not assigned(Transaction) then
  575. DatabaseError(SErrConnTransactionnSet);
  576. qry := tsqlquery.Create(nil);
  577. qry.transaction := Transaction;
  578. qry.database := Self;
  579. with qry do
  580. begin
  581. ReadOnly := True;
  582. sql.clear;
  583. sql.add('select '+
  584. 'ic.relname as indexname, '+
  585. 'tc.relname as tablename, '+
  586. 'ia.attname, '+
  587. 'i.indisprimary, '+
  588. 'i.indisunique '+
  589. 'from '+
  590. 'pg_attribute ta, '+
  591. 'pg_attribute ia, '+
  592. 'pg_class tc, '+
  593. 'pg_class ic, '+
  594. 'pg_index i '+
  595. 'where '+
  596. '(i.indrelid = tc.oid) and '+
  597. '(ta.attrelid = tc.oid) and '+
  598. '(ia.attrelid = i.indexrelid) and '+
  599. '(ic.oid = i.indexrelid) and '+
  600. '(ta.attnum = i.indkey[ia.attnum-1]) and '+
  601. '(upper(tc.relname)=''' + UpperCase(TableName) +''') '+
  602. 'order by '+
  603. 'ic.relname;');
  604. open;
  605. end;
  606. while not qry.eof do with IndexDefs.AddIndexDef do
  607. begin
  608. Name := trim(qry.fields[0].asstring);
  609. Fields := trim(qry.Fields[2].asstring);
  610. If qry.fields[3].asboolean then options := options + [ixPrimary];
  611. If qry.fields[4].asboolean then options := options + [ixUnique];
  612. qry.next;
  613. while (name = qry.fields[0].asstring) and (not qry.eof) do
  614. begin
  615. Fields := Fields + ';' + trim(qry.Fields[2].asstring);
  616. qry.next;
  617. end;
  618. end;
  619. qry.close;
  620. qry.free;
  621. end;
  622. function TPQConnection.GetSchemaInfoSQL(SchemaType: TSchemaType;
  623. SchemaObjectName, SchemaPattern: string): string;
  624. var s : string;
  625. begin
  626. case SchemaType of
  627. stTables : s := 'select '+
  628. 'relfilenode as recno, '+
  629. '''' + DatabaseName + ''' as catalog_name, '+
  630. ''''' as schema_name, '+
  631. 'relname as table_name, '+
  632. '0 as table_type '+
  633. 'from '+
  634. 'pg_class '+
  635. 'where '+
  636. '(relowner > 1) and relkind=''r''' +
  637. 'order by relname';
  638. stSysTables : s := 'select '+
  639. 'relfilenode as recno, '+
  640. '''' + DatabaseName + ''' as catalog_name, '+
  641. ''''' as schema_name, '+
  642. 'relname as table_name, '+
  643. '0 as table_type '+
  644. 'from '+
  645. 'pg_class '+
  646. 'where '+
  647. 'relkind=''r''' +
  648. 'order by relname';
  649. else
  650. DatabaseError(SMetadataUnavailable)
  651. end; {case}
  652. result := s;
  653. end;
  654. end.