public function testNewlineHanding()
    {
        $formatter = new SQLFormatter();
        $sqlBefore = <<<SQL
SELECT Test.Foo, Test.Bar FROM Test WHERE 'From' = "Where"
SQL;
        $sqlAfter = <<<SQL
SELECT Test.Foo, Test.Bar
FROM Test
WHERE 'From' = "Where"
SQL;
        $this->assertEquals($formatter->formatPlain($sqlBefore), $sqlAfter, 'correct replacement of newlines and don\'t replace non-uppercase tokens');
        $sqlBefore = <<<SQL
SELECT Test.Foo, Test.Bar
FROM Test
WHERE
  'From' = "Where"
SQL;
        $sqlAfter = <<<SQL
SELECT Test.Foo, Test.Bar
FROM Test
WHERE
  'From' = "Where"
SQL;
        $this->assertEquals($formatter->formatPlain($sqlBefore), $sqlAfter, 'Leave existing newlines and indentation in place');
    }
 /**
  * Error handler for database errors.
  * All database errors will call this function to report the error.  It isn't a static function;
  * it will be called on the object itself and as such can be overridden in a subclass.
  * Subclasses should run all errors through this function.
  *
  * @todo hook this into a more well-structured error handling system.
  * @param string $msg The error message.
  * @param integer $errorLevel The level of the error to throw.
  * @param string $sql The SQL related to this query
  * @param array $parameters Parameters passed to the query
  */
 protected function databaseError($msg, $errorLevel = E_USER_ERROR, $sql = null, $parameters = array())
 {
     // Prevent errors when error checking is set at zero level
     if (empty($errorLevel)) {
         return;
     }
     // Format query if given
     if (!empty($sql)) {
         $formatter = new SQLFormatter();
         $formattedSQL = $formatter->formatPlain($sql);
         $msg = "Couldn't run query:\n\n{$formattedSQL}\n\n{$msg}";
     }
     if ($errorLevel === E_USER_ERROR) {
         // Treating errors as exceptions better allows for responding to errors
         // in code, such as credential checking during installation
         throw new SS_DatabaseException($msg, 0, null, $sql, $parameters);
     } else {
         user_error($msg, $errorLevel);
     }
 }
 public function databaseError($msg, $errorLevel = E_USER_ERROR)
 {
     // try to extract and format query
     if (preg_match('/Couldn\'t run query: ([^\\|]*)\\|\\s*(.*)/', $msg, $matches)) {
         $formatter = new SQLFormatter();
         $msg = "Couldn't run query: \n" . $formatter->formatPlain($matches[1]) . "\n\n" . $matches[2];
     }
     $connect_errno = $this->dbConn->connect_errno;
     $errno = $this->dbConn->errno;
     $sqlstate = $this->dbConn->sqlstate;
     $error = $this->dbConn->error;
     $connect_error = $msg;
     $msg = sprintf('connect_errno : %s - errno : %s - sqlstate : %s - error : %s - connect_error : %s', $connect_errno, $errno, $sqlstate, $error, $connect_error);
     SS_Log::log($msg, SS_Log::ERR);
     if (Director::get_environment_type() === "live") {
         ob_clean();
         $maintenance_page = file_get_contents(Director::baseFolder() . '/maintenance/index.html');
         echo $maintenance_page;
         header("HTTP/1.0 502 Bad Gateway");
         exit;
     } else {
         user_error($msg);
     }
 }
Beispiel #4
0
 function databaseError($msg, $errorLevel = E_USER_ERROR)
 {
     // try to extract and format query
     if (preg_match('/Couldn\'t run query: ([^\\|]*)\\|\\s*(.*)/', $msg, $matches)) {
         $formatter = new SQLFormatter();
         $msg = "Couldn't run query: \n" . $formatter->formatPlain($matches[1]) . "\n\n" . $matches[2];
     }
     user_error($msg, $errorLevel);
 }
 private function restoreStrings(&$matches)
 {
     $string = $this->strings[$matches[1]];
     if ($this->optimize === true && $string[0] == '"') {
         //if a double quoted string does not contain variables or special characters, transform it to a single quoted string to save parsing time
         $match = preg_match('/(\\$[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*|\\\\[nrtvf$]|\\\\x[0-9A-Fa-f]{1,2}|\\[0-7]{1,3})/', $string);
         if ($match === 0) {
             //unescape double quotes
             $string = str_replace('\\"', '"', $string);
             //escape single quotes
             $string = addcslashes($string, '\'');
             //change quotes
             $string[0] = '\'';
             $string[strlen($string) - 1] = '\'';
         }
     }
     if ($this->trimStrings === true) {
         $string = preg_replace('/(?<=^["\'])\\s*/', '', $string);
         //remove space from the begining of the string
         $string = preg_replace('/\\s*(?=["\']$)/', '', $string);
         //remove space from the end of the string
     }
     //if sql optimization is selected and the string is a SQL query then beautify query
     if ($this->formatSQL && preg_match('/\\s*[\'"]\\s*(SELECT|UPDATE|INSERT)\\s/i', $string) > 0) {
         include_once 'SQLFormatter.inc.php';
         $sqlFormatter = new SQLFormatter();
         $string = $sqlFormatter->beautify($string, $this->indent);
     }
     return $string;
 }