/** * Create connection * * $dbh = new Connection('mysql:host=localhost;dbname=test', $user, $pass); * * $pdo = new Connection( * 'mysql:host=hostname;dbname=defaultDbName', * 'username', * 'password', * array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") * ); * * $dbh = new Connection('pgsql:dbname=$dbname; host=$host; username=$username; password=$password'); * $pdo = new Connection( 'sqlite::memory:', null, null, array(PDO::ATTR_PERSISTENT => true) ); * sqlite2:mydb.sq2 * */ public function getConnection($sourceId = 'default') { // use cached connection objects if (isset($this->conns[$sourceId])) { return $this->conns[$sourceId]; } if (!isset($this->datasources[$sourceId])) { throw new UndefinedDataSourceException("data source {$sourceId} not found."); } $config = $this->datasources[$sourceId]; $conn = new Connection($config['dsn'], $config['user'], $config['pass'], $config['connection_options']); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // TODO: can we make this optional ? return $this->conns[$sourceId] = $conn; }
/** * Create connection * * $dbh = new Connection('mysql:host=localhost;dbname=test', $user, $pass); * * $pdo = new Connection( * 'mysql:host=hostname;dbname=defaultDbName', * 'username', * 'password', * array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") * ); * * $dbh = new Connection('pgsql:dbname=$dbname; host=$host; username=$username; password=$password'); * $pdo = new Connection( 'sqlite::memory:', null, null, array(PDO::ATTR_PERSISTENT => true) ); * sqlite2:mydb.sq2 * */ public function getConnection($sourceId) { if ($sourceId === 'default' && $this->config) { if (!isset($this->datasources[$sourceId])) { $sourceId = $this->config->getDefaultDataSourceId(); } } // use cached connection objects if (isset($this->conns[$sourceId])) { return $this->conns[$sourceId]; } if (!isset($this->datasources[$sourceId])) { throw new UndefinedDataSourceException("data source {$sourceId} not found."); } $config = $this->datasources[$sourceId]; $conn = new Connection($config['dsn'], $config['user'], $config['pass'], $config['connection_options']); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // TODO: can we make this optional ? // $conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); return $this->conns[$sourceId] = $conn; }