$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{
++ } else {
$port=false;
}
- $opts = array();
- $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' );
// do nothing if the connection already has been established
- if(!self::$PDO) {
- // Add the dsn according to the database type
+ if(!self::$DOCTRINE) {
+ $config = new \Doctrine\DBAL\Configuration();
switch($type) {
case 'sqlite':
- $dsn='sqlite2:'.$datadir.'/'.$name.'.db';
- break;
case 'sqlite3':
- $dsn='sqlite:'.$datadir.'/'.$name.'.db';
- break;
- case 'mysql':
- if($port) {
- $dsn='mysql:dbname='.$name.';host='.$host.';port='.$port;
- }else{
- $dsn='mysql:dbname='.$name.';host='.$host;
- }
- $opts[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES 'UTF8'";
- break;
- case 'pgsql':
- if($port) {
- $dsn='pgsql:dbname='.$name.';host='.$host.';port='.$port;
- }else{
- $dsn='pgsql:dbname='.$name.';host='.$host;
- }
- /**
- * Ugly fix for pg connections pbm when password use spaces
- */
- $e_user = addslashes($user);
- $e_password = addslashes($pass);
- $pass = $user = null;
- $dsn .= ";user='$e_user';password='$e_password'";
- /** END OF FIX***/
- break;
- case 'oci': // Oracle with PDO is unsupported
- if ($port) {
- $dsn = 'oci:dbname=//' . $host . ':' . $port . '/' . $name;
- } else {
- $dsn = 'oci:dbname=//' . $host . '/' . $name;
- }
- break;
- default:
- return false;
- }
- try{
- self::$PDO=new PDO($dsn, $user, $pass, $opts);
- }catch(PDOException $e) {
- OC_Template::printErrorPage( 'can not connect to database, using '.$type.'. ('.$e->getMessage().')' );
- }
- // We always, really always want associative arrays
- self::$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- self::$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- return true;
- }
-
- /**
- * connect to the database using mdb2
- */
- public static function connectMDB2() {
- if(self::$connection) {
- if(self::$backend==self::BACKEND_PDO) {
- self::disconnect();
- }else{
- return true;
- }
- }
- // The global data we need
- $name = OC_Config::getValue( "dbname", "owncloud" );
- $host = OC_Config::getValue( "dbhost", "" );
- $user = OC_Config::getValue( "dbuser", "" );
- $pass = OC_Config::getValue( "dbpassword", "" );
- $type = OC_Config::getValue( "dbtype", "sqlite" );
- $SERVERROOT=OC::$SERVERROOT;
- $datadir=OC_Config::getValue( "datadirectory", "$SERVERROOT/data" );
-
- // do nothing if the connection already has been established
- if(!self::$MDB2) {
- // Require MDB2.php (not required in the head of the file so we only load it when needed)
- require_once 'MDB2.php';
-
- // Prepare options array
- $options = array(
- 'portability' => MDB2_PORTABILITY_ALL - MDB2_PORTABILITY_FIX_CASE,
- 'log_line_break' => '<br>',
- 'idxname_format' => '%s',
- 'debug' => true,
- 'quote_identifier' => true );
-
- // Add the dsn according to the database type
- switch($type) {
- case 'sqlite':
- case 'sqlite3':
- $dsn = array(
- 'phptype' => $type,
- 'database' => "$datadir/$name.db",
- 'mode' => '0644'
+ $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' );
+ $connectionParams = array(
+ 'user' => $user,
+ 'password' => $pass,
+ 'path' => $datadir.'/'.$name.'.db',
+ 'driver' => 'pdo_sqlite',
);
break;
case 'mysql':
}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 PDOStatementWrapper($result);
+ $result=new DoctrineStatementWrapper($result);
}
return $result;
}
*/
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{
++ } 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);
}
/**
if( $CONFIG_DBTYPE == 'pgsql' ) { //mysql support it too but sqlite doesn't
$content = str_replace( '<default>0000-00-00 00:00:00</default>', '<default>CURRENT_TIMESTAMP</default>', $content );
}
- file_put_contents( $file2, $content );
- $op = self::$schema->updateDatabase($file2, $previousSchema, array(), false);
-
- //clean up memory
- unlink( $file2 );
-
- if (PEAR::isError($op)) {
- $error = $op->getMessage();
- $detail = $op->getDebugInfo();
- OC_Log::write('core', 'Failed to update database structure ('.$error.', '.$detail.')', OC_Log::FATAL);
- return false;
- }
- return true;
- }
-
- /**
- * @brief connects to a MDB2 database scheme
- * @returns bool
- *
- * Connects to a MDB2 database scheme
- */
- private static function connectScheme() {
- // We need a mdb2 database connection
- self::connectMDB2();
- self::$MDB2->loadModule('Manager');
- self::$MDB2->loadModule('Reverse');
-
- // Connect if this did not happen before
- if(!self::$schema) {
- require_once 'MDB2/Schema.php';
- self::$schema=MDB2_Schema::factory(self::$MDB2);
- }
-
- return true;
}
+ /**
+ * @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 false;
}
}
-
+
/**
* returns the error code and message as a string for logging
- * works with MDB2 and PDOException
+ * works with DoctrineException
* @param mixed $error
* @return string
*/