cleanup style to db:

- add sip bindings to new methods
- table name, sql queries and constants should not be translated
- replace QVector<QString> with QStringList
This commit is contained in:
Juergen E. Fischer 2013-05-03 00:23:23 +02:00
parent d4542feedf
commit ea1aaeae01
9 changed files with 193 additions and 165 deletions

View File

@ -269,6 +269,12 @@ class QgsVectorDataProvider : QgsDataProvider
QStringList errors();
/**
* It returns false by default.
* Must be implemented by providers that support saving and loading styles to db returning true
*/
virtual bool isSaveAndLoadStyleToDBSupported();
protected:
QVariant convertValue( QVariant::Type type, QString value );

View File

@ -304,6 +304,37 @@ class QgsVectorLayer : QgsMapLayer
*/
virtual bool writeXml( QDomNode & layer_node, QDomDocument & doc );
/**
* Save named and sld style of the layer to the style table in the db.
* @param name
* @param description
* @param useAsDefault
* @param uiFileContent
* @param msgError
*/
virtual void saveStyleToDatabase( QString name, QString description,
bool useAsDefault, QString uiFileContent,
QString &msgError );
/**
* Lists all the style in db split into related to the layer and not related to
* @param ids the QVector in which will be stored the style db ids
* @param names the QVector in which will be stored the style names
* @param descriptions the QVector in which will be stored the style descriptions
* @param msgError
* @return the number of styles related to current layer
*/
virtual int listStylesInDatabase( QStringList &ids, QStringList &names,
QStringList &descriptions, QString &msgError );
/**
* Will return the named style corresponding to style id provided
*/
virtual QString getStyleFromDatabase( QString styleId, QString &msgError );
virtual QString loadNamedStyle( const QString theURI, bool &theResultFlag, bool loadFromLocalDb = false );
virtual bool applyNamedStyle( QString namedStyle , QString errorMsg );
/** convert a saved attribute editor element into a AttributeEditor structure as it's used internally.
* @param elem the DOM element
* @param parent the QObject which will own this object

View File

@ -46,8 +46,7 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
}
void QgsLoadStyleFromDBDialog::initializeLists( QVector<QString> ids, QVector<QString> names,
QVector<QString> descriptions, int sectionLimit )
void QgsLoadStyleFromDBDialog::initializeLists( QStringList ids, QStringList names, QStringList descriptions, int sectionLimit )
{
mIds = ids;
mNames = names;

View File

@ -10,23 +10,21 @@
#ifndef QGSLOADFILEFROMDBDIALOG_H
#define QGSLOADFILEFROMDBDIALOG_H
#include <QVector>
#include "ui_qgsloadstylefromdbdialog.h"
#include "qgisgui.h"
#include "qgsfield.h"
class QgsLoadStyleFromDBDialog: public QDialog, private Ui::QgsLoadStyleFromDBDialogLayout
class QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadStyleFromDBDialogLayout
{
QString mSelectedStyleId;
int mSectionLimit;
QVector<QString> mIds, mNames, mDescriptions;
QStringList mIds, mNames, mDescriptions;
QString qmlStyle;
Q_OBJECT
public:
explicit QgsLoadStyleFromDBDialog( QWidget *parent = 0 );
void initializeLists( QVector<QString> ids, QVector<QString> names, QVector<QString> descriptions, int sectionLimit );
void initializeLists( QStringList ids, QStringList names, QStringList descriptions, int sectionLimit );
QString getSelectedStyleId();
public slots:

View File

@ -146,7 +146,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
if ( layer->dataProvider()->isSaveAndLoadStyleToDBSupported() )
{
//for loading
mLoadStyleMenu = new QMenu();
mLoadStyleMenu = new QMenu();
mLoadStyleMenu->addAction( tr( "Load from file" ) );
mLoadStyleMenu->addAction( tr( "Load from database" ) );
pbnLoadStyle->setContextMenuPolicy( Qt::PreventContextMenu );
@ -297,7 +297,7 @@ void QgsVectorLayerProperties::insertExpression()
dlg.setWindowTitle( tr( "Insert expression" ) );
if ( dlg.exec() == QDialog::Accepted )
{
QString expression = dlg.expressionBuilder()->expressionText();
QString expression = dlg.expressionBuilder()->expressionText();
//Only add the expression if the user has entered some text.
if ( !expression.isEmpty() )
{
@ -754,7 +754,7 @@ void QgsVectorLayerProperties::saveStyleAs( StyleType styleType )
}
else
{
format = tr( "QGIS Layer Style File" ) + " (*.qml)";
format = tr( "QGIS Layer Style File" ) + " (*.qml)";
extension = ".qml";
}
@ -826,7 +826,7 @@ void QgsVectorLayerProperties::loadStyleMenuTriggered( QAction *action )
void QgsVectorLayerProperties::showListOfStylesFromDatabase()
{
QString errorMsg;
QVector<QString> ids, names, descriptions;
QStringList ids, names, descriptions;
//get the list of styles in the db
int sectionLimit = layer->listStylesInDatabase( ids, names, descriptions, errorMsg );

View File

@ -335,14 +335,6 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
*/
virtual bool isSaveAndLoadStyleToDBSupported() { return false; }
signals:
/** Is emitted, when editing has started */
void editingStarted();
/** Is emitted, when editing stopped */
void editingStopped();
protected:
QVariant convertValue( QVariant::Type type, QString value );

View File

@ -96,9 +96,9 @@ typedef QString loadStyle_t(
typedef int listStyles_t(
const QString& uri,
QVector<QString> &ids,
QVector<QString> &names,
QVector<QString> &descriptions,
QStringList &ids,
QStringList &names,
QStringList &descriptions,
QString& errCause
);
@ -3730,7 +3730,7 @@ QDomElement QgsAttributeEditorField::toDomElement( QDomDocument& doc ) const
return elem;
}
int QgsVectorLayer::listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names, QVector<QString> &descriptions, QString &msgError )
int QgsVectorLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );

View File

@ -22,7 +22,6 @@
#include <QSet>
#include <QList>
#include <QStringList>
#include <QVector>
#include "qgis.h"
#include "qgsmaplayer.h"
@ -713,8 +712,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
* @param msgError
* @return the number of styles related to current layer
*/
virtual int listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names,
QVector<QString> &descriptions, QString &msgError );
virtual int listStylesInDatabase( QStringList &ids, QStringList &names,
QStringList &descriptions, QString &msgError );
/**
* Will return the named style corresponding to style id provided

View File

@ -3169,7 +3169,7 @@ QGISEXTERN bool deleteLayer( const QString& uri, QString& errCause )
}
schemaTableName += QgsPostgresConn::quotedIdentifier( tableName );
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
@ -3231,26 +3231,32 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
const QString& uiFileContent, bool useAsDefault, QString& errCause )
{
QgsDataSourceURI dsUri( uri );
QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column, owner, isdef, name, desc;
QString styleTableName = QObject::tr( "layer_styles" );
QgsPostgresResult res;
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
return false;
}
QString checkExitingTableQuery = QObject::tr( "SELECT COUNT(*) FROM information_schema.tables WHERE table_name='%1'" ).arg( styleTableName );
PGresult* result = conn->PQexec( checkExitingTableQuery );
char* c = PQgetvalue( result, 0, 0 );
if ( *c == '0' )
QgsPostgresResult res = conn->PQexec( "SELECT COUNT(*) FROM information_schema.tables WHERE table_name='layer_styles'" );
if ( res.PQgetvalue( 0, 0 ).toInt() == 0 )
{
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 );
res = conn->PQexec( createTabeQuery );
res = conn->PQexec( "CREATE TABLE layer_styles("
"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"
",ownervarchar(30)"
",ui xml"
",update_time timestamp DEFAULT CURRENT_TIMESTAMP"
")" );
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
{
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() );
@ -3259,88 +3265,94 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
}
}
f_table_catalog = dsUri.database();
f_table_schema = dsUri.schema();
f_table_name = dsUri.table();
f_geometry_column = dsUri.geometryColumn();
owner = dsUri.username();
isdef = ( useAsDefault ) ? QObject::tr( "true" ) : QObject::tr( "false" );
name = ( styleName.isEmpty() ) ? dsUri.table() : styleName;
desc = ( styleDescription.isEmpty() ) ? QDateTime::currentDateTime().toString() : styleDescription;
QString uiFileColumn( "" );
QString uiFileValue( "" );
QString uiFileColumn;
QString uiFileValue;
if ( !uiFileContent.isEmpty() )
{
uiFileColumn.append( QObject::tr( ", ui" ) );
uiFileValue.append( QObject::tr( ",XMLPARSE(DOCUMENT %1)" ).arg( QgsPostgresConn::quotedValue( uiFileContent ) ) );
uiFileColumn = ",ui";
uiFileValue = QString( ",XMLPARSE(DOCUMENT %1)" ).arg( QgsPostgresConn::quotedValue( uiFileContent ) );
}
QString sql = QObject::tr( "INSERT INTO %1 ( f_table_catalog, "
"f_table_schema, f_table_name, f_geometry_column, "
"styleName, styleQML, styleSLD, useAsDefault, "
"description, owner %12) "
"VALUES(%2,%3,%4,%5,%6,XMLPARSE(DOCUMENT %7),"
"XMLPARSE(DOCUMENT %8),%9,%10,%11 %13);" )
.arg( styleTableName )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) )
.arg( QgsPostgresConn::quotedValue( name ) )
QString sql = QString( "INSERT INTO layer_styles("
"f_table_catalog,f_table_schema,f_table_name,f_geometry_column,styleName,styleQML,styleSLD,useAsDefault,description,owner%11"
") VALUES("
"%1,%2,%3,%4,%5,XMLPARSE( DOCUMENT %6),XMLPARSE(DOCUMENT %7),%8,%9,%10%12)" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) )
.arg( QgsPostgresConn::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) )
.arg( QgsPostgresConn::quotedValue( qmlStyle ) )
.arg( QgsPostgresConn::quotedValue( sldStyle ) )
.arg( isdef )
.arg( QgsPostgresConn::quotedValue( desc ) )
.arg( QgsPostgresConn::quotedValue( owner ) )
.arg( useAsDefault ? "true" : "false" )
.arg( QgsPostgresConn::quotedValue( styleDescription.isEmpty() ? QDateTime::currentDateTime().toString() : styleDescription ) )
.arg( QgsPostgresConn::quotedValue( dsUri.username() ) )
.arg( uiFileColumn )
.arg( uiFileValue );
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" )
.arg( styleTableName )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) )
.arg( QgsPostgresConn::quotedValue( name ) );
QString checkQuery = QString( "SELECT styleName"
" FROM layer_styles"
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4"
" AND styleName=%5" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.username() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) )
.arg( QgsPostgresConn::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) );
result = conn->PQexec( checkQuery );
if ( PQntuples( result ) > 0 )
res = conn->PQexec( checkQuery );
if ( res.PQntuples() > 0 )
{
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;" )
.arg( styleTableName )
.arg( isdef )
.arg( QgsPostgresConn::quotedValue( qmlStyle
) )
sql = QString( "UPDATE layer_styles"
" SET useAsDefault=%1"
",styleQML=XMLPARSE(DOCUMENT %2)"
",styleSLD=XMLPARSE(DOCUMENT %3)"
",description=%4"
",owner=%5"
" WHERE f_table_catalog=%6"
" AND f_table_schema=%7"
" AND f_table_name=%8"
" AND f_geometry_column=%9"
" AND styleName=%10" )
.arg( useAsDefault ? "true" : "false" )
.arg( QgsPostgresConn::quotedValue( qmlStyle ) )
.arg( QgsPostgresConn::quotedValue( sldStyle ) )
.arg( QgsPostgresConn::quotedValue( desc ) )
.arg( QgsPostgresConn::quotedValue( owner ) )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) )
.arg( QgsPostgresConn::quotedValue( name ) );
.arg( QgsPostgresConn::quotedValue( styleDescription.isEmpty() ? QDateTime::currentDateTime().toString() : styleDescription ) )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.username() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) )
.arg( QgsPostgresConn::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) );
}
if ( useAsDefault )
{
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;" )
.arg( styleTableName )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) );
sql = QObject::tr( "BEGIN; %1 %2 COMMIT;" )
.arg( removeDefaultSql ).arg( sql );
QString removeDefaultSql = QString( "UPDATE layer_styles"
" SET useAsDefault=false"
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.username() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) );
sql = QString( "BEGIN; %1; %2; COMMIT;" ).arg( removeDefaultSql ).arg( sql );
}
res = conn->PQexec( sql );
conn->disconnect();
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
{
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 );
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() );
return false;
}
return true;
}
@ -3348,99 +3360,93 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
QGISEXTERN QString loadStyle( const QString& uri, QString& errCause )
{
QgsDataSourceURI dsUri( uri );
QString styleTableName = QObject::tr( "layer_styles" );
QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column;
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
return QObject::tr( "" );
return "";
}
f_table_catalog = dsUri.database();
f_table_schema = dsUri.schema();
f_table_name = dsUri.table();
f_geometry_column = dsUri.geometryColumn();
QString selectQmlQuery = QString( "SELECT styleQML"
" FROM layer_styles"
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4"
" ORDER BY CASE WHEN useAsDefault THEN 1 ELSE 2 END"
",update_time DESC LIMIT 1" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) );
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;" )
.arg( styleTableName )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) );
QgsPostgresResult result = conn->PQexec( selectQmlQuery );
PGresult* result = conn->PQexec( selectQmlQuery );
if ( PQntuples( result ) == 1 )
{
char* c = PQgetvalue( result, 0, 0 );
return QObject::tr( c );;
}
return QObject::tr( "" );
return result.PQntuples() == 1 ? result.PQgetvalue( 0, 0 ) : "";
}
QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<QString> &names,
QVector<QString> &descriptions, QString& errCause )
QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &names,
QStringList &descriptions, QString& errCause )
{
QgsDataSourceURI dsUri( uri );
QString styleTableName = QObject::tr( "layer_styles" );
QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column;
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() );
return -1;
}
f_table_catalog = dsUri.database();
f_table_schema = dsUri.schema();
f_table_name = dsUri.table();
f_geometry_column = dsUri.geometryColumn();
// ORDER BY (CASE WHEN useAsDefault THEN 1 ELSE 2 END), update_time DESC;")
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;" )
.arg( styleTableName )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) );
QString selectRelatedQuery = QString( "SELECT id,styleName,description"
" FROM layer_styles"
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) );
PGresult* result = conn->PQexec( selectRelatedQuery );
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
QgsPostgresResult result = conn->PQexec( selectRelatedQuery );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectRelatedQuery ) );
errCause = QObject::tr( "Error executing the select query for related styles. The query was logged" );
return -1;
}
int numberOfRelatedStyles = PQntuples( result );
int numberOfRelatedStyles = result.PQntuples();
for ( int i = 0; i < numberOfRelatedStyles; i++ )
{
ids.append( QObject::tr( PQgetvalue( result, i, 0 ) ) );
names.append( QObject::tr( PQgetvalue( result, i, 1 ) ) );
descriptions.append( QObject::tr( PQgetvalue( result, i, 2 ) ) );
ids.append( result.PQgetvalue( i, 0 ) );
names.append( result.PQgetvalue( i, 1 ) );
descriptions.append( result.PQgetvalue( i, 2 ) );
}
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;" )
.arg( styleTableName )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) );
QString selectOthersQuery = QString( "SELECT id,styleName,description"
" FROM layer_styles"
" WHERE NOT (f_table_catalog=%1 AND f_table_schema=%2 AND f_table_name=%3 AND f_geometry_column=%4)"
" ORDER BY update_time DESC" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.geometryColumn() ) );
result = conn->PQexec( selectOthersQuery );
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectOthersQuery ) );
errCause = QObject::tr( "Error executing the select query for unrelated styles. The query was logged" );
return -1;
}
for ( int i = 0; i < PQntuples( result ); i++ )
for ( int i = 0; i < result.PQntuples(); i++ )
{
ids.append( QObject::tr( PQgetvalue( result, i, 0 ) ) );
names.append( QObject::tr( PQgetvalue( result, i, 1 ) ) );
descriptions.append( QObject::tr( PQgetvalue( result, i, 2 ) ) );
ids.append( result.PQgetvalue( i, 0 ) );
names.append( result.PQgetvalue( i, 1 ) );
descriptions.append( result.PQgetvalue( i, 2 ) );
}
return numberOfRelatedStyles;
@ -3449,33 +3455,30 @@ QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<Q
QGISEXTERN QString getStyleById( const QString& uri, QString styleId, QString& errCause )
{
QgsDataSourceURI dsUri( uri );
QString styleTableName = QObject::tr( "layer_styles" );
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() );
return QObject::tr( "" );
}
QString selectQmlQuery = QObject::tr( "SELECT styleQml FROM %1 WHERE id=%2" )
.arg( styleTableName )
.arg( styleId );
PGresult* result = conn->PQexec( selectQmlQuery );
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
QString selectQmlQuery = QString( "SELECT styleQml FROM layer_styles WHERE id=%1" ).arg( QgsPostgresConn::quotedValue( styleId ) );
QgsPostgresResult result = conn->PQexec( selectQmlQuery );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectQmlQuery ) );
errCause = QObject::tr( "Error executing the select query. The query was logged" );
return QObject::tr( "" );
return "";
}
if ( PQntuples( result ) == 1 )
if ( result.PQntuples() == 1 )
{
return PQgetvalue( result, 0, 0 );
return result.PQgetvalue( 0, 0 );
}
else
{
errCause = QObject::tr( "Consistence error in table '%1'. Style id should be unique" ).arg( styleTableName );
return QObject::tr( "" );
errCause = QObject::tr( "Consistency error in table '%1'. Style id should be unique" ).arg( "layer_styles" );
return "";
}
}