Use db setup class for option validation

This commit is contained in:
Bart Visscher 2013-04-03 17:52:18 +02:00
parent 2faccaee0d
commit 6831b78597
7 changed files with 43 additions and 35 deletions

View File

@ -19,6 +19,14 @@ class DatabaseSetupException extends Exception
} }
class OC_Setup { class OC_Setup {
static $db_setup_classes = array(
'mysql' => '\OC\Setup\MySQL',
'pgsql' => '\OC\Setup\PostgreSQL',
'oci' => '\OC\Setup\OCI',
'mssql' => '\OC\Setup\MSSQL',
'sqlite' => '\OC\Setup\Sqlite',
'sqlite3' => '\OC\Setup\Sqlite',
);
public static function getTrans(){ public static function getTrans(){
return OC_L10N::get('lib'); return OC_L10N::get('lib');
@ -40,31 +48,14 @@ class OC_Setup {
$options['directory'] = OC::$SERVERROOT."/data"; $options['directory'] = OC::$SERVERROOT."/data";
} }
if($dbtype == 'mysql' or $dbtype == 'pgsql' or $dbtype == 'oci' or $dbtype == 'mssql') { // these needs more config options if (!isset(self::$db_setup_classes[$dbtype])) {
if($dbtype == 'mysql') $dbtype = 'sqlite';
$dbprettyname = 'MySQL';
else if($dbtype == 'pgsql')
$dbprettyname = 'PostgreSQL';
else if ($dbtype == 'mssql')
$dbprettyname = 'MS SQL Server';
else
$dbprettyname = 'Oracle';
if(empty($options['dbuser'])) {
$error[] = $l->t("%s enter the database username.", array($dbprettyname));
}
if(empty($options['dbname'])) {
$error[] = $l->t("%s enter the database name.", array($dbprettyname));
}
if(substr_count($options['dbname'], '.') >= 1) {
$error[] = $l->t("%s you may not use dots in the database name", array($dbprettyname));
}
if($dbtype != 'oci' && empty($options['dbhost'])) {
$options['dbhost'] = 'localhost';
}
} }
$class = self::$db_setup_classes[$dbtype];
$db_setup = new $class(self::getTrans());
$error = array_merge($error, $db_setup->validate($options));
if(count($error) != 0) { if(count($error) != 0) {
return $error; return $error;
} }
@ -91,17 +82,8 @@ class OC_Setup {
OC_Config::setValue('datadirectory', $datadir); OC_Config::setValue('datadirectory', $datadir);
OC_Config::setValue('dbtype', $dbtype); OC_Config::setValue('dbtype', $dbtype);
OC_Config::setValue('version', implode('.', OC_Util::getVersion())); OC_Config::setValue('version', implode('.', OC_Util::getVersion()));
$db_setup_classes = array(
'mysql' => '\OC\Setup\MySQL',
'pgsql' => '\OC\Setup\PostgreSQL',
'oci' => '\OC\Setup\OCI',
'mssql' => '\OC\Setup\MSSQL',
'sqlite' => '\OC\Setup\Sqlite',
'sqlite3' => '\OC\Setup\Sqlite',
);
try { try {
$class = $db_setup_classes[$dbtype]; $db_setup->initialize($options);
$db_setup = new $class(self::getTrans(), $options);
$db_setup->setupDatabase($username); $db_setup->setupDatabase($username);
} catch (DatabaseSetupException $e) { } catch (DatabaseSetupException $e) {
$error[] = array( $error[] = array(

View File

@ -12,14 +12,27 @@ abstract class AbstractDatabase {
public function __construct($trans, $config) { public function __construct($trans, $config) {
$this->trans = $trans; $this->trans = $trans;
$this->initialize($config); }
public function validate($config) {
$errors = array();
if(empty($config['dbuser'])) {
$errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname));
}
if(empty($config['dbname'])) {
$errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname));
}
if(substr_count($config['dbname'], '.') >= 1) {
$errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname));
}
return $errors;
} }
public function initialize($config) { public function initialize($config) {
$dbuser = $config['dbuser']; $dbuser = $config['dbuser'];
$dbpass = $config['dbpass']; $dbpass = $config['dbpass'];
$dbname = $config['dbname']; $dbname = $config['dbname'];
$dbhost = isset($config['dbhost']) ? $config['dbhost'] : ''; // dbhost contents is checked earlier $dbhost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
$dbtableprefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_'; $dbtableprefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';
\OC_Config::setValue('dbname', $dbname); \OC_Config::setValue('dbname', $dbname);

View File

@ -3,6 +3,8 @@
namespace OC\Setup; namespace OC\Setup;
class MSSQL extends AbstractDatabase { class MSSQL extends AbstractDatabase {
public $dbprettyname = 'MS SQL Server';
public function setupDatabase() { public function setupDatabase() {
//check if the database user has admin right //check if the database user has admin right
$masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword); $masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword);

View File

@ -3,6 +3,8 @@
namespace OC\Setup; namespace OC\Setup;
class MySQL extends AbstractDatabase { class MySQL extends AbstractDatabase {
public $dbprettyname = 'MySQL';
public function setupDatabase($username) { public function setupDatabase($username) {
//check if the database user has admin right //check if the database user has admin right
$connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword); $connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword);

View File

@ -3,6 +3,8 @@
namespace OC\Setup; namespace OC\Setup;
class OCI extends AbstractDatabase { class OCI extends AbstractDatabase {
public $dbprettyname = 'Oracle';
protected $dbtablespace; protected $dbtablespace;
public function initialize($config) { public function initialize($config) {

View File

@ -3,6 +3,8 @@
namespace OC\Setup; namespace OC\Setup;
class PostgreSQL extends AbstractDatabase { class PostgreSQL extends AbstractDatabase {
public $dbprettyname = 'PostgreSQL';
public function setupDatabase($username) { public function setupDatabase($username) {
$e_host = addslashes($this->dbhost); $e_host = addslashes($this->dbhost);
$e_user = addslashes($this->dbuser); $e_user = addslashes($this->dbuser);

View File

@ -3,6 +3,11 @@
namespace OC\Setup; namespace OC\Setup;
class Sqlite extends AbstractDatabase { class Sqlite extends AbstractDatabase {
public $dbprettyname = 'Sqlite';
public function validate($config) {
}
public function initialize($config) { public function initialize($config) {
} }