Skip to content

Commit ef2d607

Browse files
committed
added in memory database and table indexes
1 parent a3b65d8 commit ef2d607

23 files changed

+521
-225
lines changed

.idea/gradle.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown-navigator-enh.xml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown-navigator.xml

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ android {
2222
dependencies {
2323
...
2424
implementation 'com.github.android-promise:database:TAG'
25-
implementation 'com.github.android-promise:commons:1.0'
25+
implementation 'com.github.android-promise:commons:1.1-alpha03'
2626
}
2727
```
2828

@@ -31,14 +31,20 @@ Initialize Promise in your main application file, entry point
3131

3232
##### App.java
3333
```java
34-
...
34+
public class App extends Application {
3535
@Override
3636
public void onCreate() {
3737
super.onCreate();
38-
Promise.init(this);
39-
...
38+
// 10 is the number of threads allowed to run in the background
39+
AndroidPromise.init(this, 10, BuildConfig.DEBUG);
4040
}
41-
...
41+
42+
@Override
43+
public void onTerminate() {
44+
super.onTerminate();
45+
AndroidPromise.instance().terminate();
46+
}
47+
}
4248
```
4349

4450
## Making your Record Class and Table
@@ -65,7 +71,19 @@ A table is a functional class that has methods that manipulate records in the da
6571
A sample [ComplexRecordTable class](https://github.com/android-promise/database/blob/master/dbapp/src/main/java/promise/dbapp/model/ComplexRecordTable.kt) will manipulate [ComplexRecord](https://github.com/android-promise/database/blob/master/dbapp/src/main/java/promise/dbapp/model/ComplexRecord.kt) within the database.
6672
```kotlin
6773

68-
@Table(tableName = "name_of_complex_model_table")
74+
@Table(
75+
tableName = "name_of_complex_model_table",
76+
indexes = [
77+
Table.Index(
78+
columnName = "int"
79+
),
80+
Table.Index(
81+
columnName = "double",
82+
unique = true
83+
)
84+
]
85+
)
86+
6987
class ComplexRecordTable(database: FastDatabase) : FastTable<ComplexRecord>(database) {
7088

7189
override fun onUpgrade(database: SQLiteDatabase, v1: Int, v2: Int) {
@@ -158,8 +176,6 @@ If you want to utilize rxJava in your queries, extend from [ReactiveFastDatabase
158176
```kotlin
159177

160178
@Database(
161-
name = "complex_db_name",
162-
version = 1,
163179
tables = [
164180
ComplexRecordTable::class
165181
]
@@ -183,7 +199,10 @@ object AppDatabase {
183199
result.response(true)
184200
}
185201

186-
val instance = createDatabase(AppDatabase::class.java)
202+
// can also use in memory database, no name and no migrations
203+
val inMemoryDatabase = createInMemoryDatabase(AppDatabase::class.java)
204+
205+
val instance = createDatabase(AppDatabase::class.java, "db_name")
187206

188207
val complexModelTable: ComplexRecordTable by lazy {
189208
instance.obtain<ComplexRecordTable>(ComplexRecordTable::class.java)
@@ -194,8 +213,6 @@ object AppDatabase {
194213
The tables are registered in the database annotation
195214
```kotlin
196215
@Database(
197-
name = "complex_db_name",
198-
version = 1,
199216
tables = [
200217
ComplexRecordTable::class
201218
]
@@ -255,7 +272,7 @@ We'll upgrade our database to version 2 and add the table in our database as thi
255272
##### AppDatabase
256273
```kotlin
257274
...
258-
val instance = createDatabase(AppDatabase::class.java,
275+
val instance = createDatabase(AppDatabase::class.java, "db_name",
259276
object : Migration {
260277
override fun onMigrate(database: FastDatabase,
261278
sqLiteDatabase: SQLiteDatabase,
@@ -273,7 +290,6 @@ Amd the update our new table in table registry so that new installs get the new
273290
```kotlin
274291
...
275292
@Database(
276-
name = "complex_db_name",
277293
version = 2,
278294
tables = [
279295
ComplexRecordTable::class,
@@ -357,7 +373,7 @@ watch this repo to stay updated
357373
* Peter Vincent - <dev4vin@gmail.com>
358374
# Donations
359375
If you'd like to support this library development, you could buy me coffee here:
360-
* [![Become a Patreon]("https://c6.patreon.com/becomePatronButton.bundle.js")](https://www.patreon.com/bePatron?u=31165349)
376+
* [![Become a Patreon]("https://c6.patreon.com/becomePatronButton.bundle.js")](https://www.patreon.com/bePatron?u=31932751)
361377

362378
Thank you very much in advance!
363379

dbapp/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
apply plugin: 'com.android.application'
1515
apply plugin: 'kotlin-android'
1616
apply plugin: 'kotlin-android-extensions'
17+
1718
android {
1819
compileSdkVersion 29
1920
buildToolsVersion "29.0.2"
@@ -46,7 +47,7 @@ android {
4647

4748
dependencies {
4849
implementation project(path: ':promisedb')
49-
implementation 'com.github.android-promise:commons:1.0'
50+
implementation 'com.github.android-promise:commons:1.1-alpha03'
5051
implementation "com.android.support:recyclerview-v7:$supportLibVersion"
5152
implementation 'io.reactivex.rxjava2:rxjava:2.2.17'
5253
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

dbapp/src/main/java/promise/dbapp/App.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
package promise.dbapp
1515

1616
import android.app.Application
17-
import promise.commons.Promise
17+
import promise.commons.AndroidPromise
1818

1919
class App : Application() {
2020
override fun onCreate() {
2121
super.onCreate()
22-
Promise.init(this)
22+
AndroidPromise.init(this, BuildConfig.DEBUG)
2323
}
2424
}

dbapp/src/main/java/promise/dbapp/MainActivity.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ package promise.dbapp
1616
import android.os.Bundle
1717
import androidx.appcompat.app.AppCompatActivity
1818
import com.google.android.material.snackbar.Snackbar
19-
import io.reactivex.android.schedulers.AndroidSchedulers
20-
import io.reactivex.schedulers.Schedulers
2119
import kotlinx.android.synthetic.main.activity_main.*
2220
import kotlinx.android.synthetic.main.content_main.*
23-
import promise.commons.model.Result
21+
import promise.commons.tx.PromiseResult
2422
import promise.dbapp.model.ComplexRecord
2523
import promise.dbapp.model.AppDatabase
24+
import promise.dbapp.model.NewRecord
2625
import promise.model.IdentifiableList
2726

2827
class MainActivity : AppCompatActivity() {
@@ -41,24 +40,24 @@ class MainActivity : AppCompatActivity() {
4140
override fun onPostCreate(savedInstanceState: Bundle?) {
4241
super.onPostCreate(savedInstanceState)
4342

44-
AppDatabase.allComplexModels(Result<IdentifiableList<out ComplexRecord>, Throwable>()
45-
.withCallBack {
43+
AppDatabase.allComplexModels(PromiseResult<IdentifiableList<out ComplexRecord>, Throwable>()
44+
.withCallback {
4645
if (it.isNotEmpty()) {
4746
complex_values_textview.text = it.toString()
4847
} else complex_values_textview.text = "empty list"
4948
}
50-
.withErrorCallBack { complex_values_textview.text = it.message })
49+
.withErrorCallback { complex_values_textview.text = it.message })
5150

5251
clear_button.setOnClickListener {
5352
AppDatabase.instance.deleteAll()
5453
complex_values_textview.text = ""
5554
}
5655

57-
val complexRecordTable = AppDatabase.complexModelTable
58-
var items = complexRecordTable.findAll()
56+
val newRecordTable = AppDatabase.newRecordTable
57+
var items = newRecordTable.findAll()
5958
if (items.isEmpty()) {
60-
complexRecordTable.save(IdentifiableList(ComplexRecord.someModels()))
61-
items = complexRecordTable.findAll()
59+
newRecordTable.save(IdentifiableList(NewRecord.someModels()))
60+
items = newRecordTable.findAll()
6261
}
6362
complex_values_textview.text = items.toString()
6463
}

dbapp/src/main/java/promise/dbapp/model/AppDatabase.kt

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,40 @@
1414
package promise.dbapp.model
1515

1616
import android.database.sqlite.SQLiteDatabase
17-
import io.reactivex.android.schedulers.AndroidSchedulers
18-
import io.reactivex.disposables.CompositeDisposable
19-
import io.reactivex.schedulers.Schedulers
20-
import promise.commons.Promise
21-
import promise.commons.data.log.LogUtil
22-
import promise.commons.model.Result
23-
import promise.db.FastDatabase
24-
import promise.db.Migration
17+
import promise.commons.tx.PromiseResult
2518
import promise.db.Database
19+
import promise.db.FastDatabase
2620
import promise.db.FastDatabase.Companion.createDatabase
21+
import promise.db.FastDatabase.Companion.createInMemoryDatabase
22+
import promise.db.Migration
2723
import promise.model.IdentifiableList
2824

2925
@Database(
30-
name = "complex_db_name",
31-
version = 2,
3226
tables = [
3327
ComplexRecordTable::class,
3428
NewRecordTable::class
3529
]
3630
)
3731
object AppDatabase {
3832

39-
fun allComplexModels(result: Result<IdentifiableList<out ComplexRecord>, Throwable>) {
33+
fun allComplexModels(result: PromiseResult<IdentifiableList<out ComplexRecord>, Throwable>) {
4034
val items = complexModelTable.findAll()
41-
if (items.isEmpty()) {
42-
saveSomeComplexModels(Result<Boolean, Throwable>()
43-
.withCallBack {
44-
allComplexModels(result)
45-
})
46-
return
47-
}
48-
result.response(items)
49-
}
35+
if (items.isEmpty()) saveSomeComplexModels(PromiseResult<Boolean, Throwable>()
36+
.withCallback {
37+
allComplexModels(result)
38+
})
39+
else result.response(items)
40+
}
5041

51-
private fun saveSomeComplexModels(result: Result<Boolean, Throwable>) {
42+
private fun saveSomeComplexModels(result: PromiseResult<Boolean, Throwable>) {
5243
complexModelTable.save(IdentifiableList(ComplexRecord.someModels()))
5344
result.response(true)
5445
}
46+
// can also use in memory database, no name and no migrations
47+
val inMemoryDatabase = createInMemoryDatabase(AppDatabase::class.java)
5548

5649
val instance = createDatabase(AppDatabase::class.java,
50+
"db_name",
5751
object : Migration {
5852
override fun onMigrate(database: FastDatabase,
5953
sqLiteDatabase: SQLiteDatabase,

dbapp/src/main/java/promise/dbapp/model/ComplexRecordTable.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@ import promise.db.FastDatabase
2222
import promise.db.FastTable
2323
import promise.db.Table
2424

25-
@Table(tableName = "name_of_complex_model_table")
25+
@Table(
26+
tableName = "name_of_complex_model_table",
27+
indexes = [
28+
Table.Index(
29+
columnName = "int"
30+
),
31+
Table.Index(
32+
columnName = "double",
33+
unique = true
34+
)
35+
]
36+
)
37+
2638
class ComplexRecordTable(database: FastDatabase) : FastTable<ComplexRecord>(database) {
2739

2840
override fun onUpgrade(database: SQLiteDatabase, v1: Int, v2: Int) {

dbapp/src/main/java/promise/dbapp/model/NewRecord.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package promise.dbapp.model
1515

1616
import promise.commons.model.Identifiable
17+
import promise.commons.model.List
1718

1819
// added when version of database is 1
1920
class NewRecord : Identifiable<Int> {
@@ -26,4 +27,15 @@ class NewRecord : Identifiable<Int> {
2627

2728
var toDoName: String? = null
2829
var todoDesc: String? = null
30+
31+
companion object {
32+
33+
fun someModels(): List<NewRecord> = List.fromArray(NewRecord().apply {
34+
toDoName = "toda name 1"
35+
todoDesc = "desc 1"
36+
}, NewRecord().apply {
37+
toDoName = "toda name 2"
38+
todoDesc = "desc 3"
39+
})
40+
}
2941
}

0 commit comments

Comments
 (0)