public function query($query, $args = array())
{
if ($this->pdo_statement != null) {
$this->pdo_statement->closeCursor();
}
// Allow plugins to modify the query
$query = Plugins::filter('query', $query, $args);
// Translate the query for the database engine
$query = $this->sql_t($query, $args);
// Replace braced table names in the query with their prefixed counterparts
$query = self::filter_tables($query);
// Allow plugins to modify the query after it has been processed
$query = Plugins::filter('query_postprocess', $query, $args);
if ($this->pdo_statement = $this->pdo->prepare($query)) {
if ($this->fetch_mode == \PDO::FETCH_CLASS) {
/* Try to get the result class autoloaded. */
if (!class_exists($this->fetch_class_name, true)) {
$class_name = $this->fetch_class_name;
// @todo This is a GIANT namespace kludge, replacing Model class names with no namespace that don't exist with a default prefixed class
if (strpos($class_name, '\\') == false) {
$class_name = '\\Habari\\' . $class_name;
$this->fetch_class_name = $class_name;
if (!class_exists($this->fetch_class_name, true)) {
throw new \Exception('The model class that was specified for data retreival could not be loaded.');
}
}
}
/* Ensure that the class is actually available now, otherwise segfault happens (if we haven't died earlier anyway). */
if (class_exists($this->fetch_class_name)) {
$this->pdo_statement->setFetchMode(\PDO::FETCH_CLASS, $this->fetch_class_name, array());
} else {
/* Die gracefully before the segfault occurs */
echo '<br><br>' . _t('Attempt to fetch in class mode with a non-included class') . '<br><br>';
return false;
}
} else {
$this->pdo_statement->setFetchMode($this->fetch_mode);
}
/* If we are profiling, then time the query */
if ($this->keep_profile) {
$profile = new QueryProfile($query);
$profile->params = $args;
$profile->start();
}
if (!$this->pdo_statement->execute($args)) {
$this->add_error(array('query' => $query, 'error' => $this->pdo_statement->errorInfo()));
return false;
}
if ($this->keep_profile) {
$profile->stop();
$this->profiles[] = $profile;
}
return true;
} else {
$this->add_error(array('query' => $query, 'error' => $this->pdo->errorInfo()));
return false;
}
}