Pimcore\Model\Tool\CustomReport\Adapter\Sql::buildQueryString PHP Method

buildQueryString() protected method

protected buildQueryString ( $config, boolean $ignoreSelectAndGroupBy = false, null $drillDownFilters = null, null $selectField = null ) : string
$config
$ignoreSelectAndGroupBy boolean
$drillDownFilters null
$selectField null
return string
    protected function buildQueryString($config, $ignoreSelectAndGroupBy = false, $drillDownFilters = null, $selectField = null)
    {
        $config = (array) $config;
        $sql = "";
        if ($config["sql"] && !$ignoreSelectAndGroupBy) {
            if (strpos(strtoupper(trim($config["sql"])), "SELECT") === false || strpos(strtoupper(trim($config["sql"])), "SELECT") > 5) {
                $sql .= "SELECT ";
            }
            $sql .= str_replace("\n", " ", $config["sql"]);
        } elseif ($selectField) {
            $db = Db::get();
            $sql .= "SELECT " . $db->quoteIdentifier($selectField);
        } else {
            $sql .= "SELECT *";
        }
        if ($config["from"]) {
            if (strpos(strtoupper(trim($config["from"])), "FROM") === false) {
                $sql .= " FROM ";
            }
            $sql .= " " . str_replace("\n", " ", $config["from"]);
        }
        if ($config["where"] || $drillDownFilters) {
            $whereParts = [];
            if ($config["where"]) {
                $whereParts[] = "(" . str_replace("\n", " ", $config["where"]) . ")";
            }
            if ($drillDownFilters) {
                $db = Db::get();
                foreach ($drillDownFilters as $field => $value) {
                    if ($value !== "" && $value !== null) {
                        $whereParts[] = "`{$field}` = " . $db->quote($value);
                    }
                }
            }
            if ($whereParts) {
                if ($config["where"]) {
                    $sql .= " WHERE ";
                } else {
                    if (strpos(strtoupper(trim($config["where"])), "WHERE") === false) {
                        $sql .= " WHERE ";
                    }
                }
                $sql .= " " . implode(" AND ", $whereParts);
            }
        }
        if ($config["groupby"] && !$ignoreSelectAndGroupBy) {
            if (strpos(strtoupper($config["groupby"]), "GROUP BY") === false) {
                $sql .= " GROUP BY ";
            }
            $sql .= " " . str_replace("\n", " ", $config["groupby"]);
        }
        return $sql;
    }