| 1 |
<?php
|
| 2 |
// $Id: provision.mysql.inc,v 1.20 2009/05/12 21:23:53 anarcat Exp $
|
| 3 |
/**
|
| 4 |
* @file mysql db api extension
|
| 5 |
*
|
| 6 |
* A collection of helper functions used by the main provision hooks to accomplish their tasks.
|
| 7 |
*/
|
| 8 |
|
| 9 |
function _provision_db_connection($conn = NULL) {
|
| 10 |
static $connection = NULL;
|
| 11 |
if (!is_null($conn)) {
|
| 12 |
$connection = $conn;
|
| 13 |
}
|
| 14 |
return $connection;
|
| 15 |
}
|
| 16 |
|
| 17 |
function provision_db_connect() {
|
| 18 |
if (_provision_db_connection()) {
|
| 19 |
return TRUE;
|
| 20 |
}
|
| 21 |
$connection = @mysql_connect(drush_get_option('master_db_host'), drush_get_option('master_db_user'), drush_get_option('master_db_passwd'));
|
| 22 |
|
| 23 |
if (!$connection) {
|
| 24 |
drush_set_error('PROVISION_MASTER_DB_FAILED', dt('Could not connect to the master database server.'), 'error');
|
| 25 |
}
|
| 26 |
else {
|
| 27 |
$success = @mysql_select_db('mysql', $connection);
|
| 28 |
if ($success) {
|
| 29 |
_provision_db_connection($connection);
|
| 30 |
}
|
| 31 |
else {
|
| 32 |
drush_set_error('PROVISION_MASTER_DB_FAILED', dt('Could not select the mysql database.'), 'error');
|
| 33 |
return FALSE;
|
| 34 |
}
|
| 35 |
}
|
| 36 |
return TRUE;
|
| 37 |
}
|
| 38 |
|
| 39 |
|
| 40 |
function provision_db_query($query) {
|
| 41 |
$args = func_get_args();
|
| 42 |
array_shift($args);
|
| 43 |
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
|
| 44 |
$args = $args[0];
|
| 45 |
}
|
| 46 |
_provision_db_query_callback($args, TRUE);
|
| 47 |
$query = preg_replace_callback(PROVISION_QUERY_REGEXP, '_provision_db_query_callback', $query);
|
| 48 |
return _provision_db_query($query);
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Helper function for db_query().
|
| 53 |
*/
|
| 54 |
function _provision_db_query($query, $debug = 0) {
|
| 55 |
$result = mysql_query($query, _provision_db_connection());
|
| 56 |
|
| 57 |
if (!mysql_errno(_provision_db_connection())) {
|
| 58 |
return $result;
|
| 59 |
}
|
| 60 |
else {
|
| 61 |
drush_log( mysql_error(_provision_db_connection()) ."\nquery: ". $query, 'error');
|
| 62 |
return FALSE;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
function provision_db_result($result, $row = 0) {
|
| 67 |
if ($result && mysql_num_rows($result) > $row) {
|
| 68 |
return mysql_result($result, $row);
|
| 69 |
}
|
| 70 |
return FALSE;
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Fetch one row from the result set
|
| 75 |
*
|
| 76 |
* @see db_fetch_array
|
| 77 |
*/
|
| 78 |
function provision_db_fetch_array($result) {
|
| 79 |
if ($result) {
|
| 80 |
return mysql_fetch_array($result, MYSQL_ASSOC);
|
| 81 |
}
|
| 82 |
return $result;
|
| 83 |
}
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Indicates the place holders that should be replaced in _db_query_callback().
|
| 87 |
*/
|
| 88 |
define('PROVISION_QUERY_REGEXP', '/(%d|%s|%%|%f|%b)/');
|
| 89 |
|
| 90 |
|
| 91 |
function _provision_db_query_callback($match, $init = FALSE) {
|
| 92 |
static $args = NULL;
|
| 93 |
if ($init) {
|
| 94 |
$args = $match;
|
| 95 |
return;
|
| 96 |
}
|
| 97 |
|
| 98 |
switch ($match[1]) {
|
| 99 |
case '%d': // We must use type casting to int to convert FALSE/NULL/(TRUE?)
|
| 100 |
return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe
|
| 101 |
case '%s':
|
| 102 |
return mysql_real_escape_string(array_shift($args), _provision_db_connection());
|
| 103 |
case '%%':
|
| 104 |
return '%';
|
| 105 |
case '%f':
|
| 106 |
return (float) array_shift($args);
|
| 107 |
case '%b': // binary data
|
| 108 |
return "'" . mysql_real_escape_string(array_shift($args), _provision_db_connection()) . "'";
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
function provision_db_close() {
|
| 113 |
if (_provision_db_connection()) {
|
| 114 |
$conn = _provision_db_connection();
|
| 115 |
mysql_close($conn);
|
| 116 |
}
|
| 117 |
}
|