Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 4988285

Browse files
feat: implement sounds like function for mysql
1 parent 15a33a3 commit 4988285

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

core/jvm/src/main/scala/zio/sql/expr/Expr.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ sealed trait Expr[-F, -A, +B] { self =>
3737
)(implicit ev: B <:< Boolean): Expr[F with F2, A1, Boolean] =
3838
Expr.Binary(self.widen[Boolean], that, BinaryOp.OrBool)
3939

40+
def soundsLike[F2, A1 <: A, B1 >: B](that: Expr[F2, A1, String])(implicit
41+
ev: B <:< String
42+
): Expr[F with F2, A1, String] =
43+
Expr.Binary(self.widen[String], that, BinaryOp.MySqlExtensions.SoundsLike)
44+
4045
def ===[F2, A1 <: A, B1 >: B, B2](that: Expr[F2, A1, B2])(implicit
4146
ct: ComparableTypes[B1, B2]
4247
): Expr[F with F2, A1, Boolean] =

core/jvm/src/main/scala/zio/sql/ops/Operator.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ object Operator {
5252
final case class OrBit[A: IsIntegral]() extends BinaryOp[A] {
5353
def isIntegral: IsIntegral[A] = implicitly[IsIntegral[A]]
5454
override val symbol: String = "|"
55+
}
5556

57+
object MySqlExtensions {
58+
case object SoundsLike extends BinaryOp[String] {
59+
override val symbol: String = "sounds like"
60+
}
5661
}
5762
}
5863

mysql/src/test/resources/shop_schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ values
4848
('f76c9ace-be07-4bf3-bd4c-4a9c62882e64', 'Terrence', 'Noel', true, '1999-11-02'),
4949
('784426a5-b90a-4759-afbb-571b7a0ba35e', 'Mila', 'Paterso', true, '1990-11-16'),
5050
('df8215a2-d5fd-4c6c-9984-801a1b3a2a0b', 'Alana', 'Murray', true, '1995-11-12'),
51-
('636ae137-5b1a-4c8c-b11f-c47c624d9cdc', 'Jose', 'Wiggins', false, '1987-03-23');
51+
('636ae137-5b1a-4c8c-b11f-c47c624d9cdc', 'Jose', 'Wiggins', false, '1987-03-23'),
52+
('d4f6c156-20ac-4d27-8ced-535bf4315ebc', 'Robert', 'Rupert', false, '1998-06-11');
5253

5354
insert into products
5455
(id, name, description, image_url)

mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package zio.sql.mysql
22

3-
import zio.test._
4-
import zio.test.Assertion._
3+
import zio.Chunk
54
import zio.schema._
6-
import java.time.{ LocalDate, LocalTime, ZoneId }
7-
import java.time.format.DateTimeFormatter
85
import zio.sql.Jdbc
9-
import java.util.UUID
106
import zio.sql.table._
7+
import zio.test.Assertion._
8+
import zio.test._
9+
10+
import java.time.format.DateTimeFormatter
11+
import java.time.{ LocalDate, LocalTime, ZoneId }
12+
import java.util.UUID
1113

1214
object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc {
1315

@@ -119,6 +121,22 @@ object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc {
119121

120122
assertZIO(testResult.runHead.some)(equalTo(expected))
121123
},
124+
test("sounds like") {
125+
val query = select("Robert".soundsLike("Rupert"))
126+
127+
val testResult = execute(query)
128+
129+
assertZIO(testResult.runHead.some)(equalTo(true))
130+
},
131+
test("sounds like on column") {
132+
val query = select(customerId).from(customers).where(fName.soundsLike(lName))
133+
134+
for {
135+
result <- execute(query).runCollect
136+
} yield assertTrue(
137+
result == Chunk(UUID.fromString("d4f6c156-20ac-4d27-8ced-535bf4315ebc"))
138+
)
139+
},
122140
test("current_date") {
123141
val query = select(CurrentDate)
124142

0 commit comments

Comments
 (0)