mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-05 14:52:47 +08:00
PSR规范调整
This commit is contained in:
@@ -10,20 +10,22 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\db\driver;
|
||||
use think\db\Driver;
|
||||
|
||||
use PDO;
|
||||
use think\db\Driver;
|
||||
|
||||
/**
|
||||
* Sqlsrv数据库驱动
|
||||
*/
|
||||
class Sqlsrv extends Driver{
|
||||
protected $selectSql = 'SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 %LIMIT%%COMMENT%';
|
||||
class Sqlsrv extends Driver
|
||||
{
|
||||
protected $selectSql = 'SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 %LIMIT%%COMMENT%';
|
||||
// PDO连接参数
|
||||
protected $options = [
|
||||
PDO::ATTR_CASE => PDO::CASE_LOWER,
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8,
|
||||
PDO::ATTR_CASE => PDO::CASE_LOWER,
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -32,10 +34,11 @@ class Sqlsrv extends Driver{
|
||||
* @param array $config 连接信息
|
||||
* @return string
|
||||
*/
|
||||
protected function parseDsn($config){
|
||||
$dsn = 'sqlsrv:dbname='.$config['database'].';Server='.$config['hostname'];
|
||||
if(!empty($config['hostport'])) {
|
||||
$dsn .= ','.$config['hostport'];
|
||||
protected function parseDsn($config)
|
||||
{
|
||||
$dsn = 'sqlsrv:dbname=' . $config['database'] . ';Server=' . $config['hostname'];
|
||||
if (!empty($config['hostport'])) {
|
||||
$dsn .= ',' . $config['hostport'];
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
@@ -45,22 +48,23 @@ class Sqlsrv extends Driver{
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getFields($tableName) {
|
||||
public function getFields($tableName)
|
||||
{
|
||||
list($tableName) = explode(' ', $tableName);
|
||||
$result = $this->query("SELECT column_name, data_type, column_default, is_nullable
|
||||
$result = $this->query("SELECT column_name, data_type, column_default, is_nullable
|
||||
FROM information_schema.tables AS t
|
||||
JOIN information_schema.columns AS c
|
||||
ON t.table_catalog = c.table_catalog
|
||||
AND t.table_schema = c.table_schema
|
||||
AND t.table_name = c.table_name
|
||||
WHERE t.table_name = '$tableName'");
|
||||
$info = [];
|
||||
if($result) {
|
||||
$info = [];
|
||||
if ($result) {
|
||||
foreach ($result as $key => $val) {
|
||||
$info[$val['column_name']] = [
|
||||
'name' => $val['column_name'],
|
||||
'type' => $val['data_type'],
|
||||
'notnull' => (bool) ($val['is_nullable'] === ''), // not null is empty, null is yes
|
||||
'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes
|
||||
'default' => $val['column_default'],
|
||||
'primary' => false,
|
||||
'autoinc' => false,
|
||||
@@ -75,26 +79,28 @@ class Sqlsrv extends Driver{
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($dbName='') {
|
||||
$result = $this->query("SELECT TABLE_NAME
|
||||
public function getTables($dbName = '')
|
||||
{
|
||||
$result = $this->query("SELECT TABLE_NAME
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_TYPE = 'BASE TABLE'
|
||||
");
|
||||
$info = [];
|
||||
$info = [];
|
||||
foreach ($result as $key => $val) {
|
||||
$info[$key] = current($val);
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* order分析
|
||||
* @access protected
|
||||
* @param mixed $order
|
||||
* @return string
|
||||
*/
|
||||
protected function parseOrder($order) {
|
||||
return !empty($order)? ' ORDER BY '.$order:' ORDER BY rand()';
|
||||
protected function parseOrder($order)
|
||||
{
|
||||
return !empty($order) ? ' ORDER BY ' . $order : ' ORDER BY rand()';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,12 +109,13 @@ class Sqlsrv extends Driver{
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey(&$key) {
|
||||
$key = trim($key);
|
||||
if(!preg_match('/[,\'\"\*\(\)\[.\s]/',$key)) {
|
||||
$key = '['.$key.']';
|
||||
protected function parseKey(&$key)
|
||||
{
|
||||
$key = trim($key);
|
||||
if (!preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
|
||||
$key = '[' . $key . ']';
|
||||
}
|
||||
return $key;
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,14 +124,20 @@ class Sqlsrv extends Driver{
|
||||
* @param mixed $limit
|
||||
* @return string
|
||||
*/
|
||||
public function parseLimit($limit) {
|
||||
if(empty($limit)) return '';
|
||||
$limit = explode(',',$limit);
|
||||
if(count($limit)>1)
|
||||
$limitStr = '(T1.ROW_NUMBER BETWEEN '.$limit[0].' + 1 AND '.$limit[0].' + '.$limit[1].')';
|
||||
else
|
||||
$limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND '.$limit[0].")";
|
||||
return 'WHERE '.$limitStr;
|
||||
public function parseLimit($limit)
|
||||
{
|
||||
if (empty($limit)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$limit = explode(',', $limit);
|
||||
if (count($limit) > 1) {
|
||||
$limitStr = '(T1.ROW_NUMBER BETWEEN ' . $limit[0] . ' + 1 AND ' . $limit[0] . ' + ' . $limit[1] . ')';
|
||||
} else {
|
||||
$limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND ' . $limit[0] . ")";
|
||||
}
|
||||
|
||||
return 'WHERE ' . $limitStr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,15 +147,16 @@ class Sqlsrv extends Driver{
|
||||
* @param array $options 表达式
|
||||
* @return false | integer
|
||||
*/
|
||||
public function update($data,$options) {
|
||||
$this->model = $options['model'];
|
||||
$sql = 'UPDATE '
|
||||
.$this->parseTable($options['table'])
|
||||
.$this->parseSet($data)
|
||||
.$this->parseWhere(!empty($options['where'])?$options['where']:'')
|
||||
.$this->parseLock(isset($options['lock'])?$options['lock']:false)
|
||||
.$this->parseComment(!empty($options['comment'])?$options['comment']:'');
|
||||
return $this->execute($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:[]));
|
||||
public function update($data, $options)
|
||||
{
|
||||
$this->model = $options['model'];
|
||||
$sql = 'UPDATE '
|
||||
. $this->parseTable($options['table'])
|
||||
. $this->parseSet($data)
|
||||
. $this->parseWhere(!empty($options['where']) ? $options['where'] : '')
|
||||
. $this->parseLock(isset($options['lock']) ? $options['lock'] : false)
|
||||
. $this->parseComment(!empty($options['comment']) ? $options['comment'] : '');
|
||||
return $this->execute($sql, $this->parseBind(!empty($options['bind']) ? $options['bind'] : []));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,14 +165,15 @@ class Sqlsrv extends Driver{
|
||||
* @param array $options 表达式
|
||||
* @return false | integer
|
||||
*/
|
||||
public function delete($options=[]) {
|
||||
$this->model = $options['model'];
|
||||
$sql = 'DELETE FROM '
|
||||
.$this->parseTable($options['table'])
|
||||
.$this->parseWhere(!empty($options['where'])?$options['where']:'')
|
||||
.$this->parseLock(isset($options['lock'])?$options['lock']:false)
|
||||
.$this->parseComment(!empty($options['comment'])?$options['comment']:'');
|
||||
return $this->execute($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:[]));
|
||||
public function delete($options = [])
|
||||
{
|
||||
$this->model = $options['model'];
|
||||
$sql = 'DELETE FROM '
|
||||
. $this->parseTable($options['table'])
|
||||
. $this->parseWhere(!empty($options['where']) ? $options['where'] : '')
|
||||
. $this->parseLock(isset($options['lock']) ? $options['lock'] : false)
|
||||
. $this->parseComment(!empty($options['comment']) ? $options['comment'] : '');
|
||||
return $this->execute($sql, $this->parseBind(!empty($options['bind']) ? $options['bind'] : []));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user