22 #include <QStringList> 27 : mSSLmode( SSLprefer )
29 , mUseEstimatedMetadata( false )
30 , mSelectAtIdDisabled( false )
31 , mWkbType(
QGis::WKBUnknown )
39 , mUseEstimatedMetadata( false )
40 , mSelectAtIdDisabled( false )
41 , mWkbType(
QGis::WKBUnknown )
44 while ( i < uri.length() )
57 while ( i < uri.length() && uri[i] !=
'=' && !uri[i].isSpace() )
60 QString pname = uri.mid( start, i - start );
64 if ( i == uri.length() || uri[i] !=
'=' )
81 QString pval = getValue( uri, i );
83 if ( pname ==
"table" )
90 mTable = getValue( uri, i );
104 while ( i < uri.length() && uri[i] !=
')' )
106 if ( uri[i] ==
'\\' )
111 if ( i == uri.length() )
116 mGeometryColumn = uri.mid( start, i - start );
117 mGeometryColumn.replace(
"\\)",
")" );
118 mGeometryColumn.replace(
"\\\\",
"\\" );
124 mGeometryColumn = QString::null;
127 else if ( pname ==
"key" )
131 else if ( pname ==
"estimatedmetadata" )
133 mUseEstimatedMetadata = pval ==
"true";
135 else if ( pname ==
"srid" )
139 else if ( pname ==
"type" )
141 QString geomTypeUpper = pval.toUpper();
142 if ( geomTypeUpper ==
"POINT" )
146 else if ( geomTypeUpper ==
"LINESTRING" || geomTypeUpper ==
"LINE" )
150 else if ( geomTypeUpper ==
"POLYGON" )
154 else if ( geomTypeUpper ==
"MULTIPOINT" )
158 else if ( geomTypeUpper ==
"MULTILINESTRING" )
162 else if ( geomTypeUpper ==
"MULTIPOLYGON" )
171 else if ( pname ==
"selectatid" )
173 mSelectAtIdDisabled = pval ==
"false";
175 else if ( pname ==
"service" )
179 else if ( pname ==
"user" )
183 else if ( pname ==
"password" )
187 else if ( pname ==
"connect_timeout" )
191 else if ( pname ==
"dbname" )
195 else if ( pname ==
"host" )
199 else if ( pname ==
"hostaddr" )
203 else if ( pname ==
"port" )
207 else if ( pname ==
"tty" )
211 else if ( pname ==
"options" )
215 else if ( pname ==
"sslmode" )
217 if ( pval ==
"disable" )
219 else if ( pval ==
"allow" )
221 else if ( pval ==
"prefer" )
223 else if ( pval ==
"require" )
226 else if ( pname ==
"requiressl" )
233 else if ( pname ==
"krbsrvname" )
237 else if ( pname ==
"gsslib" )
243 QgsDebugMsg(
"parameter \"" + pname +
"\":\"" + pval +
"\" added" );
253 regexp.setMinimal(
true );
254 QString safeName( aUri );
255 if ( aUri.contains(
" password=" ) )
257 regexp.setPattern(
" password=.* " );
258 safeName.replace( regexp,
" " );
260 else if ( aUri.contains(
",password=" ) )
262 regexp.setPattern(
",password=.*," );
263 safeName.replace( regexp,
"," );
265 else if ( aUri.contains(
"IDB:" ) )
267 regexp.setPattern(
" pass=.* " );
268 safeName.replace( regexp,
" " );
270 else if (( aUri.contains(
"OCI:" ) )
271 || ( aUri.contains(
"ODBC:" ) ) )
273 regexp.setPattern(
"/.*@" );
274 safeName.replace( regexp,
"/@" );
276 else if ( aUri.contains(
"SDE:" ) )
278 QStringList strlist = aUri.split(
"," );
279 safeName = strlist[0] +
"," + strlist[1] +
"," + strlist[2] +
"," + strlist[3];
346 return mGeometryColumn;
362 mUseEstimatedMetadata = theFlag;
367 return mUseEstimatedMetadata;
372 mSelectAtIdDisabled = theFlag;
377 return mSelectAtIdDisabled;
390 QString QgsDataSourceURI::escape(
const QString &theVal, QChar delim =
'\'' )
const 392 QString val = theVal;
394 val.replace(
"\\",
"\\\\" );
395 val.replace( delim, QString(
"\\%1" ).arg( delim ) );
400 void QgsDataSourceURI::skipBlanks(
const QString &
uri,
int &i )
403 while ( i <
uri.length() &&
uri[i].isSpace() )
407 QString QgsDataSourceURI::getValue(
const QString &
uri,
int &i )
409 skipBlanks(
uri, i );
413 if ( i <
uri.length() && (
uri[i] ==
'\'' ||
uri[i] ==
'"' ) )
415 QChar delim =
uri[i];
422 if ( i ==
uri.length() )
424 QgsDebugMsg(
"unterminated quoted string in connection info string" );
428 if (
uri[i] ==
'\\' )
431 if ( i ==
uri.length() )
433 if (
uri[i] != delim &&
uri[i] !=
'\\' )
436 else if (
uri[i] == delim )
448 while ( i <
uri.length() )
450 if (
uri[i].isSpace() )
456 if (
uri[i] ==
'\\' )
459 if ( i ==
uri.length() )
461 if (
uri[i] !=
'\\' &&
uri[i] !=
'\'' )
469 skipBlanks(
uri, i );
476 QStringList connectionItems;
478 if ( mDatabase !=
"" )
480 connectionItems <<
"dbname='" + escape( mDatabase ) +
"'";
483 if ( mService !=
"" )
485 connectionItems <<
"service='" + escape( mService ) +
"'";
487 else if ( mHost !=
"" )
489 connectionItems <<
"host=" + mHost;
492 if ( mService.isEmpty() )
495 connectionItems <<
"port=" + mPort;
498 if ( mUsername !=
"" )
500 connectionItems <<
"user='" + escape( mUsername ) +
"'";
502 if ( mPassword !=
"" )
504 connectionItems <<
"password='" + escape( mPassword ) +
"'";
509 connectionItems <<
"sslmode=disable";
511 connectionItems <<
"sslmode=allow";
513 connectionItems <<
"sslmode=require";
516 connectionItems <<
"sslmode=prefer";
519 return connectionItems.join(
" " );
526 if ( !mKeyColumn.isEmpty() )
528 theUri += QString(
" key='%1'" ).arg( escape( mKeyColumn ) );
531 if ( mUseEstimatedMetadata )
533 theUri += QString(
" estimatedmetadata=true" );
536 if ( !mSrid.isEmpty() )
538 theUri += QString(
" srid=%1" ).arg( mSrid );
551 theUri +=
"LINESTRING";
557 theUri +=
"MULTIPOINT";
560 theUri +=
"MULTILINESTRING";
563 theUri +=
"MULTIPOLYGON";
569 theUri +=
"LINESTRINGM";
572 theUri +=
"POLYGONM";
575 theUri +=
"MULTIPOINTM";
578 theUri +=
"MULTILINESTRINGM";
581 theUri +=
"MULTIPOLYGONM";
589 if ( mSelectAtIdDisabled )
591 theUri += QString(
" selectatid=false" );
594 for ( QMap<QString, QString>::const_iterator it = mParams.begin(); it != mParams.end(); ++it )
596 if ( it.key().contains(
"=" ) || it.key().contains(
" " ) )
598 QgsDebugMsg( QString(
"invalid uri parameter %1 skipped" ).arg( it.key() ) );
602 theUri +=
" " + it.key() +
"='" + escape( it.value() ) +
"'";
605 QString columnName( mGeometryColumn );
606 columnName.replace(
"\\",
"\\\\" );
607 columnName.replace(
")",
"\\)" );
609 theUri += QString(
" table=%1%2 sql=%3" )
611 .arg( mGeometryColumn.isNull() ? QString() : QString(
" (%1)" ).arg( columnName ) )
620 foreach ( QString key, mParams.uniqueKeys() )
622 foreach ( QString value, mParams.values( key ) )
624 url.addQueryItem( key, value );
627 return url.encodedQuery();
634 url.setEncodedQuery( uri );
635 QPair<QString, QString> item;
636 foreach ( item, url.queryItems() )
638 mParams.insertMulti( item.first, item.second );
649 if ( !mSchema.isEmpty() )
650 return QString(
"\"%1\".\"%2\"" )
651 .arg( escape( mSchema,
'"' ) )
652 .arg( escape( mTable,
'"' ) );
654 return QString(
"\"%1\"" )
655 .arg( escape( mTable,
'"' ) );
687 const QString &
table,
727 mParams.insertMulti( key, value );
732 foreach ( QString val, value )
734 mParams.insertMulti( key, val );
740 return mParams.remove( key );
745 return mParams.value( key );
750 return mParams.values( key );
755 return mParams.contains( key );
QgsDataSourceURI()
default constructor
int removeParam(const QString &key)
Remove generic param (generic mode)
QString quotedTablename() const
quoted table name
QString geometryColumn() const
void setEncodedUri(const QByteArray &uri)
set complete encoded uri (generic mode)
static QString removePassword(const QString &aUri)
Removes password element from uris.
void setDatabase(const QString &database)
Set database.
QString connectionInfo() const
return connection part of URI
WkbType
Used for symbology operations.
The QGis class provides global constants for use throughout the application.
bool useEstimatedMetadata() const
QStringList params(const QString &key) const
Get multiple generic param (generic mode)
QString keyColumn() const
void setConnection(const QString &aHost, const QString &aPort, const QString &aDatabase, const QString &aUsername, const QString &aPassword, SSLmode sslmode=SSLprefer)
Set all connection related members at once.
void setPassword(QString password)
set password
void disableSelectAtId(bool theFlag)
QGis::WkbType wkbType() const
QString uri() const
return complete uri
void setParam(const QString &key, const QString &value)
Set generic param (generic mode)
void setUseEstimatedMetadata(bool theFlag)
set use Estimated Metadata
enum SSLmode sslMode() const
bool hasParam(const QString &key) const
Test if param exists (generic mode)
void setSrid(QString srid)
QString param(const QString &key) const
Get generic param (generic mode)
void setKeyColumn(QString column)
void setUsername(QString username)
set username
void setWkbType(QGis::WkbType type)
bool selectAtIdDisabled() const
QByteArray encodedUri() const
return complete encoded uri (generic mode)
void setDataSource(const QString &aSchema, const QString &aTable, const QString &aGeometryColumn, const QString &aSql=QString(), const QString &aKeyColumn=QString())
Set all data source related members at once.