youhua
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace fastphp\db;
|
||||
|
||||
use \PDOStatement;
|
||||
@@ -27,31 +26,32 @@ class Sql
|
||||
* 多条件查询
|
||||
* ['id'=>['<',100]]
|
||||
*/
|
||||
public function where($where){
|
||||
public function where($where)
|
||||
{
|
||||
$this->param = [];
|
||||
$this->filter = '';
|
||||
if($where){
|
||||
if ($where) {
|
||||
$this->filter .= ' WHERE ';
|
||||
if(is_string($where)){
|
||||
$this->filter .= ' '.$where;
|
||||
if (is_string($where)) {
|
||||
$this->filter .= ' ' . $where;
|
||||
} else {
|
||||
$flag = true;
|
||||
foreach ($where as $key => $value){
|
||||
if($flag){
|
||||
foreach ($where as $key => $value) {
|
||||
if ($flag) {
|
||||
$flag = false;
|
||||
if(is_array($value)){
|
||||
$this->filter .= ' `'.$key.'` '.$value[0].' :'.$key;
|
||||
if (is_array($value)) {
|
||||
$this->filter .= ' `' . $key . '` ' . $value[0] . ' :' . $key;
|
||||
$this->param[$key] = $value[1];
|
||||
} else {
|
||||
$this->filter .= ' `'.$key.'` = :'.$key;
|
||||
$this->filter .= ' `' . $key . '` = :' . $key;
|
||||
$this->param[$key] = $value;
|
||||
}
|
||||
} else {
|
||||
if(is_array($value)){
|
||||
$this->filter .= ' AND `'.$key.'` '.$value[0].' :'.$key;
|
||||
if (is_array($value)) {
|
||||
$this->filter .= ' AND `' . $key . '` ' . $value[0] . ' :' . $key;
|
||||
$this->param[$key] = $value[1];
|
||||
} else {
|
||||
$this->filter .= ' AND `'.$key.'` = :'.$key;
|
||||
$this->filter .= ' AND `' . $key . '` = :' . $key;
|
||||
$this->param[$key] = $value;
|
||||
}
|
||||
}
|
||||
@@ -67,37 +67,39 @@ class Sql
|
||||
* @return $this
|
||||
* $order='id desc'
|
||||
*/
|
||||
public function order($order = 'id desc'){
|
||||
if($order){
|
||||
$this->filter .= ' ORDER BY '.$order.' ';
|
||||
public function order($order = 'id desc')
|
||||
{
|
||||
if ($order) {
|
||||
$this->filter .= ' ORDER BY ' . $order . ' ';
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* group by
|
||||
* @param array $order
|
||||
* @return $this
|
||||
* $group=['sex','name']
|
||||
*/
|
||||
public function group($group = []){
|
||||
if($order){
|
||||
public function group($group = [])
|
||||
{
|
||||
if ($order) {
|
||||
$this->filter .= ' GROUP BY ';
|
||||
$this->filter .= ' '.implode(' ,',$group).' ';
|
||||
$this->filter .= ' ' . implode(' ,', $group) . ' ';
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询limit
|
||||
* @param string
|
||||
* @param string
|
||||
* @return $this
|
||||
* $limit = '100' 限制查询100条
|
||||
* $limit = '2,100' 查询第二页 100条数据
|
||||
*/
|
||||
public function limit($limit = '100'){
|
||||
$this->filter .= ' LIMIT '.$limit.' ';
|
||||
public function limit($limit = '100')
|
||||
{
|
||||
$this->filter .= ' LIMIT ' . $limit . ' ';
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -107,7 +109,8 @@ class Sql
|
||||
* @return $this
|
||||
* $field = 'id,count(1),sub(num)'
|
||||
*/
|
||||
public function field($field = '*'){
|
||||
public function field($field = '*')
|
||||
{
|
||||
$this->field = $field;
|
||||
return $this;
|
||||
}
|
||||
@@ -116,11 +119,12 @@ class Sql
|
||||
* 查询所有
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetchAll(){
|
||||
$sql = sprintf('SELECT %s FROM `%s` %s',$this->field,$this->table,$this->filter);
|
||||
public function fetchAll()
|
||||
{
|
||||
$sql = sprintf('SELECT %s FROM `%s` %s', $this->field, $this->table, $this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$this->param);
|
||||
$sth = $this->formatParam($sth, $this->param);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->fetchAll();
|
||||
@@ -130,11 +134,12 @@ class Sql
|
||||
* 查询一条
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetch(){
|
||||
$sql = sprintf('SELECT %s FROM `%s` %s',$this->field,$this->table,$this->filter);
|
||||
public function fetch()
|
||||
{
|
||||
$sql = sprintf('SELECT %s FROM `%s` %s', $this->field, $this->table, $this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$this->param);
|
||||
$sth = $this->formatParam($sth, $this->param);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->fetch();
|
||||
@@ -145,11 +150,12 @@ class Sql
|
||||
* @param $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(){
|
||||
$sql = sprintf('DELETE FROM `%s` %s',$this->table,$this->filter);
|
||||
public function delete()
|
||||
{
|
||||
$sql = sprintf('DELETE FROM `%s` %s', $this->table, $this->filter);
|
||||
$this->filter = '';
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$this->param);
|
||||
$sth = $this->formatParam($sth, $this->param);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->rowCount();
|
||||
@@ -160,10 +166,11 @@ class Sql
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function add($data){
|
||||
$sql = sprintf('INSERT INTO `%s` %s',$this->table,$this->formatInsert($data));
|
||||
public function add($data)
|
||||
{
|
||||
$sql = sprintf('INSERT INTO `%s` %s', $this->table, $this->formatInsert($data));
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth = $this->formatParam($sth,$data);
|
||||
$sth = $this->formatParam($sth, $data);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->rowCount();
|
||||
@@ -174,8 +181,9 @@ class Sql
|
||||
* @param $data
|
||||
* @return int
|
||||
*/
|
||||
public function addAll($data){
|
||||
$sql = sprintf('INSERT INTO `%s` %s',$this->table,$this->formatInsertAll($data));
|
||||
public function addAll($data)
|
||||
{
|
||||
$sql = sprintf('INSERT INTO `%s` %s', $this->table, $this->formatInsertAll($data));
|
||||
$sth = Db::pdo()->prepare($sql);
|
||||
$sth->execute();
|
||||
|
||||
@@ -187,12 +195,13 @@ class Sql
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($data){
|
||||
$sql = sprintf('UPDATE `%s` SET %s %s',$this->table,$this->formatUpdate($data),$this->filter);
|
||||
public function update($data)
|
||||
{
|
||||
$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->formatParam($sth,$this->param);
|
||||
$sth = $this->formatParam($sth, $data);
|
||||
$sth = $this->formatParam($sth, $this->param);
|
||||
$sth->execute();
|
||||
|
||||
return $sth->rowCount();
|
||||
@@ -204,10 +213,11 @@ class Sql
|
||||
* @param array $params
|
||||
* @return PDOStatement
|
||||
*/
|
||||
public function formatParam(PDOStatement $sth,$params = []){
|
||||
foreach ($params as $param=>&$value){
|
||||
$param = is_int($param)?$param+1:':'.ltrim($param,':');
|
||||
$sth->bindParam($param,$value);
|
||||
public function formatParam(PDOStatement $sth, $params = [])
|
||||
{
|
||||
foreach ($params as $param => &$value) {
|
||||
$param = is_int($param) ? $param + 1 : ':' . ltrim($param, ':');
|
||||
$sth->bindParam($param, $value);
|
||||
}
|
||||
return $sth;
|
||||
}
|
||||
@@ -217,16 +227,17 @@ class Sql
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function formatInsert($data){
|
||||
private function formatInsert($data)
|
||||
{
|
||||
$fields = [];
|
||||
$names = [];
|
||||
foreach ($data as $key=>$value) {
|
||||
$fields[] = sprintf('`%s`',$key);
|
||||
$names[] = sprintf(':%s',$key);
|
||||
foreach ($data as $key => $value) {
|
||||
$fields[] = sprintf('`%s`', $key);
|
||||
$names[] = sprintf(':%s', $key);
|
||||
}
|
||||
$field = implode(',',$fields);
|
||||
$name = implode(',',$names);
|
||||
return sprintf('(%s) VALUES(%s)',$field,$name);
|
||||
$field = implode(',', $fields);
|
||||
$name = implode(',', $names);
|
||||
return sprintf('(%s) VALUES(%s)', $field, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,18 +245,19 @@ class Sql
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function formatInsertAll($data){
|
||||
private function formatInsertAll($data)
|
||||
{
|
||||
$fields = [];
|
||||
$values ='';
|
||||
foreach ($data[0] as $key=>$value) {
|
||||
$fields[] = sprintf('`%s`',$key);
|
||||
$values = '';
|
||||
foreach ($data[0] as $key => $value) {
|
||||
$fields[] = sprintf('`%s`', $key);
|
||||
}
|
||||
foreach ($data as $k=>$v){
|
||||
$values .= '(\''.implode('\',\'',$v).'\'),';
|
||||
foreach ($data as $k => $v) {
|
||||
$values .= '(\'' . implode('\',\'', $v) . '\'),';
|
||||
}
|
||||
$field = implode(',',$fields);
|
||||
$values = rtrim($values,',');
|
||||
return sprintf('(%s) VALUES %s',$field,$values);
|
||||
$field = implode(',', $fields);
|
||||
$values = rtrim($values, ',');
|
||||
return sprintf('(%s) VALUES %s', $field, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,12 +265,13 @@ class Sql
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function formatUpdate($data){
|
||||
private function formatUpdate($data)
|
||||
{
|
||||
$fields = [];
|
||||
foreach ($data as $key=>$value){
|
||||
$fields[] = sprintf('`%s`=:%s',$key,$key);
|
||||
foreach ($data as $key => $value) {
|
||||
$fields[] = sprintf('`%s`=:%s', $key, $key);
|
||||
$this->param[$key] = $value;
|
||||
}
|
||||
return implode(',',$fields);
|
||||
return implode(',', $fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user