11
11
12
12
tunnel .logger .setLevel ('DEBUG' )
13
13
os .environ ['ESCDELAY' ] = '0' # no delay on Linux for Escape key
14
+
15
+ # Read config file
14
16
# specify delimiter because of DSNs ("MSSQL: name = ...")
15
17
config = configparser .ConfigParser (delimiters = '=' )
16
18
17
- def read_config ():
18
- if tb .is_pyinstaller ():
19
- # https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#using-sys-executable-and-sys-argv-0
20
- script_dir = sys .executable
21
- else :
22
- script_dir = __file__
23
- # config file in same directory as this file
24
- config_file = pathlib .Path (script_dir ).with_name ('sql shell.ini' )
25
- config .optionxform = str # don't lowercase DSNs
26
- config .read (config_file , encoding = 'utf-8' )
27
- try :
28
- os .environ .update (config ['Environment' ])
29
- except KeyError :
30
- pass
19
+ if tb .is_pyinstaller ():
20
+ # https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#using-sys-executable-and-sys-argv-0
21
+ script_dir = sys .executable
22
+ else :
23
+ script_dir = __file__
24
+ # config file in same directory as this file
25
+ config_file = pathlib .Path (script_dir ).with_name ('sql shell.ini' )
26
+ config .optionxform = str # don't lowercase DSNs
27
+ config .read (config_file , encoding = 'utf-8' )
28
+ try :
29
+ os .environ .update (config ['Environment' ])
30
+ except KeyError :
31
+ pass
31
32
32
33
class DbApp (NPSAppManaged ):
33
34
def onStart (self ):
@@ -42,11 +43,6 @@ def create(self):
42
43
self .dbtype = self .add (TitleCombo , name = '* Database type:' , value = 0 , values = ['...' ]
43
44
+ dbms_types , ** widget_defaults )
44
45
45
- self .legacy_client = self .add (TitleMultiSelect , name = '- Legacy client:' ,
46
- value = None , values = ['' ], max_height = 2 , scroll_exit = True ,
47
- ** widget_defaults )
48
-
49
- # DSNs changes in config file will not be updated in this widget
50
46
try :
51
47
dsns = ['...' ] + [f'{ index + 1 } . { item } ' for index , item in enumerate (config ['DSN' ])]
52
48
except KeyError :
@@ -109,12 +105,7 @@ def on_ok(self): # NOSONAR
109
105
notify_confirm ('User is mandatory!' , title = 'ERROR' , editw = True )
110
106
return
111
107
112
- read_config ()
113
108
dbtype = dbms_types [self .dbtype .value - 1 ]
114
- if self .legacy_client .value :
115
- shelltype = f'{ dbtype } -2'
116
- else :
117
- shelltype = dbtype
118
109
try :
119
110
dsn = config ['DSN' ][list (config ['DSN' ])[self .dsn .value - 1 ]]
120
111
except KeyError :
@@ -125,13 +116,14 @@ def on_ok(self): # NOSONAR
125
116
db = self .db .value
126
117
127
118
try :
128
- section = config [shelltype ]
119
+ section = config [dbtype ]
129
120
except KeyError :
130
121
section = {}
131
122
132
123
prompt = section .get ('prompt' , '' )[1 :- 1 ]
133
124
startup_file = section .get ('startup_file' , '' )
134
125
sqlhelp = section .get ('help' )
126
+ legacy = section .get ('legacy' )
135
127
136
128
# CONNECTION PARAMETERS AND OPTIONS
137
129
opts = []
@@ -141,20 +133,17 @@ def on_ok(self): # NOSONAR
141
133
port = self .port .value or 1433
142
134
conn_params = ['-U' , user , '-P' , passwd , '-S' , '{host},{port}' , '-d' , db ]
143
135
144
- if self . legacy_client . value :
136
+ if legacy :
145
137
defshell = 'sqlcmd'
138
+ opts = ['-N' , '-C' ]
139
+ env_vars = {'SQLCMDINI' : startup_file }
146
140
else :
147
141
if pycompat .system .is_windows :
148
142
defshell = 'mssql-cli.bat'
149
143
else :
150
144
defshell = 'mssql-cli'
151
-
152
- # `-N -C` = "encrypt, trust server certificate" (NOSONAR)
153
- if shelltype == 'MSSQL' :
154
- opts = ['-N' , '-C' , '--mssqlclirc' , startup_file ]
155
- else :
156
- opts = ['-N' , '-C' ]
157
- env_vars = {'SQLCMDINI' : startup_file }
145
+ # `-N -C` = "encrypt, trust server certificate" (NOSONAR)
146
+ opts = ['-N' , '-C' , '--mssqlclirc' , startup_file ]
158
147
159
148
# named pipe connection to LocalDB
160
149
if tb .is_localdb (dsn ) or tb .is_localdb (host ):
@@ -165,34 +154,36 @@ def on_ok(self): # NOSONAR
165
154
port = self .port .value or 3306
166
155
conn_params = ['-u' , user , '-h' , '{host}' , '-P' , '{port}' , '-D' , db ]
167
156
168
- if self . legacy_client . value :
157
+ if legacy :
169
158
defshell = 'mysql'
159
+ opts = ['--protocol=TCP' ]
160
+ if startup_file :
161
+ # `--defaults-file` must be first option
162
+ opts = [f'--defaults-file={ startup_file } ' ] + opts
163
+
170
164
else :
171
165
defshell = 'mycli'
166
+ if startup_file :
167
+ opts = ['--myclirc' , startup_file ]
172
168
173
169
if passwd :
174
170
conn_params += [f'-p{ passwd } ' ]
175
171
176
- if shelltype == 'MySQL' :
177
- if startup_file :
178
- opts = ['--myclirc' , startup_file ]
179
- else :
180
- opts = ['--protocol=TCP' ]
181
- if startup_file :
182
- # `--defaults-file` must be first option
183
- opts = [f'--defaults-file={ startup_file } ' ] + opts
184
-
185
172
elif dbtype == 'Oracle' :
186
173
port = self .port .value or 1521
187
174
conn_params = [f'{ user } /{ passwd } @//{{host}}:{{port}}' ]
188
175
189
- if self . legacy_client . value :
176
+ if legacy :
190
177
defshell = 'sqlplus'
178
+ opts = ['-l' ]
179
+ env_vars = {'SQLPATH' : '' }
191
180
else :
192
181
if pycompat .system .is_windows :
193
182
defshell = 'sql.exe'
194
183
else :
195
184
defshell = 'sql'
185
+ opts = ['-logon' ]
186
+ env_vars = {'SQLPATH' : startup_file }
196
187
197
188
if db :
198
189
# SQLcl can't handle `user@host/` connection strings
@@ -201,28 +192,18 @@ def on_ok(self): # NOSONAR
201
192
if user == 'sys' :
202
193
conn_params += ['as' , 'sysdba' ]
203
194
204
- if shelltype == 'Oracle' :
205
- opts = ['-logon' ]
206
- env_vars = {'SQLPATH' : startup_file }
207
- else :
208
- opts = ['-l' ]
209
- env_vars = {'SQLPATH' : '' }
210
-
211
195
elif dbtype == 'PostgreSQL' :
212
196
port = self .port .value or 5432
213
197
conn_params = [f'postgres://{ user } :{ passwd } @{{host}}:{{port}}/{ db } ' ]
214
198
215
- if self . legacy_client . value :
199
+ if legacy :
216
200
defshell = 'psql'
201
+ env_vars = {'PSQLRC' : startup_file }
217
202
else :
218
203
defshell = 'pgcli'
219
-
220
- if shelltype == 'PostgreSQL' :
221
- opts = ['--pgclirc' , startup_file ]
204
+ opts = ['--pgclirc' , startup_file ]
222
205
if prompt :
223
206
opts += ['--prompt' , prompt ]
224
- else :
225
- env_vars = {'PSQLRC' : startup_file }
226
207
227
208
elif dbtype == 'SQLite' :
228
209
# don't start tunnel for SQLite
@@ -235,17 +216,14 @@ def on_ok(self): # NOSONAR
235
216
else :
236
217
conn_params = [db ]
237
218
238
- if self . legacy_client . value :
219
+ if legacy :
239
220
defshell = 'sqlite3'
221
+ opts = ['-init' , startup_file ]
240
222
else :
241
223
defshell = 'litecli'
242
-
243
- if shelltype == 'SQLite' :
244
- opts = ['--liteclirc' , startup_file ]
224
+ opts = ['--liteclirc' , startup_file ]
245
225
if prompt :
246
226
opts += ['--prompt' , prompt ]
247
- else :
248
- opts = ['-init' , startup_file ]
249
227
250
228
# noinspection PyUnboundLocalVariable
251
229
sqlshell = section .get ('shell' , defshell )
@@ -286,6 +264,5 @@ def on_ok(self): # NOSONAR
286
264
click .pause ()
287
265
click .clear ()
288
266
289
- read_config ()
290
267
click .clear ()
291
268
DbApp ().run ()
0 commit comments