Well since it is going to be a community project I see no reason not to, like I said, but some things are best kept underground. I will post the database file since there is nothing special about it and maybe somebody will have some suggestions on how to improve it.For more feedback perhaps you should post some source code or a subversion repository or something.
Cheers.
Code: Select all
<?php
class Database {
const MAX_FETCH_ROWS = 50;
private $_fetchRowOffset = 0;
private $_fetchRows = 30;
private $_allowedTypes = array('numeric', 'str', 'string');
private $_link;
private $_dataHandler;
static private $_instance = NULL;
private $_table;
private $_tables;
private $_queuedFields = array();
private $_queuedValues = array();
private $_result;
private $_sql;
private $_where;
final private function __construct () {
$this->_dataHandler = Data_Handler::GetInstance();
$this->_link = new mysqli;
$this->_link->init();
if (!$this->_dbConnect()) {
$this->_catchError('Critical exception on start procedure: framework unable to initiate database connection. Framework died, unable to recover.', TRUE);
}
$this->setFetchLimit($this->_fetchRows);
}
private function __set ($variable, $value) {
$this->_catchError("Minor exception on start procedure: $$variable does not exist. Framework recovered from error.");
}
private function _catchError ($error, $die = FALSE) {
file_put_contents(ROOT . 'error.log', "$error\r\n", FILE_APPEND);
if ($die) die();
}
static public function getInstance () {
if (self::$_instance == NULL) self::$_instance = new Database();
return self::$_instance;
}
final private function _dbConnect () {
return $this->_link->real_connect('127.0.0.1', 'root', 't0sara51947', 'ape');
}
final private function _query () {
if (!$this->_result = $this->_link->query($this->_sql)) {
$this->_catchError("Query failed: $this->_sql");
return FALSE;
}
return TRUE;
}
private function _escape ($sql) {
if (get_magic_quotes_gpc()) $sql = stripslashes($sql);
return $this->_link->real_escape_string($sql);
}
public function useTable ($table) {
$table = $this->_escape($table);
$this->_sql = "SELECT '' FROM `" . $table . "` LIMIT 0";
if ($this->_query()) {
$this->_table = $table;
$this->_sql = '';
return TRUE;
}
$this->_sql = '';
return FALSE;
}
public function useTables () {
$tableCount = func_num_args();
$setTables = func_get_args();
for ($i = 0; $i < $tableCount; ++$i) {
$currentTable = $this->_escape($setTables[$i]);
$this->_sql = "SELECT '' FROM `" . $currentTable . "` LIMIT 0";
if ($this->_query()) $tables[] = $currentTable;
else return FALSE;
}
$this->_tables = '`' . implode('`, `', $tables) . '`';
return TRUE;
}
public function useRef ($field, $value, $match = '') {
if (empty($match)) $match = "= '%s'";
if (empty($this->_where)) {
$this->_where = sprintf(" WHERE `%s` $match", $this->_escape($field), $this->_escape($value));
}
else {
$this->_where .= sprintf(" AND `%s` $match", $this->_escape($field), $this->_escape($value));
}
}
public function setTableData ($field, $value, $type = '') {
if (empty($field)) return FALSE;
if (!empty($type)) {
if (!in_array($type, $this->_allowedTypes)) return FALSE;
if (!$this->_dataHandler->checkType($value, $type)) return FALSE;
}
$this->_queuedFields[] = $this->_escape($field);
$this->_queuedValues[] = $this->_escape($value);
return TRUE;
}
public function insertTableData () {
if (!isset($this->_queuedFields[0])) return FALSE;
$this->_queuedFields = '`' . implode('`, `', $this->_queuedFields) . '`';
$this->_queuedValues = "'" . implode("', '", $this->_queuedValues) . "'";
$this->_sql = "INSERT INTO `$this->_table` ($this->_queuedFields) VALUES ($this->_queuedValues)";
if ($this->_query()) {
$this->_freeBuffer();
return TRUE;
}
return FALSE;
}
public function updateTableData () {
if (!isset($this->_queuedFields[0])) return FALSE;
$fieldCounter = count($this->_queuedFields);
for ($i = 0; $i < $fieldCounter; ++$i) {
$this->_sql[] = '`' . $this->_queuedFields[$i] . "` = '" . $this->_queuedValues[$i] . "'";
}
$this->_sql = implode (', ', $this->_sql);
$this->_sql = "UPDATE `$this->_table` SET $this->_sql $this->_where LIMIT 1";
if ($this->_query()) {
$this->_freeBuffer();
return TRUE;
}
return FALSE;
}
public function setFetchLimit ($limit) {
if (!$this->_dataHandler->checkType($limit, 'int')) return FALSE;
if ($limit > self::MAX_FETCH_ROWS) $this->_fetchRows = self::MAX_FETCH_ROWS;
elseif (!$limit) return FALSE;
else $this->_fetchRows = $limit;
}
public function setFetchOffset ($offset) {
if (!$this->_dataHandler->checkType($offset, 'int')) return FALSE;
$this->_fetchRowOffset = $offset;
}
public function fetchSingle ($table, $field) {
$this->_sql = 'SELECT `' . $this->_escape($field) . '` FROM `' . $this->_escape($table) . "` $this->_where LIMIT 1";
if ($this->_query()) return array_pop(mysqli_fetch_row($this->_result));
else return FALSE;
}
public function fetchRow ($table) {
$this->_sql = 'SELECT * FROM `' . $this->_escape($table) . "` $this->_where LIMIT 1";
if ($this->_query()) return mysqli_fetch_assoc($this->_result);
else return FALSE;
}
public function fetchField ($table, $field) {
$this->_sql = 'SELECT `' . $this->_escape($field) . '` FROM `' . $this->_escape($table) . "` $this->_where LIMIT $this->_fetchRowOffset, $this->_fetchRows";
if ($this->_query()) {
$field = array();
while ($row = mysqli_fetch_row($this->_result)) $field[] = $row[0];
return $field;
}
else return FALSE;
}
public function fetchRange ($table, $fields) {
$fieldArgs = func_get_args();
$fieldCount = func_num_args() - 1;
array_shift($fieldArgs);
array_walk($fieldArgs, array($this, '_escape'));
$fields = implode("`, `", $fieldArgs);
$this->_sql = "SELECT `$fields` FROM `" . $this->_escape($table) . "` $this->_where LIMIT $this->_fetchRowOffset, $this->_fetchRows";
if ($this->_query()) {
while ($row = mysqli_fetch_assoc($this->_result)) {
$fields = array();
foreach ($fieldArgs as $field) $fields[$field] = $row[$field];
$rows[] = $fields;
}
return $rows;
}
else return FALSE;
}
private function _freeBuffer () {
$this->_queuedFields = array();
$this->_queuedValues = array();
$this->_where = '';
}
}
define('REF_FULL_MATCH', "= '%s'");
define('REF_PART_MATCH', "LIKE '%%%s%%'");
define('REF_LEFT_MATCH', "LIKE '%%%s'");
define('REF_RIGHT_MATCH', "LIKE '%s%%'");
define('REF_FULL_OMIT', "!= '%s'");
define('REF_PART_OMIT', "NOT LIKE '%s'");
$db = Database::getInstance();
?>