基础配置与底层修改
This commit is contained in:
@@ -30,7 +30,7 @@ class Sql
|
||||
{
|
||||
$this->filter .= '';
|
||||
if ($where) {
|
||||
if(!strpos($this->filter,'WHERE') !== false){
|
||||
if (!strpos($this->filter, 'WHERE') !== false) {
|
||||
$this->filter .= ' WHERE ';
|
||||
} else {
|
||||
$this->filter .= ' AND ';
|
||||
@@ -39,14 +39,15 @@ class Sql
|
||||
$this->filter .= $where;
|
||||
} else {
|
||||
$flag = true;
|
||||
$ins = ['in', 'IN', 'not in', 'NOT IN'];
|
||||
foreach ($where as $key => $value) {
|
||||
if ($flag) {
|
||||
$flag = false;
|
||||
if (is_array($value)) {
|
||||
if($value[0] == 'in'){
|
||||
$this->filter .= ' `' . $key . '` ' . $value[0] . '('.implode(',',$value[1]).')';
|
||||
if (in_array($value[0], $ins)) {
|
||||
$this->filter .= ' `' . $key . '` ' . $value[0] . '(' . implode(',', $value[1]) . ')';
|
||||
} else {
|
||||
$param_key = $this->paramIsExit($key,$this->param);
|
||||
$param_key = $this->paramIsExit($key, $this->param);
|
||||
$this->filter .= ' `' . $key . '` ' . $value[0] . ' :' . $param_key;
|
||||
$this->param[$param_key] = $value[1];
|
||||
}
|
||||
@@ -56,10 +57,10 @@ class Sql
|
||||
}
|
||||
} else {
|
||||
if (is_array($value)) {
|
||||
if($value[0] == 'in'){
|
||||
$this->filter .= ' AND `' . $key . '` ' . $value[0] . '('.implode(',',$value[1]).')';
|
||||
if (in_array($value[0], $ins)) {
|
||||
$this->filter .= ' AND `' . $key . '` ' . $value[0] . '(' . implode(',', $value[1]) . ')';
|
||||
} else {
|
||||
$param_key = $this->paramIsExit($key,$this->param);
|
||||
$param_key = $this->paramIsExit($key, $this->param);
|
||||
$this->filter .= ' AND `' . $key . '` ' . $value[0] . ' :' . $param_key;
|
||||
$this->param[$param_key] = $value[1];
|
||||
}
|
||||
@@ -74,12 +75,13 @@ class Sql
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function paramIsExit($key,$param){
|
||||
if(!isset($param[$key])){
|
||||
public function paramIsExit($key, $param)
|
||||
{
|
||||
if (!isset($param[$key])) {
|
||||
return $key;
|
||||
} else {
|
||||
$key .= $key;
|
||||
return $this->paramIsExit($key,$param);
|
||||
return $this->paramIsExit($key, $param);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,11 +105,11 @@ class Sql
|
||||
* @return $this
|
||||
* $group=['sex','name']
|
||||
*/
|
||||
public function group($group = [])
|
||||
public function group($group = [], $having = '')
|
||||
{
|
||||
if ($group) {
|
||||
$this->filter .= ' GROUP BY ';
|
||||
$this->filter .= ' ' . implode(' ,', $group) . ' ';
|
||||
$this->filter .= ' ' . implode(' ,', $group) . ' ' . $having . ' ';
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
@@ -159,7 +161,7 @@ class Sql
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
$sql = sprintf('SELECT %s FROM `%s` %s', $this->field, $this->table, $this->filter);
|
||||
$sql = sprintf('SELECT %s FROM `%s` %s LIMIT 1', $this->field, $this->table, $this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth, $this->param);
|
||||
@@ -176,7 +178,7 @@ class Sql
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$sql = sprintf('DELETE FROM `%s` %s', $this->table, $this->filter);
|
||||
$sql = sprintf('DELETE FROM `%s` %s LIMIT 1', $this->table, $this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth, $this->param);
|
||||
@@ -225,7 +227,7 @@ class Sql
|
||||
$sql = sprintf('UPDATE `%s` SET %s %s', $this->table, $this->formatUpdate($data), $this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth, $data);
|
||||
$sth = $this->formatUpdateParam($sth, $data);
|
||||
$sth = $this->formatParam($sth, $this->param);
|
||||
$this->param = [];
|
||||
$sth->execute();
|
||||
@@ -248,6 +250,20 @@ class Sql
|
||||
return $sth;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定具体变量值update
|
||||
* @param PDOStatement $sth
|
||||
* @param array $params
|
||||
* @return PDOStatement
|
||||
*/
|
||||
public function formatUpdateParam(PDOStatement $sth, $params = [])
|
||||
{
|
||||
foreach ($params as $param => &$value) {
|
||||
$param = ':' . ltrim($param, ':').'update';
|
||||
$sth->bindParam($param, $value);
|
||||
}
|
||||
return $sth;
|
||||
}
|
||||
/**
|
||||
* 将数组转化为插入格式的sql语句
|
||||
* @param $data
|
||||
@@ -295,8 +311,8 @@ class Sql
|
||||
{
|
||||
$fields = [];
|
||||
foreach ($data as $key => $value) {
|
||||
$fields[] = sprintf('`%s`=:%s', $key, $key);
|
||||
$this->param[$key] = $value;
|
||||
$fields[] = sprintf('`%s`=:%s', $key, $key.'update');
|
||||
$this->param[$key.'update'] = $value;
|
||||
}
|
||||
return implode(',', $fields);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user