2014-03-09 19:00:42 +08:00
|
|
|
import subprocess
|
2009-04-11 19:41:35 +08:00
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.client import BaseDatabaseClient
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseClient(BaseDatabaseClient):
|
2008-09-09 10:13:58 +08:00
|
|
|
executable_name = 'mysql'
|
|
|
|
|
2014-05-21 21:05:41 +08:00
|
|
|
@classmethod
|
|
|
|
def settings_to_cmd_args(cls, settings_dict):
|
|
|
|
args = [cls.executable_name]
|
2009-12-22 23:18:51 +08:00
|
|
|
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'])
|
2014-05-21 21:05:41 +08:00
|
|
|
cert = settings_dict['OPTIONS'].get('ssl', {}).get('ca')
|
2009-12-22 23:18:51 +08:00
|
|
|
defaults_file = settings_dict['OPTIONS'].get('read_default_file')
|
2008-09-09 10:13:58 +08:00
|
|
|
# Seems to be no good way to set sql_mode with CLI.
|
2009-04-11 19:41:35 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
if defaults_file:
|
|
|
|
args += ["--defaults-file=%s" % defaults_file]
|
|
|
|
if user:
|
|
|
|
args += ["--user=%s" % user]
|
|
|
|
if passwd:
|
|
|
|
args += ["--password=%s" % passwd]
|
|
|
|
if host:
|
2010-08-28 21:17:50 +08:00
|
|
|
if '/' in host:
|
|
|
|
args += ["--socket=%s" % host]
|
|
|
|
else:
|
2011-01-03 21:19:59 +08:00
|
|
|
args += ["--host=%s" % host]
|
2008-08-11 20:11:25 +08:00
|
|
|
if port:
|
|
|
|
args += ["--port=%s" % port]
|
2014-05-21 21:05:41 +08:00
|
|
|
if cert:
|
|
|
|
args += ["--ssl-ca=%s" % cert]
|
2008-08-11 20:11:25 +08:00
|
|
|
if db:
|
|
|
|
args += [db]
|
2014-05-21 21:05:41 +08:00
|
|
|
return args
|
2007-03-14 20:08:19 +08:00
|
|
|
|
2014-05-21 21:05:41 +08:00
|
|
|
def runshell(self):
|
|
|
|
args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
|
2016-06-28 00:20:40 +08:00
|
|
|
subprocess.check_call(args)
|