PostgreSqlSchemaManager.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. namespace Doctrine\DBAL\Schema;
  3. use Doctrine\DBAL\Exception\DriverException;
  4. use Doctrine\DBAL\FetchMode;
  5. use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
  6. use Doctrine\DBAL\Types\Type;
  7. use Doctrine\DBAL\Types\Types;
  8. use function array_change_key_case;
  9. use function array_filter;
  10. use function array_keys;
  11. use function array_map;
  12. use function array_shift;
  13. use function assert;
  14. use function explode;
  15. use function implode;
  16. use function in_array;
  17. use function preg_match;
  18. use function preg_replace;
  19. use function sprintf;
  20. use function str_replace;
  21. use function strpos;
  22. use function strtolower;
  23. use function trim;
  24. use const CASE_LOWER;
  25. /**
  26. * PostgreSQL Schema Manager.
  27. */
  28. class PostgreSqlSchemaManager extends AbstractSchemaManager
  29. {
  30. /** @var string[]|null */
  31. private $existingSchemaPaths;
  32. /**
  33. * Gets all the existing schema names.
  34. *
  35. * @return string[]
  36. */
  37. public function getSchemaNames()
  38. {
  39. $statement = $this->_conn->executeQuery(
  40. "SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' AND nspname != 'information_schema'"
  41. );
  42. return $statement->fetchAll(FetchMode::COLUMN);
  43. }
  44. /**
  45. * Returns an array of schema search paths.
  46. *
  47. * This is a PostgreSQL only function.
  48. *
  49. * @return string[]
  50. */
  51. public function getSchemaSearchPaths()
  52. {
  53. $params = $this->_conn->getParams();
  54. $searchPaths = $this->_conn->fetchColumn('SHOW search_path');
  55. assert($searchPaths !== false);
  56. $schema = explode(',', $searchPaths);
  57. if (isset($params['user'])) {
  58. $schema = str_replace('"$user"', $params['user'], $schema);
  59. }
  60. return array_map('trim', $schema);
  61. }
  62. /**
  63. * Gets names of all existing schemas in the current users search path.
  64. *
  65. * This is a PostgreSQL only function.
  66. *
  67. * @return string[]
  68. */
  69. public function getExistingSchemaSearchPaths()
  70. {
  71. if ($this->existingSchemaPaths === null) {
  72. $this->determineExistingSchemaSearchPaths();
  73. }
  74. assert($this->existingSchemaPaths !== null);
  75. return $this->existingSchemaPaths;
  76. }
  77. /**
  78. * Sets or resets the order of the existing schemas in the current search path of the user.
  79. *
  80. * This is a PostgreSQL only function.
  81. *
  82. * @return void
  83. */
  84. public function determineExistingSchemaSearchPaths()
  85. {
  86. $names = $this->getSchemaNames();
  87. $paths = $this->getSchemaSearchPaths();
  88. $this->existingSchemaPaths = array_filter($paths, static function ($v) use ($names) {
  89. return in_array($v, $names);
  90. });
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function dropDatabase($database)
  96. {
  97. try {
  98. parent::dropDatabase($database);
  99. } catch (DriverException $exception) {
  100. // If we have a SQLSTATE 55006, the drop database operation failed
  101. // because of active connections on the database.
  102. // To force dropping the database, we first have to close all active connections
  103. // on that database and issue the drop database operation again.
  104. if ($exception->getSQLState() !== '55006') {
  105. throw $exception;
  106. }
  107. assert($this->_platform instanceof PostgreSqlPlatform);
  108. $this->_execSql(
  109. [
  110. $this->_platform->getDisallowDatabaseConnectionsSQL($database),
  111. $this->_platform->getCloseActiveDatabaseConnectionsSQL($database),
  112. ]
  113. );
  114. parent::dropDatabase($database);
  115. }
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  121. {
  122. $onUpdate = null;
  123. $onDelete = null;
  124. $localColumns = [];
  125. $foreignColumns = [];
  126. $foreignTable = null;
  127. if (preg_match('(ON UPDATE ([a-zA-Z0-9]+( (NULL|ACTION|DEFAULT))?))', $tableForeignKey['condef'], $match)) {
  128. $onUpdate = $match[1];
  129. }
  130. if (preg_match('(ON DELETE ([a-zA-Z0-9]+( (NULL|ACTION|DEFAULT))?))', $tableForeignKey['condef'], $match)) {
  131. $onDelete = $match[1];
  132. }
  133. $result = preg_match('/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)/', $tableForeignKey['condef'], $values);
  134. assert($result === 1);
  135. // PostgreSQL returns identifiers that are keywords with quotes, we need them later, don't get
  136. // the idea to trim them here.
  137. $localColumns = array_map('trim', explode(',', $values[1]));
  138. $foreignColumns = array_map('trim', explode(',', $values[3]));
  139. $foreignTable = $values[2];
  140. return new ForeignKeyConstraint(
  141. $localColumns,
  142. $foreignTable,
  143. $foreignColumns,
  144. $tableForeignKey['conname'],
  145. ['onUpdate' => $onUpdate, 'onDelete' => $onDelete]
  146. );
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. protected function _getPortableTriggerDefinition($trigger)
  152. {
  153. return $trigger['trigger_name'];
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. protected function _getPortableViewDefinition($view)
  159. {
  160. return new View($view['schemaname'] . '.' . $view['viewname'], $view['definition']);
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. protected function _getPortableUserDefinition($user)
  166. {
  167. return [
  168. 'user' => $user['usename'],
  169. 'password' => $user['passwd'],
  170. ];
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. protected function _getPortableTableDefinition($table)
  176. {
  177. $schemas = $this->getExistingSchemaSearchPaths();
  178. $firstSchema = array_shift($schemas);
  179. if ($table['schema_name'] === $firstSchema) {
  180. return $table['table_name'];
  181. }
  182. return $table['schema_name'] . '.' . $table['table_name'];
  183. }
  184. /**
  185. * {@inheritdoc}
  186. *
  187. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  188. */
  189. protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
  190. {
  191. $buffer = [];
  192. foreach ($tableIndexes as $row) {
  193. $colNumbers = array_map('intval', explode(' ', $row['indkey']));
  194. $columnNameSql = sprintf(
  195. 'SELECT attnum, attname FROM pg_attribute WHERE attrelid=%d AND attnum IN (%s) ORDER BY attnum ASC',
  196. $row['indrelid'],
  197. implode(' ,', $colNumbers)
  198. );
  199. $indexColumns = $this->_conn->fetchAllAssociative($columnNameSql);
  200. // required for getting the order of the columns right.
  201. foreach ($colNumbers as $colNum) {
  202. foreach ($indexColumns as $colRow) {
  203. if ($colNum !== $colRow['attnum']) {
  204. continue;
  205. }
  206. $buffer[] = [
  207. 'key_name' => $row['relname'],
  208. 'column_name' => trim($colRow['attname']),
  209. 'non_unique' => ! $row['indisunique'],
  210. 'primary' => $row['indisprimary'],
  211. 'where' => $row['where'],
  212. ];
  213. }
  214. }
  215. }
  216. return parent::_getPortableTableIndexesList($buffer, $tableName);
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. protected function _getPortableDatabaseDefinition($database)
  222. {
  223. return $database['datname'];
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. protected function _getPortableSequencesList($sequences)
  229. {
  230. $sequenceDefinitions = [];
  231. foreach ($sequences as $sequence) {
  232. if ($sequence['schemaname'] !== 'public') {
  233. $sequenceName = $sequence['schemaname'] . '.' . $sequence['relname'];
  234. } else {
  235. $sequenceName = $sequence['relname'];
  236. }
  237. $sequenceDefinitions[$sequenceName] = $sequence;
  238. }
  239. $list = [];
  240. foreach ($this->filterAssetNames(array_keys($sequenceDefinitions)) as $sequenceName) {
  241. $list[] = $this->_getPortableSequenceDefinition($sequenceDefinitions[$sequenceName]);
  242. }
  243. return $list;
  244. }
  245. /**
  246. * {@inheritdoc}
  247. */
  248. protected function getPortableNamespaceDefinition(array $namespace)
  249. {
  250. return $namespace['nspname'];
  251. }
  252. /**
  253. * {@inheritdoc}
  254. */
  255. protected function _getPortableSequenceDefinition($sequence)
  256. {
  257. if ($sequence['schemaname'] !== 'public') {
  258. $sequenceName = $sequence['schemaname'] . '.' . $sequence['relname'];
  259. } else {
  260. $sequenceName = $sequence['relname'];
  261. }
  262. if (! isset($sequence['increment_by'], $sequence['min_value'])) {
  263. /** @var string[] $data */
  264. $data = $this->_conn->fetchAssoc(
  265. 'SELECT min_value, increment_by FROM ' . $this->_platform->quoteIdentifier($sequenceName)
  266. );
  267. $sequence += $data;
  268. }
  269. return new Sequence($sequenceName, (int) $sequence['increment_by'], (int) $sequence['min_value']);
  270. }
  271. /**
  272. * {@inheritdoc}
  273. */
  274. protected function _getPortableTableColumnDefinition($tableColumn)
  275. {
  276. $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
  277. if (strtolower($tableColumn['type']) === 'varchar' || strtolower($tableColumn['type']) === 'bpchar') {
  278. // get length from varchar definition
  279. $length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']);
  280. $tableColumn['length'] = $length;
  281. }
  282. $matches = [];
  283. $autoincrement = false;
  284. if (
  285. $tableColumn['default'] !== null
  286. && preg_match("/^nextval\('(.*)'(::.*)?\)$/", $tableColumn['default'], $matches) === 1
  287. ) {
  288. $tableColumn['sequence'] = $matches[1];
  289. $tableColumn['default'] = null;
  290. $autoincrement = true;
  291. }
  292. if ($tableColumn['default'] !== null) {
  293. if (preg_match("/^['(](.*)[')]::/", $tableColumn['default'], $matches) === 1) {
  294. $tableColumn['default'] = $matches[1];
  295. } elseif (preg_match('/^NULL::/', $tableColumn['default']) === 1) {
  296. $tableColumn['default'] = null;
  297. }
  298. }
  299. $length = $tableColumn['length'] ?? null;
  300. if ($length === '-1' && isset($tableColumn['atttypmod'])) {
  301. $length = $tableColumn['atttypmod'] - 4;
  302. }
  303. if ((int) $length <= 0) {
  304. $length = null;
  305. }
  306. $fixed = null;
  307. if (! isset($tableColumn['name'])) {
  308. $tableColumn['name'] = '';
  309. }
  310. $precision = null;
  311. $scale = null;
  312. $jsonb = null;
  313. $dbType = strtolower($tableColumn['type']);
  314. if (
  315. $tableColumn['domain_type'] !== null
  316. && $tableColumn['domain_type'] !== ''
  317. && ! $this->_platform->hasDoctrineTypeMappingFor($tableColumn['type'])
  318. ) {
  319. $dbType = strtolower($tableColumn['domain_type']);
  320. $tableColumn['complete_type'] = $tableColumn['domain_complete_type'];
  321. }
  322. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  323. $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
  324. $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
  325. switch ($dbType) {
  326. case 'smallint':
  327. case 'int2':
  328. $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
  329. $length = null;
  330. break;
  331. case 'int':
  332. case 'int4':
  333. case 'integer':
  334. $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
  335. $length = null;
  336. break;
  337. case 'bigint':
  338. case 'int8':
  339. $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
  340. $length = null;
  341. break;
  342. case 'bool':
  343. case 'boolean':
  344. if ($tableColumn['default'] === 'true') {
  345. $tableColumn['default'] = true;
  346. }
  347. if ($tableColumn['default'] === 'false') {
  348. $tableColumn['default'] = false;
  349. }
  350. $length = null;
  351. break;
  352. case 'text':
  353. case '_varchar':
  354. case 'varchar':
  355. $tableColumn['default'] = $this->parseDefaultExpression($tableColumn['default']);
  356. $fixed = false;
  357. break;
  358. case 'interval':
  359. $fixed = false;
  360. break;
  361. case 'char':
  362. case 'bpchar':
  363. $fixed = true;
  364. break;
  365. case 'float':
  366. case 'float4':
  367. case 'float8':
  368. case 'double':
  369. case 'double precision':
  370. case 'real':
  371. case 'decimal':
  372. case 'money':
  373. case 'numeric':
  374. $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
  375. if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) {
  376. $precision = $match[1];
  377. $scale = $match[2];
  378. $length = null;
  379. }
  380. break;
  381. case 'year':
  382. $length = null;
  383. break;
  384. // PostgreSQL 9.4+ only
  385. case 'jsonb':
  386. $jsonb = true;
  387. break;
  388. }
  389. if ($tableColumn['default'] && preg_match("('([^']+)'::)", $tableColumn['default'], $match)) {
  390. $tableColumn['default'] = $match[1];
  391. }
  392. $options = [
  393. 'length' => $length,
  394. 'notnull' => (bool) $tableColumn['isnotnull'],
  395. 'default' => $tableColumn['default'],
  396. 'precision' => $precision,
  397. 'scale' => $scale,
  398. 'fixed' => $fixed,
  399. 'unsigned' => false,
  400. 'autoincrement' => $autoincrement,
  401. 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
  402. ? $tableColumn['comment']
  403. : null,
  404. ];
  405. $column = new Column($tableColumn['field'], Type::getType($type), $options);
  406. if (isset($tableColumn['collation']) && ! empty($tableColumn['collation'])) {
  407. $column->setPlatformOption('collation', $tableColumn['collation']);
  408. }
  409. if (in_array($column->getType()->getName(), [Types::JSON_ARRAY, Types::JSON], true)) {
  410. $column->setPlatformOption('jsonb', $jsonb);
  411. }
  412. return $column;
  413. }
  414. /**
  415. * PostgreSQL 9.4 puts parentheses around negative numeric default values that need to be stripped eventually.
  416. *
  417. * @param mixed $defaultValue
  418. *
  419. * @return mixed
  420. */
  421. private function fixVersion94NegativeNumericDefaultValue($defaultValue)
  422. {
  423. if ($defaultValue !== null && strpos($defaultValue, '(') === 0) {
  424. return trim($defaultValue, '()');
  425. }
  426. return $defaultValue;
  427. }
  428. /**
  429. * Parses a default value expression as given by PostgreSQL
  430. */
  431. private function parseDefaultExpression(?string $default): ?string
  432. {
  433. if ($default === null) {
  434. return $default;
  435. }
  436. return str_replace("''", "'", $default);
  437. }
  438. /**
  439. * {@inheritdoc}
  440. */
  441. public function listTableDetails($name): Table
  442. {
  443. $table = parent::listTableDetails($name);
  444. $platform = $this->_platform;
  445. assert($platform instanceof PostgreSqlPlatform);
  446. $sql = $platform->getListTableMetadataSQL($name);
  447. $tableOptions = $this->_conn->fetchAssoc($sql);
  448. if ($tableOptions !== false) {
  449. $table->addOption('comment', $tableOptions['table_comment']);
  450. }
  451. return $table;
  452. }
  453. }