// Optimize the query
$query = self::processQuery( $query );
-
+ if(OC_Config::getValue( "log_query", false)) {
+ OC_Log::write('core', 'DB prepare : '.$query, OC_Log::DEBUG);
+ }
self::connect();
// return the result
- if(self::$backend==self::BACKEND_MDB2) {
- $result = self::$connection->prepare( $query );
-
- // Die if we have an error (error means: bad query, not 0 results!)
- if( PEAR::isError($result)) {
- throw new DatabaseException($result->getMessage(), $query);
- }
- }else{
- try{
+ if (self::$backend == self::BACKEND_DOCTRINE) {
+ try {
$result=self::$connection->prepare($query);
- }catch(PDOException $e) {
- throw new DatabaseException($e->getMessage(), $query);
+ } catch(\Doctrine\DBAL\DBALException $e) {
+ throw new \DatabaseException($e->getMessage(), $query);
}
- $result=new PDOStatementWrapper($result);
+ $result=new DoctrineStatementWrapper($result);
}
if ((is_null($limit) || $limit == -1) and self::$cachingEnabled ) {
$type = OC_Config::getValue( "dbtype", "sqlite" );
$query = substr($query, 0, strlen($query) - 5);
try {
$stmt = self::prepare($query);
- $result = $stmt->execute();
+ $result = $stmt->execute($inserts);
-
- } catch(PDOException $e) {
+ } catch(\Doctrine\DBAL\DBALException $e) {
$entry = 'DB Error: "'.$e->getMessage() . '"<br />';
$entry .= 'Offending command was: ' . $query . '<br />';
OC_Log::write('core', $entry, OC_Log::FATAL);
error_log('DB error: '.$entry);
OC_Template::printErrorPage( $entry );
+ return false;
}
- if($result->numRows() == 0) {
- $query = 'INSERT INTO "' . $table . '" ("'
- . implode('","', array_keys($input)) . '") VALUES("'
- . implode('","', array_values($input)) . '")';
+ if((int)$result->numRows() === 0) {
+ $query = 'INSERT INTO `' . $table . '` (`'
+ . implode('`,`', array_keys($input)) . '`) VALUES('
+ . str_repeat('?,', count($input)-1).'? ' . ')';
} else {
return true;
}
}
$query = substr($query, 0, strlen($query) - 5);
$query .= ' HAVING COUNT(*) = 0';
+ $inserts = array_merge($inserts, $inserts);
}
- // TODO: oci should be use " (quote) instead of ` (backtick).
- //OC_Log::write('core', __METHOD__ . ', type: ' . $type . ', query: ' . $query, OC_Log::DEBUG);
-
try {
$result = self::prepare($query);
- } catch(PDOException $e) {
+ } catch(\Doctrine\DBAL\DBALException $e) {
$entry = 'DB Error: "'.$e->getMessage() . '"<br />';
$entry .= 'Offending command was: ' . $query.'<br />';
OC_Log::write('core', $entry, OC_Log::FATAL);
error_log('DB error: ' . $entry);
OC_Template::printErrorPage( $entry );
+ return false;
}
- return $result->execute();
+ return $result->execute($inserts);
}
/**
$this->statement=$statement;
}
- $query = OC_DB::prepare("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM);
+ /**
+ * pass all other function directly to the \Doctrine\DBAL\Driver\Statement
+ */
+ public function __call($name,$arguments) {
+ return call_user_func_array(array($this->statement,$name), $arguments);
+ }
+
+ /**
+ * provide numRows
+ */
+ public function numRows() {
+ $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
+ $queryString = $this->statement->getWrappedStatement()->queryString;
+ if (preg_match($regex, $queryString, $output) > 0) {
++ $query = OC_DB::prepare("SELECT COUNT(*) FROM {$output[1]}");
+ return $query->execute($this->lastArguments)->fetchColumn();
+ }else{
+ return $this->statement->rowCount();
+ }
+ }
+
/**
* make execute return the result instead of a bool
*/