Abstract.php 15.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
<?php

/**
 * Created by PhpStorm.
 * User: liuziyang
 * Date: 13-12-18
 * Time: 14:53
 */
abstract class Q_Db_Adapter_Pdo_Abstract
{

    /**
     * 数据库对象
     * @var PDO
     */
    protected $_connection;

    /**
     * 常量强制列名为指定的大小写
     * @var int
     */
    protected $_caseFolding = 0;

    /**
     * 数据库驱动参数
     * @var array
     */
    protected $driverConfig = array(
        //PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8',
        PDO::ATTR_PERSISTENT => false,
        PDO::ATTR_TIMEOUT => 30
    );

    /**
     * 字符集
     *
     * @var string
     */
    protected $charset = 'utf8';

    /**
     * fetch 参数
     * @var array
     */
    protected $fetchOptions = array(
        'column_number' => 0,
        'fetch_style' => null,
        'fetch_argument' => null,
        'ctor_args' => array(),
        'class_name' => 'stdClass'
    );

    /**
     * @var Q_Db_Adapter_Pdo_ElementParser
     */
    protected $elementParser;

    /**
     * 数据库名
     * @var String
     */
    protected $dbname;

    /**
     * 服务器地址列表
     * @var array
     */
    protected $serverConfig;

    /**
     * DB 结果缓存
     * @var bool
     */
    protected $cacheStatus = true;

    /**
     * DB 缓存时间
     * @var int
     */
    protected $cacheExpire = 60;

    /**
     * 缓存KEY
     * @var null
     */
    protected $cacheKey = null;

    /**
     * 缓存TAG
     * @var null
     */
    protected $cacheTag = null;

    /**
     * 缓存对象
     * @var Q_Cache_Memcached
     */
    protected $cacheObj;

    /**
     * @var null
     */
    protected $prepareDriverOptions = null;

    /**
     * 对应SqlMap根节点nameSpace 也就是数据库名
     *
     * 主要是为了多库下同样的表结构 使用指定的sqlmap
     *
     * @var String
     */
    protected $nameSpace;

    /**
     * key前缀使用在刷新范围内的Cache
     *
     * 如:
     * $prefix = 'list_';
     * 范围:1,10 or a,z
     * 结果就是删除 'list_1'、'list_2'、'list_3' ... 'list_10'
     * @var String
     */
    protected $cacheKeyPrefix = null;

    /**
     * 缓存节点
     * @var string
     */
    protected $cacheNodeName = 'servers';

    /**
     * 缓存池ID
     * @var string
     */
    protected $cachePersistentID = '';

    /**
     * 实例缓存
     * @return Q_Cache_Memcached
     */
    public function dbCache()
    {
        if ($this->cacheObj) {
            return $this->cacheObj;
        }
        $sqlCacheClass = $this->dbname ? $this->dbname : Q_Core_Consts::DB_SQL_CACHE_CLASS;
        $options = array(
            'persistent_id' => $this->cachePersistentID
        );
        $this->cacheObj = Q_Cache::factory('Memcached', $options, $this->cacheNodeName)->setPrefix(Q_Core_Consts::DB_SQL_CACHE_DOMAIN . '_' . $sqlCacheClass);
        return $this->cacheObj;
    }

    /**
     * 获取Db Server 服务器 Map
     *
     */
    public function getServerInfo()
    {
        return $this->serverConfig;
    }

    /**
     * 获取key
     * @return null
     */
    public function getKey()
    {
        return $this->cacheKey;
    }

    /**
     * 缓存
     * @param bool $status
     * @param String $key
     * @param integer $expire
     * @return  Q_Db_Adapter_Pdo_SqlMap
     */
    public function cache($status = true)
    {
        if (is_bool($status) === false) {
            throw new Q_Db_Exception(' Db Cache status is error ');
        }
        $this->cacheStatus = $status;
        return $this;
    }

    /**
     * 设置缓存时间
     *
     * @param integer $expire 秒
     * @return  Q_Db_Adapter_Pdo_SqlMap
     */
    public function expire($expire)
    {
        $expire = intval($expire);
        if ($expire > 0) {
            $this->cacheExpire = $expire;
        }
        return $this;
    }

    /**
     * 设置cache key
     *
     * @param String $key
     * @return  Q_Db_Adapter_Pdo_SqlMap
     */
    public function key($key = null, $prefix = null)
    {
        if (!empty($key)) {
            $this->cacheKey = $key;
        }
        $this->cacheKeyPrefix = trim($prefix);
        return $this;
    }

    /**
     * 组合key
     * @param string $sqlId
     * @param array $parameterMap
     * @param array $replaceMap
     * @param $act
     */
    protected function _makeKey($sqlId, $parameterMap, $replaceMap)
    {
        $this->cacheKey = md5($this->getSql($sqlId, $parameterMap, $replaceMap));
    }


    /**
     * @param $sqlId
     * @param $parameterMap
     * @param $replaceMap
     * @param $act
     * @return array|Mixed
     */
    protected function _prepare($sqlId, $parameterMap, $replaceMap, $act)
    {
        if ($this->cacheStatus === true) {
            $this->dbCache();
            if (empty($this->cacheKey)) {
                $this->_makeKey($sqlId, $parameterMap, $replaceMap);
            }
            /** 判定是否获取Tag **/
            if (!empty($this->cacheTag)) {
                $cacheVal = $this->cacheObj->tag($this->cacheTag)->get($this->cacheKey);
            } else {
                $cacheVal = $this->cacheObj->get($this->cacheKey);
            }
            if (!empty($cacheVal)) {
                $this->_resetParameter();
                return $cacheVal;
            }
        }
        $this->checkElement($sqlId);
        $this->elementParser->setReplaceMap($replaceMap);
        $sql = $this->elementParser->sql($this->nameSpace . '.' . $sqlId);
        // db start
        $statement = $this->_connect()->prepare($sql);
        $_statement = new Q_Db_Adapter_Pdo_Statement($statement);
        $_statement->bindParams($parameterMap);
        $statement->execute();
        switch ($act) {
            case 'fetchRow':
                $result = $_statement->fetchRow();
                break;
            case 'fetchOne':
                $result = $_statement->fetchOne($this->fetchOptions['column_number']);
                break;
            case 'fetchAll':
                $result = $_statement->fetchAll($this->fetchOptions['fetch_style'], $this->fetchOptions['fetch_argument'], $this->fetchOptions['ctor_args']);
                break;
            case 'fetchCol':
                $result = $_statement->fetchCol();
                break;
            case 'fetchPairs':
                $result = $_statement->fetchPairs();
                break;
            case 'fetchAllObject':
                $result = $_statement->fetchAllObject();
                break;
            case 'fetchObject':
                $result = $_statement->fetchObject($this->fetchOptions['class_name'], $this->fetchOptions['ctor_args']);
                break;
            case 'fetchAssoc':
                $result = $_statement->fetchAssoc();
                break;
        }
        if ($this->cacheStatus === true) {
            if (!empty($this->cacheTag)) {
                $this->cacheObj->tag($this->cacheTag)->set($this->cacheKey, $result, $this->cacheExpire);
            } else {
                $this->cacheObj->set($this->cacheKey, $result, $this->cacheExpire);
            }
        }
        $this->_resetParameter();
        $statement->closeCursor();
        return $result;
    }

    /**
     * @param $sqlId
     * @param array $parameterMap
     * @param null $replaceMap
     * @return Q_Db_Adapter_Pdo_Finale
     */
    protected function _execute($sqlId, $parameterMap = array(), $replaceMap = null)
    {
        $this->checkElement($sqlId);
        $this->elementParser->setReplaceMap($replaceMap);
        $sql = $this->elementParser->sql($this->nameSpace . '.' . $sqlId);
        // db start
        $pdo = $this->_connect(false);
        $statement = $pdo->prepare($sql);
        $_statement = new Q_Db_Adapter_Pdo_Statement($statement);
        $_statement->bindParams($parameterMap);
        $statement->execute();
        $this->delCache();
        return new Q_Db_Adapter_Pdo_Finale($pdo, $statement);
    }

    protected function _dsn($readtag = true)
    {
        $options = array(
            'mode' => $readtag === true ? Q_Server_Core::SERVER_MODE_READ : Q_Server_Core::SERVER_MODE_WRITE,
            'select' => Q_Server_Core::SERVER_SELECT_MODE_RAND
        );
        $servers = Q_Server::factory('db', 'mysql', strtolower($this->dbname), $options);
        if (empty($servers)) {
            throw new Q_Db_Exception('server config is null ,find: ' . $this->dbname . ' No such config file ');
        }
        $driver_options = array(
            'dbname' => isset($servers['dbname']) ? $servers['dbname'] : $this->dbname,
            'driver_options' => $this->driverConfig
        );
        $this->driverConfig = array_merge($servers, $driver_options);
        // use all remaining parts in the DSN
        $dsn = array(
            'host' => 'host=' . $this->driverConfig['host'],
            'dbname' => 'dbname=' . $this->driverConfig['dbname'],
            'port' => 'port=' . $this->driverConfig['port'],
            'charset' => 'charset=' . $this->charset
        );
        return $this->_pdoType . ':' . implode(';', $dsn);
    }

    /**
     * 创建PDO数据库对象
     * @throws Q_Db_Exception
     * @return PDO
     */
    protected function _connect($actMode = true)
    {
        $rwtag = $actMode === true ? 'r' : 'w';
        $_connectionKey = $this->dbname . '_' . $rwtag;
        if (isset($this->_connection[$_connectionKey])) {
            return $this->_connection[$_connectionKey];
        }
        $dsn = $this->_dsn($actMode);
        // 检查pdo模块
        if (!extension_loaded('pdo')) {
            throw new Q_Db_Exception('The PDO extension is required for this adapter but the extension is not loaded');
        }
        // 检查PDO中的驱动
        if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
            throw new Q_Db_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
        }
        // 添加一个请求一个持久连接,而非创建一个新连接
        if (isset($this->driverConfig['persistent']) && ($this->driverConfig['persistent'] == true)) {
            $this->driverConfig['driver_options'][PDO::ATTR_PERSISTENT] = true;
        }
        try {
            $_connection = new PDO(
                $dsn,
                $this->driverConfig['username'],
                $this->driverConfig['password'],
                $this->driverConfig['driver_options']
            );
            // set the PDO connection to perform case-folding on array keys, or not
            $_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
            // always use exceptions.
           $_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            // 判断环境设置是否开启预处理
            if (version_compare(phpversion(), '5.3.6', '<') == true) {
                $_connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            }
           
        } catch (PDOException $e) {
            throw new Q_Db_Exception($e->getMessage(), $e->getCode(), $e);
        }
        $this->_connection[$_connectionKey] = $_connection;
        return $_connection;
    }

    /**
     * Force the connection to close.
     *
     * @return void
     */
    public function closeConnection()
    {
        $this->_connection = array();
    }

    /**
     * 检查节点是否存在
     *
     * @param String $sqlId
     * @return bool
     */
    protected function checkElement($sqlId)
    {
        $element = $this->elementParser->element($this->nameSpace . '.' . $sqlId);
        if ($element == NULL) {
            throw new Q_Db_Exception($sqlId . ' element not find');
        }
        return true;
    }

    /**
     * 获取当前执行sql
     * @param string $sqlId sql定义ID
     * @param array $parameterMap 参数集
     * @param array $replaceMap 替换列表
     * @return String
     */
    public function getSql($sqlId, $parameterMap = array(), $replaceMap = array())
    {
        $this->checkElement($sqlId);
        $this->elementParser->setReplaceMap($replaceMap);
        $sql = $this->elementParser->sql($this->nameSpace . '.' . $sqlId);
        if (strstr($sql, ':')) {
            $matches_s = array();
            foreach ($parameterMap as $key => $val) {
                if (is_string($val)) {
                    $val = "'{$val}'";
                }
                $matches_s[':' . $key] = $val;
            }
            $asSql = strtr($sql, $matches_s);
        } else {
            $asSql = $sql;
            foreach ($parameterMap as $val) {
                $strPos = strpos($asSql, '?');
                if (is_string($val)) {
                    $val = "'{$val}'";
                }
                $asSql = substr_replace($asSql, $val, $strPos, 1);
            }
        }
        return $asSql;
    }

    /**
     * 删除Cache 只限于自己设定的key
     *
     **/
    public function delCache()
    {
        #删除Tag 里面 的 key
        if (!empty($this->cacheKey) && !empty($this->cacheTag)) {
            $this->delCaches('tag');
        } elseif (!empty($this->cacheTag)) {
            $this->dbCache();
            $this->cacheObj->deleteTag($this->cacheTag);
        } elseif (!empty($this->cacheKey)) {
            $this->delCaches();
        }
        #重置一些默认值
        $this->_resetParameter();
    }


    /**
     * 删除多个Cache key 和 范围Cache key
     *
     * 例子:1,10 或 a,z
     *
     * @param String $act
     */
    protected function delCaches($act = '')
    {
        $this->dbCache();
        #sql_map_db_cache_key 类型是 数字或字母  Case: 1,10 or a-z
        if (is_string($this->cacheKey) && substr_count($this->cacheKey, ',') == 1) {
            $keyArray = explode(',', $this->cacheKey);
            #验证key是否合法
            $keyRange = range($keyArray[0], $keyArray[1]);
            foreach ($keyRange as $val) {
                $rangeKey = '';
                if (empty($this->cacheKeyPrefix)) {
                    $rangeKey = $val;
                } else {
                    $rangeKey = $this->cacheKeyPrefix . $val;
                }
                if ($act == 'tag') {
                    $this->cacheObj->tag($this->cacheTag)->delete($rangeKey);
                } else {
                    $this->cacheObj->delete($rangeKey);
                }
            }

            #如果是数组的情况下
        } elseif (is_array($this->cacheKey)) {
            foreach ($this->cacheKey as $val) {
                if ($act == 'tag') {
                    $this->cacheObj->tag($this->cacheTag)->delete($val);
                } else {
                    $this->cacheObj->delete($val);
                }
            }
            #是字符的情况下
        } elseif (is_string($this->cacheKey)) {
            if ($act == 'tag') {
                $this->cacheObj->tag($this->cacheTag)->delete($this->cacheKey);
            } else {
                $this->cacheObj->delete($this->cacheKey);
            }
        }
    }

    /**
     * 恢复默认设置
     */
    protected function _resetParameter()
    {
        unset($this->cacheKey);
        unset($this->cacheTag);
        unset($this->cacheKeyPrefix);
        unset($this->actMode);
        $this->cacheStatus = true;
        $this->fetchOptions = array(
            'column_number' => 0,
            'fetch_style' => null,
            'fetch_argument' => null,
            'ctor_args' => array()
        );
    }

    /**
     * 设置缓存池id
     * @param $persistentID
     * @return $this
     */
    public function setCachePersistentID($persistentID)
    {
        $this->cachePersistentID = $persistentID;
        return $this;
    }

    /**
     * 设置编码
     * @param $charset
     * @return $this
     */
    public function setCharset($charset)
    {
        $this->charset = $charset;
        return $this;
    }

    /**
     * 实例缓存
     * @return Q_Cache_Memcached
     */
    public function cacheObj()
    {
        return $this->dbCache();
    }
}