Skip to content

Commit

Permalink
- renamed KeyEqualsTo to EqualsTo
Browse files Browse the repository at this point in the history
- updated doc
  • Loading branch information
Aleksandr Shabalin committed Nov 27, 2024
1 parent 0d03992 commit a5c8fe7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.7.14"
version = "2.7.5"
maxColumn = 120
align = most
continuationIndent.defnSite = 2
Expand Down
49 changes: 33 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,47 @@ import scala.jdk.DurationConverters._
import com.datastax.oss.driver.api.core.ConsistencyLevel
import com.ringcentral.cassandra4io.CassandraSession
import com.ringcentral.cassandra4io.cql._

case class Model(id: Int, data: String)


case class Model(pk: Long, ck: String, data: String, metaData: String)
case class Key(pk: Long, ck: String)
case class Data(data: String, metaData: String)

trait Dao[F[_]] {
def put(value: Model): F[Unit]
def get(id: Int): F[Option[Model]]
def update(key: Key, data: Data): F[Unit]
def get(key: Key): F[Option[Model]]
}

object Dao {

private val tableName = "table"
private val insertQuery = cqlt"insert into ${Const(tableName)} (id, data) values (${Put[Int]}, ${Put[String]})"
.config(_.setTimeout(1.second.toJava))
private val selectQuery = cqlt"select id, data from ${Const(tableName)} where id = ${Put[Int]}".as[Model]

def apply[F[_]: Async](session: CassandraSession[F]) = for {
private val tableName = "table"
private val insertQuery =
cqlt"insert into ${Const(tableName)} (pk, ck, data, meta_data) values (${Put[Long]}, ${Put[String]}, ${Put[String]}, ${Put[String]})"
.config(_.setTimeout(1.second.toJava))
private val insertQueryAlternative =
cqlt"insert into ${Const(tableName)} (${Columns[Model]}) values (${Values[Model]})"
private val updateQuery = cqlt"update ${Const(tableName)} set ${Assignment[Data]} where ${EqualsTo[Key]}"
private val selectQuery = cqlt"select ${Columns[Model]} from ${Const(tableName)} where ${EqualsTo[Key]}".as[Model]

def apply[F[_] : Async](session: CassandraSession[F]) = for {
insert <- insertQuery.prepare(session)
select <- selectQuery.prepare(session)
update <- updateQuery.prepare(session)
updateAlternative <- insertQueryAlternative.prepare(session)
select <- selectQuery.prepare(session)
} yield new Dao[F] {
override def put(value: Model) = insert(value.id, value.data).execute.void
override def get(id: Int) = select(id).config(_.setExecutionProfileName("default")).select.head.compile.last
}
}
override def put(value: Model) = insert(
value.pk,
value.ck,
value.data,
value.metaData
).execute.void // updateAlternative(value).execute.void
override def update(key: Key, data: Data): F[Unit] = updateQuery(data, key).execute.void
override def get(key: Key) = select(id).config(_.setExecutionProfileName("default")).select.head.compile.last
}
}
```
As you can see `${Columns[Model]}` expands to `pk, ck, data, meta_data`, `${Values[Model]}` to `?, ?, ?, ?`, `${Assignment[Data]}` to `pk = ?, ck = ?, data = ?, meta_data = ?` and `${EqualsTo[Key]}` expands to `pk = ? and ck = ? and data = ? and meta_data = ?`.
Latter three types adjust query type as well for being able to bind corresponding values

### Handling optional fields (`null`)

Expand Down
8 changes: 4 additions & 4 deletions src/it/scala/com/ringcentral/cassandra4io/cql/CqlSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.ringcentral.cassandra4io.CassandraTestsSharedInstances
import fs2.Stream
import weaver._

import java.time.{Duration, LocalDate, LocalTime}
import java.time.{ Duration, LocalDate, LocalTime }
import java.util.UUID
import java.util.concurrent.atomic.AtomicInteger

Expand Down Expand Up @@ -202,7 +202,7 @@ trait CqlSuite {

val insert = cqlt"INSERT INTO ${Const("test_data_interpolated")}(${Columns[Table]}) VALUES (${Values[Table]})"
val select =
cqlt"SELECT ${Columns[Table]} FROM ${Const("test_data_interpolated")} WHERE ${KeyEqualsTo[Key]}"
cqlt"SELECT ${Columns[Table]} FROM ${Const("test_data_interpolated")} WHERE ${EqualsTo[Key]}"
.as[Table]

val data1 = Table(1, "projection-1", "data-1", 1, 1732547921580L)
Expand All @@ -224,9 +224,9 @@ trait CqlSuite {
case class Data(projectionData: String, offset: Long, timestamp: Long)
case class Key(key: Long, projectionKey: String)

val update = cqlt"UPDATE ${Const("test_data_interpolated")} SET ${Assignment[Data]} WHERE ${KeyEqualsTo[Key]}"
val update = cqlt"UPDATE ${Const("test_data_interpolated")} SET ${Assignment[Data]} WHERE ${EqualsTo[Key]}"
val select =
cqlt"SELECT ${Columns[Data]} FROM ${Const("test_data_interpolated")} WHERE ${KeyEqualsTo[Key]}"
cqlt"SELECT ${Columns[Data]} FROM ${Const("test_data_interpolated")} WHERE ${EqualsTo[Key]}"
.as[Data]

val data1 = Data("data-1", 1, 1732547921580L)
Expand Down
28 changes: 14 additions & 14 deletions src/main/scala/com/ringcentral/cassandra4io/cql/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,17 @@ package object cql {
QueryTemplate[V, Row](
ctx.parts
.foldLeft[(HList, StringBuilder)]((params, new StringBuilder())) {
case ((Const(const) :: tail, builder), part) => (tail, builder.appendAll(part).appendAll(const))
case (((restriction: KeyEqualsTo[_]) :: tail, builder), part) =>
case ((Const(const) :: tail, builder), part) => (tail, builder.appendAll(part).appendAll(const))
case (((restriction: EqualsTo[_]) :: tail, builder), part) =>
(tail, builder.appendAll(part).appendAll(restriction.keys.map(key => s"${key} = ?").mkString(" AND ")))
case (((assignment: Assignment[_]) :: tail, builder), part) =>
case (((assignment: Assignment[_]) :: tail, builder), part) =>
(tail, builder.appendAll(part).appendAll(assignment.keys.map(key => s"${key} = ?").mkString(", ")))
case (((columns: Columns[_]) :: tail, builder), part) =>
case (((columns: Columns[_]) :: tail, builder), part) =>
(tail, builder.appendAll(part).appendAll(columns.keys.mkString(", ")))
case (((values: Values[_]) :: tail, builder), part) =>
case (((values: Values[_]) :: tail, builder), part) =>
(tail, builder.appendAll(part).appendAll(List.fill(values.size)("?").mkString(", ")))
case ((_ :: tail, builder), part) => (tail, builder.appendAll(part).appendAll("?"))
case ((HNil, builder), part) => (HNil, builder.appendAll(part))
case ((_ :: tail, builder), part) => (tail, builder.appendAll(part).appendAll("?"))
case ((HNil, builder), part) => (HNil, builder.appendAll(part))
}
._2
.toString(),
Expand Down Expand Up @@ -289,27 +289,27 @@ package object cql {
}

case class Const(fragment: String)
trait Columns[T] {
trait Columns[T] {
def keys: List[String]
}
object Columns {
object Columns {
def apply[T: ColumnsValues]: Columns[T] = new Columns[T] {
override def keys: List[String] = ColumnsValues[T].keys
}
}
trait Values[T] {
trait Values[T] {
def size: Int
def binder: Binder[T]
}
object Values {
object Values {
def apply[T: ColumnsValues]: Values[T] = new Values[T] {
override def size: Int = ColumnsValues[T].size
override def binder: Binder[T] = ColumnsValues[T].binder
}
}
trait KeyEqualsTo[T] extends Columns[T] with Values[T]
object KeyEqualsTo {
def apply[T: ColumnsValues]: KeyEqualsTo[T] = new KeyEqualsTo[T] {
trait EqualsTo[T] extends Columns[T] with Values[T]
object EqualsTo {
def apply[T: ColumnsValues]: EqualsTo[T] = new EqualsTo[T] {
override def keys: List[String] = ColumnsValues[T].keys
override def size: Int = ColumnsValues[T].size
override def binder: Binder[T] = ColumnsValues[T].binder
Expand Down

0 comments on commit a5c8fe7

Please sign in to comment.