-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgspostgresprojectstorage.cpp
342 lines (278 loc) · 12.5 KB
/
qgspostgresprojectstorage.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/***************************************************************************
qgspostgresprojectstorage.cpp
---------------------
begin : April 2018
copyright : (C) 2018 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgspostgresprojectstorage.h"
#include "qgspostgresconn.h"
#include "qgspostgresconnpool.h"
#include "qgsreadwritecontext.h"
#include <QIODevice>
#include <QJsonDocument>
#include <QJsonObject>
#include <QUrl>
static bool _parseMetadataDocument( const QJsonDocument &doc, QgsProjectStorage::Metadata &metadata )
{
if ( !doc.isObject() )
return false;
QJsonObject docObj = doc.object();
metadata.lastModified = QDateTime();
if ( docObj.contains( "last_modified_time" ) )
{
QString lastModifiedTimeStr = docObj["last_modified_time"].toString();
if ( !lastModifiedTimeStr.isEmpty() )
{
QDateTime lastModifiedUtc = QDateTime::fromString( lastModifiedTimeStr, Qt::ISODate );
lastModifiedUtc.setTimeSpec( Qt::UTC );
metadata.lastModified = lastModifiedUtc.toLocalTime();
}
}
return true;
}
static bool _projectsTableExists( QgsPostgresConn &conn, const QString &schemaName )
{
QString tableName( "qgis_projects" );
QString sql( QStringLiteral( "SELECT COUNT(*) FROM information_schema.tables WHERE table_name=%1 and table_schema=%2" )
.arg( QgsPostgresConn::quotedValue( tableName ), QgsPostgresConn::quotedValue( schemaName ) )
);
QgsPostgresResult res( conn.PQexec( sql ) );
return res.PQgetvalue( 0, 0 ).toInt() > 0;
}
QStringList QgsPostgresProjectStorage::listProjects( const QString &uri )
{
QStringList lst;
QgsPostgresProjectUri projectUri = decodeUri( uri );
if ( !projectUri.valid )
return lst;
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( projectUri.connInfo.connectionInfo( false ) );
if ( !conn )
return lst;
if ( _projectsTableExists( *conn, projectUri.schemaName ) )
{
QString sql( QStringLiteral( "SELECT name FROM %1.qgis_projects" ).arg( QgsPostgresConn::quotedIdentifier( projectUri.schemaName ) ) );
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() == PGRES_TUPLES_OK )
{
int count = result.PQntuples();
for ( int i = 0; i < count; ++i )
{
QString name = result.PQgetvalue( i, 0 );
lst << name;
}
}
}
QgsPostgresConnPool::instance()->releaseConnection( conn );
return lst;
}
bool QgsPostgresProjectStorage::readProject( const QString &uri, QIODevice *device, QgsReadWriteContext &context )
{
QgsPostgresProjectUri projectUri = decodeUri( uri );
if ( !projectUri.valid )
{
context.pushMessage( QObject::tr( "Invalid URI for PostgreSQL provider: " ) + uri, Qgis::Critical );
return false;
}
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( projectUri.connInfo.connectionInfo( false ) );
if ( !conn )
{
context.pushMessage( QObject::tr( "Could not connect to the database: " ) + projectUri.connInfo.connectionInfo( false ), Qgis::Critical );
return false;
}
if ( !_projectsTableExists( *conn, projectUri.schemaName ) )
{
context.pushMessage( QObject::tr( "Table qgis_projects does not exist or it is not accessible." ), Qgis::Critical );
QgsPostgresConnPool::instance()->releaseConnection( conn );
return false;
}
bool ok = false;
QString sql( QStringLiteral( "SELECT content FROM %1.qgis_projects WHERE name = %2" ).arg( QgsPostgresConn::quotedIdentifier( projectUri.schemaName ), QgsPostgresConn::quotedValue( projectUri.projectName ) ) );
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() == PGRES_TUPLES_OK )
{
if ( result.PQntuples() == 1 )
{
// TODO: would be useful to have QByteArray version of PQgetvalue to avoid bytearray -> string -> bytearray conversion
QString hexEncodedContent( result.PQgetvalue( 0, 0 ) );
QByteArray binaryContent( QByteArray::fromHex( hexEncodedContent.toUtf8() ) );
device->write( binaryContent );
device->seek( 0 );
ok = true;
}
else
{
context.pushMessage( QObject::tr( "The project '%1' does not exist in schema '%2'." ).arg( projectUri.projectName, projectUri.schemaName ), Qgis::Critical );
}
}
QgsPostgresConnPool::instance()->releaseConnection( conn );
return ok;
}
bool QgsPostgresProjectStorage::writeProject( const QString &uri, QIODevice *device, QgsReadWriteContext &context )
{
QgsPostgresProjectUri projectUri = decodeUri( uri );
if ( !projectUri.valid )
{
context.pushMessage( QObject::tr( "Invalid URI for PostgreSQL provider: " ) + uri, Qgis::Critical );
return false;
}
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( projectUri.connInfo.connectionInfo( false ) );
if ( !conn )
{
context.pushMessage( QObject::tr( "Could not connect to the database: " ) + projectUri.connInfo.connectionInfo( false ), Qgis::Critical );
return false;
}
if ( !_projectsTableExists( *conn, projectUri.schemaName ) )
{
// try to create projects table
QString sql = QStringLiteral( "CREATE TABLE %1.qgis_projects(name TEXT PRIMARY KEY, metadata JSONB, content BYTEA)" ).arg( QgsPostgresConn::quotedIdentifier( projectUri.schemaName ) );
QgsPostgresResult res( conn->PQexec( sql ) );
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
{
QString errCause = QObject::tr( "Unable to save project. It's not possible to create the destination table on the database. Maybe this is due to database permissions (user=%1). Please contact your database admin." ).arg( projectUri.connInfo.username() );
context.pushMessage( errCause, Qgis::Critical );
QgsPostgresConnPool::instance()->releaseConnection( conn );
return false;
}
}
// read from device and write to the table
QByteArray content = device->readAll();
QString metadataExpr = QStringLiteral( "(%1 || (now() at time zone 'utc')::text || %2 || current_user || %3)::jsonb" ).arg(
QgsPostgresConn::quotedValue( "{ \"last_modified_time\": \"" ),
QgsPostgresConn::quotedValue( "\", \"last_modified_user\": \"" ),
QgsPostgresConn::quotedValue( "\" }" )
);
// TODO: would be useful to have QByteArray version of PQexec() to avoid bytearray -> string -> bytearray conversion
QString sql( "INSERT INTO %1.qgis_projects VALUES (%2, %3, E'\\\\x" );
sql = sql.arg( QgsPostgresConn::quotedIdentifier( projectUri.schemaName ),
QgsPostgresConn::quotedValue( projectUri.projectName ),
metadataExpr // no need to quote: already quoted
);
sql += QString::fromAscii( content.toHex() );
sql += "') ON CONFLICT (name) DO UPDATE SET content = EXCLUDED.content, metadata = EXCLUDED.metadata;";
QgsPostgresResult res( conn->PQexec( sql ) );
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
{
QString errCause = QObject::tr( "Unable to insert or update project (project=%1) in the destination table on the database. Maybe this is due to table permissions (user=%2). Please contact your database admin." ).arg( projectUri.projectName, projectUri.connInfo.username() );
context.pushMessage( errCause, Qgis::Critical );
QgsPostgresConnPool::instance()->releaseConnection( conn );
return false;
}
QgsPostgresConnPool::instance()->releaseConnection( conn );
return true;
}
bool QgsPostgresProjectStorage::removeProject( const QString &uri )
{
QgsPostgresProjectUri projectUri = decodeUri( uri );
if ( !projectUri.valid )
return false;
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( projectUri.connInfo.connectionInfo( false ) );
if ( !conn )
return false;
bool removed = false;
if ( _projectsTableExists( *conn, projectUri.schemaName ) )
{
QString sql( QStringLiteral( "DELETE FROM %1.qgis_projects WHERE name = %2" ).arg( QgsPostgresConn::quotedIdentifier( projectUri.schemaName ), QgsPostgresConn::quotedValue( projectUri.projectName ) ) );
QgsPostgresResult res( conn->PQexec( sql ) );
removed = res.PQresultStatus() == PGRES_COMMAND_OK;
}
QgsPostgresConnPool::instance()->releaseConnection( conn );
return removed;
}
bool QgsPostgresProjectStorage::readProjectStorageMetadata( const QString &uri, QgsProjectStorage::Metadata &metadata )
{
QgsPostgresProjectUri projectUri = decodeUri( uri );
if ( !projectUri.valid )
return false;
QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( projectUri.connInfo.connectionInfo( false ) );
if ( !conn )
return false;
bool ok = false;
QString sql( QStringLiteral( "SELECT metadata FROM %1.qgis_projects WHERE name = %2" ).arg( QgsPostgresConn::quotedIdentifier( projectUri.schemaName ), QgsPostgresConn::quotedValue( projectUri.projectName ) ) );
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() == PGRES_TUPLES_OK )
{
if ( result.PQntuples() == 1 )
{
metadata.name = projectUri.projectName;
QString metadataStr = result.PQgetvalue( 0, 0 );
QJsonDocument doc( QJsonDocument::fromJson( metadataStr.toUtf8() ) );
ok = _parseMetadataDocument( doc, metadata );
}
}
QgsPostgresConnPool::instance()->releaseConnection( conn );
return ok;
}
#ifdef HAVE_GUI
#include "qgspostgresprojectstoragedialog.h"
QString QgsPostgresProjectStorage::visibleName()
{
return QObject::tr( "PostgreSQL" );
}
QString QgsPostgresProjectStorage::showLoadGui()
{
QgsPostgresProjectStorageDialog dlg( false );
if ( !dlg.exec() )
return QString();
return dlg.currentProjectUri();
}
QString QgsPostgresProjectStorage::showSaveGui()
{
QgsPostgresProjectStorageDialog dlg( true );
if ( !dlg.exec() )
return QString();
return dlg.currentProjectUri();
}
#endif
QString QgsPostgresProjectStorage::encodeUri( const QgsPostgresProjectUri &postUri )
{
QUrl u;
QUrlQuery urlQuery;
u.setScheme( "postgresql" );
u.setHost( postUri.connInfo.host() );
if ( !postUri.connInfo.port().isEmpty() )
u.setPort( postUri.connInfo.port().toInt() );
u.setUserName( postUri.connInfo.username() );
u.setPassword( postUri.connInfo.password() );
if ( !postUri.connInfo.service().isEmpty() )
urlQuery.addQueryItem( "service", postUri.connInfo.service() );
if ( !postUri.connInfo.authConfigId().isEmpty() )
urlQuery.addQueryItem( "authcfg", postUri.connInfo.authConfigId() );
if ( postUri.connInfo.sslMode() != QgsDataSourceUri::SslPrefer )
urlQuery.addQueryItem( "sslmode", QgsDataSourceUri::encodeSslMode( postUri.connInfo.sslMode() ) );
urlQuery.addQueryItem( "dbname", postUri.connInfo.database() );
urlQuery.addQueryItem( "schema", postUri.schemaName );
if ( !postUri.projectName.isEmpty() )
urlQuery.addQueryItem( "project", postUri.projectName );
u.setQuery( urlQuery );
return QString::fromUtf8( u.toEncoded() );
}
QgsPostgresProjectUri QgsPostgresProjectStorage::decodeUri( const QString &uri )
{
QUrl u = QUrl::fromEncoded( uri.toUtf8() );
QUrlQuery urlQuery( u.query() );
QgsPostgresProjectUri postUri;
postUri.valid = u.isValid();
QString host = u.host();
QString port = u.port() != -1 ? QString::number( u.port() ) : QString();
QString username = u.userName();
QString password = u.password();
QgsDataSourceUri::SslMode sslMode = QgsDataSourceUri::decodeSslMode( urlQuery.queryItemValue( "sslmode" ) );
QString authConfigId = urlQuery.queryItemValue( "authcfg" );
QString dbName = urlQuery.queryItemValue( "dbname" );
QString service = urlQuery.queryItemValue( "service" );
if ( !service.isEmpty() )
postUri.connInfo.setConnection( service, dbName, username, password, sslMode, authConfigId );
else
postUri.connInfo.setConnection( host, port, dbName, username, password, sslMode, authConfigId );
postUri.schemaName = urlQuery.queryItemValue( "schema" );
postUri.projectName = urlQuery.queryItemValue( "project" );
return postUri;
}