src/PicORM/QueryBuilder.php
public function buildQuery()
    {
        $where   = implode(' ', $this->_where);
        $join    = implode(' ', $this->_join);
        $orderBy = count($this->_orderBy) > 0 ? sprintf('ORDER BY %s', implode(',', $this->_orderBy)) : '';
        $groupBy = count($this->_groupBy) > 0 ? sprintf('GROUP BY %s', implode(',', $this->_groupBy)) : '';
        $hint    = count($this->_queryHint) > 0 ? sprintf(' %s ', implode(',', $this->_queryHint)) : '';
        $limit   = $this->_limit;
        $query   = '';

        switch ($this->_queryType) {
            case self::SELECT:
                $select = implode(',', $this->_select);
                $from   = !empty($this->_from) ? sprintf('FROM %s', $this->_from) : '';
                $query  = sprintf(
                    "SELECT %s %s %s %s %s %s %s %s %s",
                    $hint,
                    $select,
                    $from,
                    $join,
                    $where,
                    $groupBy,
                    $this->_having,
                    $orderBy,
                    $limit
                );
                break;
            case self::INSERT:
                $values = '';

                // save lastInsertValues
                if (!empty($this->_lastInsertValues)) {
                    $this->_insertValues[]   = $this->_lastInsertValues;
                    $this->_lastInsertValues = array();
                }

                // grab field name from first line
                $keys   = array_keys($this->_insertValues[0]);
                $nbKeys = count($keys) - 1;

                // reorder values and build string
                foreach ($this->_insertValues as $k => $oneVal) {
                    if ($k > 0) {
                        $values .= ',';
                    }
                    $values .= '(';
                    for ($i = 0; $i <= $nbKeys; $i++) {
                        $values .= $oneVal[$keys[$i]];
                        if ($i != $nbKeys) {
                            $values .= ',';
                        }
                    }
                    $values .= ')';
                }

                // build insert query string
                $query = sprintf(
                    "INSERT INTO %s (%s) VALUES %s",
                    $this->_insert,
                    implode(',', $keys),
                    $values
                );
                break;
            case self::UPDATE:
                $strSet = '';

                // test if params number match the values
                if (count($this->_setName) != count($this->_setVal)) {
                    throw new Exception("number of params / values is not correct");
                }

                // build SET values
                $maxParams = count($this->_setName) - 1;
                for ($i = 0; $i <= $maxParams; $i++) {
                    $strSet .= sprintf('%s = %s', $this->_setName[$i], $this->_setVal[$i]);
                    if ($i !== $maxParams) {
                        $strSet .= ',';
                    }
                }

                // build update query string
                $query = sprintf(
                    "UPDATE %s %s SET %s %s %s %s",
                    $this->_update,
                    $join,
                    $strSet,
                    $where,
                    $orderBy,
                    $limit
                );
                break;
            case self::DELETE:

                // build delete query string
                $query = sprintf(
                    "DELETE %s FROM %s %s %s %s %s",
                    $this->_delete,
                    $this->_delete,
                    $join,
                    $where,
                    $orderBy,
                    $limit
                );
                break;
        }

        return trim($query);
    }
public function buildQuery()
    {
        $where   = implode(' ', $this->_where);
        $join    = implode(' ', $this->_join);
        $orderBy = count($this->_orderBy) > 0 ? sprintf('ORDER BY %s', implode(',', $this->_orderBy)) : '';
        $groupBy = count($this->_groupBy) > 0 ? sprintf('GROUP BY %s', implode(',', $this->_groupBy)) : '';
        $hint    = count($this->_queryHint) > 0 ? sprintf(' %s ', implode(',', $this->_queryHint)) : '';
        $limit   = $this->_limit;
        $query   = '';

        switch ($this->_queryType) {
            case self::SELECT:
                $select = implode(',', $this->_select);
                $from   = !empty($this->_from) ? sprintf('FROM %s', $this->_from) : '';
                $query  = sprintf(
                    "SELECT %s %s %s %s %s %s %s %s %s",
                    $hint,
                    $select,
                    $from,
                    $join,
                    $where,
                    $groupBy,
                    $this->_having,
                    $orderBy,
                    $limit
                );
                break;
            case self::INSERT:
                $values = '';

                // save lastInsertValues
                if (!empty($this->_lastInsertValues)) {
                    $this->_insertValues[]   = $this->_lastInsertValues;
                    $this->_lastInsertValues = array();
                }

                // grab field name from first line
                $keys   = array_keys($this->_insertValues[0]);
                $nbKeys = count($keys) - 1;

                // reorder values and build string
                foreach ($this->_insertValues as $k => $oneVal) {
                    if ($k > 0) {
                        $values .= ',';
                    }
                    $values .= '(';
                    for ($i = 0; $i <= $nbKeys; $i++) {
                        $values .= $oneVal[$keys[$i]];
                        if ($i != $nbKeys) {
                            $values .= ',';
                        }
                    }
                    $values .= ')';
                }

                // build insert query string
                $query = sprintf(
                    "INSERT INTO %s (%s) VALUES %s",
                    $this->_insert,
                    implode(',', $keys),
                    $values
                );
                break;
            case self::UPDATE:
                $strSet = '';

                // test if params number match the values
                if (count($this->_setName) != count($this->_setVal)) {
                    throw new Exception("number of params / values is not correct");
                }

                // build SET values
                $maxParams = count($this->_setName) - 1;
                for ($i = 0; $i <= $maxParams; $i++) {
                    $strSet .= sprintf('%s = %s', $this->_setName[$i], $this->_setVal[$i]);
                    if ($i !== $maxParams) {
                        $strSet .= ',';
                    }
                }

                // build update query string
                $query = sprintf(
                    "UPDATE %s %s SET %s %s %s %s",
                    $this->_update,
                    $join,
                    $strSet,
                    $where,
                    $orderBy,
                    $limit
                );
                break;
            case self::DELETE:

                // build delete query string
                $query = sprintf(
                    "DELETE %s FROM %s %s %s %s %s",
                    $this->_delete,
                    $this->_delete,
                    $join,
                    $where,
                    $orderBy,
                    $limit
                );
                break;
        }

        return trim($query);
    }
Build query string from setted data

@return string
@throws Exception