Skip to content

Commit a6e9179

Browse files
authored
#104 Add support for MySQL LEFT(), RIGHT() (#105)
#104 Add support for MySQL LEFT(), RIGHT()
1 parent d218527 commit a6e9179

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ In order to reduce clutter it is preferable to create a separate Service Provide
8585
- [format(number, decimals, locale = 'en_US')](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_format)
8686
- [lpad(string, length, pad)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lpad)
8787
- [rpad(string, length, pad)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_rpad)
88+
- [left(string, length)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_left)
89+
- [right(string, length)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_right)
8890
### Miscellaneous
8991
- [inet_ntoa(ipAddress)](https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_inet-ntoa)
9092
### Vectorface-Specific

src/Mhorninger/MySQLite/MethodRewriteConstants.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ class MethodRewriteConstants
1515
'/WEEK(?=\\))/' => 'weeks\'',
1616
'/MONTH(?=\\))/' => 'months\'',
1717
'/YEAR(?=\\))/' => 'years\'',
18+
'/LEFT(?=.*?, .*?\\))/' => '`LEFT`',
19+
'/RIGHT(?=.*?, .*?\\))/' => '`RIGHT`',
1820
];
1921
}

src/Mhorninger/MySQLite/MySQL/StringExtended.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ trait StringExtended
88
{
99
/**
1010
* Format a number according to the nubmer of decimals provided and culture.
11+
*
1112
* @param mixed... number, decimals, culture.
1213
*/
1314
// phpcs:disable
@@ -57,4 +58,14 @@ public static function mysql_rpad($string, $length, $pad)
5758
return substr($string, 0, $length);
5859
}
5960
}
61+
62+
public static function mysql_left($string, $length)
63+
{
64+
return substr($string, 0, $length);
65+
}
66+
67+
public static function mysql_right($string, $length)
68+
{
69+
return substr($string, -$length);
70+
}
6071
}

test/Mhorninger/MySQLite/StringMethodTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,21 @@ public function testRpadNull()
125125
$this->assertNull($result->value);
126126
}
127127

128+
public function testLeft()
129+
{
130+
$query = "SELECT LEFT('TESTING', 4) as value;";
131+
$result = $this->conn->selectOne($query);
132+
$expected = 'TEST';
133+
$this->assertEquals($expected, $result->value);
134+
}
135+
136+
public function testRight()
137+
{
138+
$query = "SELECT RIGHT('TESTING', 3) as value;";
139+
$result = $this->conn->selectOne($query);
140+
$expected = 'ING';
141+
$this->assertEquals($expected, $result->value);
142+
}
143+
128144
//endregion
129145
}

0 commit comments

Comments
 (0)