summaryrefslogtreecommitdiffstats
path: root/src/Utils/Table.php
diff options
context:
space:
mode:
authorDan Ungureanu <udan1107@gmail.com>2015-06-23 19:06:11 +0300
committerDan Ungureanu <udan1107@gmail.com>2015-06-23 19:06:11 +0300
commit22f167b6fe82809c33e49a3a23dd500178282985 (patch)
tree1437b3de48840ab77a895dfafcbdbf27bde966fd /src/Utils/Table.php
parente316e267117a38e59040002154f2f980b1a6501d (diff)
downloadsql-parser-22f167b6fe82809c33e49a3a23dd500178282985.zip
sql-parser-22f167b6fe82809c33e49a3a23dd500178282985.tar.gz
sql-parser-22f167b6fe82809c33e49a3a23dd500178282985.tar.bz2
Added table utilities.
Implemented better support for keys in FieldDefFragment. Improved contexts' documentation and definition.
Diffstat (limited to 'src/Utils/Table.php')
-rw-r--r--src/Utils/Table.php45
1 files changed, 44 insertions, 1 deletions
diff --git a/src/Utils/Table.php b/src/Utils/Table.php
index 7560f9d..1e88297 100644
--- a/src/Utils/Table.php
+++ b/src/Utils/Table.php
@@ -20,6 +20,49 @@ use SqlParser\Statements\CreateStatement;
class Table
{
+ public static function getForeignKeys(CreateStatement $tree)
+ {
+ if (($tree->fields === null) || (!$tree->options->has('TABLE'))) {
+ return array();
+ }
+
+ $ret = array();
+
+ foreach ($tree->fields as $field) {
+
+ if ((empty($field->key)) || ($field->key->type !== 'FOREIGN KEY')) {
+ continue;
+ }
+
+ $tmp = array(
+ 'constraint' => $field->name,
+ 'index_list' => $field->key->columns,
+ );
+
+ if (!empty($field->references)) {
+ $tmp['ref_table_name'] = $field->references->table;
+ $tmp['ref_index_list'] = $field->references->columns;
+
+ if (($opt = $field->references->options->has('ON UPDATE'))) {
+ $tmp['on_update'] = str_replace(' ', '_', $opt);
+ }
+
+ if (($opt = $field->references->options->has('ON DELETE'))) {
+ $tmp['on_delete'] = str_replace(' ', '_', $opt);
+ }
+
+ if (($opt = $field->references->options->has('MATCH'))) {
+ $tmp['match'] = str_replace(' ', '_', $opt);
+ }
+ }
+
+ $ret[] = $tmp;
+
+ }
+
+ return $ret;
+ }
+
public static function getFields(CreateStatement $tree)
{
if (($tree->fields === null) || (!$tree->options->has('TABLE'))) {
@@ -31,7 +74,7 @@ class Table
foreach ($tree->fields as $field) {
// Skipping keys.
- if (is_string($field->type)) {
+ if (empty($field->type)) {
continue;
}