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

Commit bbc8b37

Browse files
committed
add support for optional args (for MySQL auth)
1 parent 65c9b8d commit bbc8b37

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

SQLTools.sublime-settings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@
153153
"mysql": {
154154
"options": ["-f", "--table"],
155155
"before": [],
156-
"args": "-h{host} -P{port} -u\"{username}\" -p\"{password}\" -D\"{database}\"",
156+
"args": "-h{host} -P{port} -u\"{username}\" -D\"{database}\"",
157+
"args_optional": ["--defaults-extra-file=\"{defaults_extra_file}\"", "--defaults-file=\"{defaults-file}\"", "-p\"{password}\""],
157158
"queries": {
158159
"desc" : {
159160
"query": "select concat(table_schema, '.', table_name) from information_schema.tables where table_schema = database() order by table_name;",

SQLToolsAPI/Connection.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,57 @@ def builArgs(self, queryName=None):
169169
cliOptions = self.getOptionsForSgdbCli()
170170
args = [self.cli]
171171

172-
if len(cliOptions['options']) > 0:
173-
args = args + cliOptions['options']
174-
175-
if queryName and len(cliOptions['queries'][queryName]['options']) > 0:
176-
args = args + cliOptions['queries'][queryName]['options']
177-
178-
if isinstance(cliOptions['args'], list):
179-
cliOptions['args'] = ' '.join(cliOptions['args'])
180-
181-
cliOptions = cliOptions['args'].format(**self.options)
182-
args = args + shlex.split(cliOptions)
172+
# append otional args (if any) - could be a single value or a list
173+
optionalArgs = cliOptions.get('args_optional')
174+
if optionalArgs: # only if we have optional args
175+
formattedOptArgList = []
176+
if isinstance(optionalArgs, list):
177+
for item in optionalArgs:
178+
formattedItem = self.formatOptionalArgument(item, self.options)
179+
if formattedItem:
180+
args = args + shlex.split(formattedItem)
181+
else:
182+
formattedItem = self.formatOptionalArgument(optionalArgs, self.options)
183+
if formattedItem:
184+
args = args + shlex.split(formattedItem)
185+
186+
# append options (args without values)
187+
options = cliOptions.get('options', None)
188+
if options:
189+
args = args + options
190+
191+
# append query specific options
192+
if queryName:
193+
queryOptions = cliOptions['queries'][queryName]['options']
194+
if len(queryOptions) > 0:
195+
args = args + queryOptions
196+
197+
# append main args - could be a single value or a list
198+
mainArgs = cliOptions['args']
199+
if isinstance(mainArgs, list):
200+
mainArgs = ' '.join(mainArgs)
201+
202+
mainArgs = mainArgs.format(**self.options)
203+
args = args + shlex.split(mainArgs)
183204

184205
Log('Using cli args ' + ' '.join(args))
185206
return args
186207

187208
def getOptionsForSgdbCli(self):
188209
return self.settings.get('cli_options', {}).get(self.type)
189210

211+
@staticmethod
212+
def formatOptionalArgument(argument, formatOptions):
213+
try:
214+
formattedArg = argument.format(**formatOptions)
215+
except (KeyError, IndexError):
216+
print("caught exception")
217+
return None
218+
219+
if argument == formattedArg: # string not changed after format
220+
return None
221+
return formattedArg
222+
190223
@staticmethod
191224
def setTimeout(timeout):
192225
Connection.timeout = timeout

0 commit comments

Comments
 (0)