| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <?php
- /**
- * PHP Version 5.4
- *
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since CakePHP(tm) v 3.0.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- namespace Cake\Database\Schema;
- use Cake\Database\Exception;
- use Cake\Database\Schema\Table;
- /**
- * Schema management/reflection features for Postgres.
- */
- class PostgresSchema extends BaseSchema {
- /**
- * {@inheritdoc}
- *
- */
- public function listTablesSql($config) {
- $sql = 'SELECT table_name as name FROM information_schema.tables WHERE table_schema = ? ORDER BY name';
- $schema = empty($config['schema']) ? 'public' : $config['schema'];
- return [$sql, [$schema]];
- }
- /**
- * {@inheritdoc}
- *
- */
- public function describeTableSql($name, $config) {
- $sql =
- 'SELECT DISTINCT table_schema AS schema, column_name AS name, data_type AS type,
- is_nullable AS null, column_default AS default,
- character_maximum_length AS char_length,
- d.description as comment,
- ordinal_position
- FROM information_schema.columns c
- INNER JOIN pg_catalog.pg_namespace ns ON (ns.nspname = table_schema)
- INNER JOIN pg_catalog.pg_class cl ON (cl.relnamespace = ns.oid AND cl.relname = table_name)
- LEFT JOIN pg_catalog.pg_index i ON (i.indrelid = cl.oid AND i.indkey[0] = c.ordinal_position)
- LEFT JOIN pg_catalog.pg_description d on (cl.oid = d.objoid AND d.objsubid = c.ordinal_position)
- WHERE table_name = ? AND table_schema = ? AND table_catalog = ?
- ORDER BY ordinal_position';
- $schema = empty($config['schema']) ? 'public' : $config['schema'];
- return [$sql, [$name, $schema, $config['database']]];
- }
- /**
- * Convert a column definition to the abstract types.
- *
- * The returned type will be a type that
- * Cake\Database\Type can handle.
- *
- * @param string $column The column type + length
- * @throws Cake\Database\Exception when column cannot be parsed.
- * @return array Array of column information.
- */
- protected function _convertColumn($column) {
- preg_match('/([a-z\s]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
- if (empty($matches)) {
- throw new Exception(__d('cake_dev', 'Unable to parse column type from "%s"', $column));
- }
- $col = strtolower($matches[1]);
- $length = null;
- if (isset($matches[2])) {
- $length = (int)$matches[2];
- }
- if (in_array($col, array('date', 'time', 'boolean'))) {
- return ['type' => $col, 'length' => null];
- }
- if (strpos($col, 'timestamp') !== false) {
- return ['type' => 'datetime', 'length' => null];
- }
- if ($col === 'serial' || $col === 'integer') {
- return ['type' => 'integer', 'length' => 10];
- }
- if ($col === 'bigserial' || $col === 'bigint') {
- return ['type' => 'biginteger', 'length' => 20];
- }
- if ($col === 'smallint') {
- return ['type' => 'integer', 'length' => 5];
- }
- if ($col === 'inet') {
- return ['type' => 'string', 'length' => 39];
- }
- if ($col === 'uuid') {
- return ['type' => 'string', 'fixed' => true, 'length' => 36];
- }
- if ($col === 'char' || $col === 'character') {
- return ['type' => 'string', 'fixed' => true, 'length' => $length];
- }
- if (strpos($col, 'char') !== false) {
- return ['type' => 'string', 'length' => $length];
- }
- if (strpos($col, 'text') !== false) {
- return ['type' => 'text', 'length' => null];
- }
- if ($col === 'bytea') {
- return ['type' => 'binary', 'length' => null];
- }
- if ($col === 'real' || strpos($col, 'double') !== false) {
- return ['type' => 'float', 'length' => null];
- }
- if (
- strpos($col, 'numeric') !== false ||
- strpos($col, 'money') !== false ||
- strpos($col, 'decimal') !== false
- ) {
- return ['type' => 'decimal', 'length' => null];
- }
- return ['type' => 'text', 'length' => null];
- }
- /**
- * {@inheritdoc}
- *
- */
- public function convertFieldDescription(Table $table, $row) {
- $field = $this->_convertColumn($row['type']);
- if ($field['type'] === 'boolean') {
- if ($row['default'] === 'true') {
- $row['default'] = 1;
- }
- if ($row['default'] === 'false') {
- $row['default'] = 0;
- }
- }
- $field += [
- 'null' => $row['null'] === 'YES' ? true : false,
- 'default' => $row['default'],
- 'comment' => $row['comment']
- ];
- $field['length'] = $row['char_length'] ?: $field['length'];
- $table->addColumn($row['name'], $field);
- }
- /**
- * {@inheritdoc}
- *
- */
- public function describeIndexSql($table, $config) {
- $sql = 'SELECT
- c2.relname,
- i.indisprimary,
- i.indisunique,
- i.indisvalid,
- pg_catalog.pg_get_indexdef(i.indexrelid, 0, true) AS statement
- FROM pg_catalog.pg_class AS c,
- pg_catalog.pg_class AS c2,
- pg_catalog.pg_index AS i
- WHERE c.oid = (
- SELECT c.oid
- FROM pg_catalog.pg_class c
- LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
- WHERE c.relname = ?
- AND pg_catalog.pg_table_is_visible(c.oid)
- AND n.nspname = ?
- )
- AND c.oid = i.indrelid
- AND i.indexrelid = c2.oid
- ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname';
- $schema = 'public';
- if (!empty($config['schema'])) {
- $schema = $config['schema'];
- }
- return [$sql, [$table, $schema]];
- }
- /**
- * {@inheritdoc}
- *
- */
- public function convertIndexDescription(Table $table, $row) {
- $type = Table::INDEX_INDEX;
- $name = $row['relname'];
- if ($row['indisprimary']) {
- $name = $type = Table::CONSTRAINT_PRIMARY;
- }
- if ($row['indisunique'] && $type === Table::INDEX_INDEX) {
- $type = Table::CONSTRAINT_UNIQUE;
- }
- preg_match('/\(([^\)]+)\)/', $row['statement'], $matches);
- $columns = explode(', ', $matches[1]);
- if ($type === Table::CONSTRAINT_PRIMARY || $type === Table::CONSTRAINT_UNIQUE) {
- $table->addConstraint($name, [
- 'type' => $type,
- 'columns' => $columns
- ]);
- return;
- }
- $table->addIndex($name, [
- 'type' => $type,
- 'columns' => $columns
- ]);
- }
- /**
- * {@inheritdoc}
- *
- */
- public function describeForeignKeySql($table, $config) {
- $sql = "SELECT
- r.conname AS name,
- r.confupdtype AS update_type,
- r.confdeltype AS delete_type,
- pg_catalog.pg_get_constraintdef(r.oid, true) AS definition
- FROM pg_catalog.pg_constraint AS r
- WHERE r.conrelid = (
- SELECT c.oid
- FROM pg_catalog.pg_class AS c,
- pg_catalog.pg_namespace AS n
- WHERE c.relname = ?
- AND n.nspname = ?
- AND n.oid = c.relnamespace
- )
- AND r.contype = 'f'";
- $schema = empty($config['schema']) ? 'public' : $config['schema'];
- return [$sql, [$table, $schema]];
- }
- /**
- * {@inheritdoc}
- *
- */
- public function convertForeignKeyDescription(Table $table, $row) {
- preg_match('/REFERENCES ([^\)]+)\(([^\)]+)\)/', $row['definition'], $matches);
- $tableName = $matches[1];
- $column = $matches[2];
- preg_match('/FOREIGN KEY \(([^\)]+)\) REFERENCES/', $row['definition'], $matches);
- $columns = explode(',', $matches[1]);
- $data = [
- 'type' => Table::CONSTRAINT_FOREIGN,
- 'columns' => $columns,
- 'references' => [$tableName, $column],
- 'update' => $this->_convertOnClause($row['update_type']),
- 'delete' => $this->_convertOnClause($row['delete_type']),
- ];
- $name = $row['name'];
- $table->addConstraint($name, $data);
- }
- /**
- * {@inheritdoc}
- *
- */
- protected function _convertOnClause($clause) {
- if ($clause === 'r') {
- return Table::ACTION_RESTRICT;
- }
- if ($clause === 'a') {
- return Table::ACTION_NO_ACTION;
- }
- if ($clause === 'c') {
- return Table::ACTION_CASCADE;
- }
- return Table::ACTION_SET_NULL;
- }
- /**
- * {@inheritdoc}
- *
- */
- public function columnSql(Table $table, $name) {
- $data = $table->column($name);
- $out = $this->_driver->quoteIdentifier($name);
- $typeMap = [
- 'biginteger' => ' BIGINT',
- 'boolean' => ' BOOLEAN',
- 'binary' => ' BYTEA',
- 'float' => ' FLOAT',
- 'decimal' => ' DECIMAL',
- 'text' => ' TEXT',
- 'date' => ' DATE',
- 'time' => ' TIME',
- 'datetime' => ' TIMESTAMP',
- 'timestamp' => ' TIMESTAMP',
- ];
- if (isset($typeMap[$data['type']])) {
- $out .= $typeMap[$data['type']];
- }
- if ($data['type'] === 'integer') {
- $type = ' INTEGER';
- if (in_array($name, (array)$table->primaryKey())) {
- $type = ' SERIAL';
- unset($data['null'], $data['default']);
- }
- $out .= $type;
- }
- if ($data['type'] === 'string') {
- $isFixed = !empty($data['fixed']);
- $type = ' VARCHAR';
- if ($isFixed) {
- $type = ' CHAR';
- }
- if ($isFixed && isset($data['length']) && $data['length'] == 36) {
- $type = ' UUID';
- }
- $out .= $type;
- if (isset($data['length']) && $data['length'] != 36) {
- $out .= '(' . (int)$data['length'] . ')';
- }
- }
- if ($data['type'] === 'float' && isset($data['precision'])) {
- $out .= '(' . (int)$data['precision'] . ')';
- }
- if ($data['type'] === 'decimal' &&
- (isset($data['length']) || isset($data['precision']))
- ) {
- $out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
- }
- if (isset($data['null']) && $data['null'] === false) {
- $out .= ' NOT NULL';
- }
- if (isset($data['null']) && $data['null'] === true) {
- $out .= ' DEFAULT NULL';
- unset($data['default']);
- }
- if (isset($data['default']) && $data['type'] !== 'timestamp') {
- $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
- }
- return $out;
- }
- /**
- * {@inheritdoc}
- *
- */
- public function indexSql(Table $table, $name) {
- $data = $table->index($name);
- $columns = array_map(
- [$this->_driver, 'quoteIdentifier'],
- $data['columns']
- );
- return sprintf('CREATE INDEX %s ON %s (%s)',
- $this->_driver->quoteIdentifier($name),
- $this->_driver->quoteIdentifier($table->name()),
- implode(', ', $columns)
- );
- }
- /**
- * {@inheritdoc}
- *
- */
- public function constraintSql(Table $table, $name) {
- $data = $table->constraint($name);
- $out = 'CONSTRAINT ' . $this->_driver->quoteIdentifier($name);
- if ($data['type'] === Table::CONSTRAINT_PRIMARY) {
- $out = 'PRIMARY KEY';
- }
- if ($data['type'] === Table::CONSTRAINT_UNIQUE) {
- $out .= ' UNIQUE';
- }
- return $this->_keySql($out, $data);
- }
- /**
- * Helper method for generating key SQL snippets.
- *
- * @param string $prefix The key prefix
- * @param array $data Key data.
- * @return string
- */
- protected function _keySql($prefix, $data) {
- $columns = array_map(
- [$this->_driver, 'quoteIdentifier'],
- $data['columns']
- );
- if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
- return $prefix . sprintf(
- ' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
- implode(', ', $columns),
- $this->_driver->quoteIdentifier($data['references'][0]),
- $this->_driver->quoteIdentifier($data['references'][1]),
- $this->_foreignOnClause($data['update']),
- $this->_foreignOnClause($data['delete'])
- );
- }
- return $prefix . ' (' . implode(', ', $columns) . ')';
- }
- /**
- * {@inheritdoc}
- *
- */
- public function createTableSql(Table $table, $columns, $constraints, $indexes) {
- $content = array_merge($columns, $constraints);
- $content = implode(",\n", array_filter($content));
- $tableName = $this->_driver->quoteIdentifier($table->name());
- $out = [];
- $out[] = sprintf("CREATE TABLE %s (\n%s\n)", $tableName, $content);
- foreach ($indexes as $index) {
- $out[] = $index;
- }
- foreach ($table->columns() as $column) {
- $columnData = $table->column($column);
- if (isset($columnData['comment'])) {
- $out[] = sprintf('COMMENT ON COLUMN %s.%s IS %s',
- $tableName,
- $this->_driver->quoteIdentifier($column),
- $this->_driver->schemaValue($columnData['comment'])
- );
- }
- }
- return $out;
- }
- /**
- * {@inheritdoc}
- *
- */
- public function truncateTableSql(Table $table) {
- $name = $this->_driver->quoteIdentifier($table->name());
- return [
- sprintf('TRUNCATE %s RESTART IDENTITY', $name)
- ];
- }
- }
|