-
Notifications
You must be signed in to change notification settings - Fork 6
/
dbshell_plus.py
67 lines (55 loc) · 2.06 KB
/
dbshell_plus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import errno
import subprocess
from django.db import connections
from django.core.management.commands import dbshell
class Command(dbshell.Command):
def handle(self, **options):
connection = connections[options.get('database')]
dbclis = {
'postgresql': 'pgcli',
'mysql': 'mycli'
}
cmd = dbclis.get(connection.vendor)
if cmd:
try:
getattr(self, cmd)(connection)
return
except OSError, e:
if e.errno != errno.ENOENT:
self.stderr.write("Could not start %s: %s" % (cmd, str(e)))
super(Command, self).handle(**options)
def pgcli(self, connection):
# argument code copied from Django
settings_dict = connection.settings_dict
args = ['pgcli']
if settings_dict['USER']:
args += ["-U", settings_dict['USER']]
if settings_dict['HOST']:
args.extend(["-h", settings_dict['HOST']])
if settings_dict['PORT']:
args.extend(["-p", str(settings_dict['PORT'])])
args += [settings_dict['NAME']]
subprocess.call(args)
def mycli(self, connection):
# argument code copied from Django
settings_dict = connection.settings_dict
args = ['mycli']
db = settings_dict['OPTIONS'].get('db', settings_dict['NAME'])
user = settings_dict['OPTIONS'].get('user', settings_dict['USER'])
passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD'])
host = settings_dict['OPTIONS'].get('host', settings_dict['HOST'])
port = settings_dict['OPTIONS'].get('port', settings_dict['PORT'])
if user:
args += ["--user=%s" % user]
if passwd:
args += ["--pass=%s" % passwd]
if host:
if '/' in host:
args += ["--socket=%s" % host]
else:
args += ["--host=%s" % host]
if port:
args += ["--port=%s" % port]
if db:
args += [db]
subprocess.call(args)