Merge "Fix escaping for MSSQL LIKE queries."
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 25 Apr 2016 05:01:51 +0000 (05:01 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 25 Apr 2016 05:01:51 +0000 (05:01 +0000)
includes/db/DatabaseMssql.php

index ce34537..5c46c1a 100644 (file)
@@ -1131,6 +1131,35 @@ class DatabaseMssql extends Database {
                return strlen( $name ) && $name[0] == '[' && substr( $name, -1, 1 ) == ']';
        }
 
+       /**
+        * MS SQL supports more pattern operators than other databases (ex: [,],^)
+        *
+        * @param string $s
+        * @return string
+        */
+       protected function escapeLikeInternal( $s ) {
+               return addcslashes( $s, '\%_[]^' );
+       }
+
+       /**
+        * MS SQL requires specifying the escape character used in a LIKE query
+        * or using Square brackets to surround characters that are to be escaped
+        * http://msdn.microsoft.com/en-us/library/ms179859.aspx
+        * Here we take the Specify-Escape-Character approach since it's less
+        * invasive, renders a query that is closer to other DB's and better at
+        * handling square bracket escaping
+        *
+        * @return string Fully built LIKE statement
+        */
+       public function buildLike() {
+               $params = func_get_args();
+               if ( count( $params ) > 0 && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+
+               return parent::buildLike( $params ) . " ESCAPE '\' ";
+       }
+
        /**
         * @param string $db
         * @return bool