/** * Create the Memcached connection * * @return void * * @since 12.1 * @throws RuntimeException */ protected function getConnection() { if (!static::isSupported()) { throw new RuntimeException('Memcached Extension is not available'); } $config = JFactory::getConfig(); $host = $config->get('memcached_server_host', 'localhost'); $port = $config->get('memcached_server_port', 11211); // Create the memcached connection if ($config->get('memcached_persist', true)) { static::$_db = new Memcached($this->_hash); $servers = static::$_db->getServerList(); if ($servers && ($servers[0]['host'] != $host || $servers[0]['port'] != $port)) { static::$_db->resetServerList(); $servers = array(); } if (!$servers) { static::$_db->addServer($host, $port); } } else { static::$_db = new Memcached(); static::$_db->addServer($host, $port); } static::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress); $stats = static::$_db->getStats(); $result = !empty($stats["{$host}:{$port}"]) && $stats["{$host}:{$port}"]['pid'] > 0; if (!$result) { // Null out the connection to inform the constructor it will need to attempt to connect if this class is instantiated again static::$_db = null; throw new JCacheExceptionConnecting('Could not connect to memcached server'); } }
/** * Set Database & Collection 设置数据库和表单 * * @param array $db * @param number $collection * @return */ public static function setDb($db = null, $collection = null) { if ($db && $collection) { static::$_db = $db; static::$_collection = $collection; self::$_mongoDB = null; } }
/** * 切断 */ protected static function _disconnect() { if (!static::$_db->close()) { throw new SqliteException(static::$_db->lastErrorMsg(), static::$_db->lastErrorCode()); } static::$_db = null; return true; }
/** * 切断 */ protected static function _disconnect() { if (!\mysql_close(static::$_db)) { throw new MysqlException(\mysql_error(static::$_db), \mysql_errno(static::$_db)); } static::$_db = null; return true; }
/** * 切断 */ protected static function _disconnect() { if (!static::$_db->close()) { throw new MysqliException(static::$_db->error, static::$_db->errno); } static::$_db = null; return true; }
/** * Get the instance of the PDO connection * * @return DB PDO connection */ public static function getInstance() { if (static::$_db === NULL) { $dsn = 'mysql:host=' . Config::DB_HOST . ';dbname=' . Config::DB_NAME . ';charset=utf8'; static::$_db = new PDO($dsn, Config::DB_USER, Config::DB_PASS); // Raise exceptions when a database exception occurs static::$_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } return static::$_db; }
/** * Create the Memcache connection * * @return void * * @since 11.1 * @throws RuntimeException */ protected function getConnection() { $config = JFactory::getConfig(); $this->_persistent = $config->get('memcache_persist', true); $this->_compress = $config->get('memcache_compress', false) == false ? 0 : MEMCACHE_COMPRESSED; // Create the memcache connection static::$_db = new Memcache(); static::$_db->addserver($config->get('memcache_server_host', 'localhost'), $config->get('memcache_server_port', 11211), $this->_persistent); $memcachetest = @static::$_db->connect($server['host'], $server['port']); if ($memcachetest == false) { throw new RuntimeException('Could not connect to memcache server', 404); } // Memcahed has no list keys, we do our own accounting, initialise key index if (static::$_db->get($this->_hash . '-index') === false) { static::$_db->set($this->_hash . '-index', array(), $this->_compress, 0); } return; }
/** * Create the Memcached connection * * @return void * * @since 12.1 * @throws RuntimeException */ protected function getConnection() { $config = JFactory::getConfig(); $this->_persistent = $config->get('memcached_persist', true); $this->_compress = $config->get('memcached_compress', false) == false ? 0 : Memcached::OPT_COMPRESSION; // Create the memcache connection if ($this->_persistent) { static::$_db = new Memcached(JFactory::getSession()->getId()); } else { static::$_db = new Memcached(); } $memcachedtest = static::$_db->addServer($config->get('memcached_server_host', 'localhost'), $config->get('memcached_server_port', 11211)); if ($memcachedtest == false) { throw new RuntimeException('Could not connect to memcached server', 404); } static::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress); // Memcached has no list keys, we do our own accounting, initialise key index if (static::$_db->get($this->_hash . '-index') === false) { static::$_db->set($this->_hash . '-index', array(), 0); } }
/** * Create the Memcache connection * * @return void * * @since 11.1 * @throws RuntimeException */ protected function getConnection() { if (!static::isSupported()) { throw new RuntimeException('Memcache Extension is not available'); } $config = JFactory::getConfig(); $host = $config->get('memcache_server_host', 'localhost'); $port = $config->get('memcache_server_port', 11211); // Create the memcache connection static::$_db = new Memcache(); if ($config->get('memcache_persist', true)) { $result = @static::$_db->pconnect($host, $port); } else { $result = @static::$_db->connect($host, $port); } if (!$result) { // Null out the connection to inform the constructor it will need to attempt to connect if this class is instantiated again static::$_db = null; throw new JCacheExceptionConnecting('Could not connect to memcache server'); } }
/** * 切断 */ protected static function _disconnect() { static::$_db = null; return true; }
public function __construct() { static::$_db = Registry::get("db"); }
/** * * DBConnector::resetAllStaticPropertiesExceptDefaultConfig() resets all properties * DBConnector::resetAllStaticPropertiesExceptDefaultConfig('_db') will reset only DBConnector::$_db * * @param string $prop_name name of the property (eg. 'db' or '_db') whose value is to be reset. * */ public static function resetAllStaticPropertiesExceptDefaultConfig($prop_name = '') { switch ($prop_name) { case '_config': case 'config': // Map of configuration settings static::$_config = array(); break; case '_db': case 'db': // Map of database connections, instances of the PDO class static::$_db = array(); break; case '_last_query': case 'last_query': // Last query run, only populated if logging is enabled static::$_last_query = ''; break; case '_query_log': case 'query_log': // Log of all queries run, mapped by connection key, only populated if logging is enabled static::$_query_log = array(); break; default: ////////////////////////// // Reset all properties // ////////////////////////// // Map of configuration settings static::$_config = array(); // Map of database connections, instances of the PDO class static::$_db = array(); // Last query run, only populated if logging is enabled static::$_last_query = ''; // Log of all queries run, mapped by connection key, only populated if logging is enabled static::$_query_log = array(); break; } }
/** * Initialize database adapter. * * @return void */ protected static function _setupDatabaseAdapter() { if (!static::$_db) { static::$_db = self::getDefaultAdapter(); if (!static::$_db instanceof Adapter) { //require_once 'Zend/Db/Table/Exception.php'; throw new DataObjectException('No adapter found for ' . get_called_class()); } } }
/** * Delete all registered PDO objects in _db array. */ public static function reset_db() { static::$_db = array(); }
static function init() { static::$_config = (include_once __DIR__ . "/../config.php"); static::$_db = getDb(static::$_config); }
/** * Constructor for every model class. This is protected as every model * instantiated outside of this class should use the static function * 'create'. */ public function __construct() { $this->_idField = $this->getIdField(); $this->_tableName = $this->getTableName(); if (!static::$_db) { static::$_db = Database::getInstance(); } if (!static::$_schema) { static::$_schema = new Cache(); } }