*/ abstract class PdoAdapter implements AdapterInterface { /** * @var \PDO */ protected $connection; /** * @var float */ protected $commandStartTime; /** * Class Constructor. * * @param array $options Options * @param InputInterface $input Input Interface * @param OutputInterface $output Output Interface */ public function __construct() { } /** * Sets the database connection. * * @param \PDO $connection Connection * @return AdapterInterface */ public function setConnection(\PDO $connection) { $this->connection = $connection; return $this; } /** * Gets the database connection * * @return \PDO */ public function getConnection() { if (null === $this->connection) { $this->connect(); } return $this->connection; } /** * Gets the table prefix * * @return \PDO */ public function getTablePrefix() { if(isset($this->connection->prefix)){ return $this->connection->prefix; } return ''; } /** * Sets the command start time * * @param int $time * @return AdapterInterface */ public function setCommandStartTime($time) { $this->commandStartTime = $time; return $this; } /** * Gets the command start time * * @return int */ public function getCommandStartTime() { return $this->commandStartTime; } /** * Start timing a command. * * @return void */ public function startCommandTimer() { $this->setCommandStartTime(microtime(true)); } /** * Stop timing the current command and write the elapsed time to the * output. * * @return void */ public function endCommandTimer() { return; $end = microtime(true); if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) { $this->getOutput()->writeln(' -> ' . sprintf('%.4fs', $end - $this->getCommandStartTime())); } } /** * Write a Phinx command to the output. * * @param string $command Command Name * @param array $args Command Args * @return void */ public function writeCommand($command, $args = array()) { return ''; 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); } } /** * {@inheritdoc} */ public function connect() { } /** * {@inheritdoc} */ public function disconnect() { } /** * {@inheritdoc} */ public function execute($sql) { // $sql; return $this->getConnection()->exec($sql); } /** * {@inheritdoc} */ public function query($sql) { return $this->getConnection()->query($sql); } /** * {@inheritdoc} */ public function fetchRow($sql) { $result = $this->query($sql); return $result->fetch(); } /** * {@inheritdoc} */ public function fetchAll($sql) { $rows = array(); $result = $this->query($sql); while ($row = $result->fetch()) { $rows[] = $row; } return $rows; } /** * {@inheritdoc} */ 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(); } /** * {@inheritdoc} */ public function getAdapterType() { return $this->getOption('adapter'); } /** * {@inheritdoc} */ public function getColumnTypes() { return array( 'string', 'char', 'text', 'integer', 'biginteger', 'float', 'decimal', 'datetime', 'timestamp', 'time', 'date', 'blob', 'binary', 'varbinary', 'boolean', 'uuid', // Geospatial data types 'geometry', 'point', 'linestring', 'polygon', ); } /** * {@inheritdoc} */ public function isValidColumnType(Column $column) { return in_array($column->getType(), $this->getColumnTypes()); } /** * Cast a value to a boolean appropriate for the adapter. * * @param mixed $value The value to be cast * * @return mixed */ public function castToBool($value) { return (bool) $value ? 1 : 0; } }