1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Sql.Database.Physical.Postgresql;
interface
uses
Boolean,
Integer,
Sql.Connection.Settings,
Sql.Database,
Sql.Database.Physical.Default,
Sql.Database.Physical.Postgresql.Api,
Sql.Database.PreparedStatement,
Sql.Result,
Sql.Statement,
Text;
type
TPostgresql = class(TDefaultPhysicalSqlDatabase)
private
Dll: TPostgresqlDll;
Connection: PPGConn;
protected
function GetCouldConnect(): TBoolean; override;
procedure ExecuteStartTransaction(); override;
procedure ExecuteCommitTransaction(); override;
procedure ExecuteRollbackTransaction(); override;
function CompileSql(Statement: TSqlStatement): TText;
function EscapeConnectValue(const Value: TText): TText;
function ConnectKeyValuePair(const Key: TText; const Value: TText): TText;
public
constructor Create(Dll: TPostgresqlDll; ConnectionSettings: TSqlConnectionSettings); reintroduce; virtual;
destructor Destroy(); override;
function Query(Statement: TSqlStatement; ResultMethod: TSqlQueryResultMethod = sqrmStoreResult): TSqlResult; override;
function Prepare(Statement: TSqlStatement): TSqlPreparedStatement; override;
function ConnectionIdentifier(): TInteger; override;
function InsertId(): TInteger; override;
function NumberOfAffectedRows(): TInteger; override;
function IsAlive(): TBoolean; override;
function UserExists(const Name: TText): TBoolean; override;
function GetSystemInformation(): TObject; override;
function Embellish(): TSqlDatabase; override;
end;
implementation
uses
Character,
Character.Encoding,
Exception,
Integer._32,
Integer._32.TextFormat,
Sql,
Sql.Database.Logical.Compatibility,
Sql.Database.Physical.Postgresql.Compiler,
Sql.Database.Physical.Postgresql.Result,
Sql.Database.Physical.Postgresql.Schema.System,
Sql.Database.Physical.Postgresql.SystemInformation,
Sql.Exception,
Windows.Api.Kernel.DateTime;
{ TPostgresql }
function TPostgresql.CompileSql(Statement: TSqlStatement): TText;
var
Compiler: TPostgresqlCompiler;
begin
Statement.AddReference();
Compiler := TPostgresqlCompiler.Create();
Result := Compiler.Compile(TCharacterEncoding.Windows1252, Statement); // TODO: Verify encoding
Compiler.Free();
Statement.ReleaseReference();
end;
function TPostgresql.ConnectionIdentifier(): TInteger;
begin
Result := Dll.backendPID(Connection);
end;
function TPostgresql.ConnectKeyValuePair(const Key, Value: TText): TText;
begin
Result := Key + '=''' + EscapeConnectValue(Value) + '''';
end;
constructor TPostgresql.Create(Dll: TPostgresqlDll; ConnectionSettings: TSqlConnectionSettings);
begin
inherited Create(ConnectionSettings);
Self.Dll := Dll;
if ConnectionSettings.Port = 0 then
ConnectionSettings.Port := POSTGRESQL_PORT;
if Dll.IsLoaded then
Connection := Dll.connectdb(
ConnectKeyValuePair('host', ConnectionSettings.Server) +
' ' + ConnectKeyValuePair('user', ConnectionSettings.UserName) +
' ' + ConnectKeyValuePair('password', ConnectionSettings.Password) +
' ' + ConnectKeyValuePair('dbname', ConnectionSettings.DatabaseName) +
' ' + ConnectKeyValuePair('port', Integer32ToText(ConnectionSettings.Port))
);
end;
destructor TPostgresql.Destroy();
begin
if Dll.IsLoaded then
Dll.finish(Connection);
inherited Destroy();
end;
function TPostgresql.Embellish(): TSqlDatabase;
begin
Result := TCompatibilitySqlDatabase.Create(Self, True);
end;
function TPostgresql.EscapeConnectValue(const Value: TText): TText;
var
i: TInteger32;
c: TCharacter;
begin
Result := '';
for i := 0 to Value.Count - 1 do
begin
c := Value[i];
if c in ['''', '\'] then
Result := Result + '\' + c
else
Result := Result + c;
end;
end;
procedure TPostgresql.ExecuteCommitTransaction();
begin
Dll.exec(Connection, 'COMMIT');
end;
procedure TPostgresql.ExecuteRollbackTransaction();
begin
Dll.exec(Connection, 'ROLLBACK');
end;
procedure TPostgresql.ExecuteStartTransaction();
begin
Dll.exec(Connection, 'BEGIN');
end;
function TPostgresql.GetCouldConnect(): TBoolean;
begin
Result := Dll.IsLoaded and (Dll.status(Connection) = CONNECTION_OK);
end;
function TPostgresql.GetSystemInformation(): TObject;
begin
Result := TPostgresqlSystemInformation.Instance;
end;
function TPostgresql.InsertId(): TInteger;
begin
Result := SelectInteger(SelectLastInsertId());
end;
function TPostgresql.IsAlive(): TBoolean;
begin
Result := Dll.status(Connection) = CONNECTION_OK;
end;
function TPostgresql.NumberOfAffectedRows(): TInteger;
begin
raise EException.Create('Not implemented');
end;
function TPostgresql.Prepare(Statement: TSqlStatement): TSqlPreparedStatement;
begin
raise EException.Create('Not implemented');
end;
function TPostgresql.Query(Statement: TSqlStatement; ResultMethod: TSqlQueryResultMethod): TSqlResult;
var
PGResult: PPGResult;
CompiledSql: TText;
Status: TExecStatusType;
begin
CompiledSql := CompileSql(Statement);
PGResult := Dll.exec(Connection, CompiledSql);
Status := Dll.resultStatus(PGResult);
if Status = PGRES_TUPLES_OK then
Result := TPostgresqlResult.Create(Dll, PGResult)
else if Status = PGRES_COMMAND_OK then
begin
Result := nil;
Dll.clear(PGResult);
end
else
raise ESqlQuery.Create(CompiledSql, Dll.resultErrorMessage(PGResult));
TickCountLastQuery := GetTickCount();
end;
function TPostgresql.UserExists(const Name: TText): TBoolean;
var
UserTable: TPostgresqlUserTable;
begin
UserTable := TPostgresqlSystemSchema.Instance.UserTable;
Result := Exists(Select([ UserTable.Name ], [ UserTable ], [ UserTable.Name = Name ]));
end;
end.
|