function parseSelectQuery() { $colNames = array(); $colTables = array(); $colAliases = array(); $colFuncs = array(); $fieldValues = array(); $tables = array(); $tableAliases = array(); $groupColumns = array(); $orderColumns = array(); $orderTypes = array(); $where_expr = ""; $distinct = 0; $joins = array(); // parse Distinct if (strtoupper($this->peekNextElement()) == "DISTINCT") { $distinct = 1; $this->skipNextElement(); } // parse Columns $arrElements = array(); $colIndex = -1; while ($this->parseNextElements(",", array("FROM"), $arrElements)) { ++$colIndex; $colNames[$colIndex] = ""; $colTables[$colIndex] = ""; $colAliases[$colIndex] = ""; $colFuncs[$colIndex] = ""; // FUNC() | FUNC(col) | FUNC(table.col) | FUNC(col) AS alias | FUNC(table.col) AS alias | FUNC() AS alias // function ? if (count($arrElements) >= 3 && $arrElements[1] == "(") { $colFuncs[$colIndex] = strtoupper($arrElements[0]); // remove function from $arrElements array_splice($arrElements, 0, 2); $pos = array_search(")", $arrElements); if (!is_false($pos) && !_is_null($pos)) { array_splice($arrElements, $pos, 1); } } // *empty array* | col | table.col | col AS alias | table.col AS alias | AS alias // table ? if (count($arrElements) >= 3 && $arrElements[1] == ".") { $colTables[$colIndex] = $arrElements[0]; array_splice($arrElements, 0, 2); } // *empty array* | col | col AS alias | AS alias // alias ? if (count($arrElements) >= 3 && strtoupper($arrElements[1]) == "AS") { $colAliases[$colIndex] = $arrElements[2]; array_splice($arrElements, 1, 2); } // *empty array* | col | AS alias // alias on function without column if (count($arrElements) >= 2 && strtoupper($arrElements[0]) == "AS") { $colAliases[$colIndex] = $arrElements[1]; array_splice($arrElements, 0, 2); } // *empty array* | col // column name ? if (count($arrElements) >= 1) { $colNames[$colIndex] = $arrElements[0]; array_splice($arrElements, 0, 1); } if (count($arrElements) > 0) { $errStr = "Unexpected Element(s): "; for ($i = 0; $i < count($arrElements); ++$i) { $errStr .= $arrElements[$i] . " "; } print_error_msg($errStr); return null; } } // skip FROM $this->skipNextElement(); // parse Tables $arrElements = array(); $tableIndex = 0; $joinIndex = 0; while ($elem = $this->peekNextElement()) { $elemUpper = strtoupper($elem); if (in_array($elemUpper, array("GROUP", "WHERE", "ORDER", "LIMIT", ";"))) { break; } if ($elemUpper == "AS") { $this->skipNextElement(); $tableAliases[$tableIndex] = $this->parseNextElement(); continue; } if ($elemUpper == "LEFT") { if (!isset($joins[$joinIndex])) { $joins[$joinIndex] = new Join(); } $joins[$joinIndex]->type = JOIN_LEFT; $this->skipNextElement(); continue; } if ($elemUpper == "RIGHT") { if (!isset($joins[$joinIndex])) { $joins[$joinIndex] = new Join(); } $joins[$joinIndex]->type = JOIN_RIGHT; $this->skipNextElement(); continue; } if ($elemUpper == "INNER") { if (!isset($joins[$joinIndex])) { $joins[$joinIndex] = new Join(); } $joins[$joinIndex]->type = JOIN_INNER; $this->skipNextElement(); continue; } if ($elemUpper == "JOIN") { if (!isset($joins[$joinIndex])) { $joins[$joinIndex] = new Join(); } $joins[$joinIndex]->leftTableIndex = $tableIndex; $this->skipNextElement(); $tables[++$tableIndex] = $this->parseNextElement(); $tableAliases[$tableIndex] = ""; $joins[$joinIndex]->rightTableIndex = $tableIndex; continue; } if ($elemUpper == "OUTER") { $this->skipNextElement(); // ignore continue; } if ($elemUpper == ",") { ++$tableIndex; $this->skipNextElement(); continue; } if ($elemUpper == "ON") { $exprElements = array(); $this->skipNextElement(); $this->parseNextElements("", array(",", "GROUP", "WHERE", "ORDER", "LIMIT", ";", "LEFT", "RIGHT", "INNER", "OUTER", "JOIN"), $exprElements); foreach ($exprElements as $exprElem) { // no spaces on .'s if ($exprElem == ".") { remove_last_char($joins[$joinIndex]->expr); $joins[$joinIndex]->expr .= $exprElem; } else { $joins[$joinIndex]->expr .= $exprElem . " "; } } $joinIndex++; continue; } // if table is allready set its an alias without AS, else its the table name if (isset($tables[$tableIndex])) { $tableAliases[$tableIndex] = $elem; $this->skipNextElement(); } else { $tables[$tableIndex] = $elem; $tableAliases[$tableIndex] = ""; $this->skipNextElement(); } } // parse Where statement (Raw, because the escape-chars are needend in the ExpressionParser) if (strtoupper($this->peekNextElement()) == "WHERE") { $this->skipNextElement(); while (!is_empty_str($elem = $this->peekNextElementRaw())) { if (strtoupper($elem) == "GROUP" || strtoupper($elem) == "ORDER" || $elem == ";" || strtoupper($elem) == "LIMIT") { break; } $this->skipNextElement(); // no " " on points if ($elem == ".") { remove_last_char($where_expr); $where_expr .= $elem; } else { $where_expr .= $elem . " "; } } } debug_print("WHERE EXPR: {$where_expr}<br>"); // parse GROUP BY $groupColumnIndex = 0; if (strtoupper($this->peekNextElement()) == "GROUP") { $this->skipNextElement(); if (strtoupper($this->parseNextElement()) != "BY") { print_error_msg("BY expected"); return null; } while (!is_empty_str($elem = $this->peekNextElement())) { if ($elem == ";" || strtoupper($elem) == "LIMIT" || strtoupper($elem) == "ORDER") { break; } $this->skipNextElement(); if ($elem == ",") { $groupColumnIndex++; } else { if (!isset($groupColumns[$groupColumnIndex])) { $groupColumns[$groupColumnIndex] = $elem; } else { $groupColumns[$groupColumnIndex] .= $elem; } } } } // parse ORDER BY $orderColumnIndex = 0; if (strtoupper($this->peekNextElement()) == "ORDER") { $this->skipNextElement(); if (strtoupper($this->parseNextElement()) != "BY") { print_error_msg("BY expected"); return null; } while (!is_empty_str($elem = $this->peekNextElement())) { if ($elem == ";" || strtoupper($elem) == "LIMIT") { break; } $this->skipNextElement(); if ($elem == ",") { $orderColumnIndex++; } else { if (strtoupper($elem) == "ASC") { $orderTypes[$orderColumnIndex] = ORDER_ASC; } else { if (strtoupper($elem) == "DESC") { $orderTypes[$orderColumnIndex] = ORDER_DESC; } else { if (!isset($orderColumns[$orderColumnIndex])) { $orderColumns[$orderColumnIndex] = $elem; } else { $orderColumns[$orderColumnIndex] .= $elem; } $orderTypes[$orderColumnIndex] = ORDER_ASC; } } } } } // parse LIMIT $limit = array(); if (strtoupper($this->peekNextElement()) == "LIMIT") { $this->skipNextElement(); while (!is_empty_str($elem = $this->peekNextElement())) { if ($elem == ";") { break; } $this->skipNextElement(); if ($elem != ",") { $limit[] = $elem; } } } $sqlObj = new SqlQuery("SELECT", $colNames, $tables, $colAliases, $colTables, $where_expr, $groupColumns, $orderColumns, $orderTypes, $limit); $sqlObj->tableAliases = $tableAliases; $sqlObj->colFuncs = $colFuncs; $sqlObj->distinct = $distinct; $sqlObj->joins = $joins; return $sqlObj; }
function parseSelectQuery() { $colNames = array(); $colTables = array(); $colAliases = array(); $colFuncs = array(); $fieldValues = array(); $tables = array(); $tableAliases = array(); $groupColumns = array(); $orderColumns = array(); $orderTypes = array(); $where_expr = ""; $distinct = 0; // parse Distinct if (strtoupper($this->peekNextElement()) == "DISTINCT") { $distinct = 1; $this->skipNextElement(); } // parse Columns $arrElements = array(); $colIndex = -1; while ($this->parseNextElements(",", array("FROM"), $arrElements)) { ++$colIndex; $colNames[$colIndex] = ""; $colTables[$colIndex] = ""; $colAliases[$colIndex] = ""; $colFuncs[$colIndex] = ""; // FUNC() | FUNC(col) | FUNC(table.col) | FUNC(col) AS alias | FUNC(table.col) AS alias | FUNC() AS alias // function ? if (count($arrElements) >= 3 && $arrElements[1] == "(") { $colFuncs[$colIndex] = strtoupper($arrElements[0]); // remove function from $arrElements array_splice($arrElements, 0, 2); $pos = array_search(")", $arrElements); if (!is_false($pos) && !_is_null($pos)) { array_splice($arrElements, $pos, 1); } } // *empty array* | col | table.col | col AS alias | table.col AS alias | AS alias // table ? if (count($arrElements) >= 3 && $arrElements[1] == ".") { $colTables[$colIndex] = $arrElements[0]; array_splice($arrElements, 0, 2); } // *empty array* | col | col AS alias | AS alias // alias ? if (count($arrElements) >= 3 && strtoupper($arrElements[1]) == "AS") { $colAliases[$colIndex] = $arrElements[2]; array_splice($arrElements, 1, 2); } // *empty array* | col | AS alias // alias on function without column if (count($arrElements) >= 2 && strtoupper($arrElements[0]) == "AS") { $colAliases[$colIndex] = $arrElements[1]; array_splice($arrElements, 0, 2); } // *empty array* | col // column name ? if (count($arrElements) >= 1) { $colNames[$colIndex] = $arrElements[0]; array_splice($arrElements, 0, 1); } if (count($arrElements) > 0) { $errStr = "Unexpected Element(s): "; for ($i = 0; $i < count($arrElements); ++$i) { $errStr .= $arrElements[$i] . " "; } print_error_msg($errStr); return null; } } // skip FROM $this->skipNextElement(); // parse Tables $arrElements = array(); while ($this->parseNextElements(",", array("GROUP", "WHERE", "ORDER", "LIMIT", ";"), $arrElements)) { $tables[] = $arrElements[0]; if (count($arrElements) > 2 && strtoupper($arrElements[1]) == "AS") { $tableAliases[] = $arrElements[2]; // mysql like Table aliasing support, without AS } else { if (count($arrElements) > 1) { $tableAliases[] = $arrElements[1]; // end of mysql like Table aliasing support } else { $tableAliases[] = ""; } } } // parse Where statement (Raw, because the escape-chars are needend in the ExpressionParser) if (strtoupper($this->peekNextElement()) == "WHERE") { $this->skipNextElement(); while (!is_empty_str($elem = $this->peekNextElementRaw())) { if (strtoupper($elem) == "GROUP" || strtoupper($elem) == "ORDER" || $elem == ";" || strtoupper($elem) == "LIMIT") { break; } $this->skipNextElement(); // no " " on points if ($elem == ".") { remove_last_char($where_expr); $where_expr .= $elem; } else { $where_expr .= $elem . " "; } } } debug_print("WHERE EXPR: {$where_expr}<br>"); // parse GROUP BY $groupColumnIndex = 0; if (strtoupper($this->peekNextElement()) == "GROUP") { $this->skipNextElement(); if (strtoupper($this->parseNextElement()) != "BY") { print_error_msg("BY expected"); return null; } while (!is_empty_str($elem = $this->peekNextElement())) { if ($elem == ";" || strtoupper($elem) == "LIMIT" || strtoupper($elem) == "ORDER") { break; } $this->skipNextElement(); if ($elem == ",") { $groupColumnIndex++; } else { if (!isset($groupColumns[$groupColumnIndex])) { $groupColumns[$groupColumnIndex] = $elem; } else { $groupColumns[$groupColumnIndex] .= $elem; } } } } // parse ORDER BY $orderColumnIndex = 0; if (strtoupper($this->peekNextElement()) == "ORDER") { $this->skipNextElement(); if (strtoupper($this->parseNextElement()) != "BY") { print_error_msg("BY expected"); return null; } while (!is_empty_str($elem = $this->peekNextElement())) { if ($elem == ";" || strtoupper($elem) == "LIMIT") { break; } $this->skipNextElement(); if ($elem == ",") { $orderColumnIndex++; } else { if (strtoupper($elem) == "ASC") { $orderTypes[$orderColumnIndex] = ORDER_ASC; } else { if (strtoupper($elem) == "DESC") { $orderTypes[$orderColumnIndex] = ORDER_DESC; } else { if (!isset($orderColumns[$orderColumnIndex])) { $orderColumns[$orderColumnIndex] = $elem; } else { $orderColumns[$orderColumnIndex] .= $elem; } $orderTypes[$orderColumnIndex] = ORDER_ASC; } } } } } // parse LIMIT $limit = array(); if (strtoupper($this->peekNextElement()) == "LIMIT") { $this->skipNextElement(); while (!is_empty_str($elem = $this->peekNextElement())) { if ($elem == ";") { break; } $this->skipNextElement(); if ($elem != ",") { $limit[] = $elem; } } } $sqlObj = new SqlQuery("SELECT", $colNames, $tables, $colAliases, $colTables, $where_expr, $groupColumns, $orderColumns, $orderTypes, $limit); $sqlObj->tableAliases = $tableAliases; $sqlObj->colFuncs = $colFuncs; $sqlObj->distinct = $distinct; return $sqlObj; }