diff --git a/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala b/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala index 975f6830..fd8fd84f 100644 --- a/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala +++ b/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala @@ -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 => @@ -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 { diff --git a/mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala b/mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala index bcf4dad1..32ba491e 100644 --- a/mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala +++ b/mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala @@ -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._ @@ -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) @@ -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)