SqlParser\Utils\Query::getAll PHP Method

getAll() public static method

Parses a query and gets all information about it.
public static getAll ( string $query ) : array
$query string The query to be parsed.
return array The array returned is the one returned by `static::getFlags()`, with the following keys added: - parser - the parser used to analyze the query; - statement - the first statement resulted from parsing; - select_tables - the real name of the tables selected; if there are no table names in the `SELECT` expressions, the table names are fetched from the `FROM` expressions - select_expr - selected expressions
    public static function getAll($query)
    {
        $parser = new Parser($query);
        if (empty($parser->statements[0])) {
            return static::getFlags(null, true);
        }
        $statement = $parser->statements[0];
        $ret = static::getFlags($statement, true);
        $ret['parser'] = $parser;
        $ret['statement'] = $statement;
        if ($statement instanceof SelectStatement) {
            $ret['select_tables'] = array();
            $ret['select_expr'] = array();
            // Finding tables' aliases and their associated real names.
            $tableAliases = array();
            foreach ($statement->from as $expr) {
                if (isset($expr->table) && $expr->table !== '' && isset($expr->alias) && $expr->alias !== '') {
                    $tableAliases[$expr->alias] = array($expr->table, isset($expr->database) ? $expr->database : null);
                }
            }
            // Trying to find selected tables only from the select expression.
            // Sometimes, this is not possible because the tables aren't defined
            // explicitly (e.g. SELECT * FROM film, SELECT film_id FROM film).
            foreach ($statement->expr as $expr) {
                if (isset($expr->table) && $expr->table !== '') {
                    if (isset($tableAliases[$expr->table])) {
                        $arr = $tableAliases[$expr->table];
                    } else {
                        $arr = array($expr->table, isset($expr->database) && $expr->database !== '' ? $expr->database : null);
                    }
                    if (!in_array($arr, $ret['select_tables'])) {
                        $ret['select_tables'][] = $arr;
                    }
                } else {
                    $ret['select_expr'][] = $expr->expr;
                }
            }
            // If no tables names were found in the SELECT clause or if there
            // are expressions like * or COUNT(*), etc. tables names should be
            // extracted from the FROM clause.
            if (empty($ret['select_tables'])) {
                foreach ($statement->from as $expr) {
                    if (isset($expr->table) && $expr->table !== '') {
                        $arr = array($expr->table, isset($expr->database) && $expr->database !== '' ? $expr->database : null);
                        if (!in_array($arr, $ret['select_tables'])) {
                            $ret['select_tables'][] = $arr;
                        }
                    }
                }
            }
        }
        return $ret;
    }

Usage Example

Beispiel #1
0
 public function testGetAll()
 {
     $this->assertEquals(array('distinct' => false, 'drop_database' => false, 'group' => false, 'having' => false, 'is_affected' => false, 'is_analyse' => false, 'is_count' => false, 'is_delete' => false, 'is_explain' => false, 'is_export' => false, 'is_func' => false, 'is_group' => false, 'is_insert' => false, 'is_maint' => false, 'is_procedure' => false, 'is_replace' => false, 'is_select' => false, 'is_show' => false, 'is_subquery' => false, 'join' => false, 'limit' => false, 'offset' => false, 'order' => false, 'querytype' => false, 'reload' => false, 'select_from' => false, 'union' => false), Query::getAll(''));
     $query = 'SELECT *, actor.actor_id, sakila2.film.*
         FROM sakila2.city, sakila2.film, actor';
     $parser = new Parser($query);
     $this->assertEquals(array_merge(Query::getFlags($parser->statements[0], true), array('parser' => $parser, 'statement' => $parser->statements[0], 'select_expr' => array('*'), 'select_tables' => array(array('actor', null), array('film', 'sakila2')))), Query::getAll($query));
     $query = 'SELECT * FROM sakila.actor, film';
     $parser = new Parser($query);
     $this->assertEquals(array_merge(Query::getFlags($parser->statements[0], true), array('parser' => $parser, 'statement' => $parser->statements[0], 'select_expr' => array('*'), 'select_tables' => array(array('actor', 'sakila'), array('film', null)))), Query::getAll($query));
     $query = 'SELECT a.actor_id FROM sakila.actor AS a, film';
     $parser = new Parser($query);
     $this->assertEquals(array_merge(Query::getFlags($parser->statements[0], true), array('parser' => $parser, 'statement' => $parser->statements[0], 'select_expr' => array(), 'select_tables' => array(array('actor', 'sakila')))), Query::getAll($query));
 }
All Usage Examples Of SqlParser\Utils\Query::getAll