Browse Source

[notebook] Support multi queries in the editor

Romain Rigaux 10 years ago
parent
commit
6730f26

+ 22 - 2
desktop/libs/notebook/src/notebook/connectors/hiveserver2.py

@@ -24,6 +24,7 @@ from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.i18n import force_unicode
 
 from notebook.connectors.base import Api, QueryError, QueryExpired
+from beeswax.design import strip_trailing_semicolon, split_statements
 
 LOG = logging.getLogger(__name__)
 
@@ -58,6 +59,8 @@ class HS2Api(Api):
 
   def _get_handle(self, snippet):
     snippet['result']['handle']['secret'], snippet['result']['handle']['guid'] = HiveServerQueryHandle.get_decoded(snippet['result']['handle']['secret'], snippet['result']['handle']['guid'])
+    snippet['result']['handle'].pop('statement_id')
+    snippet['result']['handle'].pop('has_more') 
     return HiveServerQueryHandle(**snippet['result']['handle'])
 
   def _get_db(self, snippet):
@@ -72,7 +75,18 @@ class HS2Api(Api):
 
   def execute(self, notebook, snippet):
     db = self._get_db(snippet)
-    query = hql_query(snippet['statement'], QUERY_TYPES[0])
+
+    # Multiquery, if not first statement or arrived to the last query
+    statement_id = snippet['result']['handle'].get('statement_id', 0)
+    if snippet['result']['handle'].get('has_more'):
+      statement_id += 1
+    else:
+      statement_id = 0
+
+    statements = self._get_statements(snippet['statement'])
+    statement = statements[statement_id]
+
+    query = hql_query(statement, QUERY_TYPES[0])
 
     try:
       handle = db.client.query(query)
@@ -87,9 +101,15 @@ class HS2Api(Api):
         'operation_type': handle.operation_type,
         'has_result_set': handle.has_result_set,
         'modified_row_count': handle.modified_row_count,
-        'log_context': handle.log_context
+        'log_context': handle.log_context,
+        'statement_id': statement_id,
+        'has_more': statement_id < len(statements) - 1
     }
 
+  def _get_statements(self, hql_query):
+    hql_query = strip_trailing_semicolon(hql_query)
+    return [strip_trailing_semicolon(statement.strip()) for statement in split_statements(hql_query)]    
+
   @query_error_handler
   def check_status(self, notebook, snippet):
     response = {}

+ 5 - 2
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -857,13 +857,13 @@ ${ require.config() }
 
 <script type ="text/html" id="snippet-execution-controls">
   <!-- ko if: $root.editorMode -->
-  <div class="snippet-actions" style="position:absolute; bottom: 26px">
+  <div class="snippet-actions" style="position: absolute; bottom: 26px">
     <a class="snippet-side-btn" data-bind="visible: $parent.history().length > 0, click: function() { $parent.showHistory(! $parent.showHistory()); window.setTimeout(redrawFixedHeaders, 100); }, css: {'blue': $parent.showHistory}" title="${ _('Show history') }">
       <i class="fa fa-fw fa-history"></i>
     </a>
   </div>
   <!-- /ko -->
-  <div class="snippet-actions" style="position:absolute; bottom: 0">
+  <div class="snippet-actions" style="position: absolute; bottom: 0">
     <a class="snippet-side-btn" style="cursor: default;" data-bind="visible: status() == 'loading'" title="${ _('Creating session') }">
       <i class="fa fa-fw fa-spinner fa-spin"></i>
     </a>
@@ -873,6 +873,9 @@ ${ require.config() }
     <a class="snippet-side-btn" data-bind="click: execute, visible: status() != 'running' && status() != 'loading', css: {'blue': $parent.history().length == 0 || $root.editorMode, 'disabled': statement() === '' }" title="${ _('CTRL + ENTER') }">
       <i class="fa fa-fw fa-play"></i>
     </a>
+    <a class="snippet-side-btn" data-bind="click: execute, visible: result && result.handle().has_more, css: {'blue': $parent.history().length == 0 || $root.editorMode, 'disabled': statement() === '' }" title="${ _('CTRL + ENTER') }">
+      <i class="fa fa-fw fa-cog"></i>
+    </a>
   </div>
 </script>