summaryrefslogtreecommitdiffstats
path: root/src/Utils/Misc.php
blob: 22227a8b6ebadca09d6f2a663ef2a44eb01f1a5a (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
<?php

namespace SqlParser\Utils;

use SqlParser\Statements\SelectStatement;

/**
 * Miscellaneous utilities.
 *
 * @category   Misc
 * @package    SqlParser
 * @subpackage Utils
 * @author     Dan Ungureanu <udan1107@gmail.com>
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
 */
class Misc
{

    /**
     * Gets a list of all aliases and their original names.
     *
     * @param SelectStatement $tree     The tree that was generated after parsing.
     * @param string          $database The name of the database.
     *
     * @return array
     */
    public static function getAliases(SelectStatement $tree, $database)
    {
        $retval = array();

        $tables = array();

        if ((!empty($tree->join->expr->table))
            && (!empty($tree->join->expr->alias))
        ) {
            $thisDb = empty($tree->join->expr->database) ?
                $database : $tree->join->expr->database;

            $retval = array(
                $thisDb => array(
                    'alias' => null,
                    'tables' => array(
                        $tree->join->expr->table => array(
                            'alias' => $tree->join->expr->alias,
                            'columns' => array(),
                        ),
                    ),
                ),
            );

            $tables[$thisDb][$tree->join->expr->alias] = $tree->join->expr->table;
        }

        foreach ($tree->from as $expr) {
            if (empty($expr->table)) {
                continue;
            }

            $thisDb = empty($expr->database) ? $database : $expr->database;

            if (!isset($retval[$thisDb])) {
                $retval[$thisDb] = array(
                    'alias' => null,
                    'tables' => array(),
                );
            }

            if (!isset($retval[$thisDb]['tables'][$expr->table])) {
                $retval[$thisDb]['tables'][$expr->table] = array(
                    'alias' => empty($expr->alias) ? null : $expr->alias,
                    'columns' => array(),
                );
            }

            if (!isset($tables[$thisDb])) {
                $tables[$thisDb] = array();
            }
            $tables[$thisDb][$expr->alias] = $expr->table;
        }

        foreach ($tree->expr as $expr) {
            if ((empty($expr->column)) || (empty($expr->alias))) {
                continue;
            }

            $thisDb = empty($expr->database) ? $database : $expr->database;

            if (empty($expr->table)) {
                foreach ($retval[$thisDb]['tables'] as &$table) {
                    $table['columns'][$expr->column] = $expr->alias;
                }
            } else {
                $thisTable = isset($tables[$thisDb][$expr->table]) ?
                    $tables[$thisDb][$expr->table] : $expr->table;
                $retval[$thisDb]['tables']
                    [$thisTable]['columns'][$expr->column] = $expr->alias;
            }
        }

        return $retval;
    }
}