|
6 | 6 | #include "qgsreadwritecontext.h"
|
7 | 7 |
|
8 | 8 | #include <QIODevice>
|
| 9 | +#include <QJsonDocument> |
| 10 | +#include <QJsonObject> |
9 | 11 | #include <QUrl>
|
10 | 12 |
|
| 13 | + |
| 14 | +static bool _parseMetadataDocument( const QJsonDocument &doc, QgsProjectStorage::Metadata &metadata ) |
| 15 | +{ |
| 16 | + if ( !doc.isObject() ) |
| 17 | + return false; |
| 18 | + |
| 19 | + QJsonObject docObj = doc.object(); |
| 20 | + metadata.lastModified = QDateTime(); |
| 21 | + if ( docObj.contains( "last_modified_time" ) ) |
| 22 | + { |
| 23 | + QString lastModifiedTimeStr = docObj["last_modified_time"].toString(); |
| 24 | + if ( !lastModifiedTimeStr.isEmpty() ) |
| 25 | + { |
| 26 | + QDateTime lastModifiedUtc = QDateTime::fromString( lastModifiedTimeStr, Qt::ISODate ); |
| 27 | + lastModifiedUtc.setTimeSpec( Qt::UTC ); |
| 28 | + metadata.lastModified = lastModifiedUtc.toLocalTime(); |
| 29 | + } |
| 30 | + } |
| 31 | + return true; |
| 32 | +} |
| 33 | + |
| 34 | + |
11 | 35 | static bool _projectsTableExists( QgsPostgresConn &conn, const QString &schemaName )
|
12 | 36 | {
|
13 | 37 | QString tableName( "qgis_projects" );
|
@@ -123,13 +147,18 @@ bool QgsPostgresProjectStorage::writeProject( const QString &uri, QIODevice *dev
|
123 | 147 | // read from device and write to the table
|
124 | 148 | QByteArray content = device->readAll();
|
125 | 149 |
|
126 |
| - QString metadata = "{ \"last_modified\": 123 }"; // TODO: real metadata |
| 150 | + QString metadataExpr = QStringLiteral( "(%1 || (now() at time zone 'utc')::text || %2 || current_user || %3)::jsonb" ).arg( |
| 151 | + QgsPostgresConn::quotedValue( "{ \"last_modified_time\": \"" ), |
| 152 | + QgsPostgresConn::quotedValue( "\", \"last_modified_user\": \"" ), |
| 153 | + QgsPostgresConn::quotedValue( "\" }" ) |
| 154 | + ); |
127 | 155 |
|
128 | 156 | // TODO: would be useful to have QByteArray version of PQexec() to avoid bytearray -> string -> bytearray conversion
|
129 | 157 | QString sql( "INSERT INTO %1.qgis_projects VALUES (%2, %3, E'\\\\x" );
|
130 | 158 | sql = sql.arg( QgsPostgresConn::quotedIdentifier( projectUri.schemaName ),
|
131 | 159 | QgsPostgresConn::quotedValue( projectUri.projectName ),
|
132 |
| - QgsPostgresConn::quotedValue( metadata ) ); |
| 160 | + metadataExpr // no need to quote: already quoted |
| 161 | + ); |
133 | 162 | sql += QString::fromAscii( content.toHex() );
|
134 | 163 | sql += "') ON CONFLICT (name) DO UPDATE SET content = EXCLUDED.content, metadata = EXCLUDED.metadata;";
|
135 | 164 |
|
@@ -164,6 +193,33 @@ bool QgsPostgresProjectStorage::removeProject( const QString &uri )
|
164 | 193 | }
|
165 | 194 |
|
166 | 195 |
|
| 196 | +bool QgsPostgresProjectStorage::readProjectMetadata( const QString &uri, QgsProjectStorage::Metadata &metadata ) |
| 197 | +{ |
| 198 | + QgsPostgresProjectUri projectUri = parseUri( uri ); |
| 199 | + if ( !projectUri.valid ) |
| 200 | + return false; |
| 201 | + |
| 202 | + QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( projectUri.connInfo.connectionInfo( false ) ); |
| 203 | + |
| 204 | + bool ok = false; |
| 205 | + QString sql( QStringLiteral( "SELECT metadata FROM %1.qgis_projects WHERE name = %2" ).arg( QgsPostgresConn::quotedIdentifier( projectUri.schemaName ), QgsPostgresConn::quotedValue( projectUri.projectName ) ) ); |
| 206 | + QgsPostgresResult result( conn->PQexec( sql ) ); |
| 207 | + if ( result.PQresultStatus() == PGRES_TUPLES_OK ) |
| 208 | + { |
| 209 | + if ( result.PQntuples() == 1 ) |
| 210 | + { |
| 211 | + QString metadataStr = result.PQgetvalue( 0, 0 ); |
| 212 | + QJsonDocument doc( QJsonDocument::fromJson( metadataStr.toUtf8() ) ); |
| 213 | + ok = _parseMetadataDocument( doc, metadata ); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + QgsPostgresConnPool::instance()->releaseConnection( conn ); |
| 218 | + |
| 219 | + return ok; |
| 220 | +} |
| 221 | + |
| 222 | + |
167 | 223 | QgsPostgresProjectUri QgsPostgresProjectStorage::parseUri( const QString &uri )
|
168 | 224 | {
|
169 | 225 | QUrl u = QUrl::fromEncoded( uri.toUtf8() );
|
|
0 commit comments