Table.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Table utilities.
  4. *
  5. * @package SqlParser
  6. * @subpackage Utils
  7. */
  8. namespace SqlParser\Utils;
  9. use SqlParser\Statements\CreateStatement;
  10. /**
  11. * Table utilities.
  12. *
  13. * @category Statement
  14. * @package SqlParser
  15. * @subpackage Utils
  16. * @author Dan Ungureanu <udan1107@gmail.com>
  17. * @license http://opensource.org/licenses/GPL-2.0 GNU Public License
  18. */
  19. class Table
  20. {
  21. /**
  22. * Gets the foreign keys of the table.
  23. *
  24. * @param CreateStatement $statement The statement to be processed.
  25. *
  26. * @return array
  27. */
  28. public static function getForeignKeys($statement)
  29. {
  30. if ((empty($statement->fields))
  31. || (!is_array($statement->fields))
  32. || (!$statement->options->has('TABLE'))
  33. ) {
  34. return array();
  35. }
  36. $ret = array();
  37. foreach ($statement->fields as $field) {
  38. if ((empty($field->key)) || ($field->key->type !== 'FOREIGN KEY')) {
  39. continue;
  40. }
  41. $columns = array();
  42. foreach ($field->key->columns as $column) {
  43. $columns[] = $column['name'];
  44. }
  45. $tmp = array(
  46. 'constraint' => $field->name,
  47. 'index_list' => $columns,
  48. );
  49. if (!empty($field->references)) {
  50. $tmp['ref_db_name'] = $field->references->table->database;
  51. $tmp['ref_table_name'] = $field->references->table->table;
  52. $tmp['ref_index_list'] = $field->references->columns;
  53. if (($opt = $field->references->options->has('ON UPDATE'))) {
  54. $tmp['on_update'] = str_replace(' ', '_', $opt);
  55. }
  56. if (($opt = $field->references->options->has('ON DELETE'))) {
  57. $tmp['on_delete'] = str_replace(' ', '_', $opt);
  58. }
  59. // if (($opt = $field->references->options->has('MATCH'))) {
  60. // $tmp['match'] = str_replace(' ', '_', $opt);
  61. // }
  62. }
  63. $ret[] = $tmp;
  64. }
  65. return $ret;
  66. }
  67. /**
  68. * Gets fields of the table.
  69. *
  70. * @param CreateStatement $statement The statement to be processed.
  71. *
  72. * @return array
  73. */
  74. public static function getFields($statement)
  75. {
  76. if ((empty($statement->fields))
  77. || (!is_array($statement->fields))
  78. || (!$statement->options->has('TABLE'))
  79. ) {
  80. return array();
  81. }
  82. $ret = array();
  83. foreach ($statement->fields as $field) {
  84. // Skipping keys.
  85. if (empty($field->type)) {
  86. continue;
  87. }
  88. $ret[$field->name] = array(
  89. 'type' => $field->type->name,
  90. 'timestamp_not_null' => false,
  91. );
  92. if ($field->options) {
  93. if ($field->type->name === 'TIMESTAMP') {
  94. if ($field->options->has('NOT NULL')) {
  95. $ret[$field->name]['timestamp_not_null'] = true;
  96. }
  97. }
  98. if (($option = $field->options->has('DEFAULT'))) {
  99. $ret[$field->name]['default_value'] = $option;
  100. if ($option === 'CURRENT_TIMESTAMP') {
  101. $ret[$field->name]['default_current_timestamp'] = true;
  102. }
  103. }
  104. if (($option = $field->options->has('ON UPDATE'))) {
  105. if ($option === 'CURRENT_TIMESTAMP') {
  106. $ret[$field->name]['on_update_current_timestamp'] = true;
  107. }
  108. }
  109. if (($option = $field->options->has('AS'))) {
  110. $ret[$field->name]['generated'] = true;
  111. $ret[$field->name]['expr'] = $option;
  112. }
  113. }
  114. }
  115. return $ret;
  116. }
  117. }