[+] Classes: add PDO support if extension is activated

[*] Classes: add engine check (innodb)
This commit is contained in:
rMalie
2011-10-13 15:00:08 +00:00
parent f8fb147034
commit cf4db864d3
5 changed files with 56 additions and 16 deletions
+7 -2
View File
@@ -44,12 +44,17 @@ class ConfigurationTestCore
public static function test_phpversion()
{
return version_compare(substr(phpversion(), 0, 3), '5.1', '>=');
return version_compare(substr(phpversion(), 0, 5), '5.1.0', '>=');
}
public static function test_mysql_support()
{
return function_exists('mysql_connect');
return extension_loaded('mysql') || extension_loaded('mysqli') || extension_loaded('pdo_mysql');
}
public static function test_pdo_mysql()
{
return extension_loaded('pdo_mysql');
}
public static function test_magicquotes()
+5 -3
View File
@@ -180,8 +180,10 @@ abstract class DbCore
public static function getClass()
{
$class = 'MySQL';
if (0&&class_exists('mysqli', false))
if (extension_loaded('mysqli'))
$class = 'DbMySQLi';
else if (extension_loaded('pdo_mysql'))
$class = 'DbPDO';
return $class;
}
@@ -525,9 +527,9 @@ abstract class DbCore
* @param bool $newDbLink
* @return int
*/
public static function checkConnection($server, $user, $pwd, $db, $new_db_link = true)
public static function checkConnection($server, $user, $pwd, $db, $new_db_link = true, $engine = null)
{
return call_user_func_array(array(Db::getClass(), 'tryToConnect'), array($server, $user, $pwd, $db, $new_db_link));
return call_user_func_array(array(Db::getClass(), 'tryToConnect'), array($server, $user, $pwd, $db, $new_db_link, $engine));
}
/**
+13 -2
View File
@@ -138,11 +138,22 @@ class DbMySQLiCore extends Db
return $this->link->query('USE '.pSQL($db_name));
}
static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true)
static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null)
{
$link = @new mysqli($server, $user, $pwd, $db);
if (mysqli_connect_error())
return 1;
return (mysqli_connect_errno() == 1049) ? 2 : 1;
if (strtolower($engine) == 'innodb')
{
$sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
$result = $link->query($sql);
if (!$result)
return 4;
$row = $result->fetch_assoc();
if (!$row || strtolower($row['Value']) != 'yes')
return 4;
}
$link->close();
return 0;
}
+19 -8
View File
@@ -84,7 +84,7 @@ class DbPDOCore extends Db
*/
protected function _numRows($result)
{
return $result->rowCount();;
return $result->rowCount();
}
/**
@@ -151,17 +151,28 @@ class DbPDOCore extends Db
/**
* @see DbCore::tryToConnect()
*/
static public function tryToConnect($server, $user, $pwd, $db)
static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null)
{
try
{
$test = new PDO('mysql:dbname='.$db.';host='.$server, $user, $pwd);
$link = @new PDO('mysql:dbname='.$db.';host='.$server, $user, $pwd);
}
catch (PDOException $e)
{
return 1;
return ($e->getCode() == 1049) ? 2 : 1;
}
unset($test);
if (strtolower($engine) == 'innodb')
{
$sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
$result = $link->query($sql);
if (!$result)
return 4;
$row = $result->fetch();
if (!$row || strtolower($row['Value']) != 'yes')
return 4;
}
unset($link);
return 0;
}
@@ -172,14 +183,14 @@ class DbPDOCore extends Db
{
try
{
$test = new PDO('mysql:dbname='.$db.';host='.$server, $user, $pwd);
$link = new PDO('mysql:host='.$server, $user, $pwd);
}
catch (PDOException $e)
{
return false;
}
$result = $test->exec('SET NAMES \'utf8\'');
unset($test);
$result = $link->exec('SET NAMES \'utf8\'');
unset($link);
return ($result === false) ? false : true;
}
+12 -1
View File
@@ -136,12 +136,23 @@ class MySQLCore extends Db
return mysql_select_db($db_name, $this->link);
}
public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true)
public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null)
{
if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink))
return 1;
if (!@mysql_select_db($db, $link))
return 2;
if (strtolower($engine) == 'innodb')
{
$sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
$result = mysql_query($sql);
if (!$result)
return 4;
$row = mysql_fetch_assoc($result);
if (!$row || strtolower($row['Value']) != 'yes')
return 4;
}
@mysql_close($link);
return 0;
}