1212}
1313
1414dbms_defaults = {
15- 'MSSQL' : {
16- 'shell' : 'mssql-cli' ,
17- 'shell-windows' : 'mssql-cli.bat' ,
18- 'legacy' : 'sqlcmd'
19- },
20-
21- 'MySQL' : {
22- 'shell' : 'mycli' ,
23- 'legacy' : 'mysql'
24- },
25-
26- 'Oracle' : {
27- 'shell' : 'sql' ,
28- 'shell-windows' : 'sql.exe' ,
29- 'legacy' : 'sqlplus'
30- },
31-
32- 'PostgreSQL' : {
33- 'shell' : 'pgcli' ,
34- 'legacy' : 'psql'
35- },
36-
37- 'SQLite' : {
38- 'shell' : 'litecli' ,
39- 'legacy' : 'sqlite3'
40- }
15+ 'MSSQL' : {'shell' : 'mssql-cli' , 'shell-windows' : 'mssql-cli.bat' , 'legacy' : 'sqlcmd' },
16+ 'MySQL' : {'shell' : 'mycli' , 'legacy' : 'mysql' },
17+ 'Oracle' : {'shell' : 'sql' , 'shell-windows' : 'sql.exe' , 'legacy' : 'sqlplus' },
18+ 'PostgreSQL' : {'shell' : 'pgcli' , 'legacy' : 'psql' },
19+ 'SQLite' : {'shell' : 'litecli' , 'legacy' : 'sqlite3' }
4120}
4221
4322tunnel .logger .setLevel ('DEBUG' )
@@ -161,105 +140,84 @@ def on_ok(self): # NOSONAR
161140
162141 sqlshell = section .get ('shell' , defshell )
163142
164- # OPTIONS AND CONNECTION PARAMETERS
165- # effective params are: opts[shelltype], (DSN|conn_params[dbtype]), env_vars[shelltype]
166- params = {
167- # `-N -C` = "encrypt, trust server certificate" (NOSONAR)
168- 'MSSQL' : {
169- 'opts' : ['-N' , '-C' , '--mssqlclirc' , startup_file ],
170- 'conn_params' : ['-U' , user , '-P' , passwd , '-S' , '{host},{port}' , '-d' , db ]
171- },
172-
173- 'MSSQL-2' : {
174- 'opts' : ['-N' , '-C' ],
175- 'env_vars' : {'SQLCMDINI' : startup_file }
176- },
177-
178- 'MySQL' : {
179- 'opts' : ['--myclirc' , startup_file ],
180- 'conn_params' : ['-u' , user , f'-p{ passwd } ' , '-h' , '{host}' , '-P' , '{port}' , '-D' , db ]
181- },
182-
183- 'MySQL-2' : {
184- 'opts' : [f'--defaults-file={ startup_file } ' , '--protocol=TCP' ]
185- },
186-
187- 'Oracle' : {
188- 'opts' : ['-logon' ],
189- 'conn_params' : [f'{ user } /{ passwd } @//{{host}}:{{port}}/{ db } ' ],
190- 'env_vars' : {'SQLPATH' : startup_file }
191- },
192-
193- 'Oracle-2' : {
194- 'opts' : ['-l' ],
195- 'env_vars' : {'SQLPATH' : '' }
196- },
197-
198- 'PostgreSQL' : {
199- 'opts' : ['--pgclirc' , startup_file , '--prompt' , prompt ],
200- 'conn_params' : [f'postgres://{ user } :{ passwd } @{{host}}:{{port}}/{ db } ' ]
201- },
202-
203- 'PostgreSQL-2' : {
204- 'env_vars' : {'PSQLRC' : startup_file }
205- },
206-
207- 'SQLite' : {
208- 'opts' : ['--liteclirc' , startup_file , '--prompt' , prompt ],
209- 'conn_params' : [db ]
210- },
211-
212- 'SQLite-2' : {
213- 'opts' : ['-init' , startup_file ]
214- }
215- }
216-
217- opts = params [shelltype ].get ('opts' , [])
218- conn_params = params [dbtype ]['conn_params' ]
219- env_vars = params [shelltype ].get ('env_vars' , {})
220-
221- # SPECIAL CASES FOR RDBMS
143+ # CONNECTION PARAMETERS AND OPTIONS
144+ opts = []
145+ env_vars = {}
146+
222147 if dbtype == 'MSSQL' :
148+ conn_params = ['-U' , user , '-P' , passwd , '-S' , '{host},{port}' , '-d' , db ]
149+
150+ # `-N -C` = "encrypt, trust server certificate" (NOSONAR)
151+ if shelltype == 'MSSQL' :
152+ opts = ['-N' , '-C' , '--mssqlclirc' , startup_file ]
153+ else :
154+ opts = ['-N' , '-C' ]
155+ env_vars = {'SQLCMDINI' : startup_file }
156+
223157 # named pipe connection to LocalDB
224158 if tb .is_localdb (dsn ) or tb .is_localdb (host ):
225- opts .remove ('-N' ) # `-N` = "encrypt" (NOSONAR)
226159 conn_params [5 ] = '{host}' # host,port -> host
160+ opts .remove ('-N' ) # `-N` = "encrypt" (NOSONAR)
227161
228162 elif dbtype == 'MySQL' :
229- if not passwd :
230- conn_params .remove ('-p' )
163+ conn_params = ['-u' , user , '-h' , '{host}' , '-P' , '{port}' , '-D' , db ]
231164
232- if not startup_file :
233- if shelltype == 'MySQL' :
234- opts = []
235- else :
236- opts .remove ('--defaults-file=' )
165+ if passwd :
166+ conn_params += [f'-p{ passwd } ' ]
167+
168+ if shelltype == 'MySQL' :
169+ if startup_file :
170+ opts = ['--myclirc' , startup_file ]
171+ else :
172+ opts = ['--protocol=TCP' ]
173+ if startup_file :
174+ # `--defaults-file` must be first option
175+ opts = [f'--defaults-file={ startup_file } ' ] + opts
237176
238177 elif dbtype == 'Oracle' :
239- if not db :
240- # remove trailing "/" because SQLcl can't handle `user@host/` connection
241- # strings
242- conn_params [0 ] = conn_params [0 ][:- 1 ]
178+ conn_params = [f'{ user } /{ passwd } @//{{host}}:{{port}}' ]
179+
180+ if db :
181+ # SQLcl can't handle `user@host/` connection strings
182+ conn_params [0 ] += f'/{ db } '
243183
244184 if user == 'sys' :
245185 conn_params += ['as' , 'sysdba' ]
246186
187+ if shelltype == 'Oracle' :
188+ opts = ['-logon' ]
189+ env_vars = {'SQLPATH' : startup_file }
190+ else :
191+ opts = ['-l' ]
192+ env_vars = {'SQLPATH' : '' }
193+
247194 elif dbtype == 'PostgreSQL' :
248- if not prompt and shelltype == 'PostgreSQL' :
249- opts = opts [:2 ]
195+ conn_params = [f'postgres://{ user } :{ passwd } @{{host}}:{{port}}/{ db } ' ]
250196
251- elif dbtype == 'SQLite' :
252- if not prompt and shelltype == 'SQLite' :
253- opts = opts [:2 ]
197+ if shelltype == 'PostgreSQL' :
198+ opts = ['--pgclirc' , startup_file ]
199+ if prompt :
200+ opts += ['--prompt' , prompt ]
201+ else :
202+ env_vars = {'PSQLRC' : startup_file }
254203
204+ elif dbtype == 'SQLite' :
255205 if db :
256206 # replace "\" with "/" for litecli prompt
257207 conn_params = [pathlib .Path (db ).as_posix ()]
208+ else :
209+ conn_params = [db ]
210+
211+ if shelltype == 'SQLite' :
212+ opts = ['--liteclirc' , startup_file ]
213+ if prompt :
214+ opts += ['--prompt' , prompt ]
215+ else :
216+ opts = ['-init' , startup_file ]
258217
259218 # don't start tunnel for SQLite
260219 host = None
261220 port = None
262- #
263221
264222 if self .dsn .value :
265223 # don't start tunnel for DSN connections
@@ -282,6 +240,7 @@ def on_ok(self): # NOSONAR
282240 host = dbtunnel .local_bind_host
283241 port = str (dbtunnel .local_bind_port )
284242
243+ # noinspection PyUnboundLocalVariable
285244 conn_params = [param .format (host = host , port = port ) for param in conn_params ]
286245 subprocess .run ([sqlshell ] + opts + conn_params ) # pylint: disable = subprocess-run-check
287246
0 commit comments