protected function parse(Query $qry) { $sql = ''; $args = []; $clauses = $qry->getClauses(); if (count($clauses)) { list($clauseSql, $clauseArgs) = $this->parseClauses($clauses); $sql .= " WHERE {$clauseSql}"; $args = array_merge($args, $clauseArgs); } if ($qry->isRandomSorted()) { $sql .= " ORDER BY RAND()"; } else { $sorts = $qry->getSortings(); if (count($sorts)) { $sortings = []; foreach ($sorts as $field => $direction) { if (is_int($field)) { $field = $direction; $direction = 'asc'; } switch (strtolower($direction)) { case 'asc': case 'ascending': case '+': case '<': case '^': default: $direction = 'ASC'; break; case 'desc': case 'descending': case '-': case '>': case 'v': $direction = 'DESC'; break; } $sortings[] = '`' . $this->inflectInputColumnName($field) . "` {$direction}"; } $sql .= " ORDER BY " . implode(',', $sortings); } } $limit = $qry->getLimit(); $limitStart = $qry->getLimitStart(); if (!is_null($limit)) { $limit = intval($limit); $limitStart = intval($limitStart ? $limitStart : 0); $sql .= " LIMIT {$limitStart},{$limit}"; } return [$sql, $args]; }