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

Commit 3b9014a

Browse files
feat: improvement for sounds like on literal
1 parent bc13907 commit 3b9014a

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package zio.sql.mysql
22

3-
import java.time._
4-
import java.sql.ResultSet
5-
import java.util.UUID
6-
import zio.sql.Sql
7-
import zio.sql.select._
83
import zio.sql.expr._
94
import zio.sql.ops.Operator.RelationalOp
5+
import zio.sql.select._
106
import zio.sql.typetag._
7+
import zio.sql.{ Features, Sql }
8+
9+
import java.sql.ResultSet
10+
import java.time._
11+
import java.util.UUID
1112

1213
trait MysqlSqlModule extends Sql { self =>
1314

@@ -30,6 +31,12 @@ trait MysqlSqlModule extends Sql { self =>
3031
def soundsLike[F2, A2 <: A1](that: Expr[F2, A2, B])(implicit ev: B <:< String): Expr[F1 with F2, A2, Boolean] =
3132
Expr.Relational(expr, that, RelationalOp.MySqlExtensions.SoundsLike)
3233
}
34+
35+
implicit class LiteralOps[B](line: B)(implicit literal: B => Expr[Features.Literal, Any, B]) {
36+
def soundsLike[F, A](that: Expr[F, A, B])(implicit
37+
ev: B <:< String
38+
): Expr[Features.Literal with F, A, Boolean] = literal(line).soundsLike(that)
39+
}
3340
}
3441

3542
object MysqlFunctionDef {

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package zio.sql.mysql
33
import zio.Chunk
44
import zio.schema._
55
import zio.sql.Jdbc
6-
import zio.sql.expr.Expr.literal
76
import zio.sql.table._
87
import zio.test.Assertion._
98
import zio.test._
@@ -124,7 +123,21 @@ object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc {
124123
assertZIO(testResult.runHead.some)(equalTo(expected))
125124
},
126125
test("sounds like") {
127-
val query = select(literal("Robert").soundsLike("Rupert"))
126+
val query = select("Robert".soundsLike("Rupert"))
127+
128+
val testResult = execute(query)
129+
130+
assertZIO(testResult.runHead.some)(equalTo(true))
131+
},
132+
test("sounds like don't match") {
133+
val query = select("Grisha".soundsLike("Berezin"))
134+
135+
val testResult = execute(query)
136+
137+
assertZIO(testResult.runHead.some)(equalTo(false))
138+
},
139+
test("sounds like don't match inverse") {
140+
val query = select("Grisha".soundsLike("Berezin").isNotTrue)
128141

129142
val testResult = execute(query)
130143

@@ -139,6 +152,21 @@ object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc {
139152
result == Chunk(UUID.fromString("d4f6c156-20ac-4d27-8ced-535bf4315ebc"))
140153
)
141154
},
155+
test("sounds like on column inverse") {
156+
val query = select(customerId).from(customers).where(fName.soundsLike(lName).isNotTrue)
157+
158+
for {
159+
result <- execute(query).runCollect
160+
} yield assertTrue(
161+
result == Chunk(
162+
UUID.fromString("60b01fc9-c902-4468-8d49-3c0f989def37"),
163+
UUID.fromString("f76c9ace-be07-4bf3-bd4c-4a9c62882e64"),
164+
UUID.fromString("784426a5-b90a-4759-afbb-571b7a0ba35e"),
165+
UUID.fromString("df8215a2-d5fd-4c6c-9984-801a1b3a2a0b"),
166+
UUID.fromString("636ae137-5b1a-4c8c-b11f-c47c624d9cdc")
167+
)
168+
)
169+
},
142170
test("current_date") {
143171
val query = select(CurrentDate)
144172

0 commit comments

Comments
 (0)