PostgreSqlSchemaManager.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 strlen;
  22. use function strpos;
  23. use function strtolower;
  24. use function trim;
  25. use const CASE_LOWER;
  26. /**
  27. * PostgreSQL Schema Manager.
  28. */
  29. class PostgreSqlSchemaManager extends AbstractSchemaManager
  30. {
  31. /** @var string[]|null */
  32. private $existingSchemaPaths;
  33. /**
  34. * Gets all the existing schema names.
  35. *
  36. * @return string[]
  37. */
  38. public function getSchemaNames()
  39. {
  40. $statement = $this->_conn->executeQuery(
  41. "SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' AND nspname != 'information_schema'"
  42. );
  43. return $statement->fetchAll(FetchMode::COLUMN);
  44. }
  45. /**
  46. * Returns an array of schema search paths.
  47. *
  48. * This is a PostgreSQL only function.
  49. *
  50. * @return string[]
  51. */
  52. public function getSchemaSearchPaths()
  53. {
  54. $params = $this->_conn->getParams();
  55. $searchPaths = $this->_conn->fetchColumn('SHOW search_path');
  56. assert($searchPaths !== false);
  57. $schema = explode(',', $searchPaths);
  58. if (isset($params['user'])) {
  59. $schema = str_replace('"$user"', $params['user'], $schema);
  60. }
  61. return array_map('trim', $schema);
  62. }
  63. /**
  64. * Gets names of all existing schemas in the current users search path.
  65. *
  66. * This is a PostgreSQL only function.
  67. *
  68. * @return string[]
  69. */
  70. public function getExistingSchemaSearchPaths()
  71. {
  72. if ($this->existingSchemaPaths === null) {
  73. $this->determineExistingSchemaSearchPaths();
  74. }
  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 (preg_match("/^nextval\('(.*)'(::.*)?\)$/", $tableColumn['default'], $matches)) {
  285. $tableColumn['sequence'] = $matches[1];
  286. $tableColumn['default'] = null;
  287. $autoincrement = true;
  288. }
  289. if (preg_match("/^['(](.*)[')]::/", $tableColumn['default'], $matches)) {
  290. $tableColumn['default'] = $matches[1];
  291. } elseif (preg_match('/^NULL::/', $tableColumn['default'])) {
  292. $tableColumn['default'] = null;
  293. }
  294. $length = $tableColumn['length'] ?? null;
  295. if ($length === '-1' && isset($tableColumn['atttypmod'])) {
  296. $length = $tableColumn['atttypmod'] - 4;
  297. }
  298. if ((int) $length <= 0) {
  299. $length = null;
  300. }
  301. $fixed = null;
  302. if (! isset($tableColumn['name'])) {
  303. $tableColumn['name'] = '';
  304. }
  305. $precision = null;
  306. $scale = null;
  307. $jsonb = null;
  308. $dbType = strtolower($tableColumn['type']);
  309. if (
  310. strlen($tableColumn['domain_type'])
  311. && ! $this->_platform->hasDoctrineTypeMappingFor($tableColumn['type'])
  312. ) {
  313. $dbType = strtolower($tableColumn['domain_type']);
  314. $tableColumn['complete_type'] = $tableColumn['domain_complete_type'];
  315. }
  316. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  317. $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
  318. $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
  319. switch ($dbType) {
  320. case 'smallint':
  321. case 'int2':
  322. $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
  323. $length = null;
  324. break;
  325. case 'int':
  326. case 'int4':
  327. case 'integer':
  328. $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
  329. $length = null;
  330. break;
  331. case 'bigint':
  332. case 'int8':
  333. $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
  334. $length = null;
  335. break;
  336. case 'bool':
  337. case 'boolean':
  338. if ($tableColumn['default'] === 'true') {
  339. $tableColumn['default'] = true;
  340. }
  341. if ($tableColumn['default'] === 'false') {
  342. $tableColumn['default'] = false;
  343. }
  344. $length = null;
  345. break;
  346. case 'text':
  347. case '_varchar':
  348. case 'varchar':
  349. $tableColumn['default'] = $this->parseDefaultExpression($tableColumn['default']);
  350. $fixed = false;
  351. break;
  352. case 'interval':
  353. $fixed = false;
  354. break;
  355. case 'char':
  356. case 'bpchar':
  357. $fixed = true;
  358. break;
  359. case 'float':
  360. case 'float4':
  361. case 'float8':
  362. case 'double':
  363. case 'double precision':
  364. case 'real':
  365. case 'decimal':
  366. case 'money':
  367. case 'numeric':
  368. $tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
  369. if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) {
  370. $precision = $match[1];
  371. $scale = $match[2];
  372. $length = null;
  373. }
  374. break;
  375. case 'year':
  376. $length = null;
  377. break;
  378. // PostgreSQL 9.4+ only
  379. case 'jsonb':
  380. $jsonb = true;
  381. break;
  382. }
  383. if ($tableColumn['default'] && preg_match("('([^']+)'::)", $tableColumn['default'], $match)) {
  384. $tableColumn['default'] = $match[1];
  385. }
  386. $options = [
  387. 'length' => $length,
  388. 'notnull' => (bool) $tableColumn['isnotnull'],
  389. 'default' => $tableColumn['default'],
  390. 'precision' => $precision,
  391. 'scale' => $scale,
  392. 'fixed' => $fixed,
  393. 'unsigned' => false,
  394. 'autoincrement' => $autoincrement,
  395. 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
  396. ? $tableColumn['comment']
  397. : null,
  398. ];
  399. $column = new Column($tableColumn['field'], Type::getType($type), $options);
  400. if (isset($tableColumn['collation']) && ! empty($tableColumn['collation'])) {
  401. $column->setPlatformOption('collation', $tableColumn['collation']);
  402. }
  403. if (in_array($column->getType()->getName(), [Types::JSON_ARRAY, Types::JSON], true)) {
  404. $column->setPlatformOption('jsonb', $jsonb);
  405. }
  406. return $column;
  407. }
  408. /**
  409. * PostgreSQL 9.4 puts parentheses around negative numeric default values that need to be stripped eventually.
  410. *
  411. * @param mixed $defaultValue
  412. *
  413. * @return mixed
  414. */
  415. private function fixVersion94NegativeNumericDefaultValue($defaultValue)
  416. {
  417. if (strpos($defaultValue, '(') === 0) {
  418. return trim($defaultValue, '()');
  419. }
  420. return $defaultValue;
  421. }
  422. /**
  423. * Parses a default value expression as given by PostgreSQL
  424. */
  425. private function parseDefaultExpression(?string $default): ?string
  426. {
  427. if ($default === null) {
  428. return $default;
  429. }
  430. return str_replace("''", "'", $default);
  431. }
  432. /**
  433. * {@inheritdoc}
  434. */
  435. public function listTableDetails($name): Table
  436. {
  437. $table = parent::listTableDetails($name);
  438. $platform = $this->_platform;
  439. assert($platform instanceof PostgreSqlPlatform);
  440. $sql = $platform->getListTableMetadataSQL($name);
  441. $tableOptions = $this->_conn->fetchAssoc($sql);
  442. if ($tableOptions !== false) {
  443. $table->addOption('comment', $tableOptions['table_comment']);
  444. }
  445. return $table;
  446. }
  447. }