You are here

public function Scheme::removeSQLComments in Drupal driver for SQL Server and SQL Azure 8.2

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/Scheme.php, line 329

Class

Scheme

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;
}