rdbms: make selectRowCount() use $var argument to exclude NULLs
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseMssql.php
index b1c8909..1f6132b 100644 (file)
@@ -505,19 +505,26 @@ class DatabaseMssql extends Database {
         * Returns -1 if count cannot be found
         * Takes same arguments as Database::select()
         * @param string $table
-        * @param string $vars
+        * @param string $var
         * @param string $conds
         * @param string $fname
         * @param array $options
+        * @param array $join_conds
         * @return int
         */
-       public function estimateRowCount( $table, $vars = '*', $conds = '',
-               $fname = __METHOD__, $options = []
+       public function estimateRowCount( $table, $var = '*', $conds = '',
+               $fname = __METHOD__, $options = [], $join_conds = []
        ) {
+               $conds = $this->normalizeConditions( $conds, $fname );
+               $column = $this->extractSingleFieldFromList( $var );
+               if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
+                       $conds[] = "$column IS NOT NULL";
+               }
+
                // http://msdn2.microsoft.com/en-us/library/aa259203.aspx
                $options['EXPLAIN'] = true;
                $options['FOR COUNT'] = true;
-               $res = $this->select( $table, $vars, $conds, $fname, $options );
+               $res = $this->select( $table, $var, $conds, $fname, $options, $join_conds );
 
                $rows = -1;
                if ( $res ) {
@@ -569,7 +576,7 @@ class DatabaseMssql extends Database {
                        }
                }
 
-               return empty( $result ) ? false : $result;
+               return $result ?: false;
        }
 
        /**
@@ -1225,6 +1232,19 @@ class DatabaseMssql extends Database {
                return $sql;
        }
 
+       public function buildSubstring( $input, $startPosition, $length = null ) {
+               $this->assertBuildSubstringParams( $startPosition, $length );
+               if ( $length === null ) {
+                       /**
+                        * MSSQL doesn't allow an empty length parameter, so when we don't want to limit the
+                        * length returned use the default maximum size of text.
+                        * @see https://docs.microsoft.com/en-us/sql/t-sql/statements/set-textsize-transact-sql
+                        */
+                       $length = 2147483647;
+               }
+               return 'SUBSTRING(' . implode( ',', [ $input, $startPosition, $length ] ) . ')';
+       }
+
        /**
         * Returns an associative array for fields that are of type varbinary, binary, or image
         * $table can be either a raw table name or passed through tableName() first