/**
  * Creates a new DatabaseException.
  * 
  * @param	string							$message		error message
  * @param	\wcf\system\database\Database				$db			affected db object
  * @param	\wcf\system\database\statement\PreparedStatement	$preparedStatement	affected prepared statement
  * @param	string							$sqlQuery		SQL query if prepare() failed
  */
 public function __construct($message, Database $db, PreparedStatement $preparedStatement = null, $sqlQuery = null)
 {
     $this->db = $db;
     $this->DBType = $db->getDBType();
     $this->preparedStatement = $preparedStatement;
     $this->sqlQuery = $sqlQuery;
     // prefer errors from prepared statement
     if ($this->preparedStatement !== null && $this->preparedStatement->getErrorNumber()) {
         $this->errorNumber = $this->preparedStatement->getErrorNumber();
         $this->errorDesc = $this->preparedStatement->getErrorDesc();
     } else {
         $this->errorNumber = $this->db->getErrorNumber();
         $this->errorDesc = $this->db->getErrorDesc();
     }
     parent::__construct($message, intval($this->errorNumber));
 }
 /**
  * Returns the current sql version of the database.
  * 
  * @return	string
  */
 public function getSQLVersion()
 {
     if ($this->sqlVersion === null) {
         try {
             $this->sqlVersion = $this->db->getVersion();
         } catch (DatabaseException $e) {
             $this->sqlVersion = 'unknown';
         }
     }
     return $this->sqlVersion;
 }
 /**
  * Gets the max value of a specific column.
  * 
  * @param	string		$tableName
  * @param	string		$columnName
  * @return	integer
  */
 protected function __getMaxID($tableName, $columnName)
 {
     $sql = "SELECT\tMAX(" . $columnName . ") AS maxID\n\t\t\tFROM\t" . $tableName;
     $statement = $this->database->prepareStatement($sql);
     $statement->execute();
     $row = $statement->fetchArray();
     if ($row !== false) {
         return $row['maxID'];
     }
     return 0;
 }
 /**
  * Executes a prepared statement.
  * 
  * @param	array		$parameters
  */
 public function execute(array $parameters = array())
 {
     $this->parameters = $parameters;
     $this->database->incrementQueryCount();
     try {
         if (WCF::benchmarkIsEnabled()) {
             Benchmark::getInstance()->start($this->query, Benchmark::TYPE_SQL_QUERY);
         }
         if (empty($parameters)) {
             $this->pdoStatement->execute();
         } else {
             $this->pdoStatement->execute($parameters);
         }
         if (WCF::benchmarkIsEnabled()) {
             Benchmark::getInstance()->stop();
         }
     } catch (\PDOException $e) {
         if (WCF::benchmarkIsEnabled()) {
             Benchmark::getInstance()->stop();
         }
         throw new DatabaseException('Could not execute prepared statement: ' . $e->getMessage(), $this->database, $this);
     }
 }
 /**
  * @see wcf\system\database\Database::escapeString()
  */
 public function escapeString($string)
 {
     $string = str_replace("", "\\x00", $string);
     // escape nul bytes
     return parent::escapeString($string);
 }
Example #6
0
 /**
  * Checks if in the chosen database are tables in conflict with the wcf tables
  * which will be created in the next step.
  * 
  * @param	\wcf\system\database\Database	$db
  * @param	integer				$dbNumber
  */
 protected function getConflictedTables($db, $dbNumber)
 {
     // get content of the sql structure file
     $sql = file_get_contents(TMP_DIR . 'setup/db/install.sql');
     // installation number value 'n' (WCF_N) must be reflected in the executed sql queries
     $sql = str_replace('wcf1_', 'wcf' . $dbNumber . '_', $sql);
     // get all tablenames which should be created
     preg_match_all("%CREATE\\s+TABLE\\s+(\\w+)%", $sql, $matches);
     // get all installed tables from chosen database
     $existingTables = $db->getEditor()->getTableNames();
     // check if existing tables are in conflict with wcf tables
     $conflictedTables = array();
     foreach ($existingTables as $existingTableName) {
         foreach ($matches[1] as $wcfTableName) {
             if ($existingTableName == $wcfTableName) {
                 $conflictedTables[] = $wcfTableName;
             }
         }
     }
     return $conflictedTables;
 }