123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 |
- <?php
- namespace Qii\Driver;
- class Base
- {
- const VERSION = '1.2';
- public $_cache;
- public $language;
- protected $_query = array(
- "INSERT" => "INSERT INTO %s(%s) VALUES('%s')",
- "REPLACE" => "REPLACE %s (%s) VALUES('%s')",
- "SELECT" => "SELECT %s FROM %s",
- "UPDATE" => "UPDATE %s SET ",
- "DELETE" => "DELETE FROM %s %s",
- "WHERE" => " WHERE %s",
- "ORDER" => " ORDER BY %s %s",
- "GROUP" => " GROUP BY %s",
- "LIMIT" => " LIMIT %d, %d"
- );
- private $query;
- private $setArray = array();
- public $modelSQL = "";
- protected $fields;
- protected $where;
- protected $groupBy;
- protected $limit;
- protected $orderBy;
- public $load;
- /**
- * @var string $_response Response对象
- */
- protected $_response;
- //方法对应的别名
- protected $_modelAlias = array('selectRows' => 'selectAll', 'select' => 'selectRow', 'getOne' => 'selectOne', 'getRow' => 'selectRow', 'getAll' => 'selectAll', 'remove' => 'deleteRows');
- public function __construct()
- {
- $this->language = \Qii\Autoloader\Psr4::getInstance()->loadClass('Qii\Language\Loader');
- $this->load = \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
- $this->_response = new \Qii\Driver\Response();
- }
- final public function getAlias($alias)
- {
- return isset($this->_modelAlias[$alias]) ? $this->_modelAlias[$alias] : null;
- }
- /**
- * 设置Cache
- *
- * @param String $cache
- * @param Array $policy
- */
- final public function setCache($cache, $policy)
- {
- \Qii\Autoloader\Import::requires(Qii_DIR . DS . 'Qii' . DS . 'Cache.php');
- $this->_cache = \Qii\Autoloader\Psr4::loadClass('\Qii\Cache', $cache)->initialization($policy);//载入cache类文件
- }
- /**
- * 缓存内容
- *
- * @param String $id
- * @param Array $value
- * @return Bool
- */
- final public function cache($id, $value)
- {
- return $this->_cache->set($id, $value);
- }
- /**
- * 获取缓存的类
- */
- final public function getCache()
- {
- return $this->_cache;
- }
- /**
- * 获取缓存内容
- *
- * @param String $id
- * @return Array
- */
- final public function getCacheData($id)
- {
- return $this->_cache->get($id);
- }
- /**
- * 获取表的名称
- * @param String $table
- * @return String
- */
- public function getTable($table)
- {
- return $table;
- }
- public function setLanguage()
- {
- $this->language = \Qii\Autoloader\Psr4::loadClass('Qii_Language_Loader');
- }
- /**
- *
- * Insert Object
- * @param String $table
- * @param Array|Object $dataArray
- */
- final function insertObject($table, $dataArray)
- {
- if (empty($table)) {
- return -1;
- }
- if (sizeof($dataArray) > 0 || get_object_vars($dataArray) > 0) {
- $keys = array();
- $values = array();
- foreach ($dataArray AS $key => $value) {
- $keys[] = $key;
- if(is_array($value))
- {
- throw new \Qii\Exceptions\InvalidFormat(_i('Invalid %s format', $key), __LINE__);
- }
- $values[] = $this->setQuote($value);
- }
- $this->modelSQL = $sql = "INSERT INTO `{$table}`(`" . join("`, `", $keys) . "`) VALUES('" . join("', '", $values) . "')";
- $this->setQuery($sql);
- $this->cleanData();
- $this->setError();
- return $this->lastInsertId();
- }
- return -2;
- }
- /**
- *
- * Replace Object
- * @param String $table
- * @param Array|Object $dataArray
- */
- final function replaceObject($table, $dataArray)
- {
- if (empty($table)) {
- return -1;
- }
- if (sizeof($dataArray) > 0 || get_object_vars($dataArray) > 0) {
- $keys = array();
- $values = array();
- foreach ($dataArray AS $key => $value) {
- $keys[] = $key;
- if(is_array($value))
- {
- throw new \Qii\Exceptions\InvalidFormat(_i('Invalid %s format', $key), __LINE__);
- }
- $values[] = $this->setQuote($value);
- }
- $this->modelSQL = $sql = "REPLACE INTO `{$table}`(`" . join("`, `", $keys) . "`) VALUES('" . join("', '", $values) . "')";
- $rs = $this->setQuery($sql);
- $this->cleanData();
- $this->setError();
- return $this->AffectedRows($rs);
- }
- return -2;
- }
- /**
- *
- * Update data
- * @param String $table
- * @param Array|Objct $dataArray
- * @param Array $keys
- */
- final function updateObject($table, $dataArray, $keys = array())
- {
- if (empty($table) || !is_array($keys)) {
- return -1;
- }
- if (sizeof($dataArray) > 0 || get_object_vars($dataArray) > 0) {
- $values = array();
- $where = array();
- foreach ($dataArray AS $key => $value) {
- if(is_array($value))
- {
- throw new \Qii\Exceptions\InvalidFormat(_i('Invalid %s format', $key), __LINE__);
- }
- $value = $this->setQuote($value);
- if (in_array($key, $keys)) {
- $where[] = "`{$key}` = '" . $value . "'";
- } else {
- $values[] = "`{$key}` = '" . $value . "'";
- }
- }
- //$keys为key => value的方式,就直接用keys
- if (empty($where) && count($keys) > 0) {
- foreach ($keys as $key => $value) {
- $value = $this->setQuote($value);
- $where[] = "`{$key}` = '" . $value . "'";
- }
- }
- $this->modelSQL = $sql = "UPDATE `{$table}` SET " . join(", ", $values) . (sizeof($where) > 0 ? " WHERE " . join(" AND ", $where) : '');
- $rs = $this->setQuery($sql);
- $this->cleanData();
- $this->setError();
- return $this->AffectedRows($rs);
- }
- return 0;
- }
- /**
- *
- * 删除数据
- * @param String $table
- * @param Array $keys
- */
- final function deleteObject($table, $keys = array())
- {
- if (empty($table)) {
- return -1;
- }
- $where = array();
- if (sizeof($keys) > 0 || get_object_vars($keys)) {
- foreach ($keys AS $k => $v) {
- $where[] = "`{$k}` = '" . $this->setQuote($v) . "'";
- }
- }
- $this->modelSQL = $sql = "DELETE FROM `{$table}`" . (sizeof($where) > 0 ? " WHERE " . join(" AND ", $where) : '');
- $rs = $this->query($sql);
- $this->cleanData();
- $this->setError();
- return $this->AffectedRows($rs);
- }
- /**
- * 需要清除的数据
- *
- * @return Array
- */
- final function cleanOptions()
- {
- return array('fields', 'where', 'groupBy', 'orderBy', 'limit', 'setArray');
- }
- /**
- * 清除数据
- *
- */
- final function cleanData()
- {
- $array = $this->cleanOptions();
- foreach ($array AS $k) {
- if (is_array($this->$k)) {
- $this->$k = array();
- } else {
- $this->$k = null;
- }
- }
- }
- /**
- *
- * 查询的字段
- * @param String $fileds
- */
- final function fields($fileds = "*")
- {
- $this->fields = null;
- if (empty($fileds)) $fileds = "*";
- if (is_array($fileds)) $fileds = join(',', $fileds);
- $this->fields = $fileds;
- return $this;
- }
- /**
- *
- * GROUP BY方法
- * @param String $fields
- */
- final function groupBy($fields)
- {
- $this->groupBy = null;
- if (!empty($fields)) {
- $this->groupBy = sprintf($this->_query['GROUP'], $fields);
- }
- return $this;
- }
- /**
- *
- * 插入数据用
- * @param Array $array
- */
- final function dataArray($array)
- {
- $this->fileds = null;
- if (is_array($array)) {
- $tmpArray = array();
- foreach ($array AS $k => $v) {
- $tmpArray['k'][] = $k;
- $tmpArray['v'][] = $this->setQuote($v);
- }
- $this->fileds = $tmpArray;
- }
- return $this;
- }
- /**
- *
- * Order By函数
- * @param String $field
- * @param String $orderBy
- */
- final function orderBy($field, $orderBy)
- {
- $this->orderBy = null;
- if (!empty($field)) {
- $this->orderBy = sprintf($this->_query['ORDER'], $field, $orderBy);
- }
- return $this;
- }
- /**
- *
- * Limit函数,如果省略第二个参数则第一个为0,第二个参数值取第一个
- * @param Int $limit
- * @param Int $offset
- */
- final function limit($limit, $offset = 0)
- {
- $this->limit = null;
- if ($limit !== '') {
- if (!$offset) {
- $this->limit = sprintf($this->_query["LIMIT"], 0, $limit);
- } else {
- $this->limit = sprintf($this->_query["LIMIT"], $limit, $offset);
- }
- }
- return $this;
- }
- /**
- * 传的条件为数组
- * @param Array $where 条件
- * @return Object 对象本身
- */
- final function whereArray($where)
- {
- $this->where = null;
- if (!empty($where)) {
- $whereArray = array();
- foreach ($where AS $k => $v) {
- $whereArray[] = " `{$k}` = '{$v}'";
- }
- if (sizeof($whereArray) > 0) {
- $whereSQL = join(" AND ", $whereArray);
- $this->where = sprintf($this->_query["WHERE"], $whereSQL);
- }
- }
- return $this;
- }
- /**
- *
- * WHERE 子句
- * @param String $where
- */
- final function where($where)
- {
- if (is_array($where)) return $this->whereArray($where);
- $this->where = null;
- if (!empty($where)) {
- $this->where = sprintf($this->_query["WHERE"], $where);
- }
- return $this;
- }
- /**
- *
- * 插入数据到指定表中
- * @param String $table
- */
- final function insertRow($table)
- {
- $this->modelSQL = $sql = sprintf($this->_query['INSERT'], $table, join(",", $this->fileds['k']), join("', '", $this->fileds['v']));
- $this->cleanData();
- $this->setQuery($sql, '');
- return $this->lastInsertId();
- }
- /**
- *
- * Replace方法
- * @param String $table
- */
- final function replaceRow($table)
- {
- $this->modelSQL = $sql = sprintf($this->_query['REPLACE'], $table, join(",", $this->fileds['k']), join("', '", $this->fileds['v']));
- $this->cleanData();
- return $this->exec($sql);
- }
- /**
- *
- * 查询一行
- * @param String $table
- */
- final function selectRow($table)
- {
- $this->modelSQL = $sql = sprintf($this->_query['SELECT'], ((trim($this->fields) != '') ? $this->fields : "*"), $table) . $this->where . $this->groupBy . $this->orderBy . $this->limit;
- $this->cleanData();
- return $this->getRow($sql);
- }
- /**
- *
- * 查询一行
- * @param String $table
- */
- final function selectOne($table)
- {
- $this->modelSQL = $sql = sprintf($this->_query['SELECT'], ((trim($this->fields) != '') ? $this->fields : "*"), $table) . $this->where . $this->groupBy . $this->orderBy . $this->limit;
- $this->cleanData();
- return $this->getOne($sql);
- }
- /**
- *
- * 查询所有
- * @param String $table
- * @return Array
- */
- final function selectAll($table)
- {
- $this->modelSQL = $sql = sprintf($this->_query['SELECT'], ((trim($this->fields) != '') ? $this->fields : "*"), $table) . $this->where . $this->groupBy . $this->orderBy . $this->limit;
- $this->cleanData();
- return $this->getAll($sql);
- }
- /**
- * 将指定字段减指定值
- *
- * @param $data
- * @return $this
- */
- final function downsetCounter($data)
- {
- if (is_array($data)) {
- foreach ($data AS $k => $value) {
- $this->setArray[] = $k . "=" . $k . '-' . $value;
- }
- }
- $this->set(null);
- return $this;
- }
- /**
- * 将指定字段加指定值
- *
- * @param $data
- * @return $this
- */
- final function upsetCounter($data)
- {
- if (is_array($data)) {
- foreach ($data AS $k => $value) {
- $this->setArray[] = $k . "=" . $k . '+' . $value;
- }
- }
- $this->set(null);
- return $this;
- }
- /**
- * 更新数据时候用,方法同setData
- * @param Array $data
- */
- final function set($data)
- {
- return $this->setData($data);
- }
- /**
- *
- * 更新数据时候用
- * @param Array $data
- * @return $this
- */
- final function setData($data)
- {
- if (is_array($data)) {
- $set = array();
- foreach ($data AS $k => $value) {
- $set[] = $k . "='" . $this->setQuote($value) . "'";
- }
- if (sizeof($this->setArray) > 0) {
- $this->set = " " . join(", ", $set) . ", " . join(",", $this->setArray);
- } else {
- $this->set = " " . join(", ", $set);
- }
- } else {
- if (sizeof($this->setArray) > 0) {
- $this->set = join(",", $this->setArray);
- } else {
- $this->set = "";
- }
- }
- return $this;
- }
- /**
- * 执行更新操作,updateRows的alias
- * @param String $table
- * @return number
- */
- /*
- final function update($table){
- return $this->updateRows($table);
- }
- */
- /**
- *
- * 执行更新操作
- * @param $table
- * @return Int 返回影响的行数
- */
- final function updateRows($table)
- {
- $this->modelSQL = $sql = sprintf($this->_query['UPDATE'], $table) . $this->set . $this->where . $this->limit;
- $this->cleanData();
- return $this->exec($sql);
- }
- /**
- *
- * 执行删除操作
- * @param String $table
- */
- final function deleteRows($table)
- {
- $this->modelSQL = $sql = sprintf($this->_query['DELETE'], $table, $this->where) . $this->limit;
- $this->cleanData();
- return $this->exec($sql);
- }
- /**
- * 执行Model过程中保存的相关信息
- *
- * @param String $option
- * @return Mix
- */
- final function querySQL($option = '')
- {
- $allow = array('_queryTimes', '_querySeconds', '_errorInfo', '_exeSQL');
- if (in_array($option, $allow)) {
- return $this->{$option};
- }
- return 0;
- }
- /**
- * 将结果编码一下
- * @param String $word
- * @return String|multitype:
- */
- public function setQuote($word)//过滤sql字符
- {
- if (ini_get("magic_quotes_gpc")) {
- return $word;
- }
- return is_array($word) ? array_map('addslashes', $word) : addslashes($word);
- }
- /**
- * 获取错误码
- */
- public function getCode()
- {
- return $this->_response->getCode();
- }
- /**
- * 获取错误信息
- */
- public function getMessage()
- {
- if($this->_response->isError())
- {
- return $this->_response->getMessage();
- }
- }
- /**
- * 返回response对象
- *
- * @return Bool
- */
- public function getResponse()
- {
- return $this->_response;
- }
- public function iconv($str)
- {
- if (is_array($str)) {
- return array_map(function ($n) {
- return iconv('GB2312', 'UTF-8', $n);
- }, $str);
- }
- return iconv('GB2312', 'UTF-8', $str);
- }
- /**
- * 如果不存在指定的方法则调用提示错误
- *
- * @param String $name
- * @param Mix $args
- * @return Mix
- */
- public function __call($method, $argvs)
- {
- if (isset($this->_modelAlias[$method])) {
- if (method_exists($this, $this->_modelAlias[$method])) {
- return call_user_func_array(array($this, $this->_modelAlias[$method]), $argvs);
- }
- \Qii::setError(false, __LINE__, 1506, 'Alias ' . get_called_class() . '->' . $method . '()');
- }
- \Qii::setError(false, __LINE__, 1506, get_called_class() . '->' . $method . '()');
- }
- }
|