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

Commit 04063b6

Browse files
committed
Change how top level and per-query options work
The new concept of applying `options` is: Top level `options` are applied to all CLI invocations (all queries). In addition to Top Level `options` each named query `options` are appended. New named query `execute` is introduced, which helps to better control which options are applied when the user executes the query using `ST: Execute` or `ST: Execute All File`.
1 parent 1324fbf commit 04063b6

File tree

2 files changed

+65
-51
lines changed

2 files changed

+65
-51
lines changed

SQLTools.sublime-settings

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,36 @@
7474
"PGPASSWORD": "{password}"
7575
},
7676
"queries": {
77-
"desc" : {
77+
"exec": {
78+
"options": []
79+
},
80+
"desc": {
7881
"query": "select '|' || quote_ident(table_schema)||'.'||quote_ident(table_name) ||'|' as tblname from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema') order by table_schema = current_schema() desc, table_schema, table_name",
7982
"options": ["--tuples-only", "--no-psqlrc"]
8083
},
8184
"desc table": {
82-
"query": "\\d+ %s",
83-
"options": ["--no-password"]
85+
"query": "\\d+ {0}",
86+
"options": []
8487
},
8588
"show records": {
8689
"query": "select * from {0} limit {1}",
87-
"options": ["--no-password"]
90+
"options": []
8891
},
8992
"columns": {
9093
"query": "select '|' || quote_ident(table_name) || '.' || quote_ident(column_name) || '|' from information_schema.columns where table_schema not in ('pg_catalog', 'information_schema') order by table_name, ordinal_position",
91-
"options": ["--no-password", "--tuples-only", "--no-psqlrc"]
94+
"options": ["--tuples-only", "--no-psqlrc"]
9295
},
9396
"functions": {
9497
"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 not in ('pg_catalog', 'information_schema')",
95-
"options": ["--no-password", "--tuples-only", "--no-psqlrc"]
98+
"options": ["--tuples-only", "--no-psqlrc"]
9699
},
97100
"desc function": {
98-
"query": "\\sf %s",
99-
"options": ["--no-password"]
101+
"query": "\\sf {0}",
102+
"options": []
100103
},
101104
"explain plan": {
102105
"query": "explain {0};",
103-
"options": ["--no-password"]
106+
"options": []
104107
}
105108
}
106109
},
@@ -130,62 +133,68 @@
130133
"after": [],
131134
"args": "{username}/{password}@\"(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={host})(PORT={port})))(CONNECT_DATA=(SERVICE_NAME={service})))\"",
132135
"queries": {
136+
"exec": {
137+
"options": []
138+
},
133139
"desc" : {
134140
"query": "select concat(concat(concat(concat('|', owner), '.'), table_name), '|') as tbls from all_tables UNION ALL select concat(concat(concat(concat('|', owner), '.'), view_name), '|') as tbls from all_views;",
135-
"options": ["-S"]
141+
"options": []
136142
},
137143
"columns": {
138144
"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 UNION ALL SELECT concat(concat(concat(concat('|', c.table_name), '.'), c.column_name), '|') AS cols FROM all_tab_columns c INNER JOIN all_views t ON c.owner = t.owner AND c.table_name = t.view_name;",
139-
"options": ["-S"]
145+
"options": []
140146
},
141147
"desc table": {
142-
"query": "desc %s;",
143-
"options": ["-S"]
148+
"query": "desc {0};",
149+
"options": []
144150
},
145151
"show records": {
146152
"query": "select * from {0} WHERE ROWNUM <= {1};",
147-
"options": ["-S"]
153+
"options": []
148154
},
149155
"explain plan": {
150156
"query": "explain plan for {0};\nselect plan_table_output from table(dbms_xplan.display());",
151-
"options": ["-S"]
157+
"options": []
152158
}
153159
}
154160
},
155161
"mysql": {
156-
"options": ["-f", "--table", "--default-character-set=utf8"],
162+
"options": ["--default-character-set=utf8"],
157163
"before": [],
158164
"after": [],
159165
"args": "-h{host} -P{port} -u\"{username}\" -D\"{database}\"",
160166
"args_optional": ["--login-path=\"{login-path}\"", "--defaults-extra-file=\"{defaults-extra-file}\"", "-p\"{password}\""],
161167
"queries": {
168+
"exec": {
169+
"options": ["--table", "-f"]
170+
},
162171
"desc" : {
163172
"query": "select concat('|', case when table_schema REGEXP '[^0-9a-zA-Z$_]' then concat('`',table_schema,'`') else table_schema end, '.', case when table_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',table_name,'`') else table_name end, '|') from information_schema.tables where table_schema = database() order by table_name;",
164-
"options": ["-f", "--silent", "--raw", "--default-character-set=utf8"]
173+
"options": ["--silent", "--raw"]
165174
},
166175
"desc table": {
167-
"query": "desc %s",
168-
"options": ["-f", "--table", "--default-character-set=utf8"]
176+
"query": "desc {0}",
177+
"options": ["--table"]
169178
},
170179
"show records": {
171180
"query": "select * from {0} limit {1}",
172-
"options": ["-f", "--table", "--default-character-set=utf8"]
181+
"options": ["--table"]
173182
},
174183
"columns": {
175184
"query": "select concat('|', case when table_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',table_name,'`') else table_name end, '.', case when column_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',column_name,'`') else column_name end, '|') from information_schema.columns where table_schema = database() order by table_name, ordinal_position;",
176-
"options": ["-f", "--silent", "--raw", "--default-character-set=utf8"]
185+
"options": ["--silent", "--raw"]
177186
},
178187
"functions": {
179188
"query": "select concat('|', case when routine_schema REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_schema,'`') else routine_schema end, '.', case when routine_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_name,'`') else routine_name end, '()', '|') from information_schema.routines where routine_schema = database();",
180-
"options": ["-f", "--silent", "--raw", "--default-character-set=utf8"]
189+
"options": ["--silent", "--raw"]
181190
},
182191
"desc function": {
183-
"query": "select routine_definition from information_schema.routines where concat(case when routine_schema REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_schema,'`') else routine_schema end, '.', case when routine_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_name,'`') else routine_name end) = '%s';",
184-
"options": ["-f", "--silent", "--raw", "--default-character-set=utf8"]
192+
"query": "select routine_definition from information_schema.routines where concat(case when routine_schema REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_schema,'`') else routine_schema end, '.', case when routine_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_name,'`') else routine_name end) = '{0}';",
193+
"options": ["--silent", "--raw"]
185194
},
186195
"explain plan": {
187196
"query": "explain {0};",
188-
"options": ["-f", "--table", "--default-character-set=utf8"]
197+
"options": ["--table"]
189198
}
190199
}
191200
},
@@ -196,6 +205,9 @@
196205
"args": "-d \"{database}\"",
197206
"args_optional": ["-S \"{host},{port}\"", "-S \"{host}\\{instance}\"", "-U \"{username}\"", "-P \"{password}\""],
198207
"queries": {
208+
"exec": {
209+
"options": []
210+
},
199211
"desc": {
200212
"query": "set nocount on; select concat(table_schema, '.', table_name) from information_schema.tables order by table_name;",
201213
"options": ["-h", "-1"]
@@ -205,7 +217,7 @@
205217
"options": ["-h", "-1"]
206218
},
207219
"desc table": {
208-
"query": "exec sp_help \"%s\";",
220+
"query": "exec sp_help \"{0}\";",
209221
"options": []
210222
},
211223
"show records": {
@@ -220,6 +232,9 @@
220232
"after": [],
221233
"args": "-h {host} -p {port} -U \"{username}\" -w \"{password}\" -d \"{database}\"",
222234
"queries": {
235+
"exec": {
236+
"options": []
237+
},
223238
"desc" : {
224239
"query": "select '|' || table_schema || '.' || table_name || '|' as tblname from v_catalog.tables where is_system_table = false",
225240
"options": ["--tuples-only", "--no-vsqlrc"]
@@ -229,7 +244,7 @@
229244
"options": ["--tuples-only", "--no-vsqlrc"]
230245
},
231246
"desc table": {
232-
"query": "\\d %s",
247+
"query": "\\d {0}",
233248
"options": []
234249
},
235250
"show records": {
@@ -244,10 +259,14 @@
244259
},
245260
"sqsh": {
246261
"options": [],
247-
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l\""],
262+
"before": [],
248263
"after": [],
249264
"args": "-S {host}:{port} -U\"{username}\" -P\"{password}\" -D{database}",
250265
"queries": {
266+
"exec": {
267+
"options": [],
268+
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l\""]
269+
},
251270
"desc": {
252271
"query": "select concat(table_schema, '.', table_name) from information_schema.tables order by table_name;",
253272
"options": [],
@@ -259,29 +278,32 @@
259278
"before" :["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
260279
},
261280
"desc table": {
262-
"query": "exec sp_columns \"%s\";",
281+
"query": "exec sp_columns \"{0}\";",
263282
"options": [],
264283
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
265284
},
266285
"show records": {
267286
"query": "select top {1} * from \"{0}\";",
268287
"options": [],
269-
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
288+
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l\""]
270289
}
271290
}
272291
},
273292
"sqlite": {
274-
"options": ["-column", "-header"],
293+
"options": [],
275294
"before": [],
276295
"after": [],
277296
"args": "\"{database}\"",
278297
"queries": {
298+
"exec": {
299+
"options": ["-column", "-header"]
300+
},
279301
"desc" : {
280302
"query": ".headers off\nSELECT '|' || name || '|' FROM sqlite_master WHERE type='table';",
281303
"options": ["-noheader"]
282304
},
283305
"desc table": {
284-
"query": ".schema \"%s\"",
306+
"query": ".schema \"{0}\"",
285307
"options": ["-column", "-header"]
286308
},
287309
"show records": {
@@ -300,16 +322,19 @@
300322
"after": [],
301323
"args": "-u \"{username}\" -p \"{password}\" \"{host}/{port}:{database}\"",
302324
"queries": {
325+
"exec": {
326+
"options": []
327+
},
303328
"desc" : {
304329
"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);",
305330
"options": []
306331
},
307332
"desc table": {
308-
"query": "show table \"%s\";",
333+
"query": "show table \"{0}\";",
309334
"options": []
310335
},
311336
"show records": {
312-
"query": "select first 100 * from \"%s\";",
337+
"query": "select first {1} * from \"{0}\";",
313338
"options": []
314339
}
315340
}

SQLToolsAPI/Connection.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ def buildNamedQuery(self, queryName, queries):
196196

197197
cliOptions = self.getOptionsForSgdbCli()
198198
beforeCli = cliOptions.get('before')
199-
beforeQuery = cliOptions.get('queries', {}).get(queryName, {}).get('before')
200199
afterCli = cliOptions.get('after')
200+
beforeQuery = cliOptions.get('queries', {}).get(queryName, {}).get('before')
201201
afterQuery = cliOptions.get('queries', {}).get(queryName, {}).get('after')
202202

203203
# sometimes we preprocess the raw queries from user, in that case we already have a list
@@ -216,7 +216,8 @@ def buildNamedQuery(self, queryName, queries):
216216
if afterQuery is not None:
217217
builtQueries.extend(afterQuery)
218218

219-
print(builtQueries)
219+
# remove empty list items
220+
builtQueries = list(filter(None, builtQueries))
220221

221222
return '\n'.join(builtQueries)
222223

@@ -238,28 +239,16 @@ def buildArgs(self, queryName=None):
238239
args = args + shlex.split(formattedItem)
239240

240241
# append generic options
241-
# options = cliOptions.get('options', None)
242-
# if options:
243-
# args = args + options
242+
options = cliOptions.get('options', None)
243+
if options:
244+
args = args + options
244245

245246
# append query specific options (if present)
246-
# if queryName:
247-
# queryOptions = cliOptions.get('queries', {}).get(queryName, {}).get.('options')
248-
# if queryOptions:
249-
# if len(queryOptions) > 0:
250-
# args = args + queryOptions
251-
252-
# append query specific options
253247
if queryName:
254248
queryOptions = cliOptions.get('queries', {}).get(queryName, {}).get('options')
255249
if queryOptions:
256250
if len(queryOptions) > 0:
257251
args = args + queryOptions
258-
else:
259-
# append generic options (only if not custom query)
260-
options = cliOptions.get('options', None)
261-
if options:
262-
args = args + options
263252

264253
# append main args - could be a single value or a list
265254
mainArgs = cliOptions['args']

0 commit comments

Comments
 (0)