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 This function takes a tree with an arithmetric expression and brackets each term * @param tree SQL query tree node * @param newTree where the rewritten tree is written to * * This function takes a tree with an arithmetric expression and brackets each term. * Example: IN: x=0.998373 or (y=sin(0.998373) and z=0.998373) and z=43 or * ((z=23 and z=4) or x=1) or y=34 and x between 1 and 2 * OUT: ( ( x = 0.998373 ) or ( ( ( y = SIN (0.998373) ) and ( z = 0.998373 ) ) * and ( z = 43 ) ) or ( ( ( z = 23 ) and ( z = 4 ) ) or ( x = 1 ) ) * or ( ( y = 34 ) and ( x between 1 and 2 ) ) ) */ function PHPSQLParseWhereTokens_groupTerms($tree, &$newTree) { $currLeaf = array(); $isBetween = false; $currLeaf['expr_type'] = "bracket_expression"; $currLeaf['base_expr'] = ""; $currLeaf['sub_tree'] = array(); if (!hasSubtree($newTree)) { $newTree['expr_type'] = "bracket_expression"; $newTree['base_expr'] = ""; $newTree['sub_tree'] = array(); } foreach ($tree as $token) { //NOTE: Subqueries are treated as normal terms in this context. if (!empty($token['sub_tree']) && $token['expr_type'] !== "function" && !isSubquery($token)) { $tmpArray = array(); PHPSQLParseWhereTokens_groupTerms($token['sub_tree'], $tmpArray); $token = $tmpArray; $token['base_expr'] = getBaseExpr($token); } #a normal node - if this node is an AND or OR, split there if (array_key_exists('expr_type', $token) && isOperator($token) && (strtolower($token['base_expr']) === "or" || strtolower($token['base_expr']) === "and")) { //is this the and in the between clause? if yes, ignore this if (strtolower($token['base_expr']) === "and" && $isBetween === true) { $isBetween = false; } else { //it could be that currLeaf is empty - this is the case if a sub_tree has been added //before - just add the operator then if (hasSubtree($currLeaf)) { if (count($currLeaf['sub_tree']) === 1 && $currLeaf['sub_tree'][0]['expr_type'] === "bracket_expression") { //if there is only one entry in sub_tree, we don't need to bracket this $currLeaf = $currLeaf['sub_tree'][0]; } array_push($newTree['sub_tree'], $currLeaf); } array_push($newTree['sub_tree'], $token); //create new leaf $currLeaf['expr_type'] = "bracket_expression"; $currLeaf['base_expr'] = ""; $currLeaf['sub_tree'] = array(); continue; } } //is this a between case? if (array_key_exists('expr_type', $token) && isOperator($token) && strtolower($token['base_expr']) === "between") { $isBetween = true; } //if this is a bracket_expression, see what is inside - if there is just another bracket_expressing //in there, remove the outer one, we don't want to have ( ( foo ) ) type bracketing if ($token['expr_type'] === "bracket_expression" && count($token['sub_tree']) === 1) { $token = $token['sub_tree'][0]; $token['base_expr'] = getBaseExpr($token); } array_push($currLeaf['sub_tree'], $token); } if (hasSubtree($currLeaf)) { //the last leaf is remaining and wants to be added to the tree as well $currLeaf['base_expr'] = getBaseExpr($currLeaf); //if this is a bracket_expression, see what is inside - if there is just another bracket_expressing //in there, remove the outer one, we don't want to have ( ( foo ) ) type bracketing if ($currLeaf['expr_type'] === "bracket_expression" && count($currLeaf['sub_tree']) === 1) { $currLeaf = $currLeaf['sub_tree'][0]; $currLeaf['base_expr'] = getBaseExpr($currLeaf); } array_push($newTree['sub_tree'], $currLeaf); } $newTree['base_expr'] = getBaseExpr($newTree); }