/** * If $name is null then the default connection will be returned. * * @see Config * @param string $name Optional name of a connection * @return Connection */ public static function getConnection($name = null) { $config = Config::instance(); $name = $name ? $name : $config->getDefaultConnection(); if (!isset(self::$connections[$name]) || !self::$connections[$name]->connection) { self::$connections[$name] = Connection::instance($config->getConnection($name)); } return self::$connections[$name]; }
/** * If $name is null then the default connection will be returned. * * @see Config * @param string $name Optional name of a connection * @return Connection */ public static function get_connection($name = null) { $config = Config::instance(); $name = $name ? $name : $config->get_default_connection(); if (!isset(self::$connections[$name]) || !self::$connections[$name]->connection) { self::$connections[$name] = Connection::instance($config->get_connection($name)); // If we have PHP DebugBar installed then we wrap the connection around it and register it if (is_a(self::$connections[$name]->connection, 'PDO') && class_exists('DebugBar\\DebugBar') && self::$debugBarConnections != null) { self::$connections[$name]->connection = new TraceablePDO(self::$connections[$name]->connection); self::$debugBarConnections->addConnection(self::$connections[$name]->connection, $name . ' #' . (count(self::$debugBarConnections->getConnections()) + 1)); } } return self::$connections[$name]; }
/** * setups db using activerecord */ public static function PRE_init_db() { # init activerecord configs \ActiveRecord\Config::initialize(function ($cfg) { # fetching db related configurations $dbcfg = \zinux\kernel\application\config::GetConfig("idisqus.db"); # setting connection string $cfg->set_connections(array(\application\dbBootstrap::MODE_TORATAN => "{$dbcfg["type"]}://{$dbcfg["username"]}:{$dbcfg["password"]}@{$dbcfg["host"]}/{$dbcfg["name"]}?charset=utf8")); # enable the connection string as to \application\dbBootstrap::MODE_TORATAN $cfg->set_default_connection(\application\dbBootstrap::MODE_TORATAN); }); # set default datetime format \ActiveRecord\DateTime::$DEFAULT_FORMAT = "iso8601"; # testing db connection \ActiveRecord\Connection::instance(); # if we reach here we are all OK }
public function testConnectToInvalidDatabaseShouldNotCreateDbFile() { try { if ($GLOBALS['OS'] !== 'WIN') { $xcon = Connection::instance("sqlite://" . self::InvalidDb); var_dump($xcon); $this->assertFalse(true); } else { //sqlite://windows(c%3A/GitHub/activerecord/Test/Fixtures/test.db) $wincon = Connection::instance("sqlite://windows('c%3A/GitHub/activerecord/Test/Fixtures/'" . self::InvalidDb . "')'"); var_dump($wincon); $this->assertFalse(true); } } catch (ExceptionDatabase $e) { //$this->assertFalse(\file_exists(__DIR__."/".self::InvalidDb)); } }
public function testSetCharset() { $connection_string = Config::instance()->getConnection($this->connection_name); $conn = Connection::instance($connection_string . '?charset=utf8'); $this->assertEquals("SET NAMES 'utf8'", $conn->last_query); }
/** * @expectedException Activerecord\Exceptions\ExceptionDatabase */ public function testConnectToInvalidDatabase() { Connection::instance("{$this->conn->protocol}://test:test@127.0.0.1/" . self::InvalidDb); }
public function testSetCharset() { $connection_string = Config::instance()->getConnection($this->connection_name); $conn = Connection::instance($connection_string . '?charset=utf8'); $this->assertEquals(';charset=utf8', $conn->dsn_params); }