|
23 | 23 | #include <qgsmessagelog.h>
|
24 | 24 | #include <qgsrectangle.h>
|
25 | 25 | #include <qgscoordinatereferencesystem.h>
|
| 26 | +#include <QMessageBox> |
26 | 27 |
|
27 | 28 | #include "qgsvectorlayerimport.h"
|
28 | 29 | #include "qgsprovidercountcalcevent.h"
|
@@ -3169,7 +3170,7 @@ QGISEXTERN bool deleteLayer( const QString& uri, QString& errCause )
|
3169 | 3170 | }
|
3170 | 3171 | schemaTableName += QgsPostgresConn::quotedIdentifier( tableName );
|
3171 | 3172 |
|
3172 |
| - QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
| 3173 | + QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
3173 | 3174 | if ( !conn )
|
3174 | 3175 | {
|
3175 | 3176 | errCause = QObject::tr( "Connection to database failed" );
|
@@ -3231,254 +3232,260 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
|
3231 | 3232 | const QString& uiFileContent, bool useAsDefault, QString& errCause )
|
3232 | 3233 | {
|
3233 | 3234 | QgsDataSourceURI dsUri( uri );
|
| 3235 | + QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column, owner, isdef, name, desc; |
| 3236 | + QString styleTableName = QObject::tr( "layer_styles" ); |
| 3237 | + QgsPostgresResult res; |
3234 | 3238 |
|
3235 |
| - QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
| 3239 | + QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
3236 | 3240 | if ( !conn )
|
3237 | 3241 | {
|
3238 | 3242 | errCause = QObject::tr( "Connection to database failed" );
|
3239 | 3243 | return false;
|
3240 | 3244 | }
|
3241 | 3245 |
|
3242 |
| - QgsPostgresResult res = conn->PQexec( "SELECT COUNT(*) FROM information_schema.tables WHERE table_name='layer_styles'" ); |
3243 |
| - if ( res.PQgetvalue( 0, 0 ).toInt() == 0 ) |
3244 |
| - { |
3245 |
| - res = conn->PQexec( "CREATE TABLE layer_styles(" |
3246 |
| - "id SERIAL PRIMARY KEY" |
3247 |
| - ",f_table_catalog varchar(256)" |
3248 |
| - ",f_table_schema varchar(256)" |
3249 |
| - ",f_table_name varchar(256)" |
3250 |
| - ",f_geometry_column varchar(256)" |
3251 |
| - ",styleName varchar(30)" |
3252 |
| - ",styleQML xml" |
3253 |
| - ",styleSLD xml" |
3254 |
| - ",useAsDefault boolean" |
3255 |
| - ",description text" |
3256 |
| - ",owner varchar(30)" |
3257 |
| - ",ui xml" |
3258 |
| - ",update_time timestamp DEFAULT CURRENT_TIMESTAMP" |
3259 |
| - ")" ); |
3260 |
| - if ( res.PQresultStatus() != PGRES_COMMAND_OK ) |
3261 |
| - { |
3262 |
| - errCause = QObject::tr( "Unable to save layer style. It's not possible to create the destination table on the database. Maybe this is due to table permissions (user=%1). Please contact your database admin" ).arg( dsUri.username() ); |
3263 |
| - conn->disconnect(); |
3264 |
| - return false; |
3265 |
| - } |
3266 |
| - } |
3267 |
| - |
3268 |
| - QString uiFileColumn; |
3269 |
| - QString uiFileValue; |
3270 |
| - if ( !uiFileContent.isEmpty() ) |
3271 |
| - { |
3272 |
| - uiFileColumn = ",ui"; |
3273 |
| - uiFileValue = QString( ",XMLPARSE(DOCUMENT %1)" ).arg( QgsPostgresConn::quotedValue( uiFileContent ) ); |
3274 |
| - } |
3275 |
| - |
3276 |
| - QString sql = QString( "INSERT INTO layer_styles(" |
3277 |
| - "f_table_catalog,f_table_schema,f_table_name,f_geometry_column,styleName,styleQML,styleSLD,useAsDefault,description,owner%11" |
3278 |
| - ") VALUES (" |
3279 |
| - "%1,%2,%3,%4,%5,XMLPARSE(DOCUMENT %6),XMLPARSE(DOCUMENT %7),%8,%9,%10%12" |
3280 |
| - ")" ) |
3281 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.database() ) ) |
3282 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.schema() ) ) |
3283 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.table() ) ) |
3284 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) ) |
3285 |
| - .arg( QgsPostgresConn::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) ) |
3286 |
| - .arg( QgsPostgresConn::quotedValue( qmlStyle ) ) |
3287 |
| - .arg( QgsPostgresConn::quotedValue( sldStyle ) ) |
3288 |
| - .arg( useAsDefault ? "true" : "false" ) |
3289 |
| - .arg( QgsPostgresConn::quotedValue( styleDescription.isEmpty() ? QDateTime::currentDateTime().toString() : styleDescription ) ) |
3290 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.username() ) ) |
3291 |
| - .arg( uiFileColumn ) |
3292 |
| - .arg( uiFileValue ); |
3293 |
| - |
3294 |
| - QString checkQuery = QString( "SELECT styleName" |
3295 |
| - " FROM layer_styles" |
3296 |
| - " WHERE f_table_catalog=%1" |
3297 |
| - " AND f_table_schema=%2" |
3298 |
| - " AND f_table_name=%3" |
3299 |
| - " AND f_geometry_column=%4" |
3300 |
| - " AND styleName=%5" ) |
3301 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.database() ) ) |
3302 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.schema() ) ) |
3303 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.table() ) ) |
3304 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) ) |
3305 |
| - .arg( QgsPostgresConn::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) ); |
3306 |
| - |
3307 |
| - res = conn->PQexec( checkQuery ); |
3308 |
| - if ( res.PQntuples() > 0 ) |
3309 |
| - { |
3310 |
| - sql = QString( "UPDATE layer_styles" |
3311 |
| - " SET useAsDefault=%1" |
3312 |
| - ",styleQML=XMLPARSE(DOCUMENT %2)" |
3313 |
| - ",styleSLD=XMLPARSE(DOCUMENT %3)" |
3314 |
| - ",description=%4" |
3315 |
| - ",owner=%5" |
3316 |
| - " WHERE f_table_catalog=%6" |
3317 |
| - " AND f_table_schema=%7" |
3318 |
| - " AND f_table_name=%8" |
3319 |
| - " AND f_geometry_column=%9" |
3320 |
| - " AND styleName=%10" ) |
3321 |
| - .arg( useAsDefault ? "true" : "false" ) |
3322 |
| - .arg( QgsPostgresConn::quotedValue( qmlStyle ) ) |
3323 |
| - .arg( QgsPostgresConn::quotedValue( sldStyle ) ) |
3324 |
| - .arg( QgsPostgresConn::quotedValue( styleDescription.isEmpty() ? QDateTime::currentDateTime().toString() : styleDescription ) ) |
3325 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.username() ) ) |
3326 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.database() ) ) |
3327 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.schema() ) ) |
3328 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.table() ) ) |
3329 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) ) |
3330 |
| - .arg( QgsPostgresConn::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) ); |
3331 |
| - } |
3332 |
| - |
3333 |
| - if ( useAsDefault ) |
3334 |
| - { |
3335 |
| - QString removeDefaultSql = QString( "UPDATE layer_styles" |
3336 |
| - " SET useAsDefault=false" |
3337 |
| - " WHERE f_table_catalog=%1" |
3338 |
| - " AND f_table_schema=%2" |
3339 |
| - " AND f_table_name=%3" |
3340 |
| - " AND f_geometry_column=%4" ) |
3341 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.database() ) ) |
3342 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.schema() ) ) |
3343 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.table() ) ) |
3344 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) ); |
3345 |
| - sql = QString( "BEGIN; %1; %2; COMMIT;" ).arg( removeDefaultSql ).arg( sql ); |
| 3246 | + QString checkExitingTableQuery = QObject::tr( "SELECT COUNT(*) FROM information_schema.tables WHERE table_name='%1'" ).arg( styleTableName ); |
| 3247 | + |
| 3248 | + PGresult* result = conn->PQexec( checkExitingTableQuery ); |
| 3249 | + char* c = PQgetvalue( result, 0, 0 ); |
| 3250 | + if( *c == '0' ) |
| 3251 | + { |
| 3252 | + QString createTabeQuery = QObject::tr( "CREATE TABLE public.%1 (id SERIAL PRIMARY KEY, f_table_catalog varchar(256), f_table_schema varchar(256), f_table_name varchar(256), f_geometry_column varchar(256), styleName varchar(30), styleQML xml, styleSLD xml, useAsDefault boolean, description text, owner varchar(30), ui xml, update_time timestamp DEFAULT CURRENT_TIMESTAMP );" ).arg( styleTableName ); |
| 3253 | + |
| 3254 | + res = conn->PQexec( createTabeQuery ); |
| 3255 | + if ( res.PQresultStatus() != PGRES_COMMAND_OK ) |
| 3256 | + { |
| 3257 | + errCause = QObject::tr( "Unable to save layer style. It's not possible to create the destination table on the database. Maybe this is due to table permissions (user=%1). Please contact your database admin" ).arg( dsUri.username() ); |
| 3258 | + conn->disconnect(); |
| 3259 | + return false; |
| 3260 | + } |
| 3261 | + } |
| 3262 | + |
| 3263 | + f_table_catalog = dsUri.database(); |
| 3264 | + f_table_schema = dsUri.schema(); |
| 3265 | + f_table_name = dsUri.table(); |
| 3266 | + f_geometry_column = dsUri.geometryColumn(); |
| 3267 | + owner = dsUri.username(); |
| 3268 | + isdef = (useAsDefault) ? QObject::tr( "true" ) : QObject::tr( "false" ); |
| 3269 | + name = ( styleName.isEmpty() ) ? dsUri.table() : styleName; |
| 3270 | + desc = ( styleDescription.isEmpty() ) ? QDateTime::currentDateTime().toString() : styleDescription; |
| 3271 | + |
| 3272 | + QString uiFileColumn( "" ); |
| 3273 | + QString uiFileValue( "" ); |
| 3274 | + if( !uiFileContent.isEmpty() ) |
| 3275 | + { |
| 3276 | + uiFileColumn.append( QObject::tr( ", ui" ) ); |
| 3277 | + uiFileValue.append( QObject::tr( ",XMLPARSE(DOCUMENT %1)" ).arg( QgsPostgresConn::quotedValue( uiFileContent ) ) ); |
| 3278 | + } |
| 3279 | + |
| 3280 | + QString sql = QObject::tr( "INSERT INTO %1 ( f_table_catalog, " |
| 3281 | + "f_table_schema, f_table_name, f_geometry_column, " |
| 3282 | + "styleName, styleQML, styleSLD, useAsDefault, " |
| 3283 | + "description, owner %12) " |
| 3284 | + "VALUES(%2,%3,%4,%5,%6,XMLPARSE(DOCUMENT %7)," |
| 3285 | + "XMLPARSE(DOCUMENT %8),%9,%10,%11 %13);" ) |
| 3286 | + .arg( styleTableName ) |
| 3287 | + .arg( QgsPostgresConn::quotedValue( f_table_catalog ) ) |
| 3288 | + .arg( QgsPostgresConn::quotedValue( f_table_schema ) ) |
| 3289 | + .arg( QgsPostgresConn::quotedValue( f_table_name ) ) |
| 3290 | + .arg( QgsPostgresConn::quotedValue( f_geometry_column ) ) |
| 3291 | + .arg( QgsPostgresConn::quotedValue( name ) ) |
| 3292 | + .arg( QgsPostgresConn::quotedValue( qmlStyle ) ) |
| 3293 | + .arg( QgsPostgresConn::quotedValue( sldStyle ) ) |
| 3294 | + .arg( isdef ) |
| 3295 | + .arg( QgsPostgresConn::quotedValue( desc ) ) |
| 3296 | + .arg( QgsPostgresConn::quotedValue( owner ) ) |
| 3297 | + .arg( uiFileColumn ) |
| 3298 | + .arg( uiFileValue ); |
| 3299 | + |
| 3300 | + QString checkQuery = QObject::tr( "SELECT styleName FROM %1 WHERE f_table_catalog=%2 AND f_table_schema=%3 AND f_table_name=%4 AND f_geometry_column=%5 AND styleName=%6" ) |
| 3301 | + .arg( styleTableName ) |
| 3302 | + .arg( QgsPostgresConn::quotedValue( f_table_catalog ) ) |
| 3303 | + .arg( QgsPostgresConn::quotedValue( f_table_schema ) ) |
| 3304 | + .arg( QgsPostgresConn::quotedValue(f_table_name ) ) |
| 3305 | + .arg( QgsPostgresConn::quotedValue(f_geometry_column ) ) |
| 3306 | + .arg( QgsPostgresConn::quotedValue( name ) ); |
| 3307 | + |
| 3308 | + result = conn->PQexec( checkQuery ); |
| 3309 | + if( PQntuples( result ) > 0 ) |
| 3310 | + { |
| 3311 | + |
| 3312 | + QString message = QObject::tr( "A style named \"%1\" already exists in the database for this layer. Do you want to overwrite it?" ).arg( name ); |
| 3313 | + QMessageBox* duplicateMessageBox = new QMessageBox( QMessageBox::Question, "Save style in database", message, QMessageBox::Yes|QMessageBox::No ); |
| 3314 | + |
| 3315 | + if( duplicateMessageBox->exec() == QMessageBox::No ){ |
| 3316 | + errCause = QObject::tr( "Operation aborted. No changes were made in the database" ); |
| 3317 | + return false; |
| 3318 | + } |
| 3319 | + |
| 3320 | + sql = QObject::tr( "UPDATE %1 SET useAsDefault=%2, styleQML=XMLPARSE(DOCUMENT %3), styleSLD=XMLPARSE(DOCUMENT %4), description=%5, owner=%6 WHERE f_table_catalog=%7 AND f_table_schema=%8 AND f_table_name=%9 AND f_geometry_column=%10 AND styleName=%11;") |
| 3321 | + .arg( styleTableName ) |
| 3322 | + .arg( isdef ) |
| 3323 | + .arg( QgsPostgresConn::quotedValue( qmlStyle |
| 3324 | + ) ) |
| 3325 | + .arg( QgsPostgresConn::quotedValue( sldStyle ) ) |
| 3326 | + .arg( QgsPostgresConn::quotedValue( desc ) ) |
| 3327 | + .arg( QgsPostgresConn::quotedValue( owner ) ) |
| 3328 | + .arg( QgsPostgresConn::quotedValue( f_table_catalog ) ) |
| 3329 | + .arg( QgsPostgresConn::quotedValue( f_table_schema ) ) |
| 3330 | + .arg( QgsPostgresConn::quotedValue( f_table_name ) ) |
| 3331 | + .arg( QgsPostgresConn::quotedValue( f_geometry_column ) ) |
| 3332 | + .arg( QgsPostgresConn::quotedValue( name ) ); |
| 3333 | + } |
| 3334 | + |
| 3335 | + if( useAsDefault ) |
| 3336 | + { |
| 3337 | + QString removeDefaultSql = QObject::tr( "UPDATE %1 SET useAsDefault=false WHERE f_table_catalog=%2 AND f_table_schema=%3 AND f_table_name=%4 AND f_geometry_column=%5;") |
| 3338 | + .arg( styleTableName ) |
| 3339 | + .arg( QgsPostgresConn::quotedValue( f_table_catalog ) ) |
| 3340 | + .arg( QgsPostgresConn::quotedValue( f_table_schema ) ) |
| 3341 | + .arg( QgsPostgresConn::quotedValue(f_table_name ) ) |
| 3342 | + .arg( QgsPostgresConn::quotedValue(f_geometry_column ) ); |
| 3343 | + sql = QObject::tr("BEGIN; %1 %2 COMMIT;") |
| 3344 | + .arg( removeDefaultSql ).arg( sql ); |
3346 | 3345 | }
|
3347 | 3346 |
|
3348 | 3347 | res = conn->PQexec( sql );
|
3349 | 3348 | conn->disconnect();
|
3350 | 3349 | if ( res.PQresultStatus() != PGRES_COMMAND_OK )
|
3351 | 3350 | {
|
3352 |
| - errCause = QObject::tr( "Unable to save layer style. It's not possible to insert a new record into the style table. Maybe this is due to table permissions (user=%1). Please contact your database administrator." ).arg( dsUri.username() ); |
| 3351 | + errCause = QObject::tr( "Unable to save layer style. It's not possible to insert a new record in style table. Maybe this is due to table permissions (user=%1). Please contact your database admin" ).arg( owner ); |
3353 | 3352 | return false;
|
3354 | 3353 | }
|
3355 |
| - |
3356 | 3354 | return true;
|
3357 | 3355 | }
|
3358 | 3356 |
|
3359 | 3357 |
|
3360 | 3358 | QGISEXTERN QString loadStyle( const QString& uri, QString& errCause )
|
3361 | 3359 | {
|
3362 |
| - QgsDataSourceURI dsUri( uri ); |
| 3360 | + QgsDataSourceURI dsUri( uri ); |
| 3361 | + QString styleTableName = QObject::tr( "layer_styles" ); |
| 3362 | + QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column; |
3363 | 3363 |
|
3364 |
| - QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
3365 |
| - if ( !conn ) |
3366 |
| - { |
3367 |
| - errCause = QObject::tr( "Connection to database failed" ); |
3368 |
| - return ""; |
3369 |
| - } |
| 3364 | + QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
| 3365 | + if ( !conn ) |
| 3366 | + { |
| 3367 | + errCause = QObject::tr( "Connection to database failed" ); |
| 3368 | + return QObject::tr( "" ); |
| 3369 | + } |
| 3370 | + |
| 3371 | + f_table_catalog = dsUri.database(); |
| 3372 | + f_table_schema = dsUri.schema(); |
| 3373 | + f_table_name = dsUri.table(); |
| 3374 | + f_geometry_column = dsUri.geometryColumn(); |
3370 | 3375 |
|
3371 |
| - QString selectQmlQuery = QString( "SELECT styleQML" |
3372 |
| - " FROM layer_styles" |
3373 |
| - " WHERE f_table_catalog=%1" |
3374 |
| - " AND f_table_schema=%2" |
3375 |
| - " AND f_table_name=%3" |
3376 |
| - " AND f_geometry_column=%4" |
3377 |
| - " ORDER BY CASE WHEN useAsDefault THEN 1 ELSE 2 END" |
3378 |
| - ",update_time DESC LIMIT 1" ) |
3379 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.database() ) ) |
3380 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.schema() ) ) |
3381 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.table() ) ) |
3382 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) ); |
| 3376 | + QString selectQmlQuery = QObject::tr( "SELECT styleQML FROM %1 WHERE f_table_catalog=%2 AND f_table_schema=%3 AND f_table_name=%4 AND f_geometry_column=%5 ORDER BY (CASE WHEN useAsDefault THEN 1 ELSE 2 END), update_time DESC LIMIT 1;") |
| 3377 | + .arg( styleTableName ) |
| 3378 | + .arg( QgsPostgresConn::quotedValue( f_table_catalog ) ) |
| 3379 | + .arg( QgsPostgresConn::quotedValue( f_table_schema ) ) |
| 3380 | + .arg( QgsPostgresConn::quotedValue( f_table_name ) ) |
| 3381 | + .arg( QgsPostgresConn::quotedValue( f_geometry_column ) ); |
3383 | 3382 |
|
3384 |
| - QgsPostgresResult result = conn->PQexec( selectQmlQuery ); |
| 3383 | + PGresult* result = conn->PQexec( selectQmlQuery ); |
| 3384 | + if( PQntuples(result) == 1 ) |
| 3385 | + { |
| 3386 | + char* c = PQgetvalue( result, 0, 0 ); |
| 3387 | + return QObject::tr( c );; |
| 3388 | + } |
3385 | 3389 |
|
3386 |
| - return result.PQntuples() == 1 ? result.PQgetvalue( 0, 0 ) : ""; |
| 3390 | + return QObject::tr( "" ); |
3387 | 3391 | }
|
3388 | 3392 |
|
3389 |
| -QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &names, |
3390 |
| - QStringList &descriptions, QString& errCause ) |
| 3393 | +QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<QString> &names, |
| 3394 | + QVector<QString> &descriptions, QString& errCause ) |
3391 | 3395 | {
|
3392 |
| - QgsDataSourceURI dsUri( uri ); |
| 3396 | + QgsDataSourceURI dsUri( uri ); |
| 3397 | + QString styleTableName = QObject::tr( "layer_styles" ); |
| 3398 | + QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column; |
3393 | 3399 |
|
3394 |
| - QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
3395 |
| - if ( !conn ) |
3396 |
| - { |
3397 |
| - errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() ); |
3398 |
| - return -1; |
3399 |
| - } |
| 3400 | + QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
| 3401 | + if ( !conn ) |
| 3402 | + { |
| 3403 | + errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() ); |
| 3404 | + return -1; |
| 3405 | + } |
3400 | 3406 |
|
3401 |
| - // ORDER BY (CASE WHEN useAsDefault THEN 1 ELSE 2 END), update_time DESC;") |
3402 |
| - QString selectRelatedQuery = QString( "SELECT id,styleName,description" |
3403 |
| - " FROM layer_styles" |
3404 |
| - " WHERE f_table_catalog=%1" |
3405 |
| - " AND f_table_schema=%2" |
3406 |
| - " AND f_table_name=%3" |
3407 |
| - " AND f_geometry_column=%4" ) |
3408 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.database() ) ) |
3409 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.schema() ) ) |
3410 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.table() ) ) |
3411 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) ); |
| 3407 | + f_table_catalog = dsUri.database(); |
| 3408 | + f_table_schema = dsUri.schema(); |
| 3409 | + f_table_name = dsUri.table(); |
| 3410 | + f_geometry_column = dsUri.geometryColumn(); |
3412 | 3411 |
|
3413 |
| - QgsPostgresResult result = conn->PQexec( selectRelatedQuery ); |
3414 |
| - if ( result.PQresultStatus() != PGRES_TUPLES_OK ) |
3415 |
| - { |
3416 |
| - QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectRelatedQuery ) ); |
3417 |
| - errCause = QObject::tr( "Error executing the select query for related styles. The query was logged" ); |
3418 |
| - return -1; |
3419 |
| - } |
| 3412 | + // ORDER BY (CASE WHEN useAsDefault THEN 1 ELSE 2 END), update_time DESC;") |
| 3413 | + QString selectRelatedQuery = QObject::tr( "SELECT id, styleName, description FROM %1 WHERE f_table_catalog=%2 AND f_table_schema=%3 AND f_table_name=%4 AND f_geometry_column=%5;" ) |
| 3414 | + .arg( styleTableName ) |
| 3415 | + .arg( QgsPostgresConn::quotedValue( f_table_catalog ) ) |
| 3416 | + .arg( QgsPostgresConn::quotedValue( f_table_schema ) ) |
| 3417 | + .arg( QgsPostgresConn::quotedValue( f_table_name ) ) |
| 3418 | + .arg( QgsPostgresConn::quotedValue( f_geometry_column ) ); |
3420 | 3419 |
|
3421 |
| - int numberOfRelatedStyles = result.PQntuples(); |
3422 |
| - for ( int i = 0; i < numberOfRelatedStyles; i++ ) |
3423 |
| - { |
3424 |
| - ids.append( result.PQgetvalue( i, 0 ) ); |
3425 |
| - names.append( result.PQgetvalue( i, 1 ) ); |
3426 |
| - descriptions.append( result.PQgetvalue( i, 2 ) ); |
3427 |
| - } |
| 3420 | + PGresult* result = conn->PQexec( selectRelatedQuery ); |
| 3421 | + if ( PQresultStatus( result ) != PGRES_TUPLES_OK ) |
| 3422 | + { |
| 3423 | + QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectRelatedQuery ) ); |
| 3424 | + errCause = QObject::tr( "Error executing the select query for related styles. The query was logged" ); |
| 3425 | + return -1; |
| 3426 | + } |
| 3427 | + int numberOfRelatedStyles = PQntuples( result ); |
| 3428 | + for( int i=0; i<numberOfRelatedStyles; i++ ) |
| 3429 | + { |
| 3430 | + ids.append( QObject::tr( PQgetvalue( result, i, 0 ) ) ); |
| 3431 | + names.append( QObject::tr( PQgetvalue( result, i, 1 ) ) ); |
| 3432 | + descriptions.append( QObject::tr( PQgetvalue( result, i, 2 ) ) ); |
| 3433 | + } |
3428 | 3434 |
|
3429 |
| - QString selectOthersQuery = QString( "SELECT id,styleName,description" |
3430 |
| - " FROM layer_styles" |
3431 |
| - " WHERE NOT (f_table_catalog=%1 AND f_table_schema=%2 AND f_table_name=%3 AND f_geometry_column=%4)" |
3432 |
| - " ORDER BY update_time DESC" ) |
3433 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.database() ) ) |
3434 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.schema() ) ) |
3435 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.table() ) ) |
3436 |
| - .arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) ); |
| 3435 | + QString selectOthersQuery = QObject::tr( "SELECT id, styleName, description FROM %1 WHERE NOT(f_table_catalog=%2 AND f_table_schema=%3 AND f_table_name=%4 AND f_geometry_column=%5) ORDER BY update_time DESC;") |
| 3436 | + .arg( styleTableName ) |
| 3437 | + .arg( QgsPostgresConn::quotedValue( f_table_catalog ) ) |
| 3438 | + .arg( QgsPostgresConn::quotedValue( f_table_schema ) ) |
| 3439 | + .arg( QgsPostgresConn::quotedValue( f_table_name ) ) |
| 3440 | + .arg( QgsPostgresConn::quotedValue( f_geometry_column ) ); |
3437 | 3441 |
|
3438 |
| - result = conn->PQexec( selectOthersQuery ); |
3439 |
| - if ( result.PQresultStatus() != PGRES_TUPLES_OK ) |
3440 |
| - { |
3441 |
| - QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectOthersQuery ) ); |
3442 |
| - errCause = QObject::tr( "Error executing the select query for unrelated styles. The query was logged" ); |
3443 |
| - return -1; |
3444 |
| - } |
3445 |
| - for ( int i = 0; i < result.PQntuples(); i++ ) |
3446 |
| - { |
3447 |
| - ids.append( result.PQgetvalue( i, 0 ) ); |
3448 |
| - names.append( result.PQgetvalue( i, 1 ) ); |
3449 |
| - descriptions.append( result.PQgetvalue( i, 2 ) ); |
3450 |
| - } |
| 3442 | + result = conn->PQexec( selectOthersQuery ); |
| 3443 | + if ( PQresultStatus( result ) != PGRES_TUPLES_OK ) |
| 3444 | + { |
| 3445 | + QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectOthersQuery ) ); |
| 3446 | + errCause = QObject::tr( "Error executing the select query for unrelated styles. The query was logged" ); |
| 3447 | + return -1; |
| 3448 | + } |
| 3449 | + for( int i=0; i<PQntuples( result ); i++ ) |
| 3450 | + { |
| 3451 | + ids.append( QObject::tr( PQgetvalue( result, i, 0 ) ) ); |
| 3452 | + names.append( QObject::tr( PQgetvalue( result, i, 1 ) ) ); |
| 3453 | + descriptions.append( QObject::tr( PQgetvalue( result, i, 2 ) ) ); |
| 3454 | + } |
3451 | 3455 |
|
3452 |
| - return numberOfRelatedStyles; |
| 3456 | + return numberOfRelatedStyles; |
3453 | 3457 | }
|
3454 | 3458 |
|
3455 |
| -QGISEXTERN QString getStyleById( const QString& uri, QString styleId, QString& errCause ) |
| 3459 | +QGISEXTERN QString getStyleById(const QString& uri, QString styleId, QString& errCause ) |
3456 | 3460 | {
|
3457 |
| - QgsDataSourceURI dsUri( uri ); |
| 3461 | + QgsDataSourceURI dsUri( uri ); |
| 3462 | + QString styleTableName = QObject::tr( "layer_styles" ); |
3458 | 3463 |
|
3459 |
| - QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
3460 |
| - if ( !conn ) |
3461 |
| - { |
3462 |
| - errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() ); |
3463 |
| - return QObject::tr( "" ); |
3464 |
| - } |
| 3464 | + QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false ); |
| 3465 | + if ( !conn ) |
| 3466 | + { |
| 3467 | + errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() ); |
| 3468 | + return QObject::tr( "" ); |
| 3469 | + } |
3465 | 3470 |
|
3466 |
| - QString selectQmlQuery = QString( "SELECT styleQml FROM layer_styles WHERE id=%1" ).arg( QgsPostgresConn::quotedValue( styleId ) ); |
3467 |
| - QgsPostgresResult result = conn->PQexec( selectQmlQuery ); |
3468 |
| - if ( result.PQresultStatus() != PGRES_TUPLES_OK ) |
3469 |
| - { |
3470 |
| - QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectQmlQuery ) ); |
3471 |
| - errCause = QObject::tr( "Error executing the select query. The query was logged" ); |
3472 |
| - return ""; |
3473 |
| - } |
| 3471 | + QString selectQmlQuery = QObject::tr( "SELECT styleQml FROM %1 WHERE id=%2") |
| 3472 | + .arg( styleTableName ) |
| 3473 | + .arg( styleId ); |
| 3474 | + PGresult* result = conn->PQexec( selectQmlQuery ); |
| 3475 | + if ( PQresultStatus( result ) != PGRES_TUPLES_OK ) |
| 3476 | + { |
| 3477 | + QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectQmlQuery ) ); |
| 3478 | + errCause = QObject::tr( "Error executing the select query. The query was logged" ); |
| 3479 | + return QObject::tr( "" ); |
| 3480 | + } |
| 3481 | + if( PQntuples( result ) == 1) |
| 3482 | + { |
| 3483 | + return PQgetvalue( result, 0, 0 ); |
| 3484 | + } |
| 3485 | + else |
| 3486 | + { |
| 3487 | + errCause = QObject::tr( "Consistence error in table '%1'. Style id should be unique" ).arg( styleTableName ); |
| 3488 | + return QObject::tr( "" ); |
| 3489 | + } |
3474 | 3490 |
|
3475 |
| - if ( result.PQntuples() == 1 ) |
3476 |
| - { |
3477 |
| - return result.PQgetvalue( 0, 0 ); |
3478 |
| - } |
3479 |
| - else |
3480 |
| - { |
3481 |
| - errCause = QObject::tr( "Consistency error in table '%1'. Style id should be unique" ).arg( "layer_styles" ); |
3482 |
| - return ""; |
3483 |
| - } |
3484 | 3491 | }
|
0 commit comments