summaryrefslogtreecommitdiffstats
path: root/lib/class/dba.class.php
diff options
context:
space:
mode:
authorPaul Arthur <paul.arthur@flowerysong.com>2013-01-29 00:27:58 -0500
committerPaul Arthur <paul.arthur@flowerysong.com>2013-01-29 03:08:52 -0500
commitb7c9678012aaeac44784ccf19598140bdf49d6ca (patch)
tree1b10f896a9a23adbee0cbb08ff9f8d69831b39ca /lib/class/dba.class.php
parent3010ae8c85fe8cb656c9e96a95d0efee0fb2126a (diff)
downloadampache-b7c9678012aaeac44784ccf19598140bdf49d6ca.tar.gz
ampache-b7c9678012aaeac44784ccf19598140bdf49d6ca.tar.bz2
ampache-b7c9678012aaeac44784ccf19598140bdf49d6ca.zip
Switch Dba from mysql to PDO-MySQL
The mysql extension is deprecated in PHP 5.5 and will be removed in the future.
Diffstat (limited to 'lib/class/dba.class.php')
-rw-r--r--lib/class/dba.class.php254
1 files changed, 94 insertions, 160 deletions
diff --git a/lib/class/dba.class.php b/lib/class/dba.class.php
index 11898604..d432095c 100644
--- a/lib/class/dba.class.php
+++ b/lib/class/dba.class.php
@@ -53,85 +53,67 @@ class Dba {
/**
* query
- * This is the meat of the class this does a query, it emulates
- * The mysql_query function
*/
- public static function query($sql) {
-
+ public static function query($sql, $params) {
// Run the query
- $resource = mysql_query($sql,self::dbh());
- debug_event('Query',$sql,'6');
+ if ($params) {
+ $stmt = self::dbh()->prepare($sql);
+ $stmt->execute($params);
+ }
+ else {
+ $stmt = self::dbh()->query($sql);
+ }
+
+ debug_event('Query', $sql . ' ' . json_encode($params), 6);
// Save the query, to make debug easier
self::$_sql = $sql;
self::$stats['query']++;
- // Do a little error checking here and try to recover from some forms of failure
- if (!$resource) {
- switch (mysql_errno(self::dbh())) {
- case '2006':
- case '2013':
- case '2055':
- debug_event('DBH','Lost connection to database server, trying to re-connect and hope nobody noticed','1');
- self::disconnect();
- // Try again
- $resource = mysql_query($sql,self::dbh());
- break;
- default:
- debug_event('DBH',mysql_error(self::dbh()) . ' ['. mysql_errno(self::dbh()) . ']','1');
- break;
- } // end switch on error #
- } // if failed query
-
- return $resource;
-
- } // query
+ if ($stmt->errorCode() && $stmt->errorCode() != '00000') {
+ debug_event('Dba', 'Error: ' . json_encode($stmt->errorInfo()), 1);
+ }
+
+ return $stmt;
+ }
/**
* read
- * This is a wrapper for query, it's so that in the future if we ever wanted
- * to split reads and writes we could
*/
- public static function read($sql) {
-
- return self::query($sql);
-
- } // read
+ public static function read($sql, $params = null) {
+ return self::query($sql, $params);
+ }
/**
* write
- * This is a wrapper for a write query, it is so that we can split out reads and
- * writes if we want to
*/
- public static function write($sql) {
-
- return self::query($sql);
-
- } // write
+ public static function write($sql, $params = null) {
+ return self::query($sql, $params);
+ }
/**
* escape
+ *
* This runs a escape on a variable so that it can be safely inserted
* into the sql
*/
public static function escape($var) {
-
- $string = mysql_real_escape_string($var,self::dbh());
-
- return $string;
-
- } // escape
+ $var = self::dbh()->quote($var);
+ // This is slightly ugly
+ return trim($var, "'");
+ }
/**
* fetch_assoc
- * This emulates the mysql_fetch_assoc and takes a resource result
- * we force it to always return an array, albeit an empty one
+ *
+ * This emulates the mysql_fetch_assoc.
+ * We force it to always return an array, albeit an empty one
* The optional finish parameter affects whether we automatically clean
* up the result set after the last row is read.
*/
public static function fetch_assoc($resource, $finish = true) {
- $result = mysql_fetch_assoc($resource);
+ $result = $resource->fetch(PDO::FETCH_ASSOC);
if (!$result) {
if ($finish) {
@@ -141,19 +123,19 @@ class Dba {
}
return $result;
-
- } // fetch_assoc
+ }
/**
* fetch_row
- * This emulates the mysql_fetch_row and takes a resource result
+ *
+ * This emulates the mysql_fetch_row
* we force it to always return an array, albeit an empty one
* The optional finish parameter affects whether we automatically clean
* up the result set after the last row is read.
*/
public static function fetch_row($resource, $finish = true) {
- $result = mysql_fetch_row($resource);
+ $result = $resource->fetch(PDO::FETCH_NUM);;
if (!$result) {
if ($finish) {
@@ -163,63 +145,53 @@ class Dba {
}
return $result;
-
- } // fetch_row
+ }
/**
* num_rows
+ *
* This emulates the mysql_num_rows function which is really
* just a count of rows returned by our select statement, this
- * doesn't work for updates or inserts
+ * doesn't work for updates or inserts.
*/
public static function num_rows($resource) {
if ($resource) {
- $result = mysql_num_rows($resource);
+ $result = $resource->rowCount();
if ($result) {
return $result;
}
}
return 0;
- } // num_rows
-
- /**
- * seek
- * This resets the row pointer to the specified position
- */
- public static function seek($resource, $row) {
- return mysql_data_seek($resource, $row);
}
/**
* finish
+ *
* This closes a result handle and clears the memory associated with it
*/
public static function finish($resource) {
-
- // Clear the result memory
- mysql_free_result($resource);
-
- } // finish
+ $resource->closeCursor();
+ }
/**
* affected_rows
+ *
* This emulates the mysql_affected_rows function
*/
public static function affected_rows($resource) {
-
- $result = mysql_affected_rows($resource);
+ $result = $resource->rowCount();
if (!$result) {
- return '0';
+ return 0;
}
return $result;
-
- } // affected_rows
+ }
/**
* _connect
+ *
* This connects to the database, used by the DBH function
*/
private static function _connect() {
@@ -228,43 +200,34 @@ class Dba {
$hostname = Config::get('database_hostname');
$password = Config::get('database_password');
- $dbh = mysql_connect($hostname, $username, $password);
- if (!$dbh) {
- debug_event('Database', 'Unable to connect to database: ' . mysql_error(), 1);
+ try {
+ $dbh = new PDO('mysql:host=' . $hostname, $username, $password);
+ }
+ catch (PDOException $e) {
+ debug_event('Dba', 'Connection failed: ' . $e->getMessage(), 1);
return null;
}
return $dbh;
- } // _connect
+ }
private static function _setup_dbh($dbh, $database) {
- $data = self::translate_to_mysqlcharset(Config::get('site_charset'));
-
- if (function_exists('mysql_set_charset')) {
- if (!$charset = mysql_set_charset($data['charset'], $dbh)) {
- debug_event('Database', 'Unable to set MySQL connection charset to ' . $data['charset'] . ', this may cause issues...', 1);
- }
- }
- else {
- $sql = "SET NAMES " . mysql_real_escape_string($data['charset']);
- $charset = mysql_query($sql,$dbh);
- if ($error = mysql_error($dbh)) {
- debug_event('Database', 'Unable to set MySQL connection charset to ' . $data['charset'] . ' using SET NAMES, this may cause issues: ' . $error, 1);
- }
-
+ $charset = self::translate_to_mysqlcharset(Config::get('site_charset'));
+ $charset = $charset['charset'];
+ if ($dbh->exec('SET NAMES ' . $charset) === false) {
+ debug_event('Dba', 'Unable to set connection charset to ' . $charset, 1);
}
- $select_db = mysql_select_db($database, $dbh);
- if (!$select_db) {
- debug_event('Database', 'Unable to select database ' . $database . ': ' . mysql_error(), 1);
+ if ($dbh->exec('USE `' . $database . '`') === false) {
+ debug_event('Dba', 'Unable to select database ' . $database . ': ' . json_encode($dbh->errorInfo()), 1);
}
if (Config::get('sql_profiling')) {
- mysql_query('set profiling=1', $dbh);
- mysql_query('set profiling_history_size=50', $dbh);
- mysql_query('set query_cache_type=0', $dbh);
+ $dbh->exec('SET profiling=1');
+ $dbh->exec('SET profiling_history_size=50');
+ $dbh->exec('SET query_cache_type=0');
}
- } // _select_db
+ }
/**
* check_database
@@ -272,32 +235,27 @@ class Dba {
* Make sure that we can connect to the database
*/
public static function check_database() {
-
$dbh = self::_connect();
- if (!is_resource($dbh)) {
+ if ($dbh->errorCode()) {
return false;
}
- mysql_close($dbh);
return true;
-
- } // check_database
+ }
public static function check_database_exists() {
$dbh = self::_connect();
- $select = mysql_select_db(Config::get('database_name'), $dbh);
- mysql_close($dbh);
- return $select;
+ return $dbh->exec('USE `' . Config::get('database_name') . '`');
}
/**
* check_database_inserted
- * checks to make sure that you have inserted the database
- * and that the user you are using has access to it
+ *
+ * Checks to make sure that you have inserted the database
+ * and that the user you are using has access to it.
*/
public static function check_database_inserted() {
-
$sql = "DESCRIBE session";
$db_results = Dba::read($sql);
@@ -311,11 +269,6 @@ class Dba {
}
return true;
-
- } // check_database_inserted
-
- public static function get_client_info() {
- return mysql_get_client_info();
}
/**
@@ -326,7 +279,7 @@ class Dba {
if (Config::get('sql_profiling')) {
print '<br/>Profiling data: <br/>';
- $res = Dba::read('show profiles');
+ $res = Dba::read('SHOW PROFILES');
print '<table>';
while ($r = Dba::fetch_row($res)) {
print '<tr><td>' . implode('</td><td>', $r) . '</td></tr>';
@@ -349,7 +302,7 @@ class Dba {
// Assign the Handle name that we are going to store
$handle = 'dbh_' . $database;
- if (!is_resource(Config::get($handle))) {
+ if (!is_object(Config::get($handle))) {
$dbh = self::_connect();
self::_setup_dbh($dbh, $database);
Config::set($handle, $dbh, true);
@@ -358,62 +311,48 @@ class Dba {
else {
return Config::get($handle);
}
-
-
- } // dbh
+ }
/**
* disconnect
- * This nukes the dbh connection based, this isn't used very often...
+ *
+ * This nukes the dbh connection, this isn't used very often...
*/
public static function disconnect($database='') {
-
if (!$database) {
$database = Config::get('database_name');
}
$handle = 'dbh_' . $database;
- // Try to close it correctly
- mysql_close(Config::get($handle));
-
// Nuke it
Config::set($handle, false, true);
return true;
-
- } // disconnect
+ }
/**
* insert_id
- * This emulates the mysql_insert_id function, it takes
- * an optional database target
*/
public static function insert_id() {
-
- $id = mysql_insert_id(self::dbh());
- return $id;
-
- } // insert_id
+ return self::dbh()->lastInsertId();
+ }
/**
* error
* this returns the error of the db
*/
public static function error() {
-
- return mysql_error();
-
- } // error
+ return self::dbh()->errorCode();
+ }
/**
* translate_to_mysqlcharset
- * This translates the specified charset to a mysqlcharset, stupid ass mysql
- * demands that it's charset list is different!
+ *
+ * This translates the specified charset to a mysql charset.
*/
public static function translate_to_mysqlcharset($charset) {
-
- // MySQL translte real charset names into fancy smancy MySQL land names
+ // Translate real charset names into fancy MySQL land names
switch (strtoupper($charset)) {
case 'CP1250':
case 'WINDOWS-1250':
@@ -452,21 +391,21 @@ class Dba {
$target_charset = 'utf8';
$target_collation = 'utf8_unicode_ci';
break;
- } // end mysql charset translation
+ }
return array('charset'=>$target_charset,'collation'=>$target_collation);
- } // translate_to_mysqlcharset
+ }
/**
* reset_db_charset
- * This cruises through the database and trys to set the charset to the current
- * site charset, this is an admin function that can be run by an administrator
- * this can mess up data if you switch between charsets that are not overlapping
- * a catalog verify must be re-run to correct them
+ *
+ * This cruises through the database and trys to set the charset to the
+ * current site charset. This is an admin function that can be run by an
+ * administrator only. This can mess up data if you switch between charsets
+ * that are not overlapping.
*/
public static function reset_db_charset() {
-
$translated_charset = self::translate_to_mysqlcharset(Config::get('site_charset'));
$target_charset = $translated_charset['charset'];
$target_collation = $translated_charset['collation'];
@@ -498,13 +437,10 @@ class Dba {
if (!$charset_results) {
debug_event('CHARSET','Unable to update the charset of ' . $table['Field'] . '.' . $table['Type'] . ' to ' . $target_charset,'3');
} // if it fails
- } // if its a varchar
- } // end columns
-
- } // end tables
-
-
- } // reset_db_charset
+ }
+ }
+ }
+ }
/**
* optimize_tables
@@ -527,7 +463,5 @@ class Dba {
$db_results_inner = Dba::write($sql);
}
}
-
-} // dba class
-
+}
?>