QVector<QgsDataItem*> QgsPGConnectionItem::createChildren() { QVector<QgsDataItem*>items; QgsDataSourceUri uri = QgsPostgresConn::connUri( mName ); // TODO: wee need to cancel somehow acquireConnection() if deleteLater() was called on this item to avoid later credential dialog if connection failed QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( uri.connectionInfo( false ) ); if ( !conn ) { items.append( new QgsErrorItem( this, tr( "Connection failed" ), mPath + "/error" ) ); QgsDebugMsg( "Connection failed - " + uri.connectionInfo( false ) ); return items; } QList<QgsPostgresSchemaProperty> schemas; bool ok = conn->getSchemas( schemas ); QgsPostgresConnPool::instance()->releaseConnection( conn ); if ( !ok ) { items.append( new QgsErrorItem( this, tr( "Failed to get schemas" ), mPath + "/error" ) ); return items; } Q_FOREACH ( const QgsPostgresSchemaProperty& schema, schemas ) { QgsPGSchemaItem * schemaItem = new QgsPGSchemaItem( this, mName, schema.name, mPath + '/' + schema.name ); if ( !schema.description.isEmpty() ) { schemaItem->setToolTip( schema.description ); } items.append( schemaItem ); }
void QgsPgNewConnection::testConnection() { QgsDataSourceUri uri; if ( !txtService->text().isEmpty() ) { uri.setConnection( txtService->text(), txtDatabase->text(), txtUsername->text(), txtPassword->text(), ( QgsDataSourceUri::SslMode ) cbxSSLmode->currentData().toInt(), mAuthConfigSelect->configId() ); } else { uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), txtUsername->text(), txtPassword->text(), ( QgsDataSourceUri::SslMode ) cbxSSLmode->currentData().toInt(), mAuthConfigSelect->configId() ); } QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), true ); if ( conn ) { // Database successfully opened; we can now issue SQL commands. bar->pushMessage( tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ), QgsMessageBar::INFO ); // free pg connection resources conn->unref(); } else { bar->pushMessage( tr( "Connection failed - consult message log for details." ), QgsMessageBar::WARNING ); } }
void QgsPGConnectionItem::createSchema() { QString schemaName = QInputDialog::getText( nullptr, tr( "Create Schema" ), tr( "Schema name:" ) ); if ( schemaName.isEmpty() ) return; QgsDataSourceUri uri = QgsPostgresConn::connUri( mName ); QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false ); if ( !conn ) { QMessageBox::warning( nullptr, tr( "Create Schema" ), tr( "Unable to create schema." ) ); return; } //create the schema QString sql = QStringLiteral( "CREATE SCHEMA %1" ).arg( QgsPostgresConn::quotedIdentifier( schemaName ) ); QgsPostgresResult result( conn->PQexec( sql ) ); if ( result.PQresultStatus() != PGRES_COMMAND_OK ) { QMessageBox::warning( nullptr, tr( "Create Schema" ), tr( "Unable to create schema %1\n%2" ).arg( schemaName, result.PQresultErrorMessage() ) ); conn->unref(); return; } conn->unref(); refresh(); // the parent should be updated if ( mParent ) mParent->refreshConnections(); }
void QgsPGSchemaItem::renameSchema() { QgsNewNameDialog dlg( tr( "schema '%1'" ).arg( mName ), mName ); dlg.setWindowTitle( tr( "Rename Schema" ) ); if ( dlg.exec() != QDialog::Accepted || dlg.name() == mName ) return; QString schemaName = QgsPostgresConn::quotedIdentifier( mName ); QgsDataSourceUri uri = QgsPostgresConn::connUri( mConnectionName ); QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false ); if ( !conn ) { QMessageBox::warning( nullptr, tr( "Rename Schema" ), tr( "Unable to rename schema." ) ); return; } //rename the schema QString sql = QStringLiteral( "ALTER SCHEMA %1 RENAME TO %2" ) .arg( schemaName, QgsPostgresConn::quotedIdentifier( dlg.name() ) ); QgsPostgresResult result( conn->PQexec( sql ) ); if ( result.PQresultStatus() != PGRES_COMMAND_OK ) { QMessageBox::warning( nullptr, tr( "Rename Schema" ), tr( "Unable to rename schema %1\n%2" ).arg( schemaName, result.PQresultErrorMessage() ) ); conn->unref(); return; } conn->unref(); QMessageBox::information( nullptr, tr( "Rename Schema" ), tr( "Schema renamed successfully." ) ); if ( mParent ) mParent->refresh(); }
QString QgsOracleConn::toPoolName( const QgsDataSourceUri &uri ) { QString conninfo = uri.connectionInfo(); if ( uri.hasParam( "dbworkspace" ) ) conninfo += " dbworkspace=" + uri.param( "dbworkspace" ); return conninfo; }
void QgsPgNewConnection::testConnection() { QgsTemporaryCursorOverride cursorOverride( Qt::WaitCursor ); QgsDataSourceUri uri; if ( !txtService->text().isEmpty() ) { uri.setConnection( txtService->text(), txtDatabase->text(), mAuthSettings->username(), mAuthSettings->password(), ( QgsDataSourceUri::SslMode ) cbxSSLmode->currentData().toInt(), mAuthSettings->configId() ); } else { uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), mAuthSettings->username(), mAuthSettings->password(), ( QgsDataSourceUri::SslMode ) cbxSSLmode->currentData().toInt(), mAuthSettings->configId() ); } QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), true ); if ( conn ) { // Database successfully opened; we can now issue SQL commands. bar->pushMessage( tr( "Connection to %1 was successful." ).arg( txtDatabase->text() ), Qgis::Info ); // free pg connection resources conn->unref(); } else { bar->pushMessage( tr( "Connection failed - consult message log for details." ), Qgis::Warning ); } }
void QgsGeomColumnTypeThread::run() { QgsDataSourceUri uri = QgsPostgresConn::connUri( mName ); mConn = QgsPostgresConnPool::instance()->acquireConnection( uri.connectionInfo( false ) ); if ( !mConn ) { QgsDebugMsg( "Connection failed - " + uri.connectionInfo( false ) ); return; } mStopped = false; bool dontResolveType = QgsPostgresConn::dontResolveType( mName ); emit progressMessage( tr( "Retrieving tables of %1…" ).arg( mName ) ); QVector<QgsPostgresLayerProperty> layerProperties; if ( !mConn->supportedLayers( layerProperties, QgsPostgresConn::geometryColumnsOnly( mName ), QgsPostgresConn::publicSchemaOnly( mName ), mAllowGeometrylessTables ) || layerProperties.isEmpty() ) { QgsPostgresConnPool::instance()->releaseConnection( mConn ); mConn = nullptr; return; } int i = 0, n = layerProperties.size(); for ( QVector<QgsPostgresLayerProperty>::iterator it = layerProperties.begin(), end = layerProperties.end(); it != end; ++it ) { QgsPostgresLayerProperty &layerProperty = *it; if ( !mStopped ) { emit progress( i++, n ); emit progressMessage( tr( "Scanning column %1.%2.%3…" ) .arg( layerProperty.schemaName, layerProperty.tableName, layerProperty.geometryColName ) ); if ( !layerProperty.geometryColName.isNull() && ( layerProperty.types.value( 0, QgsWkbTypes::Unknown ) == QgsWkbTypes::Unknown || layerProperty.srids.value( 0, std::numeric_limits<int>::min() ) == std::numeric_limits<int>::min() ) ) { if ( dontResolveType ) { QgsDebugMsg( QStringLiteral( "skipping column %1.%2 without type constraint" ).arg( layerProperty.schemaName, layerProperty.tableName ) ); continue; } mConn->retrieveLayerTypes( layerProperty, mUseEstimatedMetadata ); } } if ( mStopped ) { layerProperty.types.clear(); layerProperty.srids.clear(); break; } // Now tell the layer list dialog box... emit setLayerType( layerProperty ); } emit progress( 0, 0 ); emit progressMessage( mStopped ? tr( "Table retrieval stopped." ) : tr( "Table retrieval finished." ) ); QgsPostgresConnPool::instance()->releaseConnection( mConn ); mConn = nullptr; }
void QgsPGSchemaItem::deleteSchema() { // check if schema contains tables/views QgsDataSourceUri uri = QgsPostgresConn::connUri( mConnectionName ); QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false ); if ( !conn ) { QMessageBox::warning( nullptr, tr( "Delete Schema" ), tr( "Unable to delete schema." ) ); return; } QString sql = QStringLiteral( "SELECT table_name FROM information_schema.tables WHERE table_schema='%1'" ).arg( mName ); QgsPostgresResult result( conn->PQexec( sql ) ); if ( result.PQresultStatus() != PGRES_TUPLES_OK ) { QMessageBox::warning( nullptr, tr( "Delete Schema" ), tr( "Unable to delete schema." ) ); conn->unref(); return; } QStringList childObjects; int maxListed = 10; for ( int idx = 0; idx < result.PQntuples(); idx++ ) { childObjects << result.PQgetvalue( idx, 0 ); QgsPostgresSchemaProperty schema; if ( idx == maxListed - 1 ) break; } int count = result.PQntuples(); if ( count > 0 ) { QString objects = childObjects.join( QStringLiteral( "\n" ) ); if ( count > maxListed ) { objects += QStringLiteral( "\n[%1 additional objects not listed]" ).arg( count - maxListed ); } if ( QMessageBox::question( nullptr, QObject::tr( "Delete Schema" ), QObject::tr( "Schema '%1' contains objects:\n\n%2\n\nAre you sure you want to delete the schema and all these objects?" ).arg( mName, objects ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes ) { conn->unref(); return; } } else { if ( QMessageBox::question( nullptr, QObject::tr( "Delete Schema" ), QObject::tr( "Are you sure you want to delete the schema '%1'?" ).arg( mName ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes ) return; } QString errCause; bool res = ::deleteSchema( mName, uri, errCause, count > 0 ); if ( !res ) { QMessageBox::warning( nullptr, tr( "Delete Schema" ), errCause ); } else { QMessageBox::information( nullptr, tr( "Delete Schema" ), tr( "Schema deleted successfully." ) ); if ( mParent ) mParent->refresh(); } }
QVector<QgsDataItem *> QgsPGSchemaItem::createChildren() { QVector<QgsDataItem *>items; QgsDataSourceUri uri = QgsPostgresConn::connUri( mConnectionName ); QgsPostgresConn *conn = QgsPostgresConnPool::instance()->acquireConnection( uri.connectionInfo( false ) ); if ( !conn ) { items.append( new QgsErrorItem( this, tr( "Connection failed" ), mPath + "/error" ) ); QgsDebugMsg( "Connection failed - " + uri.connectionInfo( false ) ); return items; } QVector<QgsPostgresLayerProperty> layerProperties; bool ok = conn->supportedLayers( layerProperties, QgsPostgresConn::geometryColumnsOnly( mConnectionName ), QgsPostgresConn::publicSchemaOnly( mConnectionName ), QgsPostgresConn::allowGeometrylessTables( mConnectionName ), mName ); if ( !ok ) { items.append( new QgsErrorItem( this, tr( "Failed to get layers" ), mPath + "/error" ) ); QgsPostgresConnPool::instance()->releaseConnection( conn ); return items; } bool dontResolveType = QgsPostgresConn::dontResolveType( mConnectionName ); const auto constLayerProperties = layerProperties; for ( QgsPostgresLayerProperty layerProperty : constLayerProperties ) { if ( layerProperty.schemaName != mName ) continue; if ( !layerProperty.geometryColName.isNull() && ( layerProperty.types.value( 0, QgsWkbTypes::Unknown ) == QgsWkbTypes::Unknown || layerProperty.srids.value( 0, std::numeric_limits<int>::min() ) == std::numeric_limits<int>::min() ) ) { if ( dontResolveType ) { //QgsDebugMsg( QStringLiteral( "skipping column %1.%2 without type constraint" ).arg( layerProperty.schemaName ).arg( layerProperty.tableName ) ); continue; } conn->retrieveLayerTypes( layerProperty, true /* useEstimatedMetadata */ ); } for ( int i = 0; i < layerProperty.size(); i++ ) { QgsPGLayerItem *layerItem = createLayer( layerProperty.at( i ) ); if ( layerItem ) items.append( layerItem ); } } QgsPostgresConnPool::instance()->releaseConnection( conn ); QgsProjectStorage *storage = QgsApplication::projectStorageRegistry()->projectStorageFromType( "postgresql" ); if ( QgsPostgresConn::allowProjectsInDatabase( mConnectionName ) && storage ) { QgsPostgresProjectUri postUri; postUri.connInfo = uri; postUri.schemaName = mName; QString schemaUri = QgsPostgresProjectStorage::encodeUri( postUri ); const QStringList projectNames = storage->listProjects( schemaUri ); for ( const QString &projectName : projectNames ) { QgsPostgresProjectUri projectUri( postUri ); projectUri.projectName = projectName; items.append( new QgsProjectItem( this, projectName, QgsPostgresProjectStorage::encodeUri( projectUri ) ) ); } } return items; }
QgsDb2Provider::QgsDb2Provider( QString uri ) : QgsVectorDataProvider( uri ) , mNumberFeatures( 0 ) , mEnvironment( ENV_LUW ) , mWkbType( QgsWkbTypes::Unknown ) { QgsDebugMsg( "uri: " + uri ); QgsDataSourceUri anUri = QgsDataSourceUri( uri ); if ( !anUri.srid().isEmpty() ) mSRId = anUri.srid().toInt(); else mSRId = -1; if ( 0 != anUri.wkbType() ) { mWkbType = anUri.wkbType(); } QgsDebugMsg( QString( "mWkbType: %1" ).arg( mWkbType ) ); QgsDebugMsg( QString( "new mWkbType: %1" ).arg( anUri.wkbType() ) ); mValid = true; mSkipFailures = false; int dim; // Not used db2WkbTypeAndDimension( mWkbType, mGeometryColType, dim ); // Get DB2 geometry type name mFidColName = anUri.keyColumn().toUpper(); QgsDebugMsg( "mFidColName " + mFidColName ); mExtents = anUri.param( "extents" ); QgsDebugMsg( "mExtents " + mExtents ); mUseEstimatedMetadata = anUri.useEstimatedMetadata(); QgsDebugMsg( QString( "mUseEstimatedMetadata: '%1'" ).arg( mUseEstimatedMetadata ) ); mSqlWhereClause = anUri.sql(); QString errMsg; mDatabase = getDatabase( uri, errMsg ); mConnInfo = anUri.connectionInfo(); QgsCoordinateReferenceSystem layerCrs = crs(); QgsDebugMsg( "CRS: " + layerCrs.toWkt() ); if ( !errMsg.isEmpty() ) { setLastError( errMsg ); QgsDebugMsg( mLastError ); mValid = false; return; } // Create a query for default connection mQuery = QSqlQuery( mDatabase ); mSchemaName = anUri.schema(); mTableName = anUri.table().toUpper(); QStringList sl = mTableName.split( '.' ); if ( sl.length() == 2 ) // Never seems to be the case { mSchemaName = sl[0]; mTableName = sl[1]; } if ( mSchemaName.isEmpty() ) { mSchemaName = anUri.username().toUpper(); } QgsDebugMsg( "mSchemaName: '" + mSchemaName + "; mTableName: '" + mTableName ); if ( !anUri.geometryColumn().isEmpty() ) mGeometryColName = anUri.geometryColumn().toUpper(); loadFields(); updateStatistics(); if ( mGeometryColName.isEmpty() ) { // table contains no geometries mWkbType = QgsWkbTypes::NoGeometry; mSRId = 0; } //fill type names into sets mNativeTypes // integer types << QgsVectorDataProvider::NativeType( tr( "8 Bytes integer" ), "bigint", QVariant::Int ) << QgsVectorDataProvider::NativeType( tr( "4 Bytes integer" ), "integer", QVariant::Int ) << QgsVectorDataProvider::NativeType( tr( "2 Bytes integer" ), "smallint", QVariant::Int ) << QgsVectorDataProvider::NativeType( tr( "Decimal number (numeric)" ), "numeric", QVariant::Double, 1, 31, 0, 31 ) << QgsVectorDataProvider::NativeType( tr( "Decimal number (decimal)" ), "decimal", QVariant::Double, 1, 31, 0, 31 ) // floating point << QgsVectorDataProvider::NativeType( tr( "Decimal number (real)" ), "real", QVariant::Double ) << QgsVectorDataProvider::NativeType( tr( "Decimal number (double)" ), "double", QVariant::Double ) // date/time types << QgsVectorDataProvider::NativeType( tr( "Date" ), "date", QVariant::Date, -1, -1, -1, -1 ) << QgsVectorDataProvider::NativeType( tr( "Time" ), "time", QVariant::Time, -1, -1, -1, -1 ) << QgsVectorDataProvider::NativeType( tr( "Date & Time" ), "datetime", QVariant::DateTime, -1, -1, -1, -1 ) // string types << QgsVectorDataProvider::NativeType( tr( "Text, fixed length (char)" ), "char", QVariant::String, 1, 254 ) << QgsVectorDataProvider::NativeType( tr( "Text, variable length (varchar)" ), "varchar", QVariant::String, 1, 32704 ) << QgsVectorDataProvider::NativeType( tr( "Text, variable length large object (clob)" ), "clob", QVariant::String, 1, 2147483647 ) //DBCLOB is for 1073741824 double-byte characters, data length should be the same as CLOB (2147483647)? << QgsVectorDataProvider::NativeType( tr( "Text, variable length large object (dbclob)" ), "dbclob", QVariant::String, 1, 1073741824 ) ; }
QgsOracleConn::QgsOracleConn( QgsDataSourceUri uri ) : mRef( 1 ) , mCurrentUser( QString::null ) , mHasSpatial( -1 ) { QgsDebugMsg( QString( "New Oracle connection for " ) + uri.connectionInfo() ); QString database = databaseName( uri.database(), uri.host(), uri.port() ); QgsDebugMsg( QString( "New Oracle database " ) + database ); mDatabase = QSqlDatabase::addDatabase( "QOCISPATIAL", QString( "oracle%1" ).arg( snConnections++ ) ); mDatabase.setDatabaseName( database ); QString options = uri.hasParam( "dboptions" ) ? uri.param( "dboptions" ) : "OCI_ATTR_PREFETCH_ROWS=1000"; QString workspace = uri.hasParam( "dbworkspace" ) ? uri.param( "dbworkspace" ) : QString::null; mDatabase.setConnectOptions( options ); mDatabase.setUserName( uri.username() ); mDatabase.setPassword( uri.password() ); QgsDebugMsg( QString( "Connecting with options: " ) + options ); if ( !mDatabase.open() ) { QString username = uri.username(); QString password = uri.password(); QString realm( database ); if ( !username.isEmpty() ) realm.prepend( username + "@" ); QgsCredentials::instance()->lock(); while ( !mDatabase.open() ) { bool ok = QgsCredentials::instance()->get( realm, username, password, mDatabase.lastError().text() ); if ( !ok ) break; if ( !username.isEmpty() ) { uri.setUsername( username ); realm = username + "@" + database; } if ( !password.isEmpty() ) uri.setPassword( password ); QgsDebugMsg( "Connecting to " + database ); mDatabase.setUserName( username ); mDatabase.setPassword( password ); } if ( mDatabase.isOpen() ) QgsCredentials::instance()->put( realm, username, password ); QgsCredentials::instance()->unlock(); } if ( !mDatabase.isOpen() ) { mDatabase.close(); QgsMessageLog::logMessage( tr( "Connection to database failed" ), tr( "Oracle" ) ); mRef = 0; return; } if ( !workspace.isNull() ) { QSqlQuery qry( mDatabase ); if ( !qry.exec( QString( "BEGIN\nDBMS_WM.GotoWorkspace(%1);\nEND;" ).arg( quotedValue( workspace ) ) ) ) { mDatabase.close(); QgsMessageLog::logMessage( tr( "Could not switch to workspace %1 [%2]" ).arg( workspace, qry.lastError().databaseText() ), tr( "Oracle" ) ); mRef = 0; return; } } }