/** * Class BaseMethod * �ӿڶ������ * @auther Ӧ�� */ class BaseMethod { // raw��������ָ���ֶ��Ƿ�ԭʼ���ݴ��䣬Ϊtrue���Բ�����ʽ���ݸ�pdo const PROPERTYNAME_RAW = 'raw'; // type���������ƶ��ֶ��������� const PROPERTYNAME_TYPE = 'type'; // value���������ֶε�ֵ const PROPERTYNAME_VALUE = 'value'; // type���Կ�ѡֵ const TYPENAME_STRING = 'string'; // �ַ������� const TYPENAME_NUMBER = 'number'; // ��ֵ���� const TYPENAME_DATE = 'date'; // �������� const TYPENAME_TIMESTAMP = 'timestamp'; // ʱ������� const TYPENAME_LOB = 'lob'; // ���������� const TYPENAME_RAW = 'raw'; // Դ�������� // where �ֶ�Ҫ�õ�������Ԫ���� const CONDITION_NAMES = 'conditions'; // �����ֶ� const CONDITION_VALUE = 'values'; // ����ֵ // ��ѯ�ֶ�Ҫ�õ�������Ԫ���� const FIELDNAME_FIELD = 'field'; // Դ�ֶ��� const FIELDNAME_ALIAS = 'alias'; // ���ֶ�����������Alias // sql����õ�������Ԫ���� const COMMAND_LINE = 'commandline'; const COMMAND_VARS = 'commandvars'; /** * @note ������ * @var int * @auther Ӧ�� */ protected $ErrorCode; /** * @note ������Ϣ * @var string|mixed * @auther Ӧ�� */ protected $ErrorInfo; /** * @var DatabaseHelper */ protected $Database; //private $ReferenceCharacter; private $RefCharLeft; private $RefCharRight; /** * BaseMethod constructor. */ public function __construct() { if (0 == strcasecmp('MYSQL', DATABASE_TYPE)) { $p = array( PARAMETERNAME_HOSTNAME => DB_HOSTNAME, PARAMETERNAME_HOSTPORT => DB_HOSTPORT, PARAMETERNAME_DATABASE => DB_DATABASE, PARAMETERNAME_USERNAME => DB_USERNAME, PARAMETERNAME_PASSWORD => DB_PASSWORD, PARAMETERNAME_CHARSET => DB_CHARSET, ); $this->Database = new MYSQLHelper(); $this->Database->FetchStyle = PDO::FETCH_ASSOC; if (!$this->Database->Connect($p)) { $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); $this->Database = null; } $this->RefCharLeft = '`'; $this->RefCharRight = '`'; } elseif (0 == strcasecmp('SQLSRV', DATABASE_TYPE)) { $p = array( PARAMETERNAME_HOSTNAME => DB_HOSTNAME, PARAMETERNAME_HOSTPORT => DB_HOSTPORT, PARAMETERNAME_DATABASE => DB_DATABASE, PARAMETERNAME_USERNAME => DB_USERNAME, PARAMETERNAME_PASSWORD => DB_PASSWORD, ); $this->Database = new SQLSRVHelper(); $this->Database->FetchStyle = PDO::FETCH_ASSOC; if (!$this->Database->Connect($p)) { $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); $this->Database = null; } $this->RefCharLeft = '['; $this->RefCharRight = ']'; } elseif (0 == strcasecmp('ORACLE', DATABASE_TYPE)) { $p = array( PARAMETERNAME_HOSTNAME => DB_HOSTNAME, PARAMETERNAME_HOSTPORT => DB_HOSTPORT, PARAMETERNAME_SERVICE => DB_DATABASE, PARAMETERNAME_USERNAME => DB_USERNAME, PARAMETERNAME_PASSWORD => DB_PASSWORD, ); $this->Database = new ORACLEHelper(); $this->Database->FetchStyle = PDO::FETCH_ASSOC; if (!$this->Database->Connect($p)) { $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); $this->Database = null; } $this->RefCharLeft = ''; $this->RefCharRight = ''; } else { $this->Database = null; $this->ErrorCode = ERRORCODE_BADPARAMETER; $this->ErrorInfo = sprintf(ERRORINFO_BADPARAMETER, 'dbtype', DATABASE_TYPE); } } /** * BaseMethod destructor */ public function __destruct() { $this->Database = null; } /** * @note pdo�������ݣ��з��ؽ������ * @param mixed * @return mixed|null */ protected function PDO_Request() { if (null == $this->Database) { return null; } $return = call_user_func_array(array($this->Database, 'Request'), func_get_args()); $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); return $return; } /** * @note pdoִ����䣨�޷��ؽ������ * @param mixed * @return mixed|null */ protected function PDO_Execute() { if (null == $this->Database) { return null; } $return = call_user_func_array(array($this->Database, 'Execute'), func_get_args()); $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); return $return; } /** * @note ���һ��ִ�в���󣬲������Զ���� * @param string $name * @return bool|null|string */ protected function PDO_LastInsertId($name = null) { if (null == $this->Database) { return null; } else { return $this->Database->LastInsertId($name); } } /** * @note ����һ������ * @return bool|null */ protected function PDO_BeginTransaction() { if (null == $this->Database) { return null; } else { return $this->Database->BeginTransaction(); } } /** * @note �ύһ������ * @return bool|null */ protected function PDO_Commit() { if (null == $this->Database) { return null; } else { return $this->Database->Commit(); } } /** * @note �ع�һ������ * @return bool|null */ protected function PDO_Rollback() { if (null == $this->Database) { return null; } else { return $this->Database->Rollback(); } } protected function GetRandString($prefix = '') { return (empty($prefix) ? '' : $prefix) . rand(1000, 9999); } /** * @note ����ʶ�������޶��� * @param string $Identifiers Ҫ����ı�ʶ�� * @param bool $Quoted �Ƿ��Ǵ������� * @param string $SpecialChar Ҫ�ر���(����)���ַ� */ protected function GetIdentifiers($Identifiers, $Quoted = false, $SpecialChar = null) { if (is_null($Identifiers) || '' == $Identifiers) { return ''; } elseif ($Quoted) { $Length = mb_strlen($Identifiers, USEDCHARSET); if ("'" == mb_substr($Identifiers, 0, 1, USEDCHARSET)) { $Length--; $Identifiers = mb_substr($Identifiers, 1, $Length, USEDCHARSET); } if ("'" == mb_substr($Identifiers, $Length - 1, 1, USEDCHARSET)) { $Length--; $Identifiers = mb_substr($Identifiers, 0, $Length, USEDCHARSET); } return "'" . str_replace("'", "''", $Identifiers) . "'"; } elseif (!empty($SpecialChar) && mb_strstr($Identifiers, $SpecialChar, false, USEDCHARSET)) { $Identifiers = str_replace($SpecialChar, $this->RefCharLeft . $SpecialChar . $this->RefCharRight, $Identifiers); return $this->RefCharLeft . $Identifiers . $this->RefCharRight; } else { return $this->RefCharLeft . $Identifiers . $this->RefCharRight; } } /** * @note ��ȡsql�������� * @param array|mixed $ands ���ֶ���=�ֶ�ֵ�� * @param array|mixed $ors ���ֶ���=�ֶ�ֵ�� * @return array */ private function GetWheres($ands = null, $ors = null) { if (is_object($ands)) { $ands = (array)$ands; } if (is_object($ors)) { $ors = (array)$ors; } $result = array( Self::CONDITION_NAMES => '', Self::CONDITION_VALUE => array(), ); if (!empty($ands) || !empty($ors)) { // and���� $_ands = '1 = 1'; if (is_array($ands)) { foreach ($ands as $k => $v) { if (null === $v) { continue; } if (is_object($v)) { $v = (array)$v; } // 2016-12-17��չ���ֶ�ֵ����ָ��raw���ԣ����rawΪtrue���Բ�����ʽ���ݡ� $raw = isset($v[Self::PROPERTYNAME_RAW]) ? $v[Self::PROPERTYNAME_RAW] : false; $type = isset($v[Self::PROPERTYNAME_TYPE]) ? $v[Self::PROPERTYNAME_TYPE] : Self::TYPENAME_RAW; // ֵ if (is_array($v)) { $v = isset($v[Self::PROPERTYNAME_VALUE]) ? $v[Self::PROPERTYNAME_VALUE] : null; } if ($raw) { switch ($type) { case Self::TYPENAME_DATE: { if (is_numeric($v)) { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $this->Timestamp2DatetimeFormat($v)); } else { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $this->String2DatetimeFormat($v)); } break; } case Self::TYPENAME_TIMESTAMP: { if (is_numeric($v)) { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $v); } else { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $this->Datetime2TimestampFormat($v)); } break; } case Self::TYPENAME_STRING: { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $this->GetIdentifiers($v, true)); break; } default: { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $v); break; } } } else { switch ($type) { case Self::TYPENAME_DATE: { if (is_numeric($v)) { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $this->Timestamp2DatetimeFormat('?')); } else { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $this->String2DatetimeFormat('?', false)); } break; } case Self::TYPENAME_TIMESTAMP: { if (is_numeric($v)) { $_ands .= sprintf(' and %s = ?', $this->GetIdentifiers($k)); } else { $_ands .= sprintf(' and %s = %s', $this->GetIdentifiers($k), $this->Datetime2TimestampFormat('?', false)); } break; } default: { $_ands .= sprintf(' and %s = ?', $this->GetIdentifiers($k)); break; } } array_push($result[Self::CONDITION_VALUE], $v); } } } else { $_ands = empty($ands) ? '' : $ands; } // or ���� $_ors = ''; if (is_array($ors)) { $_ors = '1 = 0'; foreach ($ors as $k => $v) { if (null === $v) { continue; } if (is_object($v)) { $v = (array)$v; } // 2016-12-17��չ���ֶ�ֵ����ָ��raw���ԣ����rawΪtrue���Բ�����ʽ���ݡ� $raw = isset($v[Self::PROPERTYNAME_RAW]) ? $v[Self::PROPERTYNAME_RAW] : false; $type = isset($v[Self::PROPERTYNAME_TYPE]) ? $v[Self::PROPERTYNAME_TYPE] : Self::TYPENAME_RAW; // ֵ if (is_array($v)) { $v = isset($v[Self::PROPERTYNAME_VALUE]) ? $v[Self::PROPERTYNAME_VALUE] : null; } if ($raw) { switch ($type) { case Self::TYPENAME_DATE: { if (is_numeric($v)) { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $this->Timestamp2DatetimeFormat($v)); } else { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $this->String2DatetimeFormat($v)); } break; } case Self::TYPENAME_TIMESTAMP: { if (is_numeric($v)) { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $v); } else { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $this->Datetime2TimestampFormat($v)); } break; } case Self::TYPENAME_STRING: { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $this->GetIdentifiers($v, true)); break; } default: { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $v); break; } } } else { switch ($type) { case Self::TYPENAME_DATE: { if (is_numeric($v)) { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $this->Timestamp2DatetimeFormat('?')); } else { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $this->String2DatetimeFormat('?', false)); } break; } case Self::TYPENAME_TIMESTAMP: { if (is_numeric($v)) { $_ors .= sprintf(' or %s = ?', $this->GetIdentifiers($k)); } else { $_ors .= sprintf(' or %s = %s', $this->GetIdentifiers($k), $this->Datetime2TimestampFormat('?', false)); } break; } default: { $_ors .= sprintf(' or %s = ?', $this->GetIdentifiers($k)); break; } } array_push($result[Self::CONDITION_VALUE], $v); } } } else { $_ors = empty($ors) ? '' : $ors; } if (empty($_ands) && empty($_ors)) { $result[Self::CONDITION_NAMES] = ''; } elseif (empty($_ands)) { $result[Self::CONDITION_NAMES] = $_ors; } elseif (empty($_ors)) { $result[Self::CONDITION_NAMES] = $_ands; } else { $result[Self::CONDITION_NAMES] = '(' . $_ands . ') and (' . $_ors . ')'; } } return $result; } /** * @note ��ȡsql����������Ϣ * @param array|mixed $orders ���ֶ���=�ֶ�ֵ�� * @return string */ private function GetOrders($orders = null) { if (empty($orders)) { return ''; } if (is_object($orders)) $orders = (array)$orders; if (is_array($orders)) { $os = ''; foreach ($orders as $k => $v) { $os .= sprintf(' %s %s,', $this->GetIdentifiers($k, false, '.'), empty($v) ? 'asc' : $v); } $os = mb_substr($os, 0, mb_strlen($os, USEDCHARSET) - 1, USEDCHARSET); } else { $os = $orders; } return $os; } /** * 2017-03-31 ���� * @note ��ȡsql���ķ�����Ϣ * @param array|mixed $groups ���ֶ���=�ֶ�ֵ�� * @return string */ private function GetGroups($groups = null) { if (empty($groups)) { return ''; } if (is_object($groups)) $groups = (array)$groups; if (is_array($groups)) { $gs = ''; foreach ($groups as $k => $v) { $gs .= sprintf(' %s,', $this->GetIdentifiers($v, false, '.')); } $gs = mb_substr($gs, 0, mb_strlen($gs, USEDCHARSET) - 1, USEDCHARSET); } else { $gs = $groups; } return $gs; } /** * @note ��ȡsql��ѯ���ı���Ϣ��2017-03-03�������÷������ڴ������������ * @param array|mixed $tables * @return string */ private function GetSelectTables($tables = null) { if (is_object($tables)) { $tables = (array)$tables; } if (is_array($tables)) { $return = ''; foreach($tables as $k => $v) { // ���� if (!empty($k) && !is_numeric($k)) { $return .= ',' . $this->GetIdentifiers($v, false, '.') . ' ' . $this->GetIdentifiers($k); } else { $return .= ',' . $this->GetIdentifiers($v, false, '.'); } } $sublen = mb_strlen(',', USEDCHARSET); $return = mb_substr($return, $sublen, mb_strlen($return, USEDCHARSET) - $sublen, USEDCHARSET); return $return; } else { return $this->GetIdentifiers($tables, false, ','); } } /** * @note ��ȡsql��ѯ�����ֶ���Ϣ * @param array|mixed $fields������=>ԭ�ֶ�/���ʽ�� * @return array */ private function GetSelectFields($fields = null) { $fs = array( Self::FIELDNAME_FIELD => '', Self::FIELDNAME_ALIAS => '', ); if (is_object($fields)) { $fields = (array)$fields; } if (is_array($fields)) { foreach($fields as $k => $v) { if (is_object($v) || is_array($v)) { $arr = $this->GetSelectFields($v); $fs[Self::FIELDNAME_FIELD] .= ', ' . $arr[Self::FIELDNAME_FIELD]; $fs[Self::FIELDNAME_ALIAS] .= ', ' . $arr[Self::FIELDNAME_ALIAS]; continue; } elseif (null === $v) { continue; } // 2017-03-03�����Ӷ������а���.�Ĵ��� $nv = mb_strstr($v, '.', false, USEDCHARSET); if (!empty($nv)) // ����.�����������޶����� { $v = $this->GetIdentifiers($v, false, '.'); $sublen = mb_strlen('.', USEDCHARSET); $nv = $this->GetIdentifiers(mb_substr($nv, $sublen, mb_strlen($nv, USEDCHARSET) - $sublen, USEDCHARSET)); } else { $v = $this->GetIdentifiers($v); $nv = $v; } // ���� if (!empty($k) && !is_numeric($k)) { $k = $this->GetIdentifiers($k); $fs[Self::FIELDNAME_FIELD] .= ', ' . $v . ' ' . $k; $fs[Self::FIELDNAME_ALIAS] .= ', ' . $k; } else { $fs[Self::FIELDNAME_FIELD] .= ', ' . $v; $fs[Self::FIELDNAME_ALIAS] .= ', ' . $nv; } } $sublen = mb_strlen(', ', USEDCHARSET); if (!empty($fs[Self::FIELDNAME_FIELD])) { $fs[Self::FIELDNAME_FIELD] = mb_substr($fs[Self::FIELDNAME_FIELD], $sublen, mb_strlen($fs[Self::FIELDNAME_FIELD], USEDCHARSET) - $sublen, USEDCHARSET); } if (!empty($fs[Self::FIELDNAME_ALIAS])) { $fs[Self::FIELDNAME_ALIAS] = mb_substr($fs[Self::FIELDNAME_ALIAS], $sublen, mb_strlen($fs[Self::FIELDNAME_ALIAS], USEDCHARSET) - $sublen, USEDCHARSET); } } else { $fs[Self::FIELDNAME_FIELD] = $fields; $fs[Self::FIELDNAME_ALIAS] = $fields; } return $fs; } /** * 2016-12-17��չ���ֶ�ֵ����ָ��raw���ԣ����rawΪtrue���Բ�����ʽ���ݡ� * @note ִ��һ��������� * @param string $tablename ���� * @param array $fields �ֶΣ��ֶ���=�ֶ�ֵ�� * @param bool $replace �Ƿ񸲸Dz��� * @param array|string $refer ����Ǹ���ʱ, �ο����ֶ� * @return bool|mixed|null */ protected function PDO_Insert($tablename, $fields, $replace = false, $refer = null) { if (null == $this->Database) { return false; } if (is_object($fields)) { $fields = (array)$fields; } $ks = ''; $kss = ''; $kst = ''; $ksv = ''; $ps = ''; $ss = ''; $vs = array(); foreach ($fields as $k => $v) { if (null === $v) { continue; } if (is_object($v)) { $v = (array)$v; } // 2016-12-17��չ���ֶ�ֵ����ָ��raw���ԣ����rawΪtrue���Բ�����ʽ���ݡ� $raw = isset($v[Self::PROPERTYNAME_RAW]) ? $v[Self::PROPERTYNAME_RAW] : false; $type = isset($v[Self::PROPERTYNAME_TYPE]) ? $v[Self::PROPERTYNAME_TYPE] : Self::TYPENAME_RAW; // ֵ if (is_array($v)) { $v = isset($v[Self::PROPERTYNAME_VALUE]) ? $v[Self::PROPERTYNAME_VALUE] : null; } /* ��ʼƴsql�������Ҫ���ֶ��������ֶ�ֵ */ // ԭʼ���ֶ��� $ks .= sprintf('%s,', $this->GetIdentifiers($k)); // ԭ����ѯ��������ֶ������ϲ�����ʱʹ�ã� $kss .= sprintf('s.%s,', $this->GetIdentifiers($k)); // Ŀ��������ֶ������ϲ�����ʱʹ�ã� $kst .= sprintf('t.%s,', $this->GetIdentifiers($k)); // ��ֵ��ϵ���ϲ�����ʱʹ�� $ksv .= sprintf('s.%s = t.%s,', $this->GetIdentifiers($k), $this->GetIdentifiers($k)); // 2016-12-17��չ���ֶ�ֵ����ָ��raw���ԣ����rawΪtrue���Բ�����ʽ���ݡ� // �������ֵ if ($raw) { switch ($type) { case Self::TYPENAME_DATE: { if (is_numeric($v)) { // �������ݵ�mask $ps .= $this->Timestamp2DatetimeFormat($v) . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $this->Timestamp2DatetimeFormat($v), $this->GetIdentifiers($k)); } else { // �������ݵ�mask $ps .= $this->String2DatetimeFormat($v) . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $this->String2DatetimeFormat($v), $this->GetIdentifiers($k)); } break; } case Self::TYPENAME_TIMESTAMP: { if (is_numeric($v)) { // �������ݵ�mask $ps .= $v . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $v, $this->GetIdentifiers($k)); } else { // �������ݵ�mask $ps .= $this->Datetime2TimestampFormat($v) . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $this->Datetime2TimestampFormat($v), $this->GetIdentifiers($k)); } break; } case Self::TYPENAME_STRING: { // �������ݵ�mask $ps .= $this->GetIdentifiers($v, true) . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $this->GetIdentifiers($v, true), $this->GetIdentifiers($k)); break; } default: { // �������ݵ�mask $ps .= $v . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $v, $this->GetIdentifiers($k)); break; } } } else { switch ($type) { case Self::TYPENAME_DATE: { if (is_numeric($v)) { // �������ݵ�mask $ps .= $this->Timestamp2DatetimeFormat('?') . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $this->Timestamp2DatetimeFormat('?'), $this->GetIdentifiers($k)); } else { // �������ݵ�mask $ps .= $this->String2DatetimeFormat('?', false) . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $this->String2DatetimeFormat('?', false), $this->GetIdentifiers($k)); } break; } case Self::TYPENAME_TIMESTAMP: { if (is_numeric($v)) { // �������ݵ�mask $ps .= '?,'; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('? %s,', $this->GetIdentifiers($k)); } else { // �������ݵ�mask $ps .= $this->Datetime2TimestampFormat('?', false) . ','; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('%s %s,', $this->Datetime2TimestampFormat('?', false), $this->GetIdentifiers($k)); } break; } default: { // �������ݵ�mask $ps .= '?,'; // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss .= sprintf('? %s,', $this->GetIdentifiers($k)); break; } } // ���ֶ�ֵ��������б��� array_push($vs, $v); } } if (0 == count($vs)) { return false; } // ԭʼ���ֶ��� $ks = mb_substr($ks, 0, mb_strlen($ks, USEDCHARSET) - 1, USEDCHARSET); // ԭ����ѯ��������ֶ������ϲ�����ʱʹ�ã� $kss = mb_substr($kss, 0, mb_strlen($kss, USEDCHARSET) - 1, USEDCHARSET); // Ŀ��������ֶ������ϲ�����ʱʹ�ã� $kst = mb_substr($kst, 0, mb_strlen($kst, USEDCHARSET) - 1, USEDCHARSET); // ��ֵ��ϵ���ϲ�����ʱʹ�� $ksv = mb_substr($ksv, 0, mb_strlen($ksv, USEDCHARSET) - 1, USEDCHARSET); // �������ݵ�mask $ps = mb_substr($ps, 0, mb_strlen($ps, USEDCHARSET) - 1, USEDCHARSET); // ��ѯ���ʱʹ�õ��ֶ��������ϲ�����ʱʹ�ã� $ss = mb_substr($ss, 0, mb_strlen($ss, USEDCHARSET) - 1, USEDCHARSET); $command = ''; $repcmd = ''; $inscmd = ''; $rs = ''; switch (DATABASE_TYPE) { case 'MYSQL': $repcmd = sprintf('replace into %s(%s) values (%s)', $this->GetIdentifiers($tablename), $ks, $ps); $inscmd = sprintf('insert into %s(%s) values (%s)', $this->GetIdentifiers($tablename), $ks, $ps); break; case 'ORACLE': if (is_array($refer) && count($refer)) { foreach ($refer as $k => $v) { $rs .= sprintf(' s.%s = t.%s and', $this->GetIdentifiers($v), $this->GetIdentifiers($v)); } $rs = mb_substr($rs, 0, mb_strlen($rs, USEDCHARSET) - 3, USEDCHARSET); } else { $rs = $refer; } if (mb_strlen($rs, USEDCHARSET)) { $command = 'merge into %s t' . PHP_EOL . 'using (select %s from dual) s' . PHP_EOL . 'on %s' . PHP_EOL . 'when matched then' . PHP_EOL . ' update set %s' . PHP_EOL . 'when not matched then' . PHP_EOL . ' insert' . PHP_EOL . ' (%s)' . PHP_EOL . ' values' . PHP_EOL . ' (%s)'; $repcmd = sprintf($command, $this->GetIdentifiers($tablename), $ss, $rs, $ksv, $kst, $kss); } else { $repcmd = ''; } $inscmd = sprintf('insert into %s(%s) values (%s)', $this->GetIdentifiers($tablename), $ks, $ps); break; case 'SQLSRV': if (is_array($refer) && count($refer)) { foreach ($refer as $k => $v) { $rs .= sprintf(' s.%s = t.%s and', $this->GetIdentifiers($v), $this->GetIdentifiers($v)); } $rs = mb_substr($rs, 0, mb_strlen($rs, USEDCHARSET) - 3, USEDCHARSET); } else { $rs = $refer; } if (mb_strlen($rs, USEDCHARSET)) { $command = 'merge into %s t' . PHP_EOL . 'using (select %s) s' . PHP_EOL . 'on %s' . PHP_EOL . 'when matched then' . PHP_EOL . ' update set %s' . PHP_EOL . 'when not matched then' . PHP_EOL . ' insert' . PHP_EOL . ' (%s)' . PHP_EOL . ' values' . PHP_EOL . ' (%s)'; $repcmd = sprintf($command, $this->GetIdentifiers($tablename), $ss, $rs, $ksv, $kst, $kss); } else { $repcmd = ''; } $inscmd = sprintf('insert into %s(%s) values (%s)', $this->GetIdentifiers($tablename), $ks, $ps); break; default: $this->ErrorCode = ERRORCODE_BADPARAMETER; $this->ErrorInfo = sprintf(ERRORINFO_BADPARAMETER, 'dbtype', DATABASE_TYPE); return false; break; } $command = $replace ? $repcmd : $inscmd; $return = $this->PDO_Execute($command, $vs); $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); return $return; } /** * 2016-12-17��չ���ֶ�ֵ����ָ��raw���ԣ����rawΪtrue���Բ�����ʽ���ݡ� * @note ִ��һ���޸Ĺ��� * @param string $tablename ���� * @param string $fields �ֶΣ��ֶ���=�ֶ�ֵ�� * @param array $ands ������and�����ֶ���=�ֶ�ֵ�� * @param array $ors ������or�����ֶ���=�ֶ�ֵ�� * @return bool|mixed|null */ protected function PDO_Update($tablename, $fields, $ands = null, $ors = null) { if (null == $this->Database) { return false; } if (is_object($fields)) { $fields = (array)$fields; } $vs = array(); if (is_array($fields)) { $fs = ''; foreach ($fields as $k => $v) { if (null === $v) { continue; } if (is_object($v)) { $v = (array)$v; } // 2016-12-17��չ���ֶ�ֵ����ָ��raw���ԣ����rawΪtrue���Բ�����ʽ���ݡ� $raw = isset($v[Self::PROPERTYNAME_RAW]) ? $v[Self::PROPERTYNAME_RAW] : false; $type = isset($v[Self::PROPERTYNAME_TYPE]) ? $v[Self::PROPERTYNAME_TYPE] : Self::TYPENAME_RAW; // ֵ if (is_array($v)) { $v = isset($v[Self::PROPERTYNAME_VALUE]) ? $v[Self::PROPERTYNAME_VALUE] : null; } if ($raw) { switch ($type) { case Self::TYPENAME_DATE: { if (is_numeric($v)) { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $this->Timestamp2DatetimeFormat($v)); } else { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $this->String2DatetimeFormat($v)); } break; } case Self::TYPENAME_TIMESTAMP: { if (is_numeric($v)) { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $v); } else { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $this->Datetime2TimestampFormat($v)); } break; } case Self::TYPENAME_STRING: { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $this->GetIdentifiers($v, true)); break; } default: { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $v); break; } } } else { switch ($type) { case Self::TYPENAME_DATE: { if (is_numeric($v)) { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $this->Timestamp2DatetimeFormat('?')); } else { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $this->String2DatetimeFormat('?', false)); } break; } case Self::TYPENAME_TIMESTAMP: { if (is_numeric($v)) { $fs .= sprintf('%s = ?,', $this->GetIdentifiers($k, false, '.')); } else { $fs .= sprintf('%s = %s,', $this->GetIdentifiers($k, false, '.'), $this->Datetime2TimestampFormat('?', false)); } break; } default: { $fs .= sprintf('%s = ?,', $this->GetIdentifiers($k, false, '.')); break; } } array_push($vs, $v); } } if (empty($fs)) { return false; } $fs = mb_substr($fs, 0, mb_strlen($fs, USEDCHARSET) - 1, USEDCHARSET); } else { $fs = $fields; } $ws = $this->GetWheres($ands, $ors); if (empty($ws[Self::CONDITION_NAMES])) { $command = sprintf('update %s set %s', $this->GetIdentifiers($tablename, false, ','), $fs); $return = $this->PDO_Execute($command, $vs); } else { $command = sprintf('update %s set %s where %s', $this->GetIdentifiers($tablename, false, ','), $fs, $ws[Self::CONDITION_NAMES]); $return = $this->PDO_Execute($command, $vs, $ws[Self::CONDITION_VALUE]); } $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); return $return; } /** * @note ִ��һ��ɾ������ * @param string $tablename ���� * @param array $ands ������and�����ֶ���=�ֶ�ֵ�� * @param array $ors ������or�����ֶ���=�ֶ�ֵ�� * @return bool|mixed|null */ protected function PDO_Delete($tablename, $ands = null, $ors = null) { if (null == $this->Database) { return false; } $ws = $this->GetWheres($ands, $ors); if (empty($ws[Self::CONDITION_NAMES])) { $command = sprintf('delete from %s', $this->GetIdentifiers($tablename)); $return = $this->PDO_Execute($command); } else { $command = sprintf('delete from %s where %s', $this->GetIdentifiers($tablename), $ws[Self::CONDITION_NAMES]); $return = $this->PDO_Execute($command, $ws[Self::CONDITION_VALUE]); } $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); return $return; } /** * 2017-03-31 ���ӷ���֧�� * @note ִ��һ����ѯ���� * @param string|array $tables ������2017-03-03��������֧�֣� * @param string|array $fields Ҫ��ѯ���ֶΣ�2016-12-08�����������֧�֣���ʽΪ������=>ԭ�ֶ�/���ʽ�� * @param array $ands ������and�����ֶ���=�ֶ�ֵ�� * @param array $ors ������or�����ֶ���=�ֶ�ֵ�� * @param array $orders �����ֶΣ��ֶ���=����˳�� * @param array $groups �����ֶΣ��ֶ����������� * @return bool|mixed|null */ protected function PDO_Select($tables, $fields, $ands = null, $ors = null, $orders = null, $groups = null) { if (null == $this->Database) { return false; } $tables = $this->GetSelectTables($tables); $fields = $this->GetSelectFields($fields)[Self::FIELDNAME_FIELD]; if (empty($tables) || empty($fields)) { return false; } $gs = $this->GetGroups($groups); if (!empty($gs)) { $gs = ' group by ' . $gs; } $os = $this->GetOrders($orders); if (!empty($os)) { $os = ' order by ' . $os; } $ws = $this->GetWheres($ands, $ors); if (empty($ws[Self::CONDITION_NAMES])) { $command = sprintf('select %s from %s%s%s', $fields, $tables, $gs, $os); $return = $this->PDO_Request($command); } else { $command = sprintf('select %s from %s where %s%s%s', $fields, $tables, $ws[Self::CONDITION_NAMES], $gs, $os); $return = $this->PDO_Request($command, $ws[Self::CONDITION_VALUE]); } //var_dump($command); $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); return $return; } /** * 2016-08-16���ӡ�2017-03-31���ӷ���֧�� * @note ִ��һ����ѯ���̣���ҳ�� * @param string|array $tables ������2017-03-03��������֧�֣� * @param string|array $fields Ҫ��ѯ���ֶΣ�2016-12-08�����������֧�֣���ʽΪ������=>ԭ�ֶ�/���ʽ�� * @param array $ands ������and�����ֶ���=�ֶ�ֵ�� * @param array $ors ������or�����ֶ���=�ֶ�ֵ�� * @param array $orders �����ֶΣ��ֶ���=����˳�� * @param array $groups �����ֶΣ��ֶ����������� * @param int $pageindex ҳ��ţ�1Ϊ��һ����ţ� * @param int $pagesize һҳ��С * @return bool|mixed|null * @note Ӧ�� */ protected function PDO_Select_Page($tables, $fields, $ands = null, $ors = null, $orders = null, $groups = null, $pageindex = 1, $pagesize = 20) { if (null == $this->Database) { return false; } // �ɴ������ if (is_numeric($groups)) { $pagesize = $pageindex; $pageindex = $groups; $groups = null; } // ��ʼ��� $start = ($pageindex - 1) * $pagesize + 1; // ��ֹ��� $stop = $pageindex * $pagesize; // ��ҳ�õ��ֶ��� $newfieldname = $this->GetIdentifiers($this->GetRandString('nf_')); // ��ҳ�õı��� $newtablename = $this->GetIdentifiers($this->GetRandString('nt_')); // Ҫ��ѯ�ı� $tables = $this->GetSelectTables($tables); // Ҫ��ѯ���ֶ� $fields = $this->GetSelectFields($fields); if (empty($tables) || empty($fields[Self::FIELDNAME_FIELD]) || empty($fields[Self::FIELDNAME_ALIAS])) { return false; } $gs = $this->GetGroups($groups); if (!empty($gs)) { $gs = ' group by ' . $gs; } $os = $this->GetOrders($orders); if (!empty($os)) { $os = ' order by ' . $os; } $ws = $this->GetWheres($ands, $ors); switch (DATABASE_TYPE) { case 'MYSQL': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . ' %s,' . PHP_EOL . ' @i := @i + 1 %s' . PHP_EOL . ' from' . PHP_EOL . ' %s,' . PHP_EOL . ' (select @i := 0) %s' . PHP_EOL . ' %s' . PHP_EOL . ' $s' . PHP_EOL . ' limit %d' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s >= %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], $fields[Self::FIELDNAME_FIELD], $newfieldname, $tables, $newtablename, $gs, $os, $stop, $newtablename, $newfieldname, $start ); //var_dump($command); $return = $this->PDO_Request($command); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . ' %s,' . PHP_EOL . ' @i := @i + 1 %s' . PHP_EOL . ' from' . PHP_EOL . ' %s,' . PHP_EOL . ' (select @i := 0) %s' . PHP_EOL . ' where' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' limit %d' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s >= %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], $fields[Self::FIELDNAME_FIELD], $newfieldname, $tables, $newtablename, $ws[Self::CONDITION_NAMES], $gs, $os, $stop, $newtablename, $newfieldname, $start ); //var_dump($command); $return = $this->PDO_Request($command, $ws[Self::CONDITION_VALUE]); } break; case 'ORACLE': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . ' %s,' . PHP_EOL . ' row_number() over(%s) %s' . PHP_EOL . ' from' . PHP_EOL . ' %s' . PHP_EOL . //' where' . PHP_EOL . //' rownum <= %d' . PHP_EOL . ' %s' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s between %d and %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], $fields[Self::FIELDNAME_FIELD], $os, $newfieldname, $tables, //$stop, $gs, $newtablename, $newfieldname, $start, $stop ); //var_dump($command); $return = $this->PDO_Request($command); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . ' %s,' . PHP_EOL . ' row_number() over(%s) %s' . PHP_EOL . ' from' . PHP_EOL . ' %s' . PHP_EOL . ' where' . PHP_EOL . //' rownum <= %d and' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s between %d and %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], $fields[Self::FIELDNAME_FIELD], $os, $newfieldname, $tables, //$stop, $ws[Self::CONDITION_NAMES], $gs, $newtablename, $newfieldname, $start, $stop ); //var_dump($command); $return = $this->PDO_Request($command, $ws[Self::CONDITION_VALUE]); } break; case 'SQLSRV': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . //' top %d' . PHP_EOL . ' %s,' . PHP_EOL . ' row_number() over(%s) %s' . PHP_EOL . ' from' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s between %d and %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], //$stop, $fields[Self::FIELDNAME_FIELD], $os, $newfieldname, $tables, $gs, $newtablename, $newfieldname, $start, $stop ); //var_dump($command); $return = $this->PDO_Request($command); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . //' top %d' . PHP_EOL . ' %s,' . PHP_EOL . ' row_number() over(%s) %s' . PHP_EOL . ' from' . PHP_EOL . ' %s' . PHP_EOL . ' where' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s between %d and %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], //$stop, $fields[Self::FIELDNAME_FIELD], $os, $newfieldname, $tables, $ws[Self::CONDITION_NAMES], $gs, $newtablename, $newfieldname, $start, $stop ); //var_dump($command); $return = $this->PDO_Request($command, $ws[Self::CONDITION_VALUE]); } break; default: $this->ErrorCode = ERRORCODE_BADPARAMETER; $this->ErrorInfo = sprintf(ERRORINFO_BADPARAMETER, 'dbtype', DATABASE_TYPE); return false; break; } $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); return $return; } /** * 2017-02-17���ӡ�2017-03-31���ӷ���֧�� * @note ��������IJ������ɶ�Ӧ��sql��䡣 * @param string $request �Ӳ�ѯ�� * @param string|array $tablename ����������֧�ֱ����� * @param bool $ispage �Ƿ���Ҫ��ҳ�� * @param string $fields Ҫ��ѯ���ֶΣ�����=>ԭ�ֶ�/���ʽ�� * @param array $ands ������and�����ֶ���=�ֶ�ֵ�� * @param array $ors ������or�����ֶ���=�ֶ�ֵ�� * @param array $orders �����ֶΣ��ֶ���=����˳�� * @param array $groups �����ֶΣ��ֶ����������� * @param int $pageindex ҳ��ţ�1Ϊ��һ����ţ� * @param int $pagesize һҳ��С * @return bool|mixed|null * @note Ӧ�� */ private function GetSelectPageCommand($request, $tablename, $ispage, $fields, $ands = null, $ors = null, $orders = null, $groups = null, $pageindex = 1, $pagesize = 20) { $return = array( Self::COMMAND_LINE => '', Self::COMMAND_VARS => array(), ); // ��ҳ�õ��ֶ��� $newfieldname = $this->GetIdentifiers($this->GetRandString('nf_')); // ��ҳ�õı��� $newtablename = $this->GetIdentifiers($this->GetRandString('nt_')); // �Ӳ�ѯ���� $newrequest = $this->GetIdentifiers($this->GetRandString('nr_')); // ���� $tablename = $this->GetSelectTables($tablename); // �ֶ��� $fields = $this->GetSelectFields($fields); if (empty($tablename) || empty($fields[Self::FIELDNAME_FIELD]) || empty($fields[Self::FIELDNAME_ALIAS])) { return $return; } $gs = $this->GetGroups($groups); if (!empty($gs)) { $gs = ' group by ' . $gs; } $os = $this->GetOrders($orders); if (!empty($os)) { $os = ' order by ' . $os; } if ($ispage) { // ��ʼ��� $start = ($pageindex - 1) * $pagesize + 1; // ��ֹ��� $stop = $pageindex * $pagesize; $ws = $this->GetWheres($ands, $ors); switch (DATABASE_TYPE) { case 'MYSQL': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . ' %s,' . PHP_EOL . ' @i := @i + 1 %s' . PHP_EOL . ' from' . PHP_EOL . '%s' . ' %s,' . PHP_EOL . ' (select @i := 0) %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' limit %d' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s >= %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], $fields[Self::FIELDNAME_FIELD], $newfieldname, empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $newtablename, $gs, $os, $stop, $newtablename, $newfieldname, $start ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = array(); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . ' %s,' . PHP_EOL . ' @i := @i + 1 %s' . PHP_EOL . ' from' . PHP_EOL . '%s' . ' %s,' . PHP_EOL . ' (select @i := 0) %s' . PHP_EOL . ' where' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' limit %d' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s >= %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], $fields[Self::FIELDNAME_FIELD], $newfieldname, empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $newtablename, $ws[Self::CONDITION_NAMES], $gs, $os, $stop, $newtablename, $newfieldname, $start ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = $ws[Self::CONDITION_VALUE]; } break; case 'ORACLE': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . ' %s,' . PHP_EOL . ' row_number() over(%s) %s' . PHP_EOL . ' from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . //' where' . PHP_EOL . //' rownum <= %d' . PHP_EOL . ' %s' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s between %d and %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], $fields[Self::FIELDNAME_FIELD], $os, $newfieldname, empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, //$stop, $gs, $newtablename, $newfieldname, $start, $stop ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = array(); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . ' %s,' . PHP_EOL . ' row_number() over(%s) %s' . PHP_EOL . ' from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . ' where' . PHP_EOL . //' rownum <= %d and' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s between %d and %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], $fields[Self::FIELDNAME_FIELD], $os, $newfieldname, empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, //$stop, $ws[Self::CONDITION_NAMES], $gs, $newtablename, $newfieldname, $start, $stop ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = $ws[Self::CONDITION_VALUE]; } break; case 'SQLSRV': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . //' top %d' . PHP_EOL . ' %s,' . PHP_EOL . ' row_number() over(%s) %s' . PHP_EOL . ' from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s between %d and %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], //$stop, $fields[Self::FIELDNAME_FIELD], $os, $newfieldname, empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $gs, $newtablename, $newfieldname, $start, $stop ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = array(); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . ' (' . PHP_EOL . ' select' . PHP_EOL . //' top %d' . PHP_EOL . ' %s,' . PHP_EOL . ' row_number() over(%s) %s' . PHP_EOL . ' from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . ' where' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' ) %s' . PHP_EOL . 'where' . PHP_EOL . ' %s between %d and %d'; $command = sprintf($command, $fields[Self::FIELDNAME_ALIAS], //$stop, $fields[Self::FIELDNAME_FIELD], $os, $newfieldname, empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $ws[Self::CONDITION_NAMES], $gs, $newtablename, $newfieldname, $start, $stop ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = $ws[Self::CONDITION_VALUE]; } break; } return $return; } else { $ws = $this->GetWheres($ands, $ors); switch (DATABASE_TYPE) { case 'MYSQL': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s'; $command = sprintf($command, $fields[Self::FIELDNAME_FIELD], empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $gs, $os ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = array(); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . 'where' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s'; $command = sprintf($command, $fields[Self::FIELDNAME_FIELD], empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $ws[Self::CONDITION_NAMES], $gs, $os ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = $ws[Self::CONDITION_VALUE]; } break; case 'ORACLE': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s'; $command = sprintf($command, $fields[Self::FIELDNAME_FIELD], empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $gs, $os ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = array(); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . 'where' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s'; $command = sprintf($command, $fields[Self::FIELDNAME_FIELD], empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $ws[Self::CONDITION_NAMES], $gs, $os ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = $ws[Self::CONDITION_VALUE]; } break; case 'SQLSRV': if (empty($ws[Self::CONDITION_NAMES])) { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s'; $command = sprintf($command, $fields[Self::FIELDNAME_FIELD], empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $gs, $os ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = array(); } else { $command = 'select' . PHP_EOL . ' %s' . PHP_EOL . 'from' . PHP_EOL . '%s' . ' %s' . PHP_EOL . 'where' . PHP_EOL . ' %s' . PHP_EOL . ' %s' . PHP_EOL . ' %s'; $command = sprintf($command, $fields[Self::FIELDNAME_FIELD], empty($request) ? '' : ' (' . $request . ') ' . $newrequest . ',' . PHP_EOL, $tablename, $ws[Self::CONDITION_NAMES], $gs, $os ); $return[Self::COMMAND_LINE] = $command; $return[Self::COMMAND_VARS] = $ws[Self::CONDITION_VALUE]; } break; } return $return; } } /** * 2017-02-17���� * @note ִ��һ����ѯ���̣���ҳ�� * ���ע�������ʱ��ʹ�������ӷ�ʽ��ѯ���ݡ�oracle��sqlserver���Ҫʹ���������ӣ���Ҫ�ֶ���ǹ����ֶε����ӷ�ʽ�� * ���� * oracle�У� ��1.�ֶ�1 = ��2.�ֶ�2(+) �ȼ� ��1 left join ��2 on ��1.�ֶ�1 = ��2.�ֶ�2 * sqlserver�У� ��1.�ֶ�1 *= ��2.�ֶ�2 �ȼ� ��1 left join ��2 on ��1.�ֶ�1 = ��2.�ֶ�2 * @param array $tables �������б� * @param string $pagetable Ҫ��ҳ�ı�����Ϊ�����ʾ���б�ͬ����ҳ�� * @param string $fields Ҫ��ѯ���ֶΣ�����=>ԭ�ֶ�/���ʽ�� * @param array $ands ������and�����ֶ���=�ֶ�ֵ�� * @param array $ors ������or�����ֶ���=�ֶ�ֵ�� * @param array $orders �����ֶΣ��ֶ���=����˳�� * @param array $groups �����ֶΣ��ֶ����������� * @param int $pageindex ҳ��ţ�1Ϊ��һ����ţ� * @param int $pagesize һҳ��С * @return bool|mixed|null * @note Ӧ�� */ protected function PDO_Select_Page_2($tables, $pagetable, $fields, $ands = null, $ors = null, $orders = null, $groups = null, $pageindex = 1, $pagesize = 20) { if (null == $this->Database) { return false; } // �ɴ������ if (is_numeric($groups)) { $pagesize = $pageindex; $pageindex = $groups; $groups = null; } $fields = array_values((array)$fields); $ands = array_values((array)$ands); $ors = array_values((array)$ors); $commandline = ''; $commandvars = array(); $commandfield = array(); $index = 0; foreach ($tables as $tablealias => $tablename) { $t = $commandfield; $commandfield = array(); foreach ($t as $k => $v) { array_push($commandfield, (is_numeric($k) || empty($k) ? $v : $k)); } $fields[$index] = isset($fields[$index]) ? (array)$fields[$index] : array(); if (!empty($fields[$index])) { foreach ($fields[$index] as $k => $v) { if (is_numeric($k) || empty($k)) array_push($commandfield, $v); else $commandfield[$k] = $v; } } $command = $this->GetSelectPageCommand($commandline, array($tablealias => $tablename), strcasecmp($tablename, $pagetable) == 0, $commandfield, isset($ands[$index]) ? $ands[$index] : null, isset($ors[$index]) ? $ors[$index] : null, empty($orders) ? null : $orders[$index], empty($groups) ? null : $groups[$index], $pageindex, $pagesize ); $commandline = $command[Self::COMMAND_LINE]; $commandvars += $command[Self::COMMAND_VARS]; $index++; } //OutputDebugMessage($commandline); //var_dump($commandline); //var_dump($commandvars); $return = $this->PDO_Request($commandline, $commandvars); $this->ErrorCode = $this->Database->GetErrorCode(); $this->ErrorInfo = $this->Database->GetErrorInfo(); return $return; } /** * 2016-12-08���� * @note �ַ���ת��Ϊ�����͵ı��ʽ * @param string $st Ҫת�����ֶλ���ʽ * @param bool $quoted �Ƿ���Ҫ�������ӣ����� * @return string * @note Ӧ�� */ protected function String2DatetimeFormat($st, $quoted = true) { if ($quoted) { $st = $this->GetIdentifiers($st, true); } switch (DATABASE_TYPE) { case 'MYSQL': return "(STR_TO_DATE({$st}, '%Y-%m-%d %H:%i:%s'))"; break; case 'ORACLE': return "(to_date({$st}, 'yyyy-mm-dd hh24:mi:ss'))"; break; case 'SQLSRV': return "(convert(datetime, {$st}, 120))"; break; default: return ""; break; } } /** * 2016-12-08���� * @note ����ת��Ϊ�ַ����͵ı��ʽ * @param string $dt Ҫת�����ֶλ���ʽ * @param bool $quoted �Ƿ���Ҫ�������ӣ����� * @return string * @note Ӧ�� */ protected function Datetime2StringFormat($dt, $quoted = true) { if ($quoted) { $dt = $this->GetIdentifiers($dt, true); } switch (DATABASE_TYPE) { case 'MYSQL': return "(DATE_FORMAT({$dt}, '%Y-%m-%d %H:%i:%s'))"; break; case 'ORACLE': return "(to_char({$dt}, 'yyyy-mm-dd hh24:mi:ss'))"; break; case 'SQLSRV': return "(convert(varchar, {$dt}, 120))"; break; default: return ""; break; } } /** * 2016-12-08���� * @note ʱ���ת��Ϊ�����͵ı��ʽ * @param string $ts Ҫת�����ֶλ���ʽ * @return string * @note Ӧ�� */ protected function Timestamp2DatetimeFormat($ts) { switch (DATABASE_TYPE) { case 'MYSQL': return "(FROM_UNIXTIME({$ts}))"; break; case 'ORACLE': return "(to_date('19700101', 'yyyymmdd') + {$ts} / 86400 + to_number(substr(tz_offset(sessiontimezone), 1, 3)) / 24)"; break; case 'SQLSRV': return "(dateadd(S, {$ts} + 8 * 3600, '1970-01-01 00:00:00'))"; break; default: return ""; break; } } /** * 2016-12-08���� * @note ����ת��Ϊʱ����͵ı��ʽ * @param string $ds Ҫת�����ֶλ���ʽ * @param bool $quoted �Ƿ���Ҫ�������ӣ����� * @return string * @note Ӧ�� */ protected function Datetime2TimestampFormat($ds, $quoted = true) { if ($quoted) { $ds = $this->GetIdentifiers($ds, true); } switch (DATABASE_TYPE) { case 'MYSQL': return "(UNIX_TIMESTAMP({$ds}))"; break; case 'ORACLE': return "((to_date({$ds}, 'yyyy-mm-dd hh24:mi:ss') - to_date('19700101', 'yyyymmdd')) * 86400 - to_number(substr(tz_offset(sessiontimezone), 1, 3)) * 3600)"; break; case 'SQLSRV': return "(datediff(S, '1970-01-01 00:00:00', {$ds}) - 8 * 3600)"; break; default: return ""; break; } } /** * 2016-12-08���� * @note ����ת��Ϊ�ַ����͵ı��ʽ * @param string $f Ҫת�����ֶλ���ʽ * @return string * @note Ӧ�� */ protected function Float2StringFormat($f) { switch (DATABASE_TYPE) { case 'MYSQL': return "(cast(convert({$f}, decimal(18, 2)) as char))"; break; case 'ORACLE': return "(to_char({$f}, 'fm99999990.00'))"; break; case 'SQLSRV': return "(cast(convert(decimal(18, 2), {$f}) as char))"; break; default: return ""; break; } } /** * 2016-01-16���� * @note ��ȡ��ǰ������ϵͳʱ��(����ʽ) * @param bool $raw �Ƿ񷵻����ݿ���ʽ * @return string * @note Ӧ�� */ public function GetSystemTime($raw = false) { if ($raw) { switch (DATABASE_TYPE) { case 'MYSQL': return 'now()'; break; case 'ORACLE': return 'sysdate'; break; case 'SQLSRV': return 'getdate()'; break; default: return ''; break; } } else return date('Y-m-d H:i:s'); } /** * @note ��ȡ����ֵ * @param bool $always_local * @return int */ public function GetErrorCode($always_local = false) { if ($always_local || null == $this->Database) { return $this->ErrorCode; } else { return $this->Database->GetErrorCode(); } } /** * @note ��ȡ������Ϣ * @param bool $always_local * @return mixed|string */ public function GetErrorInfo($always_local = false) { if ($always_local || null == $this->Database) { return $this->ErrorInfo; } else { return $this->Database->GetErrorInfo(); } } /** * @note �û���Ȩ��Ϣת�û��루�û�id�� * @param string $user_auth_token * @return string */ protected function UserAuthTokenToUserCode($user_auth_token) { return $user_auth_token; } /** * @note ����һ���µ�id * @param string $prefix * @return string */ protected 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 string $tablename * @param string|array $wheres * @return bool * @auther Ӧ�� */ // DataExists('ims_bigmarket_market', array('app_id' => 123, 'market_id' => 123)); protected function DataExists($tablename, $wheres) { $result = $this->PDO_Select($tablename, 'count(1) c', $wheres); if (!$result) { return false; } return 0 != $result[0]['c']; } }