Skip to content
Permalink
Browse files

Determine pkey in Oracle view

  • Loading branch information
troopa81 authored and mhugo committed Mar 12, 2020
1 parent dee5882 commit 497233d2055ddaea1f02ae2b1acd5f244680f884
Showing with 33 additions and 2 deletions.
  1. +2 −2 src/providers/oracle/qgsoracleprovider.cpp
  2. +31 −0 tests/src/python/test_provider_oracle.py
@@ -895,8 +895,8 @@ bool QgsOracleProvider::determinePrimaryKey()
if ( !mIsQuery )
{
if ( !exec( qry, QString( "SELECT column_name"
" FROM all_ind_columns a"
" JOIN all_constraints b ON a.index_name=constraint_name AND a.index_owner=b.owner"
" FROM all_cons_columns a"
" JOIN all_constraints b ON a.constraint_name=b.constraint_name AND a.owner=b.owner"
" WHERE b.constraint_type='P' AND b.owner=? AND b.table_name=?" ),
QVariantList() << mOwnerName << mTableName ) )
{
@@ -726,6 +726,37 @@ def testGetFeatureFidInvalid(self):
feature = vl.getFeature(2)
self.assertTrue(feature.isValid())

def testDeterminePKOnView(self):
"""
Determine primary key on a view if primary key constraint (disabled) exists on view
"""

self.execSQLCommand('DROP TABLE "QGIS"."TABLE_TESTPKS"', ignore_errors=True)
self.execSQLCommand("""CREATE TABLE "QGIS"."TABLE_TESTPKS" (pk1 INTEGER, DESCRIPTION VARCHAR2(25), pk2 NUMBER, CONSTRAINT cons_pk PRIMARY KEY(pk1, pk2))""")
self.execSQLCommand("""INSERT INTO "QGIS"."TABLE_TESTPKS" VALUES(1000,'Desc for 1st record', 1)""")
self.execSQLCommand("""INSERT INTO "QGIS"."TABLE_TESTPKS" VALUES(2000,'Desc for 2nd record', 2)""")
self.execSQLCommand("""CREATE OR REPLACE VIEW "QGIS"."VIEW_TESTPKS" AS SELECT * FROM "QGIS"."TABLE_TESTPKS" """)

vl = QgsVectorLayer(
self.dbconn + ' sslmode=disable table="QGIS"."TABLE_TESTPKS" sql=',
'test', 'oracle')

self.assertEqual(vl.dataProvider().pkAttributeIndexes(), [0, 2])

vl = QgsVectorLayer(
self.dbconn + ' sslmode=disable table="QGIS"."VIEW_TESTPKS" sql=',
'test', 'oracle')

self.assertEqual(vl.dataProvider().pkAttributeIndexes(), [])

self.execSQLCommand("""ALTER VIEW VIEW_TESTPKS ADD CONSTRAINT const_view_pks PRIMARY KEY (pk1,pk2) DISABLE""")

vl = QgsVectorLayer(
self.dbconn + ' sslmode=disable table="QGIS"."VIEW_TESTPKS" sql=',
'test', 'oracle')

self.assertEqual(vl.dataProvider().pkAttributeIndexes(), [0, 2])


if __name__ == '__main__':
unittest.main()

0 comments on commit 497233d

Please sign in to comment.
You can’t perform that action at this time.