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

Commit a2b2e11

Browse files
committed
Fixes #19 using option
1 parent 558c108 commit a2b2e11

File tree

5 files changed

+50
-30
lines changed

5 files changed

+50
-30
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ SQLTools will save you (for sure) a lot of time and help you to increase your pr
1515

1616
<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>
1717

18-
<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+GrsVdUb7SXQ2Q9f15PiqNgsgD7l54kvg1VjAHlBRT98yhl73ZxmyVFkVci49K1nqhhB6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h85xes2hyiNRpxDaMx1QwPNNTOMTg2mqKxKqlcgr0Ld2d3e1sHOzhMBpFMRB5C7gU7v9oYhnS/6vHKm7up9Gqur3Ebzo9KEdJ+nmUPoIZV1NBTNSJ4B5AnJGkDuxvsHWqbLDa8ng8IAAAAASUVORK5CYII=" /></a>
1918

2019

2120
## Contributors

SQLTools.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,20 @@ def loadDefaultConnection():
115115
return default
116116

117117

118-
def output(content, panel=None, syntax=None, prependText=None):
118+
def createOutput(panel=None, syntax=None, prependText=None):
119119
# hide previously set command running message (if any)
120120
Window().status_message('')
121121
if not panel:
122122
panel = getOutputPlace(syntax)
123123
if prependText:
124124
panel.run_command('append', {'characters': str(prependText)})
125-
panel.run_command('append', {'characters': content})
126-
panel.set_read_only(True)
125+
126+
def append(outputContent):
127+
panel.set_read_only(False)
128+
panel.run_command('append', {'characters': outputContent})
129+
panel.set_read_only(True)
130+
131+
return append
127132

128133

129134
def toNewTab(content, name="", suffix="SQLTools Saved Query"):
@@ -401,7 +406,7 @@ def cb(index):
401406
prependText = 'Table "{tableName}"\n'.format(tableName=tableName)
402407
return ST.conn.getTableRecords(
403408
tableName,
404-
partial(output, prependText=prependText))
409+
createOutput(prependText=prependText))
405410

406411
ST.selectTable(cb)
407412

@@ -419,7 +424,7 @@ def cb(index):
419424
if index < 0:
420425
return None
421426
Window().status_message(MESSAGE_RUNNING_CMD)
422-
return ST.conn.getTableDescription(ST.tables[index], partial(output, syntax=currentSyntax))
427+
return ST.conn.getTableDescription(ST.tables[index], createOutput(syntax=currentSyntax))
423428

424429
ST.selectTable(cb)
425430

@@ -438,7 +443,7 @@ def cb(index):
438443
return None
439444
Window().status_message(MESSAGE_RUNNING_CMD)
440445
functionName = ST.functions[index].split('(', 1)[0]
441-
return ST.conn.getFunctionDescription(functionName, partial(output, syntax=currentSyntax))
446+
return ST.conn.getFunctionDescription(functionName, createOutput(syntax=currentSyntax))
442447

443448
# get everything until first occurence of "(", e.g. get "function_name"
444449
# from "function_name(int)"
@@ -453,7 +458,7 @@ def run():
453458
return
454459

455460
Window().status_message(MESSAGE_RUNNING_CMD)
456-
ST.conn.explainPlan(getSelection(), output)
461+
ST.conn.explainPlan(getSelection(), createOutput())
457462

458463

459464
class StExecute(WindowCommand):
@@ -464,7 +469,7 @@ def run():
464469
return
465470

466471
Window().status_message(MESSAGE_RUNNING_CMD)
467-
ST.conn.execute(getSelection(), output)
472+
ST.conn.execute(getSelection(), createOutput(), stream=settings.get('use_streams', False))
468473

469474

470475
class StFormat(TextCommand):
@@ -498,7 +503,7 @@ def run():
498503
def cb(index):
499504
if index < 0:
500505
return None
501-
return ST.conn.execute(history.get(index), output)
506+
return ST.conn.execute(history.get(index), createOutput())
502507

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

@@ -534,7 +539,7 @@ def cb(index):
534539
if index < 0:
535540
return None
536541

537-
param2 = output if mode == "run" else options[index][0]
542+
param2 = createOutput() if mode == "run" else options[index][0]
538543
func = ST.conn.execute if mode == "run" else toNewTab
539544
return func(options[index][1], param2)
540545

SQLTools.sublime-settings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"safe_limit" : false,
1111
"show_query" : false,
1212
"expand_to_paragraph" : false,
13+
"use_streams" : false, // use streams for results
1314
/**
1415
* The list of syntax selectors for which the plugin autocompletion will be active.
1516
* An empty list means autocompletion always active.

SQLToolsAPI/Command.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
from .Log import Log
88

99

10-
class Command:
10+
class Command(object):
1111
timeout = 15
1212

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

18+
self.stream = stream
1819
self.args = args
1920
self.callback = callback
2021
self.query = query
@@ -43,6 +44,23 @@ def run(self):
4344
env=os.environ.copy(),
4445
startupinfo=si)
4546

47+
if self.stream:
48+
self.process.stdin.write(self.query.encode())
49+
self.process.stdin.close()
50+
for line in self.process.stdout:
51+
self.callback(line.decode(self.encoding,
52+
'replace').replace('\r', ''))
53+
54+
queryTimerEnd = time.time()
55+
if 'show_query' in self.options and self.options['show_query']:
56+
resultInfo = "/*\n-- Executed querie(s) at {0} took {1:.3f}ms --".format(
57+
str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(queryTimerStart))),
58+
(queryTimerEnd - queryTimerStart))
59+
resultLine = "-" * (len(resultInfo) - 3)
60+
resultString = "{0}\n{1}\n{2}\n{3}\n*/".format(
61+
resultInfo, resultLine, self.query, resultLine)
62+
return self.callback(resultString)
63+
4664
results, errors = self.process.communicate(input=self.query.encode())
4765

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

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

6886
self.callback(resultString)
6987

7088
@staticmethod
71-
def createAndRun(args, query, callback, options=None, timeout=15, silenceErrors=False):
89+
def createAndRun(args, query, callback, options=None, timeout=15, silenceErrors=False, stream=False):
7290
if options is None:
7391
options = {}
7492
command = Command(args, callback, query, options=options,
@@ -78,18 +96,14 @@ def createAndRun(args, query, callback, options=None, timeout=15, silenceErrors=
7896

7997
class ThreadCommand(Command, Thread):
8098
def __init__(self, args, callback, query=None, encoding='utf-8',
81-
options=None, timeout=Command.timeout, silenceErrors=False):
99+
options=None, timeout=Command.timeout, silenceErrors=False, stream=False):
82100
if options is None:
83101
options = {}
84102

85-
self.args = args
86-
self.callback = callback
87-
self.query = query
88-
self.encoding = encoding
89-
self.options = options
90-
self.timeout = timeout
91-
self.silenceErrors = silenceErrors
92-
self.process = None
103+
Command.__init__(self, args, callback, query=query,
104+
encoding=encoding, options=options,
105+
timeout=timeout, silenceErrors=silenceErrors,
106+
stream=stream)
93107
Thread.__init__(self)
94108

95109
def stop(self):
@@ -108,13 +122,14 @@ def stop(self):
108122
pass
109123

110124
@staticmethod
111-
def createAndRun(args, query, callback, options=None, timeout=Command.timeout, silenceErrors=False):
125+
def createAndRun(args, query, callback, options=None,
126+
timeout=Command.timeout, silenceErrors=False, stream=False):
112127
# Don't allow empty dicts or lists as defaults in method signature,
113128
# cfr http://nedbatchelder.com/blog/200806/pylint.html
114129
if options is None:
115130
options = {}
116131
command = ThreadCommand(args, callback, query, options=options,
117-
timeout=timeout, silenceErrors=silenceErrors)
132+
timeout=timeout, silenceErrors=silenceErrors, stream=stream)
118133
command.start()
119134
killTimeout = Timer(command.timeout, command.stop)
120135
killTimeout.start()

SQLToolsAPI/Connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from . import Command as C
88

99

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

137-
def execute(self, queries, callback):
137+
def execute(self, queries, callback, stream=False):
138138
queryToRun = ''
139139

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

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

170170
def builArgs(self, queryName=None):
171171
cliOptions = self.getOptionsForSgdbCli()

0 commit comments

Comments
 (0)