Skip to content
Browse files

add:

BasiDb class can be used with socket connection like mysql.
  • Loading branch information...
1 parent 898b158 commit 3a3319cbea4bc6a26d1092067f66e923c7a49b7f @djalmaoliveira committed
Showing with 43 additions and 5 deletions.
  1. +6 −5 framework/class/BasicDb.php
  2. +37 −0 tests/Classes/BasicDBTest.php
View
11 framework/class/BasicDb.php
@@ -19,15 +19,16 @@ static function getInstance($app_mode=null) {
}
if ( isset(Liber::$aDbConfig[$app_mode]) ) {
$config = Liber::$aDbConfig[$app_mode];
- $dsn = $config[4].':dbname='.$config[1].';host='.$config[0];
+ $host = ($config[0][0]=='/')?"unix_socket=".$config[0]:"host={$config[0]}";
+ $dsn = $config[4].":$host;dbname={$config[1]}";
try {
$o = new PDO($dsn, $config[2], $config[3]);
if ($o) {
if ($config[4] == 'mysql') {
- $o->exec("set names 'utf8'");
+ $o->exec("set names 'utf8'");
}
- }
- return $o;
+ }
+ return $o;
} catch(PDOException $e) {
trigger_error("No database connection."); // Caution: Exception message show password on stack trace.
return null;
@@ -37,7 +38,7 @@ static function getInstance($app_mode=null) {
return null;
}
}
-
+
}
?>
View
37 tests/Classes/BasicDBTest.php
@@ -0,0 +1,37 @@
+<?php
+
+include_once dirname(dirname(__FILE__)).'/include.php';
+
+class BasucDBTest extends PHPUnit_Framework_TestCase {
+
+ var $dbconf;
+
+ function SetUp() {
+ Liber::loadClass('Mailer');
+ $this->dbconf = Array('/var/run/mysqld/mysqld.sock','test','root','root', 'mysql');
+ }
+
+ function tearDown() {
+ }
+
+
+ function testMysqlSocketConnection() {
+ Liber::$aDbConfig['PROD'] = $this->dbconf;
+ Liber::$aDbConfig['DEV'] = $this->dbconf;
+ Liber::db();
+
+ $this->assertInternalType('object',Liber::db(), 'No connection using socket');
+ }
+
+ function testMysqlConnection() {
+ $this->dbconf[0] = 'localhost';
+ Liber::$aDbConfig['PROD'] = $this->dbconf;
+ Liber::$aDbConfig['DEV'] = $this->dbconf;
+ Liber::db();
+
+ $this->assertInternalType('object',Liber::db(), 'No connection using TCP/IP');
+ }
+
+
+}
+?>

0 comments on commit 3a3319c

Please sign in to comment.
Something went wrong with that request. Please try again.