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 setUp() { if (self::DB_DSN_PREFIX == 'sqlsrv' && (!extension_loaded('pdo') || !extension_loaded('sqlsrv') || !extension_loaded('pdo_sqlsrv'))) { $this->markTestSkipped('PDO and SQLSRV extensions are required.'); } else { if (self::DB_DSN_PREFIX != 'sqlsrv' && (!extension_loaded('pdo') || !extension_loaded('pdo_dblib'))) { $this->markTestSkipped('PDO and MSSQL extensions are required.'); } } if (self::DB_DSN_PREFIX == 'sqlsrv') { $dsn = self::DB_DSN_PREFIX . ':Server=' . self::DB_HOST . ';Database=' . self::DB_NAME; } else { $dsn = self::DB_DSN_PREFIX . ':host=' . self::DB_HOST . ';dbname=' . self::DB_NAME; } $this->db = new CDbConnection($dsn, self::DB_USER, self::DB_PASS); if (self::DB_DSN_PREFIX == 'sqlsrv') { $this->db->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM); } try { $this->db->active = true; } catch (Exception $e) { $schemaFile = realpath(dirname(__FILE__) . '/../data/mssql.sql'); $this->markTestSkipped("Please read {$schemaFile} for details on setting up the test environment for MSSQL test case."); } $tables = array('comments', 'post_category', 'posts', 'categories', 'profiles', 'users', 'items', 'orders', 'types'); foreach ($tables as $table) { $sql = <<<EOD IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[{$table}]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1) DROP TABLE [dbo].[{$table}] EOD; $this->db->createCommand($sql)->execute(); } $rawSqls = file_get_contents(dirname(__FILE__) . '/../data/mssql.sql'); // remove comments from SQL $sqls = ''; foreach (array_filter(explode("\n", $rawSqls)) as $line) { if (substr($line, 0, 2) == '--') { continue; } $sqls .= $line . "\n"; } // run SQL foreach (explode('GO', $sqls) as $sql) { if (trim($sql) !== '') { $this->db->createCommand($sql)->execute(); } } CActiveRecord::$db = $this->db; }
/** * Connects to the specified schema and assigns it to all models which need it. * * @param $schema schema * @return CDbConnection */ protected function connectDb($schema) { // Assign request $this->request = Yii::app()->getRequest(); // Check parameter if (is_null($schema)) { $this->db = null; return null; } // Connect to database $connectionString = 'mysql:host=' . Yii::app()->user->host . ';port=' . Yii::app()->user->port . ';dbname=' . $schema . '; charset=utf8'; $this->db = new CDbConnection($connectionString, utf8_decode(Yii::app()->user->name), utf8_decode(Yii::app()->user->password)); $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES \'utf8\''); $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET CHARACTER SET \'utf8\''); $this->db->charset = 'utf8'; $this->db->emulatePrepare = true; $this->db->active = true; // Schema name is set in connection string // $this->db->createCommand('USE ' . $this->db->quoteTableName($schema))->execute(); // Assign to all models which need it ActiveRecord::$db = Routine::$db = Row::$db = Trigger::$db = View::$db = $this->db; // Return connection return $this->db; }