public function openConnection() { $dsn = "mysql:host={$this->host};port={$this->port};"; $this->db = new CDbConnection($dsn); $this->db->setAttribute(PDO::ATTR_TIMEOUT, $this->connectionTimeout); $this->db->setActive(true); }
/** * Open Sphinx persistent connection. * * @throws ESphinxException if client is already connected. * @throws ESphinxException if client has connection error. * @link http://sphinxsearch.com/docs/current.html#api-func-open */ public function openConnection() { $dsn = "mysql:host={$this->server};port={$this->port};"; $this->db = new ESphinxQlDbConnection($dsn); $this->db->setAttribute(PDO::ATTR_TIMEOUT, $this->connectionTimeout); $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $this->db->setActive(true); }
public function fork() { $this->b2b_DbConnection->setActive(false); $this->oneC_DbConnection->setActive(false); $pid = pcntl_fork(); $this->b2b_DbConnection->setActive(true); $this->oneC_DbConnection->setActive(true); return $pid; }
/** * 打开或关闭数据库连接 * * @param boolean $value whether to open or close DB connection * @throws CException if connection fails */ public function setActive($value) { if ($value != $this->getActive() && $value) { $this->setAttribute(PDO::ATTR_TIMEOUT, $this->timeout); } parent::setActive($value); }
/** * Constructor. * @param CDbConnection $conn database connection. */ public function __construct($conn) { $conn->setActive(true); $this->_connection = $conn; foreach ($conn->schemaCachingExclude as $name) { $this->_cacheExclude[$name] = true; } }
public function setupDB() { $dump_file = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'db.sql'; if (!file_exists($dump_file)) { return array('error' => 'Dump file was not found'); } $connection = new CDbConnection('mysql:host=' . $this->host . ';port=' . $this->port, $this->user, $this->password); $connection->setActive(true); $sql = "DROP DATABASE IF EXISTS `{$this->dbname}`; CREATE DATABASE `{$this->dbname}` CHARACTER SET utf8 COLLATE utf8_general_ci;"; $connection->createCommand($sql)->query(); $connection->setActive(false); $connection = null; $connection = new CDbConnection('mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password); $sql = file_get_contents($dump_file); $connection->createCommand($sql)->query(); $this->saveDBInstallStatusConfig(1); return array('ok' => 1); }
public function install() { # Does the application need installing? Check if database exists, and can connect try { # Required fields $required = array("db-host", "db-name", "db-username", "db-password", "table-prefix"); # Did all the required fields get passed in? if (count(array_intersect($required, array_keys($_REQUEST))) != count($required)) { throw new Exception("Not all required fields were submitted."); } # Verify the required unempty fields foreach ($required as $field) { # Skip the fields that can be empty if ($field == "table-prefix" or $field == "db-password") { continue; } # Check if empty, throw error if they are. if (empty($_REQUEST[$field])) { throw new Exception("Field <i>" . lookupfieldname($field) . "</i> cannot be empty."); } } # Try connecting to the database with the passed in credentials try { # Setup connection details $dsn = 'mysql:host=' . $_REQUEST["db-host"] . ';dbname=' . $_REQUEST["db-name"]; $username = $_REQUEST["db-username"]; $password = $_REQUEST["db-password"]; $prefix = $_REQUEST["table-prefix"]; # Make the connection $conn = new CDbConnection($dsn, $username, $password); $conn->active = true; $conn->setActive(true); } catch (Exception $e) { throw new Exception("Could not connect to database. Make sure you have created the database first. Details: " . $e->getMessage()); } # Setup the database params for saving in the extended configuration $db_params = array('components' => array('db' => array('connectionString' => $dsn, 'emulatePrepare' => true, 'username' => $username, 'password' => $password, 'charset' => 'utf8', 'tablePrefix' => $prefix)), 'params' => array('LOCALAPP_SERVER' => $_SERVER["HTTP_HOST"])); # Make sure to only overwrite if explicitly asked to $config_ext = Yii::app()->basePath . "\\config\\main-ext.php"; if (is_file($config_ext)) { throw new Exception("Database configuration already exists. Delete this configuration in order to install this application."); } # Open up the file and write the new configuration. $handle = fopen($config_ext, "w"); fwrite($handle, "<?php return "); fwrite($handle, var_export($db_params, true)); fwrite($handle, "; ?>"); fclose($handle); # Make read-only chmod($config_ext, 060); } catch (Exception $e) { $this->set_error($e->getMessage()); return false; } # If we made it to here, installation is a success! return true; }
/** * Validator for connection to MySQL * @return bool Whether or not we could connect to MySQL */ public function validateConnection() { // Make sure all fields are provided if ($this->validate()) { // Just turning the connection on and off. A CDbException will be thrown if something goes wrong try { $connection = new CDbConnection("mysql:host={$this->host};dbname={$this->dbname}", $this->username, $this->password); $connection->setActive(true); $connection->setActive(false); $this->dsn = $connection->connectionString; return true; } catch (Exception $e) { // Add errors to all fields for the visual indicator $this->addError('username', ''); $this->addError('password', ''); $this->addError('dbname', ''); $this->addError('host', ''); $this->addError('dsn', Yii::t('Install.main', 'Unable to connect to database using the provided credentials.')); return false; } } $this->addError('dsn', Yii::t('Install.main', 'Unable to connect to database using the provided credentials.')); return false; }
public function checkUser() { if ($this->hasErrors('db_exp_sql_host') || $this->hasErrors('db_exp_sql_dbname') || $this->hasErrors('db_exp_sql_login') || $this->hasErrors('db_exp_sql_password') || $this->hasErrors('db_exp_sql_port')) { return false; } try { $connection = new CDbConnection('mysql:host=' . $this->db_exp_sql_host, $this->db_exp_sql_login, $this->db_exp_sql_password); $res = $connection->setActive(true); $sql = "CREATE DATABASE IF NOT EXISTS `{$this->db_exp_sql_dbname}` CHARACTER SET UTF8"; $res = $connection->createCommand($sql)->query(); } catch (CDbException $e) { $this->addError('db_exp_sql_login', $e->getMessage()); return false; } return true; }
public function setActive($value) { if ($value && $this->autoReconnect) { $lifetime = time() - $this->_lastActive; if ($lifetime > intval($this->autoReconnect)) { try { if ($this->getActive()) { @$this->getPdoInstance()->query('SELECT 1'); } } catch (Exception $e) { Yii::trace($e->getMessage() . " Last connection's lifetime: {$lifetime}, trying to close and reconnect... ", 'components.DbConnection.setActive'); parent::setActive(false); } } } parent::setActive($value); $this->_lastActive = time(); }
/** * This is our overloaded getDbConnection, allowing us to tell yii what our db connection is * without it having to go through */ public function getDbConnection() { $connection = new CDbConnection("mysql:host={$this->dsn['host']};dbname={$this->dsn['dbname']}", $this->dsn['username'], $this->dsn['password']); $connection->setActive(true); return $connection; }
public function checkUser() { if ($this->hasErrors('host') || $this->hasErrors('user') || $this->hasErrors('password') || $this->hasErrors('port')) { return false; } try { $connection = new CDbConnection('mysql:host=' . $this->host, $this->user, $this->password); $connection->setActive(true); $sql = "DROP DATABASE IF EXISTS `{$this->dbname}`;"; $connection->createCommand($sql)->query(); $sql = "CREATE DATABASE `{$this->dbname}` CHARACTER SET utf8 COLLATE utf8_general_ci;"; $connection->createCommand($sql)->query(); } catch (CDbException $e) { $this->addError('user', $e->getMessage()); return false; } return true; }
} foreach ($fieldmap as $k => $v) { $value = addslashes($_POST[$k]); fwrite($fh, "define('{$v[0]}', \"{$value}\");\n"); } fwrite($fh, "define('DB_VERSION', " . DB_VERSION . ");\n"); fclose($fh); header('location: setup.php?a=config'); return; } if (field_value('yii', false) && file_exists($yii = field_value('yii', false))) { require_once $yii; try { $db = new CDbConnection(field_value('dsn', false), field_value('user', false), field_value('password', false)); $db->tablePrefix = field_value('prefix'); $db->setActive(true); $db_ok = true; $curr_db_ver = filemtime(dirname(__FILE__) . '/protected/data/CurrentDbScheme.php'); $db_new = DB_VERSION >= $curr_db_ver; } catch (CException $e) { $db_ok = false; $db_new = false; $err_msg = $e->getMessage(); } } else { $err_msg = 'Path to yii ("' . field_value('yii') . '") not found'; } if (isset($_GET['a'])) { $page_act = $_GET['a']; } else { $page_act = 'config';
/** * This method will save the admin user into the database, */ public function save() { if (!$this->validateForm()) { return false; } try { // Store some data in session temporarily Yii::app()->session['encryptionKey'] = $this->encryptionKey; Yii::app()->session['siteName'] = $this->siteName; Yii::app()->session['primaryEmail'] = $this->email; // Try to save the record into the database $connection = new CDbConnection(Yii::app()->session['dsn']['dsn'], Yii::app()->session['dsn']['username'], Yii::app()->session['dsn']['password']); $connection->setActive(true); $connection->createCommand('INSERT INTO users (id, email, password, firstName, lastName, displayName, user_role, status, created, updated) VALUES (1, :email, :password, :firstName, :lastName, :displayName, 9, 1, UTC_TIMESTAMP(), UTC_TIMESTAMP())')->bindParam(':email', $this->email)->bindParam(':password', $this->encryptedPassword)->bindParam(':firstName', $this->firstName)->bindParam(':lastName', $this->lastName)->bindParam(':displayName', $this->displayName)->execute(); return true; } catch (CDbException $e) { $this->addError('password', Yii::t('Install.main', 'There was an error saving your details to the database.')); return false; } return false; }
/** * Init connection with backup database */ private function initDbConnection() { It::debug("try to init DB connection (0)", 'backup_database'); try { It::debug("try to init DB connection (1)", 'backup_database'); // check if (remote) host is reachable $fp = @fsockopen($this->settings->db_exp_sql_host, $this->settings->db_exp_sql_port); if ($fp === false) { throw new Exception("Host " . $this->settings->db_exp_sql_host . ':' . $this->settings->db_exp_sql_port . ' is unreachable'); } // create connection with database $dsn = 'mysql:host=' . $this->settings->db_exp_sql_host . ';port=' . $this->settings->db_exp_sql_port . ';dbname=' . $this->settings->db_exp_sql_dbname; $connection = new CDbConnection($dsn, $this->settings->db_exp_sql_login, $this->settings->db_exp_sql_password); It::debug("try to init DB connection (2)", 'backup_database'); try { It::debug("try to init DB connection (3)", 'backup_database'); $res = $connection->setActive(true); It::debug("try to init DB connection (4)", 'backup_database'); $this->db_conn = $connection; It::debug("try to init DB connection (5)", 'backup_database'); } catch (PDOException $e) { It::debug("try to init DB connection (6)", 'backup_database'); throw new Exception($e->getMessage()); } It::debug("try to init DB connection (7)", 'backup_database'); } catch (CDbException $e) { It::debug("try to init DB connection (8)", 'backup_database'); throw new Exception($e->getMessage()); } It::debug("try to init DB connection (DONE)", 'backup_database'); }