Skip to content

Commit 294a3dd

Browse files
Jan Haitjemastevelacey
authored andcommitted
Mysql Instr support
1 parent f592c49 commit 294a3dd

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ functions available in MySQL, Oracle, Sqlite and PostgreSQL.
1919

2020
| DB | Functions |
2121
|:--:|:---------:|
22-
| MySQL | `ACOS, AES_DECRYPT, AES_ENCRYPT, ANY_VALUE, ASCII, ASIN, ATAN, ATAN2, BINARY, BIT_COUNT, BIT_XOR, CEIL, CHAR_LENGTH, COLLATE, CONCAT_WS, CONVERT_TZ, COS, COT, COUNTIF, CRC32, DATE, DATE_FORMAT, DATEADD, DATEDIFF, DATESUB, DAY, DAYNAME, DAYOFWEEK, DAYOFYEAR, DEGREES, DIV, EXP, EXTRACT, FIELD, FIND_IN_SET, FLOOR, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, LAST_DAY, LEAST, LOG, LOG10, LOG2, LPAD, MATCH, MD5, MINUTE, MONTH, MONTHNAME, NOW, NULLIF, PI, POWER, QUARTER, RADIANS, RAND, REGEXP, REPLACE, ROUND, RPAD, SECOND, SECTOTIME, SHA1, SHA2, SIN, SOUNDEX, STD, STDDEV, STRTODATE, STR_TO_DATE, SUBSTRING_INDEX, TAN, TIME, TIMEDIFF, TIMESTAMPADD, TIMESTAMPDIFF, TIMETOSEC, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, YEAR, YEARMONTH, YEARWEEK` |
22+
| MySQL | `ACOS, AES_DECRYPT, AES_ENCRYPT, ANY_VALUE, ASCII, ASIN, ATAN, ATAN2, BINARY, BIT_COUNT, BIT_XOR, CEIL, CHAR_LENGTH, COLLATE, CONCAT_WS, CONVERT_TZ, COS, COT, COUNTIF, CRC32, DATE, DATE_FORMAT, DATEADD, DATEDIFF, DATESUB, DAY, DAYNAME, DAYOFWEEK, DAYOFYEAR, DEGREES, DIV, EXP, EXTRACT, FIELD, FIND_IN_SET, FLOOR, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, INSTR, LAST_DAY, LEAST, LOG, LOG10, LOG2, LPAD, MATCH, MD5, MINUTE, MONTH, MONTHNAME, NOW, NULLIF, PI, POWER, QUARTER, RADIANS, RAND, REGEXP, REPLACE, ROUND, RPAD, SECOND, SECTOTIME, SHA1, SHA2, SIN, SOUNDEX, STD, STDDEV, STRTODATE, STR_TO_DATE, SUBSTRING_INDEX, TAN, TIME, TIMEDIFF, TIMESTAMPADD, TIMESTAMPDIFF, TIMETOSEC, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, YEAR, YEARMONTH, YEARWEEK` |
2323
| Oracle | `DAY, LISTAGG, MONTH, NVL, TO_CHAR, TO_DATE, TRUNC, YEAR` |
2424
| Sqlite | `DATE, MINUTE, HOUR, DAY, WEEK, WEEKDAY, MONTH, YEAR, STRFTIME, DATE_FORMAT*, CASE WHEN THEN ELSE END, IFNULL, REPLACE, ROUND` |
2525
| PostgreSQL | `TO_DATE, TO_CHAR, AT_TIME_ZONE, COUNT_FILTER, STRING_AGG` |

config/mysql.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ doctrine:
8383
group_concat: DoctrineExtensions\Query\Mysql\GroupConcat
8484
ifelse: DoctrineExtensions\Query\Mysql\IfElse
8585
ifnull: DoctrineExtensions\Query\Mysql\IfNull
86+
instr: DoctrineExtensions\Query\Mysql\Instr
8687
least: DoctrineExtensions\Query\Mysql\Least
8788
lpad: DoctrineExtensions\Query\Mysql\Lpad
8889
match: DoctrineExtensions\Query\Mysql\MatchAgainst

src/Query/Mysql/Instr.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Query\Mysql;
4+
5+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
6+
use Doctrine\ORM\Query\Lexer;
7+
use Doctrine\ORM\Query\Parser;
8+
use Doctrine\ORM\Query\SqlWalker;
9+
10+
/**
11+
* Class Instr
12+
* @package DoctrineExtensions\Query\Mysql
13+
* @author Jan H <[email protected]>
14+
*/
15+
class Instr extends FunctionNode
16+
{
17+
public $originalString = null;
18+
public $subString = null;
19+
20+
/**
21+
* @param \Doctrine\ORM\Query\Parser $parser
22+
*/
23+
public function parse(Parser $parser)
24+
{
25+
$parser->match(Lexer::T_IDENTIFIER);
26+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
27+
$this->originalString = $parser->ArithmeticPrimary();
28+
$parser->match(Lexer::T_COMMA);
29+
$this->subString = $parser->ArithmeticPrimary();
30+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
31+
}
32+
33+
/**
34+
* @param \Doctrine\ORM\Query\SqlWalker $sqlWalker
35+
* @return string
36+
*/
37+
public function getSql(SqlWalker $sqlWalker)
38+
{
39+
return sprintf('INSTR(%s, %s)',
40+
$this->originalString->dispatch($sqlWalker),
41+
$this->subString->dispatch($sqlWalker)
42+
);
43+
}
44+
}

tests/Query/Mysql/StringTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,14 @@ public function testRpad()
179179
$q->getSql()
180180
);
181181
}
182+
183+
/**
184+
* Test cases for MYSQL INSTR function.
185+
*/
186+
public function testInstr()
187+
{
188+
$q = $this->entityManager->createQuery("SELECT INSTR('www.mysql.com', 'mysql') from DoctrineExtensions\Tests\Entities\Blank");
189+
$this->assertEquals("SELECT INSTR('www.mysql.com', 'mysql') AS sclr_0 FROM Blank b0_", $q->getSql());
190+
}
191+
182192
}

0 commit comments

Comments
 (0)