Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/mtxr/SQLTools
Browse files Browse the repository at this point in the history
  • Loading branch information
mtxr committed May 13, 2017
2 parents 7ad11de + 5faab57 commit a5e9349
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ SQLTools will save you (for sure) a lot of time and help you to increase your pr

<span class="badge-paypal"><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RSMB6DGK238V8" title="Donate to this project using Paypal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" alt="PayPal donate button" /></a></span>

<a href="1GScMvdPWRiPaFf13gSqrAb8hrbW3MzWPs" title="Donate bitcoins"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAFRUlEQVR4Xu2dbXfqMAyD4f//aO7ZYDt9i/NIJAXutK+kaWpZtmyXcb1cLrfLxL/bbb399Xr9vdv2s+UxnHVf11d7tvavHr86/wyzfVkngBSWDSAP44QhAt17VK2Mufxsuc8oT1zus72X85kbcns2aoXPVcii8bd3swDSs9D686W9AsjDNi32hCEHOUTxNycsbdUZZfiMiFIypBXvtwY6w4tGSFYKrAvICHsFkAOUAgh1XTGcuSHlTwFCZW8Voqo8IWKLuga0g/CRISuA7Ns7WPaOQJwWfLSvVSkilx1uD4w+myKCTk/qYcibMYR6MU2slfdRhv9phgSQMAT5AJXILnNpI/X0XpYjIZXkSVsnW5RobqNjAeQFB3VVAOkUm2e0hbDsVVCmnk/XuaEhDBF7QQFkb7AmQ1xGVNdRyo+Y7vWKRsoel50j7Pc2LzkEkDucAeTh1s7EcAQjdmrvRgX4oLtT2UjXVfJ1hFw+2TyXawC5Q0oZMsgvm9sEEDFkBRDxfTFXIb0NQ5avko6IuT0PotM+GrsVdUb7SXQ2Q9f15PiqNg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9O1waQjqVcWa1I4tYRFDbSDnU5U/90hri9JnpdABFzCDWsUh85xes2hyiNRpxDaMx1QwPNNTOMTg2mqKxKqlcgr0Ld2d3e1sHOzhMBpFMRB5C7gU7v9oYhnS/6vHKm7up9Gqur3Ebzo9KEdJ+nmUPoIZV1NBTNSJ4B5AnJGkDuxvsHWqbLDa8ng8IAAAAASUVORK5CYII=" /></a>


## Contributors
Expand Down
26 changes: 15 additions & 11 deletions SQLTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys
import os
import re
from functools import partial

import sublime
from sublime_plugin import WindowCommand, EventListener, TextCommand
Expand Down Expand Up @@ -115,15 +114,20 @@ def loadDefaultConnection():
return default


def output(content, panel=None, syntax=None, prependText=None):
def createOutput(panel=None, syntax=None, prependText=None):
# hide previously set command running message (if any)
Window().status_message('')
if not panel:
panel = getOutputPlace(syntax)
if prependText:
panel.run_command('append', {'characters': str(prependText)})
panel.run_command('append', {'characters': content})
panel.set_read_only(True)

def append(outputContent):
panel.set_read_only(False)
panel.run_command('append', {'characters': outputContent})
panel.set_read_only(True)

return append


def toNewTab(content, name="", suffix="SQLTools Saved Query"):
Expand Down Expand Up @@ -401,7 +405,7 @@ def cb(index):
prependText = 'Table "{tableName}"\n'.format(tableName=tableName)
return ST.conn.getTableRecords(
tableName,
partial(output, prependText=prependText))
createOutput(prependText=prependText))

ST.selectTable(cb)

Expand All @@ -419,7 +423,7 @@ def cb(index):
if index < 0:
return None
Window().status_message(MESSAGE_RUNNING_CMD)
return ST.conn.getTableDescription(ST.tables[index], partial(output, syntax=currentSyntax))
return ST.conn.getTableDescription(ST.tables[index], createOutput(syntax=currentSyntax))

ST.selectTable(cb)

Expand All @@ -438,7 +442,7 @@ def cb(index):
return None
Window().status_message(MESSAGE_RUNNING_CMD)
functionName = ST.functions[index].split('(', 1)[0]
return ST.conn.getFunctionDescription(functionName, partial(output, syntax=currentSyntax))
return ST.conn.getFunctionDescription(functionName, createOutput(syntax=currentSyntax))

# get everything until first occurence of "(", e.g. get "function_name"
# from "function_name(int)"
Expand All @@ -453,7 +457,7 @@ def run():
return

Window().status_message(MESSAGE_RUNNING_CMD)
ST.conn.explainPlan(getSelection(), output)
ST.conn.explainPlan(getSelection(), createOutput())


class StExecute(WindowCommand):
Expand All @@ -464,7 +468,7 @@ def run():
return

Window().status_message(MESSAGE_RUNNING_CMD)
ST.conn.execute(getSelection(), output)
ST.conn.execute(getSelection(), createOutput(), stream=settings.get('use_streams', False))


class StFormat(TextCommand):
Expand Down Expand Up @@ -498,7 +502,7 @@ def run():
def cb(index):
if index < 0:
return None
return ST.conn.execute(history.get(index), output)
return ST.conn.execute(history.get(index), createOutput())

Window().show_quick_panel(history.all(), cb)

Expand Down Expand Up @@ -534,7 +538,7 @@ def cb(index):
if index < 0:
return None

param2 = output if mode == "run" else options[index][0]
param2 = createOutput() if mode == "run" else options[index][0]
func = ST.conn.execute if mode == "run" else toNewTab
return func(options[index][1], param2)

Expand Down
1 change: 1 addition & 0 deletions SQLTools.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"safe_limit" : false,
"show_query" : false,
"expand_to_paragraph" : false,
"use_streams" : false, // use streams for results
/**
* The list of syntax selectors for which the plugin autocompletion will be active.
* An empty list means autocompletion always active.
Expand Down
47 changes: 31 additions & 16 deletions SQLToolsAPI/Command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from .Log import Log


class Command:
class Command(object):
timeout = 15

def __init__(self, args, callback, query=None, encoding='utf-8',
options=None, timeout=15, silenceErrors=False):
options=None, timeout=15, silenceErrors=False, stream=False):
if options is None:
options = {}

self.stream = stream
self.args = args
self.callback = callback
self.query = query
Expand Down Expand Up @@ -43,6 +44,23 @@ def run(self):
env=os.environ.copy(),
startupinfo=si)

if self.stream:
self.process.stdin.write(self.query.encode())
self.process.stdin.close()
for line in self.process.stdout:
self.callback(line.decode(self.encoding,
'replace').replace('\r', ''))

queryTimerEnd = time.time()
if 'show_query' in self.options and self.options['show_query']:
resultInfo = "/*\n-- Executed querie(s) at {0} took {1:.3f}ms --".format(
str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(queryTimerStart))),
(queryTimerEnd - queryTimerStart))
resultLine = "-" * (len(resultInfo) - 3)
resultString = "{0}\n{1}\n{2}\n{3}\n*/".format(
resultInfo, resultLine, self.query, resultLine)
return self.callback(resultString)

results, errors = self.process.communicate(input=self.query.encode())

queryTimerEnd = time.time()
Expand All @@ -58,17 +76,17 @@ def run(self):
'replace').replace('\r', '')

if 'show_query' in self.options and self.options['show_query']:
resultInfo = "/*\n-- Executed querie(s) at {0} took {1}ms --".format(
resultInfo = "/*\n-- Executed querie(s) at {0} took {1:.3f}ms --".format(
str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(queryTimerStart))),
str(queryTimerEnd - queryTimerStart))
(queryTimerEnd - queryTimerStart))
resultLine = "-" * (len(resultInfo) - 3)
resultString = "{0}\n{1}\n{2}\n{3}\n*/\n{4}".format(
resultInfo, resultLine, self.query, resultLine, resultString)

self.callback(resultString)

@staticmethod
def createAndRun(args, query, callback, options=None, timeout=15, silenceErrors=False):
def createAndRun(args, query, callback, options=None, timeout=15, silenceErrors=False, stream=False):
if options is None:
options = {}
command = Command(args, callback, query, options=options,
Expand All @@ -78,18 +96,14 @@ def createAndRun(args, query, callback, options=None, timeout=15, silenceErrors=

class ThreadCommand(Command, Thread):
def __init__(self, args, callback, query=None, encoding='utf-8',
options=None, timeout=Command.timeout, silenceErrors=False):
options=None, timeout=Command.timeout, silenceErrors=False, stream=False):
if options is None:
options = {}

self.args = args
self.callback = callback
self.query = query
self.encoding = encoding
self.options = options
self.timeout = timeout
self.silenceErrors = silenceErrors
self.process = None
Command.__init__(self, args, callback, query=query,
encoding=encoding, options=options,
timeout=timeout, silenceErrors=silenceErrors,
stream=stream)
Thread.__init__(self)

def stop(self):
Expand All @@ -108,13 +122,14 @@ def stop(self):
pass

@staticmethod
def createAndRun(args, query, callback, options=None, timeout=Command.timeout, silenceErrors=False):
def createAndRun(args, query, callback, options=None,
timeout=Command.timeout, silenceErrors=False, stream=False):
# Don't allow empty dicts or lists as defaults in method signature,
# cfr http://nedbatchelder.com/blog/200806/pylint.html
if options is None:
options = {}
command = ThreadCommand(args, callback, query, options=options,
timeout=timeout, silenceErrors=silenceErrors)
timeout=timeout, silenceErrors=silenceErrors, stream=stream)
command.start()
killTimeout = Timer(command.timeout, command.stop)
killTimeout.start()
6 changes: 3 additions & 3 deletions SQLToolsAPI/Connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from . import Command as C


class Connection:
class Connection(object):
DB_CLI_NOT_FOUND_MESSAGE = """'{0}' could not be found.
Please set the path to '{0}' binary in your SQLTools settings before continuing.
Example of "cli" section in SQLTools.sublime-settings:
Expand Down Expand Up @@ -134,7 +134,7 @@ def explainPlan(self, queries, callback):
queryToRun = '\n'.join(self.getOptionsForSgdbCli()['before'] + stripped_queries)
self.Command.createAndRun(self.builArgs('explain plan'), queryToRun, callback, timeout=self.timeout)

def execute(self, queries, callback):
def execute(self, queries, callback, stream=False):
queryToRun = ''

for query in self.getOptionsForSgdbCli()['before']:
Expand Down Expand Up @@ -165,7 +165,7 @@ def execute(self, queries, callback):
if Connection.history:
Connection.history.add(queryToRun)

self.Command.createAndRun(self.builArgs(), queryToRun, callback, options={'show_query': self.show_query}, timeout=self.timeout)
self.Command.createAndRun(self.builArgs(), queryToRun, callback, options={'show_query': self.show_query}, timeout=self.timeout, stream=stream)

def builArgs(self, queryName=None):
cliOptions = self.getOptionsForSgdbCli()
Expand Down

0 comments on commit a5e9349

Please sign in to comment.