/** * Execute query. Warning! No escaping! * * @param string $query * @param bool $return Return something back? (Fetch) * @param int $fetchMode Fetch mode when returning something. * @param string $fetchClass Class to fetch * * @return mixed * * @throws \Exception */ public function raw($query, $return = false, $fetchMode = null, $fetchClass = null) { if (!$return) { return $this->exec($query); } $fetchMode = Database::normalizeFetchType($fetchMode); $statement = $this->query($query); if ($fetchMode === Database::FETCH_CLASS) { $statement->setFetchMode($fetchMode, $fetchClass); } else { $statement->setFetchMode($fetchMode); } return $statement->fetchAll(); }
/** * Execute the query, fetch if provided details to do so. * * @param bool $fetch Fetch on or off? * @param null $fetchMode Fetch mode (null for default) * @param null $fetchClass Fetch class (null for none) * * @throws QueryBuilderParseException * @throws QueryBuilderException * @throws QueryException * @throws DatabaseException * * @throws \Exception * * @return bool Boolean when fetching is off. * @return array|object|null Result when fetching is on. */ private function doExecute($fetch = false, $fetchMode = null, $fetchClass = null) { if ($fetch) { $fetchMode = Database::normalizeFetchType($fetchMode); } // Parse SQL and Bind $sql = $this->query->getSQL(); $bind = $this->query->getBind(); // Connection and execute $statement = $this->connection->prepare($sql); foreach ($bind as $idx => $value) { $statement->bindValue($idx + 1, $value, Database::typeOfValue($value)); } // Fetch if ($fetch) { if ($fetchMode === Database::FETCH_CLASS) { // Verify class new \ReflectionClass($fetchClass); $statement->setFetchMode($fetchMode, $fetchClass); } else { $statement->setFetchMode($fetchMode); } } // Execute $success = $statement->execute(); if (!$success) { return false; } if ($fetch && $fetch === 'single') { return $statement->fetch(); } if ($fetch) { return $statement->fetchAll(); } return true; }