Respect coding style

This commit is contained in:
Thomas Müller 2012-09-05 15:49:42 +03:00
parent e5cb638c09
commit 24977d0fd0
1 changed files with 57 additions and 57 deletions

View File

@ -42,14 +42,15 @@ class OC_DB {
* @return BACKEND_MDB2 or BACKEND_PDO * @return BACKEND_MDB2 or BACKEND_PDO
*/ */
private static function getDBBackend(){ private static function getDBBackend(){
if(class_exists('PDO') && OC_Config::getValue('installed', false)){//check if we can use PDO, else use MDB2 (installation always needs to be done my mdb2) //check if we can use PDO, else use MDB2 (installation always needs to be done my mdb2)
if(class_exists('PDO') && OC_Config::getValue('installed', false)) {
$type = OC_Config::getValue( "dbtype", "sqlite" ); $type = OC_Config::getValue( "dbtype", "sqlite" );
if($type=='oci') { //oracle also always needs mdb2 if($type=='oci') { //oracle also always needs mdb2
return self::BACKEND_MDB2; return self::BACKEND_MDB2;
} }
if($type=='sqlite3') $type='sqlite'; if($type=='sqlite3') $type='sqlite';
$drivers=PDO::getAvailableDrivers(); $drivers=PDO::getAvailableDrivers();
if(array_search($type,$drivers)!==false){ if(array_search($type, $drivers)!==false) {
return self::BACKEND_PDO; return self::BACKEND_PDO;
} }
} }
@ -63,13 +64,13 @@ class OC_DB {
* Connects to the database as specified in config.php * Connects to the database as specified in config.php
*/ */
public static function connect($backend=null){ public static function connect($backend=null){
if(self::$connection){ if(self::$connection) {
return; return;
} }
if(is_null($backend)){ if(is_null($backend)) {
$backend=self::getDBBackend(); $backend=self::getDBBackend();
} }
if($backend==self::BACKEND_PDO){ if($backend==self::BACKEND_PDO) {
self::connectPDO(); self::connectPDO();
self::$connection=self::$PDO; self::$connection=self::$PDO;
self::$backend=self::BACKEND_PDO; self::$backend=self::BACKEND_PDO;
@ -84,8 +85,8 @@ class OC_DB {
* connect to the database using pdo * connect to the database using pdo
*/ */
public static function connectPDO(){ public static function connectPDO(){
if(self::$connection){ if(self::$connection) {
if(self::$backend==self::BACKEND_MDB2){ if(self::$backend==self::BACKEND_MDB2) {
self::disconnect(); self::disconnect();
}else{ }else{
return; return;
@ -97,8 +98,8 @@ class OC_DB {
$user = OC_Config::getValue( "dbuser", "" ); $user = OC_Config::getValue( "dbuser", "" );
$pass = OC_Config::getValue( "dbpassword", "" ); $pass = OC_Config::getValue( "dbpassword", "" );
$type = OC_Config::getValue( "dbtype", "sqlite" ); $type = OC_Config::getValue( "dbtype", "sqlite" );
if(strpos($host,':')){ if(strpos($host, ':')) {
list($host,$port)=explode(':',$host,2); list($host, $port)=explode(':', $host,2);
}else{ }else{
$port=false; $port=false;
} }
@ -106,9 +107,9 @@ class OC_DB {
$datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' );
// do nothing if the connection already has been established // do nothing if the connection already has been established
if(!self::$PDO){ if(!self::$PDO) {
// Add the dsn according to the database type // Add the dsn according to the database type
switch($type){ switch($type) {
case 'sqlite': case 'sqlite':
$dsn='sqlite2:'.$datadir.'/'.$name.'.db'; $dsn='sqlite2:'.$datadir.'/'.$name.'.db';
break; break;
@ -116,7 +117,7 @@ class OC_DB {
$dsn='sqlite:'.$datadir.'/'.$name.'.db'; $dsn='sqlite:'.$datadir.'/'.$name.'.db';
break; break;
case 'mysql': case 'mysql':
if($port){ if($port) {
$dsn='mysql:dbname='.$name.';host='.$host.';port='.$port; $dsn='mysql:dbname='.$name.';host='.$host.';port='.$port;
}else{ }else{
$dsn='mysql:dbname='.$name.';host='.$host; $dsn='mysql:dbname='.$name.';host='.$host;
@ -124,7 +125,7 @@ class OC_DB {
$opts[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES 'UTF8'"; $opts[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES 'UTF8'";
break; break;
case 'pgsql': case 'pgsql':
if($port){ if($port) {
$dsn='pgsql:dbname='.$name.';host='.$host.';port='.$port; $dsn='pgsql:dbname='.$name.';host='.$host.';port='.$port;
}else{ }else{
$dsn='pgsql:dbname='.$name.';host='.$host; $dsn='pgsql:dbname='.$name.';host='.$host;
@ -139,22 +140,22 @@ class OC_DB {
/** END OF FIX***/ /** END OF FIX***/
break; break;
case 'oci': // Oracle with PDO is unsupported case 'oci': // Oracle with PDO is unsupported
if ($port) { if ($port) {
$dsn = 'oci:dbname=//' . $host . ':' . $port . '/' . $name; $dsn = 'oci:dbname=//' . $host . ':' . $port . '/' . $name;
} else { } else {
$dsn = 'oci:dbname=//' . $host . '/' . $name; $dsn = 'oci:dbname=//' . $host . '/' . $name;
} }
break; break;
} }
try{ try{
self::$PDO=new PDO($dsn,$user,$pass,$opts); self::$PDO=new PDO($dsn, $user, $pass, $opts);
}catch(PDOException $e){ }catch(PDOException $e){
echo( '<b>can not connect to database, using '.$type.'. ('.$e->getMessage().')</center>'); echo( '<b>can not connect to database, using '.$type.'. ('.$e->getMessage().')</center>');
die(); die();
} }
// We always, really always want associative arrays // We always, really always want associative arrays
self::$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); self::$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
self::$PDO->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); self::$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} }
return true; return true;
} }
@ -162,9 +163,9 @@ class OC_DB {
/** /**
* connect to the database using mdb2 * connect to the database using mdb2
*/ */
public static function connectMDB2(){ public static function connectMDB2() {
if(self::$connection){ if(self::$connection) {
if(self::$backend==self::BACKEND_PDO){ if(self::$backend==self::BACKEND_PDO) {
self::disconnect(); self::disconnect();
}else{ }else{
return; return;
@ -180,9 +181,9 @@ class OC_DB {
$datadir=OC_Config::getValue( "datadirectory", "$SERVERROOT/data" ); $datadir=OC_Config::getValue( "datadirectory", "$SERVERROOT/data" );
// do nothing if the connection already has been established // do nothing if the connection already has been established
if(!self::$MDB2){ if(!self::$MDB2) {
// Require MDB2.php (not required in the head of the file so we only load it when needed) // Require MDB2.php (not required in the head of the file so we only load it when needed)
require_once('MDB2.php'); require_once 'MDB2.php';
// Prepare options array // Prepare options array
$options = array( $options = array(
@ -239,10 +240,10 @@ class OC_DB {
self::$MDB2 = MDB2::factory( $dsn, $options ); self::$MDB2 = MDB2::factory( $dsn, $options );
// Die if we could not connect // Die if we could not connect
if( PEAR::isError( self::$MDB2 )){ if( PEAR::isError( self::$MDB2 )) {
echo( '<b>can not connect to database, using '.$type.'. ('.self::$MDB2->getUserInfo().')</center>'); echo( '<b>can not connect to database, using '.$type.'. ('.self::$MDB2->getUserInfo().')</center>');
OC_Log::write('core',self::$MDB2->getUserInfo(),OC_Log::FATAL); OC_Log::write('core', self::$MDB2->getUserInfo(), OC_Log::FATAL);
OC_Log::write('core',self::$MDB2->getMessage(),OC_Log::FATAL); OC_Log::write('core', self::$MDB2->getMessage(), OC_Log::FATAL);
die( $error ); die( $error );
} }
@ -290,14 +291,14 @@ class OC_DB {
self::connect(); self::connect();
// return the result // return the result
if(self::$backend==self::BACKEND_MDB2){ if(self::$backend==self::BACKEND_MDB2) {
$result = self::$connection->prepare( $query ); $result = self::$connection->prepare( $query );
// Die if we have an error (error means: bad query, not 0 results!) // Die if we have an error (error means: bad query, not 0 results!)
if( PEAR::isError($result)) { if( PEAR::isError($result)) {
$entry = 'DB Error: "'.$result->getMessage().'"<br />'; $entry = 'DB Error: "'.$result->getMessage().'"<br />';
$entry .= 'Offending command was: '.$query.'<br />'; $entry .= 'Offending command was: '.$query.'<br />';
OC_Log::write('core',$entry,OC_Log::FATAL); OC_Log::write('core', $entry,OC_Log::FATAL);
error_log('DB error: '.$entry); error_log('DB error: '.$entry);
die( $entry ); die( $entry );
} }
@ -307,7 +308,7 @@ class OC_DB {
}catch(PDOException $e){ }catch(PDOException $e){
$entry = 'DB Error: "'.$e->getMessage().'"<br />'; $entry = 'DB Error: "'.$e->getMessage().'"<br />';
$entry .= 'Offending command was: '.$query.'<br />'; $entry .= 'Offending command was: '.$query.'<br />';
OC_Log::write('core',$entry,OC_Log::FATAL); OC_Log::write('core', $entry,OC_Log::FATAL);
error_log('DB error: '.$entry); error_log('DB error: '.$entry);
die( $entry ); die( $entry );
} }
@ -328,7 +329,7 @@ class OC_DB {
*/ */
public static function insertid($table=null){ public static function insertid($table=null){
self::connect(); self::connect();
if($table !== null){ if($table !== null) {
$prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); $prefix = OC_Config::getValue( "dbtableprefix", "oc_" );
$suffix = OC_Config::getValue( "dbsequencesuffix", "_id_seq" ); $suffix = OC_Config::getValue( "dbsequencesuffix", "_id_seq" );
$table = str_replace( '*PREFIX*', $prefix, $table ); $table = str_replace( '*PREFIX*', $prefix, $table );
@ -344,8 +345,8 @@ class OC_DB {
*/ */
public static function disconnect(){ public static function disconnect(){
// Cut connection if required // Cut connection if required
if(self::$connection){ if(self::$connection) {
if(self::$backend==self::BACKEND_MDB2){ if(self::$backend==self::BACKEND_MDB2) {
self::$connection->disconnect(); self::$connection->disconnect();
} }
self::$connection=false; self::$connection=false;
@ -407,7 +408,7 @@ class OC_DB {
* http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions037.htm * http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions037.htm
*/ */
if( $CONFIG_DBTYPE == 'pgsql' ){ //mysql support it too but sqlite doesn't 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 ); $content = str_replace( '<default>0000-00-00 00:00:00</default>', '<default>CURRENT_TIMESTAMP</default>', $content );
} }
@ -420,10 +421,10 @@ class OC_DB {
unlink( $file2 ); unlink( $file2 );
// Die in case something went wrong // Die in case something went wrong
if( $definition instanceof MDB2_Schema_Error ){ if( $definition instanceof MDB2_Schema_Error ) {
die( $definition->getMessage().': '.$definition->getUserInfo()); die( $definition->getMessage().': '.$definition->getUserInfo());
} }
if(OC_Config::getValue('dbtype','sqlite')==='oci'){ if(OC_Config::getValue('dbtype', 'sqlite')==='oci') {
unset($definition['charset']); //or MDB2 tries SHUTDOWN IMMEDIATE unset($definition['charset']); //or MDB2 tries SHUTDOWN IMMEDIATE
$oldname = $definition['name']; $oldname = $definition['name'];
$definition['name']=OC_Config::getValue( "dbuser", $oldname ); $definition['name']=OC_Config::getValue( "dbuser", $oldname );
@ -432,7 +433,7 @@ class OC_DB {
$ret=self::$schema->createDatabase( $definition ); $ret=self::$schema->createDatabase( $definition );
// Die in case something went wrong // Die in case something went wrong
if( $ret instanceof MDB2_Error ){ if( $ret instanceof MDB2_Error ) {
echo (self::$MDB2->getDebugOutput()); echo (self::$MDB2->getDebugOutput());
die ($ret->getMessage() . ': ' . $ret->getUserInfo()); die ($ret->getMessage() . ': ' . $ret->getUserInfo());
} }
@ -456,7 +457,7 @@ class OC_DB {
$previousSchema = self::$schema->getDefinitionFromDatabase(); $previousSchema = self::$schema->getDefinitionFromDatabase();
if (PEAR::isError($previousSchema)) { if (PEAR::isError($previousSchema)) {
$error = $previousSchema->getMessage(); $error = $previousSchema->getMessage();
OC_Log::write('core','Failed to get existing database structure for upgrading ('.$error.')',OC_Log::FATAL); OC_Log::write('core', 'Failed to get existing database structure for upgrading ('.$error.')', OC_Log::FATAL);
return false; return false;
} }
@ -483,7 +484,7 @@ class OC_DB {
if (PEAR::isError($op)) { if (PEAR::isError($op)) {
$error = $op->getMessage(); $error = $op->getMessage();
$detail = $op->getDebugInfo(); $detail = $op->getDebugInfo();
OC_Log::write('core','Failed to update database structure ('.$error.', '.$detail.')',OC_Log::FATAL); OC_Log::write('core', 'Failed to update database structure ('.$error.', '.$detail.')', OC_Log::FATAL);
return false; return false;
} }
return true; return true;
@ -502,8 +503,8 @@ class OC_DB {
self::$MDB2->loadModule('Reverse'); self::$MDB2->loadModule('Reverse');
// Connect if this did not happen before // Connect if this did not happen before
if(!self::$schema){ if(!self::$schema) {
require_once('MDB2/Schema.php'); require_once 'MDB2/Schema.php';
self::$schema=MDB2_Schema::factory(self::$MDB2); self::$schema=MDB2_Schema::factory(self::$MDB2);
} }
@ -521,24 +522,24 @@ class OC_DB {
private static function processQuery( $query ){ private static function processQuery( $query ){
self::connect(); self::connect();
// We need Database type and table prefix // We need Database type and table prefix
if(is_null(self::$type)){ if(is_null(self::$type)) {
self::$type=OC_Config::getValue( "dbtype", "sqlite" ); self::$type=OC_Config::getValue( "dbtype", "sqlite" );
} }
$type = self::$type; $type = self::$type;
if(is_null(self::$prefix)){ if(is_null(self::$prefix)) {
self::$prefix=OC_Config::getValue( "dbtableprefix", "oc_" ); self::$prefix=OC_Config::getValue( "dbtableprefix", "oc_" );
} }
$prefix = self::$prefix; $prefix = self::$prefix;
// differences in escaping of table names ('`' for mysql) and getting the current timestamp // differences in escaping of table names ('`' for mysql) and getting the current timestamp
if( $type == 'sqlite' || $type == 'sqlite3' ){ if( $type == 'sqlite' || $type == 'sqlite3' ) {
$query = str_replace( '`', '"', $query ); $query = str_replace( '`', '"', $query );
$query = str_ireplace( 'NOW()', 'datetime(\'now\')', $query ); $query = str_ireplace( 'NOW()', 'datetime(\'now\')', $query );
$query = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $query ); $query = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $query );
}elseif( $type == 'pgsql' ){ }elseif( $type == 'pgsql' ) {
$query = str_replace( '`', '"', $query ); $query = str_replace( '`', '"', $query );
$query = str_ireplace( 'UNIX_TIMESTAMP()', 'cast(extract(epoch from current_timestamp) as integer)', $query ); $query = str_ireplace( 'UNIX_TIMESTAMP()', 'cast(extract(epoch from current_timestamp) as integer)', $query );
}elseif( $type == 'oci' ){ }elseif( $type == 'oci' ) {
$query = str_replace( '`', '"', $query ); $query = str_replace( '`', '"', $query );
$query = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $query ); $query = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $query );
} }
@ -600,7 +601,7 @@ class OC_DB {
foreach($apps as $app){ foreach($apps as $app){
$path = OC_App::getAppPath($app).'/appinfo/database.xml'; $path = OC_App::getAppPath($app).'/appinfo/database.xml';
if(file_exists($path)){ if(file_exists($path)) {
self::removeDBStructure( $path ); self::removeDBStructure( $path );
} }
} }
@ -608,8 +609,7 @@ class OC_DB {
// Create new tables // Create new tables
self::createDBFromStructure( $file ); self::createDBFromStructure( $file );
self::commit(); self::commit();
}
}
/** /**
* Start a transaction * Start a transaction
@ -626,9 +626,9 @@ class OC_DB {
/** /**
* Commit the database changes done during a transaction that is in progress * Commit the database changes done during a transaction that is in progress
*/ */
public static function commit(){ public static function commit() {
self::connect(); self::connect();
if(!self::$inTransaction){ if(!self::$inTransaction) {
return false; return false;
} }
self::$connection->commit(); self::$connection->commit();
@ -641,7 +641,7 @@ class OC_DB {
* @return bool * @return bool
*/ */
public static function isError($result){ public static function isError($result){
if(!$result){ if(!$result) {
return true; return true;
}elseif(self::$backend==self::BACKEND_MDB2 and PEAR::isError($result)){ }elseif(self::$backend==self::BACKEND_MDB2 and PEAR::isError($result)){
return true; return true;
@ -672,7 +672,7 @@ class PDOStatementWrapper{
}else{ }else{
$result=$this->statement->execute(); $result=$this->statement->execute();
} }
if($result){ if($result) {
return $this; return $this;
}else{ }else{
return false; return false;
@ -703,7 +703,7 @@ class PDOStatementWrapper{
* pass all other function directly to the PDOStatement * pass all other function directly to the PDOStatement
*/ */
public function __call($name,$arguments){ public function __call($name,$arguments){
return call_user_func_array(array($this->statement,$name),$arguments); return call_user_func_array(array($this->statement,$name), $arguments);
} }
/** /**