Re: [pgadmin4][patch] External Tables for GreenPlum #3168

2018-03-02 Thread Dave Page
Thanks, patch applied.

On Fri, Mar 2, 2018 at 4:37 PM, Joao De Almeida Pereira <
jdealmeidapere...@pivotal.io> wrote:

> Hello Hackers,
>
> Attached you can find the patch that remove the External Tables from the
> Tables node and create a new node at the database level for GreenPlum.
>  - Do not display External Tables in the Tables node
>  - Create a new node under databases for External Tables
>  - Generate DDL for external tables
>  - Generate properties for external tables
>
> All these changes can only be seen when accessing a GreenPlum database.
>
> Thanks
> Joao
>



-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


[pgadmin4][patch] External Tables for GreenPlum #3168

2018-03-02 Thread Joao De Almeida Pereira
Hello Hackers,

Attached you can find the patch that remove the External Tables from the
Tables node and create a new node at the database level for GreenPlum.
 - Do not display External Tables in the Tables node
 - Create a new node under databases for External Tables
 - Generate DDL for external tables
 - Generate properties for external tables

All these changes can only be seen when accessing a GreenPlum database.

Thanks
Joao
diff --git a/web/pgadmin/browser/server_groups/servers/databases/external_tables/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/external_tables/__init__.py
new file mode 100644
index ..137c6c44
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/external_tables/__init__.py
@@ -0,0 +1,275 @@
+##
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2018, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+##
+
+"""Implements External Tables Node"""
+import os
+from functools import wraps
+from gettext import gettext
+
+from flask import render_template
+
+from config import PG_DEFAULT_DRIVER
+from pgadmin.browser.collection import CollectionNodeModule
+from pgadmin.browser.server_groups.servers import databases
+from pgadmin.browser.server_groups.servers.databases \
+.external_tables.mapping_utils import map_execution_location
+from pgadmin.browser.server_groups.servers.databases \
+.external_tables.properties import Properties, \
+PropertiesTableNotFoundException, PropertiesException
+from pgadmin.browser.server_groups.servers.databases \
+.external_tables.reverse_engineer_ddl import ReverseEngineerDDL
+from pgadmin.browser.utils import PGChildNodeView
+from pgadmin.utils.ajax import make_json_response, make_response, \
+internal_server_error
+from pgadmin.utils.compile_template_name import compile_template_path
+from pgadmin.utils.driver import get_driver
+
+
+class ExternalTablesModule(CollectionNodeModule):
+"""
+class ExternalTablesModule(CollectionNodeModule)
+
+A module class for External Tables node derived from
+CollectionNodeModule.
+
+Methods:
+---
+* __init__(*args, **kwargs)
+  - Method is used to initialize the External Tables module
+and it's base module.
+
+* get_nodes(gid, sid, did)
+  - Method is used to generate the browser collection node.
+
+* script_load()
+  - Load the module script for External Tables, when any of
+the database node is initialized.
+"""
+
+NODE_TYPE = 'external_table'
+COLLECTION_LABEL = gettext("External Tables")
+
+def __init__(self, *args, **kwargs):
+"""
+Method is used to initialize the External tables module and
+it's base module.
+
+Args:
+*args:
+**kwargs:
+"""
+
+super(ExternalTablesModule, self).__init__(*args, **kwargs)
+self.max_ver = 0
+
+def get_nodes(self, gid, sid, did):
+yield self.generate_browser_collection_node(did)
+
+@property
+def script_load(self):
+"""
+Load the module script for External tables,
+when any of the database node is initialized.
+
+Returns: node type of the database module.
+"""
+return databases.DatabaseModule.NODE_TYPE
+
+@property
+def module_use_template_javascript(self):
+"""
+Returns whether Jinja2 template is used for generating the javascript
+module.
+"""
+return False
+
+
+blueprint = ExternalTablesModule(__name__)
+
+
+class ExternalTablesView(PGChildNodeView):
+node_type = blueprint.node_type
+
+parent_ids = [
+{'type': 'int', 'id': 'server_group_id'},
+{'type': 'int', 'id': 'server_id'},
+{'type': 'int', 'id': 'database_id'}
+]
+
+ids = [
+{'type': 'int', 'id': 'external_table_id'}
+]
+
+operations = dict({
+'obj': [
+{'get': 'properties'}
+],
+'nodes': [{'get': 'node'}, {'get': 'nodes'}],
+'sql': [{'get': 'sql'}],
+'children': [{'get': 'children'}]
+})
+
+def check_precondition(function_wrapped):
+"""
+This function will behave as a decorator which will checks
+database connection before running view, it will also attaches
+manager,conn & template_path properties to self
+"""
+
+@wraps(function_wrapped)
+def wrap(*args, **kwargs):
+# Here args[0] will hold self & kwargs will hold gid,sid,did
+self = args[0]
+self.manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(
+kwargs['server_id']
+)
+self.connection = self.manager.connection(
+did=kwargs['database_id']
+)
+