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

Commit acc77c8

Browse files
committed
Imporove error handling when creating Connection
The changes include raising an apropriate Exception if DB cli binary could not be found, with clear next steps on how to fix the issue. Upon creating a connection, if such Exception is encountered a sublime status message is displayed to indicate this problem. fixes #74, fixes #82 related #39, #41, #48, #51
1 parent 10efe2d commit acc77c8

File tree

4 files changed

+75
-23
lines changed

4 files changed

+75
-23
lines changed

SQLTools.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,28 @@ def getConnections():
6565
options = connections.get('connections', {})
6666

6767
for name, config in options.items():
68-
connectionsObj[name] = Connection(name, config, settings=settings.all())
68+
connectionsObj[name] = createConnection(name, config, settings=settings.all())
6969

7070
# project settings
7171
try:
7272
options = Window().project_data().get('connections', {})
7373
for name, config in options.items():
74-
connectionsObj[name] = Connection(name, config, settings=settings.all())
74+
connectionsObj[name] = createConnection(name, config, settings=settings.all())
7575
except Exception:
7676
pass
7777

7878
return connectionsObj
7979

80+
def createConnection(name, config, settings):
81+
newConnection = None
82+
# if DB cli binary could not be found in path a FileNotFoundError is thrown
83+
try:
84+
newConnection = Connection(name, config, settings=settings)
85+
except FileNotFoundError as e:
86+
# use only first line of the Exception in status message
87+
Window().status_message( __package__ + ": " + str(e).splitlines()[0] )
88+
raise e
89+
return newConnection
8090

8191
def loadDefaultConnection():
8292
default = settings.get('default', False)

SQLTools.sublime-settings

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"unescape_quotes" : [
1616
"php"
1717
],
18+
/**
19+
* If DB cli binary is not in PATH, set the full path in "cli" section.
20+
* Note: forward slashes ("/") should be used in path. Example:
21+
* "mysql" : "c:/Program Files/MySQL/MySQL Server 5.7/bin/mysql.exe"
22+
*/
1823
"cli" : {
1924
"mysql" : "mysql",
2025
"pgsql" : "psql",

SQLToolsAPI/Connection.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66
from . import Utils as U
77
from . import Command as C
88

9-
109
class Connection:
10+
DB_CLI_NOT_FOUND_MESSAGE = """'{0}' could not be found.
11+
Please set the path to '{0}' binary in your SQLTools settings before continuing.
12+
Example of "cli" section in SQLTools.sublime-settings:
13+
/* ... (note the use of forward slashes) */
14+
"cli" : {{
15+
"mysql" : "c:/Program Files/MySQL/MySQL Server 5.7/bin/mysql.exe",
16+
"pgsql" : "c:/Program Files/PostgreSQL/9.6/bin/psql.exe"
17+
}}
18+
You might need to restart the editor for settings to be refreshed."""
19+
1120
timeout = None
1221
history = None
1322
settings = None
@@ -29,15 +38,6 @@ def __init__(self, name, options, settings={}, commandClass='ThreadCommand'):
2938
self.Command = getattr(C, commandClass)
3039

3140
self.cli = settings.get('cli')[options['type']]
32-
cli_path = shutil.which(self.cli)
33-
34-
if cli_path is None:
35-
Log((
36-
"'{0}' could not be found.\n\n" +
37-
"Please set the '{0}' path in your SQLTools settings " +
38-
"before continue.").format(self.cli))
39-
return
40-
4141
self.settings = settings
4242
self.rowsLimit = settings.get('show_records', {}).get('limit', 50)
4343
self.options = options
@@ -53,6 +53,11 @@ def __init__(self, name, options, settings={}, commandClass='ThreadCommand'):
5353
self.safe_limit = settings.get('safe_limit', None)
5454
self.show_query = settings.get('show_query', None)
5555

56+
cli_path = shutil.which(self.cli)
57+
if cli_path is None:
58+
Log(self.DB_CLI_NOT_FOUND_MESSAGE.format(self.cli))
59+
raise FileNotFoundError(self.DB_CLI_NOT_FOUND_MESSAGE.format(self.cli))
60+
5661
def __str__(self):
5762
return self.name
5863

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
{
22
"connections": {
3-
/**
4-
* "Connection name": { // Connection name, used in menu (Display name)
5-
* "database" : "DATABASE",
6-
* "host" : "HOSTNAME",
7-
* "port" : "PORT",
8-
* "type" : "pgsql", // SGDB to use: (mysql, pgsql, oracle, vertica)
9-
* "username" : "USERNAME",
10-
* "password" : null // (if you are using postgresql, you must setup a pgpass file and set it to null)
11-
* "encoding" : null // Your DB encodings. Default: utf-8
12-
* },
13-
*/
3+
/*
4+
"Generic Template": { // Connection name, used in menu (Display name)
5+
"type" : "pgsql", // DB type: (mysql, pgsql, oracle, vertica, sqlite, firebird, sqsh)
6+
"host" : "HOSTNAME", // DB host to connect to
7+
"port" : "PORT", // DB port
8+
"database" : "DATABASE", // DB name (for SQLite this is the path to DB file)
9+
"username" : "USERNAME", // DB username
10+
"password" : null, // (if you are using PostgreSQL, you must setup a "pgpass.conf" file and set it to null)
11+
"encoding" : "utf-8" // Your DB encoding. Default: utf-8
12+
},
13+
"Connection MySQL": {
14+
"type" : "mysql",
15+
"host" : "127.0.0.1",
16+
"port" : 3306,
17+
"username": "user",
18+
"password": "password",
19+
"database": "dbname",
20+
"encoding": "utf-8"
21+
},
22+
"Connection PostgreSQL": {
23+
"type" : "pgsql",
24+
"host" : "127.0.0.1",
25+
"port" : 5432,
26+
"username": "anotheruser",
27+
"database": "dbname",
28+
"encoding": "utf-8"
29+
},
30+
"Connection Oracle": {
31+
"type" : "oracle",
32+
"host" : "127.0.0.1",
33+
"port" : 1522,
34+
"username": "anotheruser",
35+
"password": "password",
36+
"database": "dbname",
37+
"service" : "servicename",
38+
"encoding": "utf-8"
39+
},
40+
"Connection SQLite": {
41+
"type" : "sqlite",
42+
"database": "d:/sqlite/sample_db/chinook.db", // note the forward slashes in path
43+
"encoding": "utf-8"
44+
}
45+
*/
1446
},
1547
"default": null
1648
}

0 commit comments

Comments
 (0)