C:\xampp\phpMyAdmin\vendor\phpmyadmin\sql-parser\src\Utils\Misc.php


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
<?php

/**
 * Miscellaneous utilities.
 */

namespace PhpMyAdmin\SqlParser\Utils;

use 
PhpMyAdmin\SqlParser\Components\Expression;
use 
PhpMyAdmin\SqlParser\Statements\SelectStatement;

/**
 * Miscellaneous utilities.
 *
 * @category   Misc
 *
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
 */
class Misc
{
    
/**
     * Gets a list of all aliases and their original names.
     *
     * @param SelectStatement $statement the statement to be processed
     * @param string          $database  the name of the database
     *
     * @return array
     */
    
public static function getAliases($statement$database)
    {
        if (!(
$statement instanceof SelectStatement)
            || (empty(
$statement->expr))
            || (empty(
$statement->from))
        ) {
            return array();
        }

        
$retval = array();

        
$tables = array();

        
/**
         * Expressions that may contain aliases.
         * These are extracted from `FROM` and `JOIN` keywords.
         *
         * @var Expression[]
         */
        
$expressions $statement->from;

        
// Adding expressions from JOIN.
        
if (!empty($statement->join)) {
            foreach (
$statement->join as $join) {
                
$expressions[] = $join->expr;
            }
        }

        foreach (
$expressions as $expr) {
            if ((!isset(
$expr->table)) || ($expr->table === '')) {
                continue;
            }

            
$thisDb = ((isset($expr->database)) && ($expr->database !== '')) ?
                
$expr->database $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' => ((isset($expr->alias)) && ($expr->alias !== '')) ?
                        
$expr->alias null,
                    
'columns' => array(),
                );
            }

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

        foreach (
$statement->expr as $expr) {
            if ((!isset(
$expr->column)) || ($expr->column === '')
                || (!isset(
$expr->alias)) || ($expr->alias === '')
            ) {
                continue;
            }

            
$thisDb = ((isset($expr->database)) && ($expr->database !== '')) ?
                
$expr->database $database;

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

        return 
$retval;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586