You are here

public function DatabaseSchema_sqlsrv::removeSQLComments in Drupal driver for SQL Server and SQL Azure 7.2

Same name and namespace in other branches
  1. 7.3 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::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

sqlsrv/schema.inc, line 346
Database schema code for Microsoft SQL Server database servers.

Class

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