From 6373fc9b1fddf644d1ca8d2a532fd9202700f9ac Mon Sep 17 00:00:00 2001 From: Grigorii Berezin Date: Sun, 28 Apr 2024 22:21:53 +0300 Subject: [PATCH] feat: implement sounds like function for mysql --- .../main/scala/zio/sql/mysql/MysqlSqlModule.scala | 6 ++++++ .../zio/sql/mysql/CustomFunctionDefSpec.scala | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala b/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala index 6b116f2ce..bca619b5b 100644 --- a/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala +++ b/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala @@ -24,6 +24,11 @@ trait MysqlSqlModule extends Sql { self => ) } } + + implicit class TStringOps[F1, F2, Source](param1: Expr[F1, Source, String]) { + def `sounds like`(param2: Expr[F2, Source, String]): Expr[F1 with F2, Source, Boolean] = + MysqlFunctionDef.SoundsLike(param1, param2) + } } object MysqlFunctionDef { @@ -44,5 +49,6 @@ trait MysqlSqlModule extends Sql { self => val RPad = FunctionDef[(String, Int, String), String](FunctionName("rpad")) val Uuid = Expr.FunctionCall0[UUID](FunctionDef[Any, UUID](FunctionName("uuid"))) val Radians = FunctionDef[Double, Double](FunctionName("radians")) + val SoundsLike = FunctionDef[(String, String), Boolean](FunctionName("sounds like")) } } diff --git a/mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala b/mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala index 34f512f73..9d3298c8e 100644 --- a/mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala +++ b/mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala @@ -12,6 +12,7 @@ import zio.sql.table._ object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc { import MysqlFunctionDef._ + import MysqlSpecific._ case class Customers(id: UUID, dob: LocalDate, first_name: String, last_name: String, verified: Boolean) @@ -119,6 +120,20 @@ object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc { assertZIO(testResult.runHead.some)(equalTo(expected)) }, + test("sounds like") { + val query = select(SoundsLike("Robert", "Rupert")) + + val testResult = execute(query) + + assertZIO(testResult.runHead.some)(equalTo(true)) + }, + test("sounds like infix") { + val query = select("Robert" `sounds_like` "Rubert") + + val testResult = execute(query) + + assertZIO(testResult.runHead.some)(equalTo(true)) + }, test("current_date") { val query = select(CurrentDate)