|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phpmig\Adapter\PDO; |
| 4 | + |
| 5 | +use Phpmig\Migration\Migration, |
| 6 | + Phpmig\Adapter\AdapterInterface, |
| 7 | + PDO; |
| 8 | + |
| 9 | +/** |
| 10 | + * Simple PDO adapter to work with Postgres SQL database in particular. |
| 11 | + * @author Theodson https://github.com/theodson |
| 12 | + */ |
| 13 | +class SqlPgsql extends Sql |
| 14 | +{ |
| 15 | + private $quote = "\""; |
| 16 | + private $schemaName = "public"; |
| 17 | + |
| 18 | + /** |
| 19 | + * Constructor |
| 20 | + * |
| 21 | + * @param \PDO $connection |
| 22 | + * @param string $tableName |
| 23 | + */ |
| 24 | + public function __construct(\PDO $connection, $tableName, $schemaName = 'public') |
| 25 | + { |
| 26 | + parent::__construct($connection, $tableName); |
| 27 | + $driver = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME); |
| 28 | + $this->quote = in_array($driver, array('mysql', 'pgsql')) ? '"' : '`'; |
| 29 | + $this->schemaName = $schemaName; |
| 30 | + } |
| 31 | + |
| 32 | + private function quotedTableName() |
| 33 | + { |
| 34 | + $sql = "{$this->quote}{$this->schemaName}{$this->quote}.{$this->quote}{$this->tableName}{$this->quote}"; |
| 35 | + return $sql; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Fetch all |
| 40 | + * |
| 41 | + * @return array |
| 42 | + */ |
| 43 | + public function fetchAll() |
| 44 | + { |
| 45 | + $sql = "SELECT {$this->quote}version{$this->quote} FROM {$this->quotedTableName()} ORDER BY {$this->quote}version{$this->quote} ASC"; |
| 46 | + return $this->connection->query($sql, PDO::FETCH_COLUMN, 0)->fetchAll(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Up |
| 51 | + * |
| 52 | + * @param Migration $migration |
| 53 | + * @return self |
| 54 | + */ |
| 55 | + public function up(Migration $migration) |
| 56 | + { |
| 57 | + $sql = "INSERT into {$this->quotedTableName()} (version) VALUES (:version);"; |
| 58 | + $this->connection->prepare($sql) |
| 59 | + ->execute(array(':version' => $migration->getVersion())); |
| 60 | + return $this; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Down |
| 65 | + * |
| 66 | + * @param Migration $migration |
| 67 | + * @return self |
| 68 | + */ |
| 69 | + public function down(Migration $migration) |
| 70 | + { |
| 71 | + $sql = "DELETE from {$this->quotedTableName()} where version = :version"; |
| 72 | + $this->connection->prepare($sql) |
| 73 | + ->execute(array(':version' => $migration->getVersion())); |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * Is the schema ready? |
| 80 | + * |
| 81 | + * @return bool |
| 82 | + */ |
| 83 | + public function hasSchema() |
| 84 | + { |
| 85 | + $tables = $this->connection->query("SELECT table_name FROM information_schema.tables WHERE table_schema = '{$this->schemaName}';"); |
| 86 | + while ($table = $tables->fetchColumn()) { |
| 87 | + if ($table == $this->tableName) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * Create Schema |
| 97 | + * |
| 98 | + * @return DBAL |
| 99 | + */ |
| 100 | + public function createSchema() |
| 101 | + { |
| 102 | + $sql = sprintf("SELECT schema_name FROM {$this->quote}information_schema{$this->quote}.{$this->quote}schemata{$this->quote} WHERE schema_name = '%s';", |
| 103 | + $this->schemaName); |
| 104 | + $pgSchemas = $this->connection->exec($sql); |
| 105 | + |
| 106 | + if (empty($pgSchemas)) { |
| 107 | + $sql = sprintf("CREATE SCHEMA %s;", $this->schemaName); |
| 108 | + if (FALSE === $this->connection->exec($sql)) { |
| 109 | + $e = $this->connection->errorInfo(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + $sql = "CREATE table {$this->quotedTableName()} (version %s NOT NULL, {$this->quote}migrate_date{$this->quote} timestamp(6) WITH TIME ZONE DEFAULT now())"; |
| 114 | + $driver = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME); |
| 115 | + $sql = sprintf($sql, in_array($driver, array('mysql', 'pgsql')) ? 'VARCHAR(255)' : ''); |
| 116 | + |
| 117 | + if (FALSE === $this->connection->exec($sql)) { |
| 118 | + $e = $this->connection->errorInfo(); |
| 119 | + } |
| 120 | + return $this; |
| 121 | + } |
| 122 | + |
| 123 | +} |
| 124 | + |
0 commit comments