Example #1
0
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);
            }
        }
    }
}
/**
 * @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;
}