提交 76a7a5b7 编写于 作者: guolin's avatar guolin

Add kotlin extensions to FluentQuery class for better usage.

上级 bcd050f9
......@@ -71,7 +71,7 @@ public class FluentQuery {
* Declaring to query which columns in table.
*
* <pre>
* LitePalSupport.select(&quot;name&quot;, &quot;age&quot;).find(Person.class);
* LitePal.select(&quot;name&quot;, &quot;age&quot;).find(Person.class);
* </pre>
*
* This will find all rows with name and age columns in Person table.
......@@ -91,7 +91,7 @@ public class FluentQuery {
* Declaring to query which rows in table.
*
* <pre>
* LitePalSupport.where(&quot;name = ? or age &gt; ?&quot;, &quot;Tom&quot;, &quot;14&quot;).find(Person.class);
* LitePal.where(&quot;name = ? or age &gt; ?&quot;, &quot;Tom&quot;, &quot;14&quot;).find(Person.class);
* </pre>
*
* This will find rows which name is Tom or age greater than 14 in Person
......@@ -111,7 +111,7 @@ public class FluentQuery {
* Declaring how to order the rows queried from table.
*
* <pre>
* LitePalSupport.order(&quot;name desc&quot;).find(Person.class);
* LitePal.order(&quot;name desc&quot;).find(Person.class);
* </pre>
*
* This will find all rows in Person table sorted by name with inverted
......@@ -132,7 +132,7 @@ public class FluentQuery {
* Limits the number of rows returned by the query.
*
* <pre>
* LitePalSupport.limit(2).find(Person.class);
* LitePal.limit(2).find(Person.class);
* </pre>
*
* This will find the top 2 rows in Person table.
......@@ -152,7 +152,7 @@ public class FluentQuery {
* used with {@link #limit(int)}, or nothing will return.
*
* <pre>
* LitePalSupport.limit(1).offset(2).find(Person.class);
* LitePal.limit(1).offset(2).find(Person.class);
* </pre>
*
* This will find the third row in Person table.
......@@ -171,7 +171,7 @@ public class FluentQuery {
* way to finish a complicated query:
*
* <pre>
* LitePalSupport.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(1).offset(2)
* LitePal.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(1).offset(2)
* .find(Person.class);
* </pre>
*
......@@ -271,7 +271,7 @@ public class FluentQuery {
* way to finish a complicated query:
*
* <pre>
* LitePalSupport.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(1).offset(2)
* LitePal.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(10).offset(2)
* .findFirst(Person.class);
* </pre>
*
......@@ -357,7 +357,7 @@ public class FluentQuery {
* way to finish a complicated query:
*
* <pre>
* LitePalSupport.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(1).offset(2)
* LitePal.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(10).offset(2)
* .findLast(Person.class);
* </pre>
*
......@@ -443,14 +443,14 @@ public class FluentQuery {
* Count the records.
*
* <pre>
* LitePalSupport.count(Person.class);
* LitePal.count(Person.class);
* </pre>
*
* This will count all rows in person table.<br>
* You can also specify a where clause when counting.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).count(Person.class);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).count(Person.class);
* </pre>
*
* @param modelClass
......@@ -476,14 +476,14 @@ public class FluentQuery {
* Count the records.
*
* <pre>
* LitePalSupport.count(&quot;person&quot;);
* LitePal.count(&quot;person&quot;);
* </pre>
*
* This will count all rows in person table.<br>
* You can also specify a where clause when counting.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).count(&quot;person&quot;);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).count(&quot;person&quot;);
* </pre>
*
* @param tableName
......@@ -530,13 +530,13 @@ public class FluentQuery {
* Calculates the average value on a given column.
*
* <pre>
* LitePalSupport.average(Person.class, &quot;age&quot;);
* LitePal.average(Person.class, &quot;age&quot;);
* </pre>
*
* You can also specify a where clause when calculating.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).average(Person.class, &quot;age&quot;);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).average(Person.class, &quot;age&quot;);
* </pre>
*
* @param modelClass
......@@ -566,13 +566,13 @@ public class FluentQuery {
* Calculates the average value on a given column.
*
* <pre>
* LitePalSupport.average(&quot;person&quot;, &quot;age&quot;);
* LitePal.average(&quot;person&quot;, &quot;age&quot;);
* </pre>
*
* You can also specify a where clause when calculating.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).average(&quot;person&quot;, &quot;age&quot;);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).average(&quot;person&quot;, &quot;age&quot;);
* </pre>
*
* @param tableName
......@@ -624,13 +624,13 @@ public class FluentQuery {
* with the same data type of the column.
*
* <pre>
* LitePalSupport.max(Person.class, &quot;age&quot;, int.class);
* LitePal.max(Person.class, &quot;age&quot;, int.class);
* </pre>
*
* You can also specify a where clause when calculating.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max(Person.class, &quot;age&quot;, Integer.TYPE);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max(Person.class, &quot;age&quot;, Integer.TYPE);
* </pre>
*
* @param modelClass
......@@ -665,13 +665,13 @@ public class FluentQuery {
* with the same data type of the column.
*
* <pre>
* LitePalSupport.max(&quot;person&quot;, &quot;age&quot;, int.class);
* LitePal.max(&quot;person&quot;, &quot;age&quot;, int.class);
* </pre>
*
* You can also specify a where clause when calculating.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max(&quot;person&quot;, &quot;age&quot;, Integer.TYPE);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max(&quot;person&quot;, &quot;age&quot;, Integer.TYPE);
* </pre>
*
* @param tableName
......@@ -727,13 +727,13 @@ public class FluentQuery {
* with the same data type of the column.
*
* <pre>
* LitePalSupport.min(Person.class, &quot;age&quot;, int.class);
* LitePal.min(Person.class, &quot;age&quot;, int.class);
* </pre>
*
* You can also specify a where clause when calculating.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min(Person.class, &quot;age&quot;, Integer.TYPE);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min(Person.class, &quot;age&quot;, Integer.TYPE);
* </pre>
*
* @param modelClass
......@@ -768,13 +768,13 @@ public class FluentQuery {
* with the same data type of the column.
*
* <pre>
* LitePalSupport.min(&quot;person&quot;, &quot;age&quot;, int.class);
* LitePal.min(&quot;person&quot;, &quot;age&quot;, int.class);
* </pre>
*
* You can also specify a where clause when calculating.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min(&quot;person&quot;, &quot;age&quot;, Integer.TYPE);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min(&quot;person&quot;, &quot;age&quot;, Integer.TYPE);
* </pre>
*
* @param tableName
......@@ -830,13 +830,13 @@ public class FluentQuery {
* with the same data type of the column.
*
* <pre>
* LitePalSupport.sum(Person.class, &quot;age&quot;, int.class);
* LitePal.sum(Person.class, &quot;age&quot;, int.class);
* </pre>
*
* You can also specify a where clause when calculating.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum(Person.class, &quot;age&quot;, Integer.TYPE);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum(Person.class, &quot;age&quot;, Integer.TYPE);
* </pre>
*
* @param modelClass
......@@ -871,13 +871,13 @@ public class FluentQuery {
* with the same data type of the column.
*
* <pre>
* LitePalSupport.sum(&quot;person&quot;, &quot;age&quot;, int.class);
* LitePal.sum(&quot;person&quot;, &quot;age&quot;, int.class);
* </pre>
*
* You can also specify a where clause when calculating.
*
* <pre>
* LitePalSupport.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum(&quot;person&quot;, &quot;age&quot;, Integer.TYPE);
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum(&quot;person&quot;, &quot;age&quot;, Integer.TYPE);
* </pre>
*
* @param tableName
......
/*
* Copyright (C) Tony Green, LitePal Framework Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.litepal.extension
import org.litepal.FluentQuery
import org.litepal.LitePal
import org.litepal.crud.async.FindExecutor
/**
* Extension of FluentQuery class for Kotlin api.
* @author Tony Green
* @since 2.1
*/
/**
* Finds multiple records by the cluster parameters. You can use the below
* way to finish a complicated query:
*
* LitePal.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(1).offset(2).find&lt;Person&gt;()
*
* You can also do the same job with SQLiteDatabase like this:
*
* getSQLiteDatabase().query(&quot;Person&quot;, &quot;name&quot;, &quot;age &gt; ?&quot;, new String[] { &quot;14&quot; }, null, null, &quot;age&quot;,
* &quot;2,1&quot;)
*
* Obviously, the first way is much more semantic.<br>
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
* {@link FluentQuery#find(Class, boolean)}.
*
* @return An object list with founded data from database, or an empty list.
*/
inline fun <reified T> FluentQuery.find(): List<T> = find(T::class.java)
/**
* Basically same as {@link #find(Class)} but pending to a new thread for executing.
*
* @return A FindMultiExecutor instance.
*/
inline fun <reified T> FluentQuery.findAsync() = findAsync(T::class.java)
/**
* It is mostly same as {@link FluentQuery#find(Class)} but an isEager
* parameter. If set true the associated models will be loaded as well.
*
* Note that isEager will only work for one deep level relation, considering the query efficiency.
* You have to implement on your own if you need to load multiple deepness of relation at once.
*
* @param isEager
* True to load the associated models, false not.
* @return An object list with founded data from database, or an empty list.
*/
inline fun <reified T> FluentQuery.find(isEager: Boolean): List<T> = find(T::class.java, isEager)
/**
* Basically same as {@link #find(Class, boolean)} but pending to a new thread for executing.
*
* @param isEager
* True to load the associated models, false not.
* @return A FindMultiExecutor instance.
*/
inline fun <reified T> FluentQuery.findAsync(isEager: Boolean) = findAsync(T::class.java, isEager)
/**
* Finds the first record by the cluster parameters. You can use the below
* way to finish a complicated query:
*
* LitePal.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(10).offset(2).findFirst&lt;Person&gt;()
*
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
* {@link FluentQuery#findFirst(Class, boolean)}.
*
* @return An object with founded data from database, or null.
*/
inline fun <reified T> FluentQuery.findFirst(): T? = findFirst(T::class.java)
/**
* Basically same as {@link #findFirst(Class)} but pending to a new thread for executing.
*
* @return A FindExecutor instance.
*/
inline fun <reified T> FluentQuery.findFirstAsync(): FindExecutor<T> = findFirstAsync(T::class.java)
/**
* It is mostly same as {@link FluentQuery#findFirst(Class)} but an isEager
* parameter. If set true the associated models will be loaded as well.
*
* Note that isEager will only work for one deep level relation, considering the query efficiency.
* You have to implement on your own if you need to load multiple deepness of relation at once.
*
* @param isEager
* True to load the associated models, false not.
* @return An object with founded data from database, or null.
*/
inline fun <reified T> FluentQuery.findFirst(isEager: Boolean): T? = findFirst(T::class.java, isEager)
/**
* Basically same as {@link #findFirst(Class, boolean)} but pending to a new thread for executing.
*
* @param isEager
* True to load the associated models, false not.
* @return A FindExecutor instance.
*/
inline fun <reified T> FluentQuery.findFirstAsync(isEager: Boolean): FindExecutor<T> = findFirstAsync(T::class.java, isEager)
/**
* Finds the last record by the cluster parameters. You can use the below
* way to finish a complicated query:
*
* LitePal.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(10).offset(2).findLast&lt;Person&gt;()
*
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
* {@link FluentQuery#findLast(Class, boolean)}.
*
* @return An object with founded data from database, or null.
*/
inline fun <reified T> FluentQuery.findLast(): T? = findLast(T::class.java)
/**
* Basically same as {@link #findLast(Class)} but pending to a new thread for executing.
*
* @return A FindExecutor instance.
*/
inline fun <reified T> FluentQuery.findLastAsync(): FindExecutor<T> = findLastAsync(T::class.java)
/**
* It is mostly same as {@link FluentQuery#findLast(Class)} but an isEager
* parameter. If set true the associated models will be loaded as well.
*
* Note that isEager will only work for one deep level relation, considering the query efficiency.
* You have to implement on your own if you need to load multiple deepness of relation at once.
*
* @param isEager
* True to load the associated models, false not.
* @return An object with founded data from database, or null.
*/
inline fun <reified T> FluentQuery.findLast(isEager: Boolean): T? = findLast(T::class.java, isEager)
/**
* Basically same as {@link #findLast(Class, boolean)} but pending to a new thread for executing.
*
* @param isEager
* True to load the associated models, false not.
* @return A FindExecutor instance.
*/
inline fun <reified T> FluentQuery.findLastAsync(isEager: Boolean): FindExecutor<T> = findLastAsync(T::class.java, isEager)
/**
* Count the records.
*
* LitePal.count&lt;Person&gt;()
*
* This will count all rows in person table.
*
* You can also specify a where clause when counting.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).count&lt;Person&gt;()
*
* @return Count of the specified table.
*/
inline fun <reified T> FluentQuery.count() = count(T::class.java)
/**
* Basically same as [LitePal.count] but pending to a new thread for executing.
*
* @return A CountExecutor instance.
*/
inline fun <reified T> FluentQuery.countAsync() = countAsync(T::class.java)
/**
* Calculates the average value on a given column.
*
* LitePal.average&lt;Person&gt;(&quot;age&quot;)
*
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).average&lt;Person&gt;(&quot;age&quot;)
*
* @param column
* The based on column to calculate.
* @return The average value on a given column.
*/
inline fun <reified T> FluentQuery.average(column: String) = average(T::class.java, column)
/**
* Basically same as [LitePal.average] but pending to a new thread for executing.
*
* @param column
* The based on column to calculate.
* @return A AverageExecutor instance.
*/
inline fun <reified T> FluentQuery.averageAsync(column: String) = averageAsync(T::class.java, column)
/**
* Calculates the maximum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.max&lt;Person, Int&gt;(&quot;age&quot;)
*
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max&lt;Person, Int&gt;(&quot;age&quot;)
*
* @param columnName
* The based on column to calculate.
*
* @return The maximum value on a given column.
*/
inline fun <reified T, reified R> FluentQuery.max(columnName: String): R = max(T::class.java, columnName, R::class.java)
/**
* Basically same as [LitePal.max] but pending to a new thread for executing.
*
* @param columnName
* The based on column to calculate.
* @return A FindExecutor instance.
*/
inline fun <reified T, reified R> FluentQuery.maxAsync(columnName: String) = maxAsync(T::class.java, columnName, R::class.java)
/**
* Calculates the maximum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.max&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* @param tableName
* Which table to query from.
* @param columnName
* The based on column to calculate.
* @return The maximum value on a given column.
*/
inline fun <reified R> FluentQuery.max(tableName: String, columnName: String): R = max(tableName, columnName, R::class.java)
/**
* Basically same as [LitePal.max] but pending to a new thread for executing.
*
* @param tableName
* Which table to query from.
* @param columnName
* The based on column to calculate.
* @return A FindExecutor instance.
*/
inline fun <reified R> FluentQuery.maxAsync(tableName: String, columnName: String) = maxAsync(tableName, columnName, R::class.java)
/**
* Calculates the minimum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.min&lt;Person, Int&gt;(&quot;age&quot;)
*
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min&lt;Person, Int&gt;(&quot;age&quot;)
*
* @param columnName
* The based on column to calculate.
* @return The minimum value on a given column.
*/
inline fun <reified T, reified R> FluentQuery.min(columnName: String): R = min(T::class.java, columnName, R::class.java)
/**
* Basically same as [LitePal.min] but pending to a new thread for executing.
*
* @param columnName
* The based on column to calculate.
* @return A FindExecutor instance.
*/
inline fun <reified T, reified R> FluentQuery.minAsync(columnName: String) = minAsync(T::class.java, columnName, R::class.java)
/**
* Calculates the minimum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.min&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* @param tableName
* Which table to query from.
* @param columnName
* The based on column to calculate.
* @return The minimum value on a given column.
*/
inline fun <reified R> FluentQuery.min(tableName: String, columnName: String): R = min(tableName, columnName, R::class.java)
/**
* Basically same as [LitePal.min] but pending to a new thread for executing.
*
* @param tableName
* Which table to query from.
* @param columnName
* The based on column to calculate.
* @return A FindExecutor instance.
*/
inline fun <reified R> FluentQuery.minAsync(tableName: String, columnName: String) = minAsync(tableName, columnName, R::class.java)
/**
* Calculates the sum of values on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.sum&lt;Person, Int&gt;(&quot;age&quot;)
*
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum&lt;Person, Int&gt;(&quot;age&quot;)
*
* @param columnName
* The based on column to calculate.
* @return The sum value on a given column.
*/
inline fun <reified T, reified R> FluentQuery.sum(columnName: String): R = sum(T::class.java, columnName, R::class.java)
/**
* Basically same as [LitePal.sum] but pending to a new thread for executing.
*
* @param columnName
* The based on column to calculate.
* @return A FindExecutor instance.
*/
inline fun <reified T, reified R> FluentQuery.sumAsync(columnName: String) = sumAsync(T::class.java, columnName, R::class.java)
/**
* Calculates the sum of values on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.sum&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* @param tableName
* Which table to query from.
* @param columnName
* The based on column to calculate.
* @return The sum value on a given column.
*/
inline fun <reified R> FluentQuery.sum(tableName: String, columnName: String): R = sum(tableName, columnName, R::class.java)
/**
* Basically same as [LitePal.sum] but pending to a new thread for executing.
*
* @param tableName
* Which table to query from.
* @param columnName
* The based on column to calculate.
* @return A FindExecutor instance.
*/
inline fun <reified R> FluentQuery.sumAsync(tableName: String, columnName: String) = sumAsync(tableName, columnName, R::class.java)
\ No newline at end of file
......@@ -25,6 +25,52 @@ import org.litepal.LitePal
* @since 2.1
*/
/**
* Count the records.
*
* LitePal.count&lt;Person&gt;()
*
* This will count all rows in person table.
*
* You can also specify a where clause when counting.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).count&lt;Person&gt;()
*
* @return Count of the specified table.
*/
inline fun <reified T> LitePal.count() = count(T::class.java)
/**
* Basically same as [LitePal.count] but pending to a new thread for executing.
*
* @return A CountExecutor instance.
*/
inline fun <reified T> LitePal.countAsync() = countAsync(T::class.java)
/**
* Calculates the average value on a given column.
*
* LitePal.average&lt;Person&gt;(&quot;age&quot;)
*
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).average&lt;Person&gt;(&quot;age&quot;)
*
* @param column
* The based on column to calculate.
* @return The average value on a given column.
*/
inline fun <reified T> LitePal.average(column: String) = average(T::class.java, column)
/**
* Basically same as [LitePal.average] but pending to a new thread for executing.
*
* @param column
* The based on column to calculate.
* @return A AverageExecutor instance.
*/
inline fun <reified T> LitePal.averageAsync(column: String) = averageAsync(T::class.java, column)
/**
* Calculates the maximum value on a given column. The value is returned
* with the same data type of the column.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册