Skip to content

Commit

Permalink
数据库完善,当连接错误时不会在异常堆叠里把数据库信息给暴露出来
Browse files Browse the repository at this point in the history
数据库支持 new Database('mysql://root:[email protected]/test'); 这样的快速调用写法
  • Loading branch information
breath-co2 committed Jan 7, 2013
1 parent 088888b commit 6cb390f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 30 deletions.
23 changes: 21 additions & 2 deletions core/classes/myqee/database.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class MyQEE_Database extends Database_QueryBuilder
/**
* 返回数据库实例化对象
*
* 支持 Database::instance('mysqli://root:[email protected]/myqee/'); 的方式
*
* @param string $config_name
* @return Database
*/
Expand All @@ -110,7 +112,9 @@ public static function instance($config_name = 'default')
}

/**
* Sets the initial columns to select from.
* new Database('default');
* 支持
* new Database('mysqli://root:[email protected]/myqee/'); 的方式
*
* @param array column list
* @return void
Expand All @@ -121,10 +125,25 @@ public function __construct($config_name = 'default')
{
$this->config = $config_name;
}
elseif (false!==strpos($config_name,'://'))
{
list($type) = explode('://', $config_name);

$this->config = array
(
'type' => $type,
'connection' => $config_name,
'table_prefix' => '',
'charset' => 'utf8',
'caching' => false,
'profiling' => true,
);
}
else
{
$this->config = Core::config('database.' . $config_name);
}

$this->config['charset'] = strtoupper($this->config['charset']);
if ( !isset($this->config['auto_change_charset']) )
{
Expand Down Expand Up @@ -567,7 +586,7 @@ public static function parse_dsn($dsn)
if ( isset($connection['path']) && $connection['path'] )
{
// Strip leading slash
$db['database'] = substr($connection['path'], 1);
$db['database'] = trim(trim(substr($connection['path'], 1),'/'));
}
}

Expand Down
29 changes: 24 additions & 5 deletions core/classes/myqee/database/driver/mongo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,34 @@ protected function _connect()
$options['persist'] = is_string($persistent)?$persistent:'x';
}

if ($username)
$error_code = 0;
$error_msg = '';
try
{
$tmplink = new Mongo("mongodb://{$username}:{$password}@{$hostname}:{$port}/",$options);
if ($username)
{
$tmplink = new Mongo("mongodb://{$username}:{$password}@{$hostname}:{$port}/",$options);
}
else
{
$tmplink = new Mongo("mongodb://{$hostname}:{$port}/",$options);
}
}
else
catch (Exception $e)
{
$tmplink = new Mongo("mongodb://{$hostname}:{$port}/",$options);
$error_msg = $e->getMessage();
$error_code = $e->getCode();
$tmplink = false;
}

if (false===$tmplink)
{
if (!IS_DEBUG)
{
$error_msg = 'connect mongodb server error.';
}
throw new Exception($error_msg, $error_code);
}
if (false===$tmplink)throw new Exception('connect mongodb server error.');

if (null!==$readpreference)
{
Expand Down
35 changes: 27 additions & 8 deletions core/classes/myqee/database/driver/mysql.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,36 @@ protected function _connect()
{
$time = microtime(true);

if ( empty($persistent) )
$error_code = 0;
$error_msg = '';
try
{
$tmplink = mysql_connect($hostname . ($port && $port != 3306 ? ':' . $port : ''), $username, $password, true);
if ( empty($persistent) )
{
$tmplink = mysql_connect($hostname . ($port && $port != 3306 ? ':' . $port : ''), $username, $password, true);
}
else
{
$tmplink = mysql_pconnect($hostname . ($port && $port != 3306 ? ':' . $port : ''), $username, $password);
}
}
else
catch (Exception $e)
{
$tmplink = mysql_pconnect($hostname . ($port && $port != 3306 ? ':' . $port : ''), $username, $password);
$error_msg = $e->getMessage();
$error_code = $e->getCode();
$tmplink = false;
}

if (false===$tmplink)
{
if ( !( $error_msg && 2===$error_code && preg_match('#(Unknown database|Access denied for user)#i', $error_msg)) )
{
$error_msg = 'connect mysql server error.';
}
throw new Exception($error_msg, $error_code);
}
if (false===$tmplink)throw new Exception('connect mysql server error.');

Core::debug()->info('mysql://'.$username.'@'.$hostname.'/ connection time:' . (microtime(true) - $time));
if (IS_DEBUG)Core::debug()->info('mysql://'.$username.'@'.$hostname.'/ connection time:' . (microtime(true) - $time));

# 连接ID
$this->_connection_ids[$this->_connection_type] = $_connection_id;
Expand All @@ -208,10 +227,10 @@ protected function _connect()
$last_error = new Exception('connect mysql server error',$e->getCode());
}

if (2===$e->getCode() && preg_match('#(Unknown database|Access denied for user)#i', $e->getMessage()))
if (2===$e->getCode() && preg_match('#(Unknown database|Access denied for user)#i', $e->getMessage(),$m))
{
// 指定的库不存在,直接返回
throw $e;
throw new Exception(strtolower($m[1])=='unknown database'?__('The mysql database does not exist'):__('The mysql database account or password error'));
}
else
{
Expand Down
40 changes: 30 additions & 10 deletions core/classes/myqee/database/driver/mysqli.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,39 @@ protected function _connect()
try
{
$time = microtime(true);
if ( empty($persistent) )

$error_code = 0;
$error_msg = '';
try
{
if ( empty($persistent) )
{
$tmplink = mysqli_init();
mysqli_options($tmplink, MYSQLI_OPT_CONNECT_TIMEOUT, 3);
mysqli_real_connect($tmplink, $hostname, $username, $password, $database, $port, null, MYSQLI_CLIENT_COMPRESS);
}
else
{
$tmplink = new mysqli($hostname, $username, $password, $database, $port);
}
}
catch (Exception $e)
{
$tmplink = mysqli_init();
mysqli_options($tmplink, MYSQLI_OPT_CONNECT_TIMEOUT, 3);
mysqli_real_connect($tmplink, $hostname, $username, $password, $database, $port, null, MYSQLI_CLIENT_COMPRESS);
$error_msg = $e->getMessage();
$error_code = $e->getCode();
$tmplink = false;
}
else

if (false===$tmplink)
{
$tmplink = new mysqli($hostname, $username, $password, $database, $port);
if ( !( $error_msg && 2===$error_code && preg_match('#(Unknown database|Access denied for user)#i', $error_msg)) )
{
$error_msg = 'connect mysqli server error.';
}
throw new Exception($error_msg, $error_code);
}
if (false===$tmplink)throw new Exception('connect mysqli server error.');

Core::debug()->info('mysqli://'.$username.'@'.$hostname.':'.$port.'/'.$database.'/ connection time:' . (microtime(true) - $time));
if (IS_DEBUG)Core::debug()->info('mysqli://'.$username.'@'.$hostname.':'.$port.'/'.$database.'/ connection time:' . (microtime(true) - $time));

# 连接ID
$this->_connection_ids[$this->_connection_type] = $_connection_id;
Expand All @@ -211,10 +231,10 @@ protected function _connect()
$last_error = new Exception('connect mysqli server error',$e->getCode());
}

if ( 2===$e->getCode() && preg_match('#(Unknown database|Access denied for user)#i', $e->getMessage()) )
if ( 2===$e->getCode() && preg_match('#(Unknown database|Access denied for user)#i', $e->getMessage() , $m) )
{
// 指定的库不存在,直接返回
throw $e;
throw new Exception(strtolower($m[1])=='unknown database'?__('The mysql database does not exist'):__('The mysql database account or password error'));
}
else
{
Expand Down
27 changes: 22 additions & 5 deletions core/classes/myqee/database/driver/sqlite.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,32 @@ protected function _connect()
Database_Driver_SQLite::$_current_connection_id_to_db[$_connection_id] = Core::debug_path($db);

$time = microtime(true);
if ($persistent)
try
{
$tmplink = sqlite_popen($db);
if ($persistent)
{
$tmplink = sqlite_popen($db);
}
else
{
$tmplink = sqlite_open($db);
}
}
else
catch (Exception $e)
{
$tmplink = sqlite_open($db);
$error_msg = $e->getMessage();
$error_code = $e->getCode();
$tmplink = false;
}

if (false===$tmplink)
{
if (!IS_DEBUG)
{
$error_msg = 'open sqlite error.';
}
throw new Exception($error_msg, $error_code);
}
if (false===$tmplink)throw new Exception('open sqlite error.');

if (IS_DEBUG)Core::debug()->info('sqlite '.Core::debug_path($db).' connection time:' . (microtime(true) - $time));

Expand Down

0 comments on commit 6cb390f

Please sign in to comment.