Skip to content

Commit

Permalink
feat: implement sounds like function for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriiBerezin committed Apr 29, 2024
1 parent 15a33a3 commit b116754
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions core/jvm/src/main/scala/zio/sql/expr/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ sealed trait Expr[-F, -A, +B] { self =>
): Expr[F with F2, A1, Boolean] =
Expr.Relational(self, that, RelationalOp.LessThanEqual)

def soundsLike[F2, A1 <: A](that: Expr[F2, A1, String])(implicit
ev: B <:< String
): Expr[F with F2, A1, Boolean] =
Expr.Relational(self, that, RelationalOp.MySqlExtensions.SoundsLike)

def like[F2, A1 <: A](that: Expr[F2, A1, String])(implicit ev: B <:< String): Expr[F with F2, A1, Boolean] =
Expr.Relational(self, that, RelationalOp.Like)

Expand Down
7 changes: 6 additions & 1 deletion core/jvm/src/main/scala/zio/sql/ops/Operator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ object Operator {
final case class OrBit[A: IsIntegral]() extends BinaryOp[A] {
def isIntegral: IsIntegral[A] = implicitly[IsIntegral[A]]
override val symbol: String = "|"

}
}

Expand Down Expand Up @@ -97,6 +96,12 @@ object Operator {
case object Like extends RelationalOp {
override val symbol: String = "like"
}

object MySqlExtensions {
case object SoundsLike extends RelationalOp {
override val symbol: String = "sounds like"
}
}
}

sealed trait UnaryOp[A] extends Operator
Expand Down
3 changes: 2 additions & 1 deletion mysql/src/test/resources/shop_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ values
('f76c9ace-be07-4bf3-bd4c-4a9c62882e64', 'Terrence', 'Noel', true, '1999-11-02'),
('784426a5-b90a-4759-afbb-571b7a0ba35e', 'Mila', 'Paterso', true, '1990-11-16'),
('df8215a2-d5fd-4c6c-9984-801a1b3a2a0b', 'Alana', 'Murray', true, '1995-11-12'),
('636ae137-5b1a-4c8c-b11f-c47c624d9cdc', 'Jose', 'Wiggins', false, '1987-03-23');
('636ae137-5b1a-4c8c-b11f-c47c624d9cdc', 'Jose', 'Wiggins', false, '1987-03-23'),
('d4f6c156-20ac-4d27-8ced-535bf4315ebc', 'Robert', 'Rupert', false, '1998-06-11');

insert into products
(id, name, description, image_url)
Expand Down
29 changes: 24 additions & 5 deletions mysql/src/test/scala/zio/sql/mysql/CustomFunctionDefSpec.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package zio.sql.mysql

import zio.test._
import zio.test.Assertion._
import zio.Chunk
import zio.schema._
import java.time.{ LocalDate, LocalTime, ZoneId }
import java.time.format.DateTimeFormatter
import zio.sql.Jdbc
import java.util.UUID
import zio.sql.expr.Expr.literal
import zio.sql.table._
import zio.test.Assertion._
import zio.test._

import java.time.format.DateTimeFormatter
import java.time.{LocalDate, LocalTime, ZoneId}
import java.util.UUID

object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc {

Expand Down Expand Up @@ -119,6 +122,22 @@ object CustomFunctionDefSpec extends MysqlRunnableSpec with Jdbc {

assertZIO(testResult.runHead.some)(equalTo(expected))
},
test("sounds like") {
val query = select("Robert".soundsLike("Rupert"))

val testResult = execute(query)

assertZIO(testResult.runHead.some)(equalTo(true))
},
test("sounds like on column") {
val query = select(customerId).from(customers).where(fName.soundsLike(lName))

for {
result <- execute(query).runCollect
} yield assertTrue(
result == Chunk(UUID.fromString("d4f6c156-20ac-4d27-8ced-535bf4315ebc"))
)
},
test("current_date") {
val query = select(CurrentDate)

Expand Down

0 comments on commit b116754

Please sign in to comment.