Skip to content

Commit

Permalink
Version 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
vinga committed Aug 9, 2018
1 parent 10b8a8f commit 6b0a3f7
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 70 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
same "printed KPage" as the copyright notice for easier
identification within third-party archives.

Copyright [2018] [Kameo]
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ Add to your project maven/gradle dependency:
<dependency>
<groupId>com.kameocode</groupId>
<artifactId>any-dao</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
```
```
compile 'com.kameocode:any-dao:1.0.1'
compile 'com.kameocode:any-dao:1.0.2'
```
Create instance of AnyDao:
```
Expand Down Expand Up @@ -276,7 +276,7 @@ These expressions should be accessible directly on expression/path elements:

> Paging
```
val pagedListOfUsers = anyDao.pages(UserODB::class, Page(100)) {
val pagedListOfUsers = anyDao.pages(UserODB::class, KPage(100)) {
it[UserODB::email] like "email1"
}
pagedListOfUsers.forEach { list->
Expand Down Expand Up @@ -321,4 +321,5 @@ For more examples, see: [tests](src/test/kotlin/com/kameocode/anydao/test/)
* added support for pure java classes for anyDao.exist and anyDao.count

**v 1.0.2**
* Support for nullable select
* Support for nullable select
* Page is now KPage
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {

group 'com.kameocode'
archivesBaseName = "any-dao"
version '1.0.1'
version '1.0.2'

description 'AnyDAO is an Kotlin JPA wrapper library which makes your database queries short, clean and easy to read.'

Expand Down Expand Up @@ -113,7 +113,7 @@ uploadArchives {
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
authentication(userName: tossrhUsername, password: tossrhPassword)
}
// repository(url: mavenLocal().url)
//repository(url: mavenLocal().url)


pom.project {
Expand Down
78 changes: 39 additions & 39 deletions src/main/kotlin/com/kameocode/anydao/AnyDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class AnyDao(val em: EntityManager) {
}


fun <E : Any, RESULT : Any> all(clz: Class<E>, resultClass: Class<RESULT>, query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)? = null): List<RESULT> {
fun <E : Any, RESULT> all(clz: Class<E>, resultClass: Class<RESULT>, query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)? = null): List<RESULT> {
val qq: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>) = query ?: { this as KSelect<RESULT> }
val pc = QueryPathContext<RESULT>(clz, em)
val res = pc.invokeQuery(qq).resultList
Expand All @@ -136,23 +136,23 @@ class AnyDao(val em: EntityManager) {
return all(clz.java, E::class.java)
}

inline fun <E : Any, reified RESULT : Any> all(clz: KClass<E>, noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)? = null): List<RESULT> {
inline fun <E : Any, reified RESULT> all(clz: KClass<E>, noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)? = null): List<RESULT> {
return all(clz.java, RESULT::class.java, query)
}

@Throws(NoResultException::class)
fun <E : Any, RESULT : Any> one(clz: Class<E>, resultClass: Class<RESULT>, query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): RESULT {
fun <E : Any, RESULT> one(clz: Class<E>, resultClass: Class<RESULT>, query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): RESULT {
val pc = QueryPathContext<RESULT>(clz, em)
val jpaQuery = pc.invokeQuery(query)
return pc.mapToPluralsIfNeeded<RESULT>(jpaQuery.singleResult)
}

@Throws(NoResultException::class)
inline fun <E : Any, reified RESULT : Any> one(clz: KClass<E>, noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): RESULT {
inline fun <E : Any, reified RESULT> one(clz: KClass<E>, noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): RESULT {
return one(clz.java, RESULT::class.java, query)
}

fun <E : Any, RESULT : Any> first(clz: Class<E>, resultClass: Class<RESULT>, query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): RESULT? {
fun <E : Any, RESULT> first(clz: Class<E>, resultClass: Class<RESULT>, query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): RESULT? {
val pc = QueryPathContext<RESULT>(clz, em)
val jpaQuery = pc.invokeQuery(query)
jpaQuery.maxResults = 1
Expand Down Expand Up @@ -192,29 +192,29 @@ class AnyDao(val em: EntityManager) {
return one(clz, Long::class.java, wrapperQuery)
}

inline fun <E : Any, reified RESULT : Any> first(clz: KClass<E>, noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): RESULT? {
inline fun <E : Any, reified RESULT> first(clz: KClass<E>, noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): RESULT? {
return first(clz.java, RESULT::class.java, query)
}

inline fun <E : Any, reified RESULT : Any> allMutable(clz: KClass<E>, noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)? = null): MutableList<RESULT> {
inline fun <E : Any, reified RESULT> allMutable(clz: KClass<E>, noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)? = null): MutableList<RESULT> {
return all(clz.java, RESULT::class.java, query) as MutableList<RESULT>
}

inline fun <E : Any, reified RESULT : Any> pages(clz: KClass<E>, page: Page = Page(),
noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)
): PagesResult<RESULT> {
inline fun <E : Any, reified RESULT> pages(clz: KClass<E>, KPage: KPage = KPage(),
noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)
): KPagesResult<RESULT> {

return pages(clz.java, RESULT::class.java, page, query)
return pages(clz.java, RESULT::class.java, KPage, query)
}

fun <E : Any, RESULT : Any> pages(clz: Class<E>, resultClz: Class<RESULT>, page: Page = Page(),
query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)
): PagesResult<RESULT> {
return object : PagesResult<RESULT>(page.pageSize) {
var currentpage = page;
fun <E : Any, RESULT> pages(clz: Class<E>, resultClz: Class<RESULT>, KPage: KPage = KPage(),
query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)
): KPagesResult<RESULT> {
return object : KPagesResult<RESULT>(KPage.pageSize) {
var currentpage = KPage;

override fun beforeForeach() {
currentpage = page
currentpage = KPage
}

override fun invoke(): List<RESULT> {
Expand All @@ -225,39 +225,39 @@ class AnyDao(val em: EntityManager) {
};
}

inline fun <E : Any, reified RESULT : Any> page(clz: KClass<E>, page: Page,
noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): List<RESULT> {
inline fun <E : Any, reified RESULT> page(clz: KClass<E>, KPage: KPage,
noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): List<RESULT> {

return page(clz.java, RESULT::class.java, page, query)
return page(clz.java, RESULT::class.java, KPage, query)
}

fun <E : Any, RESULT : Any> page(clz: Class<E>, resultClass: Class<RESULT>, page: Page,
query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): List<RESULT> {
fun <E : Any, RESULT> page(clz: Class<E>, resultClass: Class<RESULT>, KPage: KPage,
query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): List<RESULT> {

val wrapperQuery: KRoot<E>.(KRoot<E>) -> (KSelect<RESULT>) = {
val result = query.invoke(this, this);
this.limit(page.pageSize);
this.skip(page.offset)
this.limit(KPage.pageSize);
this.skip(KPage.offset)
result;
}

return all(clz, resultClass, wrapperQuery)
}

inline fun <E : Any, NUM, reified RESULT : Any> pageSorted(clz: KClass<E>, prop: KProperty1<E, NUM>, num: NUM?, page: Page,
noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): List<RESULT> where NUM : Number, NUM : Comparable<NUM> {
return pageSorted(clz.java, RESULT::class.java, prop, num, page, query);
inline fun <E : Any, NUM, reified RESULT> pageSorted(clz: KClass<E>, prop: KProperty1<E, NUM>, num: NUM?, KPage: KPage,
noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): List<RESULT> where NUM : Number, NUM : Comparable<NUM> {
return pageSorted(clz.java, RESULT::class.java, prop, num, KPage, query);
}

fun <E : Any, NUM, RESULT : Any> pageSorted(clz: Class<E>, resultClz: Class<RESULT>, prop: KProperty1<E, NUM>, num: NUM?, page: Page,
query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): List<RESULT> where NUM : Number, NUM : Comparable<NUM> {
fun <E : Any, NUM, RESULT> pageSorted(clz: Class<E>, resultClz: Class<RESULT>, prop: KProperty1<E, NUM>, num: NUM?, KPage: KPage,
query: (KRoot<E>.(KRoot<E>) -> KSelect<RESULT>)): List<RESULT> where NUM : Number, NUM : Comparable<NUM> {

val wrapperQuery: KRoot<E>.(KRoot<E>) -> (KSelect<RESULT>) = {
if (num != null)
this[prop].greaterThan(num)
this.orderBy(prop)
val result = query.invoke(this, this);
this.limit(page.pageSize);
this.limit(KPage.pageSize);
this.skip(0)

result;
Expand All @@ -267,22 +267,22 @@ class AnyDao(val em: EntityManager) {
}


inline fun <reified E : Any, reified NUM> pagesSorted(clz: KClass<E>, prop: KProperty1<E, NUM>, page: Page = Page(),
inline fun <reified E : Any, reified NUM> pagesSorted(clz: KClass<E>, prop: KProperty1<E, NUM>, KPage: KPage = KPage(),
noinline query: (KRoot<E>.(KRoot<E>) -> KSelect<E>)
): PagesResult<E> where NUM : Number, NUM : Comparable<NUM> {
return pagesSorted(clz.java, E::class.java, prop, page, query);
): KPagesResult<E> where NUM : Number, NUM : Comparable<NUM> {
return pagesSorted(clz.java, E::class.java, prop, KPage, query);
}

fun <E : Any, NUM> pagesSorted(clz: Class<E>, resultClass: Class<E>, prop: KProperty1<E, NUM>, page: Page = Page(),
fun <E : Any, NUM> pagesSorted(clz: Class<E>, resultClass: Class<E>, prop: KProperty1<E, NUM>, KPage: KPage = KPage(),
query: (KRoot<E>.(KRoot<E>) -> KSelect<E>)
): PagesResult<E> where NUM : Number, NUM : Comparable<NUM> {
): KPagesResult<E> where NUM : Number, NUM : Comparable<NUM> {

return object : PagesResult<E>(page.pageSize) {
var currentpage = page;
return object : KPagesResult<E>(KPage.pageSize) {
var currentpage = KPage;
var num: NUM? = null;

override fun beforeForeach() {
currentpage = page
currentpage = KPage
num = null
}

Expand Down Expand Up @@ -318,7 +318,7 @@ class AnyDao(val em: EntityManager) {

companion object {
@JvmStatic
fun <E : Any, RESULT : Any> getPredicate(root: Root<E>, criteriaQuery: CriteriaQuery<*>, cb: CriteriaBuilder,
fun <E : Any, RESULT> getPredicate(root: Root<E>, criteriaQuery: CriteriaQuery<*>, cb: CriteriaBuilder,
query: KRoot<E>.(KRoot<E>) -> KSelect<RESULT>): Predicate {
val pc = PredicatePathContext(root as Root<Any>, criteriaQuery, cb);
return pc.toPredicate(query)
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/com/kameocode/anydao/AnyDaoClasses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.kameocode.anydao.context.PathContext
import com.kameocode.anydao.wraps.FromWrap
import com.kameocode.anydao.wraps.PathWrap
import com.kameocode.anydao.wraps.RootWrap
import com.kameocode.anydao.wraps.RootWrapUpdate
import java.io.Serializable
import javax.persistence.Tuple
import javax.persistence.TupleElement
Expand All @@ -16,7 +15,7 @@ import kotlin.reflect.KProperty1

typealias KRoot<E> = RootWrap<E, E>
typealias QueryUnit<T> = T.(T) -> Unit
typealias KQuery<E, RESULT> = KRoot<E>.(KRoot<E>) -> (KSelect<RESULT>)
typealias KQuery<E, RESULT> = RootWrap<E, E>.(RootWrap<E, E>) -> (KSelect<RESULT>)
typealias KClause<E> = PathWrap<E, Any>.(PathWrap<E, Any>) -> Unit
typealias KFromClause<E> = FromWrap<E, Any>.(FromWrap<E, Any>) -> Unit

Expand Down Expand Up @@ -116,12 +115,12 @@ class TupleWrap(private val arr: Array<Any>,

}

data class Page(val pageSize: Int = 10, val offset: Int = 0) {
fun next() = Page(pageSize, offset + pageSize)
data class KPage(val pageSize: Int = 10, val offset: Int = 0) {
fun next() = KPage(pageSize, offset + pageSize)
}

abstract class PagesResult<E>(val pageSize: Int) {
abstract fun invoke(): List<E>
abstract class KPagesResult<E>(val pageSize: Int) {
internal abstract fun invoke(): List<E>

abstract protected fun beforeForeach()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class QueryPathContext<G>(clz: Class<*>,



fun <RESULT : Any> mapToPluralsIfNeeded(res: RESULT): RESULT {
fun <RESULT> mapToPluralsIfNeeded(res: RESULT): RESULT {
if (selector is com.kameocode.anydao.AnyDao.PathArraySelect ) {
return res;
}
Expand All @@ -82,7 +82,7 @@ class QueryPathContext<G>(clz: Class<*>,
return res
}

fun <RESULT : Any> mapToPluralsIfNeeded(res: List<RESULT>): List<RESULT> {
fun <RESULT> mapToPluralsIfNeeded(res: List<RESULT>): List<RESULT> {
if (selector is com.kameocode.anydao.AnyDao.PathArraySelect) {
return res;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/com/kameocode/anydao/wraps/PathWrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ open class PathWrap<E, G> constructor(
return this
}


infix fun <F> select(pw: KProperty1<E, F>): SelectWrap<F> {
return select(get(pw))
}
Expand All @@ -55,6 +54,10 @@ open class PathWrap<E, G> constructor(
}


infix fun <F> selectNullable(pw: ExpressionWrap<F, G>): SelectWrap<F?> {
return pw.getDirectSelection() as SelectWrap<F?>
}

fun <F, G> select(pw1: ISelectExpressionProvider<F>, pw2: ISelectExpressionProvider<G>): com.kameocode.anydao.AnyDao.PathPairSelect<F, G> {
return com.kameocode.anydao.AnyDao.PathPairSelect(pw1.getDirectSelection(), pw2.getDirectSelection(), false, pc.cb)
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/com/kameocode/anydao/test/ClausesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.kameocode.anydao.KClause
import com.kameocode.anydao.KFromClause
import com.kameocode.anydao.KQuery
import com.kameocode.anydao.KRoot
import com.kameocode.anydao.Page
import com.kameocode.anydao.KPage
import com.kameocode.anydao.test.helpers.AddressODB
import com.kameocode.anydao.test.helpers.BaseTest
import com.kameocode.anydao.test.helpers.TaskODB
Expand Down Expand Up @@ -320,8 +320,8 @@ class ClausesTest : BaseTest() {
// expected
}
val first = anyDao.first(UserODB::class, query)
val page = anyDao.page(UserODB::class, Page(), query)
val pages = anyDao.pages(UserODB::class, Page(), query)
val page = anyDao.page(UserODB::class, KPage(), query)
val pages = anyDao.pages(UserODB::class, KPage(), query)
Assert.assertEquals(setOf(u2.task, u3.task).map { it.id }.toSet(), res1.map { it.id }.toSet())

Assert.assertEquals(2, count)
Expand Down
Loading

0 comments on commit 6b0a3f7

Please sign in to comment.