/** * Adds the datasource to the Connection Manager's list of sources if it is * not already there. It would normally be there if you add the datasource * details to your app/config/database.php file, but this code negates the * need to do that. It adds the datasource for the current model being * constructed with default basic configuration options, and extra options * from the GDATA_CONFIG->{$this->useDbConfig} class property from the file in * plugins/gdata/config/gdata_config.php if it exists, and extra options from * Gdata.config key in the Configure class, if set. Options should include * X-GData-Key as a minimum if required by the Gdata API, and also optionally * oauth_consumer_key, oauth_consumer_secret, oauth_token and * oauth_token_secret keys * * @param mixed $id * @param string $table * @param mixed $ds */ public function __construct($id = false, $table = null, $ds = null) { // Get the list of datasource that the ConnectionManager is aware of $sources = ConnectionManager::sourceList(); // If this model's datasource isn't in it, add it if (!in_array($this->useDbConfig, $sources)) { // Default minimum config $config = array('datasource' => 'Gdata.GdataSource', 'driver' => 'Gdata.' . Inflector::camelize($this->useDbConfig)); // Try an import the plugins/gdata/config/gdata_config.php file and merge // any default and datasource specific config with the defaults above if (App::import(array('type' => 'File', 'name' => 'Gdata.GDATA_CONFIG', 'file' => 'config' . DS . 'gdata_config.php'))) { $GDATA_CONFIG = new GDATA_CONFIG(); if (isset($GDATA_CONFIG->default)) { $config = array_merge($config, $GDATA_CONFIG->default); } if (isset($GDATA_CONFIG->{$this->useDbConfig})) { $config = array_merge($config, $GDATA_CONFIG->{$this->useDbConfig}); } } // Add any config from Configure class that you might have added at any // point before the model is instantiated. if (($configureConfig = Configure::read('Gdata.config')) != false) { $config = array_merge($config, $configureConfig); } // Add the datasource, with it's new config, to the ConnectionManager ConnectionManager::create($this->useDbConfig, $config); } parent::__construct($id, $table, $ds); }
/** * Gets the connection names that should have logs + dumps generated. * * @param Controller $controller The controller. * @return array */ public function beforeRender(Controller $controller) { if (!class_exists('ConnectionManager')) { return array(); } $connections = array(); $dbConfigs = ConnectionManager::sourceList(); foreach ($dbConfigs as $configName) { $driver = null; $db = ConnectionManager::getDataSource($configName); if (empty($db->config['driver']) && empty($db->config['datasource']) || !method_exists($db, 'getLog')) { continue; } if (isset($db->config['datasource'])) { $driver = $db->config['datasource']; } $explain = false; $isExplainable = preg_match('/(Mysql|Postgres)$/', $driver); if ($isExplainable) { $explain = true; } $connections[$configName] = $explain; } return array('connections' => $connections, 'threshold' => $this->slowRate); }
/** * Adds the datasource to the connection manager if it's not already there, * which it won't be if you've not added it to your app/config/database.php * file. * * @param $id * @param $table * @param $ds * @return void */ public function __construct($id = false, $table = null, $ds = null) { $sources = ConnectionManager::sourceList(); if (!in_array('braintree', $sources)) { ConnectionManager::create('braintree', array('datasource' => 'Braintree.BraintreeSource')); } parent::__construct($id, $table, $ds); }
/** * set DataSource Object * * @param string $datasource */ public function setTwitterSource($datasource) { if (empty($datasource) || !in_array($datasource, array_keys(get_class_vars('DATABASE_CONFIG'))) && !in_array($datasource, ConnectionManager::sourceList())) { return; } $this->settings['datasource'] = $datasource; $this->getTwitterSource(); }
function setUp() { parent::setUp(); // set up cache Cache::write('Cache.disable', false); // set up default cache config for tests Cache::config('default', array('engine' => 'File', 'duration' => '+6 hours', 'prefix' => 'cacher_tests_', 'path' => CACHE)); $this->CacheData = ClassRegistry::init('CacheData'); if (!in_array('cacher', ConnectionManager::sourceList())) { ConnectionManager::create('cacher', array('original' => $this->CacheData->useDbConfig, 'datasource' => 'Cacher.CacheSource')); } $this->dataSource = new CacheSourceTest(array('original' => $this->CacheData->useDbConfig)); }
/** * beforeFilter * * @return void */ public function beforeFilter() { parent::beforeFilter(); set_time_limit(300); /* インストール状態判別 */ if (file_exists(APP . 'Config' . DS . 'database.php')) { ConnectionManager::sourceList(); $db = ConnectionManager::$config; if ($db->default['datasource'] != '') { $installed = 'complete'; } else { $installed = 'half'; } } else { $installed = 'yet'; } switch ($this->request->action) { case 'alert': break; case 'reset': if (Configure::read('debug') != -1) { $this->notFound(); } break; default: if ($installed == 'complete') { if ($this->request->action != 'step5') { $this->notFound(); } } else { $installationData = Cache::read('Installation', 'default'); if (empty($installationData['lastStep'])) { if (Configure::read('debug') == 0) { $this->redirect(array('action' => 'alert')); } } } break; } /*if (strpos($this->request->webroot, 'webroot') === false) { $this->request->webroot = DS; }*/ $this->Security->csrfCheck = false; $this->Security->validatePost = false; }
/** * Sets up a connection using passed settings * * ### Config * - `config` The name of an existing Cache configuration to use. Default is 'default' * - `clearOnSave` Whether or not to delete the cache on saves * - `clearOnDelete` Whether or not to delete the cache on deletes * - `auto` Automatically cache or look for `'cache'` in the find conditions * where the key is `true` or a duration * * @param Model $Model The calling model * @param array $config Configuration settings * @see Cache::config() */ public function setup(Model $Model, $config = array()) { $_defaults = array('config' => 'default', 'clearOnDelete' => true, 'clearOnSave' => true, 'auto' => false, 'gzip' => false); $settings = array_merge($_defaults, $config); $Model->_useDbConfig = $Model->useDbConfig; $ds = ConnectionManager::getDataSource($Model->useDbConfig); if (!in_array('cacher', ConnectionManager::sourceList())) { $settings += array('original' => $Model->useDbConfig, 'datasource' => 'Cacher.CacheSource'); $settings = array_merge($ds->config, $settings); ConnectionManager::create('cacher', $settings); } else { $ds = ConnectionManager::getDataSource('cacher'); $ds->config = array_merge($ds->config, $settings); } if (!isset($this->settings[$Model->alias])) { $this->settings[$Model->alias] = $settings; } $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings); }
/** * Sets up a connection using passed settings * * ### Config * - `config` The name of an existing Cache configuration to use. Default is 'default' * - `clearOnSave` Whether or not to delete the cache on saves * - `clearOnDelete` Whether or not to delete the cache on deletes * - `auto` Automatically cache or look for `'cache'` in the find conditions * where the key is `true` or a duration * * @param Model $model The calling model * @param array $config Configuration settings * @return void * @see Cache::config() */ public function setup(Model $model, $config = array()) { $settings = array_merge($this->_defaults, $config); // Modified and set config for CachedSoure by behavior setting $ds = ConnectionManager::getDataSource($model->useDbConfig); $dsConfig = array_merge($ds->config, array('original' => $model->useDbConfig, 'datasource' => 'Cache.CachedSource', 'config' => $settings['config'], 'map' => $settings['map'], 'gzip' => $settings['gzip'])); if (!in_array('cached', ConnectionManager::sourceList())) { ConnectionManager::create('cached', $dsConfig); } else { $ds = ConnectionManager::getDataSource('cached'); $ds->setConfig($dsConfig); } // Merge behavior setting if (!isset($this->settings[$model->alias])) { $this->settings[$model->alias] = $settings; } else { $this->settings[$model->alias] = array_merge($this->settings[$model->alias], $settings); } return parent::setup($model, $config); }
/** * get db configs. * * @param string $controller * @access public * @return void */ function startUp(&$controller) { if (!class_exists('ConnectionManager')) { $this->dbConfigs = array(); return false; } $this->dbConfigs = ConnectionManager::sourceList(); return true; }
/** * Gets the connection names that should have logs + dumps generated. * * @param string $controller * @access public * @return void */ function beforeRender(&$controller) { if (!class_exists('ConnectionManager')) { return array(); } $connections = array(); $dbConfigs = ConnectionManager::sourceList(); foreach ($dbConfigs as $configName) { $db =& ConnectionManager::getDataSource($configName); $driver = $db->config['driver']; $explain = false; $isExplainable = $driver === 'mysql' || $driver === 'mysqli' || $driver === 'postgres'; if ($isExplainable && $db->isInterfaceSupported('getLog')) { $explain = true; } $connections[$configName] = $explain; } return array('connections' => $connections, 'threshold' => $this->slowRate); }
/** * Shows sql dump * * @param bool $sorted Get the queries sorted by time taken, defaults to false. * @param bool $clear If True the existing log will cleared. */ public function sqlDump($sorted = false, $clear = true) { if (!class_exists('ConnectionManager') || Configure::read('debug') < 2) { return; } $sources = ConnectionManager::sourceList(); $logs = array(); foreach ($sources as $source) { $db = ConnectionManager::getDataSource($source); if (!method_exists($db, 'getLog')) { continue; } $logs[$source] = $db->getLog($sorted, $clear); } if (empty($logs)) { return; } $this->out('<b>SQL dump:</b>'); foreach ($logs as $source => $log) { $this->out("<b>Source: {$source}, queries: {$log['count']}, took: {$log['time']}ms</b>"); foreach ($log['log'] as $k => $i) { $i += array('error' => ''); $this->out($k + 1 . ". {$i['query']} <sqlinfo>{e:{$i['error']}, a:{$i['affected']}, t:{$i['took']}, n:{$i['numRows']}}</sqlinfo>"); } } }
/** * Returns the datasource object for the current controller's modelClass * * @return DataSource object */ public function getDataSource() { // Get the list of datasources that the ConnectionManager is aware of $sources = ConnectionManager::sourceList(); // If the twitter datasource is in it, return it if (in_array('twitter', $sources)) { return ConnectionManager::getDataSource('twitter'); } App::import('DataSource', 'Twitter.TwitterSource'); return new TwitterSource(); }
function testSetup() { $this->CacheData->Behaviors->attach('Cacher.Cache', array('duration' => '+1 days')); $this->assertTrue(in_array('cacher', ConnectionManager::sourceList())); $this->assertEquals($this->CacheData->_useDbConfig, 'test'); $this->assertEquals($this->CacheData->useDbConfig, 'test'); }
/** * Get Sql Logs for each DB config * * @param string $controller * @access public * @return void */ function beforeRender(&$controller) { $queryLogs = array(); if (!class_exists('ConnectionManager')) { return array(); } $dbConfigs = ConnectionManager::sourceList(); foreach ($dbConfigs as $configName) { $db =& ConnectionManager::getDataSource($configName); if ($db->isInterfaceSupported('showLog')) { ob_start(); $db->showLog(); $queryLogs[$configName] = ob_get_clean(); } } return $queryLogs; }
function _cake_constants() { $Constants = array('Cake Constants' => array('APP' => APP, 'APP_DIR' => APP_DIR, 'APP_PATH' => APP_PATH, 'CACHE' => CACHE, 'CAKE' => CAKE, 'CAKE_CORE_INCLUDE_PATH' => CAKE_CORE_INCLUDE_PATH, 'COMPONENTS' => COMPONENTS, 'CONFIGS' => CONFIGS, 'CONTROLLER_TESTS' => CONTROLLER_TESTS, 'CONTROLLERS' => CONTROLLERS, 'CORE_PATH' => CORE_PATH, 'CSS' => CSS, 'DEBUG (use Configure::read("debug"))' => DEBUG, 'ELEMENTS' => ELEMENTS, 'FULL_BASE_URL' => FULL_BASE_URL, 'FULL_BASE_URL . Router::url(\'\', false)' => FULL_BASE_URL . Router::url('', false), 'HELPER_TESTS' => HELPER_TESTS, 'HELPERS' => HELPERS, 'INFLECTIONS' => INFLECTIONS, 'JS' => JS, 'LAYOUTS' => LAYOUTS, 'LIB_TESTS' => LIB_TESTS, 'LIBS' => LIBS, 'LOGS' => LOGS, 'MODEL_TESTS' => MODEL_TESTS, 'MODELS' => MODELS, 'PROJECT_ROOT (Cakewell only)' => PROJECT_ROOT, 'TESTS' => TESTS, 'TMP' => TMP, 'ROOT' => ROOT, 'VENDORS' => VENDORS, 'VIEWS' => VIEWS, 'WEBROOT_DIR' => WEBROOT_DIR, 'WWW_ROOT' => WWW_ROOT), 'Controller Properties' => array('$this->action' => $this->action, '$this->here' => $this->here, '$this->name' => $this->name, '$this->params[\'url\']' => $this->params['url']), 'Other' => array('ConnectionManager::sourceList' => ConnectionManager::sourceList(), 'Configure Class Settings' => 'see config_test')); return $Constants; }
/** * Get Sql Logs for each DB config * * @param string $controller * @access public * @return void */ function beforeRender(&$controller) { if (!class_exists('ConnectionManager')) { return array(); } App::import('Core', 'Xml'); $queryLogs = array(); $dbConfigs = ConnectionManager::sourceList(); foreach ($dbConfigs as $configName) { $db =& ConnectionManager::getDataSource($configName); if ($db->isInterfaceSupported('showLog')) { ob_start(); $db->showLog(); $htmlBlob = ob_get_clean(); $Xml =& new Xml($htmlBlob); $table = $Xml->children[0]; $tbody = $table->children('tbody'); $rows = $tbody[0]->children; if (empty($rows) || empty($rows[0]->children)) { continue; } $queries = $explained = array(); foreach ($rows as $row) { $tds = $this->_getCells($row); $queries[] = $tds; $isSlow = $tds[5] > 0 && $tds[4] / $tds[5] != 1 && $tds[4] / $tds[5] <= $this->slowRate; if ($isSlow && preg_match('/^SELECT /', $tds[1])) { $explain = $this->_explainQuery($db, $tds[1]); if (!empty($explain)) { $explained[] = $explain; } } } $queryLogs[$configName]['queries'] = $queries; $queryLogs[$configName]['explains'] = $explained; } } return $queryLogs; }
/** * create all datasouces sql dump file * * @param string $save path to save folder. */ function processAll($save) { $datasources = ConnectionManager::sourceList(); foreach ($datasources as $datasource) { $this->process($datasource, null, $save); } }
/** * Tests if a model's database is connected * * @param string Name of model to test * @access private * @param unknown $model_name * @return boolean $model_name Connection status of a model's database */ private function _isConnected($model_name) { if (isset($this->_smartMirrors[$model_name]['dbConfig'])) { if (in_array($this->_smartMirrors[$model_name]['dbConfig'], ConnectionManager::sourceList())) { return true; } } return false; }
function run() { // establish a new connection for all data sources foreach (ConnectionManager::sourceList() as $sourceName) { ConnectionManager::getDataSource($sourceName)->connect(); } while (true) { if ($this->getVariable('status') === self::READY) { $taskName = $this->getVariable('task'); $methodName = $this->getVariable('method'); if (empty($methodName)) { $methodName = 'execute'; } // try and load the task if (!$this->loadTask($taskName)) { $this->setVariable('status', self::ERROR); continue; } $this->setVariable('status', self::BUSY); $task =& $this->{$taskName}; $task->thread =& $this; $task->pid = $this->getVariable('pid'); $task->data =& $this->getVariable('data'); // execute the task here $task->{$methodName}(); // if status hasn't changed, then we mark it as a success! if ($this->getVariable('status') === self::BUSY) { $this->setVariable('status', self::SUCCESS); } } else { // sleep for 10000 nanoseconds while we wait for more intructions usleep(10000); } } }
/** * データソースを取得する * * @param string $configKeyName * @param array $dbConfig * @return DataSource * @access public */ function &_getDataSource($dbConfigKeyName = 'baser', $dbConfig = null) { $sources = ConnectionManager::sourceList(); if ($dbConfig) { if (in_array($dbConfigKeyName, $sources)) { $db =& ConnectionManager::getDataSource($dbConfigKeyName); } else { $db =& ConnectionManager::create($dbConfigKeyName, $dbConfig); } } else { $db =& ConnectionManager::getDataSource($dbConfigKeyName); } return $db; }
/** * Get a list of sources defined in database.php * * @codeCoverageIgnore * @return array */ protected function _getSources() { return ConnectionManager::sourceList(); }
/** * testSourceList method * * @return void */ public function testSourceList() { ConnectionManager::getDataSource('test'); $sources = ConnectionManager::sourceList(); $this->assertTrue(count($sources) >= 1); $this->assertTrue(in_array('test', array_keys($sources))); }
public function afterFilter() { parent::afterFilter(); // sql logging to chrome console if (class_exists('ConnectionManager') && Configure::read('debug') >= 2) { App::import('Vendor', 'ChromePhp/ChromePhp'); $sources = ConnectionManager::sourceList(); $logs = array(); foreach ($sources as $source) { $db = ConnectionManager::getDataSource($source); $logs[$source] = $db->getLog(); } foreach ($logs as $source => $logInfo) { $text = $logInfo['count'] > 1 ? 'queries' : 'query'; ChromePhp::info('------- SQL: ' . sprintf('(%s) %s %s took %s ms', $source, count($logInfo['log']), $text, $logInfo['time']) . ' -------'); ChromePhp::info('------- REQUEST: ' . $this->request->params['controller'] . '/' . $this->request->params['action'] . ' -------'); foreach ($logInfo['log'] as $k => $i) { $i += array('error' => ''); if (!empty($i['params']) && is_array($i['params'])) { $bindParam = $bindType = null; if (preg_match('/.+ :.+/', $i['query'])) { $bindType = true; } foreach ($i['params'] as $bindKey => $bindVal) { if ($bindType === true) { $bindParam .= h($bindKey) . " => " . h($bindVal) . ", "; } else { $bindParam .= h($bindVal) . ", "; } } $i['query'] .= " , params[ " . rtrim($bindParam, ', ') . " ]"; } $error = !empty($i['error']) ? "\nError: " . $i['error'] : "\n"; $logStr = $i['query'] . $error . "\nAffected: " . $i['affected'] . "\nNum. Rows: " . $i['numRows'] . "\nTook(ms): " . $i['took'] . "\n\n"; if (!empty($i['error'])) { ChromePhp::error($logStr); } else { if ($i['took'] >= 100) { ChromePhp::warn($logStr); } else { ChromePhp::info($logStr); } } } } } }
/** * @brief add connection to the connection manager * * allow plugins to use their own db configs. If there is a conflict, * eg: a plugin tries to set a config that alreay exists an error will * be thrown and the connection will not be created. * * default is a reserved connection that can only be set in database.php * and not via the events. * * @code * // for databases * array( * 'my_connection' => array( * 'driver' => 'mysqli', * 'persistent' => true, * 'host' => 'localhost', * 'login' => 'username', * 'password' => 'pw', * 'database' => 'db_name', * 'encoding' => 'utf8' * ) * ) * * // or other datasources * array( * 'my_connection' => array( * 'datasource' => 'Emails.Imap' * ) * ) * @endcode * * @access private * * @return void */ private function __setupDatabaseConnections() { $connections = array_filter(current(EventCore::trigger($this, 'requireDatabaseConfigs'))); foreach ($connections as $plugin => $connection) { $key = current(array_keys($connection)); $connection = current($connection); $alreadyUsed = strtolower($key) == 'default' || in_array($key, ConnectionManager::sourceList()); if ($alreadyUsed) { continue; throw new Exception(sprintf(__('The connection "%s" in the plugin "%s" has already been used. Skipping'), $key, $plugin)); } ConnectionManager::create($key, $connection); } }
/** * testSourceList method * * @access public * @return void */ function testSourceList() { $sources = ConnectionManager::sourceList(); $this->assertTrue(count($sources) >= 1); $connections = array('default', 'test', 'test_suite'); $this->assertTrue(count(array_intersect($sources, $connections)) >= 1); }
function logSQL() { $sources = ConnectionManager::sourceList(); if (!isset($logs)) { $logs = array(); foreach ($sources as $source) { $db =& ConnectionManager::getDataSource($source); if (!$db->isInterfaceSupported('getLog')) { continue; } $logs[$source] = $db->getLog(false, false); } } echo '<pre>'; print_r($logs); echo '</pre>'; }
/** * Register error handler */ Configure::write('Error.handler', 'FirePHP::errorHandler'); /** * Register exception handler */ //Configure::write('Exception.handler', 'FirePHP::exceptionHandler'); /** * Register sql log to console */ register_shutdown_function(function () { if (!class_exists('ConnectionManager') || !ConnectionManager::$config || Configure::read('debug') < 2) { return; } $sources = ConnectionManager::sourceList(); $logs = array(); foreach ($sources as $source) { $db = ConnectionManager::getDataSource($source); if (!method_exists($db, 'getLog')) { continue; } $logs[$source] = $db->getLog(); } foreach ($logs as $source => $logInfo) { $text = $logInfo['count'] > 1 ? 'queries' : 'query'; $label = "{$logInfo['count']} {$text} took {$logInfo['time']} ms (DESCRIBE queries are not shown)"; $table = array(); $table[] = array("Nr", "Query", "Affected", "Num. rows", "Took (ms)"); foreach ($logInfo['log'] as $k => $i) { // Filter numerous DESCRIBE queries from to be shown
function dump_sql() { $sql_dump = ''; if (!class_exists('ConnectionManager') || Configure::read('debug') < 2) { return false; } $noLogs = !isset($logs); if ($noLogs) { $sources = ConnectionManager::sourceList(); $logs = array(); foreach ($sources as $source) { $db =& ConnectionManager::getDataSource($source); if (!$db->isInterfaceSupported('getLog')) { continue; } $logs[$source] = $db->getLog(); } } if ($noLogs || isset($_forced_from_dbo_)) { foreach ($logs as $source => $logInfo) { $text = $logInfo['count'] > 1 ? 'queries' : 'query'; $this->out("cakeSqlLog_" . preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true))); $this->out('(' . $source . ') ' . $logInfo['count'] . ' ' . $text . ' took ' . $logInfo['time'] . ' ms'); $this->out('Nr Query Error Affected Num. rows Took (ms)'); foreach ($logInfo['log'] as $k => $i) { $this->out($i['query']); } } } else { $this->out('Encountered unexpected $logs cannot generate SQL log'); } //return $sql_dump; }