forked from mtxr/SublimeText-SQLTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLToolsModels.py
More file actions
350 lines (271 loc) · 11.8 KB
/
SQLToolsModels.py
File metadata and controls
350 lines (271 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import sublime, os, tempfile, threading, signal, shlex, subprocess, sys
sys.path.append(os.path.dirname(__file__))
if sys.version_info >= (3, 0):
import sqlparse3 as sqlparse
else:
import sqlparse2 as sqlparse
class Const:
SETTINGS_EXTENSION = "sublime-settings"
SETTINGS_FILENAME = "SQLTools.{0}".format(SETTINGS_EXTENSION)
SGDB_FILENAME = "SQLToolsSGBD.{0}".format(SETTINGS_EXTENSION)
CONNECTIONS_FILENAME = "SQLToolsConnections.{0}".format(SETTINGS_EXTENSION)
USER_QUERIES_FILENAME = "SQLToolsSavedQueries.{0}".format(SETTINGS_EXTENSION)
VERSION = "v0.3.1"
pass
class Log:
@staticmethod
def debug(message):
if not sublime.load_settings(Const.SETTINGS_FILENAME).get('debug', False):
return
print ("SQLTools %s: %s" % (Const.VERSION, message))
class Settings:
@staticmethod
def getConnections():
connections = {}
options = sublime.load_settings(Const.CONNECTIONS_FILENAME).get('connections')
for connection in options:
connections[connection] = Connection(connection, options[connection])
return connections
@staticmethod
def userFolder():
return '{0}/User'.format(sublime.packages_path())
class Storage:
savedQueries = None
savedQueriesArray = None
selectedQuery = ''
@staticmethod
def getSavedQueries():
Storage.savedQueries = sublime.load_settings(Const.USER_QUERIES_FILENAME)
Storage.savedQueriesArray = Storage.savedQueries.get('queries', {})
return Storage.savedQueries
@staticmethod
def flushSavedQueries():
if not Storage.savedQueries:
Storage.getSavedQueries()
return sublime.save_settings(Const.USER_QUERIES_FILENAME)
@staticmethod
def promptQueryAlias():
Storage.selectedQuery = Selection.get()
Window().show_input_panel('Query alias', '', Storage.saveQuery, None, None)
@staticmethod
def saveQuery(alias):
if len(alias) <= 0:
return
Storage.getSavedQueries()
Storage.savedQueriesArray[alias] = '\n'.join(Storage.selectedQuery)
Storage.savedQueries.set('queries', Storage.savedQueriesArray)
Storage.flushSavedQueries()
@staticmethod
def removeQuery(alias):
if len(alias) <= 0:
return
Storage.getSavedQueries()
Storage.savedQueriesArray.pop(alias)
Storage.savedQueries.set('queries', Storage.savedQueriesArray)
Storage.flushSavedQueries()
@staticmethod
def getSavedQuery(alias):
if len(alias) <= 0:
return
Storage.getSavedQueries()
return Storage.savedQueriesArray[alias]
class Connection:
def __init__(self, name, options):
self.cli = sublime.load_settings(Const.SETTINGS_FILENAME).get('cli')[options['type']]
self.rowsLimit = sublime.load_settings(Const.SETTINGS_FILENAME).get('show_records').get('limit', 50)
self.options = options
self.name = name
self.type = options['type']
self.host = options['host']
self.port = options['port']
self.username = options['username']
self.database = options['database']
if 'encoding' in options:
self.encoding = options['encoding']
if 'password' in options:
self.password = options['password']
if 'service' in options:
self.service = options['service']
def __str__(self):
return self.name
def _info(self):
return 'DB: {0}, Connection: {1}@{2}:{3}'.format(self.database, self.username, self.host, self.port)
def _quickPanel(self):
return [self.name, self._info()]
@staticmethod
def killCommandAfterTimeout(command):
timeout = sublime.load_settings(Const.SETTINGS_FILENAME).get('thread_timeout', 5000)
sublime.set_timeout(command.stop, timeout)
@staticmethod
def loadDefaultConnectionName():
default = sublime.load_settings(Const.CONNECTIONS_FILENAME).get('default', False)
if not default:
return
Log.debug('Default database set to ' + default + '. Loading options and auto complete.')
return default
def getTables(self, callback):
query = self.getOptionsForSgdbCli()['queries']['desc']['query']
self.runCommand(self.builArgs('desc'), query, lambda result: Utils.getResultAsList(result, callback))
def getColumns(self, callback):
try:
query = self.getOptionsForSgdbCli()['queries']['columns']['query']
self.runCommand(self.builArgs('columns'), query, lambda result: Utils.getResultAsList(result, callback))
except Exception:
pass
def getFunctions(self, callback):
try:
query = self.getOptionsForSgdbCli()['queries']['functions']['query']
self.runCommand(self.builArgs('functions'), query, lambda result: Utils.getResultAsList(result, callback))
except Exception:
pass
def getTableRecords(self, tableName, callback):
query = self.getOptionsForSgdbCli()['queries']['show records']['query'].format(tableName, self.rowsLimit)
self.runCommand(self.builArgs('show records'), query, lambda result: callback(result))
def getTableDescription(self, tableName, callback):
query = self.getOptionsForSgdbCli()['queries']['desc table']['query'] % tableName
self.runCommand(self.builArgs('desc table'), query, lambda result: callback(result))
def getFunctionDescription(self, functionName, callback):
query = self.getOptionsForSgdbCli()['queries']['desc function']['query'] % functionName
self.runCommand(self.builArgs('desc function'), query, lambda result: callback(result))
def execute(self, queries, callback):
queryToRun = ''
for query in self.getOptionsForSgdbCli()['before']:
queryToRun += query + "\n"
if type(queries) is str:
queries = [queries];
for query in queries:
queryToRun += query + "\n"
queryToRun = queryToRun.rstrip('\n')
windowVars = sublime.active_window().extract_variables()
if type(windowVars) is dict and 'file_extension' in windowVars:
windowVars = windowVars['file_extension'].lstrip()
unescapeExtension = sublime.load_settings(Const.SETTINGS_FILENAME).get('unescape_quotes')
if windowVars in unescapeExtension:
queryToRun = queryToRun.replace("\\\"", "\"").replace("\\\'", "\'")
Log.debug("Query: " + queryToRun)
History.add(queryToRun)
self.runCommand(self.builArgs(), queryToRun, lambda result: callback(result))
def runCommand(self, args, query, callback):
command = Command(args, callback, query)
command.start()
Connection.killCommandAfterTimeout(command)
def builArgs(self, queryName=None):
cliOptions = self.getOptionsForSgdbCli()
args = [self.cli]
if len(cliOptions['options']) > 0:
args = args + cliOptions['options']
if queryName and len(cliOptions['queries'][queryName]['options']) > 0:
args = args + cliOptions['queries'][queryName]['options']
argsType = 'string'
if type(cliOptions['args']) is list:
argsType = 'list'
cliOptions['args'] = ' '.join(cliOptions['args'])
Log.debug('Usgin cli args ' + ' '.join(args + shlex.split(cliOptions['args'].format(**self.options))))
return args + shlex.split(cliOptions['args'].format(**self.options))
def getOptionsForSgdbCli(self):
return sublime.load_settings(Const.SETTINGS_FILENAME).get('cli_options')[self.type]
class Selection:
@staticmethod
def get():
text = []
if View().sel():
for region in View().sel():
if region.empty():
text.append(View().substr(View().line(region)))
else:
text.append(View().substr(region))
return text
@staticmethod
def formatSql(edit):
for region in View().sel():
if region.empty():
selectedRegion = sublime.Region(0, View().size())
selection = View().substr(selectedRegion)
View().replace(edit, selectedRegion, Utils.formatSql(selection))
View().set_syntax_file("Packages/SQL/SQL.tmLanguage")
else:
text = View().substr(region)
View().replace(edit, region, Utils.formatSql(text))
class Command(threading.Thread):
def __init__(self, args, callback, query=None, encoding='utf-8'):
self.query = query
self.process = None
self.args = args
self.encoding = encoding
self.callback = callback
threading.Thread.__init__(self)
def run(self):
if not self.query:
return
sublime.status_message(' ST: running SQL command')
self.tmp = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.sql')
self.tmp.write(self.query)
self.tmp.close()
self.args = map(str, self.args)
si = None
if os.name == 'nt':
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
self.process = subprocess.Popen(self.args, stdout=subprocess.PIPE,stderr=subprocess.PIPE, stdin=open(self.tmp.name), env=os.environ.copy(), startupinfo=si)
results, errors = self.process.communicate()
if errors:
self.callback(errors.decode(self.encoding, 'replace').replace('\r', ''))
return
self.callback(results.decode(self.encoding, 'replace').replace('\r', ''))
def stop(self):
if not self.process:
return
try:
os.kill(self.process.pid, signal.SIGKILL)
self.process = None
sublime.message_dialog("Your command is taking too long to run. Try to run outside using your database cli.")
Log.debug("Your command is taking too long to run. Process killed")
except Exception:
pass
if self.tmp and os.path.exists(self.tmp.name):
os.unlink(self.tmp.name)
class Utils:
@staticmethod
def getResultAsList(results, callback=None):
resultList = []
for result in results.splitlines():
try:
resultList.append(result.split('|')[1].strip())
except IndexError:
pass
if callback:
callback(resultList)
return resultList
@staticmethod
def formatSql(raw):
settings = sublime.load_settings(Const.SETTINGS_FILENAME).get("format")
try:
result = sqlparse.format(raw,
keyword_case = settings.get("keyword_case"),
identifier_case = settings.get("identifier_case"),
strip_comments = settings.get("strip_comments"),
indent_tabs = settings.get("indent_tabs"),
indent_width = settings.get("indent_width"),
reindent = settings.get("reindent")
)
if View().settings().get('ensure_newline_at_eof_on_save'):
result += "\n"
return result
except Exception as e:
return None
class History:
queries = []
@staticmethod
def add(query):
if len(History.queries) >= sublime.load_settings(Const.SETTINGS_FILENAME).get('history_size', 100):
History.queries.pop(0)
History.queries.insert(0, query)
@staticmethod
def get(index):
if index < 0 or index > (len(History.queries) - 1):
raise "No query selected"
return History.queries[index]
def Window():
return sublime.active_window()
def View():
return Window().active_view()