You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.py 1.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import subprocess
  2. from django.db.backends.base.client import BaseDatabaseClient
  3. class DatabaseClient(BaseDatabaseClient):
  4. executable_name = 'mysql'
  5. @classmethod
  6. def settings_to_cmd_args(cls, settings_dict):
  7. args = [cls.executable_name]
  8. db = settings_dict['OPTIONS'].get('db', settings_dict['NAME'])
  9. user = settings_dict['OPTIONS'].get('user', settings_dict['USER'])
  10. passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD'])
  11. host = settings_dict['OPTIONS'].get('host', settings_dict['HOST'])
  12. port = settings_dict['OPTIONS'].get('port', settings_dict['PORT'])
  13. cert = settings_dict['OPTIONS'].get('ssl', {}).get('ca')
  14. defaults_file = settings_dict['OPTIONS'].get('read_default_file')
  15. # Seems to be no good way to set sql_mode with CLI.
  16. if defaults_file:
  17. args += ["--defaults-file=%s" % defaults_file]
  18. if user:
  19. args += ["--user=%s" % user]
  20. if passwd:
  21. args += ["--password=%s" % passwd]
  22. if host:
  23. if '/' in host:
  24. args += ["--socket=%s" % host]
  25. else:
  26. args += ["--host=%s" % host]
  27. if port:
  28. args += ["--port=%s" % port]
  29. if cert:
  30. args += ["--ssl-ca=%s" % cert]
  31. if db:
  32. args += [db]
  33. return args
  34. def runshell(self):
  35. args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
  36. subprocess.call(args)