mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-05 22:52:49 +08:00
添加api接口的支持
This commit is contained in:
@@ -10,9 +10,10 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db;
|
||||
use think\config;
|
||||
use think\debug;
|
||||
use think\log;
|
||||
use think\Config;
|
||||
use think\Debug;
|
||||
use think\Log;
|
||||
use think\Exception;
|
||||
use PDO;
|
||||
|
||||
abstract class Driver {
|
||||
@@ -101,7 +102,7 @@ abstract class Driver {
|
||||
Log::record($e->getMessage(),'ERR');
|
||||
return $this->connect($autoConnection,$linkNum);
|
||||
}elseif($config['debug']){
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,15 +162,15 @@ abstract class Driver {
|
||||
}
|
||||
$this->bind = [];
|
||||
try{
|
||||
$result = $this->PDOStatement->execute();
|
||||
// 调试结束
|
||||
$this->debug(false);
|
||||
if ( false === $result ) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
return $this->getResult();
|
||||
}
|
||||
$result = $this->PDOStatement->execute();
|
||||
// 调试结束
|
||||
$this->debug(false);
|
||||
if ( false === $result ) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
return $this->getResult();
|
||||
}
|
||||
}catch (\PDOException $e) {
|
||||
$this->error();
|
||||
return false;
|
||||
@@ -214,18 +215,18 @@ abstract class Driver {
|
||||
}
|
||||
$this->bind = [];
|
||||
try{
|
||||
$result = $this->PDOStatement->execute();
|
||||
$this->debug(false);
|
||||
if ( false === $result) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
$this->numRows = $this->PDOStatement->rowCount();
|
||||
if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
|
||||
$this->lastInsID = $this->_linkID->lastInsertId();
|
||||
}
|
||||
return $this->numRows;
|
||||
}
|
||||
$result = $this->PDOStatement->execute();
|
||||
$this->debug(false);
|
||||
if ( false === $result) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
$this->numRows = $this->PDOStatement->rowCount();
|
||||
if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
|
||||
$this->lastInsID = $this->_linkID->lastInsertId();
|
||||
}
|
||||
return $this->numRows;
|
||||
}
|
||||
}catch (\PDOException $e) {
|
||||
$this->error();
|
||||
return false;
|
||||
@@ -341,7 +342,7 @@ abstract class Driver {
|
||||
// 记录错误日志
|
||||
Log::record($this->error,'ERR');
|
||||
if($this->config['debug']) {// 开启数据库调试模式
|
||||
E($this->error);
|
||||
throw new Exception($this->error);
|
||||
}else{
|
||||
return $this->error;
|
||||
}
|
||||
@@ -505,10 +506,6 @@ abstract class Driver {
|
||||
// 解析特殊条件表达式
|
||||
$whereStr .= $this->parseThinkWhere($key,$val);
|
||||
}else{
|
||||
// 查询字段的安全过滤
|
||||
// if(!preg_match('/^[A-Z_\|\&\-.a-z0-9\(\)\,]+$/',trim($key))){
|
||||
// E(L('_EXPRESS_ERROR_').':'.$key);
|
||||
// }
|
||||
// 多条件支持
|
||||
$multi = is_array($val) && isset($val['_multi']);
|
||||
$key = trim($key);
|
||||
@@ -578,7 +575,7 @@ abstract class Driver {
|
||||
$data = is_string($val[1])? explode(',',$val[1]):$val[1];
|
||||
$whereStr .= $key.' '.$this->exp[$exp].' '.$this->parseValue($data[0]).' AND '.$this->parseValue($data[1]);
|
||||
}else{
|
||||
E(L('_EXPRESS_ERROR_').':'.$val[0]);
|
||||
throw new Exception(L('_EXPRESS_ERROR_').':'.$val[0]);
|
||||
}
|
||||
}else {
|
||||
$count = count($val);
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace think\db\driver;
|
||||
use think\db\Driver;
|
||||
|
||||
use think\Exception;
|
||||
/**
|
||||
* Mongo数据库驱动
|
||||
*/
|
||||
@@ -31,7 +31,7 @@ class Mongo extends Driver {
|
||||
*/
|
||||
public function __construct($config=''){
|
||||
if ( !class_exists('mongoClient') ) {
|
||||
E(L('_NOT_SUPPERT_').':Mongo');
|
||||
throw new Exception(L('_NOT_SUPPERT_').':Mongo');
|
||||
}
|
||||
if(!empty($config)) {
|
||||
$this->config = array_merge($this->config,$config);
|
||||
@@ -52,7 +52,7 @@ class Mongo extends Driver {
|
||||
try{
|
||||
$this->linkID[$linkNum] = new \mongoClient( $host,$this->config['params']);
|
||||
}catch (\MongoConnectionException $e){
|
||||
E($e->getmessage());
|
||||
throw new Exception($e->getmessage());
|
||||
}
|
||||
}
|
||||
return $this->linkID[$linkNum];
|
||||
@@ -86,8 +86,8 @@ class Mongo extends Driver {
|
||||
$this->debug(false);
|
||||
$this->_collectionName = $collection; // 记录当前Collection名称
|
||||
}
|
||||
}catch (MongoException $e){
|
||||
E($e->getMessage());
|
||||
}catch (\MongoException $e){
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ class Mongo extends Driver {
|
||||
$result = $this->_mongo->command($command);
|
||||
$this->debug(false);
|
||||
if(!$result['ok']) {
|
||||
E($result['errmsg']);
|
||||
throw new Exception($result['errmsg']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
@@ -133,7 +133,7 @@ class Mongo extends Driver {
|
||||
if($result['ok']) {
|
||||
return $result['retval'];
|
||||
}else{
|
||||
E($result['errmsg']);
|
||||
throw new Exception($result['errmsg']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +194,7 @@ class Mongo extends Driver {
|
||||
}
|
||||
return $result;
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ class Mongo extends Driver {
|
||||
$this->debug(false);
|
||||
return $result;
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ class Mongo extends Driver {
|
||||
$result = $this->_collection->find([],[$pk=>1])->sort([$pk=>-1])->limit(1);
|
||||
$this->debug(false);
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
$data = $result->getNext();
|
||||
return isset($data[$pk])?$data[$pk]+1:1;
|
||||
@@ -273,7 +273,7 @@ class Mongo extends Driver {
|
||||
$this->debug(false);
|
||||
return $result;
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,7 +299,7 @@ class Mongo extends Driver {
|
||||
$this->debug(false);
|
||||
return $result;
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ class Mongo extends Driver {
|
||||
$this->debug(false);
|
||||
return $result;
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,7 +398,7 @@ class Mongo extends Driver {
|
||||
}
|
||||
return $resultSet;
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -439,7 +439,7 @@ class Mongo extends Driver {
|
||||
}
|
||||
return $result;
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,7 +467,7 @@ class Mongo extends Driver {
|
||||
$this->debug(false);
|
||||
return $count;
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,7 +493,7 @@ class Mongo extends Driver {
|
||||
$result = $this->_collection->findOne();
|
||||
$this->debug(false);
|
||||
} catch (\MongoCursorException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
if($result) { // 存在数据则分析字段
|
||||
$info = [];
|
||||
@@ -631,7 +631,7 @@ class Mongo extends Driver {
|
||||
}else{
|
||||
// 查询字段的安全过滤
|
||||
if(!preg_match('/^[A-Z_\|\&\-.a-z0-9]+$/',trim($key))){
|
||||
E(L('_ERROR_QUERY_').':'.$key);
|
||||
throw new Exception(L('_ERROR_QUERY_').':'.$key);
|
||||
}
|
||||
$key = trim($key);
|
||||
if(strpos($key,'|')) {
|
||||
|
||||
@@ -59,19 +59,25 @@ class Oracle extends Driver{
|
||||
$this->debug(true);
|
||||
$this->PDOStatement = $this->_linkID->prepare($str);
|
||||
if(false === $this->PDOStatement) {
|
||||
E($this->error());
|
||||
}
|
||||
$result = $this->PDOStatement->execute($bind);
|
||||
$this->debug(false);
|
||||
if ( false === $result) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
$this->numRows = $this->PDOStatement->rowCount();
|
||||
if($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
|
||||
$this->lastInsID = $this->_linkID->lastInsertId();
|
||||
}
|
||||
try{
|
||||
$result = $this->PDOStatement->execute($bind);
|
||||
$this->debug(false);
|
||||
if ( false === $result) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
$this->numRows = $this->PDOStatement->rowCount();
|
||||
if($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
|
||||
$this->lastInsID = $this->_linkID->lastInsertId();
|
||||
}
|
||||
return $this->numRows;
|
||||
}
|
||||
return $this->numRows;
|
||||
}catch (\PDOException $e) {
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +135,7 @@ class Oracle extends Driver{
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit) {
|
||||
public function parseLimit($limit) {
|
||||
$limitStr = '';
|
||||
if(!empty($limit)) {
|
||||
$limit = explode(',',$limit);
|
||||
|
||||
@@ -90,11 +90,11 @@ class Lite {
|
||||
if(empty($config)) $config = $this->config;
|
||||
try{
|
||||
if(empty($config['dsn'])) {
|
||||
E('Think/Db/Lite 必须设置 dsn参数');
|
||||
throw new Exception('Think/Db/Lite 必须设置 dsn参数');
|
||||
}
|
||||
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$config['params']);
|
||||
}catch (\PDOException $e) {
|
||||
E($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
return $this->linkID[$linkNum];
|
||||
@@ -128,8 +128,10 @@ class Lite {
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
$this->PDOStatement = $this->_linkID->prepare($str);
|
||||
if(false === $this->PDOStatement)
|
||||
E($this->error());
|
||||
if(false === $this->PDOStatement){
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
foreach ($bind as $key => $val) {
|
||||
if(is_array($val)){
|
||||
$this->PDOStatement->bindValue($key, $val[0], $val[1]);
|
||||
@@ -137,14 +139,19 @@ class Lite {
|
||||
$this->PDOStatement->bindValue($key, $val);
|
||||
}
|
||||
}
|
||||
$result = $this->PDOStatement->execute();
|
||||
// 调试结束
|
||||
$this->debug(false);
|
||||
if ( false === $result ) {
|
||||
try{
|
||||
$result = $this->PDOStatement->execute();
|
||||
// 调试结束
|
||||
$this->debug(false);
|
||||
if ( false === $result ) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
return $this->getResult();
|
||||
}
|
||||
}catch (\PDOException $e) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
return $this->getResult();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,8 +175,9 @@ class Lite {
|
||||
// 记录开始执行时间
|
||||
$this->debug(true);
|
||||
$this->PDOStatement = $this->_linkID->prepare($str);
|
||||
if(false === $this->PDOStatement) {
|
||||
E($this->error());
|
||||
if(false === $this->PDOStatement){
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
foreach ($bind as $key => $val) {
|
||||
if(is_array($val)){
|
||||
@@ -178,17 +186,22 @@ class Lite {
|
||||
$this->PDOStatement->bindValue($key, $val);
|
||||
}
|
||||
}
|
||||
$result = $this->PDOStatement->execute();
|
||||
$this->debug(false);
|
||||
if ( false === $result) {
|
||||
try{
|
||||
$result = $this->PDOStatement->execute();
|
||||
$this->debug(false);
|
||||
if ( false === $result) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
$this->numRows = $this->PDOStatement->rowCount();
|
||||
if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
|
||||
$this->lastInsID = $this->_linkID->lastInsertId();
|
||||
}
|
||||
return $this->numRows;
|
||||
}
|
||||
}catch (\PDOException $e) {
|
||||
$this->error();
|
||||
return false;
|
||||
} else {
|
||||
$this->numRows = $this->PDOStatement->rowCount();
|
||||
if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
|
||||
$this->lastInsID = $this->_linkID->lastInsertId();
|
||||
}
|
||||
return $this->numRows;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,7 +313,7 @@ class Lite {
|
||||
// 记录错误日志
|
||||
Log::record($this->error,'ERR');
|
||||
if($this->config['debug']) {// 开启数据库调试模式
|
||||
E($this->error);
|
||||
throw new Exception($this->error);
|
||||
}else{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user