Added Database and Flatfile plugin engines for both Config and Log classes. Rewrote Main to make use of the new plugins. Updated config file definitions to make use of plugins.
162 lines
4.4 KiB
PHP
162 lines
4.4 KiB
PHP
<?php
|
|
|
|
class Sihnon_Database {
|
|
|
|
private $config;
|
|
private $dbh;
|
|
|
|
/**
|
|
* Associative array of connection parameters for the database configuration
|
|
* @var array(string=>string)
|
|
*/
|
|
private $database_config;
|
|
|
|
private $hostname;
|
|
private $username;
|
|
private $password;
|
|
private $dbname;
|
|
|
|
private $prepared_statements = array();
|
|
|
|
public function __construct($dbconfig) {
|
|
$this->database_cconfig = parse_ini_file($dbconfig);
|
|
var_dump($this->database_config);
|
|
|
|
$this->hostname = $this->getDatabaseConfig('hostname');
|
|
$this->username = $this->getDatabaseConfig('username');
|
|
$this->password = $this->getDatabaseConfig('password');
|
|
$this->dbname = $this->getDatabaseConfig('dbname');
|
|
|
|
try {
|
|
$this->dbh = new PDO("mysql:host={$this->hostname};dbname={$this->dbname}", $this->username, $this->password);
|
|
} catch (PDOException $e) {
|
|
throw new Sihnon_Exception_DatabaseConnectFailed($e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
public function __destruct() {
|
|
$this->dbh = null;
|
|
}
|
|
|
|
/**
|
|
* Returns the value of the named item from the database configuration file
|
|
*
|
|
* @param string $key Name of the setting to retrieve
|
|
*/
|
|
private function getDatabaseConfig($key) {
|
|
if (!isset($this->database_config[$key])) {
|
|
throw new Sihnon_Exception_DatabaseConfigMissing($key);
|
|
}
|
|
|
|
return $this->database_config[$key];
|
|
}
|
|
|
|
public function selectAssoc($sql, $key_col, $value_cols) {
|
|
$results = array();
|
|
|
|
foreach ($this->dbh->query($sql) as $row) {
|
|
if (is_array($value_cols)) {
|
|
$values = array();
|
|
foreach ($value_cols as $value_col) {
|
|
$values[$value_col] = $row[$value_col];
|
|
}
|
|
|
|
$results[$row[$key_col]] = $values;
|
|
} else {
|
|
$results[$row[$key_col]] = $row[$value_col];
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
public function selectList($sql, $bind_params = null) {
|
|
if ($bind_params) {
|
|
$stmt = $this->dbh->prepare($sql);
|
|
|
|
foreach ($bind_params as $param) {
|
|
$stmt->bindValue(':'.$param['name'], $param['value'], $param['type']);
|
|
}
|
|
|
|
$result = $stmt->execute();
|
|
if (!$result) {
|
|
list($dummy, $code, $message) = $stmt->errorInfo();
|
|
throw new Sihnon_Exception_DatabaseQueryFailed($message, $code);
|
|
}
|
|
|
|
return $stmt->fetchAll();
|
|
|
|
} else {
|
|
$results = array();
|
|
|
|
$result = $this->dbh->query($sql);
|
|
foreach ($result as $row) {
|
|
$results[] = $row;
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
}
|
|
|
|
public function selectOne($sql, $bind_params = null) {
|
|
$rows = $this->selectList($sql, $bind_params);
|
|
if (count($rows) != 1) {
|
|
throw new Sihnon_Exception_ResultCountMismatch(count($rows));
|
|
}
|
|
|
|
return $rows[0];
|
|
}
|
|
|
|
public function insert($sql, $bind_params = null) {
|
|
$stmt = $this->dbh->prepare($sql);
|
|
|
|
if ($bind_params) {
|
|
foreach ($bind_params as $param) {
|
|
if (isset($param['type'])) {
|
|
$stmt->bindValue(':'.$param['name'], $param['value'], $param['type']);
|
|
} else {
|
|
$stmt->bindValue(':'.$param['name'], $param['value']);
|
|
}
|
|
}
|
|
}
|
|
|
|
$result = $stmt->execute();
|
|
if (!$result) {
|
|
list($code, $dummy, $message) = $stmt->errorInfo();
|
|
throw new Sihnon_Exception_DatabaseQueryFailed($message, $code);
|
|
}
|
|
}
|
|
|
|
public function update($sql, $bind_params = null) {
|
|
$stmt = $this->dbh->prepare($sql);
|
|
|
|
if ($bind_params) {
|
|
foreach ($bind_params as $param) {
|
|
if (isset($param['type'])) {
|
|
$stmt->bindValue(':'.$param['name'], $param['value'], $param['type']);
|
|
} else {
|
|
$stmt->bindValue(':'.$param['name'], $param['value']);
|
|
}
|
|
}
|
|
}
|
|
|
|
$result = $stmt->execute();
|
|
if (!$result) {
|
|
list($code, $dummy, $message) = $stmt->errorInfo();
|
|
throw new Sihnon_Exception_DatabaseQueryFailed($message, $code);
|
|
}
|
|
}
|
|
|
|
public function errorInfo() {
|
|
return $this->dbh->errorInfo();
|
|
}
|
|
|
|
public function lastInsertId() {
|
|
return $this->dbh->lastInsertId();
|
|
}
|
|
|
|
}
|
|
|
|
?>
|