Table.php 3.72 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
<?php

/**
 * Table utilities.
 */

namespace PhpMyAdmin\SqlParser\Utils;

use PhpMyAdmin\SqlParser\Statements\CreateStatement;

/**
 * Table utilities.
 *
 * @category   Statement
 *
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
 */
class Table
{
    /**
     * Gets the foreign keys of the table.
     *
     * @param CreateStatement $statement the statement to be processed
     *
     * @return array
     */
    public static function getForeignKeys($statement)
    {
        if (empty($statement->fields)
            || (! is_array($statement->fields))
            || (! $statement->options->has('TABLE'))
        ) {
            return array();
        }

        $ret = array();

        foreach ($statement->fields as $field) {
            if (empty($field->key) || ($field->key->type !== 'FOREIGN KEY')) {
                continue;
            }

            $columns = array();
            foreach ($field->key->columns as $column) {
                $columns[] = $column['name'];
            }

            $tmp = array(
                'constraint' => $field->name,
                'index_list' => $columns
            );

            if (! empty($field->references)) {
                $tmp['ref_db_name'] = $field->references->table->database;
                $tmp['ref_table_name'] = $field->references->table->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;
    }

    /**
     * Gets fields of the table.
     *
     * @param CreateStatement $statement the statement to be processed
     *
     * @return array
     */
    public static function getFields($statement)
    {
        if (empty($statement->fields)
            || (! is_array($statement->fields))
            || (! $statement->options->has('TABLE'))
        ) {
            return array();
        }

        $ret = array();

        foreach ($statement->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;
                    }
                }

                if ($option = $field->options->has('AS')) {
                    $ret[$field->name]['generated'] = true;
                    $ret[$field->name]['expr'] = $option;
                }
            }
        }

        return $ret;
    }
}