Show db.php syntax highlighted
<?php
class TA_Query {
private $id;
private $name;
private $type;
private $table;
private $WHERE;
private $LIMIT;
private $ORDER;
private $dynamic_fields = array (
'time',
'date'
);
private $params = array ();
function __construct($id, $type, $table, $name) {
$this->id = $id;
$this->name = $name;
$this->type = $type;
$this->table = $table;
}
function addParam($name, $value, $type) {
$this->params[$name . ";" . $type] = $value;
}
function increment($name, $size) {
if ($this->type != TA :: UPDATE)
return false;
$this->params[$name . ";increment"] = $size;
return true;
}
function WHERE($condition) {
$this->WHERE = "WHERE " . $condition;
}
function LIMIT($string) {
$this->LIMIT = "LIMIT " . $string;
}
function ORDER($name, $ordering) {
$this->ORDER = "ORDER BY `" . $name . "` ";
if ($ordering == TA :: ASCENDING) {
$this->ORDER .= "ASC";
} else
if ($ordering == TA :: DESCENDING) {
$this->ORDER .= "DESC";
} else {
$this->ORDER .= $ordering;
}
}
function getName() {
return $this->name;
}
function INSERT2SELECT() {
if ($this->type == TA :: INSERT) {
$query = "";
$query .= "SELECT *";
$query .= " FROM `" . $this->table . "` WHERE ";
$i = 0;
foreach ($this->params as $name => $value) {
$data = explode(";", $name);
$isDynamic = in_array((string) $data[0], $this->dynamic_fields);
if (!$isDynamic) {
$query .= "`" . $data[0] . "`=";
if ($data[1] == TA :: STRING)
$query .= "'" . $value . "' ";
else {
if (empty ($value))
$query .= "'" . $value . "' ";
else
$query .= "'" . $value . "' ";
}
}
if (!$isDynamic && $i != sizeof($this->params) - 1)
$query .= "AND ";
$i++;
}
$query .= "LIMIT 1 ";
$query .= ";";
return $query;
} else
return false;
}
function getQuery() {
$query = "";
if ($this->type == TA :: SELECT) {
$query .= "SELECT ";
if (!sizeof($this->params)) {
$query .= "*";
} else {
$i = 0;
foreach ($this->params as $name => $value) {
$data = explode(";", $name);
$query .= "`" . $data[0] . "` ";
if ($i != sizeof($this->params) - 1)
$query .= ", ";
$i++;
}
}
$query .= " FROM `" . $this->table . "` ";
} else
if ($this->type == TA :: UPDATE) {
$query .= "UPDATE ";
$query .= "`" . $this->table . "` ";
if (!sizeof($this->params)) {
exit ("NEED SOME PARAMETERS FOR UPDATE QUERY");
} else {
$i = 0;
$query .= "SET ";
foreach ($this->params as $name => $value) {
$data = explode(";", $name);
$query .= "`" . $data[0] . "`=";
if ($data[1] == "increment") {
$query .= "`" . $data[0] . "`+'" . $value . "' ";
} else
if ($data[1] == TA :: STRING)
$query .= "'" . $value . "' ";
else {
if (empty ($value))
$query .= "'" . $value . "' ";
else
//$query .= $value." ";
$query .= "'" . $value . "' ";
}
if ($i != sizeof($this->params) - 1)
$query .= ",";
$i++;
}
}
} else
if ($this->type == TA :: INSERT) {
$query .= "INSERT INTO ";
$query .= "`" . $this->table . "` ";
if (!sizeof($this->params)) {
exit ("NEED NAMES AND VALUES FOR INSERT QUERY");
} else {
$i = 0;
$query .= "(";
foreach ($this->params as $name => $value) {
$data = explode(";", $name);
$query .= "`" . $data[0] . "`";
if ($i != sizeof($this->params) - 1)
$query .= ", ";
$i++;
}
$query .= ") VALUES (";
$i = 0;
foreach ($this->params as $name => $value) {
$data = explode(";", $name);
if ($data[1] == TA :: STRING)
$query .= "'" . $value . "'";
else {
if (empty ($value))
$query .= "'" . $value . "' ";
else
$query .= "'" . $value . "' ";
//$query .= $value." ";
}
if ($i != sizeof($this->params) - 1)
$query .= ", ";
$i++;
}
$query .= ")";
}
} else
if ($this->type == TA :: DELETE) {
$query .= "DELETE FROM ";
$query .= "`" . $this->table . "` ";
}
if (!empty ($this->WHERE)) {
$query .= $this->WHERE . " ";
}
if (!empty ($this->ORDER))
$query .= $this->ORDER . " ";
if (!empty ($this->LIMIT))
$query .= $this->LIMIT . " ";
$query .= ";";
return $query;
}
}
class TA_Result {
private $result;
function __construct($result) {
$this->result = $result;
}
function getResult() {
return $this->result;
}
function FetchObject() {
$result = DB :: FetchObject($this->result);
if ($result == NULL)
$result = false;
return $result;
}
function NumRows() {
return DB :: NumRows($this->result);
}
}
class TA {
const SELECT = 1;
const INSERT = 2;
const UPDATE = 3;
const DELETE = 4;
const STRING = 10;
const NUMBER = 11;
const OTHER = 12;
const ASCENDING = 20;
const DESCENDING = 21;
const NO_RESTRICT = 30;
const ONCE = 31;
const NO_LIMIT = "NO_LIMIT";
private $currentInstance = false;
private $queries = array ();
private $results = array ();
private $qid = 0;
private $last_qid = 0;
function __construct($instance = false) {
$this->currentInstance = $instance;
}
function addQuery($type, $table, $ID = false) {
if ($this->currentInstance !== false && $this->currentInstance !== NULL && $this->currentInstance->getName() != $this->currentInstance->getInstance()) {
$table = str_replace($this->currentInstance->getName(), $this->currentInstance->getInstance(), $table);
}
if (DB :: $prefix != "xcs") {
$table = str_replace("xcs", DB :: $prefix, $table);
} else {
// ECP over XCS support
$table = str_replace("ecp", DB :: $prefix, $table);
}
$this->queries[$this->qid] = new TA_Query($this->qid, $type, $table, $ID);
$this->last_qid = $this->qid;
$this->qid++;
DB :: $queries++;
}
function removeQuery($ID = false) {
if (!sizeof($this->queries))
return false;
if ($ID === false) {
unset ($this->queries[$this->last_qid]);
if (!sizeof($this->results)) {
DB :: CloseResult($this->results[$this->last_qid]->getResult());
unset ($this->results[$this->last_qid]);
}
$this->qid--;
$this->last_qid = $this->qid;
return true;
} else {
foreach ($this->queries as $qid => $query) {
if ($query->getName() == $ID) {
unset ($this->queries[$qid]);
if (!sizeof($this->results)) {
DB :: CloseResult($this->results[$qid]->getResult());
unset ($this->results[$qid]);
}
$this->qid--;
$this->last_qid = $this->qid;
return true;
}
}
}
return false;
}
function WHERE($condition) {
$this->queries[$this->last_qid]->WHERE($condition);
}
function LIMIT($string) {
if ((int) $string > 0 && $string != TA :: NO_LIMIT)
$this->queries[$this->last_qid]->LIMIT($string);
}
function ORDER($name, $ordering = TA :: ASCENDING) {
$this->queries[$this->last_qid]->ORDER($name, $ordering);
}
function addParam($name, $type = TA :: OTHER, $in_value = null) {
if (!isset ($in_value))
return false;
if ($type == TA :: STRING) {
$out_value = addslashes((string) $in_value);
//$out_value = str_replace("\n","<br />",$out_value);
} else
if ($type == TA :: NUMBER) {
if (empty ($in_value))
$in_value = 0;
else
if ($in_value == NULL)
$in_value = 0;
$out_value = (int) $in_value;
} else
if ($type == TA :: OTHER) {
$out_value = $in_value;
}
$this->queries[$this->last_qid]->addParam($name, $out_value, $type);
return true;
}
function increment($name, $size = 1) {
return $this->queries[$this->last_qid]->increment($name, $size);
}
function Result($ID = false) {
if ($ID == false) {
return $this->results[$this->last_qid];
} else {
foreach ($this->queries as $qid => $query) {
if ($query->getName() == $ID) {
return $this->results[$qid];
}
}
}
}
function Execute($execute_only = false) {
foreach ($this->queries as $qid => $TA_Query) {
if ($execute_only === false || (is_array($execute_only) && in_array($this->queries[$qid]->getName(), $execute_only) || is_string($execute_only) && $execute_only == $this->queries[$qid]->getName())) {
//$skip_duplicate= false;
//if (($query= $this->queries[$qid]->INSERT2SELECT()) !== false) {
// $this->results[$qid]= new TA_Result(DB :: Query($query));
// if ($this->results[$qid]->NumRows()) {
// $skip_duplicate= true;
// }
//}
//if (!$skip_duplicate) {
$query = $this->queries[$qid]->getQuery();
//$query = utf8_encode($query);
$this->results[$qid] = new TA_Result(DB :: Query($query));
//}
$this->last_qid = $qid;
}
}
return true;
}
function End() {
/*if (!sizeof($this->results)) {
foreach ($this->results as $result) {
DB :: CloseResult($result->getResult());
}
}*/
unset ($this);
}
}
interface DB_interface {
const ASC = '~-asc-ending-~';
const DESC= '~-desc-ending-~';
public static function Connect($host, $username, $passwd, $dbname, $prefix = 'xcs', $port = '', $socket = '');
public static function ServerVersion();
public static function ServerInfo();
public static function SelectDB($dbname);
public static function Query($query);
public static function SetPrefix($prefix);
public static function RestorePrefix();
public static function NumRows($result = null);
public static function NumFields($result = null);
public static function FetchRow($result = null);
public static function FetchObject($result = null);
public static function FetchArray($result = null);
public static function Error();
public static function Close();
public static function EscapeString($string);
public static function CloseResult($result = null);
}
abstract class DB_abstract implements DB_interface {
public static $prefix = null;
public static $old_prefix = null;
public static $isConnected = false;
public static $queries = 0;
public static function Connect($host, $username, $passwd, $dbname, $prefix = 'xcs', $port = '', $socket = '') {
self :: $isConnected = true;
}
public static function ServerVersion() {
}
public static function ServerInfo() {
}
public static function SelectDB($dbname) {
}
public static function Query($query) {
}
public static function MultiQuery($queries) {
$inStr = false;
$query = "";
@ set_time_limit(5000);
for ($pos = 0; $pos != strlen($queries); $pos++) {
$char = substr($queries, $pos, 1);
$query .= $char;
if ($char == "'" && !$inStr)
$inStr = true;
else
if ($char == "'" && $inStr)
$inStr = false;
if ($char == ";" && !$inStr) {
DB :: Query($query);
$query = "";
}
}
}
public static function SetPrefix($prefix) {
if (self :: $prefix == $prefix)
return false;
self :: $old_prefix = self :: $prefix;
//self :: $prefix= $prefix;
return true;
}
public static function RestorePrefix() {
if (!self :: $old_prefix)
return false;
self :: $prefix = self :: $old_prefix;
return true;
}
public static function NumRows($result = null) {
}
public static function NumFields($result = null) {
}
public static function FetchRow($result = null) {
}
public static function FetchObject($result = null) {
}
public static function FetchArray($result = null) {
}
public static function Error() {
}
public static function Close() {
}
public static function EscapeString($string) {
return String :: Escape($string);
}
public static function CloseResult($result = null) {
}
public static function Select($table, $object) {
$currentModule = Engine :: getFlag('currentModule');
$TA = new TA($currentModule);
$TA->addQuery(TA :: SELECT, $table);
$condition = '';
$index = 0;
foreach ($object as $name => $value) {
$index++;
}
$sizeof = $index;
$index = 0;
foreach ($object as $name => $value) {
$nextObject = next($object);
prev($object);
if ($value == DB :: ASC) {
$TA->ORDER($name, TA :: ASCENDING);
} else
if ($value == DB :: DESC) {
$TA->ORDER($name, TA :: DESCENDING);
} else {
if ($index != 0 && $index < ($sizeof -1) && ($nextObject != DB :: ASC || $nextObject != DB :: DESC)) {
$condition .= ' AND ';
}
$condition .= "`" . $name . "`='" . self :: EscapeString($value) . "'";
}
$index++;
}
if (!empty ($condition))
$TA->WHERE($condition);
$TA->LIMIT(1);
$TA->Execute();
$Array = array ();
while ($object = $TA->Result()->FetchObject()) {
$Array[] = $object;
}
if (sizeof($Array)) {
if (sizeof($Array) == 1) {
$result = $Array[0];
} else {
$result = $Array;
}
} else {
$result = false;
}
$TA->End();
return $result;
}
public static function DeleteById($table, $id, $module = false) {
$currentModule = Engine :: getFlag('currentModule');
if (!$id)
return false;
if ($module === false && $currentModule !== false)
$module = $currentModule;
$TA = new TA($module);
$TA->addQuery(TA :: DELETE, $table);
$TA->WHERE("`id`='" . $id . "'");
$TA->LIMIT(1);
$TA->Execute();
$TA->End();
return true;
}
public static function IncrementById($table, $id, $column, $module = false) {
$currentModule = Engine :: getFlag('currentModule');
if (!$id)
return false;
if ($module === false && $currentModule !== false)
$module = $currentModule;
$TA = new TA($module);
$TA->addQuery(TA :: UPDATE, $table);
$TA->increment($column);
$TA->WHERE("`id`='" . $id . "'");
$TA->LIMIT(1);
$TA->Execute();
$TA->End();
return true;
}
public static function UpdateById($table, $id, $params, $module = false) {
$currentModule = Engine :: getFlag('currentModule');
if (!$id)
return false;
if ($module === false && $currentModule !== false)
$module = $currentModule;
$TA = new TA($module);
$TA->addQuery(TA :: UPDATE, $table);
foreach ($params as $name => $value) {
$TA->addParam($name, TA :: STRING, $value);
}
$TA->WHERE("`id`='" . $id . "'");
$TA->Execute();
$TA->End();
return true;
}
public static function UpdateBy($table, $id, $params, $module = false) {
$currentModule = Engine :: getFlag('currentModule');
if (!$id)
return false;
if ($module === false && $currentModule !== false)
$module = $currentModule;
$TA = new TA($module);
$TA->addQuery(TA :: UPDATE, $table);
foreach ($params as $name => $value) {
$TA->addParam($name, TA :: STRING, $value);
}
$key = key($id);
$value = current($id);
$TA->WHERE("`" . $key . "`='" . $value . "'");
$TA->Execute();
$TA->End();
return true;
}
public static function Insert($table, $params, $module = false) {
$currentModule = Engine :: getFlag('currentModule');
if ($module === false && $currentModule !== false)
$module = $currentModule;
$TA = new TA($module);
$TA->addQuery(TA :: INSERT, $table);
foreach ($params as $name => $value) {
$TA->addParam($name, TA :: STRING, $value);
}
$TA->Execute();
$TA->End();
return true;
}
}
?>
See more files for this project here