| 1 |
import os |
| 2 |
import subprocess |
| 3 |
|
| 4 |
|
| 5 |
class BaseDatabaseClient: |
| 6 |
"""Encapsulate backend-specific methods for opening a client shell.""" |
| 7 |
|
| 8 |
# This should be a string representing the name of the executable |
| 9 |
# (e.g., "psql"). Subclasses must override this. |
| 10 |
executable_name = None |
| 11 |
|
| 12 |
def __init__(self, connection): |
| 13 |
# connection is an instance of BaseDatabaseWrapper. |
| 14 |
self.connection = connection |
| 15 |
|
| 16 |
@classmethod |
| 17 |
def settings_to_cmd_args_env(cls, settings_dict, parameters): |
| 18 |
raise NotImplementedError( |
| 19 |
"subclasses of BaseDatabaseClient must provide a " |
| 20 |
"settings_to_cmd_args_env() method or override a runshell()." |
| 21 |
) |
| 22 |
|
| 23 |
def runshell(self, parameters): |
| 24 |
args, env = self.settings_to_cmd_args_env( |
| 25 |
self.connection.settings_dict, parameters |
| 26 |
) |
| 27 |
env = {**os.environ, **env} if env else None |
| 28 |
subprocess.run(args, env=env, check=True) |