From ccdd6124ce7e1b4c915880b102d5fff2ac766fab Mon Sep 17 00:00:00 2001 From: gisn8 Date: Fri, 4 Oct 2024 11:41:56 -0400 Subject: [PATCH 1/4] Addressing Issue #35767 to allow for view creation into specified schema from [ Create a view ] in DBManager --- python/plugins/db_manager/db_plugins/postgis/connector.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/python/plugins/db_manager/db_plugins/postgis/connector.py b/python/plugins/db_manager/db_plugins/postgis/connector.py index d6f3343a5375..522c8f625282 100644 --- a/python/plugins/db_manager/db_plugins/postgis/connector.py +++ b/python/plugins/db_manager/db_plugins/postgis/connector.py @@ -1014,7 +1014,13 @@ def moveTable(self, table, new_table, new_schema=None): self._commit() def createView(self, view, query): - sql = "CREATE VIEW %s AS %s" % (self.quoteId(view), query) + user_input = view + + if '.' in user_input: # To allow view creation into specified schema + schema, view_name = user_input.split('.') + sql = "CREATE VIEW %s AS %s" % (self.quoteId([schema, view_name]), query) + else: # No schema specified; uses public + sql = "CREATE VIEW %s AS %s" % (self.quoteId(view), query) self._execute_and_commit(sql) def createSpatialView(self, view, query): From 18addb924537fb7a11619e83f3ad171fb86406d6 Mon Sep 17 00:00:00 2001 From: gisn8 Date: Tue, 8 Oct 2024 08:10:19 -0400 Subject: [PATCH 2/4] Handle invalid view names with more than one period I've updated the code as recommended and added an error message should there there be too many periods. Thanks for the suggestion! --- .../db_manager/db_plugins/postgis/connector.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/python/plugins/db_manager/db_plugins/postgis/connector.py b/python/plugins/db_manager/db_plugins/postgis/connector.py index 522c8f625282..79fbda540dd5 100644 --- a/python/plugins/db_manager/db_plugins/postgis/connector.py +++ b/python/plugins/db_manager/db_plugins/postgis/connector.py @@ -1014,12 +1014,15 @@ def moveTable(self, table, new_table, new_schema=None): self._commit() def createView(self, view, query): - user_input = view + view_name_parts = view.split('.') - if '.' in user_input: # To allow view creation into specified schema - schema, view_name = user_input.split('.') + if len(view_name_parts) > 2: + # Raise an error when more than one period is used. + raise ValueError("Invalid view name: Please use the format 'schema.viewname', or enter only the viewname for the public schema.") + elif len(view_name_parts) == 2: # To allow view creation into specified schema + schema, view_name = view_name_parts sql = "CREATE VIEW %s AS %s" % (self.quoteId([schema, view_name]), query) - else: # No schema specified; uses public + else: # No specific schema specified sql = "CREATE VIEW %s AS %s" % (self.quoteId(view), query) self._execute_and_commit(sql) From 0b4bc4e48e31835f2e367c0ec759cd7bf8403406 Mon Sep 17 00:00:00 2001 From: gisn8 Date: Tue, 8 Oct 2024 15:32:45 -0400 Subject: [PATCH 3/4] Fix: clean up whitespace and formatting issues caught by Flake8 --- python/plugins/db_manager/db_plugins/postgis/connector.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/plugins/db_manager/db_plugins/postgis/connector.py b/python/plugins/db_manager/db_plugins/postgis/connector.py index 79fbda540dd5..a696316ef4c8 100644 --- a/python/plugins/db_manager/db_plugins/postgis/connector.py +++ b/python/plugins/db_manager/db_plugins/postgis/connector.py @@ -1015,14 +1015,14 @@ def moveTable(self, table, new_table, new_schema=None): def createView(self, view, query): view_name_parts = view.split('.') - + if len(view_name_parts) > 2: # Raise an error when more than one period is used. raise ValueError("Invalid view name: Please use the format 'schema.viewname', or enter only the viewname for the public schema.") - elif len(view_name_parts) == 2: # To allow view creation into specified schema + elif len(view_name_parts) == 2: # To allow view creation into specified schema schema, view_name = view_name_parts sql = "CREATE VIEW %s AS %s" % (self.quoteId([schema, view_name]), query) - else: # No specific schema specified + else: # No specific schema specified sql = "CREATE VIEW %s AS %s" % (self.quoteId(view), query) self._execute_and_commit(sql) From 9314b276fb326f4aae02160314e45ee0b8a047d2 Mon Sep 17 00:00:00 2001 From: gisn8 Date: Thu, 17 Oct 2024 07:52:45 -0400 Subject: [PATCH 4/4] Set raise error to DbError Thanks! Much more user-friendly. Co-authored-by: Nyall Dawson --- python/plugins/db_manager/db_plugins/postgis/connector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/plugins/db_manager/db_plugins/postgis/connector.py b/python/plugins/db_manager/db_plugins/postgis/connector.py index a696316ef4c8..afe4ae3fc3f6 100644 --- a/python/plugins/db_manager/db_plugins/postgis/connector.py +++ b/python/plugins/db_manager/db_plugins/postgis/connector.py @@ -1018,7 +1018,7 @@ def createView(self, view, query): if len(view_name_parts) > 2: # Raise an error when more than one period is used. - raise ValueError("Invalid view name: Please use the format 'schema.viewname', or enter only the viewname for the public schema.") + raise DbError("Invalid view name: Please use the format 'schema.viewname', or enter only the viewname for the public schema.") elif len(view_name_parts) == 2: # To allow view creation into specified schema schema, view_name = view_name_parts sql = "CREATE VIEW %s AS %s" % (self.quoteId([schema, view_name]), query)