<?php
namespace Phinx\Db\Adapter;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
use Phinx\Db\Table;
use Phinx\Db\Table\Column;
use Phinx\Migration\MigrationInterface;
/**
* Phinx PDO Adapter.
*
* @author Rob Morgan <robbym@gmail.com>
*/
abstract class PdoAdapter implements AdapterInterface
{
protected $options = array();
protected $input;
protected $output;
protected $schemaTableName = 'migrations';
protected $connection;
protected $commandStartTime;
public function __construct(array $options, InputInterface $input = null, OutputInterface $output = null)
{
$this->setOptions($options);
if (null !== $input) {
$this->setInput($input);
}
if (null !== $output) {
$this->setOutput($output);
}
}
public function setOptions(array $options)
{
$this->options = $options;
if (isset($options['default_migration_table'])) {
$this->setSchemaTableName($options['default_migration_table']);
}
if (isset($options['connection'])) {
$this->setConnection($options['connection']);
}
return $this;
}
public function getOptions()
{
return $this->options;
}
public function hasOption($name)
{
return isset($this->options[$name]);
}
public function getOption($name)
{
if (!$this->hasOption($name)) {
return;
}
return $this->options[$name];
}
public function setInput(InputInterface $input)
{
$this->input = $input;
return $this;
}
public function getInput()
{
return $this->input;
}
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}
public function getOutput()
{
if (null === $this->output) {
$output = new OutputInterface('nothing');
$this->setOutput($output);
}
return $this->output;
}
public function setSchemaTableName($schemaTableName)
{
$this->schemaTableName = $schemaTableName;
return $this;
}
public function getSchemaTableName()
{
return $this->schemaTableName;
}
public function setConnection(\PDO $connection)
{
$this->connection = $connection;
if (!$this->hasSchemaTable()) {
$this->createSchemaTable();
} else {
$table = new Table($this->getSchemaTableName(), array(), $this);
if (!$table->hasColumn('migration_name')) {
$table
->addColumn('migration_name', 'string',
array('limit' => 100, 'after' => 'version', 'default' => null, 'null' => true)
)
->save();
}
if (!$table->hasColumn('breakpoint')) {
$table
->addColumn('breakpoint', 'boolean', array('default' => false))
->save();
}
}
return $this;
}
public function getConnection()
{
if (null === $this->connection) {
$this->connect();
}
return $this->connection;
}
public function setCommandStartTime($time)
{
$this->commandStartTime = $time;
return $this;
}
public function getCommandStartTime()
{
return $this->commandStartTime;
}
public function startCommandTimer()
{
$this->setCommandStartTime(microtime(true));
}
public function endCommandTimer()
{
$end = microtime(true);
if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
$this->getOutput()->writeln(' -> ' . sprintf('%.4fs', $end - $this->getCommandStartTime()));
}
}
public function writeCommand($command, $args = array())
{
if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
if (count($args)) {
$outArr = array();
foreach ($args as $arg) {
if (is_array($arg)) {
$arg = array_map(function ($value) {
return '\'' . $value . '\'';
}, $arg);
$outArr[] = '[' . implode(', ', $arg) . ']';
continue;
}
$outArr[] = '\'' . $arg . '\'';
}
$this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')');
return;
}
$this->getOutput()->writeln(' -- ' . $command);
}
}
public function connect()
{
}
public function disconnect()
{
}
public function execute($sql)
{
return $this->getConnection()->exec($sql);
}
public function query($sql)
{
return $this->getConnection()->query($sql);
}
public function fetchRow($sql)
{
$result = $this->query($sql);
return $result->fetch();
}
public function fetchAll($sql)
{
$rows = array();
$result = $this->query($sql);
while ($row = $result->fetch()) {
$rows[] = $row;
}
return $rows;
}
public function insert(Table $table, $row)
{
$this->startCommandTimer();
$this->writeCommand('insert', array($table->getName()));
$sql = sprintf(
"INSERT INTO %s ",
$this->quoteTableName($table->getName())
);
$columns = array_keys($row);
$sql .= "(". implode(', ', array_map(array($this, 'quoteColumnName'), $columns)) . ")";
$sql .= " VALUES (" . implode(', ', array_fill(0, count($columns), '?')) . ")";
$stmt = $this->getConnection()->prepare($sql);
$stmt->execute(array_values($row));
$this->endCommandTimer();
}
public function getVersions()
{
$rows = $this->getVersionLog();
return array_keys($rows);
}
public function getVersionLog()
{
$result = array();
$rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY version ASC', $this->getSchemaTableName()));
foreach ($rows as $version) {
$result[$version['version']] = $version;
}
return $result;
}
public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime)
{
if (strcasecmp($direction, MigrationInterface::UP) === 0) {
$sql = sprintf(
"INSERT INTO %s (%s, %s, %s, %s, %s) VALUES ('%s', '%s', '%s', '%s', %s);",
$this->getSchemaTableName(),
$this->quoteColumnName('version'),
$this->quoteColumnName('migration_name'),
$this->quoteColumnName('start_time'),
$this->quoteColumnName('end_time'),
$this->quoteColumnName('breakpoint'),
$migration->getVersion(),
substr($migration->getName(), 0, 100),
$startTime,
$endTime,
$this->castToBool(false)
);
$this->query($sql);
} else {
$sql = sprintf(
"DELETE FROM %s WHERE %s = '%s'",
$this->getSchemaTableName(),
$this->quoteColumnName('version'),
$migration->getVersion()
);
$this->query($sql);
}
return $this;
}
public function toggleBreakpoint(MigrationInterface $migration)
{
$this->query(
sprintf(
'UPDATE %1$s SET %2$s = CASE %2$s WHEN %3$s THEN %4$s ELSE %3$s END WHERE %5$s = \'%6$s\';',
$this->getSchemaTableName(),
$this->quoteColumnName('breakpoint'),
$this->castToBool(true),
$this->castToBool(false),
$this->quoteColumnName('version'),
$migration->getVersion()
)
);
return $this;
}
public function resetAllBreakpoints()
{
return $this->execute(
sprintf(
'UPDATE %1$s SET %2$s = %3$s WHERE %2$s <> %3$s;',
$this->getSchemaTableName(),
$this->quoteColumnName('breakpoint'),
$this->castToBool(false)
)
);
}
public function hasSchemaTable()
{
return $this->hasTable($this->getSchemaTableName());
}
public function createSchemaTable()
{
try {
$options = array(
'id' => false,
'primary_key' => 'version'
);
$table = new Table($this->getSchemaTableName(), $options, $this);
if ($this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'mysql'
&& version_compare($this->getConnection()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6.0', '>=')) {
$table->addColumn('version', 'biginteger', array('limit' => 14))
->addColumn('migration_name', 'string', array('limit' => 100, 'default' => null, 'null' => true))
->addColumn('start_time', 'timestamp', array('default' => 'CURRENT_TIMESTAMP'))
->addColumn('end_time', 'timestamp', array('default' => 'CURRENT_TIMESTAMP'))
->addColumn('breakpoint', 'boolean', array('default' => false))
->save();
} else {
$table->addColumn('version', 'biginteger')
->addColumn('migration_name', 'string', array('limit' => 100, 'default' => null, 'null' => true))
->addColumn('start_time', 'timestamp')
->addColumn('end_time', 'timestamp')
->addColumn('breakpoint', 'boolean', array('default' => false))
->save();
}
} catch (\Exception $exception) {
throw new \InvalidArgumentException('There was a problem creating the schema table: ' . $exception->getMessage());
}
}
public function getAdapterType()
{
return $this->getOption('adapter');
}
public function getColumnTypes()
{
return array(
'string',
'char',
'text',
'integer',
'biginteger',
'float',
'decimal',
'datetime',
'timestamp',
'time',
'date',
'blob',
'binary',
'varbinary',
'boolean',
'uuid',
'geometry',
'point',
'linestring',
'polygon',
);
}
public function isValidColumnType(Column $column) {
return in_array($column->getType(), $this->getColumnTypes());
}
public function castToBool($value)
{
return (bool) $value ? 1 : 0;
}
}