00001 <?php
00002
00003
00014
00015 require_once './includes/database.mysql-common.inc';
00016
00020 function db_status_report($phase) {
00021 $t = get_t();
00022
00023 $version = db_version();
00024
00025 $form['mysql'] = array(
00026 'title' => $t('MySQL database'),
00027 'value' => ($phase == 'runtime') ? l($version, 'admin/reports/status/sql') : $version,
00028 );
00029
00030 if (version_compare($version, DRUPAL_MINIMUM_MYSQL) < 0) {
00031 $form['mysql']['severity'] = REQUIREMENT_ERROR;
00032 $form['mysql']['description'] = $t('Your MySQL Server is too old. Drupal requires at least MySQL %version.', array('%version' => DRUPAL_MINIMUM_MYSQL));
00033 }
00034
00035 return $form;
00036 }
00037
00043 function db_version() {
00044 list($version) = explode('-', mysql_get_server_info());
00045 return $version;
00046 }
00047
00051 function db_connect(a>) {
00052 a> = parse_url(a>);
00053
00054
00055 if (!function_exists('mysql_connect')) {
00056 _db_error_page('Unable to use the MySQL database because the MySQL extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.');
00057 }
00058
00059
00060 a>['user'] = urldecode(a>['user']);
00061
00062 a>['pass'] = isset(a>['pass']) ? urldecode(a>['pass']) : '';
00063 a>['host'] = urldecode(a>['host']);
00064 a>['path'] = urldecode(a>['path']);
00065
00066
00067 if (isset(a>['port'])) {
00068 a>['host'] = a>['host'] . ':' . a>['port'];
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 $connection = @mysql_connect(a>['host'], a>['user'], a>['pass'], TRUE, 2);
00078 if (!$connection || !mysql_select_db(substr(a>['path'], 1))) {
00079
00080 _db_error_page(mysql_error());
00081 }
00082
00083 mysql_query("SET SESSION sql_mode='ANSI'", $connection);
00084
00085 mysql_query('SET NAMES "utf8"', $connection);
00086 return $connection;
00087 }
00088
00092 function _db_query($query, $debug = 0) {
00093 global $active_db, $queries, $user;
00094
00095 if (variable_get('dev_query', 0)) {
00096 list($usec, $sec) = explode(' ', microtime());
00097 $timer = (float)$usec + (float)$sec;
00098
00099
00100
00101 $bt = debug_backtrace();
00102
00103 $name = $user->uid ? $user->name : variable_get('anonymous', 'Anonymous');
00104
00105 $name = str_replace(array('*', '/'), '', $name);
00106 $query = '/* ' . $name . ' : ' . $bt[2]['function'] . ' */ ' . $query;
00107 }
00108
00109 $result = mysql_query($query, $active_db);
00110
00111 if (variable_get('dev_query', 0)) {
00112 $query = $bt[2]['function'] . "\n" . $query;
00113 list($usec, $sec) = explode(' ', microtime());
00114 $stop = (float)$usec + (float)$sec;
00115 $diff = $stop - $timer;
00116 $queries[] = array($query, $diff);
00117 }
00118
00119 if ($debug) {
00120 print '<p>query: ' . $query . '<br />error:' . mysql_error($active_db) . '</p>';
00121 }
00122
00123 if (!mysql_errno($active_db)) {
00124 return $result;
00125 }
00126 else {
00127
00128 ${DB_ERROR} = TRUE;
00129 trigger_error(check_plain(mysql_error($active_db) . "\nquery: " . $query), E_USER_WARNING);
00130 return FALSE;
00131 }
00132 }
00133
00143 function db_fetch_object($result) {
00144 if ($result) {
00145 return mysql_fetch_object($result);
00146 }
00147 }
00148
00159 function db_fetch_array($result) {
00160 if ($result) {
00161 return mysql_fetch_array($result, MYSQL_ASSOC);
00162 }
00163 }
00164
00176 function db_result($result) {
00177 if ($result && mysql_num_rows($result) > 0) {
00178
00179
00180 $array = mysql_fetch_row($result);
00181 return $array[0];
00182 }
00183 return FALSE;
00184 }
00185
00189 function db_error() {
00190 global $active_db;
00191 return mysql_errno($active_db);
00192 }
00193
00197 function db_affected_rows() {
00198 global $active_db;
00199 return mysql_affected_rows($active_db);
00200 }
00201
00230 function db_query_range($query) {
00231 $args = func_get_args();
00232 $count = array_pop($args);
00233 $from = array_pop($args);
00234 array_shift($args);
00235
00236 $query = db_prefix_tables($query);
00237 if (isset($args[0]) and is_array($args[0])) {
00238 $args = $args[0];
00239 }
00240 _db_query_callback($args, TRUE);
00241 $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
00242 $query .= ' LIMIT ' . (int)$from . ', ' . (int)$count;
00243 return _db_query($query);
00244 }
00245
00278 function db_query_temporary($query) {
00279 $args = func_get_args();
00280 $tablename = array_pop($args);
00281 array_shift($args);
00282
00283 $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' Engine=HEAP SELECT', db_prefix_tables($query));
00284 if (isset($args[0]) and is_array($args[0])) {
00285 $args = $args[0];
00286 }
00287 _db_query_callback($args, TRUE);
00288 $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
00289 return _db_query($query);
00290 }
00291
00300 function db_encode_blob($data) {
00301 global $active_db;
00302 return "'" . mysql_real_escape_string($data, $active_db) . "'";
00303 }
00304
00313 function db_decode_blob($data) {
00314 return $data;
00315 }
00316
00320 function db_escape_string($text) {
00321 global $active_db;
00322 return mysql_real_escape_string($text, $active_db);
00323 }
00324
00328 function db_lock_table($table) {
00329 db_query('LOCK TABLES {' . db_escape_table($table) . '} WRITE');
00330 }
00331
00335 function db_unlock_tables() {
00336 db_query('UNLOCK TABLES');
00337 }
00338
00342 function db_table_exists($table) {
00343 return (bool) db_fetch_object(db_query("SHOW TABLES LIKE '{" . db_escape_table($table) . "}'"));
00344 }
00345
00349 function db_column_exists($table, $column) {
00350 return (bool) db_fetch_object(db_query("SHOW COLUMNS FROM {" . db_escape_table($table) . "} LIKE '" . db_escape_table($column) . "'"));
00351 }
00352
00364 function db_distinct_field($table, $field, $query) {
00365 $field_to_select = 'DISTINCT(' . $table . '.' . $field . ')';
00366
00367 return preg_replace('/(SELECT.*)(?:' . $table . '\.|\s)(?<!DISTINCT\()(?<!DISTINCT\(' . $table . '\.)' . $field . '(.*FROM )/AUsi', '\1 ' . $field_to_select . '\2', $query);
00368 }
00369