diff --git a/django/core/management.py b/django/core/management.py index c4ce4cbf71..6b26d8b58a 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -1045,11 +1045,18 @@ def run_shell(use_plain=False): code.interact() run_shell.args = '[--plain]' +def dbshell(): + "Runs the command-line client for the current DATABASE_ENGINE." + from django.db import runshell + runshell() +dbshell.args = "" + # Utilities for command-line script DEFAULT_ACTION_MAPPING = { 'adminindex': get_admin_index, 'createcachetable' : createcachetable, + 'dbshell': dbshell, 'diffsettings': diffsettings, 'inspectdb': inspectdb, 'install': install, @@ -1072,11 +1079,12 @@ DEFAULT_ACTION_MAPPING = { NO_SQL_TRANSACTION = ( 'adminindex', 'createcachetable', + 'dbshell', 'diffsettings', 'install', 'reset', 'sqlindexes', - 'syncdb' + 'syncdb', ) class DjangoOptionParser(OptionParser): @@ -1138,7 +1146,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING): if action == 'shell': action_mapping[action](options.plain is True) - elif action in ('syncdb', 'validate', 'diffsettings'): + elif action in ('syncdb', 'validate', 'diffsettings', 'dbshell'): action_mapping[action]() elif action == 'inspectdb': try: diff --git a/django/db/__init__.py b/django/db/__init__.py index c7584a9bd3..317d6059bf 100644 --- a/django/db/__init__.py +++ b/django/db/__init__.py @@ -22,6 +22,7 @@ except ImportError, e: get_introspection_module = lambda: __import__('django.db.backends.%s.introspection' % settings.DATABASE_ENGINE, '', '', ['']) get_creation_module = lambda: __import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, '', '', ['']) +runshell = lambda: __import__('django.db.backends.%s.client' % settings.DATABASE_ENGINE, '', '', ['']).runshell() connection = backend.DatabaseWrapper() DatabaseError = backend.DatabaseError diff --git a/django/db/backends/ado_mssql/client.py b/django/db/backends/ado_mssql/client.py new file mode 100644 index 0000000000..5c197cafa4 --- /dev/null +++ b/django/db/backends/ado_mssql/client.py @@ -0,0 +1,2 @@ +def runshell(): + raise NotImplementedError diff --git a/django/db/backends/dummy/client.py b/django/db/backends/dummy/client.py new file mode 100644 index 0000000000..e332987aa8 --- /dev/null +++ b/django/db/backends/dummy/client.py @@ -0,0 +1,3 @@ +from django.db.backends.dummy.base import complain + +runshell = complain diff --git a/django/db/backends/mysql/client.py b/django/db/backends/mysql/client.py new file mode 100644 index 0000000000..f9d6297b8e --- /dev/null +++ b/django/db/backends/mysql/client.py @@ -0,0 +1,14 @@ +from django.conf import settings +import os + +def runshell(): + args = [''] + args += ["--user=%s" % settings.DATABASE_USER] + if settings.DATABASE_PASSWORD: + args += ["--password=%s" % settings.DATABASE_PASSWORD] + if settings.DATABASE_HOST: + args += ["--host=%s" % settings.DATABASE_HOST] + if settings.DATABASE_PORT: + args += ["--port=%s" % settings.DATABASE_PORT] + args += [settings.DATABASE_NAME] + os.execvp('mysql', args) diff --git a/django/db/backends/postgresql/client.py b/django/db/backends/postgresql/client.py new file mode 100644 index 0000000000..3d0d7a0d2a --- /dev/null +++ b/django/db/backends/postgresql/client.py @@ -0,0 +1,14 @@ +from django.conf import settings +import os + +def runshell(): + args = [''] + args += ["-U%s" % settings.DATABASE_USER] + if settings.DATABASE_PASSWORD: + args += ["-W"] + if settings.DATABASE_HOST: + args += ["-h %s" % settings.DATABASE_HOST] + if settings.DATABASE_PORT: + args += ["-p %s" % settings.DATABASE_PORT] + args += [settings.DATABASE_NAME] + os.execvp('psql', args) diff --git a/django/db/backends/sqlite3/client.py b/django/db/backends/sqlite3/client.py new file mode 100644 index 0000000000..097218341f --- /dev/null +++ b/django/db/backends/sqlite3/client.py @@ -0,0 +1,6 @@ +from django.conf import settings +import os + +def runshell(): + args = ['', settings.DATABASE_NAME] + os.execvp('sqlite3', args) diff --git a/docs/django-admin.txt b/docs/django-admin.txt index 47f42a9060..fa493c4d0a 100644 --- a/docs/django-admin.txt +++ b/docs/django-admin.txt @@ -64,6 +64,21 @@ backend. See the `cache documentation`_ for more information. .. _cache documentation: http://www.djangoproject.com/documentation/cache/ +dbshell +------- + +Runs the command-line client for the database engine specified in your +``DATABASE_ENGINE`` setting, with the connection parameters specified in your +``DATABASE_USER``, ``DATABASE_PASSWORD``, etc., settings. + + * For PostgreSQL, this runs the ``psql`` command-line client. + * For MySQL, this runs the ``mysql`` command-line client. + * For SQLite, this runs the ``sqlite3`` command-line client. + +This command assumes the programs are on your PATH so that a simple call to +the program name (``psql``, ``mysql``, ``sqlite3``) will find the program in +the right place. + diffsettings ------------