Skip to content

Commit

Permalink
Changed the way the database engine is initialized and connected
Browse files Browse the repository at this point in the history
  • Loading branch information
spotweb committed Jul 1, 2012
1 parent abde423 commit 1a74065
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 95 deletions.
53 changes: 8 additions & 45 deletions lib/SpotDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,51 +38,10 @@ function __construct($db) {
function connect() {
SpotTiming::start(__FUNCTION__);

/*
* Erase username/password so it won't show up in any stacktrace
*/
$dbCon = dbeng_abs::getDbFactory($this->_dbsettings['engine']);;
$daoFactory = Dao_Factory::getDAOFactory($this->_dbsettings['engine']);

# SQlite heeft geen username gedefinieerd
if (isset($this->_dbsettings['user'])) {
$tmpUser = $this->_dbsettings['user'];
$this->_dbsettings['user'] = '*FILTERED*';
} # if
# en ook geen pass
if (isset($this->_dbsettings['pass'])) {
$tmpPass = $this->_dbsettings['pass'];
$this->_dbsettings['pass'] = '*FILTERED*';
} # if

switch ($this->_dbsettings['engine']) {
case 'mysql' : $this->_conn = new dbeng_mysql($this->_dbsettings['host'],
$tmpUser,
$tmpPass,
$this->_dbsettings['dbname']);
$daoFactory = Dao_Factory::getDAOFactory("mysql");
break;

case 'pdo_mysql': $this->_conn = new dbeng_pdo_mysql($this->_dbsettings['host'],
$tmpUser,
$tmpPass,
$this->_dbsettings['dbname']);
$daoFactory = Dao_Factory::getDAOFactory("mysql");
break;

case 'pdo_pgsql' : $this->_conn = new dbeng_pdo_pgsql($this->_dbsettings['host'],
$tmpUser,
$tmpPass,
$this->_dbsettings['dbname']);
$daoFactory = Dao_Factory::getDAOFactory("postgresql");
break;

case 'pdo_sqlite': $this->_conn = new dbeng_pdo_sqlite($this->_dbsettings['path']);
$daoFactory = Dao_Factory::getDAOFactory("sqlite");
break;

default : throw new Exception('Unknown DB engine specified (' . $this->_dbsettings['engine'] . ', please choose pdo_pgsql, mysql or pdo_mysql');
} # switch

$daoFactory->setConnection($this->_conn);
$daoFactory->setConnection($dbCon);
$this->_auditDao = $daoFactory->getAuditDao();
$this->_blackWhiteListDao = $daoFactory->getBlackWhiteListDao();
$this->_cacheDao = $daoFactory->getCacheDao();
Expand All @@ -98,7 +57,11 @@ function connect() {
$this->_spotStateListDao = $daoFactory->getSpotStateListDao();
$this->_nntpConfigDao = $daoFactory->getNntpConfigDao();

$this->_conn->connect();
$dbCon->connect($this->_dbsettings['host'],
$this->_dbsettings['user'],
$this->_dbsettings['pass'],
$this->_dbsettings['dbname']);
$this->_conn = $dbCon;
SpotTiming::stop(__FUNCTION__);
} # connect

Expand Down
22 changes: 21 additions & 1 deletion lib/dbeng/dbeng_abs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,31 @@
abstract class dbeng_abs {
protected $_batchInsertChunks = 500;
private $_error = '';


/*
* Factory class which instantiates the specified DAO factory object
*/
public static function getDbFactory($engine) {
/*
* Erase username/password so it won't show up in any stacktrace,
* only erase them if they exist (eg: sqlite has no username and
* password)
*/
switch ($engine) {
case 'mysql' : return new dbeng_mysql(); break;
case 'pdo_mysql' : return new dbeng_pdo_mysql(); break;
case 'pdo_pgsql' : return new dbeng_pdo_pgsql(); break;
case 'pdo_sqlite' : return new dbeng_pdo_sqlite(); break;

default : throw new Exception("Unknown database engine (" . $dbSettings['engine'] . ") factory specified");
} // switch
} # getDbFactory()

/*
* Connects to the database
*/
abstract function connect();
abstract function connect($host, $user, $pass, $db);

/*
* Executes the query and discards any output. Returns true of no
Expand Down
20 changes: 5 additions & 15 deletions lib/dbeng/dbeng_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,25 @@
# a mess

class dbeng_mysql extends dbeng_abs {
private $_db_host;
private $_db_user;
private $_db_pass;
private $_db_db;

private $_conn;

function __construct($host, $user, $pass, $db)
{
$this->_db_host = $host;
$this->_db_user = $user;
$this->_db_pass = $pass;
$this->_db_db = $db;

function __construct() {
/*
* arbitrarily chosen because some insert statements might
* be very large.
*/
$this->_batchInsertChunks = 100;
}

function connect() {
$this->_conn = mysql_connect($this->_db_host, $this->_db_user, $this->_db_pass);
function connect($host, $user, $pass, $db) {
$this->_conn = mysql_connect($host, $user, $pass);

if (!$this->_conn) {
throw new DatabaseConnectionException("Unable to connect to MySQL server: " . mysql_error());
} # if


if (!@mysql_select_db($this->_db_db, $this->_conn)) {
if (!@mysql_select_db($db, $this->_conn)) {
throw new DatabaseConnectionException("Unable to select MySQL db: " . mysql_error($this->_conn));
return false;
} # if
Expand Down
20 changes: 7 additions & 13 deletions lib/dbeng/dbeng_pdo_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,24 @@ class dbeng_pdo_mysql extends dbeng_pdo {

private $_rows = 0;

function __construct($host, $user, $pass, $db)
{
$this->_db_host = $host;
$this->_db_user = $user;
$this->_db_pass = $pass;
$this->_db_db = $db;

/*
function __construct() {
/*
* arbitrarily chosen because some insert statements might
* be very large.
*/
$this->_batchInsertChunks = 100;
}

function connect() {
function connect($host, $user, $pass, $db) {
if (!$this->_conn instanceof PDO) {
if ($this->_db_host[0] === '/') {
$this->_db_conn = "unix_socket=" . $this->_db_host;
if ($host[0] === '/') {
$db_conn = "unix_socket=" . $host;
} else {
$this->_db_conn = "host=" . $this->_db_host . ";port=3306";
$db_conn = "host=" . $host . ";port=3306";
}

try {
$this->_conn = new PDO('mysql:' . $this->_db_conn . ';dbname=' . $this->_db_db, $this->_db_user, $this->_db_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this->_conn = new PDO('mysql:' . $db_conn . ';dbname=' . $db, $user, $this->pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
} catch (PDOException $e) {
throw new DatabaseConnectionException($e->getMessage(), -1);
}
Expand Down
19 changes: 4 additions & 15 deletions lib/dbeng/dbeng_pdo_pgsql.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
<?php
class dbeng_pdo_pgsql extends dbeng_pdo {
private $_db_host;
private $_db_user;
private $_db_pass;
private $_db_db;

protected $_conn;

private $_rows = 0;

function __construct($host, $user, $pass, $db)
{
$this->_db_host = $host;
$this->_db_user = $user;
$this->_db_pass = $pass;
$this->_db_db = $db;

function __construct() {
/*
* arbitrarily chosen because some insert statements might
* be very large.
Expand All @@ -34,12 +23,12 @@ function bool2dt($b) {
return 'false';
} # bool2dt

function connect() {
function connect($host, $user, $pass, $db) {
if (!$this->_conn instanceof PDO) {
$this->_db_conn = "host=" . $this->_db_host;
$db_conn = "host=" . $this->_db_host;

try {
$this->_conn = new PDO('pgsql:' . $this->_db_conn . ';dbname=' . $this->_db_db, $this->_db_user, $this->_db_pass);
$this->_conn = new PDO('pgsql:' . $db_conn . ';dbname=' . $db, $user, $pass);
} catch (PDOException $e) {
throw new DatabaseConnectionException($e->getMessage(), -1);
} # catch
Expand Down
9 changes: 3 additions & 6 deletions lib/dbeng/dbeng_pdo_sqlite.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
<?php
class dbeng_pdo_sqlite extends dbeng_pdo {
private $_db_path;
protected $_conn;

function __construct($path) {
$this->_db_path = $path;

function __construct() {
/*
* sqlite does not support batch inserts
*/
$this->_batchInsertChunks = 1;
} # ctor

function connect() {
function connect($host, $user, $pass, $db) {
try {
if (!$this->_conn instanceof PDO) {
$this->_conn = new PDO('sqlite:' . $this->_db_path);
$this->_conn = new PDO('sqlite:' . $db);
$this->_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} # if
} catch(PDOException $e) {
Expand Down

0 comments on commit 1a74065

Please sign in to comment.