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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. server_ca = settings_dict['OPTIONS'].get('ssl', {}).get('ca')
  14. client_cert = settings_dict['OPTIONS'].get('ssl', {}).get('cert')
  15. client_key = settings_dict['OPTIONS'].get('ssl', {}).get('key')
  16. defaults_file = settings_dict['OPTIONS'].get('read_default_file')
  17. # Seems to be no good way to set sql_mode with CLI.
  18. if defaults_file:
  19. args += ["--defaults-file=%s" % defaults_file]
  20. if user:
  21. args += ["--user=%s" % user]
  22. if passwd:
  23. args += ["--password=%s" % passwd]
  24. if host:
  25. if '/' in host:
  26. args += ["--socket=%s" % host]
  27. else:
  28. args += ["--host=%s" % host]
  29. if port:
  30. args += ["--port=%s" % port]
  31. if server_ca:
  32. args += ["--ssl-ca=%s" % server_ca]
  33. if client_cert:
  34. args += ["--ssl-cert=%s" % client_cert]
  35. if client_key:
  36. args += ["--ssl-key=%s" % client_key]
  37. if db:
  38. args += [db]
  39. return args
  40. def runshell(self):
  41. args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
  42. subprocess.check_call(args)