-
Notifications
You must be signed in to change notification settings - Fork 593
Expand file tree
/
Copy pathQueryExtensions.kt
More file actions
211 lines (133 loc) · 7.94 KB
/
Copy pathQueryExtensions.kt
File metadata and controls
211 lines (133 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.raizlabs.android.dbflow.kotlinextensions
import android.database.Cursor
import com.raizlabs.android.dbflow.sql.language.*
import com.raizlabs.android.dbflow.sql.language.Set
import com.raizlabs.android.dbflow.sql.language.property.IProperty
import com.raizlabs.android.dbflow.sql.queriable.AsyncQuery
import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable
import com.raizlabs.android.dbflow.sql.queriable.Queriable
import com.raizlabs.android.dbflow.structure.AsyncModel
import com.raizlabs.android.dbflow.structure.BaseModel
import com.raizlabs.android.dbflow.structure.database.DatabaseStatement
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction
import kotlin.reflect.KClass
/**
* Description: A file containing extensions for adding query syntactic sugar.
*/
val select: Select
get() = SQLite.select()
inline fun <reified T : Any> Select.from(): From<T> = from(T::class.java)
fun <T : Any> delete(modelClass: KClass<T>): From<T> = SQLite.delete(modelClass.java)
infix fun <T : Any> Select.from(modelClass: KClass<T>): From<T> = from(modelClass.java)
infix fun <T : Any> From<T>.whereExists(where: Where<T>): Where<T> = where().exists(where)
infix fun <T : Any> From<T>.where(sqlCondition: SQLCondition): Where<T> = where(sqlCondition)
infix fun <T : Any> Set<T>.where(sqlCondition: SQLCondition): Where<T> = where(sqlCondition)
infix fun <T : Any> Where<T>.and(sqlCondition: SQLCondition): Where<T> = and(sqlCondition)
infix fun <T : Any> Where<T>.or(sqlCondition: SQLCondition): Where<T> = and(sqlCondition)
infix fun <T : Any> Case<T>.`when`(sqlCondition: SQLCondition): CaseCondition<T> = `when`(sqlCondition)
infix fun <T : Any> Case<T>.`when`(property: IProperty<*>): CaseCondition<T> = `when`(property)
infix fun <T : Any> Case<T>.`when`(value: T?): CaseCondition<T> = `when`(value)
infix fun <T : Any> CaseCondition<T>.then(value: T?): Case<T> = then(value)
infix fun <T : Any> CaseCondition<T>.then(property: IProperty<*>): Case<T> = then(property)
infix fun <T : Any> Case<T>.`else`(value: T?) = _else(value)
infix fun <T : Any> Case<T>.end(columnName: String) = end(columnName)
// queriable extensions
val Queriable.count: Long
get() = count()
val Queriable.cursor: Cursor?
get() = query()
val Queriable.hasData: Boolean
get() = hasData()
val Queriable.statement: DatabaseStatement
get() = compileStatement()
val <T : Any> ModelQueriable<T>.list: MutableList<T>
get() = queryList()
val <T : Any> ModelQueriable<T>.result: T?
get() = querySingle()
val <T : Any> ModelQueriable<T>.cursorResult: CursorResult<T>
get() = queryResults()
// async extensions
val <T : Any> ModelQueriable<T>.async: AsyncQuery<T>
get() = async()
infix fun <T : Any> AsyncQuery<T>.list(callback: (QueryTransaction<*>, MutableList<T>?) -> Unit)
= queryListResultCallback { queryTransaction, mutableList -> callback(queryTransaction, mutableList) }
.execute()
infix fun <T : Any> AsyncQuery<T>.result(callback: (QueryTransaction<*>, T?) -> Unit)
= querySingleResultCallback { queryTransaction, model -> callback(queryTransaction, model) }
.execute()
infix fun <T : Any> AsyncQuery<T>.cursorResult(callback: (QueryTransaction<*>, CursorResult<T>) -> Unit)
= queryResultCallback { queryTransaction, cursorResult -> callback(queryTransaction, cursorResult) }
.execute()
val BaseModel.async: AsyncModel<BaseModel>
get() = async()
infix fun <T : Any> AsyncModel<T>.insert(listener: (T) -> Unit) = withListener { listener(it) }.insert()
infix fun <T : Any> AsyncModel<T>.update(listener: (T) -> Unit) = withListener { listener(it) }.update()
infix fun <T : Any> AsyncModel<T>.delete(listener: (T) -> Unit) = withListener { listener(it) }.delete()
infix fun <T : Any> AsyncModel<T>.save(listener: (T) -> Unit) = withListener { listener(it) }.save()
// Transformable methods
infix fun <T : Any> Transformable<T>.groupBy(nameAlias: NameAlias) = groupBy(nameAlias)
infix fun <T : Any> Transformable<T>.groupBy(property: IProperty<*>) = groupBy(property)
infix fun <T : Any> Transformable<T>.orderBy(orderBy: OrderBy) = orderBy(orderBy)
infix fun <T : Any> Transformable<T>.limit(limit: Int) = limit(limit)
infix fun <T : Any> Transformable<T>.offset(offset: Int) = offset(offset)
infix fun <T : Any> Transformable<T>.having(sqlCondition: SQLCondition) = having(sqlCondition)
// join
infix fun <T : Any, V : Any> From<V>.innerJoin(joinTable: KClass<T>) = join(joinTable.java, Join.JoinType.INNER)
infix fun <T : Any, V : Any> From<V>.crossJoin(joinTable: KClass<T>) = join(joinTable.java, Join.JoinType.CROSS)
infix fun <T : Any, V : Any> From<V>.leftOuterJoin(joinTable: KClass<T>) = join(joinTable.java, Join.JoinType.LEFT_OUTER)
infix fun <T : Any, V : Any> Join<T, V>.on(sqlCondition: SQLCondition) = on(sqlCondition)
// update methods
fun <T : Any> update(modelClass: KClass<T>): Update<T> = SQLite.update(modelClass.java)
infix fun <T : Any> Update<T>.set(sqlCondition: SQLCondition) = set(sqlCondition)
// delete
inline fun <reified T : Any> delete() = SQLite.delete(T::class.java)
inline fun <reified T : Any> delete(deleteClause: From<T>.() -> BaseModelQueriable<T>) = deleteClause(SQLite.delete(T::class.java))
// insert methods
fun <T : Any> insert(modelClass: KClass<T>) = SQLite.insert(modelClass.java)
infix fun <T : Any> Insert<T>.orReplace(into: Array<out Pair<IProperty<*>, *>>) = orReplace().into(*into)
infix fun <T : Any> Insert<T>.orRollback(into: Array<out Pair<IProperty<*>, *>>) = orRollback().into(*into)
infix fun <T : Any> Insert<T>.orAbort(into: Array<out Pair<IProperty<*>, *>>) = orAbort().into(*into)
infix fun <T : Any> Insert<T>.orFail(into: Array<out Pair<IProperty<*>, *>>) = orFail().into(*into)
infix fun <T : Any> Insert<T>.orIgnore(into: Array<out Pair<IProperty<*>, *>>) = orIgnore().into(*into)
infix fun <T : Any> Insert<T>.select(from: From<*>) = select(from)
fun into(vararg pairs: Pair<IProperty<*>, *>): Array<out Pair<IProperty<*>, *>> = pairs
fun <T> Insert<T>.into(vararg pairs: Pair<IProperty<*>, *>): Insert<T> {
val columns: MutableList<IProperty<*>> = java.util.ArrayList()
val values = java.util.ArrayList<Any?>()
pairs.forEach {
columns.add(it.first)
values.add(it.second)
}
this.columns(columns).values(values.toArray())
return this
}
// DSL
fun <T> select(vararg property: IProperty<out IProperty<*>>,
init: Select.() -> BaseModelQueriable<T>): BaseModelQueriable<T> {
val select = SQLite.select(*property)
return init(select)
}
fun <T : Any> select(init: Select.() -> BaseModelQueriable<T>):
BaseModelQueriable<T> = init(SQLite.select())
inline fun <reified T : Any> Select.from(fromClause: From<T>.() -> Where<T>):
BaseModelQueriable<T> = fromClause(from(T::class.java))
inline fun <T : Any> From<T>.where(sqlConditionClause: () -> SQLCondition) = where(sqlConditionClause())
inline fun <T : Any> Set<T>.where(sqlConditionClause: () -> SQLCondition) = where(sqlConditionClause())
inline fun <T : Any> Where<T>.and(sqlConditionClause: () -> SQLCondition) = and(sqlConditionClause())
inline fun <T : Any> Where<T>.or(sqlConditionClause: () -> SQLCondition) = or(sqlConditionClause())
inline fun <T : Any, reified TJoin : Any> From<T>.join(joinType: Join.JoinType, function: Join<TJoin, T>.() -> Unit): Where<T> {
function(join(TJoin::class.java, joinType))
return where()
}
inline fun <reified T : Any> insert(insertMethod: Insert<T>.() -> Unit): Insert<T> {
val insert = SQLite.insert(T::class.java)
insertMethod(insert)
return insert
}
inline infix fun <T : Any, TJoin : Any> Join<TJoin, T>.on(conditionFunction: () -> Array<out SQLCondition>) = on(*conditionFunction())
inline fun <reified T : Any> update(setMethod: Update<T>.() -> BaseModelQueriable<T>): BaseModelQueriable<T> {
val update = SQLite.update(T::class.java)
return setMethod(update)
}
inline fun <T : Any> Update<T>.set(setClause: Set<T>.() -> Where<T>):
BaseModelQueriable<T> = setClause(set())