Skip to content

Commit

Permalink
Lots of minor fixes including fixing sort for SQLiteSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed May 7, 2024
1 parent d85f020 commit 4029a7b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Scala versions
val scala213 = "2.13.14"
val scala3 = "3.4.1"
val scala3 = "3.3.3"
val scala2 = List(scala213)
val allScalaVersions = scala3 :: scala2

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/lightdb/LightDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class LightDB {

def commit(): IO[Unit] = collections.map(_.commit()).sequence.map(_ => ())

protected[lightdb] def verifyInitialized(): Unit = if (!initialized) throw new RuntimeException("Database not initialized!")
protected[lightdb] def verifyInitialized(): Unit = if (!initialized) throw new RuntimeException(s"Database not initialized ($directory)!")

def init(truncate: Boolean = false): IO[Unit] = if (_initialized.compareAndSet(false, true)) {
for {
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/scala/lightdb/model/AbstractCollection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lightdb.model

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import cats.implicits.toTraverseOps
import cats.implicits._
import io.chrisdavenport.keysemaphore.KeySemaphore
import lightdb.{DocLock, Document, Id, IndexedLinks, LightDB, MaxLinks, Store}

Expand Down Expand Up @@ -83,6 +83,7 @@ trait AbstractCollection[D <: Document[D]] {
def truncate(): IO[Unit] = for {
_ <- store.truncate()
_ <- model.indexedLinks.map(_.store.truncate()).sequence
_ <- commit().whenA(autoCommit)
} yield ()

def get(id: Id[D]): IO[Option[D]] = store.getJsonDoc(id)(model.rw)
Expand Down
16 changes: 13 additions & 3 deletions sqlite/src/main/scala/lightdb/sqlite/SQLiteSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fabric._
import fabric.io.JsonFormatter
import lightdb.{Document, Id}
import lightdb.index.{IndexSupport, IndexedField}
import lightdb.query.{PagedResults, Query, SearchContext}
import lightdb.query.{PagedResults, Query, SearchContext, Sort}
import lightdb.util.FlushingBacklog

import java.nio.file.Path
Expand Down Expand Up @@ -84,12 +84,20 @@ trait SQLiteSupport[D <: Document[D]] extends IndexSupport[D] {
} else {
-1
}
// TODO: Add sort
val sort = query.sort.collect {
case Sort.ByField(field, reverse) =>
val dir = if (reverse) "DESC" else "ASC"
s"${field.fieldName} $dir"
} match {
case Nil => ""
case list => list.mkString("ORDER BY ", ", ", "")
}
val sql = s"""SELECT
| *
|FROM
| $collectionName
|$filters
|$sort
|LIMIT
| ${query.pageSize}
|OFFSET
Expand Down Expand Up @@ -126,12 +134,14 @@ trait SQLiteSupport[D <: Document[D]] extends IndexSupport[D] {
override protected def indexDoc(doc: D, fields: List[IndexedField[_, D]]): IO[Unit] =
backlog.enqueue(doc).map(_ => ())

private def prepare(sql: String, params: List[Json]): PreparedStatement = {
private def prepare(sql: String, params: List[Json]): PreparedStatement = try {
val ps = connection.prepareStatement(sql)
params.zipWithIndex.foreach {
case (value, index) => setValue(ps, index + 1, value)
}
ps
} catch {
case t: Throwable => throw new RuntimeException(s"Error handling SQL query: $sql (params: ${params.mkString(", ")})", t)
}

private def setValue(ps: PreparedStatement, index: Int, value: Json): Unit = value match {
Expand Down

0 comments on commit 4029a7b

Please sign in to comment.