Introduce IDatabase::buildIntegerCast
authoraddshore <addshorewiki@gmail.com>
Sun, 4 Mar 2018 13:50:28 +0000 (13:50 +0000)
committeraddshore <addshorewiki@gmail.com>
Wed, 7 Mar 2018 13:00:18 +0000 (13:00 +0000)
Change-Id: Ib24856d1ebe017ff07ae497972c764b4a3f3c7df

includes/libs/rdbms/database/DBConnRef.php
includes/libs/rdbms/database/Database.php
includes/libs/rdbms/database/DatabaseMysqlBase.php
includes/libs/rdbms/database/IDatabase.php
tests/phpunit/includes/libs/rdbms/database/DatabaseMysqlBaseTest.php
tests/phpunit/includes/libs/rdbms/database/DatabaseSQLTest.php

index 910d42f..f26b985 100644 (file)
@@ -358,6 +358,10 @@ class DBConnRef implements IDatabase {
                return $this->__call( __FUNCTION__, func_get_args() );
        }
 
+       public function buildIntegerCast( $field ) {
+               return $this->__call( __FUNCTION__, func_get_args() );
+       }
+
        public function databasesAreIndependent() {
                return $this->__call( __FUNCTION__, func_get_args() );
        }
index fc0db57..2c59963 100644 (file)
@@ -1892,6 +1892,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return $field;
        }
 
+       public function buildIntegerCast( $field ) {
+               return 'CAST( ' . $field . ' AS INTEGER )';
+       }
+
        public function databasesAreIndependent() {
                return false;
        }
index a5220b9..8fb8db5 100644 (file)
@@ -1456,6 +1456,15 @@ abstract class DatabaseMysqlBase extends Database {
                return parent::isTransactableQuery( $sql ) &&
                        !preg_match( '/^SELECT\s+(GET|RELEASE|IS_FREE)_LOCK\(/', $sql );
        }
+
+       /**
+        * @param string $field Field or column to cast
+        * @return string
+        */
+       public function buildIntegerCast( $field ) {
+               return 'CAST( ' . $field . ' AS SIGNED )';
+       }
+
 }
 
 class_alias( DatabaseMysqlBase::class, 'DatabaseMysqlBase' );
index 9311c07..28a8125 100644 (file)
@@ -1072,6 +1072,13 @@ interface IDatabase {
         */
        public function buildStringCast( $field );
 
+       /**
+        * @param string $field Field or column to cast
+        * @return string
+        * @since 1.31
+        */
+       public function buildIntegerCast( $field );
+
        /**
         * Returns true if DBs are assumed to be on potentially different servers
         *
index 14c7057..1d948d7 100644 (file)
@@ -604,4 +604,14 @@ class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
 
                ];
        }
+
+       /**
+        * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
+        */
+       public function testBuildIntegerCast() {
+               $db = new FakeDatabaseMysqlBase();
+               $output = $db->buildIntegerCast( 'fieldName' );
+               $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
+       }
+
 }
index b3c8cce..0f47595 100644 (file)
@@ -1249,4 +1249,12 @@ class DatabaseSQLTest extends PHPUnit\Framework\TestCase {
                $this->database->buildSubstring( 'foo', $start, $length );
        }
 
+       /**
+        * @covers \Wikimedia\Rdbms\Database::buildIntegerCast
+        */
+       public function testBuildIntegerCast() {
+               $output = $this->database->buildIntegerCast( 'fieldName' );
+               $this->assertSame( 'CAST( fieldName AS INTEGER )', $output );
+       }
+
 }