public function __construct() { if (load_class('core_config')->get('system')->debug) { $start_time = microtime(TRUE); } if (load_class('core_config')->get('database')->charset) { load_class('core_config')->get('database')->master['charset'] = load_class('core_config')->get('database')->charset; if (load_class('core_config')->get('database')->slave) { load_class('core_config')->get('database')->slave['charset'] = load_class('core_config')->get('database')->charset; } } $this->db['master'] = Zend_Db::factory(load_class('core_config')->get('database')->driver, load_class('core_config')->get('database')->master); try { $this->db['master']->query("SET sql_mode = ''"); } catch (Exception $e) { throw new Zend_Exception('Can\'t connect master database: ' . $e->getMessage()); } if (load_class('core_config')->get('system')->debug and class_exists('AWS_APP', false)) { AWS_APP::debug_log('database', microtime(TRUE) - $start_time, 'Connect Master DB'); } if (load_class('core_config')->get('database')->slave) { if (load_class('core_config')->get('system')->debug) { $start_time = microtime(TRUE); } $this->db['slave'] = Zend_Db::factory(load_class('core_config')->get('database')->driver, load_class('core_config')->get('database')->slave); try { $this->db['slave']->query("SET sql_mode = ''"); } catch (Exception $e) { throw new Zend_Exception('Can\'t connect slave database: ' . $e->getMessage()); } if (load_class('core_config')->get('system')->debug and class_exists('AWS_APP', false)) { AWS_APP::debug_log('database', microtime(TRUE) - $start_time, 'Connect Slave DB'); } } else { $this->db['slave'] =& $this->db['master']; } if (!defined('MYSQL_VERSION')) { define('MYSQL_VERSION', $this->db['master']->getServerVersion()); } //Zend_Db_Table_Abstract::setDefaultAdapter($this->db['master']); $this->setObject(); }
/** * 系统初始化 */ private static function init() { set_exception_handler(array('AWS_APP', 'exception_handle')); self::$config = load_class('core_config'); self::$db = load_class('core_db'); self::$plugins = load_class('core_plugins'); self::$settings = self::model('setting')->get_settings(); if ((!defined('G_SESSION_SAVE') or G_SESSION_SAVE == 'db') and get_setting('db_version') > 20121123) { Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable(array('name' => get_table('sessions'), 'primary' => 'id', 'modifiedColumn' => 'modified', 'dataColumn' => 'data', 'lifetimeColumn' => 'lifetime'))); self::$session_type = 'db'; } Zend_Session::setOptions(array('name' => G_COOKIE_PREFIX . '_Session', 'cookie_domain' => G_COOKIE_DOMAIN)); if (G_SESSION_SAVE == 'file' and G_SESSION_SAVE_PATH) { Zend_Session::setOptions(array('save_path' => G_SESSION_SAVE_PATH)); } Zend_Session::start(); self::$session = new Zend_Session_Namespace(G_COOKIE_PREFIX . '_Anwsion'); if ($default_timezone = get_setting('default_timezone')) { date_default_timezone_set($default_timezone); } if ($img_url = get_setting('img_url')) { define('G_STATIC_URL', $img_url); } else { define('G_STATIC_URL', base_url() . '/static'); } if (self::config()->get('system')->debug) { if ($cornd_timer = self::cache()->getGroup('crond')) { foreach ($cornd_timer as $cornd_tag) { if ($cornd_runtime = self::cache()->get($cornd_tag)) { AWS_APP::debug_log('crond', 0, 'Tag: ' . str_replace('crond_timer_', '', $cornd_tag) . ', Last run time: ' . date('Y-m-d H:i:s', $cornd_runtime)); } } } } }
/** * GET * @param $key */ public function get($key) { if (AWS_APP::config()->get('system')->debug) { list($usec, $sec) = explode(' ', microtime()); $start_time = (double) $usec + (double) $sec; } if (!$key) { return false; } $result = $this->cache_factory->load($this->cachePrefix . $key); if (AWS_APP::config()->get('system')->debug) { list($usec, $sec) = explode(' ', microtime()); $end_time = (double) $usec + (double) $sec; $stime = sprintf("%06f", $end_time - $start_time); AWS_APP::debug_log('cache', $stime, 'Get Cache: ' . str_replace($this->groupPrefix, '', $key) . ', result type: ' . gettype($result)); } return $result; }
/** * 计算字段总和, SELECT SUM() 方法 * * 面向对象数据库操作, 表名无需加表前缀, 数据也无需使用 $this->quote 进行过滤 ($where 条件除外) * * @param string * @param string * @param string * @return int */ public function sum($table, $column, $where = '') { $this->slave(); $select = $this->select(); $select->from($this->get_table($table), 'SUM(' . $column . ') AS n'); if ($where) { $select->where($where); } $sql = $select->__toString(); if (AWS_APP::config()->get('system')->debug) { $start_time = microtime(TRUE); } try { $result = $this->db()->fetchRow($select); } catch (Exception $e) { show_error("Database error\n------\n\nSQL: {$sql}\n\nError Message: " . $e->getMessage(), $e->getMessage()); } if (AWS_APP::config()->get('system')->debug) { AWS_APP::debug_log('database', microtime(TRUE) - $start_time, $sql); } return intval($result['n']); }