22 #include <QStringList>
27 : mSSLmode( SSLprefer )
29 , mUseEstimatedMetadata( false )
30 , mSelectAtIdDisabled( false )
31 , mWkbType(
QGis::WKBUnknown )
37 : mSSLmode( SSLprefer )
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,
'"' ) );
660 const QString &database,
661 const QString &username,
662 const QString &password,
674 const QString &database,
675 const QString &username,
676 const QString &password,
687 const QString &table,
688 const QString &geometryColumn,
690 const QString &keyColumn )
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 );