def explain(self): self.results.assure_visible() buf = self.textview.get_buffer() bounds = buf.get_selection_bounds() if not bounds: bounds = buf.get_bounds() statement = buf.get_text(*bounds) if len(sqlparse.split(statement)) > 1: dialogs.error(_(u"Select a single statement to explain.")) return if not self.connection: return queries = [ Query(stmt, self.connection) for stmt in self.connection.explain_statements(statement) ] def _execute_next(last, queries): if last is not None and last.failed: self.results.set_explain_results(last) return q = queries.pop(0) if len(queries) == 0: q.connect('finished', lambda x: self.results.set_explain_results(x)) else: q.connect('finished', lambda x: _execute_next(x, queries)) q.execute() _execute_next(None, queries)
def test_execute_emit(self): self.res_q = None def _check_cb(datasource, q): self.res_q = q sql1 = 'create table foo (val integer);' conn = self.ds.dbconnect() query = Query(sql1, conn) self.assertEmitted((self.ds, 'executed', _check_cb), query.execute) self.assert_(isinstance(self.res_q, Query), 'Expected Query, got %r' % self.res_q)
def exec_threaded(statement, start_line): if self.app.config.get("sqlparse.enabled", True): stmts = sqlparse.split(statement) else: stmts = [statement] for stmt in stmts: add_offset = len(stmt.splitlines()) if not stmt.strip(): start_line += add_offset continue query = Query(stmt, self.connection) # query.coding_hint = self.connection.coding_hint gtk.gdk.threads_enter() query.set_data('editor_start_line', start_line) query.connect("started", self.on_query_started) query.connect("finished", self.on_query_finished, tag_notice) gtk.gdk.threads_leave() query.execute(True) start_line += add_offset if query.failed: # hmpf, doesn't work that way... so just return here... return
def test_parsed(self): q = Query('select * from foo', self.conn) self.assert_(isinstance(q.parsed, sqlparse.sql.Statement), 'Expected sqlparse.sql.Statement, got %r' % q.parsed)
def execute_query(self, statement_at_cursor=False): # TODO(andi): This method needs some refactoring: # - the actual execution code is doubled # - that affects offset counting too self.results.assure_visible() def exec_threaded(statement, start_line): if self.app.config.get("sqlparse.enabled", True): stmts = sqlparse.split(statement) else: stmts = [statement] for stmt in stmts: add_offset = len(stmt.splitlines()) if not stmt.strip(): start_line += add_offset continue query = Query(stmt, self.connection) # query.coding_hint = self.connection.coding_hint gtk.gdk.threads_enter() query.set_data('editor_start_line', start_line) query.connect("started", self.on_query_started) query.connect("finished", self.on_query_finished, tag_notice) gtk.gdk.threads_leave() query.execute(True) start_line += add_offset if query.failed: # hmpf, doesn't work that way... so just return here... return # gtk.gdk.threads_enter() # dlg = gtk.MessageDialog(None, # gtk.DIALOG_MODAL| # gtk.DIALOG_DESTROY_WITH_PARENT, # gtk.MESSAGE_ERROR, # gtk.BUTTONS_YES_NO, # _(u"An error occurred. Continue?")) # if dlg.run() == gtk.RESPONSE_NO: # leave = True # else: # leave = False # dlg.destroy() # gtk.gdk.threads_leave() # if leave: # return buffer = self.textview.get_buffer() self.results.reset() if not statement_at_cursor: bounds = buffer.get_selection_bounds() if not bounds: bounds = buffer.get_bounds() else: bounds = self.textview.get_current_statement() if bounds is None: return buffer.remove_tag_by_name('error', *bounds) statement = buffer.get_text(*bounds) if self.app.config.get("editor.replace_variables"): tpl = string.Template(statement) tpl_search = tpl.pattern.search(tpl.template) if tpl_search and tpl_search.groupdict().get("named"): dlg = StatementVariablesDialog(tpl) if dlg.run() == gtk.RESPONSE_OK: statement = dlg.get_statement() else: statement = None dlg.destroy() if not statement: return def foo(connection, msg): self.results.add_message(msg) tag_notice = self.connection.connect("notice", foo) if self.connection.threadsafety >= 2: start_line = bounds[0].get_line()+1 thread.start_new_thread(exec_threaded, (statement, start_line)) else: start_line = bounds[0].get_line()+1 line_offset = start_line if self.app.config.get("sqlparse.enabled", True): stmts = sqlparse.split(statement) else: stmts = [statement] for stmt in stmts: add_offset = len(stmt.splitlines()) if not stmt.strip(): line_offset += add_offset continue query = Query(stmt, self.connection) query.set_data('editor_start_line', line_offset) # query.coding_hint = self.connection.coding_hint query.connect("started", self.on_query_started) query.connect("finished", self.on_query_finished, tag_notice) query.execute() line_offset += add_offset
def execute_query(self, statement_at_cursor=False): # TODO(andi): This method needs some refactoring: # - the actual execution code is doubled # - that affects offset counting too self.results.assure_visible() def exec_threaded(statement, start_line): if self.app.config.get("sqlparse.enabled", True): stmts = sqlparse.split(statement) else: stmts = [statement] for stmt in stmts: add_offset = len(stmt.splitlines()) if not stmt.strip(): start_line += add_offset continue query = Query(stmt, self.connection) # query.coding_hint = self.connection.coding_hint gtk.gdk.threads_enter() query.set_data('editor_start_line', start_line) query.connect("started", self.on_query_started) query.connect("finished", self.on_query_finished, tag_notice) gtk.gdk.threads_leave() query.execute(True) start_line += add_offset if query.failed: # hmpf, doesn't work that way... so just return here... return # gtk.gdk.threads_enter() # dlg = gtk.MessageDialog(None, # gtk.DIALOG_MODAL| # gtk.DIALOG_DESTROY_WITH_PARENT, # gtk.MESSAGE_ERROR, # gtk.BUTTONS_YES_NO, # _(u"An error occurred. Continue?")) # if dlg.run() == gtk.RESPONSE_NO: # leave = True # else: # leave = False # dlg.destroy() # gtk.gdk.threads_leave() # if leave: # return buffer = self.textview.get_buffer() self.results.reset() if not statement_at_cursor: bounds = buffer.get_selection_bounds() if not bounds: bounds = buffer.get_bounds() else: bounds = self.textview.get_current_statement() if bounds is None: return buffer.remove_tag_by_name('error', *bounds) statement = buffer.get_text(*bounds) if self.app.config.get("editor.replace_variables"): tpl = string.Template(statement) tpl_search = tpl.pattern.search(tpl.template) if tpl_search and tpl_search.groupdict().get("named"): dlg = StatementVariablesDialog(tpl) if dlg.run() == gtk.RESPONSE_OK: statement = dlg.get_statement() else: statement = None dlg.destroy() if not statement: return def foo(connection, msg): self.results.add_message(msg) tag_notice = self.connection.connect("notice", foo) if self.connection.threadsafety >= 2: start_line = bounds[0].get_line() + 1 thread.start_new_thread(exec_threaded, (statement, start_line)) else: start_line = bounds[0].get_line() + 1 line_offset = start_line if self.app.config.get("sqlparse.enabled", True): stmts = sqlparse.split(statement) else: stmts = [statement] for stmt in stmts: add_offset = len(stmt.splitlines()) if not stmt.strip(): line_offset += add_offset continue query = Query(stmt, self.connection) query.set_data('editor_start_line', line_offset) # query.coding_hint = self.connection.coding_hint query.connect("started", self.on_query_started) query.connect("finished", self.on_query_finished, tag_notice) query.execute() line_offset += add_offset