summaryrefslogtreecommitdiffstats
path: root/lib/db.php
diff options
context:
space:
mode:
authorBart Visscher <bartv@thisnet.nl>2012-12-07 11:35:24 +0100
committerBart Visscher <bartv@thisnet.nl>2012-12-07 11:35:57 +0100
commit89cd8a3fc75f9b96d295ae4292ee92102c3138a6 (patch)
treeaab017f29362c92e5a3e28c5b84960ee6e0072ea /lib/db.php
parent8bb62e74bd14e4fec6ed1cd371d21af0f618076b (diff)
parentc487d9fc7e5b1c31069819cc8adc920ab9660513 (diff)
downloadnextcloud-server-89cd8a3fc75f9b96d295ae4292ee92102c3138a6.tar.gz
nextcloud-server-89cd8a3fc75f9b96d295ae4292ee92102c3138a6.zip
Merge branch 'master' into doctrine
Conflicts: lib/base.php lib/db.php
Diffstat (limited to 'lib/db.php')
-rw-r--r--lib/db.php99
1 files changed, 89 insertions, 10 deletions
diff --git a/lib/db.php b/lib/db.php
index a6500a2e3bd..2e21a11d545 100644
--- a/lib/db.php
+++ b/lib/db.php
@@ -92,8 +92,8 @@ class OC_DB {
$pass = OC_Config::getValue( "dbpassword", "" );
$type = OC_Config::getValue( "dbtype", "sqlite" );
if(strpos($host, ':')) {
- list($host, $port)=explode(':', $host,2);
- }else{
+ list($host, $port)=explode(':', $host, 2);
+ } else {
$port=false;
}
@@ -192,9 +192,9 @@ class OC_DB {
}catch(PDOException $e) {
$entry = 'DB Error: "'.$e->getMessage().'"<br />';
$entry .= 'Offending command was: '.htmlentities($query).'<br />';
- OC_Log::write('core', $entry,OC_Log::FATAL);
+ OC_Log::write('core', $entry, OC_Log::FATAL);
error_log('DB error: '.$entry);
- die( $entry );
+ OC_Template::printErrorPage( $entry );
}
$result=new DoctrineStatementWrapper($result);
}
@@ -213,12 +213,19 @@ class OC_DB {
*/
public static function insertid($table=null) {
self::connect();
- if($table !== null) {
- $prefix = OC_Config::getValue( "dbtableprefix", "oc_" );
- $suffix = OC_Config::getValue( "dbsequencesuffix", "_id_seq" );
- $table = str_replace( '*PREFIX*', $prefix, $table ).$suffix;
+ $type = OC_Config::getValue( "dbtype", "sqlite" );
+ if( $type == 'pgsql' ) {
+ $query = self::prepare('SELECT lastval() AS id');
+ $row = $query->execute()->fetchRow();
+ return $row['id'];
+ } else {
+ if($table !== null) {
+ $prefix = OC_Config::getValue( "dbtableprefix", "oc_" );
+ $suffix = OC_Config::getValue( "dbsequencesuffix", "_id_seq" );
+ $table = str_replace( '*PREFIX*', $prefix, $table ).$suffix;
+ }
+ return self::$connection->lastInsertId($table);
}
- return self::$connection->lastInsertId($table);
}
/**
@@ -301,6 +308,78 @@ class OC_DB {
}
/**
+ * @brief Insert a row if a matching row doesn't exists.
+ * @param string $table. The table to insert into in the form '*PREFIX*tableName'
+ * @param array $input. An array of fieldname/value pairs
+ * @returns The return value from PDOStatementWrapper->execute()
+ */
+ public static function insertIfNotExist($table, $input) {
+ self::connect();
+ $prefix = OC_Config::getValue( "dbtableprefix", "oc_" );
+ $table = str_replace( '*PREFIX*', $prefix, $table );
+
+ if(is_null(self::$type)) {
+ self::$type=OC_Config::getValue( "dbtype", "sqlite" );
+ }
+ $type = self::$type;
+
+ $query = '';
+ // differences in escaping of table names ('`' for mysql) and getting the current timestamp
+ if( $type == 'sqlite' || $type == 'sqlite3' ) {
+ // NOTE: For SQLite we have to use this clumsy approach
+ // otherwise all fieldnames used must have a unique key.
+ $query = 'SELECT * FROM "' . $table . '" WHERE ';
+ foreach($input as $key => $value) {
+ $query .= $key . " = '" . $value . '\' AND ';
+ }
+ $query = substr($query, 0, strlen($query) - 5);
+ try {
+ $stmt = self::prepare($query);
+ $result = $stmt->execute();
+ } catch(PDOException $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 );
+ }
+
+ if($result->numRows() == 0) {
+ $query = 'INSERT INTO "' . $table . '" ("'
+ . implode('","', array_keys($input)) . '") VALUES("'
+ . implode('","', array_values($input)) . '")';
+ } else {
+ return true;
+ }
+ } elseif( $type == 'pgsql' || $type == 'oci' || $type == 'mysql') {
+ $query = 'INSERT INTO `' .$table . '` ('
+ . implode(',', array_keys($input)) . ') SELECT \''
+ . implode('\',\'', array_values($input)) . '\' FROM ' . $table . ' WHERE ';
+
+ foreach($input as $key => $value) {
+ $query .= $key . " = '" . $value . '\' AND ';
+ }
+ $query = substr($query, 0, strlen($query) - 5);
+ $query .= ' HAVING COUNT(*) = 0';
+ }
+
+ // 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) {
+ $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 $result->execute();
+ }
+
+ /**
* @brief does minor changes to query
* @param string $query Query string
* @return string corrected query string
@@ -403,7 +482,7 @@ class OC_DB {
return false;
}
}
-
+
/**
* returns the error code and message as a string for logging
* works with DoctrineException