| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- /**
- * Phinx
- *
- * (The MIT license)
- * Copyright (c) 2015 Rob Morgan
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated * documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- *
- * @package Phinx
- * @subpackage Phinx\Db\Adapter
- */
- namespace Schema\Db\Adapter;
- use Schema\Db\Table;
- use Schema\Db\Table\Column;
- /**
- * Phinx PDO Adapter.
- *
- * @author Rob Morgan <robbym@gmail.com>
- */
- 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;
- }
- }
|