Commit 0c95e0ed authored by Alex Ne's avatar Alex Ne

Коррекция

parent 5fbcc180
......@@ -38,7 +38,7 @@ class Memcache {
}
$key = $this->prefix . $key;
return $this->mcache->set(md5($key), $value, 0, $expire);
return $this->mcache->set($key, $value, 0, $expire);
}
/**
......@@ -50,7 +50,7 @@ class Memcache {
}
$key = $this->prefix . $key;
return $this->mcache->get(md5($key), 0);
return $this->mcache->get($key, 0);
}
}
?>
\ No newline at end of file
<?php
namespace X\DB;
class TableItem
{
class TableItem {
/**
* @var mixed
*/
protected $table_name;
/**
* @var string
*/
protected $sql = "";
/**
* @var string
*/
protected $sql_where = "";
/**
* @var string
*/
protected $sql_order = "";
/**
* @var string
*/
protected $sql_limit = "";
/**
* @var mixed
*/
protected $sql_type;
/**
* @var mixed
*/
protected $driver = null;
function __construct($name, $driver)
{
$this->driver = $driver;
/**
* @param $name
* @param $driver
*/
public function __construct($name, $driver) {
$this->driver = $driver;
$this->table_name = $name;
}
public function insert($Data)
{
$this->sql = "INSERT INTO ".$this->build_insert($Data);
/**
* @param $Data
* @return mixed
*/
public function insert($Data) {
$this->sql = "INSERT INTO " . $this->build_insert($Data);
$this->sql_type = "insert";
return $this;
}
public function replace($Data)
{
$this->sql = "REPLACE INTO ".$this->build_insert($Data);
/**
* @param $Data
* @return mixed
*/
public function replace($Data) {
$this->sql = "REPLACE INTO " . $this->build_insert($Data);
$this->sql_type = "insert";
return $this;
}
private function build_insert($Data)
{
$keys = "`".implode('`,`', array_keys($Data))."`";
/**
* @param $Data
*/
private function build_insert($Data) {
$keys = "`" . implode('`,`', array_keys($Data)) . "`";
$_values = array_values($Data);
for ($i=0; $i < count($_values); $i++)
{
for ($i = 0; $i < count($_values); $i++) {
$_values[$i] = $this->escape($_values[$i]);
}
$values = "'".implode("','", $_values)."'";
$values = "'" . implode("','", $_values) . "'";
return "`{$this->table_name}` ({$keys}) VALUES ({$values})";
}
public function update($Data)
{
if( !is_array($Data) || count($Data) == 0 ) return false;
/**
* @param $Data
*/
public function update($Data) {
if ( ! is_array($Data) || count($Data) == 0) {
return false;
}
$params = "";
foreach ($Data as $key => $value) {
$params .= strlen($params)==0?"":", ";
$params .= "`".$key."` = '".$this->escape($value)."'";
$params .= strlen($params) == 0 ? "" : ", ";
$params .= "`" . $key . "` = '" . $this->escape($value) . "'";
}
$this->sql = "UPDATE `{$this->table_name}` SET {$params}";
$this->sql = "UPDATE `{$this->table_name}` SET {$params}";
$this->sql_type = "update";
return $this;
}
public function select($columns = [])
{
if(count($columns)>0)
$columns = "`".implode("`,`", $columns)."`";
else $columns = "*";
public function select() {
$columns = func_get_args();
if (count($columns) == 1 && is_array($columns[0])) {
$columns = "`" . implode("`,`", $columns[0]) . "`";
} else if (count($columns) > 1) {
$columns = "`" . implode("`,`", $columns) . "`";
} else {
$columns = "*";
}
$this->sql_type = "select";
$this->sql = "SELECT {$columns} FROM `{$this->table_name}`";
$this->sql = "SELECT {$columns} FROM `{$this->table_name}`";
return $this;
}
public function where($data)
{
/**
* @param $data
* @return mixed
*/
public function where($data) {
$this->sql_where = " WHERE " . $this->where_clause($data);
return $this;
}
private function where_clause($data, $operator = false, $gr=false)
{
/**
* @param $data
* @param $operator
* @param false $gr
* @return mixed
*/
private function where_clause($data, $operator = false, $gr = false) {
$where = "";
if(is_array($data))
{
if($operator == false && count($data)==2)
{
$where .= $this->where_operation($data[0], $data[1])." ";
}
else
{
if (is_array($data)) {
if ($operator == false && count($data) == 2) {
$where .= $this->where_operation($data[0], $data[1]) . " ";
} else {
$operations = [];
foreach ($data as $key => $value)
{
if( strtolower($key) == "and" || strtolower($key) == "or" )
$operations[] = (($gr)?"(":"").
$this->where_clause($value, strtolower($key),true).
(($gr)?") ":" ");
else $operations[] = $this->where_operation($key, $value);
foreach ($data as $key => $value) {
if (strtolower($key) == "and" || strtolower($key) == "or") {
$operations[] = (($gr) ? "(" : "") .
$this->where_clause($value, strtolower($key), true) .
(($gr) ? ") " : " ");
} else {
$operations[] = $this->where_operation($key, $value);
}
}
$where .= implode(" ".strtoupper($operator)." ", $operations);
$where .= implode(" " . strtoupper($operator) . " ", $operations);
}
return $where;
}
if(is_string($data)) return $data;
if (is_string($data)) {
return $data;
}
}
private function array_quote($array)
{
$temp = array();
/**
* @param $array
*/
private function array_quote($array) {
$temp = [];
foreach ($array as $value)
{
foreach ($array as $value) {
$temp[] = is_int($value) ? $value : $this->escape($value);
}
return implode($temp, ',');
}
private function where_operation($key, $value)
{
/**
* @param $key
* @param $value
*/
private function where_operation($key, $value) {
$wheres = [];
$type = gettype($value);
$type = gettype($value);
preg_match('/(#?)([\w\.\-]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<|\!?~)\])?/i', $key, $match);
$column = "`".$match[ 2 ]."`";
$column = "`" . $match[2] . "`";
if (isset($match[ 4 ]))
{
$operator = $match[ 4 ];
if (isset($match[4])) {
$operator = $match[4];
if ($operator == '!')
{
switch ($type)
{
if ($operator == '!') {
switch ($type) {
case 'NULL':
$wheres[] = $column . ' IS NOT NULL';
break;
......@@ -143,80 +191,59 @@ class TableItem
break;
case 'string':
$wheres[] = $column . ' != ' . "'".$this->escape($value)."'";
$wheres[] = $column . ' != ' . "'" . $this->escape($value) . "'";
break;
}
}
if ($operator == '<>' || $operator == '><')
{
if ($type == 'array')
{
if ($operator == '><')
{
if ($operator == '<>' || $operator == '><') {
if ($type == 'array') {
if ($operator == '><') {
$column .= ' NOT';
}
if (is_numeric($value[ 0 ]) && is_numeric($value[ 1 ]))
{
$wheres[] = '(' . $column . ' BETWEEN ' . $value[ 0 ] . ' AND ' . $value[ 1 ] . ')';
}
else
{
$wheres[] = '(' . $column . ' BETWEEN ' . "'".$this->escape($value[ 0 ])."'" . ' AND ' . "'".$this->escape($value[ 1 ])."'" . ')';
if (is_numeric($value[0]) && is_numeric($value[1])) {
$wheres[] = '(' . $column . ' BETWEEN ' . $value[0] . ' AND ' . $value[1] . ')';
} else {
$wheres[] = '(' . $column . ' BETWEEN ' . "'" . $this->escape($value[0]) . "'" . ' AND ' . "'" . $this->escape($value[1]) . "'" . ')';
}
}
}
if ($operator == '~' || $operator == '!~')
{
if ($type != 'array')
{
$value = array($value);
if ($operator == '~' || $operator == '!~') {
if ($type != 'array') {
$value = [$value];
}
$like_clauses = array();
$like_clauses = [];
foreach ($value as $item)
{
$item = strval($item);
foreach ($value as $item) {
$item = strval($item);
$suffix = mb_substr($item, -1, 1);
if ($suffix === '_')
{
if ($suffix === '_') {
$item = substr_replace($item, '%', -1);
}
elseif ($suffix === '%')
{
} else if ($suffix === '%') {
$item = '%' . substr_replace($item, '', -1, 1);
}
elseif (preg_match('/^(?!%).+(?<!%)$/', $item))
{
} else if (preg_match('/^(?!%).+(?<!%)$/', $item)) {
$item = '%' . $item . '%';
}
$like_clauses[] = $column . ($operator === '!~' ? ' NOT' : '') . ' LIKE ' . "'".$this->escape($item)."'";
$like_clauses[] = $column . ($operator === '!~' ? ' NOT' : '') . ' LIKE ' . "'" . $this->escape($item) . "'";
}
$wheres[] = implode(' OR ', $like_clauses);
}
if (in_array($operator, array('>', '>=', '<', '<=')))
{
if (is_numeric($value))
{
if (in_array($operator, ['>', '>=', '<', '<='])) {
if (is_numeric($value)) {
$wheres[] = $column . ' ' . $operator . ' ' . $value;
}
else
{
$wheres[] = $column . ' ' . $operator . ' ' . "'".$this->escape($value)."'";
} else {
$wheres[] = $column . ' ' . $operator . ' ' . "'" . $this->escape($value) . "'";
}
}
}
else
{
switch ($type)
{
} else {
switch ($type) {
case 'NULL':
$wheres[] = $column . ' IS NULL';
break;
......@@ -235,44 +262,73 @@ class TableItem
break;
case 'string':
$wheres[] = $column . ' = ' . "'".$this->escape($value)."'";
$wheres[] = $column . ' = ' . "'" . $this->escape($value) . "'";
break;
}
}
return implode(' ', $wheres);
}
return implode(' ', $wheres);
}
public function order($coll, $type)
{
/**
* @param $coll
* @param $type
*/
public function order($coll, $type) {
$this->sql_order = " ORDER BY `{$coll}` {$type}";
return $this;
}
public function limit( $op1, $op2 = false )
{
if($op2) $this->sql_limit = " LIMIT {$op1},{$op2}";
else $this->sql_limit = " LIMIT {$op1}";
/**
* @param $op1
* @param $op2
*/
public function limit($op1, $op2 = false) {
if ($op2) {
$this->sql_limit = " LIMIT {$op1},{$op2}";
} else {
$this->sql_limit = " LIMIT {$op1}";
}
return $this;
}
private function escape($data)
{
if($this->driver instanceof \X_DB_MySQLi) return $this->driver->esc($data);
/**
* @param $data
* @return mixed
*/
private function escape($data) {
if ($this->driver instanceof \X_DB_MySQLi) {
return $this->driver->esc($data);
}
}
public function getSQL()
{
return $this->sql.$this->sql_where;
/**
* @return mixed
*/
public function getSQL() {
return $this->sql . $this->sql_where;
}
public function exec($op1=false,$op2=false,$op3=false)
{
if($this->sql_type == "update" && $this->driver instanceof \X_DB_MySQLi)
/**
* @param $op1
* @param false $op2
* @param false $op3
* @return mixed
*/
public function exec($op1 = false, $op2 = false, $op3 = false) {
if ($this->sql_type == "update" && $this->driver instanceof \X_DB_MySQLi) {
return $this->driver->rq($this->sql . $this->sql_where);
if($this->sql_type == "insert" && $this->driver instanceof \X_DB_MySQLi)
return $this->driver->insert( $this->sql );
if($this->sql_type == "select" && $this->driver instanceof \X_DB_MySQLi)
return $this->driver->get($this->sql . $this->sql_where . $this->sql_order . $this->sql_limit ,$op1,$op2,$op3);
else throw new \Exception("Internal error", 0);
}
if ($this->sql_type == "insert" && $this->driver instanceof \X_DB_MySQLi) {
return $this->driver->insert($this->sql);
}
if ($this->sql_type == "select" && $this->driver instanceof \X_DB_MySQLi) {
return $this->driver->get($this->sql . $this->sql_where . $this->sql_order . $this->sql_limit, $op1, $op2, $op3);
} else {
throw new \Exception("Internal error", 0);
}
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment