/** * Refresh the protected foreign keys variable. * All foreign keys are removed from the original constraints. * * @return void */ protected function _extractForeignKeys(Connection $connection) { $dialect = $connection->driver()->schemaDialect(); foreach ($this->_constraints as $name => $attrs) { if ($attrs['type'] === static::CONSTRAINT_FOREIGN) { $this->_foreignKeys[$name] = $attrs; $this->_foreignKeysSql[$name] = $dialect->constraintSql($this, $name); unset($this->_constraints[$name]); } } }
/** * Set all sequence's current value to the lowest available field value. * * @return bool */ public function sequences() { $this->out(sprintf('%s - %s', date('H:i:s'), 'Set all sequence\'s current values')); $success = $this->connection->begin() !== false; $schema = Hash::get($this->connection->config(), 'schema') ?: 'public'; $conditions = ["table_schema = '{$schema}'"]; foreach ($this->connection->driver()->sequences($conditions) as $sequence) { $sequence['sequence'] = preg_replace('/^nextval\\(\'(.*)\'.*\\)$/', '\\1', $sequence['sequence']); $sql = "SELECT setval('{$sequence['sequence']}', COALESCE(MAX({$sequence['column']}),0)+1, false) FROM {$sequence['table']};"; $success = $success && $this->connection->query($sql)->fetchAll('assoc') !== false; } if ($success) { $success = $this->connection->commit() !== false && $success; } else { $success = $this->connection->rollback() !== false && $success; } if ($this->command === __FUNCTION__) { $this->_stop($success ? self::SUCCESS : self::ERROR); } return $success; }
/** * Constructor * * @param \Phinx\Db\Adapter\AdapterInterface $adapter The original adapter to decorate. * @param \Cake\Database\Connection $connection The connection to actually use. */ public function __construct(AdapterInterface $adapter, Connection $connection) { $this->adapter = $adapter; $this->connection = $connection; $pdo = $adapter->getConnection(); $connection->driver()->connection($pdo); }
/** * Constructor. * * @param \Cake\Database\Connection $connection */ public function __construct(Connection $connection) { $this->_connection = $connection; $this->_dialect = $connection->driver()->schemaDialect(); $config = $this->_connection->config(); if (!empty($config['cacheMetadata'])) { $this->cacheMetadata(true); } }
/** * Constructor * * @param \Phinx\Db\Adapter\AdapterInterface $adapter The original adapter to decorate. * @param \Cake\Database\Connection $connection The connection to actually use. */ public function __construct(AdapterInterface $adapter, Connection $connection) { $this->adapter = $adapter; $this->connection = $connection; $pdo = $adapter->getConnection(); if ($pdo->getAttribute(PDO::ATTR_ERRMODE) !== PDO::ERRMODE_EXCEPTION) { $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } $connection->driver()->connection($pdo); }
/** * Tests that the `driver` option supports the short classname/plugin syntax. * * @return void */ public function testDriverOptionClassNameSupport() { $connection = new Connection(['driver' => 'TestDriver']); $this->assertInstanceOf('\\TestApp\\Database\\Driver\\TestDriver', $connection->driver()); $connection = new Connection(['driver' => 'TestPlugin.TestDriver']); $this->assertInstanceOf('\\TestPlugin\\Database\\Driver\\TestDriver', $connection->driver()); list(, $name) = namespaceSplit(get_class($this->connection->driver())); $connection = new Connection(['driver' => $name]); $this->assertInstanceOf(get_class($this->connection->driver()), $connection->driver()); }
/** * Constructor. * * @param \Cake\Database\Connection $connection The connection instance. */ public function __construct(Connection $connection) { $this->_connection = $connection; $this->_dialect = $connection->driver()->schemaDialect(); }
/** * Generates SQL statements dropping foreign keys for the table. * * @param \Cake\Database\Connection $db Connection to run the SQL queries on. * @param \Cake\Database\Schema\Table $table Drop foreign keys for this table. * @return array List of SQL statements dropping foreign keys. */ protected function _generateDropForeignKeys($db, Schema $table) { $type = 'other'; if ($db->driver() instanceof Mysql) { $type = 'mysql'; } $queries = []; foreach ($table->constraints() as $constraintName) { $constraint = $table->constraint($constraintName); if ($constraint['type'] === Schema::CONSTRAINT_FOREIGN) { // TODO: Move this into the driver if ($type === 'mysql') { $template = 'ALTER TABLE %s DROP FOREIGN KEY %s'; } else { $template = 'ALTER TABLE %s DROP CONSTRAINT %s'; } $queries[] = sprintf($template, $table->name(), $constraintName); } } return $queries; }
/** * Generate the SQL statements to truncate a table * * @param Connection $connection The connection to generate SQL for. * @return array SQL to drop a table. */ public function truncateSql(Connection $connection) { $dialect = $connection->driver()->schemaDialect(); return $dialect->truncateTableSql($this); }
/** * Builds oracle connection based on generic cakephp connection class. * * @param \Cake\Database\Connection $connection Connection object. * @return OracleConnection */ public static function build(Connection $connection) { $config = $connection->config(); $config['driver'] = $connection->driver(); return new OracleConnection($config); }
/** * Constructor * * @param \Phinx\Db\Adapter\AdapterInterface $adapter The original adapter to decorate. * @param \Cake\Database\Connection $connection The connection to actually use. */ public function __construct(AdapterInterface $adapter, Connection $connection) { $this->adapter = $adapter; $this->connection = $connection; $pdo = $adapter->getConnection(); if ($pdo->getAttribute(PDO::ATTR_ERRMODE) !== PDO::ERRMODE_EXCEPTION) { $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } $connection->cacheMetadata(false); if ($connection->driver() instanceof Postgres) { $config = $connection->config(); $schema = empty($config['schema']) ? 'public' : $config['schema']; $pdo->exec('SET search_path TO ' . $schema); } $connection->driver()->connection($pdo); }