summaryrefslogtreecommitdiffstats
path: root/src/Utils/Table.php
blob: 1e8829773342ede33e74bc4a49891bd370c6f50b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace SqlParser\Utils;

use SqlParser\Lexer;
use SqlParser\Parser;
use SqlParser\Fragments\DataTypeFragment;
use SqlParser\Fragments\ParamDefFragment;
use SqlParser\Statements\CreateStatement;

/**
 * Table utilities.
 *
 * @category   Tables
 * @package    SqlParser
 * @subpackage Utils
 * @author     Dan Ungureanu <udan1107@gmail.com>
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
 */
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'))) {
            return array();
        }

        $ret = array();

        foreach ($tree->fields as $field) {

            // Skipping keys.
            if (empty($field->type)) {
                continue;
            }

            $ret[$field->name] = array(
                'type' => $field->type->name,
                'timestamp_not_null' => false,
            );

            if ($field->options) {

                if ($field->type->name === 'TIMESTAMP') {
                    if ($field->options->has('NOT NULL')) {
                        $ret[$field->name]['timestamp_not_null'] = true;
                    }
                }

                if (($option = $field->options->has('DEFAULT'))) {
                    $ret[$field->name]['default_value'] = $option;
                    if ($option === 'CURRENT_TIMESTAMP') {
                        $ret[$field->name]['default_current_timestamp'] = true;
                    }
                }

                if (($option = $field->options->has('ON UPDATE'))) {
                    if ($option === 'CURRENT_TIMESTAMP') {
                        $ret[$field->name]['on_update_current_timestamp'] = true;
                    }
                }
            }

        }

        return $ret;
    }
}