@@ -736,6 +736,110 @@ public function testRegexpSubstrNumericSubject() {
736736 $this->assertSame( '2', $this->last_result[0]->r );
737737 }
738738
739+ /**
740+ * @dataProvider regexpInstrCases
741+ */
742+ public function testRegexpInstr( $sql, $expected ) {
743+ $this->assertQuery( "SELECT $sql AS r" );
744+ $this->assertSame( $expected, $this->last_result[0]->r );
745+ }
746+
747+ public static function regexpInstrCases() {
748+ return array(
749+ 'basic' => array( "REGEXP_INSTR('dog cat dog', 'dog')", '1' ),
750+ 'no match' => array( "REGEXP_INSTR('abc', 'xyz')", '0' ),
751+ 'second match' => array( "REGEXP_INSTR('dog cat dog', 'dog', 1, 2)", '9' ),
752+ 'pos skips first match' => array( "REGEXP_INSTR('dog cat dog', 'dog', 5)", '9' ),
753+ 'return_option=1 (end)' => array( "REGEXP_INSTR('dog cat dog', 'dog', 1, 1, 1)", '4' ),
754+ 'match_type c miss' => array( "REGEXP_INSTR('DOG', 'dog', 1, 1, 0, 'c')", '0' ),
755+ 'multibyte position' => array( "REGEXP_INSTR('café123', '[0-9]+')", '5' ),
756+ 'null expr' => array( "REGEXP_INSTR(NULL, 'abc')", null ),
757+ 'null pattern' => array( "REGEXP_INSTR('abc', NULL)", null ),
758+ // A NULL match_type returns NULL before the pattern and pos are checked.
759+ 'null match_type first' => array( "REGEXP_INSTR('abc', '(abc', 0, 1, 0, NULL)", null ),
760+ // NULL arguments return NULL before pos is checked.
761+ 'null expr, pos 0' => array( "REGEXP_INSTR(NULL, 'a', 0)", null ),
762+ 'null expr, pos -1' => array( "REGEXP_INSTR(NULL, 'a', -1)", null ),
763+ 'null expr, pos 5' => array( "REGEXP_INSTR(NULL, 'a', 5)", null ),
764+ 'null pattern, pos 0' => array( "REGEXP_INSTR('abc', NULL, 0)", null ),
765+ 'null occurrence, pos 0' => array( "REGEXP_INSTR('abc', 'a', 0, NULL)", null ),
766+ );
767+ }
768+
769+ /**
770+ * @dataProvider regexpInstrErrors
771+ */
772+ public function testRegexpInstrErrors( $sql, $message ) {
773+ $this->assertQueryError( "SELECT $sql", $message );
774+ }
775+
776+ public static function regexpInstrErrors() {
777+ $return_option = 'Incorrect arguments to regexp_instr: return_option must be 1 or 0.';
778+ return array(
779+ 'invalid pattern' => array( "REGEXP_INSTR('abc', '(abc')", 'Invalid regular expression: (abc.' ),
780+ 'invalid flag' => array( "REGEXP_INSTR('abc', 'a', 1, 1, 0, 'x')", 'Invalid match_type flag: x.' ),
781+ 'pos zero' => array( "REGEXP_INSTR('abc', 'a', 0)", 'Index out of bounds in regular expression search.' ),
782+ 'pos far past end' => array( "REGEXP_INSTR('abc', 'a', 10)", 'Index out of bounds in regular expression search.' ),
783+ 'invalid return_option' => array( "REGEXP_INSTR('abc', 'a', 1, 1, 2)", $return_option ),
784+ 'extra argument' => array(
785+ "REGEXP_INSTR('abc', 'a', 1, 1, 0, 'c', 'extra')",
786+ 'SQLSTATE[HY000]: General error: 1 wrong number of arguments to function REGEXP_INSTR()',
787+ ),
788+ );
789+ }
790+
791+ public function testRegexpInstrEmptySubjectSkipsPosCheck() {
792+ // MySQL does not check pos for an empty subject and reports a match at pos.
793+ $cases = array(
794+ "'a', 2" => '0',
795+ "'^', 5" => '5',
796+ "'^', -3" => '-3',
797+ );
798+ foreach ( $cases as $args => $expected ) {
799+ $this->assertQuery( "SELECT REGEXP_INSTR('', $args) AS r" );
800+ $this->assertSame( $expected, $this->last_result[0]->r, $args );
801+ }
802+
803+ $this->assertQueryError(
804+ "SELECT REGEXP_INSTR('', '(', 2)",
805+ 'Invalid regular expression: (.'
806+ );
807+ }
808+
809+ public function testRegexpInstrOccurrenceClampedToOne() {
810+ // MySQL clamps occurrence <= 0 to 1.
811+ $this->assertQuery( "SELECT REGEXP_INSTR('abcabc', 'b', 1, 0) AS r" );
812+ $this->assertSame( '2', $this->last_result[0]->r );
813+ $this->assertQuery( "SELECT REGEXP_INSTR('abcabc', 'b', 1, -5) AS r" );
814+ $this->assertSame( '2', $this->last_result[0]->r );
815+ }
816+
817+ public function testRegexpInstrStraddlingMatch() {
818+ // A match that starts before pos is not returned; the next match at or
819+ // after pos is returned instead.
820+ $this->assertQuery( "SELECT REGEXP_INSTR('abc123def', '[0-9]+', 5) AS r" );
821+ $this->assertSame( '5', $this->last_result[0]->r );
822+ }
823+
824+ public function testRegexpInstrMultibyteReturnOptionEnd() {
825+ // Multibyte match ('é' is 2 bytes) with return_option=1 (one past end).
826+ // 'aéb' char positions: a=1, é=2, b=3. 'é' matches at char 2, end char position = 3.
827+ $this->assertQuery( "SELECT REGEXP_INSTR('aéb', 'é', 1, 1, 1) AS r" );
828+ $this->assertSame( '3', $this->last_result[0]->r );
829+ }
830+
831+ public function testRegexpInstrPatternIsRelativeToPos() {
832+ $this->assertQuery( "SELECT REGEXP_INSTR('abc', '^b', 2) AS r" );
833+ $this->assertSame( '2', $this->last_result[0]->r );
834+ $this->assertQuery( "SELECT REGEXP_INSTR('ab', '(?<=a)b', 2) AS r" );
835+ $this->assertSame( '0', $this->last_result[0]->r );
836+ }
837+
838+ public function testRegexpInstrNumericSubject() {
839+ $this->assertQuery( "SELECT REGEXP_INSTR(123, '2') AS r" );
840+ $this->assertSame( '2', $this->last_result[0]->r );
841+ }
842+
739843 public function testInsertDateNow() {
740844 $this->assertQuery(
741845 "INSERT INTO _dates (option_name, option_value) VALUES ('first', now());"
0 commit comments