* SQL query via Doctrine prepare(), needs to be execute()'d!
*/
static public function prepare( $query , $limit = null, $offset = null, $isManipulation = null) {
- // 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();
if ($isManipulation === null) {
return $result;
}
- /**
- * @brief does minor changes to query
- * @param string $query Query string
- * @return string corrected query string
- *
- * This function replaces *PREFIX* with the value of $CONFIG_DBTABLEPREFIX
- * and replaces the ` with ' or " according to the database driver.
- */
- private static function processQuery( $query ) {
- self::connect();
- // We need Database type
- if(is_null(self::$type)) {
- self::$type=OC_Config::getValue( "dbtype", "sqlite" );
- }
- $type = self::$type;
-
- // differences in escaping of table names ('`' for mysql) and getting the current timestamp
- if( $type == 'sqlite' || $type == 'sqlite3' ) {
- $query = str_replace( '`', '"', $query );
- $query = str_ireplace( 'NOW()', 'datetime(\'now\')', $query );
- $query = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $query );
- } elseif( $type == 'pgsql' ) {
- $query = str_replace( '`', '"', $query );
- $query = str_ireplace( 'UNIX_TIMESTAMP()', 'cast(extract(epoch from current_timestamp) as integer)',
- $query );
- } elseif( $type == 'oci' ) {
- $query = str_replace( '`', '"', $query );
- $query = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $query );
- $query = str_ireplace( 'UNIX_TIMESTAMP()', "(cast(sys_extract_utc(systimestamp) as date) - date'1970-01-01') * 86400", $query );
- }elseif( $type == 'mssql' ) {
- $query = preg_replace( "/\`(.*?)`/", "[$1]", $query );
- $query = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $query );
- $query = str_replace( 'LENGTH(', 'LEN(', $query );
- $query = str_replace( 'SUBSTR(', 'SUBSTRING(', $query );
- $query = str_ireplace( 'UNIX_TIMESTAMP()', 'DATEDIFF(second,{d \'1970-01-01\'},GETDATE())', $query );
- }
-
- return $query;
- }
-
- return $query;
- }
-
/**
* @brief drop a table
* @param string $tableName the table to drop
public function lastInsertId($table) {
return $this->conn->realLastInsertId($table);
}
+
+ public function fixupStatement($statement) {
+ return $statement;
+ }
}
}
return $this->conn->lastInsertId($table);
}
+
+ public function fixupStatement($statement) {
+ $statement = str_replace( '`', '"', $statement );
+ $statement = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $statement );
+ $statement = str_ireplace( 'UNIX_TIMESTAMP()', "(cast(sys_extract_utc(systimestamp) as date) - date'1970-01-01') * 86400", $statement );
+ return $statement;
+ }
}
public function lastInsertId($table) {
return $this->conn->fetchColumn('SELECT lastval()');
}
+
+ public function fixupStatement($statement) {
+ $statement = str_replace( '`', '"', $statement );
+ $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'cast(extract(epoch from current_timestamp) as integer)', $statement );
+ return $statement;
+ }
}
namespace OC\DB;
class AdapterSqlite extends Adapter {
+ public function fixupStatement($statement) {
+ $statement = str_replace( '`', '"', $statement );
+ $statement = str_ireplace( 'NOW()', 'datetime(\'now\')', $statement );
+ $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
+ return $statement;
+ }
}
}
return $this->conn->lastInsertId($table);
}
+
+ public function fixupStatement($statement) {
+ $statement = preg_replace( "/\`(.*?)`/", "[$1]", $statement );
+ $statement = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $statement );
+ $statement = str_replace( 'LENGTH(', 'LEN(', $statement );
+ $statement = str_replace( 'SUBSTR(', 'SUBSTRING(', $statement );
+ $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'DATEDIFF(second,{d \'1970-01-01\'},GETDATE())', $statement );
+ return $statement;
+ }
}
*/
public function prepare( $statement, $limit=null, $offset=null ) {
$statement = $this->replaceTablePrefix($statement);
+ $statement = $this->adapter->fixupStatement($statement);
+
if ($limit === -1) {
$limit = null;
}
}
}
$rawQuery = $statement;
+ if(\OC_Config::getValue( "log_query", false)) {
+ \OC_Log::write('core', 'DB prepare : '.$statement, \OC_Log::DEBUG);
+ }
$result = parent::prepare($statement);
if (is_null($limit) && $this->cachingQueryStatementEnabled) {
$this->preparedQueries[$rawQuery] = $result;
public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
{
$query = $this->replaceTablePrefix($query);
- // TODO: fixup
+ $query = $this->adapter->fixupStatement($query);
return parent::executeQuery($query, $params, $types, $qcp);
}
public function executeUpdate($query, array $params = array(), array $types = array())
{
$query = $this->replaceTablePrefix($query);
- // TODO: fixup
+ $query = $this->adapter->fixupStatement($query);
return parent::executeUpdate($query, $params, $types);
}