Skip to content

Commit

Permalink
feat: improvement for sounds like on literal
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriiBerezin committed Apr 29, 2024
1 parent bc13907 commit b71d4c9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
17 changes: 12 additions & 5 deletions mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package zio.sql.mysql

import java.time._
import java.sql.ResultSet
import java.util.UUID
import zio.sql.Sql
import zio.sql.select._
import zio.sql.expr._
import zio.sql.ops.Operator.RelationalOp
import zio.sql.select._
import zio.sql.typetag._
import zio.sql.{ Features, Sql }

import java.sql.ResultSet
import java.time._
import java.util.UUID

trait MysqlSqlModule extends Sql { self =>

Expand All @@ -30,6 +31,12 @@ trait MysqlSqlModule extends Sql { self =>
def soundsLike[F2, A2 <: A1](that: Expr[F2, A2, B])(implicit ev: B <:< String): Expr[F1 with F2, A2, Boolean] =
Expr.Relational(expr, that, RelationalOp.MySqlExtensions.SoundsLike)
}

implicit class LiteralOps[B](line: B)(implicit literal: B => Expr[Features.Literal, Any, B]) {
def soundsLike[F, A](that: Expr[F, A, B])(implicit
ev: B <:< String
): Expr[Features.Literal with F, A, Boolean] = literal(line).soundsLike(that)
}
}

object MysqlFunctionDef {
Expand Down
32 changes: 30 additions & 2 deletions mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package zio.sql.mysql
import zio.Chunk
import zio.schema._
import zio.sql.Jdbc
import zio.sql.expr.Expr.literal
import zio.sql.table._
import zio.test.Assertion._
import zio.test._
Expand Down Expand Up @@ -124,7 +123,21 @@ object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc {
assertZIO(testResult.runHead.some)(equalTo(expected))
},
test("sounds like") {
val query = select(literal("Robert").soundsLike("Rupert"))
val query = select("Robert".soundsLike("Rupert"))

val testResult = execute(query)

assertZIO(testResult.runHead.some)(equalTo(true))
},
test("sounds like don't match") {
val query = select("Grisha".soundsLike("Berezin"))

val testResult = execute(query)

assertZIO(testResult.runHead.some)(equalTo(false))
},
test("sounds like don't match inverse") {
val query = select("Grisha".soundsLike("Berezin").isNotTrue)

val testResult = execute(query)

Expand All @@ -139,6 +152,21 @@ object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc {
result == Chunk(UUID.fromString("d4f6c156-20ac-4d27-8ced-535bf4315ebc"))
)
},
test("sounds like on column inverse") {
val query = select(customerId).from(customers).where(fName.soundsLike(lName).isNotTrue)

for {
result <- execute(query).runCollect
} yield assertTrue(
result == Chunk(
UUID.fromString("60b01fc9-c902-4468-8d49-3c0f989def37"),
UUID.fromString("636ae137-5b1a-4c8c-b11f-c47c624d9cdc"),
UUID.fromString("784426a5-b90a-4759-afbb-571b7a0ba35e"),
UUID.fromString("df8215a2-d5fd-4c6c-9984-801a1b3a2a0b"),
UUID.fromString("f76c9ace-be07-4bf3-bd4c-4a9c62882e64")
)
)
},
test("current_date") {
val query = select(CurrentDate)

Expand Down

0 comments on commit b71d4c9

Please sign in to comment.