Bugfixes and cleanup MS SQL Server installation

This commit is contained in:
Administrator 2013-02-10 13:07:59 +01:00
parent d577f790c8
commit 41ec976fd7
5 changed files with 148 additions and 87 deletions

View file

@ -27,7 +27,7 @@ $opts = array(
'hasMySQL' => $hasMySQL,
'hasPostgreSQL' => $hasPostgreSQL,
'hasOracle' => $hasOracle,
'hasMSSQLServer' => $hasMSSQL,
'hasMSSQL' => $hasMSSQL,
'directory' => $datadir,
'secureRNG' => OC_Util::secureRNG_available(),
'htaccessWorking' => OC_Util::ishtaccessworking(),

View file

@ -922,18 +922,20 @@ class PDOStatementWrapper{
public function execute($input=array()) {
$this->lastArguments = $input;
if (count($input) > 0) {
if (!isset($type)) {
$type = OC_Config::getValue( "dbtype", "sqlite" );
}
if ($type == 'mssql') {
$this->tryFixSubstringLastArgumentDataForMSSQL($input);
$input = $this->tryFixSubstringLastArgumentDataForMSSQL($input);
}
$result=$this->statement->execute($input);
} else {
$result=$this->statement->execute();
}
if ($result) {
return $this;
} else {
@ -941,7 +943,7 @@ class PDOStatementWrapper{
}
}
private function tryFixSubstringLastArgumentDataForMSSQL(&$input) {
private function tryFixSubstringLastArgumentDataForMSSQL($input) {
$query = $this->statement->queryString;
$pos = stripos ($query, 'SUBSTRING');
@ -1013,6 +1015,8 @@ class PDOStatementWrapper{
$this->statement = $PDO->prepare($newQuery);
$this->lastArguments = $input;
return $input;
} catch (PDOException $e){
$entry = 'PDO DB Error: "'.$e->getMessage().'"<br />';
$entry .= 'Offending command was: '.$this->statement->queryString .'<br />';

View file

@ -610,36 +610,37 @@ class OC_Setup {
self::mssql_createDatabase($dbname, $masterConnection);
self::mssql_createDBUser($dbuser, $dbpass, $masterConnection);
self::mssql_createDBUser($dbuser, $dbname, $masterConnection);
sqlsrv_close($masterConnection);
$connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass);
$connection = @sqlsrv_connect($dbhost, $connectionInfo);
//fill the database if needed
$query="SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}' AND TABLE_NAME = '{$dbtableprefix}users'";
$result = sqlsrv_query($connection, $query);
if($result) {
$row=sqlsrv_fetch_array($result);
}
if(!$result or $row[0] == 0) {
OC_DB::createDbFromStructure('db_structure.xml');
}
sqlsrv_close($connection);
self::mssql_createDatabaseStructure($dbname, $dbuser, $dbpass);
}
private static function mssql_createDBLogin($name, $password, $connection) {
$query = "SELECT * FROM master.sys.server_principals WHERE name = '".$name."';";
$result = sqlsrv_query($connection, $query);
if ($result) {
$row = sqlsrv_fetch_array($result);
if ($result === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
$entry = '';
}
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
} else {
$row = sqlsrv_fetch_array($result);
if (!$result or $row[0] == 0) {
if ($row === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
$entry = '';
}
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
} else {
if ($row == null) {
$query = "CREATE LOGIN [".$name."] WITH PASSWORD = '".$password."';";
$result = sqlsrv_query($connection, $query);
if (!$result or $result === false) {
@ -653,18 +654,36 @@ class OC_Setup {
}
}
}
}
}
private static function mssql_createDBUser($name, $dbname, $connection) {
$query = "SELECT * FROM [".$dbname."].sys.database_principals WHERE name = '".$name."';";
$result = sqlsrv_query($connection, $query);
if($result) {
$row=sqlsrv_fetch_array($result);
if ($result === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
$entry = '';
}
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
} else {
$row = sqlsrv_fetch_array($result);
if (!$result or $row[0] == 0) {
if ($row === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
$entry = '';
}
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
} else {
if ($row == null) {
$query = "USE [".$dbname."]; CREATE USER [".$name."] FOR LOGIN [".$name."];";
$result = sqlsrv_query($connection, $query);
if (!$result or $result === false) {
if (!$result || $result === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry = 'DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
@ -677,7 +696,7 @@ class OC_Setup {
$query = "USE [".$dbname."]; EXEC sp_addrolemember 'db_owner', '".$name."';";
$result = sqlsrv_query($connection, $query);
if (!$result or $result === false) {
if (!$result || $result === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
@ -687,11 +706,13 @@ class OC_Setup {
echo($entry);
}
}
}
}
private static function mssql_createDatabase($dbname, $connection) {
$query = "CREATE DATABASE [".$dbname."];";
$result = sqlsrv_query($connection, $query);
if (!$result or $result === false) {
if (!$result || $result === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
@ -702,6 +723,42 @@ class OC_Setup {
}
}
private static function mssql_createDatabaseStructure($dbname, $dbuser, $dbpass) {
$connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass);
$connection = @sqlsrv_connect($dbhost, $connectionInfo);
//fill the database if needed
$query = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}' AND TABLE_NAME = '{$dbtableprefix}users'";
$result = sqlsrv_query($connection, $query);
if ($result === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
$entry = '';
}
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
} else {
$row = sqlsrv_fetch_array($result);
if ($row === false) {
if ( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
$entry = '';
}
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
} else {
if ($row == null) {
OC_DB::createDbFromStructure('db_structure.xml');
}
}
}
sqlsrv_close($connection);
}
/**
* create .htaccess files for apache hosts