You are here

public function Schema::removeSQLComments in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::removeSqlComments()

Remove comments from an SQL statement.

Parameters

mixed $sql: SQL statement to remove the comments from.

mixed $comments: Comments removed from the statement

Return value

string

See also

http://stackoverflow.com/questions/9690448/regular-expression-to-remove-...

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php, line 339
Definition of Drupal\Driver\Database\sqlsrv\Schema

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function removeSQLComments($sql, &$comments = NULL) {
  $sqlComments = '@(([\'"]).*?[^\\\\]\\2)|((?:\\#|--).*?$|/\\*(?:[^/*]|/(?!\\*)|\\*(?!/)|(?R))*\\*\\/)\\s*|(?<=;)\\s+@ms';

  /* Commented version
     $sqlComments = '@
     (([\'"]).*?[^\\\]\2) # $1 : Skip single & double quoted expressions
     |(                   # $3 : Match comments
     (?:\#|--).*?$    # - Single line comments
     |                # - Multi line (nested) comments
     /\*             #   . comment open marker
     (?: [^/*]    #   . non comment-marker characters
     |/(?!\*) #   . ! not a comment open
     |\*(?!/) #   . ! not a comment close
     |(?R)    #   . recursive case
     )*           #   . repeat eventually
     \*\/             #   . comment close marker
     )\s*                 # Trim after comments
     |(?<=;)\s+           # Trim after semi-colon
     @msx';
      */
  $uncommentedSQL = trim(preg_replace($sqlComments, '$1', $sql));
  if (is_array($comments)) {
    preg_match_all($sqlComments, $sql, $comments);
    $comments = array_filter($comments[3]);
  }
  return $uncommentedSQL;
}