Skip to content

Commit

Permalink
added generic mode
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Apr 17, 2012
1 parent 24924a9 commit d7e8ae1
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 40 deletions.
145 changes: 105 additions & 40 deletions src/core/qgsdatasourceuri.cpp
Expand Up @@ -21,6 +21,7 @@

#include <QStringList>
#include <QRegExp>
#include <QUrl>

QgsDataSourceURI::QgsDataSourceURI()
: mSSLmode( SSLprefer )
Expand All @@ -32,6 +33,11 @@ QgsDataSourceURI::QgsDataSourceURI()
// do nothing
}

QgsDataSourceURI::QgsDataSourceURI( const QgsDataSourceURI& dataSourceURI )
{
mParams = dataSourceURI.mParams;
}

QgsDataSourceURI::QgsDataSourceURI( QString uri )
: mSSLmode( SSLprefer )
, mKeyColumn( "" )
Expand Down Expand Up @@ -530,47 +536,47 @@ QString QgsDataSourceURI::uri() const
{
theUri += " type=";

switch( mWkbType )
switch ( mWkbType )
{
case QGis::WKBPoint:
theUri += "POINT";
break;
case QGis::WKBLineString:
theUri += "LINESTRING";
break;
case QGis::WKBPolygon:
theUri += "POLYGON";
break;
case QGis::WKBMultiPoint:
theUri += "MULTIPOINT";
break;
case QGis::WKBMultiLineString:
theUri += "MULTILINESTRING";
break;
case QGis::WKBMultiPolygon:
theUri += "MULTIPOLYGON";
break;
case QGis::WKBPoint25D:
theUri += "POINTM";
break;
case QGis::WKBLineString25D:
theUri += "LINESTRINGM";
break;
case QGis::WKBPolygon25D:
theUri += "POLYGONM";
break;
case QGis::WKBMultiPoint25D:
theUri += "MULTIPOINTM";
break;
case QGis::WKBMultiLineString25D:
theUri += "MULTILINESTRINGM";
break;
case QGis::WKBMultiPolygon25D:
theUri += "MULTIPOLYGONM";
break;
case QGis::WKBUnknown:
case QGis::WKBNoGeometry:
break;
case QGis::WKBPoint:
theUri += "POINT";
break;
case QGis::WKBLineString:
theUri += "LINESTRING";
break;
case QGis::WKBPolygon:
theUri += "POLYGON";
break;
case QGis::WKBMultiPoint:
theUri += "MULTIPOINT";
break;
case QGis::WKBMultiLineString:
theUri += "MULTILINESTRING";
break;
case QGis::WKBMultiPolygon:
theUri += "MULTIPOLYGON";
break;
case QGis::WKBPoint25D:
theUri += "POINTM";
break;
case QGis::WKBLineString25D:
theUri += "LINESTRINGM";
break;
case QGis::WKBPolygon25D:
theUri += "POLYGONM";
break;
case QGis::WKBMultiPoint25D:
theUri += "MULTIPOINTM";
break;
case QGis::WKBMultiLineString25D:
theUri += "MULTILINESTRINGM";
break;
case QGis::WKBMultiPolygon25D:
theUri += "MULTIPOLYGONM";
break;
case QGis::WKBUnknown:
case QGis::WKBNoGeometry:
break;
}
}

Expand All @@ -587,6 +593,36 @@ QString QgsDataSourceURI::uri() const
return theUri;
}

QByteArray QgsDataSourceURI::encodedUri() const
{
QUrl url;
foreach( QString key, mParams.uniqueKeys() )
{
foreach( QString value, mParams.values( key ) )
{
url.addQueryItem( key, value );
}
}
return url.encodedQuery();
}

void QgsDataSourceURI::setEncodedUri( const QByteArray & uri )
{
mParams.clear();
QUrl url;
url.setEncodedQuery( uri );
QPair<QString, QString> item;
foreach( item, url.queryItems() )
{
mParams.insertMulti( item.first, item.second );
}
}

void QgsDataSourceURI::setEncodedUri( const QString & uri )
{
setEncodedUri( uri.toAscii() );
}

QString QgsDataSourceURI::quotedTablename() const
{
if ( !mSchema.isEmpty() )
Expand Down Expand Up @@ -663,3 +699,32 @@ void QgsDataSourceURI::setSrid( QString srid )
{
mSrid = srid;
}

void QgsDataSourceURI::setParam( const QString &key, const QString &value )
{
// may be multiple
mParams.insertMulti( key, value );
}

void QgsDataSourceURI::setParam( const QString &key, const QStringList &value )
{
foreach( QString val, value )
{
mParams.insertMulti( key, val );
}
}

QString QgsDataSourceURI::param( const QString &key ) const
{
return mParams.value( key );
}

QStringList QgsDataSourceURI::params( const QString &key ) const
{
return mParams.values( key );
}

bool QgsDataSourceURI::hasParam( const QString &key ) const
{
return mParams.contains( key );
}
45 changes: 45 additions & 0 deletions src/core/qgsdatasourceuri.h
Expand Up @@ -21,10 +21,16 @@

#include "qgis.h"

#include <QMap>

/** \ingroup core
* Class for storing the component parts of a PostgreSQL/RDBMS datasource URI.
* This structure stores the database connection information, including host, database,
* user name, password, schema, password, and sql where clause
*
* Extended to support generic params so that it may be used by any provider.
* The 2 modes (the old - RDMS specific and the new generic) may not yet be mixed.
* (Radim Blazek 4/2012)
*/
class CORE_EXPORT QgsDataSourceURI
{
Expand All @@ -35,18 +41,55 @@ class CORE_EXPORT QgsDataSourceURI
//! default constructor
QgsDataSourceURI();

//! copy constructor (generic mode)
QgsDataSourceURI( const QgsDataSourceURI& dataSourceURI );

//! constructor which parses input URI
QgsDataSourceURI( QString uri );

//! constructor which parses input encoded URI (generic mode)
// \note added in 1.9
QgsDataSourceURI( const QByteArray & uri );

//! return connection part of URI
QString connectionInfo() const;

//! return complete uri
QString uri() const;

//! return complete encoded uri (generic mode)
// \note added in 1.9
QByteArray encodedUri() const;

//! set complete encoded uri (generic mode)
// \note added in 1.9
void setEncodedUri( const QByteArray & uri );

//! set complete encoded uri (generic mode)
// \note added in 1.9
void setEncodedUri( const QString & uri );

//! quoted table name
QString quotedTablename() const;

//! Set generic param (generic mode)
// \note if key exists, another is inserted
// \note added in 1.9
void setParam( const QString &key, const QString &value );
void setParam( const QString &key, const QStringList &value );

//! Get generic param (generic mode)
// \note added in 1.9
QString param( const QString &key ) const;

//! Get multiple generic param (generic mode)
// \note added in 1.9
QStringList params( const QString &key ) const;

//! Test if param exists (generic mode)
// \note added in 1.9
bool hasParam( const QString &key ) const;

//! Set all connection related members at once
//! \note This optional sslmode parameter has been added in version 1.1
void setConnection( const QString& aHost,
Expand Down Expand Up @@ -163,6 +206,8 @@ class CORE_EXPORT QgsDataSourceURI
QGis::WkbType mWkbType;
//! SRID or a null string if not specified
QString mSrid;
//! Generic params store
QMap<QString, QString> mParams;
};

#endif //QGSDATASOURCEURI_H
Expand Down

0 comments on commit d7e8ae1

Please sign in to comment.