Browse Source

HUE-8758 [connectors] Port check config to support sqlalchemy interface

Romain 5 years ago
parent
commit
265c6b3e28

+ 1 - 1
desktop/libs/notebook/src/notebook/conf.py

@@ -317,7 +317,7 @@ def config_validator(user):
     res.append(('Editor', _('Could not authenticate with user %s to validate interpreters') % user))
 
   for interpreter in get_ordered_interpreters(user=user):
-    if interpreter['interface'] == 'hiveserver2':  # TODO: switch to is_sql when SqlAlchmy is ported
+    if interpreter.get('is_sql'):
       connector = interpreter['type']
 
       try:

+ 23 - 17
desktop/libs/notebook/src/notebook/connectors/sql_alchemy.py

@@ -296,26 +296,28 @@ class SqlAlchemyApi(Api):
     assist = Assist(inspector, engine, backticks=self.backticks)
     response = {'status': -1, 'result': {}}
 
-    metadata, sample_data = assist.get_sample_data(database, table, column)
+    metadata, sample_data = assist.get_sample_data(database, table, column=column, operation=operation)
     has_result_set = sample_data is not None
 
     if sample_data:
       response['status'] = 0
       response['rows'] = escape_rows(sample_data)
 
-    if table:
+    if table and operation != 'hello':
       columns = assist.get_columns(database, table)
       response['full_headers'] = [{
         'name': col.get('name'),
         'type': str(col.get('type')),
         'comment': ''
-      } for col in columns]
+      } for col in columns
+    ]
     elif metadata:
       response['full_headers'] = [{
         'name': col[0] if type(col) is dict or type(col) is tuple else col,
         'type': 'STRING_TYPE',
         'comment': ''
-      } for col in metadata]
+      } for col in metadata
+    ]
 
     return response
 
@@ -358,19 +360,23 @@ class Assist(object):
   def get_columns(self, database, table):
     return self.db.get_columns(table, database)
 
-  def get_sample_data(self, database, table, column=None):
-    column = '%(backticks)s%(column)s%(backticks)s' % {'backticks': self.backticks, 'column': column} if column else '*'
-    statement = textwrap.dedent('''\
-      SELECT %(column)s
-      FROM %(backticks)s%(database)s%(backticks)s.%(backticks)s%(table)s%(backticks)s
-      LIMIT %(limit)s
-      ''' % {
-        'database': database,
-        'table': table,
-        'column': column,
-        'limit': 100,
-        'backticks': self.backticks
-    })
+  def get_sample_data(self, database, table, column=None, operation=None):
+    if operation == 'hello':
+      statement = "SELECT 'Hello World!'"
+    else:
+      column = '%(backticks)s%(column)s%(backticks)s' % {'backticks': self.backticks, 'column': column} if column else '*'
+      statement = textwrap.dedent('''\
+        SELECT %(column)s
+        FROM %(backticks)s%(database)s%(backticks)s.%(backticks)s%(table)s%(backticks)s
+        LIMIT %(limit)s
+        ''' % {
+          'database': database,
+          'table': table,
+          'column': column,
+          'limit': 100,
+          'backticks': self.backticks
+      })
+
     connection = self.engine.connect()
     try:
       result = connection.execution_options(stream_results=True).execute(statement)