public function __construct(Connection $conn, BaseBuilder $builder = null, Logger $logger = null) { $this->conn = $conn; $this->queryDriver = $conn->createQueryDriver(); if (!$builder) { $builder = SqlBuilder::create($this->queryDriver); } $this->builder = $builder; if (!$logger) { $c = ServiceContainer::getInstance(); $logger ?: $c['logger']; } $this->logger = $logger; }
public function testBackup() { $this->conn->query('DROP DATABASE IF EXISTS backup_test'); $this->conn->query('CREATE DATABASE IF NOT EXISTS backup_test CHARSET utf8;'); $dest = Connection::create(['dsn' => 'mysql:host=localhost;dbname=backup_test', 'user' => 'root', 'pass' => null, 'connection_options' => [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']]); $backup = new MySQLBackup(); $backup->backup($this->conn, $dest); }
public function backupToDatabase(Connection $source, $databaseName, $dropAndCreate = false) { $newDSN = clone $source->getDSN(); if ($newDSN->getAttribute('dbname') == $databaseName) { throw new LogicException('Backup to the same database.'); } if ($dropAndCreate) { $source->query('DROP DATABASE IF EXISTS ' . $databaseName); $source->query('CREATE DATABASE IF NOT EXISTS ' . $databaseName . ' CHARSET utf8'); } // Create new dest database connection $newConfig = $source->getConfig(); $newDSN->setAttribute('dbname', $databaseName); $newConfig['dsn'] = $newDSN->__toString(); $dest = Connection::create($newConfig); return $this->backup($source, $dest); }
/** * 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; }
/** * 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) { $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."); } // Only for MySQl // $conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); return $this->conns[$sourceId] = Connection::create($this->datasources[$sourceId]); }
public function runUpgradeAutomatically(Connection $conn, BaseDriver $driver, array $schemas, OptionResult $options = null) { $script = new AutomaticMigration($conn, $driver, $this->logger, $options); try { $this->logger->info('Begining transaction...'); $conn->beginTransaction(); // where to find the schema? $script->upgrade($schemas); $this->logger->info('Committing...'); $conn->commit(); } catch (Exception $e) { $this->logger->error('Exception was thrown: ' . $e->getMessage()); $this->logger->warn('Rolling back ...'); $conn->rollback(); $this->logger->warn('Recovered, escaping...'); throw $e; } }