mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-23 02:48:26 +00:00
Convert source record
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package docspell.store.qb
|
||||
|
||||
import minitest._
|
||||
import docspell.store.qb._
|
||||
import docspell.store.qb.model._
|
||||
import docspell.store.qb.DSL._
|
||||
|
||||
@ -28,10 +29,37 @@ object QueryBuilderTest extends SimpleTestSuite {
|
||||
)
|
||||
)
|
||||
|
||||
// val order =
|
||||
// orderBy(c.name.asc)
|
||||
|
||||
val q = Select(proj, tables, cond)
|
||||
println(q)
|
||||
val q = Select(proj, tables, cond).orderBy(c.name.desc)
|
||||
q match {
|
||||
case Select.Ordered(Select.SimpleSelect(proj, from, where, group), sb, vempty) =>
|
||||
assert(vempty.isEmpty)
|
||||
assertEquals(sb, OrderBy(SelectExpr.SelectColumn(c.name), OrderBy.OrderType.Desc))
|
||||
assertEquals(11, proj.size)
|
||||
from match {
|
||||
case FromExpr.From(_) =>
|
||||
fail("Unexpected from value")
|
||||
case FromExpr.Joined(f, joins) =>
|
||||
assertEquals(f, FromExpr.From(c))
|
||||
assertEquals(2, joins.size)
|
||||
joins.head match {
|
||||
case Join.InnerJoin(tbl, cond) =>
|
||||
assertEquals(tbl, owner)
|
||||
assertEquals(cond, c.ownerId === owner.id)
|
||||
case _ =>
|
||||
fail("Unexpected join result")
|
||||
}
|
||||
joins.tail.head match {
|
||||
case Join.LeftJoin(tbl, cond) =>
|
||||
assertEquals(tbl, lecturer)
|
||||
assertEquals(cond, c.lecturerId === lecturer.id)
|
||||
case _ =>
|
||||
fail("Unexpected join result")
|
||||
}
|
||||
}
|
||||
assertEquals(group, None)
|
||||
assert(where.isDefined)
|
||||
case _ =>
|
||||
fail("Unexpected case")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import minitest._
|
||||
import docspell.store.qb._
|
||||
import docspell.store.qb.model._
|
||||
import docspell.store.qb.DSL._
|
||||
import docspell.common._
|
||||
|
||||
object DoobieQueryTest extends SimpleTestSuite {
|
||||
|
||||
@ -23,28 +22,11 @@ object DoobieQueryTest extends SimpleTestSuite {
|
||||
)
|
||||
|
||||
val q = Select(proj, table, cond)
|
||||
val frag = DoobieQuery.select(q)
|
||||
val frag = DoobieQuery(q)
|
||||
assertEquals(
|
||||
frag.toString,
|
||||
"""Fragment("SELECT c.id, c.name, c.owner_id, c.lecturer_id, c.lessons FROM course c INNER JOIN person o ON c.owner_id = o.id LEFT JOIN person l ON c.lecturer_id = l.id WHERE (LOWER(c.name) LIKE ? AND o.name = ? )")"""
|
||||
)
|
||||
}
|
||||
|
||||
test("basic update") {
|
||||
val p = PersonRecord.table
|
||||
|
||||
val update = PersonRecord.update(p.name.set("john"), p.id.set(15L)).where(p.id >= 2)
|
||||
|
||||
println(DoobieQuery.update(update))
|
||||
|
||||
}
|
||||
|
||||
test("basic insert") {
|
||||
val p = PersonRecord(1, "John", Timestamp.Epoch)
|
||||
|
||||
val insert = PersonRecord.insertAll(p)
|
||||
|
||||
println(insert)
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,4 @@ object CourseRecord {
|
||||
def as(alias: String): Table =
|
||||
Table(Some(alias))
|
||||
|
||||
def update: UpdateTable =
|
||||
UpdateTable(Table(None), None, Seq.empty)
|
||||
|
||||
}
|
||||
|
@ -2,9 +2,6 @@ package docspell.store.qb.model
|
||||
|
||||
import docspell.store.qb._
|
||||
import docspell.common._
|
||||
import doobie.implicits._
|
||||
import docspell.store.impl.DoobieMeta._
|
||||
import doobie._
|
||||
|
||||
case class PersonRecord(id: Long, name: String, created: Timestamp)
|
||||
|
||||
@ -24,15 +21,4 @@ object PersonRecord {
|
||||
def as(alias: String): Table =
|
||||
Table(Some(alias))
|
||||
|
||||
def table: Table = Table(None)
|
||||
|
||||
def update(set: UpdateTable.Setter[_], sets: UpdateTable.Setter[_]*): UpdateTable =
|
||||
UpdateTable(table, None, sets :+ set)
|
||||
|
||||
def insertAll(v: PersonRecord): ConnectionIO[Int] =
|
||||
InsertTable(
|
||||
table,
|
||||
table.all,
|
||||
fr"${v.id},${v.name},${v.created}"
|
||||
).toFragment.update.run
|
||||
}
|
||||
|
Reference in New Issue
Block a user