bug1535fix.diff

Bugfix for Ticket #1535 - jcs -, 2009-02-10 08:47 AM

Download (6.16 KB)

View differences:

src/core/qgsdatasourceuri.cpp (working copy)
136 136
      {
137 137
        mPort = pval;
138 138
      }
139
      else if ( pname == "key" )
140
      {
141
        mKey = pval;
142
      }
139 143
      else if ( pname == "tty" )
140 144
      {
141 145
        QgsDebugMsg( "backend debug tty ignored" );
......
189 193
  return mGeometryColumn;
190 194
}
191 195

  
196
QString QgsDataSourceURI::key() const
197
{
198
  return mKey;
199
}
200

  
192 201
void QgsDataSourceURI::setSql( QString sql )
193 202
{
194 203
  mSql = sql;
......
298 307

  
299 308
QString QgsDataSourceURI::uri() const
300 309
{
301
  return connectionInfo()
302
         + QString( " table=%1 (%2) sql=%3" )
310
  QString r(connectionInfo());
311
  if (!mKey.isEmpty())
312
	  r += QString( " key=%1" ).arg(mKey);
313
  r += QString( " table=%1 (%2) sql=%3" )
303 314
         .arg( quotedTablename() )
304 315
         .arg( mGeometryColumn )
305 316
         .arg( mSql );
317
  return r;
306 318
}
307 319

  
308 320
QString QgsDataSourceURI::quotedTablename() const
......
329 341
void QgsDataSourceURI::setDataSource( const QString &schema,
330 342
                                      const QString &table,
331 343
                                      const QString &geometryColumn,
332
                                      const QString &sql )
344
                                      const QString &sql,
345
                                      const QString &key )
333 346
{
334 347
  mSchema = schema;
335 348
  mTable = table;
336 349
  mGeometryColumn = geometryColumn;
337 350
  mSql = sql;
351
  mKey = key;
338 352
}
src/core/qgsdatasourceuri.h (working copy)
58 58
    void setDataSource( const QString& aSchema,
59 59
                        const QString& aTable,
60 60
                        const QString& aGeometryColumn,
61
                        const QString& aSql = QString() );
61
                        const QString& aSql = QString(),
62
                        const QString& aKey = QString() );
62 63

  
63 64
    QString username() const;
64 65
    QString schema() const;
65 66
    QString table() const;
66 67
    QString sql() const;
67 68
    QString geometryColumn() const;
69
    QString key() const;
68 70

  
69 71
    void clearSchema();
70 72
    void setSql( QString sql );
......
87 89
    QString mTable;
88 90
    //! geometry column
89 91
    QString mGeometryColumn;
92
    //! primary key column
93
    QString mKey;
90 94
    //! SQL where clause used to limit features returned from the layer
91 95
    QString mSql;
92 96
    //! username
src/providers/postgres/qgspostgresprovider.cpp (working copy)
74 74
  mTableName = mUri.table();
75 75
  geometryColumn = mUri.geometryColumn();
76 76
  sqlWhereClause = mUri.sql();
77
  primaryKey = mUri.key();
77 78

  
78 79
  // Keep a schema qualified table name for convenience later on.
79 80
  mSchemaTableName = mUri.quotedTablename();
......
85 86
  QgsDebugMsg( "Geometry column is: " + geometryColumn );
86 87
  QgsDebugMsg( "Schema is: " + mSchemaName );
87 88
  QgsDebugMsg( "Table name is: " + mTableName );
89
  if (!primaryKey.isEmpty())
90
    QgsDebugMsg( "User-suggested primary key is: " + primaryKey );
88 91

  
89 92
  connectionRW = NULL;
90 93
  connectionRO = Conn::connectDb( mUri.connectionInfo(), true );
......
190 193
  loadFields();
191 194

  
192 195
  // set the primary key
193
  getPrimaryKey();
196
  getPrimaryKey(primaryKey);
194 197

  
195 198
  // Set the postgresql message level so that we don't get the
196 199
  // 'there is no transaction in progress' warning.
......
860 863
  }
861 864
}
862 865

  
863
QString QgsPostgresProvider::getPrimaryKey()
866
QString QgsPostgresProvider::getPrimaryKey(const QString& suggestedKey)
864 867
{
868
  QStringList log;
869
  QString sql;
870

  
871
  // if the user has suggested a key to use, do some verification to
872
  // ensure it is satisfactory
873
  if (!suggestedKey.isEmpty())
874
  {
875
        // Get the column data type
876
        sql = QString( "select pg_type.typname from pg_attribute,pg_type where atttypid=pg_type.oid and attname=%1 and attrelid=regclass(%2)" )
877
              .arg( quotedValue( suggestedKey ) )
878
	      .arg( quotedValue( mSchemaTableName ) );
879
        Result types = connectionRO->PQexec( sql );
880

  
881
        if ( PQntuples( types ) > 0 )
882
        {
883
          QString columnType = QString::fromUtf8( PQgetvalue( types, 0, 0 ) );
884

  
885
          if ( columnType != "int4" )
886
            log.append( tr( "The user suggested column '%1' is unsuitable because Qgis does not currently "
887
                            "support non-int4 type columns as a key into the table.\n" ).arg( suggestedKey ) );
888
          else 
889
	  {
890
	    primaryKey = suggestedKey;
891
	    primaryKeyType = columnType;
892
	    return primaryKey;
893
	  }
894
        }
895
	else
896
	{
897
          QgsDebugMsg( QString("User-suggested key column %1.%2(%3) not found.").arg(mSchemaName).arg(mTables).arg(suggestedKey) );
898
	}
899
  }
900

  
865 901
  // check to see if there is an unique index on the relation, which
866 902
  // can be used as a key into the table. Primary keys are always
867 903
  // unique indices, so we catch them as well.
868 904

  
869
  QString sql = QString( "select indkey from pg_index where indisunique='t' and indrelid=regclass(%1)::oid" )
905
  sql = QString( "select indkey from pg_index where indisunique='t' and indrelid=regclass(%1)::oid" )
870 906
                .arg( quotedValue( mSchemaTableName ) );
871 907

  
872 908
  QgsDebugMsg( "Getting unique index using '" + sql + "'" );
......
875 911

  
876 912
  QgsDebugMsg( "Got " + QString::number( PQntuples( pk ) ) + " rows." );
877 913

  
878
  QStringList log;
879

  
880 914
  // if we got no tuples we ain't got no unique index :)
881 915
  if ( PQntuples( pk ) == 0 )
882 916
  {
src/providers/postgres/qgspostgresprovider.h (working copy)
159 159

  
160 160
    /**  * Get the name of the primary key for the layer
161 161
    */
162
    QString getPrimaryKey();
162
    QString getPrimaryKey(const QString& suggestedKey = QString());
163 163

  
164 164
    /**
165 165
     * Get the field information for the layer