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
|
2020-10-05 06:25:29 +08:00
|
|
|
def settings_to_cmd_args_env(cls, settings_dict, parameters):
|
2014-05-21 21:05:41 +08:00
|
|
|
args = [cls.executable_name]
|
2020-10-05 06:31:04 +08:00
|
|
|
env = None
|
2021-05-11 04:32:10 +08:00
|
|
|
database = settings_dict['OPTIONS'].get(
|
|
|
|
'database',
|
|
|
|
settings_dict['OPTIONS'].get('db', settings_dict['NAME']),
|
|
|
|
)
|
2009-12-22 23:18:51 +08:00
|
|
|
user = settings_dict['OPTIONS'].get('user', settings_dict['USER'])
|
2020-06-12 02:12:35 +08:00
|
|
|
password = settings_dict['OPTIONS'].get(
|
|
|
|
'password',
|
|
|
|
settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD'])
|
|
|
|
)
|
2009-12-22 23:18:51 +08:00
|
|
|
host = settings_dict['OPTIONS'].get('host', settings_dict['HOST'])
|
|
|
|
port = settings_dict['OPTIONS'].get('port', settings_dict['PORT'])
|
2017-06-20 06:11:25 +08:00
|
|
|
server_ca = settings_dict['OPTIONS'].get('ssl', {}).get('ca')
|
|
|
|
client_cert = settings_dict['OPTIONS'].get('ssl', {}).get('cert')
|
|
|
|
client_key = settings_dict['OPTIONS'].get('ssl', {}).get('key')
|
2009-12-22 23:18:51 +08:00
|
|
|
defaults_file = settings_dict['OPTIONS'].get('read_default_file')
|
2020-10-15 02:07:57 +08:00
|
|
|
charset = settings_dict['OPTIONS'].get('charset')
|
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]
|
2020-06-12 02:12:35 +08:00
|
|
|
if password:
|
2020-10-05 06:31:04 +08:00
|
|
|
# The MYSQL_PWD environment variable usage is discouraged per
|
|
|
|
# MySQL's documentation due to the possibility of exposure through
|
|
|
|
# `ps` on old Unix flavors but --password suffers from the same
|
|
|
|
# flaw on even more systems. Usage of an environment variable also
|
|
|
|
# prevents password exposure if the subprocess.run(check=True) call
|
|
|
|
# raises a CalledProcessError since the string representation of
|
|
|
|
# the latter includes all of the provided `args`.
|
|
|
|
env = {'MYSQL_PWD': password}
|
2008-08-11 20:11:25 +08:00
|
|
|
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]
|
2017-06-20 06:11:25 +08:00
|
|
|
if server_ca:
|
|
|
|
args += ["--ssl-ca=%s" % server_ca]
|
|
|
|
if client_cert:
|
|
|
|
args += ["--ssl-cert=%s" % client_cert]
|
|
|
|
if client_key:
|
|
|
|
args += ["--ssl-key=%s" % client_key]
|
2020-10-15 02:07:57 +08:00
|
|
|
if charset:
|
|
|
|
args += ['--default-character-set=%s' % charset]
|
2021-05-11 04:32:10 +08:00
|
|
|
if database:
|
|
|
|
args += [database]
|
2020-04-14 15:56:40 +08:00
|
|
|
args.extend(parameters)
|
2020-10-05 06:31:04 +08:00
|
|
|
return args, env
|