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

Commit 366ac44

Browse files
committed
Change cli options behaviour, review ident queries
Few things fixed/improved: * base "options" are not applied for named queries - list those explicitly if they are needed * reviewed built-in queries and other commands * removed unused 'format' key in query settings * list of identifiers are cleared when connection is selected * Command has new param "silenceErrors" to prevent warnings and errors to show up in list of identifiers * minor code formatting
1 parent 7896367 commit 366ac44

File tree

4 files changed

+69
-93
lines changed

4 files changed

+69
-93
lines changed

SQLTools.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ def getCurrentSyntax():
196196
class ST(EventListener):
197197
conn = None
198198
tables = []
199-
functions = []
200199
columns = []
200+
functions = []
201201
connectionList = None
202202
autoCompleteList = []
203203

@@ -249,6 +249,10 @@ def setConnection(index, tablesCallback=None, columnsCallback=None, functionsCal
249249
connListNames = list(ST.connectionList.keys())
250250
connListNames.sort()
251251
ST.conn = ST.connectionList.get(connListNames[index])
252+
# clear list of identifiers in case connection is changed
253+
ST.tables = []
254+
ST.columns = []
255+
ST.functions = []
252256

253257
ST.loadConnectionData(tablesCallback, columnsCallback, functionsCallback)
254258

@@ -338,7 +342,7 @@ def on_query_completions(view, prefix, locations):
338342
# determine desired keywords case from settings
339343
formatSettings = settings.get('format', {})
340344
keywordCase = formatSettings.get('keyword_case', 'upper')
341-
uppercaseKeywords = (keywordCase.lower() == 'upper')
345+
uppercaseKeywords = keywordCase.lower().startswith('upper')
342346

343347
inhibit = False
344348
completion = Completion(uppercaseKeywords, ST.tables, ST.columns, ST.functions)

SQLTools.sublime-settings

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,31 @@
6363
"queries": {
6464
"desc" : {
6565
"query": "select '|' || quote_ident(table_schema)||'.'||quote_ident(table_name) ||'|' as tblname from information_schema.tables where table_schema = any(current_schemas(false)) and table_schema not in ('pg_catalog', 'information_schema') order by table_schema = current_schema() desc, table_schema, table_name",
66-
"options": ["--tuples-only", "--no-psqlrc"],
67-
"format" : "|%s|"
66+
"options": ["--tuples-only", "--no-psqlrc"]
6867
},
6968
"desc table": {
7069
"query": "\\d+ %s",
71-
"options": [],
72-
"format" : "|%s|"
70+
"options": ["--no-password"]
7371
},
7472
"show records": {
7573
"query": "select * from {0} limit {1}",
76-
"options": [],
77-
"format" : "|%s|"
74+
"options": ["--no-password"]
7875
},
7976
"columns": {
8077
"query": "select '|' || quote_ident(table_name) || '.' || quote_ident(column_name) || '|' from information_schema.columns where table_schema = any(current_schemas(false)) and table_schema not in ('pg_catalog', 'information_schema') order by table_name, ordinal_position",
81-
"options": ["--tuples-only", "--no-psqlrc"],
82-
"format" : "|%s|"
78+
"options": ["--no-password", "--tuples-only", "--no-psqlrc"]
8379
},
8480
"functions": {
8581
"query": "select '|' || quote_ident(n.nspname)||'.'||quote_ident(f.proname) || '(' || pg_get_function_identity_arguments(f.oid) || ')' || '|' as funname from pg_catalog.pg_proc as f inner join pg_catalog.pg_namespace as n on n.oid = f.pronamespace where f.proisagg = false and n.nspname = any(current_schemas(false)) and n.nspname not in ('pg_catalog', 'information_schema')",
86-
"options": ["--tuples-only", "--no-psqlrc"],
87-
"format" : "|%s|"
82+
"options": ["--no-password", "--tuples-only", "--no-psqlrc"]
8883
},
8984
"desc function": {
9085
"query": "\\sf %s",
91-
"options": [],
92-
"format" : "|%s|"
86+
"options": ["--no-password"]
9387
},
9488
"explain plan": {
9589
"query": "explain {0};",
96-
"options": [],
97-
"format" : "|%s|"
90+
"options": ["--no-password"]
9891
}
9992
}
10093
},
@@ -125,28 +118,23 @@
125118
"queries": {
126119
"desc" : {
127120
"query": "select concat(concat(concat(concat('|', owner), '.'), table_name), '|') as tbls from all_tables where owner = sys_context('USERENV', 'CURRENT_SCHEMA');",
128-
"options": ["-S"],
129-
"format" : "|%s|"
121+
"options": ["-S"]
130122
},
131123
"columns": {
132124
"query": "select concat(concat(concat(concat('|', c.table_name), '.'), c.column_name), '|') as cols from all_tab_columns c inner join all_tables t ON c.owner = t.owner and c.table_name = t.table_name where c.owner = sys_context('USERENV', 'CURRENT_SCHEMA');",
133-
"options": ["-S"],
134-
"format" : "|%s|"
125+
"options": ["-S"]
135126
},
136127
"desc table": {
137128
"query": "desc %s;",
138-
"options": ["-S"],
139-
"format" : "|%s|"
129+
"options": ["-S"]
140130
},
141131
"show records": {
142132
"query": "select * from {0} WHERE ROWNUM <= {1};",
143-
"options": ["-S"],
144-
"format" : "|%s|"
133+
"options": ["-S"]
145134
},
146135
"explain plan": {
147136
"query": "explain plan for {0};\nselect plan_table_output from table(dbms_xplan.display());",
148-
"options": ["-S"],
149-
"format" : "|%s|"
137+
"options": ["-S"]
150138
}
151139
}
152140
},
@@ -157,34 +145,28 @@
157145
"args_optional": ["--login-path=\"{login-path}\"", "--defaults-extra-file=\"{defaults-extra-file}\"", "-p\"{password}\""],
158146
"queries": {
159147
"desc" : {
160-
"query": "select concat(table_schema, '.', table_name) from information_schema.tables where table_schema = database() order by table_name;",
161-
"options": ["-f", "--table", "--skip-column-names"],
162-
"format" : "|%s|"
148+
"query": "select concat('|', table_schema, '.', table_name, '|') from information_schema.tables where table_schema = database() order by table_name;",
149+
"options": ["-f", "--silent", "--raw"]
163150
},
164151
"desc table": {
165-
"query": "desc `%s`",
166-
"options": ["-f", "--table"],
167-
"format" : "|%s|"
152+
"query": "desc %s",
153+
"options": ["-f", "--table"]
168154
},
169155
"show records": {
170-
"query": "select * from `{0}` limit {1}",
171-
"options": ["-f", "--table"],
172-
"format" : "|%s|"
156+
"query": "select * from {0} limit {1}",
157+
"options": ["-f", "--table"]
173158
},
174159
"columns": {
175-
"query": "select concat(table_name, '.', column_name) from information_schema.columns where table_schema = database() order by table_name, ordinal_position;",
176-
"options": ["-f", "--table", "--skip-column-names"],
177-
"format" : "|%s|"
160+
"query": "select concat('|', table_name, '.', column_name, '|') from information_schema.columns where table_schema = database() order by table_name, ordinal_position;",
161+
"options": ["-f", "--silent", "--raw"]
178162
},
179163
"functions": {
180-
"query": "select concat(routine_schema, '.', routine_name) from information_schema.routines where routine_schema = database();",
181-
"options": ["-f", "--table", "--skip-column-names"],
182-
"format" : "|%s|"
164+
"query": "select concat('|', routine_schema, '.', routine_name, '|') from information_schema.routines where routine_schema = database();",
165+
"options": ["-f", "--silent", "--raw"]
183166
},
184167
"explain plan": {
185168
"query": "explain {0};",
186-
"options": ["-f", "--table"],
187-
"format" : "|%s|"
169+
"options": ["-f", "--table"]
188170
}
189171
}
190172
},
@@ -195,28 +177,23 @@
195177
"queries": {
196178
"desc" : {
197179
"query": "select '|' || table_schema || '.' || table_name || '|' as tblname from v_catalog.tables where is_system_table = false",
198-
"options": ["--tuples-only", "--no-vsqlrc"],
199-
"format" : "|%s|"
180+
"options": ["--tuples-only", "--no-vsqlrc"]
200181
},
201182
"columns": {
202183
"query": "select '|' || table_name || '.' || column_name || '|' as tblname from v_catalog.columns where is_system_table = false order by table_name, ordinal_position",
203-
"options": ["--tuples-only", "--no-vsqlrc"],
204-
"format" : "|%s|"
184+
"options": ["--tuples-only", "--no-vsqlrc"]
205185
},
206186
"desc table": {
207187
"query": "\\d %s",
208-
"options": [],
209-
"format" : "|%s|"
188+
"options": []
210189
},
211190
"show records": {
212191
"query": "select * from {0} limit {1}",
213-
"options": [],
214-
"format" : "|%s|"
192+
"options": []
215193
},
216194
"explain plan": {
217195
"query": "explain {0};",
218-
"options": [],
219-
"format" : "|%s|"
196+
"options": []
220197
}
221198
}
222199
},
@@ -227,54 +204,46 @@
227204
"queries": {
228205
"desc": {
229206
"query": "select concat(table_schema, '.', table_name) from information_schema.tables order by table_name;",
230-
"before" :["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""],
231207
"options": [],
232-
"format": "|%s|"
208+
"before" :["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
233209
},
234210
"columns": {
235211
"query": "select concat(table_name, '.', column_name) from information_schema.columns order by table_name, ordinal_position;",
236-
"before" :["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""],
237212
"options": [],
238-
"format": "|%s|"
213+
"before" :["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
239214
},
240215
"desc table": {
241216
"query": "exec sp_columns \"%s\";",
242217
"options": [],
243-
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""],
244-
"format": "|%s|"
218+
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
245219
},
246220
"show records": {
247221
"query": "select top {1} * from \"{0}\";",
248222
"options": [],
249-
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""],
250-
"format": "|%s|"
223+
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
251224
}
252225
}
253226
},
254227
"sqlite": {
255-
"options": ["-column", "-noheader"],
256-
"before": [".headers on"],
228+
"options": ["-column", "-header"],
229+
"before": [],
257230
"args": "\"{database}\"",
258231
"queries": {
259232
"desc" : {
260233
"query": ".headers off\nSELECT '|' || name || '|' FROM sqlite_master WHERE type='table';",
261-
"options": [],
262-
"format" : "|%s|"
234+
"options": ["-noheader"]
263235
},
264236
"desc table": {
265237
"query": ".schema \"%s\"",
266-
"options": ["-column"],
267-
"format" : "%s"
238+
"options": ["-column", "-header"]
268239
},
269240
"show records": {
270241
"query": "select * from \"{0}\" limit {1};",
271-
"options": ["-column"],
272-
"format" : "|%s|"
242+
"options": ["-column", "-header"]
273243
},
274244
"explain plan": {
275245
"query": "EXPLAIN QUERY PLAN {0};",
276-
"options": ["-column"],
277-
"format" : "|%s|"
246+
"options": ["-column", "-header"]
278247
}
279248
}
280249
},
@@ -285,18 +254,15 @@
285254
"queries": {
286255
"desc" : {
287256
"query": "select '|' || rdb$relation_name || '|' from rdb$relations where rdb$view_blr is null and (rdb$system_flag is null or rdb$system_flag = 0);",
288-
"options": [],
289-
"format" : "|%s|"
257+
"options": []
290258
},
291259
"desc table": {
292260
"query": "show table \"%s\";",
293-
"options": [],
294-
"format" : "|%s|"
261+
"options": []
295262
},
296263
"show records": {
297264
"query": "select first 100 * from \"%s\";",
298-
"options": [],
299-
"format" : "|%s|"
265+
"options": []
300266
}
301267
}
302268
}

SQLToolsAPI/Command.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Command:
1111
timeout = 15
1212

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

@@ -21,6 +21,7 @@ def __init__(self, args, callback, query=None, encoding='utf-8',
2121
self.encoding = encoding
2222
self.options = options
2323
self.timeout = timeout
24+
self.silenceErrors = silenceErrors
2425
self.process = None
2526

2627
def run(self):
@@ -52,7 +53,7 @@ def run(self):
5253
resultString += results.decode(self.encoding,
5354
'replace').replace('\r', '')
5455

55-
if errors:
56+
if errors and not self.silenceErrors:
5657
resultString += errors.decode(self.encoding,
5758
'replace').replace('\r', '')
5859

@@ -67,16 +68,17 @@ def run(self):
6768
self.callback(resultString)
6869

6970
@staticmethod
70-
def createAndRun(args, query, callback, options=None, timeout=15):
71+
def createAndRun(args, query, callback, options=None, timeout=15, silenceErrors=False):
7172
if options is None:
7273
options = {}
73-
command = Command(args, callback, query, options=options, timeout=timeout)
74+
command = Command(args, callback, query, options=options,
75+
timeout=timeout, silenceErrors=silenceErrors)
7476
command.run()
7577

7678

7779
class ThreadCommand(Command, Thread):
7880
def __init__(self, args, callback, query=None, encoding='utf-8',
79-
options=None, timeout=Command.timeout):
81+
options=None, timeout=Command.timeout, silenceErrors=False):
8082
if options is None:
8183
options = {}
8284

@@ -86,6 +88,7 @@ def __init__(self, args, callback, query=None, encoding='utf-8',
8688
self.encoding = encoding
8789
self.options = options
8890
self.timeout = timeout
91+
self.silenceErrors = silenceErrors
8992
self.process = None
9093
Thread.__init__(self)
9194

@@ -105,12 +108,13 @@ def stop(self):
105108
pass
106109

107110
@staticmethod
108-
def createAndRun(args, query, callback, options=None, timeout=Command.timeout):
111+
def createAndRun(args, query, callback, options=None, timeout=Command.timeout, silenceErrors=False):
109112
# Don't allow empty dicts or lists as defaults in method signature,
110113
# cfr http://nedbatchelder.com/blog/200806/pylint.html
111114
if options is None:
112115
options = {}
113-
command = ThreadCommand(args, callback, query, options=options, timeout=timeout)
116+
command = ThreadCommand(args, callback, query, options=options,
117+
timeout=timeout, silenceErrors=silenceErrors)
114118
command.start()
115119
killTimeout = Timer(command.timeout, command.stop)
116120
killTimeout.start()

0 commit comments

Comments
 (0)