1838 lines
41 KiB
PHP
1838 lines
41 KiB
PHP
<?php
|
||
/**
|
||
* Created by Notepad++.
|
||
* User: 应俊
|
||
* Date: 2017-04-01
|
||
* Time: 11:22
|
||
*/
|
||
|
||
|
||
/***********************************************************************************************************************
|
||
* 2017-04-01:
|
||
* 修复了在合并插入时,可能引起的语法错误(oracle已测,mssql未测)。
|
||
* 修复了mb_strstr函数参数传递不当可能导致的bug。
|
||
* 修复了大数据合并时可能导致的异常(oracle)。
|
||
* 为提高代码可读性,把BaseMethod独立到一个单独的单元中,并拆分为一个父类和三个子类(对应支持的三种数据库)以及一个代理类。
|
||
* 2017-04-10:
|
||
* 增加在BaseMethodHelper类里,增加NewCommand方法获取对应的SQLCommand对象
|
||
* 2017-06-28:
|
||
* 删除了BaseMethod中重复的数据库操作方法(PDO_Select, PDO_Insert, PDO_Update, PDO_Delete, PDO_Select_With_Page等).
|
||
* 2018-06-11:
|
||
* BaseMethod中增加GetMasterDatabaseConnectParameter方法和GetServantDatabaseConnectParameter方法来读取主、从库的链接参数,这样可以派生类更灵活的指定数据库连接设置.
|
||
***********************************************************************************************************************/
|
||
|
||
|
||
require_once __DIR__ . '/DatabaseHelper.php';
|
||
|
||
|
||
/**
|
||
* Class IBaseMethod
|
||
* 接口对象基类接口
|
||
* @auther 应俊
|
||
*/
|
||
interface IBaseMethod
|
||
{
|
||
/**
|
||
* @note 请求方法(get, post)
|
||
* @return string
|
||
*/
|
||
public function RequestMethod();
|
||
|
||
/**
|
||
* @note 连接到主数据库
|
||
* @return bool
|
||
*/
|
||
public function MasterConnect();
|
||
|
||
/**
|
||
* @note 连接到从数据库
|
||
* @return bool
|
||
*/
|
||
public function ServantConnect();
|
||
|
||
/**
|
||
* @note 连接到数据库
|
||
* @return bool
|
||
*/
|
||
public function PDO_Connect();
|
||
|
||
/**
|
||
* @note 关闭pdo连接
|
||
*/
|
||
public function PDO_Close();
|
||
|
||
/**
|
||
* @note pdo请求数据(有返回结果集)
|
||
* @param mixed
|
||
* @return mixed|null
|
||
*/
|
||
public function PDO_Request();
|
||
|
||
/**
|
||
* @note pdo执行语句(无返回结果集)
|
||
* @param mixed
|
||
* @return mixed|null
|
||
*/
|
||
public function PDO_Execute();
|
||
|
||
/**
|
||
* @note 最后一次执行插入后,产生的自动编号
|
||
* @param string $name
|
||
* @return bool|null|string
|
||
*/
|
||
public function PDO_LastInsertId($name = null);
|
||
|
||
/**
|
||
* @note 开启一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_BeginTransaction($Database = null);
|
||
|
||
/**
|
||
* @note 提交一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_Commit($Database = null);
|
||
|
||
/**
|
||
* @note 回滚一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_Rollback($Database = null);
|
||
|
||
|
||
/**
|
||
* @note 最后一次执行的语句是否成功。
|
||
* @param IPDOHelper $Database
|
||
* @return bool
|
||
*/
|
||
public function PDO_IsDone($Database = null);
|
||
|
||
|
||
// /**
|
||
// * @note 用户授权信息转用户码(用户id)
|
||
// * @param string $user_auth_token
|
||
// * @return string
|
||
// */
|
||
// public function UserAuthTokenToUserCode($user_auth_token);
|
||
|
||
/**
|
||
* @note 生成一个随机字符串
|
||
* @param string $prefix 前缀
|
||
* @return string
|
||
*/
|
||
public function GetRandString($prefix = '');
|
||
|
||
/**
|
||
* @note 创建一个新的id
|
||
* @param string $prefix 前缀
|
||
* @param bool $md5 是否md5加密
|
||
* @return string
|
||
*/
|
||
public function CreateNewIdent($prefix = '', $md5 = false);
|
||
|
||
/**
|
||
* @note 获取错误值
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return int
|
||
*/
|
||
public function GetErrorCode($Database = null, $AlwaysLocal = false);
|
||
|
||
/**
|
||
* @note 获取错误信息
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return mixed|string
|
||
*/
|
||
public function GetErrorInfo($Database = null, $AlwaysLocal = false);
|
||
|
||
/**
|
||
* @note 获取错误信息
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return mixed
|
||
*/
|
||
public function GetErrors($Database = null, $AlwaysLocal = false);
|
||
|
||
/**
|
||
* @note 设置错误信息
|
||
* @param int|mixed $ErrorCode 错误码
|
||
* @param string|mixed $ErrorInfo 错误信息
|
||
* @return mixed
|
||
*/
|
||
public function SetErrors($ErrorCode, $ErrorInfo);
|
||
|
||
/**
|
||
* @note 设置数据库的错误信息
|
||
* @param IPDOHelper $Helper
|
||
* @return bool
|
||
*/
|
||
public function SetDatabaseErrors($Helper);
|
||
|
||
|
||
/**
|
||
* @note 获取主数据库对象
|
||
* @return IPDOHelper
|
||
*/
|
||
public function GetMasterDatabase();
|
||
|
||
/**
|
||
* @note 获取从数据库对象
|
||
* @return IPDOHelper
|
||
*/
|
||
public function GetServantDatabase();
|
||
|
||
|
||
/**
|
||
* $this->NewMasterCommand();
|
||
* ->Select(...)
|
||
* ->From(...)
|
||
* ->InnerJoin|LeftJoin|RightJoin(...)
|
||
* ->On(...)
|
||
* ->Where(...)
|
||
* ->GroupBy(...)
|
||
* ->Having(...)
|
||
* ->OrderBy(...)
|
||
* ->Request()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Insert(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Update(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Delete(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Truncate(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Merge(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->MergeOn(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->CallStoredProcedure(...)
|
||
* ->Values(...)
|
||
* ->Request()/Execute()
|
||
**********************************
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewMasterCommand();
|
||
|
||
/**
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewServantCommand();
|
||
}
|
||
|
||
|
||
/**
|
||
* Class BaseMethodHelper
|
||
* 自定义接口对象基类
|
||
* @auther 应俊
|
||
*/
|
||
//abstract class BaseMethodHelper implements IBaseMethod
|
||
class BaseMethodHelper implements IBaseMethod
|
||
{
|
||
/**
|
||
* @note 错误码
|
||
* @var int
|
||
* @auther 应俊
|
||
*/
|
||
protected $ErrorCode;
|
||
|
||
/**
|
||
* @note 错误信息
|
||
* @var string|mixed
|
||
* @auther 应俊
|
||
*/
|
||
protected $ErrorInfo;
|
||
|
||
/**
|
||
* @var IPDOHelper
|
||
*/
|
||
protected $MasterDatabase;
|
||
|
||
/**
|
||
* @var array of IPDOHelper
|
||
*/
|
||
protected $ServantDatabase;
|
||
|
||
/**
|
||
* @note constructor.
|
||
* @auther
|
||
*/
|
||
public function __construct()
|
||
{
|
||
$this->MasterDatabase = null;
|
||
$this->ServantDatabase = array();
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
}
|
||
|
||
|
||
/**
|
||
* @note destructor
|
||
*/
|
||
public function __destruct()
|
||
{
|
||
$this->PDO_Close();
|
||
|
||
if (is_object($this->ServantDatabase))
|
||
$this->ServantDatabase = (array)$this->ServantDatabase;
|
||
if (is_array($this->ServantDatabase))
|
||
{
|
||
foreach ($this->ServantDatabase as &$ServantDatabase)
|
||
{
|
||
$ServantDatabase = null;
|
||
}
|
||
}
|
||
|
||
$this->ServantDatabase = array();
|
||
$this->MasterDatabase = null;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 请求方法(get, post)
|
||
* @return string
|
||
*/
|
||
public function RequestMethod()
|
||
{
|
||
return $_SERVER['REQUEST_METHOD'];
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 连接到主数据库
|
||
* @return bool
|
||
*/
|
||
public function MasterConnect()
|
||
{
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* @note 连接到从数据库
|
||
* @return bool
|
||
*/
|
||
public function ServantConnect()
|
||
{
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* @note 连接到数据库
|
||
* @return bool
|
||
*/
|
||
public function PDO_Connect()
|
||
{
|
||
return call_user_func_array(array(&$this, 'MasterConnect'), func_get_args());
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 关闭pdo连接
|
||
*/
|
||
public function PDO_Close()
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
if (null != $this->MasterDatabase)
|
||
{
|
||
if (!$this->MasterDatabase->Close())
|
||
{
|
||
$this->SetDatabaseErrors($this->MasterDatabase);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
if (is_array($this->ServantDatabase) && !empty($this->ServantDatabase))
|
||
{
|
||
foreach ($this->ServantDatabase as $Database)
|
||
{
|
||
if (!($Database instanceof IPDOHelper))
|
||
continue;
|
||
|
||
if (!$Database->Close())
|
||
{
|
||
$this->SetDatabaseErrors($Database);
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note pdo请求数据(有返回结果集)
|
||
* @param mixed
|
||
* @return mixed|null
|
||
*/
|
||
public function PDO_Request()
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
/// 先尝试用从数据库连接对象去查询记录
|
||
foreach ($this->ServantDatabase as $Database)
|
||
{
|
||
if (null == $Database || !($Database instanceof IPDOHelper))
|
||
continue;
|
||
|
||
/// 调用查询方法,并返回结果
|
||
$Return = call_user_func_array(array(&$Database, 'Request'), func_get_args());
|
||
$this->SetDatabaseErrors($Database);
|
||
return $Return;
|
||
}
|
||
|
||
/// 如果无从数据库连接对象则使用主数据库连接对象去查询记录
|
||
if ($this->MasterDatabase instanceof IPDOHelper)
|
||
{
|
||
/// 调用查询方法,并返回结果
|
||
$Return = call_user_func_array(array(&$this->MasterDatabase, 'Request'), func_get_args());
|
||
$this->SetDatabaseErrors($this->MasterDatabase);
|
||
return $Return;
|
||
}
|
||
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return null;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note pdo执行语句(无返回结果集)
|
||
* @param mixed
|
||
* @return mixed|null
|
||
*/
|
||
public function PDO_Execute()
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
if ($this->MasterDatabase instanceof IPDOHelper)
|
||
{
|
||
$Return = call_user_func_array(array(&$this->MasterDatabase, 'Execute'), func_get_args());
|
||
$this->SetDatabaseErrors($this->MasterDatabase);
|
||
return $Return;
|
||
}
|
||
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return null;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 最后一次执行插入后,产生的自动编号
|
||
* @param string $name
|
||
* @return bool|null|string
|
||
*/
|
||
public function PDO_LastInsertId($name = null)
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
if (null != $this->MasterDatabase)
|
||
return $this->MasterDatabase->LastInsertId($name);
|
||
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return null;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 开启一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_BeginTransaction($Database = null)
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
if (null == $Database)
|
||
$Database = $this->MasterDatabase;
|
||
|
||
if (null == $Database)
|
||
{
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return null;
|
||
}
|
||
elseif ($Database->InTransaction())
|
||
return true;
|
||
else
|
||
return $Database->BeginTransaction();
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 提交一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_Commit($Database = null)
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
if (null == $Database)
|
||
$Database = $this->MasterDatabase;
|
||
|
||
if (null == $Database)
|
||
{
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return null;
|
||
}
|
||
elseif ($Database->InTransaction())
|
||
return $Database->Commit();
|
||
else
|
||
return false;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 回滚一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_Rollback($Database = null)
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
if (null == $Database)
|
||
$Database = $this->MasterDatabase;
|
||
|
||
if (null == $Database)
|
||
{
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return null;
|
||
}
|
||
elseif ($Database->InTransaction())
|
||
return $Database->Rollback();
|
||
else
|
||
return false;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 最后一次执行的语句是否成功。
|
||
* @param IPDOHelper $Database
|
||
* @return bool
|
||
*/
|
||
public function PDO_IsDone($Database = null)
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
if (null == $Database)
|
||
$Database = $this->MasterDatabase;
|
||
|
||
if (null != $Database)
|
||
{
|
||
$Return = call_user_func_array(array(&$Database, 'IsDone'), func_get_args());
|
||
$this->SetDatabaseErrors($Database);
|
||
return $Return;
|
||
}
|
||
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* @note 生成一个随机字符串
|
||
* @param string $prefix 前缀
|
||
* @return string
|
||
*/
|
||
public function GetRandString($prefix = '')
|
||
{
|
||
return (empty($prefix) ? '' : $prefix) . rand(1000, 9999);
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 创建一个新的id
|
||
* @param string $prefix 前缀
|
||
* @param bool $md5 是否md5加密
|
||
* @return string
|
||
*/
|
||
public function CreateNewIdent($prefix = '', $md5 = false)
|
||
{
|
||
if (empty($prefix))
|
||
$prefix = 'NEW';
|
||
|
||
if ($md5)
|
||
return $prefix . md5(date('YmdHis') . rand(1000, 9999));
|
||
else
|
||
return $prefix . date('YmdHis') . rand(1000, 9999);
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取错误值
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return int
|
||
*/
|
||
public function GetErrorCode($Database = null, $AlwaysLocal = false)
|
||
{
|
||
if (null == $Database)
|
||
$Database = $this->MasterDatabase;
|
||
|
||
if ($AlwaysLocal || null == $Database)
|
||
return $this->ErrorCode;
|
||
else
|
||
return $Database->GetErrorCode();
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 设置错误值
|
||
* @param int $ErrorCode
|
||
*/
|
||
public function SetErrorCode($ErrorCode = 0)
|
||
{
|
||
$this->ErrorCode = $ErrorCode;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取错误信息
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return mixed|string
|
||
*/
|
||
public function GetErrorInfo($Database = null, $AlwaysLocal = false)
|
||
{
|
||
if (null == $Database)
|
||
$Database = $this->MasterDatabase;
|
||
|
||
if ($AlwaysLocal || null == $Database)
|
||
return $this->ErrorInfo;
|
||
else
|
||
return $Database->GetErrorInfo();
|
||
}
|
||
|
||
/**
|
||
* @note 设置错误信息
|
||
* @param string $ErrorInfo
|
||
*/
|
||
public function SetErrorInfo($ErrorInfo = null)
|
||
{
|
||
$this->ErrorInfo = $ErrorInfo;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取错误信息
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return mixed
|
||
*/
|
||
public function GetErrors($Database = null, $AlwaysLocal = false)
|
||
{
|
||
if (null == $Database)
|
||
$Database = $this->MasterDatabase;
|
||
|
||
if ($AlwaysLocal || null == $Database)
|
||
return array('ErrorCode' => $this->ErrorCode, 'ErrorInfo' => $this->ErrorInfo);
|
||
else
|
||
return $Database->GetErrors();
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 设置错误信息
|
||
* @param int|mixed $ErrorCode 错误码
|
||
* @param string|mixed $ErrorInfo 错误信息
|
||
* @return mixed
|
||
*/
|
||
public function SetErrors($ErrorCode, $ErrorInfo)
|
||
{
|
||
/*
|
||
if (is_object($ErrorInfo))
|
||
{
|
||
$ErrorInfo = (array)$ErrorInfo;
|
||
}
|
||
|
||
if (is_array($ErrorInfo))
|
||
{
|
||
//array_keys
|
||
$keys = array_keys($ErrorInfo);
|
||
$ErrorInfo = $ErrorInfo[$keys[count($keys) - 1]];
|
||
}
|
||
|
||
//if (is_object($ErrorInfo) || is_array($ErrorInfo))
|
||
//{
|
||
// $ErrorInfo = serialize($ErrorInfo);
|
||
//}
|
||
|
||
$this->ErrorCode = $ErrorCode;
|
||
$this->ErrorInfo = $ErrorInfo;
|
||
|
||
return true;
|
||
*/
|
||
switch (gettype($ErrorInfo))
|
||
{
|
||
case 'object':
|
||
case 'array':
|
||
$ErrorInfo = JsonObjectToJsonString($ErrorInfo);
|
||
break;
|
||
}
|
||
|
||
$this->ErrorCode = $ErrorCode;
|
||
$this->ErrorInfo = Characet($ErrorInfo);
|
||
|
||
return true;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 设置数据库的错误信息
|
||
* @param IPDOHelper $Helper
|
||
* @return bool
|
||
*/
|
||
public function SetDatabaseErrors($Helper)
|
||
{
|
||
if (null != $Helper)
|
||
{
|
||
$this->SetErrors($Helper->GetErrorCode(), $Helper->GetErrorInfo());
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
$this->SetErrors(ERRORCODE_NOCONNECTED, ERRORINFO_NOCONNECTED);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取主数据库对象
|
||
* @return IPDOHelper
|
||
*/
|
||
public function GetMasterDatabase()
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
return $this->MasterDatabase;
|
||
}
|
||
|
||
/**
|
||
* @note 获取从数据库对象
|
||
* @return IPDOHelper
|
||
*/
|
||
public function GetServantDatabase()
|
||
{
|
||
$this->SetErrors(ERRORCODE_SUCCESS, ERRORINFO_SUCCESS);
|
||
|
||
foreach ($this->ServantDatabase as $Database)
|
||
{
|
||
if (null != $Database && ($Database instanceof IPDOHelper))
|
||
return $Database;
|
||
}
|
||
|
||
return $this->GetMasterDatabase();
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* $this->NewMasterCommand();
|
||
* ->Select(...)
|
||
* ->From(...)
|
||
* ->InnerJoin|LeftJoin|RightJoin(...)
|
||
* ->On(...)
|
||
* ->Where(...)
|
||
* ->GroupBy(...)
|
||
* ->Having(...)
|
||
* ->OrderBy(...)
|
||
* ->Request()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Insert(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Update(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Delete(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Truncate(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Merge(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->MergeOn(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->CallStoredProcedure(...)
|
||
* ->Values(...)
|
||
* ->Request()/Execute()
|
||
**********************************
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewMasterCommand()
|
||
{
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return null;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewServantCommand()
|
||
{
|
||
$this->SetErrors(ERRORCODE_NOIMPLEMENT, ERRORINFO_NOIMPLEMENT);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
|
||
class BaseMethodHelperWithOracle extends BaseMethodHelper
|
||
{
|
||
/**
|
||
* @note 连接到主数据库
|
||
* @return bool
|
||
*/
|
||
public function MasterConnect()
|
||
{
|
||
if (null == $this->MasterDatabase)
|
||
{
|
||
$this->MasterDatabase = new ORACLEHelper();
|
||
$this->MasterDatabase->FetchStyle = PDO::FETCH_ASSOC;
|
||
}
|
||
else
|
||
$this->MasterDatabase->Close();
|
||
|
||
if (call_user_func_array(array(&$this->MasterDatabase, 'Connect'), func_get_args()))
|
||
return true;
|
||
|
||
$this->SetDatabaseErrors($this->MasterDatabase);
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* @note 连接到从数据库
|
||
* @return bool
|
||
*/
|
||
public function ServantConnect()
|
||
{
|
||
$ServantDatabase = new ORACLEHelper();
|
||
$ServantDatabase->FetchStyle = PDO::FETCH_ASSOC;
|
||
|
||
if (call_user_func_array(array(&$ServantDatabase, 'Connect'), func_get_args()))
|
||
{
|
||
array_push($this->ServantDatabase, $ServantDatabase);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
$this->SetDatabaseErrors($ServantDatabase);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* $this->NewMasterCommand();
|
||
* ->Select(...)
|
||
* ->From(...)
|
||
* ->InnerJoin|LeftJoin|RightJoin(...)
|
||
* ->On(...)
|
||
* ->Where(...)
|
||
* ->GroupBy(...)
|
||
* ->Having(...)
|
||
* ->OrderBy(...)
|
||
* ->Request()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Insert(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Update(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Delete(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Truncate(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Merge(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->MergeOn(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->CallStoredProcedure(...)
|
||
* ->Values(...)
|
||
* ->Request()/Execute()
|
||
**********************************
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewMasterCommand()
|
||
{
|
||
return new ORACLECommand($this->GetMasterDatabase());
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewServantCommand()
|
||
{
|
||
return new ORACLECommand($this->GetServantDatabase());
|
||
}
|
||
}
|
||
|
||
|
||
class BaseMethodHelperWithSqlSrv extends BaseMethodHelper
|
||
{
|
||
/**
|
||
* @note 连接到主数据库
|
||
* @return bool
|
||
*/
|
||
public function MasterConnect()
|
||
{
|
||
if (null == $this->MasterDatabase)
|
||
{
|
||
$this->MasterDatabase = new SQLSRVHelper();
|
||
$this->MasterDatabase->FetchStyle = PDO::FETCH_ASSOC;
|
||
}
|
||
else
|
||
$this->MasterDatabase->Close();
|
||
|
||
if (call_user_func_array(array(&$this->MasterDatabase, 'Connect'), func_get_args()))
|
||
return true;
|
||
|
||
$this->SetDatabaseErrors($this->MasterDatabase);
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* @note 连接到从数据库
|
||
* @return bool
|
||
*/
|
||
public function ServantConnect()
|
||
{
|
||
$ServantDatabase = new SQLSRVHelper();
|
||
$ServantDatabase->FetchStyle = PDO::FETCH_ASSOC;
|
||
|
||
if (call_user_func_array(array(&$ServantDatabase, 'Connect'), func_get_args()))
|
||
{
|
||
array_push($this->ServantDatabase, $ServantDatabase);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
$this->SetDatabaseErrors($ServantDatabase);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* $this->NewMasterCommand();
|
||
* ->Select(...)
|
||
* ->From(...)
|
||
* ->InnerJoin|LeftJoin|RightJoin(...)
|
||
* ->On(...)
|
||
* ->Where(...)
|
||
* ->GroupBy(...)
|
||
* ->Having(...)
|
||
* ->OrderBy(...)
|
||
* ->Request()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Insert(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Update(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Delete(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Truncate(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Merge(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->MergeOn(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->CallStoredProcedure(...)
|
||
* ->Values(...)
|
||
* ->Request()/Execute()
|
||
**********************************
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewMasterCommand()
|
||
{
|
||
return new SQLSRVCommand($this->GetMasterDatabase());
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewServantCommand()
|
||
{
|
||
return new SQLSRVCommand($this->GetServantDatabase());
|
||
}
|
||
}
|
||
|
||
|
||
class BaseMethodHelperWithMySql extends BaseMethodHelper
|
||
{
|
||
/**
|
||
* @note 连接到主数据库
|
||
* @return bool
|
||
*/
|
||
public function MasterConnect()
|
||
{
|
||
if (null == $this->MasterDatabase)
|
||
{
|
||
$this->MasterDatabase = new MYSQLHelper();
|
||
$this->MasterDatabase->FetchStyle = PDO::FETCH_ASSOC;
|
||
}
|
||
else
|
||
$this->MasterDatabase->Close();
|
||
|
||
if (call_user_func_array(array(&$this->MasterDatabase, 'Connect'), func_get_args()))
|
||
return true;
|
||
|
||
$this->SetDatabaseErrors($this->MasterDatabase);
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* @note 连接到从数据库
|
||
* @return bool
|
||
*/
|
||
public function ServantConnect()
|
||
{
|
||
$ServantDatabase = new MYSQLHelper();
|
||
$ServantDatabase->FetchStyle = PDO::FETCH_ASSOC;
|
||
|
||
if (call_user_func_array(array(&$ServantDatabase, 'Connect'), func_get_args()))
|
||
{
|
||
array_push($this->ServantDatabase, $ServantDatabase);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
$this->SetDatabaseErrors($ServantDatabase);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* $this->NewMasterCommand();
|
||
* ->Select(...)
|
||
* ->From(...)
|
||
* ->InnerJoin|LeftJoin|RightJoin(...)
|
||
* ->On(...)
|
||
* ->Where(...)
|
||
* ->GroupBy(...)
|
||
* ->Having(...)
|
||
* ->OrderBy(...)
|
||
* ->Request()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Insert(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Update(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Delete(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Truncate(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Merge(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->MergeOn(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->CallStoredProcedure(...)
|
||
* ->Values(...)
|
||
* ->Request()/Execute()
|
||
**********************************
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewMasterCommand()
|
||
{
|
||
return new MYSQLCommand($this->GetMasterDatabase());
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewServantCommand()
|
||
{
|
||
return new MYSQLCommand($this->GetServantDatabase());
|
||
}
|
||
}
|
||
|
||
|
||
class BaseMethod extends Delegator //implements IBaseMethod
|
||
{
|
||
/**
|
||
* @note 是否启用redis
|
||
* @var boolean $UsedRedis
|
||
*/
|
||
private $UsedRedis;
|
||
/**
|
||
* @note redis服务器名或服务器地址
|
||
* @var string $RedisHostname
|
||
*/
|
||
private $RedisHostname;
|
||
/**
|
||
* @note redis服务器端口号
|
||
* @var integer $RedisHostport
|
||
*/
|
||
private $RedisHostport;
|
||
/**
|
||
* @note redis数据库名
|
||
* @var string $RedisDatabase
|
||
*/
|
||
private $RedisDatabase;
|
||
/**
|
||
* @note redis登录密码
|
||
* @var string $RedisPassword
|
||
*/
|
||
private $RedisPassword;
|
||
|
||
/**
|
||
* @note redis对象
|
||
* @var Redis $Redis
|
||
*/
|
||
private $Redis;
|
||
|
||
|
||
/**
|
||
* BaseMethod constructor.
|
||
*/
|
||
public function __construct()
|
||
{
|
||
/// todo: change the autogenerated stub
|
||
/// todo: 这里根据配置参数创建实际的数据库连接对象
|
||
|
||
$DatabaseType = $this->GetDatabaseType();
|
||
|
||
if (DATABASE_TYPE_NONE != $DatabaseType) /// 需要数据库连接
|
||
{
|
||
/// 根据连接参数创建连接参数对象
|
||
$MasterConnectParameter = $this->GetMasterDatabaseConnectParameter();
|
||
$ServantConnectParameter = array();
|
||
for ($Index = 0; $Index < $this->GetServantDatabaseCount(); $Index++)
|
||
{
|
||
array_push($ServantConnectParameter, $this->GetServantDatabaseConnectParameter($Index));
|
||
}
|
||
|
||
switch ($DatabaseType) /// 根据数据库类型来判断要创建哪种实例
|
||
{
|
||
case DATABASE_TYPE_NONE: /// 无数据库
|
||
{
|
||
$this->Helper = null;
|
||
break;
|
||
}
|
||
|
||
case DATABASE_TYPE_ORACLE: /// oracle
|
||
{
|
||
$this->Helper = new BaseMethodHelperWithOracle();
|
||
break;
|
||
}
|
||
|
||
case DATABASE_TYPE_SQLSRV: /// ms sql server
|
||
{
|
||
$this->Helper = new BaseMethodHelperWithSqlSrv();
|
||
break;
|
||
}
|
||
|
||
case DATABASE_TYPE_MYSQL: /// mysql
|
||
{
|
||
$this->Helper = new BaseMethodHelperWithMySql();
|
||
break;
|
||
}
|
||
|
||
default: /// 如果是不支持的数据库连接类型则抛出异常。
|
||
{
|
||
$Helper = null;
|
||
throw new Exception(sprintf(ERRORINFO_BADPARAMETER, 'dbtype', DATABASE_TYPE) . ' in ' . __FILE__ . ' on line ' . __LINE__, ERRORCODE_BADPARAMETER);
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!is_null($this->Helper) && ($this->Helper instanceof IBaseMethod))
|
||
{
|
||
/// 连接到主数据库实例
|
||
if (!$this->Helper->MasterConnect($MasterConnectParameter))
|
||
throw new Exception($this->Helper->GetErrorInfo(), $this->Helper->GetErrorCode());
|
||
|
||
/// 连接到从数据库实例
|
||
foreach ($ServantConnectParameter as $ConnectParameter)
|
||
{
|
||
if (!$this->Helper->ServantConnect($ConnectParameter))
|
||
throw new Exception($this->Helper->GetErrorInfo(), $this->Helper->GetErrorCode());
|
||
}
|
||
}
|
||
else /// 如果helper对象为空或不是IBaseMethod接口实例则抛出异常。
|
||
throw new Exception(sprintf(ERRORINFO_BADPARAMETER, 'dbtype', DATABASE_TYPE) . ' in ' . __FILE__ . ' on line ' . __LINE__, ERRORCODE_BADPARAMETER);
|
||
}
|
||
else
|
||
$this->Helper = new BaseMethodHelper(); /// 不需要数据库连接
|
||
|
||
/// 2018-01-08: 增加redis库的支持,用于记录用户token
|
||
$this->UsedRedis = REDIS_ENABLED; /// 是否启用redis
|
||
$this->RedisHostname = REDIS_HOSTNAME; /// redis服务器名或服务器地址
|
||
$this->RedisHostport = REDIS_HOSTPORT; /// redis服务器端口号
|
||
$this->RedisDatabase = REDIS_DATABASE; /// redis数据库名
|
||
$this->RedisPassword = REDIS_PASSWORD; /// redis登录密码
|
||
|
||
/// 2018-01-08: 尝试连接到redis库
|
||
if ($this->UsedRedis)
|
||
{
|
||
try
|
||
{
|
||
if (!empty($this->Redis = new Redis()))
|
||
{
|
||
$this->Redis->POpen($this->RedisHostname, $this->RedisHostport);
|
||
//if (!$this->Redis->Ping())
|
||
// throw new Exception(ERRORCODE_NOCONNECTED, ERRORINFO_NOCONNECTED);
|
||
}
|
||
}
|
||
catch (Exception $Exception)
|
||
{
|
||
$this->SetErrors($Exception->GetCode(), $Exception->GetMessage());
|
||
if (ERRORCODE_SUCCESS == $this->GetErrorCode())
|
||
$this->SetErrorCode(ERRORCODE_UNKNOWN);
|
||
if (ERRORINFO_SUCCESS == $this->GetErrorInfo() || empty($this->GetErrorInfo()))
|
||
$this->SetErrorInfo(ERRORINFO_UNKNOWN);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public function __destruct()
|
||
{
|
||
// TODO: Implement __destruct() method.
|
||
/// 2018-01-08: 回收redis资源
|
||
if ($this->UsedRedis && !empty($this->Redis))
|
||
{
|
||
try
|
||
{
|
||
$this->Redis->Close();
|
||
$this->Redis = null;
|
||
}
|
||
catch (Exception $Exception)
|
||
{
|
||
$this->SetErrors($Exception->GetCode(), $Exception->GetMessage());
|
||
if (ERRORCODE_SUCCESS == $this->GetErrorCode())
|
||
$this->SetErrorCode(ERRORCODE_UNKNOWN);
|
||
if (ERRORINFO_SUCCESS == $this->GetErrorInfo() || empty($this->GetErrorInfo()))
|
||
$this->SetErrorInfo(ERRORINFO_UNKNOWN);
|
||
}
|
||
}
|
||
|
||
$this->Helper = null;
|
||
}
|
||
|
||
|
||
/**
|
||
* @return IBaseMethod
|
||
*/
|
||
public function GetHelper()
|
||
{
|
||
return parent::GetHelper();
|
||
}
|
||
|
||
/**
|
||
* @param IBaseMethod $Helper
|
||
*/
|
||
public function SetHelper($Helper)
|
||
{
|
||
parent::SetHelper($Helper);
|
||
}
|
||
|
||
|
||
/// 2018-06-11: 增加数据库连接信息
|
||
|
||
/**
|
||
* @note 返回需要连接的数据库类型(DATABASE_TYPE_NONE:无连接;DATABASE_TYPE_ORACLE:oracle;DATABASE_TYPE_SQLSRV:ms sql server;DATABASE_TYPE_MYSQL:mysql;)
|
||
* @return string
|
||
*/
|
||
protected function GetDatabaseType()
|
||
{
|
||
return DATABASE_TYPE;
|
||
}
|
||
|
||
/**
|
||
* @note 返回主数据库连接信息
|
||
* @return ConnectParameter
|
||
*/
|
||
protected function GetMasterDatabaseConnectParameter()
|
||
{
|
||
return new ConnectParameter(MASTER_HOSTNAME, MASTER_HOSTPORT, MASTER_DATABASE, MASTER_USERNAME, MASTER_PASSWORD, MASTER_PERSISTENT, MASTER_CHARSET);
|
||
}
|
||
|
||
/**
|
||
* @note 返回从数据库个数
|
||
* @return int
|
||
*/
|
||
protected function GetServantDatabaseCount()
|
||
{
|
||
return SERVANT_COUNT;
|
||
}
|
||
|
||
/**
|
||
* @note 返回指定序号的从数据库的连接信息
|
||
* @param int $Index
|
||
* @return mixed
|
||
*/
|
||
protected function GetServantDatabaseConnectParameter($Index = -1)
|
||
{
|
||
$ServantCode = "return new ConnectParameter(SERVANT_HOSTNAME_{$Index}, SERVANT_HOSTPORT_{$Index}, SERVANT_DATABASE_{$Index}, SERVANT_USERNAME_{$Index}, SERVANT_PASSWORD_{$Index}, SERVANT_PERSISTENT_{$Index}, SERVANT_CHARSET_{$Index});";
|
||
return eval($ServantCode);
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* Get the value related to the specified key
|
||
*
|
||
* @param string $Name
|
||
* @return string|bool: If key didn't exist, FALSE is returned. Otherwise, the value related to this key is returned.
|
||
* @link http://redis.io/commands/get
|
||
* @example $redis->get('key');
|
||
*/
|
||
public function GetMemoryValue($Name)
|
||
{
|
||
if (!$this->UsedRedis || empty($this->Redis) || empty($Name))
|
||
return null;
|
||
|
||
if (!$this->Redis->Exists($Name))
|
||
return null;
|
||
|
||
return $this->Redis->Get($Name);
|
||
}
|
||
|
||
/**
|
||
* Set the string value in argument as value of the key.
|
||
*
|
||
* @param string $Name
|
||
* @param string $Value
|
||
* @param int $Timeout [optional] Calling setex() is preferred if you want a timeout.
|
||
* @return bool: TRUE if the command is successful.
|
||
* @link http://redis.io/commands/set
|
||
* @example $redis->set('key', 'value');
|
||
*/
|
||
public function SetMemoryValue($Name, $Value, $Timeout = 0)
|
||
{
|
||
if (!$this->UsedRedis || empty($this->Redis) || empty($Name))
|
||
return false;
|
||
|
||
$OldValue = $this->Redis->Get($Name);
|
||
if (strcmp($OldValue, $Value) == 0)
|
||
{
|
||
if (empty($Timeout))
|
||
return true;
|
||
else
|
||
return $this->Redis->Expire($Name, $Timeout);
|
||
}
|
||
else
|
||
return $this->Redis->Set($Name, $Value, $Timeout);
|
||
}
|
||
|
||
public function ExpireMemoryValue($Name, $Timeout)
|
||
{
|
||
if (!$this->UsedRedis || empty($this->Redis) || empty($Name))
|
||
return false;
|
||
|
||
return $this->Redis->Expire($Name, $Timeout);
|
||
}
|
||
|
||
/**
|
||
* @note 锁定内存
|
||
* @param string $Name 名称
|
||
* @param int $Timeout 超时时间
|
||
* @return bool 成功返回true,否则返回false
|
||
*/
|
||
public function LockMemory($Name = '', $Timeout = 0)
|
||
{
|
||
if ($this->UsedRedis && !empty($this->Redis) && ($this->Redis instanceof Redis))
|
||
{
|
||
if (empty($Timeout))
|
||
$Timeout = 60;
|
||
if (empty($Name))
|
||
$Name = 'lock';
|
||
|
||
/// 尝试写入时间戳
|
||
if ($this->Redis->set($Name, time() + $Timeout, array('nx', 'ex' => $Timeout)))
|
||
return true; /// 成功写入后返回true
|
||
|
||
/// 写入失败后,尝试获取已有的时间戳
|
||
$LockTime = $this->Redis->get($Name);
|
||
/// 如果已有的锁已过期则尝试删除锁并重新写入
|
||
if (time() > $LockTime)
|
||
{
|
||
$this->UnlockMemory($Name);
|
||
return $this->Redis->set($Name, time() + $Timeout, array('nx', 'ex' => $Timeout));
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 解除内存锁定
|
||
* @param string|mixed $Name 名称
|
||
* @return int 成功返回1,否则返回0
|
||
*/
|
||
public function UnlockMemory($Name = '')
|
||
{
|
||
if ($this->UsedRedis && !empty($this->Redis) && ($this->Redis instanceof Redis))
|
||
return $this->Redis->del(empty($Name) ? 'lock' : $Name);
|
||
else
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* @note 锁定文件
|
||
* @param string $Name
|
||
* @return null|resource
|
||
*/
|
||
public function LockFile($Name = '')
|
||
{
|
||
$PathName = dirname(__DIR__) . '/cache/';
|
||
if (!is_dir($PathName))
|
||
mkdir($PathName, 0777, true);
|
||
|
||
$FileName = $PathName . (empty($Name) ? 'lock' : $Name);
|
||
if ($LockFile = fopen($FileName, 'w+'))
|
||
{
|
||
if (flock($LockFile, LOCK_EX))
|
||
return $LockFile;
|
||
|
||
fclose($LockFile);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* @note 解除文件锁定
|
||
* @param string $Name
|
||
* @param null $Handle
|
||
* @return bool
|
||
*/
|
||
public function UnlockFile($Name = '', $Handle = null)
|
||
{
|
||
$FileName = dirname(__DIR__) . '/cache/' . (empty($Name) ? 'lock' : $Name);
|
||
if (!$Handle)
|
||
return false;
|
||
flock($Handle, LOCK_UN);
|
||
|
||
if (!fclose($Handle))
|
||
return false;
|
||
|
||
return unlink($FileName);
|
||
}
|
||
|
||
/// ================================================================================================================
|
||
|
||
|
||
/**
|
||
* @note 请求方法(get, post)
|
||
* @return string
|
||
*/
|
||
public function RequestMethod()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'RequestMethod'), func_get_args());
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 连接到主数据库
|
||
* @return bool
|
||
*/
|
||
public function MasterConnect()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'MasterConnect'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 连接到从数据库
|
||
* @return bool
|
||
*/
|
||
public function ServantConnect()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'ServantConnect'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 连接到数据库
|
||
* @return bool
|
||
*/
|
||
public function PDO_Connect()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_Connect'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 关闭pdo连接
|
||
*/
|
||
public function PDO_Close()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_Close'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note pdo请求数据(有返回结果集)
|
||
* @param mixed
|
||
* @return mixed|null
|
||
*/
|
||
public function PDO_Request()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_Request'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note pdo执行语句(无返回结果集)
|
||
* @param mixed
|
||
* @return mixed|null
|
||
*/
|
||
public function PDO_Execute()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_Execute'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 最后一次执行插入后,产生的自动编号
|
||
* @param string $name
|
||
* @return bool|null|string
|
||
*/
|
||
public function PDO_LastInsertId($name = null)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_LastInsertId'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 开启一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_BeginTransaction($Database = null)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_BeginTransaction'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 提交一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_Commit($Database = null)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_Commit'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 回滚一个事务
|
||
* @param IPDOHelper $Database
|
||
* @return bool|null
|
||
*/
|
||
public function PDO_Rollback($Database = null)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_Rollback'), func_get_args());
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 最后一次执行的语句是否成功。
|
||
* @param IPDOHelper $Database
|
||
* @return bool
|
||
*/
|
||
public function PDO_IsDone($Database = null)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'PDO_IsDone'), func_get_args());
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 生成一个随机字符串
|
||
* @param string $prefix 前缀
|
||
* @return string
|
||
*/
|
||
public function GetRandString($prefix = '')
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'GetRandString'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 创建一个新的id
|
||
* @param string $prefix 前缀
|
||
* @param bool $md5 是否md5加密
|
||
* @return string
|
||
*/
|
||
public function CreateNewIdent($prefix = '', $md5 = false)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'CreateNewIdent'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 获取错误值
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return int
|
||
*/
|
||
public function GetErrorCode($Database = null, $AlwaysLocal = false)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'GetErrorCode'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 设置错误值
|
||
* @param $ErrorCode
|
||
* @return mixed
|
||
*/
|
||
public function SetErrorCode($ErrorCode)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'SetErrorCode'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 获取错误信息
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return mixed|string
|
||
*/
|
||
public function GetErrorInfo($Database = null, $AlwaysLocal = false)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'GetErrorInfo'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 设置错误信息
|
||
* @param $ErrorInfo
|
||
* @return mixed
|
||
*/
|
||
public function SetErrorInfo($ErrorInfo)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'SetErrorInfo'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 获取错误信息
|
||
* @param IPDOHelper $Database
|
||
* @param bool $AlwaysLocal
|
||
* @return mixed
|
||
*/
|
||
public function GetErrors($Database = null, $AlwaysLocal = false)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'GetErrors'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 设置错误信息
|
||
* @param int|mixed $ErrorCode 错误码
|
||
* @param string|mixed $ErrorInfo 错误信息
|
||
* @return mixed
|
||
*/
|
||
public function SetErrors($ErrorCode, $ErrorInfo)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'SetErrors'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 设置数据库的错误信息
|
||
* @param IPDOHelper $Helper
|
||
* @return bool
|
||
*/
|
||
public function SetDatabaseErrors($Helper)
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'SetDatabaseErrors'), func_get_args());
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取主数据库对象
|
||
* @return IPDOHelper
|
||
*/
|
||
public function GetMasterDatabase()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'GetMasterDatabase'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 获取从数据库对象
|
||
* @return IPDOHelper
|
||
*/
|
||
public function GetServantDatabase()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'GetServantDatabase'), func_get_args());
|
||
}
|
||
|
||
|
||
/**
|
||
* $this->NewMasterCommand();
|
||
* ->Select(...)
|
||
* ->From(...)
|
||
* ->InnerJoin|LeftJoin|RightJoin(...)
|
||
* ->On(...)
|
||
* ->Where(...)
|
||
* ->GroupBy(...)
|
||
* ->Having(...)
|
||
* ->OrderBy(...)
|
||
* ->Request()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Insert(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Update(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Delete(...)
|
||
* ->Where(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Truncate(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->Merge(...)
|
||
* ->Fields(...)
|
||
* ->Values(...)
|
||
* ->MergeOn(...)
|
||
* ->Execute()
|
||
**********************************
|
||
* $this->NewMasterCommand()
|
||
* ->CallStoredProcedure(...)
|
||
* ->Values(...)
|
||
* ->Request()/Execute()
|
||
**********************************
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewMasterCommand()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'NewMasterCommand'), func_get_args());
|
||
}
|
||
|
||
/**
|
||
* @note 获取ISQLCommand对象.
|
||
* @return ISQLCommand
|
||
*/
|
||
public function NewServantCommand()
|
||
{
|
||
return call_user_func_array(array(&$this->Helper, 'NewServantCommand'), func_get_args());
|
||
}
|
||
|
||
/// ================================================================================================================
|
||
|
||
|
||
public function log($data, $tmp = '')
|
||
{
|
||
$str = date('H:m:s').' => '. $tmp .': ';
|
||
if(is_array($data))
|
||
$str .= json_encode($data);
|
||
else
|
||
$str .= $data;
|
||
file_put_contents(date('Y-m-d').'.txt', $str . PHP_EOL, FILE_APPEND);
|
||
}
|
||
}
|
||
|
||
|