Skip to content

Instantly share code, notes, and snippets.

@anxp
Created January 30, 2019 20:28
Show Gist options
  • Save anxp/d7f1ea324b8c42729298053384e5df91 to your computer and use it in GitHub Desktop.
Save anxp/d7f1ea324b8c42729298053384e5df91 to your computer and use it in GitHub Desktop.
Checking for EXTERNAL database and table existing by Drupal 7 API. First function check both, while second and third are just wrappers for initial one.
<?php
$db_connection_data = = [
'host' => 'localhost',
'database' => 'example_db',
'username' => 'root',
'password' => '',
'driver' => 'mysql',
];
//This function checks DATABASE and\or TABLE existing.
//The main idea how to check if DATABASE exists (by Drupal DB API, not pure PHP) is to check any table existing (by db_table_exists()),
//and see what happened. Function db_table_exists() returns TRUE or FALSE if DATABASE accessible, or throw an exception if
//there are no DATABASE, or it is not accessible.
//$db_connection_data is the same config array like in "settings.local.php", $table_name can be any string if we check only for DATABASE existing.
function database_and_table_existing_check(array $db_connection_data, $table_name) {
$response = [];
if (!isset($db_connection_data['host']) || !isset($db_connection_data['database']) || !isset($db_connection_data['username']) ||
!isset($db_connection_data['password']) || !isset($db_connection_data['driver'])) {
$response['error'] = 'Incomplete settings array. Check settings array (variable $db_connection_data in database_and_table_existing_check() function).';
return $response;
}
Database::addConnectionInfo('temporary_db_key', 'default', $db_connection_data);
//Set active another\external database:
db_set_active('temporary_db_key');
try {
//Function db_table_exists() returns TRUE if table exists, FALSE if not,
//and throw an exception if connection error or DB not available - that is why we do this through try-catch.
//@ before db_table_exists() is because this function not only throw an exception, but also generates warning.
$is_table_exists = @db_table_exists($table_name);
if ($is_table_exists) {
$response['table_exists'] = TRUE;
$response['db_exists'] = TRUE;
} else {
$response['table_exists'] = FALSE;
$response['db_exists'] = TRUE;
}
} catch (PDOException $e) {
$response['db_exists'] = FALSE;
$response['table_exists'] = FALSE;
}
//It's very important to revert active database to default!
db_set_active();
return $response;
}
//This is just a wrapper for database_and_table_existing_check() for checking if DB exists.
//Returns TRUE or FALSE.
function is_db_exists(array $db_connection_data) {
$response = database_and_table_existing_check($db_connection_data, 'dummy_table_name');
if (isset($response['db_exists']) && $response['db_exists'] === TRUE) {
return TRUE;
} else {
return FALSE;
}
}
//This is just a wrapper for database_and_table_existing_check() for checking if TABLE exists.
//Returns TRUE or FALSE.
function is_table_exists(array $db_connection_data, $table_name) {
$response = database_and_table_existing_check($db_connection_data, $table_name);
if (isset($response['table_exists']) && $response['table_exists'] === TRUE) {
return TRUE;
} else {
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment