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); } }