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 Collects all participating nodes from the SQL tree for a given table name or alias * @param sqlTree the SQL tree which is rewritten * @param tblDb database name * @param tblName table name * @param tblAlias table alias name * @param returnArray return all the found columns into this array * @param tableList the sorted table list * @param recLevel current recursion level * @param startBrach true if this is the first call to this function, false if it is in a recursion * @return returns the number of columns that lie in a different table than the one given, -1 if none are found * * Counts all the columns corresponding to a given table name or alias. The columns that correspond to the table are placed * into the returnArray. The function returns the number of columns that are different to the specified table. */ function PHPSQLcollectColumns($sqlSelect, $tblDb, $tblName, $tblAlias, &$returnArray, &$tableList, $recLevel, $startBranch = true) { $countDiffTables = 0; $workload = array(); if (array_key_exists('SELECT', $sqlSelect)) { $workload = $sqlSelect['SELECT']; } else { $workload = $sqlSelect; } foreach ($workload as $node) { if (!isColref($node) && !isOperator($node) && !isReserved($node)) { $currCountDiffTables = false; $currColArray = array(); if (hasSubtree($node)) { $currCountDiffTables = PHPSQLcollectColumns($node['sub_tree'], $tblDb, $tblName, $tblAlias, $currColArray, $tableList, $recLevel, false); $countDiffTables += $currCountDiffTables; } else { if ($recLevel === 0) { //if this has no subtree (as do functions without parameters like foo()), then add it at the outermost possible //place (i.e. recLeve = 0) $currCountDiffTables = 0; } } if ($startBranch === true && $currCountDiffTables === 0) { if (!hasAlias($node)) { $node['alias'] = createAliasNode(array(buildEscapedString(array($node)))); } //add the correct table names to all columns that are not in the current column if (hasSubtree($node) && count($tableList) > 0 && $recLevel !== max(array_keys($tableList))) { $toThisTableName = extractTableAlias($tableList[$recLevel + 1]['node']); if ($toThisTableName === false) { $toThisTableName = extractTableName($tableList[$recLevel + 1]['node']); } if (hasAlias($tableList[$recLevel])) { rewriteTableNameInSubqueries($node['sub_tree'], $toThisTableName, extractTableAlias($tableList[$recLevel]['node'])); } else { rewriteTableNameInSubqueries($node['sub_tree'], $toThisTableName, extractTableName($tableList[$recLevel]['node'])); } if ($node['expr_type'] !== "aggregate_function") { $node['base_expr'] = getBaseExpr($node); } } //if this is a function/aggregate that applies to a '*' "column", then only apply this at the //outermost level (i.e. reclevel=0) and not before (see Test40 for case where this applies) $canAddThisNode = true; foreach ($currColArray as $column) { if ($column['base_expr'] === "*" && $recLevel != 0) { $canAddThisNode = false; break; } } if ($canAddThisNode === true) { array_push($returnArray, $node); } } //add columns if not yet added, but only if there are dependant columns evolved if ($currCountDiffTables !== false && $currCountDiffTables > 0) { foreach ($currColArray as $column) { $found = false; //go through the already selected columns foreach ($returnArray as $returnColumn) { if (columnIsEqual($returnColumn, $column)) { $found = true; break; } } if ($found === false) { $column['alias'] = createAliasNode(array(implodeNoQuotes($column['no_quotes']))); array_push($returnArray, $column); } } } } else { if (isColref($node) || $node['base_expr'] === '*' && $startBranch === true) { $currCol = extractColumnName($node); $currTable = extractTableName($node); $currDB = extractDbName($node); if ($currTable === $tblAlias || $currTable === false || $currTable === $tblName || $tblAlias === $currDB . "." . $currTable) { array_push($returnArray, $node); } else { #check if this table has already been selected and processed. if yes, we can already process it and #donot need to wait $found = false; foreach ($tableList as $key => $tblListNode) { #skip everything not yet processed... if ($key < $recLevel) { continue; } if ($currTable === extractTableAlias($tblListNode['node'])) { $found = true; break; } } if ($found === false) { $countDiffTables += 1; } } } else { if (isReserved($node)) { //always adding reserved keywords to each select statement we issue array_push($returnArray, $node); } } } } return $countDiffTables; }