Skip to content

Commit

Permalink
Added support for @as alias for columns. Closed #5
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Sep 11, 2018
1 parent dc18a6d commit 77a307a
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 108 deletions.
9 changes: 7 additions & 2 deletions src/main/kotlin/com/github/vokorm/ConnectionUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import org.sql2o.Query
* Finds all instances of given entity. Fails if there is no table in the database with the name of [databaseTableName]. The list is eager
* and thus it's useful for smallish tables only.
*/
fun <T : Any> Connection.findAll(clazz: Class<T>): List<T> = createQuery("select * from ${clazz.entityMeta.databaseTableName}").executeAndFetch(clazz)
fun <T : Any> Connection.findAll(clazz: Class<T>): List<T> =
createQuery("select * from ${clazz.entityMeta.databaseTableName}")
.setColumnMappings(clazz.entityMeta.getSql2oColumnMappings())
.executeAndFetch(clazz)

/**
* Retrieves entity with given [id]. Returns null if there is no such entity.
*/
fun <T : Any> Connection.findById(clazz: Class<T>, id: Any): T? =
createQuery("select * from ${clazz.entityMeta.databaseTableName} where id = :id")
.addParameter("id", id)
.setColumnMappings(clazz.entityMeta.getSql2oColumnMappings())
.executeAndFetchFirst(clazz)

/**
Expand Down Expand Up @@ -75,14 +79,15 @@ fun <T: Any> Connection.findBy(clazz: Class<T>, limit: Int, filter: Filter<T>):
require (limit >= 0) { "$limit is less than 0" }
val query = createQuery("select * from ${clazz.entityMeta.databaseTableName} where ${filter.toSQL92()} limit $limit")
filter.getSQL92Parameters().entries.forEach { (name, value) -> query.addParameter(name, value) }
query.setColumnMappings(clazz.entityMeta.getSql2oColumnMappings())
return query.executeAndFetch(clazz)
}

/**
* Deletes all entities with given [clazz] matching given criteria [block].
*/
fun <T: Any> Connection.deleteBy(clazz: Class<T>, block: SqlWhereBuilder<T>.()-> Filter<T>) {
val filter = block(SqlWhereBuilder())
val filter = block(SqlWhereBuilder(clazz))
val query = createQuery("delete from ${clazz.entityMeta.databaseTableName} where ${filter.toSQL92()}")
filter.getSQL92Parameters().entries.forEach { (name, value) -> query.addParameter(name, value) }
query.executeUpdate()
Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/com/github/vokorm/Dao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ inline fun <reified T: Any> DaoOfAny<T>.getById(id: Any): T = db { con.getById(T
* @throws IllegalArgumentException if there is no entity matching given criteria, or if there are two or more matching entities.
*/
inline fun <ID: Any, reified T: Entity<ID>> Dao<T>.getBy(noinline block: SqlWhereBuilder<T>.()-> Filter<T>): T {
val filter = block(SqlWhereBuilder())
val filter = block(SqlWhereBuilder(T::class.java))
return db { con.getBy(T::class.java, filter) }
}

Expand All @@ -88,7 +88,7 @@ inline fun <ID: Any, reified T: Entity<ID>> Dao<T>.getBy(noinline block: SqlWher
* @throws IllegalArgumentException if there is no entity matching given criteria, or if there are two or more matching entities.
*/
inline fun <reified T: Any> DaoOfAny<T>.getBy(noinline block: SqlWhereBuilder<T>.()-> Filter<T>): T {
val filter = block(SqlWhereBuilder())
val filter = block(SqlWhereBuilder(T::class.java))
return db { con.getBy(T::class.java, filter) }
}

Expand All @@ -107,7 +107,7 @@ inline fun <reified T: Any> DaoOfAny<T>.getBy(noinline block: SqlWhereBuilder<T>
* @throws IllegalArgumentException if there are two or more matching entities.
*/
inline fun <ID: Any, reified T: Entity<ID>> Dao<T>.findSpecificBy(noinline block: SqlWhereBuilder<T>.()-> Filter<T>): T? {
val filter = block(SqlWhereBuilder())
val filter = block(SqlWhereBuilder(T::class.java))
return db { con.findSpecificBy(T::class.java, filter) }
}

Expand All @@ -125,7 +125,7 @@ inline fun <ID: Any, reified T: Entity<ID>> Dao<T>.findSpecificBy(noinline block
* @throws IllegalArgumentException if there are two or more matching entities.
*/
inline fun <reified T: Any> DaoOfAny<T>.findSpecificBy(noinline block: SqlWhereBuilder<T>.()-> Filter<T>): T? {
val filter = block(SqlWhereBuilder())
val filter = block(SqlWhereBuilder(T::class.java))
return db { con.findSpecificBy(T::class.java, filter) }
}

Expand Down Expand Up @@ -163,12 +163,12 @@ inline fun <reified T: Any> DaoOfAny<T>.count(): Long = db { con.getCount(T::cla
/**
* Counts all rows in given table which matches given [block] clause.
*/
inline fun <reified T: Entity<*>> Dao<T>.count(noinline block: SqlWhereBuilder<T>.()-> Filter<T>): Long = db { con.getCount(T::class.java, SqlWhereBuilder<T>().block()) }
inline fun <reified T: Entity<*>> Dao<T>.count(noinline block: SqlWhereBuilder<T>.()-> Filter<T>): Long = db { con.getCount(T::class.java, SqlWhereBuilder<T>(T::class.java).block()) }

/**
* Counts all rows in given table which matches given [block] clause.
*/
inline fun <reified T: Any> DaoOfAny<T>.count(noinline block: SqlWhereBuilder<T>.()-> Filter<T>): Long = db { con.getCount(T::class.java, SqlWhereBuilder<T>().block()) }
inline fun <reified T: Any> DaoOfAny<T>.count(noinline block: SqlWhereBuilder<T>.()-> Filter<T>): Long = db { con.getCount(T::class.java, SqlWhereBuilder<T>(T::class.java).block()) }

/**
* Deletes row with given ID. Does nothing if there is no such row.
Expand Down Expand Up @@ -236,7 +236,7 @@ inline val <reified T: Entity<*>> Dao<T>.meta: EntityMeta
* ```
*/
inline fun <reified T: Any> DaoOfAny<T>.findBy(limit: Int = Int.MAX_VALUE, noinline block: SqlWhereBuilder<T>.()-> Filter<T>): List<T> =
db { con.findBy(T::class.java, limit, block(SqlWhereBuilder())) }
db { con.findBy(T::class.java, limit, block(SqlWhereBuilder(T::class.java))) }


/**
Expand All @@ -255,4 +255,4 @@ inline fun <reified T: Any> DaoOfAny<T>.findBy(limit: Int = Int.MAX_VALUE, noinl
* ```
*/
inline fun <ID, reified T: Entity<ID>> Dao<T>.findBy(limit: Int = Int.MAX_VALUE, noinline block: SqlWhereBuilder<T>.()-> Filter<T>): List<T> =
db { con.findBy(T::class.java, limit, block(SqlWhereBuilder())) }
db { con.findBy(T::class.java, limit, block(SqlWhereBuilder(T::class.java))) }
Loading

0 comments on commit 77a307a

Please sign in to comment.