SqlBase
abstract class SqlBase implements ConfigAwareInterface (View source)
The base implementation for Drush database connections.
MySql, PostgreSql and SQLite implementations are provided by Drush. Contrib and custom database drivers can provide their own implementation by extending from this class, and naming the class like 'SqlMydriver'. Note that the camelcasing is required, as well as it is mandatory that the namespace of the extending class be 'Drush\Sql'. In order to avoid autoloader collisions, it is recommended to place the class outside of the 'src' directory of the module providing the database driver, then adding a 'classmap' entry to the autoload class of the module's composer.json file.
For example, supposing the SqlMydriver class is located in a 'drush' module subdirectory:
Traits
Note: when using this trait, also implement ConfigAwareInterface/ConfigAwareTrait.
Properties
protected ConfigInterface | $config | from ConfigAwareTrait | |
string | $queryExtra | ||
string | $queryFile | ||
protected Process | $process |
Methods
Given a list of all tables, expand the convert the wildcards in the option-provided lists into a list of actual table names.
Given the table names in the input array that may contain wildcards (*
),
expand the table names so that the array returned only contains table names
that exist in the database.
Expand wildcard tables.
Construct an array that places table names in appropriate buckets based on whether the table is to be skipped, included for structure only, or have structure and data dumped.
Consult the specified options and return the list of tables specified.
Any class that uses ConfigAwareTrait SHOULD override this method , and define a prefix for its configuration items. This is usually done in a base class. When used, this method should return a string that ends with a "."; see BaseTask::configPrefix().
No description
No description
Typically, SqlBase instances are constructed via SqlBase::create($options).
Get environment variables to pass to Process.
Get the last used Process.
No description
Get a driver specific instance of this class.
No description
No description
Set the current db spec.
The unix command used to connect to the database.
A string for connecting to a database.
No description
Handle 'pipefail' option for the specified command.
No description
No description
Execute a SQL query. Respect simulate mode.
Execute a SQL query. Always execute regardless of simulate mode.
Show the query in debug mode and simulate mode
No description
No description
No description
Drop specified database.
Build a SQL string for dropping and creating a database.
Create a new database.
Drop all tables (if DB exists) or CREATE target database.
No description
Build a string containing connection credentials.
The active database driver.
Extract the name of all existing tables in the given database.
Extract the name of all existing tables in the given database.
No description
Adjust DB connection with superuser credentials if provided.
No description
No description
Convert from an old-style database URL to an array of database settings.
Start building the command to run a query.
Details
array
getExpandedTableSelection(array $options, array $all_tables)
Given a list of all tables, expand the convert the wildcards in the option-provided lists into a list of actual table names.
array
expandAndFilterTables(array $tables, array $db_tables)
Given the table names in the input array that may contain wildcards (*
),
expand the table names so that the array returned only contains table names
that exist in the database.
array
expandWildcardTables(array $tables, array $db_tables)
Expand wildcard tables.
array
filterTables(array $tables, array $db_tables)
Filters tables.
array
getTableSelection(array $options)
Construct an array that places table names in appropriate buckets based on whether the table is to be skipped, included for structure only, or have structure and data dumped.
The keys of the array are:
- skip: tables to be skipped completed in the dump
- structure: tables to only have their structure i.e. DDL dumped
- tables: tables to have structure and data dumped
array
getRawTableList(string $option_name, array $options)
Consult the specified options and return the list of tables specified.
$this
setConfig(ConfigInterface $config)
Set the config management object.
ConfigInterface
getConfig()
Get the config management object.
static protected string
configPrefix()
Any class that uses ConfigAwareTrait SHOULD override this method , and define a prefix for its configuration items. This is usually done in a base class. When used, this method should return a string that ends with a "."; see BaseTask::configPrefix().
static protected
configClassIdentifier($classname)
No description
static protected
configPostfix()
No description
static
configure(string $key, mixed $value, ConfigInterface|null $config = null)
No description
protected mixed|null
getConfigValue(string $key, mixed|null $default = null)
No description
__construct(array $dbSpec, array $options)
Typically, SqlBase instances are constructed via SqlBase::create($options).
array
getEnv()
Get environment variables to pass to Process.
Process
getProcess()
Get the last used Process.
void
setProcess(Process $process)
No description
static SqlBase|null
create(array $options = [])
Get a driver specific instance of this class.
static SqlBase|null
getInstance($db_spec, $options)
No description
array
getDbSpec()
No description
void
setDbSpec(array $dbSpec)
Set the current db spec.
string
command()
The unix command used to connect to the database.
string
connect(bool $hide_password = true)
A string for connecting to a database.
string|bool|null
dump()
No description
protected string
addPipeFail(string $cmd, string $pipefail)
Handle 'pipefail' option for the specified command.
string
dumpCmd($table_selection)
No description
string|null
dumpFile($file)
No description
bool|null
query(string $query, $input_file = null, $result_file = '')
Execute a SQL query. Respect simulate mode.
If you don't want to query results to print during --debug then provide a $result_file whose value can be drush_bit_bucket().
bool
alwaysQuery(string $query, $input_file = null, string|null $result_file = '')
Execute a SQL query. Always execute regardless of simulate mode.
If you don't want results to print during --debug then provide a $result_file whose value can be drush_bit_bucket().
protected void
logQueryInDebugMode($query, $input_file_original)
Show the query in debug mode and simulate mode
string|null
silent()
No description
string|null
queryPrefix($query)
No description
queryFormat($query)
No description
bool|null
drop(array $tables)
Drop specified database.
string
createdbSql(string $dbname, bool $quoted = false)
Build a SQL string for dropping and creating a database.
bool|null
createdb(bool $quoted = false)
Create a new database.
bool
dropOrCreate()
Drop all tables (if DB exists) or CREATE target database.
return TRUE or FALSE depending on success.
bool
dbExists()
No description
string
creds(bool $hide_password = true)
Build a string containing connection credentials.
string
scheme()
The active database driver.
array
listTables()
Extract the name of all existing tables in the given database.
array
listTablesQuoted()
Extract the name of all existing tables in the given database.
string
paramsToOptions($parameters)
No description
void
su()
Adjust DB connection with superuser credentials if provided.
array
getOptions()
No description
getOption($name, $default = null)
No description
static array
dbSpecFromDbUrl($db_url)
Convert from an old-style database URL to an array of database settings.
array
alwaysQueryCommand($input_file)
Start building the command to run a query.