コード例 #1
0
/**
 * @brief Extracts all the participating columns from the WHERE tree for a given table
 * @param whereTree the WHERE part of the SQL tree
 * @param table currently treated table
 * @param remove the table identifier infront of the name
 * @return array with participating columns
 * 
 * Extracts all the participating columns from the WHERE tree for a given table and returns
 * an array with all the columns. It also strips the involved columns from the table / alias
 * name.
 */
function PHPSQLgetAllColsFromWhere($whereTree, $table, $removeTableName)
{
    $returnArray = array();
    //if we are looking for nothing, we should return nothing
    if ($table === false) {
        return $returnArray;
    }
    $columns = collectNodes($whereTree, "colref");
    foreach ($columns as $column) {
        $currTable = extractTableName($column);
        if ($table !== $currTable && !(count($column['no_quotes']['parts']) === 1 && $table === "")) {
            continue;
        }
        //getting rid of the table/alias name in the column description (but only if a table
        //name is provided $table)
        if (count($column['no_quotes']['parts']) > 1 && $removeTableName === true) {
            unset($column['no_quotes']['parts'][0]);
            setNoQuotes($column, array_values($column['no_quotes']['parts']));
        }
        $column['alias'] = createAliasNode(array(implodeNoQuotes($column['no_quotes'])));
        array_push($returnArray, $column);
    }
    return $returnArray;
}
コード例 #2
0
ファイル: paqu-join.php プロジェクト: garv347/swanhart-tools
function rewriteTableNameInSubqueries(&$subTree, $toThisTableName, $exceptThisTableName)
{
    foreach ($subTree as &$node) {
        if (hasSubtree($node)) {
            rewriteTableNameInSubqueries($node['sub_tree'], $toThisTableName, $exceptThisTableName);
        }
        if (isColref($node)) {
            $currTable = extractTableName($node);
            if ($currTable !== $exceptThisTableName) {
                //if there is a * in the subquery somewhere
                if (!array_key_exists("no_quotes", $node)) {
                    $node['no_quotes'] = array("delim" => ".", "parts" => array());
                }
                $node['no_quotes']['parts'] = array($toThisTableName, implodeNoQuotes($node['no_quotes']));
                $node['base_expr'] = getBaseExpr($node);
            }
        }
    }
}
コード例 #3
0
ファイル: parseSqlAll.php プロジェクト: vrtulka23/daiquiri
/**
 * @brief Add all columns to the SELECT tree
 * @param sqlTree SQL parser tree node of complete query/subquery
 * @param mysqlConn a properly initialised MySQLI/MySQLII connection to the DB
 * @param zendAdapter a valid ZEND DB adapter
 * 
 * This function will evaluate the all the tables that need SQL * attribute substitution.
 * The database is queried to retrieve a complete list of columns of each table and the
 * approperiate SELECT colref nodes are added to the SQL parser tree. The SQL * attribute
 * is removed from the sqlTree SELECT node.
 */
function _parseSqlAll_SELECT(&$sqlTree, $mysqlConn = false, $zendAdapter = false)
{
    if (!is_array($sqlTree) || !array_key_exists('SELECT', $sqlTree)) {
        return;
    }
    $table = false;
    $selectCpy = $sqlTree['SELECT'];
    $sqlTree['SELECT'] = array();
    foreach ($selectCpy as &$node) {
        if (strpos($node['base_expr'], "*") !== false && $node['sub_tree'] === false) {
            //we have found an all operator and need to find the corresponding
            //table to look things up
            $tableFullName = false;
            $dbName = extractDbName($node);
            $tableName = extractTableName($node);
            $colName = extractColumnName($node);
            if ($dbName !== false) {
                $tableFullName = "`" . $dbName . "`.`" . $tableName . "`";
            } else {
                if ($tableName !== false) {
                    $tableFullName = "`" . $tableName . "`";
                }
            }
            $table = array();
            $alias = array();
            if ($tableFullName === false) {
                //add everything *ed from all tables to this query
                foreach ($sqlTree['FROM'] as $fromNode) {
                    if (isTable($fromNode)) {
                        $table[] = $fromNode['table'];
                        if (!hasAlias($fromNode)) {
                            $alias[] = $fromNode['table'];
                        } else {
                            $alias[] = $fromNode['alias']['name'];
                        }
                    } else {
                        if (isSubquery($fromNode)) {
                            //handle subqueries...
                            _parseSqlAll_linkSubquerySELECT($fromNode['sub_tree'], $sqlTree, $fromNode['alias']['name']);
                        }
                    }
                }
            } else {
                foreach ($sqlTree['FROM'] as $fromNode) {
                    //it could be, that the table here is actually another aliased table (which should
                    //have been processed here already, since SELECT is called last) -> link to tree
                    if (isTable($fromNode)) {
                        if (hasAlias($fromNode)) {
                            if (trim($fromNode['alias']['name'], "`") === $tableName) {
                                $table[] = $fromNode['table'];
                                break;
                            }
                        } else {
                            if ($fromNode['table'] === $tableFullName) {
                                $table[] = $fromNode['table'];
                                break;
                            }
                        }
                    } else {
                        if (isSubquery($fromNode)) {
                            if (trim($fromNode['alias']['name'], "`") === $tableName) {
                                _parseSqlAll_linkSubquerySELECT($fromNode['sub_tree'], $sqlTree, $tableName);
                                continue 2;
                            }
                        }
                    }
                }
                $alias[] = $tableFullName;
            }
            if (empty($table)) {
                continue;
            }
            //now that we know the table, we need to look up what is in there
            foreach (array_keys($table) as $key) {
                if ($mysqlConn !== false) {
                    _parseSqlAll_getColsMysqlii($sqlTree, $node, $mysqlConn, $table[$key], $alias[$key]);
                }
                if ($zendAdapter !== false) {
                    _parseSqlAll_getColsZend($sqlTree, $node, $zendAdapter, $table[$key], $alias[$key]);
                }
            }
        } else {
            array_push($sqlTree['SELECT'], $node);
        }
    }
}