mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 07:32:48 +08:00
改进Model类 改进查询方法的SQL返回机制
This commit is contained in:
@@ -292,8 +292,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected function writeTransform($value, $type)
|
protected function writeTransform($value, $type)
|
||||||
{
|
{
|
||||||
if(is_array($type)){
|
if(is_array($type)){
|
||||||
$param = $type[1];
|
list($type,$param) = $type;
|
||||||
$type = $type[0];
|
|
||||||
}elseif (strpos($type, ':')) {
|
}elseif (strpos($type, ':')) {
|
||||||
list($type, $param) = explode(':', $type, 2);
|
list($type, $param) = explode(':', $type, 2);
|
||||||
}
|
}
|
||||||
@@ -328,7 +327,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
case 'array':
|
case 'array':
|
||||||
$value = (array) $value;
|
$value = (array) $value;
|
||||||
case 'json':
|
case 'json':
|
||||||
$option = !empty($param) ? (intval)$param : JSON_UNESCAPED_UNICODE;
|
$option = !empty($param) ? (int)$param : JSON_UNESCAPED_UNICODE;
|
||||||
$value = json_encode($value, $option);
|
$value = json_encode($value, $option);
|
||||||
break;
|
break;
|
||||||
case 'serialize':
|
case 'serialize':
|
||||||
@@ -374,8 +373,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected function readTransform($value, $type)
|
protected function readTransform($value, $type)
|
||||||
{
|
{
|
||||||
if(is_array($type)){
|
if(is_array($type)){
|
||||||
$param = $type[1];
|
list($type,$param) = $type;
|
||||||
$type = $type[0];
|
|
||||||
}elseif (strpos($type, ':')) {
|
}elseif (strpos($type, ':')) {
|
||||||
list($type, $param) = explode(':', $type, 2);
|
list($type, $param) = explode(':', $type, 2);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,11 +28,6 @@ abstract class Connection
|
|||||||
/** @var PDOStatement PDO操作实例 */
|
/** @var PDOStatement PDO操作实例 */
|
||||||
protected $PDOStatement;
|
protected $PDOStatement;
|
||||||
|
|
||||||
// 当前操作的数据表名
|
|
||||||
protected $table = '';
|
|
||||||
// 当前操作的数据对象名
|
|
||||||
protected $name = '';
|
|
||||||
|
|
||||||
/** @var string 当前SQL指令 */
|
/** @var string 当前SQL指令 */
|
||||||
protected $queryStr = '';
|
protected $queryStr = '';
|
||||||
// 最后插入ID
|
// 最后插入ID
|
||||||
@@ -322,26 +317,21 @@ abstract class Connection
|
|||||||
* @access public
|
* @access public
|
||||||
* @param string $sql sql指令
|
* @param string $sql sql指令
|
||||||
* @param array $bind 参数绑定
|
* @param array $bind 参数绑定
|
||||||
* @param boolean $fetch 不执行只是获取SQL
|
|
||||||
* @param boolean $master 是否在主服务器读操作
|
* @param boolean $master 是否在主服务器读操作
|
||||||
* @param bool|string $class 指定返回的数据集对象
|
* @param bool|string $class 指定返回的数据集对象
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws BindParamException
|
* @throws BindParamException
|
||||||
* @throws PDOException
|
* @throws PDOException
|
||||||
*/
|
*/
|
||||||
public function query($sql, $bind = [], $fetch = false, $master = false, $class = false)
|
public function query($sql, $bind = [], $master = false, $class = false)
|
||||||
{
|
{
|
||||||
$this->initConnect($master);
|
$this->initConnect($master);
|
||||||
if (!$this->linkID) {
|
if (!$this->linkID) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据参数绑定组装最终的SQL语句
|
// 根据参数绑定组装最终的SQL语句
|
||||||
$this->queryStr = $this->getBindSql($sql, $bind);
|
$this->queryStr = $this->getRealSql($sql, $bind);
|
||||||
|
|
||||||
if ($fetch) {
|
|
||||||
return $this->queryStr;
|
|
||||||
}
|
|
||||||
//释放前次的查询结果
|
//释放前次的查询结果
|
||||||
if (!empty($this->PDOStatement)) {
|
if (!empty($this->PDOStatement)) {
|
||||||
$this->free();
|
$this->free();
|
||||||
@@ -371,25 +361,21 @@ abstract class Connection
|
|||||||
* @access public
|
* @access public
|
||||||
* @param string $sql sql指令
|
* @param string $sql sql指令
|
||||||
* @param array $bind 参数绑定
|
* @param array $bind 参数绑定
|
||||||
* @param boolean $fetch 不执行只是获取SQL
|
|
||||||
* @param boolean $getLastInsID 是否获取自增ID
|
* @param boolean $getLastInsID 是否获取自增ID
|
||||||
* @param string $sequence 自增序列名
|
* @param string $sequence 自增序列名
|
||||||
* @return int
|
* @return int
|
||||||
* @throws BindParamException
|
* @throws BindParamException
|
||||||
* @throws PDOException
|
* @throws PDOException
|
||||||
*/
|
*/
|
||||||
public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false, $sequence = null)
|
public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null)
|
||||||
{
|
{
|
||||||
$this->initConnect(true);
|
$this->initConnect(true);
|
||||||
if (!$this->linkID) {
|
if (!$this->linkID) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// 根据参数绑定组装最终的SQL语句
|
// 根据参数绑定组装最终的SQL语句
|
||||||
$this->queryStr = $this->getBindSql($sql, $bind);
|
$this->queryStr = $this->getRealSql($sql, $bind);
|
||||||
|
|
||||||
if ($fetch) {
|
|
||||||
return $this->queryStr;
|
|
||||||
}
|
|
||||||
//释放前次的查询结果
|
//释放前次的查询结果
|
||||||
if (!empty($this->PDOStatement)) {
|
if (!empty($this->PDOStatement)) {
|
||||||
$this->free();
|
$this->free();
|
||||||
@@ -428,7 +414,7 @@ abstract class Connection
|
|||||||
* @param array $bind 参数绑定列表
|
* @param array $bind 参数绑定列表
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getBindSql($sql, array $bind = [])
|
public function getRealSql($sql, array $bind = [])
|
||||||
{
|
{
|
||||||
if ($bind) {
|
if ($bind) {
|
||||||
foreach ($bind as $key => $val) {
|
foreach ($bind as $key => $val) {
|
||||||
|
|||||||
@@ -1521,7 +1521,7 @@ class Query
|
|||||||
* @param boolean $replace 是否replace
|
* @param boolean $replace 是否replace
|
||||||
* @param boolean $getLastInsID 是否获取自增ID
|
* @param boolean $getLastInsID 是否获取自增ID
|
||||||
* @param string $sequence 自增序列名
|
* @param string $sequence 自增序列名
|
||||||
* @return integer
|
* @return integer|string
|
||||||
*/
|
*/
|
||||||
public function insert(array $data, $replace = false, $getLastInsID = false, $sequence = null)
|
public function insert(array $data, $replace = false, $getLastInsID = false, $sequence = null)
|
||||||
{
|
{
|
||||||
@@ -1529,9 +1529,13 @@ class Query
|
|||||||
$options = $this->parseExpress();
|
$options = $this->parseExpress();
|
||||||
// 生成SQL语句
|
// 生成SQL语句
|
||||||
$sql = $this->builder()->insert($data, $options, $replace);
|
$sql = $this->builder()->insert($data, $options, $replace);
|
||||||
|
if($options['fetch_sql']){
|
||||||
|
// 获取实际执行的SQL语句
|
||||||
|
return $this->connection->getRealSql($sql,$this->bind);
|
||||||
|
}
|
||||||
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
|
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
|
||||||
// 执行操作
|
// 执行操作
|
||||||
return $this->execute($sql, $this->getBind(), $options['fetch_sql'], $getLastInsID, $sequence);
|
return $this->execute($sql, $this->getBind(), $getLastInsID, $sequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1540,7 +1544,7 @@ class Query
|
|||||||
* @param mixed $data 数据
|
* @param mixed $data 数据
|
||||||
* @param boolean $replace 是否replace
|
* @param boolean $replace 是否replace
|
||||||
* @param string $sequence 自增序列名
|
* @param string $sequence 自增序列名
|
||||||
* @return integer
|
* @return integer|string
|
||||||
*/
|
*/
|
||||||
public function insertGetId(array $data, $replace = false, $sequence = null)
|
public function insertGetId(array $data, $replace = false, $sequence = null)
|
||||||
{
|
{
|
||||||
@@ -1551,7 +1555,7 @@ class Query
|
|||||||
* 批量插入记录
|
* 批量插入记录
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $dataSet 数据集
|
* @param mixed $dataSet 数据集
|
||||||
* @return integer
|
* @return integer|string
|
||||||
*/
|
*/
|
||||||
public function insertAll(array $dataSet)
|
public function insertAll(array $dataSet)
|
||||||
{
|
{
|
||||||
@@ -1562,8 +1566,13 @@ class Query
|
|||||||
}
|
}
|
||||||
// 生成SQL语句
|
// 生成SQL语句
|
||||||
$sql = $this->builder()->insertAll($dataSet, $options);
|
$sql = $this->builder()->insertAll($dataSet, $options);
|
||||||
// 执行操作
|
if($options['fetch_sql']){
|
||||||
return $this->execute($sql, $this->getBind(), $options['fetch_sql']);
|
// 获取实际执行的SQL语句
|
||||||
|
return $this->connection->getRealSql($sql,$this->bind);
|
||||||
|
}else{
|
||||||
|
// 执行操作
|
||||||
|
return $this->execute($sql, $this->getBind());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1571,7 +1580,7 @@ class Query
|
|||||||
* @access public
|
* @access public
|
||||||
* @param string $fields 要插入的数据表字段名
|
* @param string $fields 要插入的数据表字段名
|
||||||
* @param string $table 要插入的数据表名
|
* @param string $table 要插入的数据表名
|
||||||
* @return int
|
* @return integer|string
|
||||||
* @throws PDOException
|
* @throws PDOException
|
||||||
*/
|
*/
|
||||||
public function selectInsert($fields, $table)
|
public function selectInsert($fields, $table)
|
||||||
@@ -1580,15 +1589,20 @@ class Query
|
|||||||
$options = $this->parseExpress();
|
$options = $this->parseExpress();
|
||||||
// 生成SQL语句
|
// 生成SQL语句
|
||||||
$sql = $this->builder()->selectInsert($fields, $table, $options);
|
$sql = $this->builder()->selectInsert($fields, $table, $options);
|
||||||
// 执行操作
|
if($options['fetch_sql']){
|
||||||
return $this->execute($sql, $this->getBind(), $options['fetch_sql']);
|
// 获取实际执行的SQL语句
|
||||||
|
return $this->connection->getRealSql($sql,$this->bind);
|
||||||
|
}else{
|
||||||
|
// 执行操作
|
||||||
|
return $this->execute($sql, $this->getBind());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新记录
|
* 更新记录
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $data 数据
|
* @param mixed $data 数据
|
||||||
* @return int
|
* @return integer|string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @throws PDOException
|
* @throws PDOException
|
||||||
*/
|
*/
|
||||||
@@ -1622,11 +1636,13 @@ class Query
|
|||||||
}
|
}
|
||||||
// 生成UPDATE SQL语句
|
// 生成UPDATE SQL语句
|
||||||
$sql = $this->builder()->update($data, $options);
|
$sql = $this->builder()->update($data, $options);
|
||||||
if ('' == $sql) {
|
if($options['fetch_sql']){
|
||||||
return 0;
|
// 获取实际执行的SQL语句
|
||||||
|
return $this->connection->getRealSql($sql,$this->bind);
|
||||||
|
}else{
|
||||||
|
// 执行操作
|
||||||
|
return '' == $sql ? 0 : $this->execute($sql, $this->getBind());
|
||||||
}
|
}
|
||||||
// 执行操作
|
|
||||||
return $this->execute($sql, $this->getBind(), $options['fetch_sql']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1666,13 +1682,13 @@ class Query
|
|||||||
if (!$resultSet) {
|
if (!$resultSet) {
|
||||||
// 生成查询SQL
|
// 生成查询SQL
|
||||||
$sql = $this->builder()->select($options);
|
$sql = $this->builder()->select($options);
|
||||||
|
if($options['fetch_sql']){
|
||||||
|
// 获取实际执行的SQL语句
|
||||||
|
return $this->connection->getRealSql($sql,$this->bind);
|
||||||
|
}
|
||||||
// 执行查询操作
|
// 执行查询操作
|
||||||
$resultSet = $this->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_class']);
|
$resultSet = $this->query($sql, $this->getBind(), $options['master'], $options['fetch_class']);
|
||||||
|
|
||||||
if (is_string($resultSet)) {
|
|
||||||
// 返回SQL
|
|
||||||
return $resultSet;
|
|
||||||
}
|
|
||||||
if ($resultSet instanceof \PDOStatement) {
|
if ($resultSet instanceof \PDOStatement) {
|
||||||
// 返回PDOStatement对象
|
// 返回PDOStatement对象
|
||||||
return $resultSet;
|
return $resultSet;
|
||||||
@@ -1747,13 +1763,12 @@ class Query
|
|||||||
if (!$result) {
|
if (!$result) {
|
||||||
// 生成查询SQL
|
// 生成查询SQL
|
||||||
$sql = $this->builder()->select($options);
|
$sql = $this->builder()->select($options);
|
||||||
|
if($options['fetch_sql']){
|
||||||
|
// 获取实际执行的SQL语句
|
||||||
|
return $this->connection->getRealSql($sql,$this->bind);
|
||||||
|
}
|
||||||
// 执行查询
|
// 执行查询
|
||||||
$result = $this->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_class']);
|
$result = $this->query($sql, $this->getBind(), $options['master'], $options['fetch_class']);
|
||||||
|
|
||||||
if (is_string($result)) {
|
|
||||||
// 返回SQL
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($result instanceof \PDOStatement) {
|
if ($result instanceof \PDOStatement) {
|
||||||
// 返回PDOStatement对象
|
// 返回PDOStatement对象
|
||||||
@@ -1902,8 +1917,12 @@ class Query
|
|||||||
}
|
}
|
||||||
// 生成删除SQL语句
|
// 生成删除SQL语句
|
||||||
$sql = $this->builder()->delete($options);
|
$sql = $this->builder()->delete($options);
|
||||||
|
if($options['fetch_sql']){
|
||||||
|
// 获取实际执行的SQL语句
|
||||||
|
return $this->getRealSql($sql,$this->bind);
|
||||||
|
}
|
||||||
// 执行操作
|
// 执行操作
|
||||||
return $this->execute($sql, $this->getBind(), $options['fetch_sql']);
|
return $this->execute($sql, $this->getBind());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -46,14 +46,13 @@ class Oracle extends Connection
|
|||||||
* @access public
|
* @access public
|
||||||
* @param string $sql sql指令
|
* @param string $sql sql指令
|
||||||
* @param array $bind 参数绑定
|
* @param array $bind 参数绑定
|
||||||
* @param boolean $fetch 不执行只是获取SQL
|
|
||||||
* @param boolean $getLastInsID 是否获取自增ID
|
* @param boolean $getLastInsID 是否获取自增ID
|
||||||
* @param string $sequence 序列名
|
* @param string $sequence 序列名
|
||||||
* @return integer
|
* @return integer
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
* @throws \think\Exception
|
* @throws \think\Exception
|
||||||
*/
|
*/
|
||||||
public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false, $sequence = null)
|
public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null)
|
||||||
{
|
{
|
||||||
$this->initConnect(true);
|
$this->initConnect(true);
|
||||||
if (!$this->linkID) {
|
if (!$this->linkID) {
|
||||||
@@ -62,9 +61,7 @@ class Oracle extends Connection
|
|||||||
|
|
||||||
// 根据参数绑定组装最终的SQL语句
|
// 根据参数绑定组装最终的SQL语句
|
||||||
$this->queryStr = $this->getBindSql($sql, $bind);
|
$this->queryStr = $this->getBindSql($sql, $bind);
|
||||||
if ($fetch) {
|
|
||||||
return $this->queryStr;
|
|
||||||
}
|
|
||||||
$flag = false;
|
$flag = false;
|
||||||
if (preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $sql, $match)) {
|
if (preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $sql, $match)) {
|
||||||
if(is_null($sequence)){
|
if(is_null($sequence)){
|
||||||
@@ -72,6 +69,7 @@ class Oracle extends Connection
|
|||||||
}
|
}
|
||||||
$flag = (boolean) $this->query("SELECT * FROM all_sequences WHERE sequence_name='" . strtoupper($sequence) . "'");
|
$flag = (boolean) $this->query("SELECT * FROM all_sequences WHERE sequence_name='" . strtoupper($sequence) . "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
//释放前次的查询结果
|
//释放前次的查询结果
|
||||||
if (!empty($this->PDOStatement)) {
|
if (!empty($this->PDOStatement)) {
|
||||||
$this->free();
|
$this->free();
|
||||||
|
|||||||
Reference in New Issue
Block a user