├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── kotlinc.xml ├── markdown-navigator.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── LICENSE.md ├── README-RX1.md ├── README-RX2.md ├── README.md ├── RxDbFlow-Rx1 ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── au │ │ └── com │ │ └── roadhouse │ │ └── rxdbflow │ │ └── rx1 │ │ ├── DBFlowSchedulers.java │ │ ├── RxDbFlow.java │ │ ├── sql │ │ ├── language │ │ │ ├── BaseModelQueriableObservable.java │ │ │ ├── BaseQueriableObservable.java │ │ │ ├── ModelQueriableObservable.java │ │ │ ├── QueriableObservable.java │ │ │ ├── RxDelete.java │ │ │ ├── RxFrom.java │ │ │ ├── RxIndexedBy.java │ │ │ ├── RxInsert.java │ │ │ ├── RxJoin.java │ │ │ ├── RxSQLite.java │ │ │ ├── RxSelect.java │ │ │ ├── RxSet.java │ │ │ ├── RxTransformable.java │ │ │ ├── RxUpdate.java │ │ │ └── RxWhere.java │ │ ├── observables │ │ │ ├── CompleteOnResultOperator.java │ │ │ ├── DBFlowCountObservable.java │ │ │ ├── DBFlowCursorListObservable.java │ │ │ ├── DBFlowCursorObservable.java │ │ │ ├── DBFlowCustomListObservable.java │ │ │ ├── DBFlowCustomModelObservable.java │ │ │ ├── DBFlowExecuteObservable.java │ │ │ ├── DBFlowListObservable.java │ │ │ ├── DBFlowModelObservable.java │ │ │ ├── DBFlowQueryListObservable.java │ │ │ └── DBFlowResultObservable.java │ │ └── transaction │ │ │ ├── RxGenericTransactionBlock.java │ │ │ └── RxModelOperationTransaction.java │ │ └── structure │ │ ├── RxBaseModel.java │ │ ├── RxModelAdapter.java │ │ └── RxModifications.java │ └── res │ └── values │ └── strings.xml ├── RxDbFlow-Rx2 ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── au │ │ └── com │ │ └── roadhouse │ │ └── rxdbflow │ │ └── rx2 │ │ ├── DBFlowSchedulers.java │ │ ├── RxDbFlow.java │ │ ├── sql │ │ ├── language │ │ │ ├── BaseModelQueriableObservable.java │ │ │ ├── BaseQueriableObservable.java │ │ │ ├── ModelQueriableObservable.java │ │ │ ├── NullValue.java │ │ │ ├── QueriableObservable.java │ │ │ ├── RxDelete.java │ │ │ ├── RxFrom.java │ │ │ ├── RxIndexedBy.java │ │ │ ├── RxInsert.java │ │ │ ├── RxJoin.java │ │ │ ├── RxSQLite.java │ │ │ ├── RxSelect.java │ │ │ ├── RxSet.java │ │ │ ├── RxTransformable.java │ │ │ ├── RxUpdate.java │ │ │ └── RxWhere.java │ │ ├── observables │ │ │ ├── DBFlowBaseCompletable.java │ │ │ ├── DBFlowBaseSingle.java │ │ │ ├── DBFlowCompletable.java │ │ │ ├── DBFlowCountSingle.java │ │ │ ├── DBFlowCursorListSingle.java │ │ │ ├── DBFlowCursorSingle.java │ │ │ ├── DBFlowCustomListSingle.java │ │ │ ├── DBFlowCustomModelSingle.java │ │ │ ├── DBFlowExecuteCompletable.java │ │ │ ├── DBFlowListSingle.java │ │ │ ├── DBFlowMaybe.java │ │ │ ├── DBFlowModelMaybe.java │ │ │ ├── DBFlowModelSingle.java │ │ │ ├── DBFlowObservable.java │ │ │ ├── DBFlowQueryListSingle.java │ │ │ ├── DBFlowResultSingle.java │ │ │ ├── DBFlowSingle.java │ │ │ ├── DisposableOwner.java │ │ │ ├── ObserverDisposable.java │ │ │ ├── functions │ │ │ │ └── ValueAction.java │ │ │ └── operators │ │ │ │ ├── DBFlowBaseMaybe.java │ │ │ │ ├── DBFlowRestartOnChange.java │ │ │ │ └── NotifyOfUpdate.java │ │ └── transaction │ │ │ ├── DbTransaction.java │ │ │ ├── RxGenericTransactionBlock.java │ │ │ └── RxModelOperationTransaction.java │ │ └── structure │ │ ├── RxBaseModel.java │ │ ├── RxModelAdapter.java │ │ └── RxModifications.java │ └── res │ └── values │ └── strings.xml ├── build.gradle ├── exampleRx1 ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── au │ │ └── com │ │ └── roadhouse │ │ └── rxdbflow │ │ ├── ModifyingSqlTest.java │ │ └── SelectTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── au │ │ │ └── com │ │ │ └── roadhouse │ │ │ └── rxdbflow │ │ │ └── exampleRx1 │ │ │ ├── ExampleApplication.java │ │ │ └── model │ │ │ ├── ExampleDatabase.java │ │ │ ├── InheritanceTestModel.java │ │ │ └── TestModel.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ └── content_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── au │ └── com │ └── roadhouse │ └── rxdbflow │ └── ExampleUnitTest.java ├── exampleRx2 ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── au │ │ │ └── com │ │ │ └── roadhouse │ │ │ └── rxdbflow │ │ │ └── exampleRx2 │ │ │ ├── ExampleApplication.java │ │ │ ├── MainActivity.java │ │ │ └── model │ │ │ ├── ExampleDatabase.java │ │ │ ├── InheritanceTestModel.java │ │ │ └── TestModel.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ └── content_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── au │ └── com │ └── roadhouse │ └── rxdbflow │ └── ExampleUnitTest.java ├── examplerx1 └── src │ └── main │ └── java │ └── au │ └── com │ └── roadhouse │ └── rxdbflow │ └── examplerx1 │ └── MainActivity.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | vcs.xml 11 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 32 | 33 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | Android > Lint > Correctness 36 | 37 | 38 | Android > Lint > Security 39 | 40 | 41 | Android > Lint > Usability > Icons 42 | 43 | 44 | Java 45 | 46 | 47 | Packaging issuesJava 48 | 49 | 50 | Probable bugsJava 51 | 52 | 53 | 54 | 55 | Android 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 67 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![Release](https://jitpack.io/v/roadhouse-dev/RxDbflow.svg)](https://jitpack.io/#roadhouse-dev/RxDbflow) 3 | 4 | # RxDBFlow 5 | RxJava bindings for DBFlow models, and query language 6 | 7 | # Download 8 | 9 | Add the JitPack repository to your root build file 10 | 11 | ```groovy 12 | buildscript { 13 | repositories { 14 | jcenter() 15 | } 16 | dependencies { 17 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 18 | } 19 | } 20 | 21 | allprojects { 22 | repositories { 23 | ... 24 | maven { url "https://jitpack.io" } 25 | } 26 | } 27 | ``` 28 | 29 | 30 | For RxJava1 add the dependencies: 31 | ```groovy 32 | dependencies { 33 | apt 'com.github.Raizlabs.DBFlow:dbflow-processor:4.0.0-beta7' 34 | compile 'com.github.Raizlabs.DBFlow:dbflow-core:4.0.0-beta7' 35 | compile 'com.github.Raizlabs.DBFlow:dbflow:4.0.0-beta7' 36 | compile 'com.github.roadhouse-dev.RxDbflow:rxdbflow-rx1:' 37 | } 38 | ``` 39 | 40 | or for RxJava2 add the dependencies: 41 | 42 | ```groovy 43 | dependencies { 44 | apt 'com.github.Raizlabs.DBFlow:dbflow-processor:4.0.0-beta7' 45 | compile 'com.github.Raizlabs.DBFlow:dbflow-core:4.0.0-beta7' 46 | compile 'com.github.Raizlabs.DBFlow:dbflow:4.0.0-beta7' 47 | compile 'com.github.roadhouse-dev.RxDbflow:rxdbflow-rx2:' 48 | } 49 | ``` 50 | 51 | At the moment only DBFlow version 4.0.0-beta7 is supported 52 | 53 | # Build 54 | ```bash 55 | $ git clone https://github.com/roadhouse-dev/RxDbflow.git 56 | $ ./gradlew build 57 | ``` 58 | 59 | # DBFlow documentation 60 | For help with DBFlow please take a look at [DBFlow GitHub](https://github.com/Raizlabs/DBFlow) 61 | 62 | # Scheduler 63 | RxDBFlow now comes with it's own scheduler (DBFlowSchedulers.background()), this is a single threaded scheduler that will ensure 64 | all database operations happen from the same background thread. Using a multi-threaded scheduler like Schedulers.io() can cause deadlocks. 65 | 66 | 67 | # Sample usage 68 | There are slight differences between rx1 and rx2 69 | 70 | # Rx1 Sample usage 71 | [View here](README-RX1.md) 72 | 73 | # Rx2 Sample usage 74 | [View here](README-RX2.md) 75 | 76 | 77 | # Bugs and Feedback 78 | For RxDBFlow bugs, questions and discussions please use the [GitHub Issues](https://github.com/roadhouse-dev/RxDbflow/issues). 79 | 80 | For DBFlow specific bugs, questions and discussions please use the [GitHub Issues](https://github.com/Raizlabs/DBFlow/issues). 81 | 82 | # Pull Requests 83 | All pull requests are welcome, however to make the whole process smoother please use the following guides 84 | 85 | * All pull requests should be against the ```develop``` branch 86 | * Code formatting should match the default Android Studio format 87 | * Limit code changes to the scope of what you're implementing 88 | * Provide standard JavaDoc for any public accessible members and classes 89 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | group='com.github.roadhouse-dev' 4 | project.archivesBaseName="rxdbflow-rx1" 5 | 6 | android { 7 | compileSdkVersion 25 8 | buildToolsVersion "25.0.2" 9 | 10 | defaultConfig { 11 | minSdkVersion 14 12 | targetSdkVersion 25 13 | versionCode 10 14 | versionName "4.2.0.1" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | lintOptions { 25 | abortOnError false 26 | } 27 | } 28 | 29 | dependencies { 30 | compile fileTree(dir: 'libs', include: ['*.jar']) 31 | compile 'io.reactivex:rxjava:1.2.1' 32 | compile 'com.android.support:support-annotations:25.3.1' 33 | compile 'com.github.Raizlabs.DBFlow:dbflow:4.0.0-beta7' 34 | testCompile 'junit:junit:4.12' 35 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 36 | exclude group: 'com.android.support', module: 'support-annotations' 37 | }) 38 | } 39 | 40 | task sourcesJar(type: Jar) { 41 | from android.sourceSets.main.java.srcDirs 42 | classifier = 'sources' 43 | } 44 | task javadoc(type: Javadoc) { 45 | source = android.sourceSets.main.java.srcDirs 46 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 47 | failOnError false 48 | } 49 | task javadocJar(type: Jar, dependsOn: javadoc) { 50 | classifier = 'javadoc' 51 | from javadoc.destinationDir 52 | } 53 | 54 | task(type: Zip, "zipTestReport") { 55 | from("${rootDir.absolutePath}/library/build/reports/androidTests/connected/") 56 | archiveName 'test_report_library.zip' 57 | destinationDir file("${rootDir.absolutePath}/library/build/reports/") 58 | } 59 | 60 | configure(zipTestReport) { 61 | group = 'verification' 62 | description = 'Create a test report archive' 63 | } 64 | 65 | task(type: Zip, "zipLintReport") { 66 | from "${rootDir.absolutePath}/library/build/outputs/" 67 | include "lint-results-debug.html" 68 | include "lint-results-debug_files/*" 69 | archiveName 'lint_report_library.zip' 70 | destinationDir file("${rootDir.absolutePath}/library/build/reports/") 71 | } 72 | 73 | configure(zipLintReport) { 74 | group = 'verification' 75 | description = 'Create a lint report archive' 76 | } 77 | 78 | artifacts { 79 | archives sourcesJar 80 | archives javadocJar 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/dwaynehoy/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/DBFlowSchedulers.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1; 2 | 3 | import java.util.concurrent.ArrayBlockingQueue; 4 | import java.util.concurrent.BlockingQueue; 5 | import java.util.concurrent.ThreadPoolExecutor; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | import rx.Scheduler; 9 | import rx.schedulers.Schedulers; 10 | 11 | public class DBFlowSchedulers { 12 | 13 | private static Scheduler sScheduler; 14 | 15 | /** 16 | * A single threaded background scheduler, this should be used for dbflow observable operations 17 | * to avoid slamming the connection pool with multiple connection. 18 | * 19 | * @return A DBFlow background scheduler 20 | */ 21 | public static Scheduler background() { 22 | if (sScheduler == null) { 23 | BlockingQueue threadQueue = new ArrayBlockingQueue<>(200); 24 | ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, threadQueue); 25 | sScheduler = Schedulers.from(threadPoolExecutor); 26 | } 27 | 28 | return sScheduler; 29 | } 30 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/RxDbFlow.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1; 2 | 3 | import com.raizlabs.android.dbflow.config.FlowManager; 4 | 5 | import au.com.roadhouse.rxdbflow.rx1.structure.RxModelAdapter; 6 | 7 | public class RxDbFlow { 8 | 9 | public static RxModelAdapter getModelAdapter(Class modelClass) { 10 | return new RxModelAdapter<>(FlowManager.getModelAdapter(modelClass)); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/BaseModelQueriableObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.list.FlowCursorList; 4 | import com.raizlabs.android.dbflow.list.FlowQueryList; 5 | import com.raizlabs.android.dbflow.sql.language.BaseModelQueriable; 6 | import com.raizlabs.android.dbflow.sql.language.CursorResult; 7 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 8 | import com.raizlabs.android.dbflow.structure.BaseQueryModel; 9 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 10 | 11 | import java.util.List; 12 | 13 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowCursorListObservable; 14 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowCursorObservable; 15 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowCustomListObservable; 16 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowCustomModelObservable; 17 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowListObservable; 18 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowModelObservable; 19 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowQueryListObservable; 20 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowResultObservable; 21 | import rx.Observable; 22 | 23 | 24 | public class BaseModelQueriableObservable extends BaseQueriableObservable implements ModelQueriableObservable { 25 | 26 | private ModelQueriable mRealModelQueriable; 27 | 28 | protected BaseModelQueriableObservable(BaseModelQueriable realModelQueriable) { 29 | super(realModelQueriable); 30 | mRealModelQueriable = realModelQueriable; 31 | } 32 | 33 | /** 34 | * {@inheritDoc} 35 | */ 36 | @Override 37 | public Observable asSingleObservable() { 38 | return new DBFlowModelObservable<>(mRealModelQueriable.getTable(), mRealModelQueriable, null); 39 | } 40 | 41 | /** 42 | * {@inheritDoc} 43 | */ 44 | @Override 45 | public Observable asSingleObservable(DatabaseWrapper databaseWrapper) { 46 | return new DBFlowModelObservable<>(mRealModelQueriable.getTable(), mRealModelQueriable, databaseWrapper); 47 | } 48 | 49 | /** 50 | * {@inheritDoc} 51 | */ 52 | @Override 53 | public DBFlowListObservable asListObservable() { 54 | return new DBFlowListObservable<>(mRealModelQueriable.getTable(), mRealModelQueriable, null); 55 | } 56 | 57 | /** 58 | * {@inheritDoc} 59 | */ 60 | @Override 61 | public DBFlowListObservable asListObservable(DatabaseWrapper databaseWrapper) { 62 | return new DBFlowListObservable<>(mRealModelQueriable.getTable(), mRealModelQueriable, databaseWrapper); 63 | } 64 | 65 | /** 66 | * {@inheritDoc} 67 | */ 68 | @Override 69 | public Observable> asResultsObservable() { 70 | return new DBFlowResultObservable<>(mRealModelQueriable.getTable(), mRealModelQueriable); 71 | } 72 | 73 | /** 74 | * {@inheritDoc} 75 | */ 76 | @Override 77 | public Observable> asQueryListObservable() { 78 | return new DBFlowQueryListObservable<>(mRealModelQueriable.getTable(), mRealModelQueriable); 79 | } 80 | 81 | /** 82 | * {@inheritDoc} 83 | */ 84 | @Override 85 | public Observable> asCursorListObservable() { 86 | return new DBFlowCursorListObservable<>(mRealModelQueriable.getTable(), mRealModelQueriable); 87 | } 88 | 89 | /** 90 | * {@inheritDoc} 91 | */ 92 | @Override 93 | public DBFlowCursorObservable asQueryObservable() { 94 | return new DBFlowCursorObservable(mRealModelQueriable.getTable(), mRealModelQueriable, null); 95 | } 96 | 97 | /** 98 | * {@inheritDoc} 99 | */ 100 | @Override 101 | public Observable asCustomSingleObservable(Class customClazz) { 102 | return new DBFlowCustomModelObservable<>(customClazz, mRealModelQueriable); 103 | } 104 | 105 | /** 106 | * {@inheritDoc} 107 | */ 108 | @Override 109 | public Observable> asCustomListObservable(Class customClazz) { 110 | return new DBFlowCustomListObservable<>(customClazz, mRealModelQueriable); 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/BaseQueriableObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.language.BaseQueriable; 4 | import com.raizlabs.android.dbflow.sql.queriable.Queriable; 5 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 6 | 7 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowCountObservable; 8 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowCursorObservable; 9 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowExecuteObservable; 10 | 11 | public class BaseQueriableObservable implements QueriableObservable { 12 | private BaseQueriable mRealQueriable; 13 | 14 | public BaseQueriableObservable(BaseQueriable queriable) { 15 | mRealQueriable = queriable; 16 | } 17 | 18 | /** 19 | * {@inheritDoc} 20 | */ 21 | @Override 22 | public DBFlowCountObservable asCountObservable() { 23 | return new DBFlowCountObservable<>(mRealQueriable.getTable(), mRealQueriable, null); 24 | } 25 | 26 | /** 27 | * {@inheritDoc} 28 | */ 29 | @Override 30 | public DBFlowCountObservable asCountObservable(DatabaseWrapper databaseWrapper) { 31 | return new DBFlowCountObservable<>(mRealQueriable.getTable(), mRealQueriable, databaseWrapper); 32 | } 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | @Override 38 | public DBFlowExecuteObservable asExecuteObservable() { 39 | return new DBFlowExecuteObservable<>(mRealQueriable.getTable(), mRealQueriable, null); 40 | } 41 | 42 | /** 43 | * {@inheritDoc} 44 | */ 45 | @Override 46 | public DBFlowExecuteObservable asExecuteObservable(DatabaseWrapper databaseWrapper) { 47 | return new DBFlowExecuteObservable<>(mRealQueriable.getTable(), mRealQueriable, databaseWrapper); 48 | } 49 | 50 | /** 51 | * {@inheritDoc} 52 | */ 53 | @Override 54 | public DBFlowCursorObservable asQueryObservable() { 55 | return new DBFlowCursorObservable(mRealQueriable.getTable(), mRealQueriable, null); 56 | } 57 | 58 | public Queriable getRealQueriable() { 59 | return mRealQueriable; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/ModelQueriableObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.list.FlowCursorList; 4 | import com.raizlabs.android.dbflow.list.FlowQueryList; 5 | import com.raizlabs.android.dbflow.sql.language.CursorResult; 6 | import com.raizlabs.android.dbflow.structure.BaseQueryModel; 7 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 8 | 9 | import java.util.List; 10 | 11 | import rx.Observable; 12 | 13 | public interface ModelQueriableObservable extends QueriableObservable { 14 | 15 | /** 16 | * Creates an observable that emits a single model from a query. This will be the first record 17 | * returned by the query. 18 | * @return An observable that emits a single model 19 | */ 20 | Observable asSingleObservable(); 21 | 22 | /** 23 | * Creates an observable that emits a single model from a query. This will be the first record 24 | * returned by the query. 25 | * @param databaseWrapper The database wrapper from which to run the query 26 | * @return An observable that emits a single model 27 | */ 28 | Observable asSingleObservable(DatabaseWrapper databaseWrapper); 29 | 30 | /** 31 | * Creates an observable that emits a list of models from a query. 32 | * @return An observable that emits a list of model 33 | */ 34 | Observable> asListObservable(); 35 | 36 | /** 37 | * Creates an observable that emits a list of models from a query. 38 | * 39 | * @param databaseWrapper The database wrapper from which to run the query 40 | * @return An observable that emits a list of model 41 | */ 42 | Observable> asListObservable(DatabaseWrapper databaseWrapper); 43 | 44 | /** 45 | * Creates an observable that emits the results of a query as a CursorResult. 46 | * 47 | * @return An observable that emits a CursorResult 48 | */ 49 | Observable> asResultsObservable(); 50 | 51 | /** 52 | * Creates an observable that emits the results of a query as a FlowQueryList. 53 | * 54 | * @return An observable that emits a FlowQueryList 55 | */ 56 | Observable> asQueryListObservable(); 57 | 58 | /** 59 | * Creates an observable that emits the results of a query as a FlowCursorList. 60 | * 61 | * @return An observable that emits a FlowCursorList 62 | */ 63 | Observable> asCursorListObservable(); 64 | 65 | /** 66 | * Creates an observable that emits a single custom QueryModel class. This will be the first record 67 | * returned by the query. 68 | * 69 | * @return An observable that emits a single custom QueryModel class. 70 | */ 71 | Observable asCustomSingleObservable(Class customClazz); 72 | 73 | /** 74 | * Creates an observable that emits the results of a query as a list of custom QueryModel objects. 75 | * 76 | * @return An observable that emits a single custom QueryModel class. 77 | */ 78 | Observable> asCustomListObservable(Class customClazz); 79 | } 80 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/QueriableObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import android.database.Cursor; 4 | 5 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 6 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 7 | 8 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowExecuteObservable; 9 | import rx.Observable; 10 | 11 | public interface QueriableObservable { 12 | 13 | /** 14 | * Creates an observable that emits a long value. This should be paired with {@link RxSQLite#selectCountOf(IProperty[])} 15 | * or a query that returns a single long/integer value. Calling this on any other query could result in unexpected return values 16 | * 17 | * @return An observable that emits a long result 18 | */ 19 | Observable asCountObservable(); 20 | 21 | /** 22 | * Creates an observable that emits a long value. This should be paired with {@link RxSQLite#selectCountOf(IProperty[])} 23 | * or a query that returns a single long/integer value. Calling this on any other query could result in an unexpected emitted 24 | * value 25 | * 26 | * @param databaseWrapper The database wrapper to use for the query 27 | * @return An observable that emits a long result 28 | */ 29 | Observable asCountObservable(DatabaseWrapper databaseWrapper); 30 | 31 | /** 32 | * Creates an observable that executes a SQL statement. This is usually used with CRUD statements as 33 | * it only emits a Void value 34 | * 35 | * @return An observable that executes a SQL statement 36 | */ 37 | DBFlowExecuteObservable asExecuteObservable(); 38 | 39 | /** 40 | * Creates an observable that executes a SQL statement. This is usually used with CRUD statements as 41 | * it only emits a Void value 42 | * 43 | * @param databaseWrapper The database wrapper to use for the query 44 | * @return An observable that executes a SQL statement 45 | */ 46 | Observable asExecuteObservable(DatabaseWrapper databaseWrapper); 47 | 48 | /** 49 | * Creates an observable that emits a cursor containing the results of the query 50 | * @return An observable that emits a cursor 51 | */ 52 | Observable asQueryObservable(); 53 | } 54 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxDelete.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.language.Delete; 4 | 5 | /** 6 | * Creates a delete SQL statement 7 | */ 8 | public class RxDelete { 9 | private Delete mRealDelete; 10 | 11 | RxDelete(){ 12 | mRealDelete = new Delete(); 13 | } 14 | 15 | /** 16 | * Constructs a from clause for the delete statement 17 | * @param table The table to delete from 18 | * @return A RxFrom instance 19 | */ 20 | public RxFrom from(Class table){ 21 | //noinspection unchecked 22 | return new RxFrom(mRealDelete.from(table)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxIndexedBy.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.Query; 4 | import com.raizlabs.android.dbflow.sql.language.IndexedBy; 5 | import com.raizlabs.android.dbflow.sql.language.NameAlias; 6 | import com.raizlabs.android.dbflow.sql.language.OrderBy; 7 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 8 | import com.raizlabs.android.dbflow.sql.language.WhereBase; 9 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 10 | import com.raizlabs.android.dbflow.structure.BaseModel; 11 | 12 | /** 13 | * Constructs an Index By clause for a SQL query 14 | */ 15 | public class RxIndexedBy implements WhereBase, RxTransformable { 16 | 17 | private IndexedBy mRealIndexedBy; 18 | 19 | RxIndexedBy(IndexedBy realIndexedBy) { 20 | mRealIndexedBy = realIndexedBy; 21 | } 22 | 23 | /** 24 | * {@inheritDoc} 25 | */ 26 | @Override 27 | public RxWhere groupBy(NameAlias... nameAliases) { 28 | return new RxWhere<>(mRealIndexedBy.groupBy(nameAliases)); 29 | } 30 | 31 | /** 32 | * {@inheritDoc} 33 | */ 34 | @Override 35 | public RxWhere groupBy(IProperty... properties) { 36 | return new RxWhere<>(mRealIndexedBy.groupBy(properties)); 37 | } 38 | 39 | /** 40 | * {@inheritDoc} 41 | */ 42 | @Override 43 | public RxWhere orderBy(NameAlias nameAlias, boolean ascending) { 44 | return new RxWhere<>(mRealIndexedBy.orderBy(nameAlias, ascending)); 45 | } 46 | 47 | /** 48 | * {@inheritDoc} 49 | */ 50 | @Override 51 | public RxWhere orderBy(IProperty property, boolean ascending) { 52 | return new RxWhere<>(mRealIndexedBy.orderBy(property, ascending)); 53 | } 54 | 55 | /** 56 | * {@inheritDoc} 57 | */ 58 | @Override 59 | public RxWhere orderBy(OrderBy orderBy) { 60 | return new RxWhere<>(mRealIndexedBy.orderBy(orderBy)); 61 | } 62 | 63 | /** 64 | * {@inheritDoc} 65 | */ 66 | @Override 67 | public RxWhere limit(int count) { 68 | return new RxWhere<>(mRealIndexedBy.limit(count)); 69 | } 70 | 71 | /** 72 | * {@inheritDoc} 73 | */ 74 | @Override 75 | public RxWhere offset(int offset) { 76 | return new RxWhere<>(mRealIndexedBy.offset(offset)); 77 | } 78 | 79 | /** 80 | * {@inheritDoc} 81 | */ 82 | @Override 83 | public RxWhere having(SQLOperator... conditions) { 84 | return new RxWhere<>(mRealIndexedBy.having(conditions)); 85 | } 86 | 87 | /** 88 | * {@inheritDoc} 89 | */ 90 | @Override 91 | public Class getTable() { 92 | return mRealIndexedBy.getTable(); 93 | } 94 | 95 | /** 96 | * {@inheritDoc} 97 | */ 98 | @Override 99 | public Query getQueryBuilderBase() { 100 | return mRealIndexedBy.getQueryBuilderBase(); 101 | } 102 | 103 | /** 104 | * {@inheritDoc} 105 | */ 106 | @Override 107 | public String getQuery() { 108 | return mRealIndexedBy.getQuery(); 109 | } 110 | 111 | @Override 112 | public BaseModel.Action getPrimaryAction() { 113 | return mRealIndexedBy.getPrimaryAction(); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxJoin.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.Query; 4 | import com.raizlabs.android.dbflow.sql.language.Join; 5 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 6 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 7 | 8 | /** 9 | * Constructs a join clause for a SQL statement 10 | * @param The table model class to join to 11 | * @param The table model class being joined to 12 | */ 13 | public class RxJoin implements Query { 14 | 15 | private Join mRealJoin; 16 | private RxFrom mRxFrom; 17 | 18 | 19 | RxJoin(RxFrom from, Join join){ 20 | mRxFrom = from; 21 | mRealJoin = join; 22 | } 23 | 24 | @Override 25 | public String getQuery() { 26 | return mRealJoin.getQuery(); 27 | } 28 | 29 | 30 | /** 31 | * Creates a "on" clause when joining two tables 32 | * @param onConditions The conditions to use when joining the tables 33 | * @return A RxFrom clause 34 | */ 35 | public RxFrom on(SQLOperator... onConditions){ 36 | mRealJoin.on(onConditions); 37 | return mRxFrom; 38 | } 39 | 40 | /** 41 | * Creates a using clause when joining two tables 42 | * @param columns The columns to use when joining 43 | * @return A RxFrom instance 44 | */ 45 | public RxFrom using(IProperty... columns) { 46 | mRealJoin.using(columns); 47 | return mRxFrom; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxSQLite.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.raizlabs.android.dbflow.sql.language.Case; 6 | import com.raizlabs.android.dbflow.sql.language.CaseCondition; 7 | import com.raizlabs.android.dbflow.sql.language.From; 8 | import com.raizlabs.android.dbflow.sql.language.Index; 9 | import com.raizlabs.android.dbflow.sql.language.Method; 10 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 11 | import com.raizlabs.android.dbflow.sql.language.SQLite; 12 | import com.raizlabs.android.dbflow.sql.language.Trigger; 13 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 14 | import com.raizlabs.android.dbflow.sql.language.property.Property; 15 | import com.raizlabs.android.dbflow.structure.Model; 16 | 17 | /** 18 | * Provides an entry point into all RxSqlite queries 19 | */ 20 | public class RxSQLite { 21 | /** 22 | * @param properties The properties/columns to SELECT. 23 | * @return A beginning of the SELECT statement. 24 | */ 25 | public static RxSelect select(IProperty... properties) { 26 | return new RxSelect(properties); 27 | } 28 | 29 | /** 30 | * Starts a new SELECT COUNT(property1, property2, propertyn) (if properties specified) or 31 | * SELECT COUNT(*). 32 | * 33 | * @param properties Optional, if specified returns the count of non-null ROWs from a specific single/group of columns. 34 | * @return A new select statement SELECT COUNT(expression) 35 | */ 36 | public static RxSelect selectCountOf(IProperty... properties) { 37 | return new RxSelect(Method.count(properties)); 38 | } 39 | 40 | /** 41 | * @param table The tablet to update. 42 | * @param The class that implements {@link Model}. 43 | * @return A new UPDATE statement. 44 | */ 45 | public static RxUpdate update(Class table) { 46 | return new RxUpdate<>(table); 47 | } 48 | 49 | /** 50 | * @param table The table to insert. 51 | * @param The class that implements {@link Model}. 52 | * @return A new INSERT statement. 53 | */ 54 | public static RxInsert insert(Class table) { 55 | return new RxInsert<>(table); 56 | } 57 | 58 | /** 59 | * @return Begins a DELETE statement. 60 | */ 61 | public static RxDelete delete() { 62 | return new RxDelete(); 63 | } 64 | 65 | /** 66 | * Starts a DELETE statement on the specified table. 67 | * 68 | * @param table The table to delete from. 69 | * @param The class that implements {@link Model}. 70 | * @return A {@link From} with specified DELETE on table. 71 | */ 72 | @SuppressWarnings("unchecked") 73 | public static RxFrom delete(Class table) { 74 | return delete().from(table); 75 | } 76 | 77 | /** 78 | * Starts an INDEX statement on specified table. 79 | * 80 | * @param name The name of the index. 81 | * @param The class that implements {@link Model}. 82 | * @return A new INDEX statement. 83 | */ 84 | public static Index index(String name) { 85 | return new Index<>(name); 86 | } 87 | 88 | /** 89 | * Starts a TRIGGER statement. 90 | * 91 | * @param name The name of the trigger. 92 | * @return A new TRIGGER statement. 93 | */ 94 | public static Trigger createTrigger(String name) { 95 | return Trigger.create(name); 96 | } 97 | 98 | /** 99 | * Starts a CASE statement. 100 | * 101 | * @param condition The condition to check for in the WHEN. 102 | * @return A new {@link CaseCondition}. 103 | */ 104 | //TODO: Wrap Case 105 | public static CaseCondition caseWhen(@NonNull SQLOperator condition) { 106 | return SQLite.caseWhen(condition); 107 | } 108 | 109 | /** 110 | * Starts an efficient CASE statement. The value passed here is only evaulated once. A non-efficient 111 | * case statement will evaluate all of its {@link SQLOperator}. 112 | * 113 | * @param caseColumn The value 114 | * @param 115 | * @return 116 | */ 117 | public static Case _case(Property caseColumn) { 118 | return SQLite._case(caseColumn); 119 | } 120 | 121 | /** 122 | * Starts an efficient CASE statement. The value passed here is only evaulated once. A non-efficient 123 | * case statement will evaluate all of its {@link SQLOperator}. 124 | * 125 | * @param caseColumn The value 126 | * @param 127 | * @return 128 | */ 129 | public static Case _case(IProperty caseColumn) { 130 | return SQLite._case(caseColumn); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxSelect.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.Query; 4 | import com.raizlabs.android.dbflow.sql.language.Select; 5 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 6 | 7 | /** 8 | * Creates a select SQL query 9 | */ 10 | public class RxSelect implements Query{ 11 | 12 | private Select mRealSelect; 13 | 14 | RxSelect(IProperty... properties){ 15 | mRealSelect = new Select(properties); 16 | } 17 | 18 | /** 19 | * Constructs a from clause for the SQL query 20 | * @param table The table model class to run the select statement against 21 | * @return A RxFrom instance 22 | */ 23 | public RxFrom from(Class table) { 24 | return new RxFrom<>(mRealSelect.from(table)); 25 | } 26 | 27 | /** 28 | * Constructs a distinct clause for the SQL query 29 | * @return The RxSelect isntance 30 | */ 31 | public RxSelect distinct() { 32 | mRealSelect = mRealSelect.distinct(); 33 | return this; 34 | } 35 | 36 | @Override 37 | public String getQuery() { 38 | return mRealSelect.getQuery(); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return getQuery(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxSet.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.support.annotation.NonNull; 6 | 7 | import com.raizlabs.android.dbflow.sql.Query; 8 | import com.raizlabs.android.dbflow.sql.language.NameAlias; 9 | import com.raizlabs.android.dbflow.sql.language.OrderBy; 10 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 11 | import com.raizlabs.android.dbflow.sql.language.Set; 12 | import com.raizlabs.android.dbflow.sql.language.Where; 13 | import com.raizlabs.android.dbflow.sql.language.WhereBase; 14 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 15 | import com.raizlabs.android.dbflow.structure.BaseModel; 16 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 17 | 18 | import au.com.roadhouse.rxdbflow.rx1.sql.observables.DBFlowExecuteObservable; 19 | import rx.Observable; 20 | 21 | /** 22 | * Provides a set block for a select statement 23 | */ 24 | public class RxSet implements WhereBase, QueriableObservable, RxTransformable { 25 | 26 | private Set mRealSet; 27 | 28 | RxSet(Set realSet) { 29 | mRealSet = realSet; 30 | } 31 | 32 | /** 33 | * Specifies a varg of conditions to append to this SET 34 | * 35 | * @param conditions The varg of conditions 36 | * @return This instance. 37 | */ 38 | public RxSet conditions(SQLOperator... conditions) { 39 | mRealSet.conditions(conditions); 40 | return this; 41 | } 42 | 43 | /** 44 | * Specifies a set of content values to append to this SET as Conditions 45 | * 46 | * @param contentValues The set of values to append. 47 | * @return This instance. 48 | */ 49 | public RxSet conditionValues(ContentValues contentValues) { 50 | mRealSet.conditionValues(contentValues); 51 | return this; 52 | } 53 | 54 | /** 55 | * Begins completing the rest of this SET statement. 56 | * 57 | * @param conditions The conditions to fill the WHERE with. 58 | * @return The where piece of this query. 59 | */ 60 | public RxWhere where(SQLOperator... conditions) { 61 | Where where = mRealSet.where(conditions); 62 | return new RxWhere<>(where); 63 | } 64 | 65 | @Override 66 | public RxWhere groupBy(NameAlias... nameAliases) { 67 | Where where = mRealSet.groupBy(nameAliases); 68 | return new RxWhere<>(where); 69 | } 70 | 71 | @Override 72 | public RxWhere groupBy(IProperty... properties) { 73 | Where where = mRealSet.groupBy(properties); 74 | return new RxWhere<>(where); 75 | } 76 | 77 | @Override 78 | public RxWhere orderBy(NameAlias nameAlias, boolean ascending) { 79 | Where where = mRealSet.orderBy(nameAlias, ascending); 80 | return new RxWhere<>(where); 81 | } 82 | 83 | @Override 84 | public RxWhere orderBy(IProperty property, boolean ascending) { 85 | Where where = mRealSet.orderBy(property, ascending); 86 | return new RxWhere<>(where); 87 | } 88 | 89 | @Override 90 | public RxWhere orderBy(OrderBy orderBy) { 91 | Where where = mRealSet.orderBy(orderBy); 92 | return new RxWhere<>(where); 93 | } 94 | 95 | @Override 96 | public RxWhere limit(int count) { 97 | Where where = mRealSet.limit(count); 98 | return new RxWhere<>(where); 99 | } 100 | 101 | @Override 102 | public RxWhere offset(int offset) { 103 | Where where = mRealSet.offset(offset); 104 | return new RxWhere<>(where); 105 | } 106 | 107 | 108 | @Override 109 | public RxWhere having(SQLOperator... conditions) { 110 | Where where = mRealSet.having(conditions); 111 | return new RxWhere<>(where); 112 | } 113 | 114 | 115 | @Override 116 | public String getQuery() { 117 | return mRealSet.getQuery(); 118 | } 119 | 120 | @NonNull 121 | @Override 122 | public Class getTable() { 123 | return mRealSet.getTable(); 124 | } 125 | 126 | @NonNull 127 | @Override 128 | public Query getQueryBuilderBase() { 129 | return mRealSet.getQueryBuilderBase(); 130 | } 131 | 132 | 133 | @Override 134 | public Observable asCountObservable() { 135 | return where().asCountObservable(); 136 | } 137 | 138 | @Override 139 | public Observable asCountObservable(DatabaseWrapper databaseWrapper) { 140 | return where().asCountObservable(databaseWrapper); 141 | } 142 | 143 | @Override 144 | public DBFlowExecuteObservable asExecuteObservable() { 145 | return where().asExecuteObservable(); 146 | } 147 | 148 | @Override 149 | public Observable asExecuteObservable(DatabaseWrapper databaseWrapper) { 150 | return where().asExecuteObservable(databaseWrapper); 151 | } 152 | 153 | @Override 154 | public Observable asQueryObservable() { 155 | return where().asQueryObservable(); 156 | } 157 | 158 | @Override 159 | public BaseModel.Action getPrimaryAction() { 160 | return mRealSet.getPrimaryAction(); 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxTransformable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.language.NameAlias; 4 | import com.raizlabs.android.dbflow.sql.language.OrderBy; 5 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 6 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 7 | 8 | /** 9 | * An interface for all SQL query classes that transform RxSQLite query results 10 | * 11 | * @param The table model class that the sqlite query will emit 12 | */ 13 | public interface RxTransformable { 14 | 15 | /** 16 | * Constructs a group by clause 17 | * @param nameAliases The fields to group the query by 18 | * @return An instance of RxWhere 19 | */ 20 | RxWhere groupBy(NameAlias... nameAliases); 21 | 22 | /** 23 | * Constructs a group by clause 24 | * @param properties The fields to group the query by 25 | * @return An instance of RxWhere 26 | */ 27 | RxWhere groupBy(IProperty... properties); 28 | 29 | /** 30 | * Constructs an order by clause 31 | * @param nameAlias The field to use for ordering 32 | * @param ascending True to sort ascending, false otherwise 33 | * @return An instance of RxWhere 34 | */ 35 | RxWhere orderBy(NameAlias nameAlias, boolean ascending); 36 | 37 | /** 38 | * Constructs an order by clause 39 | * @param property The field to use for ordering 40 | * @param ascending True to sort ascending, false otherwise 41 | * @return An instance of RxWhere 42 | */ 43 | RxWhere orderBy(IProperty property, boolean ascending); 44 | 45 | /** 46 | * Adds an order by clause to the current query 47 | * @param orderBy The order by clause to use 48 | * @return An instance of RxWhere 49 | */ 50 | RxWhere orderBy(OrderBy orderBy); 51 | 52 | /** 53 | * Constructs a limit clause, limiting the number or returned results 54 | * @param count The number of records to limit to query to 55 | * @return An instance of RxWhere 56 | */ 57 | RxWhere limit(int count); 58 | 59 | /** 60 | * Constructs an offset clause 61 | * @param offset The number of records to offset the results by 62 | * @return An instance of RxWhere 63 | */ 64 | RxWhere offset(int offset); 65 | 66 | /** 67 | * Constructs a having clause 68 | * @param conditions The conditions contained in the having clause 69 | * @return An instance of RxWhere 70 | */ 71 | RxWhere having(SQLOperator... conditions); 72 | } 73 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxUpdate.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.annotation.ConflictAction; 4 | import com.raizlabs.android.dbflow.sql.Query; 5 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 6 | import com.raizlabs.android.dbflow.sql.language.Set; 7 | import com.raizlabs.android.dbflow.sql.language.Update; 8 | 9 | /** 10 | * Creates an update SQL query 11 | */ 12 | public class RxUpdate implements Query { 13 | 14 | private final Update mRealUpdate; 15 | 16 | RxUpdate(Class clazz){ 17 | mRealUpdate = new Update<>(clazz); 18 | } 19 | 20 | @Override 21 | public String getQuery() { 22 | return mRealUpdate.getQuery(); 23 | } 24 | 25 | /** 26 | * Adds a conflict action to the update statement 27 | * @param conflictAction The action to take on conflict 28 | * @return An RxUpdate instance 29 | */ 30 | public RxUpdate conflictAction(ConflictAction conflictAction) { 31 | mRealUpdate.conflictAction(conflictAction); 32 | return this; 33 | } 34 | 35 | /** 36 | * Adds a rollback action on conflict 37 | * @return This instance. 38 | * @see ConflictAction#ROLLBACK 39 | */ 40 | public RxUpdate orRollback() { 41 | return conflictAction(ConflictAction.ROLLBACK); 42 | } 43 | 44 | /** 45 | * Adds an abort action on conflict 46 | * @return This instance. 47 | * @see ConflictAction#ABORT 48 | */ 49 | public RxUpdate orAbort() { 50 | return conflictAction(ConflictAction.ABORT); 51 | } 52 | 53 | /** 54 | * Adds a replace action on conflict 55 | * @return This instance. 56 | * @see ConflictAction#REPLACE 57 | */ 58 | public RxUpdate orReplace() { 59 | return conflictAction(ConflictAction.REPLACE); 60 | } 61 | 62 | /** 63 | * Adds a fail action on conflict 64 | * @return This instance. 65 | * @see ConflictAction#FAIL 66 | */ 67 | public RxUpdate orFail() { 68 | return conflictAction(ConflictAction.FAIL); 69 | } 70 | 71 | /** 72 | * Adds an ignore action on conflict 73 | * @return This instance. 74 | * @see ConflictAction#IGNORE 75 | */ 76 | public RxUpdate orIgnore() { 77 | return conflictAction(ConflictAction.IGNORE); 78 | } 79 | 80 | /** 81 | * Begins a SET block for the SQL query 82 | * 83 | * @param conditions The array of conditions that define this SET statement 84 | * @return A SET query piece of this statement 85 | */ 86 | public RxSet set(SQLOperator... conditions) { 87 | Set set = mRealUpdate.set(conditions); 88 | return new RxSet<>(set); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/language/RxWhere.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.Query; 4 | import com.raizlabs.android.dbflow.sql.language.NameAlias; 5 | import com.raizlabs.android.dbflow.sql.language.OrderBy; 6 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 7 | import com.raizlabs.android.dbflow.sql.language.Where; 8 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 9 | 10 | /** 11 | * Defines the where clause of a SQL query 12 | */ 13 | public class RxWhere extends BaseModelQueriableObservable 14 | implements Query, RxTransformable { 15 | 16 | Where mRealWhere; 17 | 18 | RxWhere(Where where) { 19 | super(where); 20 | mRealWhere = where; 21 | } 22 | 23 | /** 24 | * Adds a param to the WHERE clause with the custom {@link SQLOperator} 25 | * 26 | * @param condition The {@link SQLOperator} to use 27 | * @return An instance of RxWhere 28 | */ 29 | public RxWhere and(SQLOperator condition){ 30 | mRealWhere.and(condition); 31 | 32 | return this; 33 | } 34 | 35 | /** 36 | * Constructs a where clause which contains all of the passed in conditions 37 | * 38 | * @param conditions The conditions to add to the where clause 39 | * @return An instance of RxWhere 40 | */ 41 | public RxWhere andAll(SQLOperator[] conditions) { 42 | mRealWhere.andAll(conditions); 43 | return this; 44 | } 45 | 46 | @Override 47 | public String getQuery() { 48 | return mRealWhere.getQuery(); 49 | } 50 | 51 | /** 52 | * {@inheritDoc} 53 | */ 54 | @Override 55 | public RxWhere groupBy(NameAlias... nameAliases) { 56 | mRealWhere.groupBy(nameAliases); 57 | return this; 58 | } 59 | 60 | /** 61 | * {@inheritDoc} 62 | */ 63 | @Override 64 | public RxWhere groupBy(IProperty... properties) { 65 | mRealWhere.groupBy(properties); 66 | return this; 67 | } 68 | 69 | /** 70 | * {@inheritDoc} 71 | */ 72 | @Override 73 | public RxWhere orderBy(NameAlias nameAlias, boolean ascending) { 74 | mRealWhere.orderBy(nameAlias, ascending); 75 | return this; 76 | } 77 | 78 | /** 79 | * {@inheritDoc} 80 | */ 81 | @Override 82 | public RxWhere orderBy(IProperty property, boolean ascending) { 83 | mRealWhere.orderBy(property, ascending); 84 | return this; 85 | } 86 | 87 | /** 88 | * {@inheritDoc} 89 | */ 90 | @Override 91 | public RxWhere orderBy(OrderBy orderBy) { 92 | mRealWhere.orderBy(orderBy); 93 | return this; 94 | } 95 | 96 | /** 97 | * {@inheritDoc} 98 | */ 99 | @Override 100 | public RxWhere limit(int count) { 101 | mRealWhere.limit(count); 102 | return this; 103 | } 104 | 105 | /** 106 | * {@inheritDoc} 107 | */ 108 | @Override 109 | public RxWhere offset(int offset) { 110 | mRealWhere.offset(offset); 111 | return this; 112 | } 113 | 114 | /** 115 | * {@inheritDoc} 116 | */ 117 | @Override 118 | public RxWhere having(SQLOperator... conditions) { 119 | mRealWhere.having(conditions); 120 | return this; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/observables/CompleteOnResultOperator.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.observables; 2 | 3 | import rx.Observable; 4 | import rx.Subscriber; 5 | 6 | /** 7 | * An observable operator which calls onComplete on it's subscribers 8 | */ 9 | 10 | public class CompleteOnResultOperator implements Observable.Operator { 11 | 12 | @Override 13 | public Subscriber call(final Subscriber subscriber) { 14 | return new Subscriber() { 15 | @Override 16 | public void onCompleted() { 17 | subscriber.onCompleted(); 18 | } 19 | 20 | @Override 21 | public void onError(Throwable e) { 22 | subscriber.onError(e); 23 | } 24 | 25 | @Override 26 | public void onNext(T aLong) { 27 | subscriber.onNext(aLong); 28 | subscriber.onCompleted(); 29 | } 30 | }; 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/observables/DBFlowCustomModelObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.observables; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.annotation.Nullable; 5 | 6 | import com.raizlabs.android.dbflow.config.FlowManager; 7 | import com.raizlabs.android.dbflow.runtime.FlowContentObserver; 8 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 9 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 10 | import com.raizlabs.android.dbflow.structure.BaseModel; 11 | import com.raizlabs.android.dbflow.structure.BaseQueryModel; 12 | import com.raizlabs.android.dbflow.structure.Model; 13 | 14 | import java.util.ArrayList; 15 | import java.util.Collections; 16 | import java.util.List; 17 | 18 | import rx.Observable; 19 | import rx.Subscriber; 20 | 21 | /** 22 | * Given a RxSQLite query, emits the the first element from the query as a custom model. 23 | */ 24 | public class DBFlowCustomModelObservable extends Observable { 25 | 26 | private final Class mModelClazz; 27 | private final ModelQueriable mBaseModelQueriable; 28 | private List> mSubscribedClasses; 29 | 30 | /** 31 | * Creates a new observable which runs a query and emits the first element in the result set as a CustomModel 32 | * @param clazz The table/view model in which the FlowCursorList will contain 33 | * @param baseModelQueriable The query to run 34 | */ 35 | public DBFlowCustomModelObservable(Class clazz, ModelQueriable baseModelQueriable) { 36 | super(new OnDBFlowSubscribeWithChanges<>(clazz, baseModelQueriable)); 37 | mModelClazz = clazz; 38 | mBaseModelQueriable = baseModelQueriable; 39 | mSubscribedClasses = new ArrayList<>(); 40 | } 41 | 42 | /** 43 | * Observes changes on the current table, restarts the query on change, and emits the updated 44 | * query result to any subscribers 45 | * @param tableToListen The tables to observe for changes 46 | * @return An observable which observes any changes in the specified tables 47 | */ 48 | @SafeVarargs 49 | public final Observable restartOnChange(Class... tableToListen){ 50 | Collections.addAll(mSubscribedClasses, tableToListen); 51 | return lift(new DBFlowOnChangeOperator()); 52 | } 53 | 54 | /** 55 | * Forces onComplete to be called upon returning with a result, therefore automatically 56 | * unsubscribing the subscription. This should be used when you're only interested in a 57 | * single result i.e. not using {@link #restartOnChange(Class[])}. 58 | * If this is not used, the subscriber will be responsible for unsubscribing 59 | * @return An observable which will call onComplete once the result has returned. 60 | */ 61 | public Observable completeOnResult(){ 62 | return lift(new CompleteOnResultOperator()); 63 | } 64 | 65 | private static class OnDBFlowSubscribeWithChanges implements OnSubscribe { 66 | 67 | private final Class mClazz; 68 | private final ModelQueriable mBaseModelQueriable; 69 | 70 | OnDBFlowSubscribeWithChanges(Class clazz, ModelQueriable baseModelQueriable) { 71 | mBaseModelQueriable = baseModelQueriable; 72 | mClazz = clazz; 73 | } 74 | 75 | 76 | @Override 77 | public void call(final Subscriber subscriber) { 78 | subscriber.onNext(mBaseModelQueriable.queryCustomSingle(mClazz)); 79 | } 80 | } 81 | 82 | private class DBFlowOnChangeOperator implements Operator { 83 | 84 | private FlowContentObserver mFlowContentObserver; 85 | 86 | public DBFlowOnChangeOperator() { 87 | mFlowContentObserver = new FlowContentObserver(); 88 | } 89 | 90 | @Override 91 | public Subscriber call(final Subscriber subscriber) { 92 | return new Subscriber() { 93 | @Override 94 | public void onCompleted() { 95 | 96 | } 97 | 98 | @Override 99 | public void onError(Throwable e) { 100 | 101 | } 102 | 103 | @Override 104 | public void onNext(TQueryModel tModels) { 105 | for (int i = 0; i < mSubscribedClasses.size(); i++) { 106 | mFlowContentObserver.registerForContentChanges(FlowManager.getContext(), mSubscribedClasses.get(i)); 107 | } 108 | mFlowContentObserver.addModelChangeListener(new FlowContentObserver.OnModelStateChangedListener() { 109 | @Override 110 | public void onModelStateChanged(@Nullable Class table, BaseModel.Action action, @NonNull SQLOperator[] primaryKeyValues) { 111 | if (subscriber.isUnsubscribed()) { 112 | mFlowContentObserver.unregisterForContentChanges(FlowManager.getContext()); 113 | } else { 114 | subscriber.onNext(mBaseModelQueriable.queryCustomSingle(mModelClazz)); 115 | } 116 | } 117 | }); 118 | } 119 | }; 120 | 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/observables/DBFlowExecuteObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.observables; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | import com.raizlabs.android.dbflow.sql.SqlUtils; 6 | import com.raizlabs.android.dbflow.sql.language.BaseQueriable; 7 | import com.raizlabs.android.dbflow.sql.queriable.Queriable; 8 | import com.raizlabs.android.dbflow.structure.BaseModel; 9 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 10 | 11 | import rx.Observable; 12 | import rx.Subscriber; 13 | 14 | /** 15 | * Given an RxSQLite query, executes a statement without a result. 16 | * @param The model representing the table to execute against 17 | */ 18 | public class DBFlowExecuteObservable extends Observable { 19 | 20 | private final Class mModelClazz; 21 | private final BaseQueriable mBaseQueriable; 22 | 23 | /** 24 | * Creates a new Observable which executes a sql statement against a table 25 | * @param clazz The model class representing the table to execute against 26 | * @param modelQueriable The query to execute 27 | * @param databaseWrapper The database in which the target table resides 28 | */ 29 | public DBFlowExecuteObservable(Class clazz, BaseQueriable modelQueriable, @Nullable DatabaseWrapper databaseWrapper) { 30 | super(new OnDBFlowSubscribe(modelQueriable, databaseWrapper)); 31 | mModelClazz = clazz; 32 | mBaseQueriable = modelQueriable; 33 | } 34 | 35 | /** 36 | * Publishes the results to all onchange observers after the statement has been executued. 37 | * @return An observable which publishes the change to all onchange observers 38 | */ 39 | public Observable publishTableUpdates(){ 40 | return lift(new DBFlowNotifyOfUpdate()); 41 | } 42 | 43 | private static class OnDBFlowSubscribe implements OnSubscribe { 44 | 45 | private final Queriable mBaseQueriable;; 46 | private final DatabaseWrapper mDatabaseWrapper; 47 | 48 | OnDBFlowSubscribe(Queriable modelQueriable, DatabaseWrapper databaseWrapper){ 49 | mBaseQueriable = modelQueriable; 50 | mDatabaseWrapper = databaseWrapper; 51 | } 52 | 53 | @Override 54 | public void call(final Subscriber subscriber) { 55 | if(mDatabaseWrapper != null) { 56 | mBaseQueriable.execute(mDatabaseWrapper); 57 | } else { 58 | mBaseQueriable.execute(); 59 | } 60 | 61 | subscriber.onNext(null); 62 | subscriber.onCompleted(); 63 | } 64 | } 65 | 66 | private class DBFlowNotifyOfUpdate implements Operator { 67 | @Override 68 | public Subscriber call(final Subscriber subscriber) { 69 | return new Subscriber() { 70 | @Override 71 | public void onCompleted() { 72 | 73 | } 74 | 75 | @Override 76 | public void onError(Throwable e) { 77 | 78 | } 79 | 80 | @Override 81 | public void onNext(Void tModels) { 82 | subscriber.onNext(tModels); 83 | 84 | if(mBaseQueriable.getQuery().toLowerCase().contains("delete")){ 85 | SqlUtils.notifyModelChanged(mModelClazz, BaseModel.Action.DELETE, null); 86 | } else if(mBaseQueriable.getQuery().toLowerCase().contains("update")){ 87 | SqlUtils.notifyModelChanged(mModelClazz, BaseModel.Action.UPDATE, null); 88 | } 89 | } 90 | }; 91 | 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/sql/transaction/RxGenericTransactionBlock.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.sql.transaction; 2 | 3 | import android.database.sqlite.SQLiteException; 4 | import android.support.annotation.NonNull; 5 | 6 | import com.raizlabs.android.dbflow.config.FlowManager; 7 | import com.raizlabs.android.dbflow.structure.Model; 8 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | import rx.Observable; 14 | import rx.Subscriber; 15 | 16 | /** 17 | *

Provides a transaction block that runs a series of database operations on a single database within 18 | * a single transaction. There is no limitation on what type of operations, that can run within a single transaction.

19 | *

Creation of a ModelOperationTransaction is via the use of the inner builder.

20 | */ 21 | public class RxGenericTransactionBlock extends Observable { 22 | 23 | private RxGenericTransactionBlock(Builder builder) { 24 | super(new OnTransactionSubscribe(builder.mTransactionProcessList, builder.mDatabaseWrapper)); 25 | } 26 | 27 | private static class OnTransactionSubscribe implements OnSubscribe { 28 | 29 | private final List mTransactionProcessList; 30 | private final DatabaseWrapper mDatabaseWrapper; 31 | 32 | private OnTransactionSubscribe(List transactionProcessList, DatabaseWrapper databaseWrapper) { 33 | mTransactionProcessList = transactionProcessList; 34 | mDatabaseWrapper = databaseWrapper; 35 | } 36 | 37 | @Override 38 | public void call(Subscriber subscriber) { 39 | 40 | try { 41 | mDatabaseWrapper.beginTransaction(); 42 | 43 | for (int i = 0; i < mTransactionProcessList.size(); i++) { 44 | if(!mTransactionProcessList.get(i).onProcess(mDatabaseWrapper)){ 45 | throw new SQLiteException("A transaction process item failed"); 46 | } 47 | } 48 | subscriber.onNext(null); 49 | subscriber.onCompleted(); 50 | 51 | 52 | mDatabaseWrapper.setTransactionSuccessful(); 53 | } catch (Exception e){ 54 | subscriber.onError(e); 55 | } finally{ 56 | mDatabaseWrapper.endTransaction(); 57 | } 58 | } 59 | } 60 | 61 | /** 62 | * A build which creates a new generic transaction block. 63 | */ 64 | public static class Builder { 65 | 66 | private final DatabaseWrapper mDatabaseWrapper; 67 | private List mTransactionProcessList; 68 | 69 | /** 70 | * Creates a new builder with a specific database wrapper. 71 | * @param databaseWrapper The database wrapper to run the transaction against 72 | */ 73 | public Builder(@NonNull DatabaseWrapper databaseWrapper){ 74 | mTransactionProcessList = new ArrayList<>(); 75 | mDatabaseWrapper = databaseWrapper; 76 | } 77 | 78 | /** 79 | * Creates a new builder, using a Model class to derive the target database. The class passed 80 | * to the parameter does not restrict the tables on which the database operations are performed. 81 | * As long as the operations run on a single database. 82 | * @param clazz The model class used to derive the target database. 83 | */ 84 | public Builder(@NonNull Class clazz){ 85 | mTransactionProcessList = new ArrayList<>(); 86 | mDatabaseWrapper = FlowManager.getDatabaseForTable(clazz).getWritableDatabase(); 87 | } 88 | 89 | /** 90 | * Adds a new transaction operation to be performed within a transaction block. 91 | * @param operation The operation to perform 92 | * @return An instance of the current builder object. 93 | */ 94 | public Builder addOperation(@NonNull TransactionOperation operation){ 95 | mTransactionProcessList.add(operation); 96 | return this; 97 | } 98 | 99 | /** 100 | * Builds a new GenericTransactionBlock observable which will run all database operations within 101 | * a single transaction once it's subscribed to. 102 | * @return A new GenericTransactionBlock containing all database operations 103 | */ 104 | public RxGenericTransactionBlock build(){ 105 | return new RxGenericTransactionBlock(this); 106 | } 107 | } 108 | 109 | /** 110 | * Represents a single transaction operation to perform within a transaction block. 111 | */ 112 | public interface TransactionOperation { 113 | boolean onProcess(DatabaseWrapper databaseWrapper); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/structure/RxBaseModel.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.structure; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | import com.raizlabs.android.dbflow.structure.BaseModel; 6 | import com.raizlabs.android.dbflow.structure.InvalidDBConfiguration; 7 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 8 | 9 | import au.com.roadhouse.rxdbflow.rx1.RxDbFlow; 10 | import rx.Observable; 11 | 12 | /** 13 | * A DBFlow BaseModel implementation which provides observables for saving, updating, inserting, and 14 | * deleting operations. 15 | */ 16 | @SuppressWarnings("unchecked") 17 | public class RxBaseModel extends BaseModel implements RxModifications { 18 | 19 | private RxModelAdapter mModelAdapter; 20 | 21 | /** 22 | * Returns an observable for saving the object in the database 23 | * @return An observable 24 | */ 25 | public Observable saveAsObservable(){ 26 | return getRxModelAdapter().saveAsObservable(this); 27 | } 28 | 29 | /** 30 | * Returns an observable for saving the object in the database 31 | * @param databaseWrapper The database wrapper for the database holding the table 32 | * @return An observable 33 | */ 34 | @SuppressWarnings("unchecked") 35 | public Observable saveAsObservable(@Nullable final DatabaseWrapper databaseWrapper){ 36 | return getRxModelAdapter().saveAsObservable(this, databaseWrapper); 37 | } 38 | 39 | /** 40 | * Returns an observable for inserting the object in the database 41 | * @return An observable 42 | */ 43 | public Observable insertAsObservable(){ 44 | return getRxModelAdapter().insertAsObservable(this); 45 | } 46 | 47 | /** 48 | * Returns an observable for inserting the object in the database 49 | * @param databaseWrapper The database wrapper for the database holding the table 50 | * @return An observable 51 | */ 52 | public Observable insertAsObservable(@Nullable final DatabaseWrapper databaseWrapper){ 53 | return getRxModelAdapter().insertAsObservable(this, databaseWrapper); 54 | } 55 | 56 | /** 57 | * Returns an observable for deleting the object from the database 58 | * @return An observable 59 | */ 60 | public Observable deleteAsObservable(){ 61 | return getRxModelAdapter().deleteAsObservable(this); 62 | } 63 | 64 | /** 65 | * Returns an observable for deleting the object from the database 66 | * @param databaseWrapper The database wrapper for the database holding the table 67 | * @return An observable 68 | */ 69 | public Observable deleteAsObservable(@Nullable final DatabaseWrapper databaseWrapper){ 70 | return getRxModelAdapter().deleteAsObservable(this, databaseWrapper); 71 | } 72 | 73 | /** 74 | * Returns an observable for update the object in the database 75 | * @return An observable 76 | */ 77 | public Observable updateAsObservable(){ 78 | return getRxModelAdapter().updateAsObservable(this); 79 | } 80 | 81 | /** 82 | * Returns an observable for update in object from the database 83 | * @param databaseWrapper The database wrapper for the database holding the table 84 | * @return An observable 85 | */ 86 | public Observable updateAsObservable(@Nullable final DatabaseWrapper databaseWrapper){ 87 | return getRxModelAdapter().updateAsObservable(this, databaseWrapper); 88 | } 89 | 90 | /** 91 | * Returns an observable which will refresh the model's data based on the primary key when subscribed. 92 | * @return An observable 93 | */ 94 | public Observable loadAsObservable(){ 95 | return getRxModelAdapter().loadAsObservable(this); 96 | } 97 | 98 | /** 99 | * Returns an observable which will refresh the model's data based on the primary key when subscribed. 100 | * @param databaseWrapper The database wrapper for the database holding the table 101 | * @return An observable 102 | */ 103 | public Observable loadAsObservable(@Nullable final DatabaseWrapper databaseWrapper){ 104 | return getRxModelAdapter().loadAsObservable(this, databaseWrapper); 105 | } 106 | 107 | /** 108 | * @return The associated {@link RxModelAdapter}. The {@link RxDbFlow} 109 | * may throw a {@link InvalidDBConfiguration} for this call if this class 110 | * is not associated with a table, so be careful when using this method. 111 | */ 112 | public RxModelAdapter getRxModelAdapter() { 113 | if (mModelAdapter == null) { 114 | mModelAdapter = RxDbFlow.getModelAdapter(getClass()); 115 | } 116 | return mModelAdapter; 117 | } 118 | } 119 | 120 | 121 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/java/au/com/roadhouse/rxdbflow/rx1/structure/RxModifications.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx1.structure; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 6 | 7 | import rx.Observable; 8 | 9 | /** 10 | * Provides a common interface for any 11 | * @param 12 | */ 13 | public interface RxModifications { 14 | 15 | Observable saveAsObservable(); 16 | 17 | Observable saveAsObservable(@Nullable DatabaseWrapper databaseWrapper); 18 | 19 | Observable insertAsObservable(); 20 | 21 | Observable insertAsObservable(@Nullable DatabaseWrapper databaseWrapper); 22 | 23 | Observable deleteAsObservable(); 24 | 25 | Observable deleteAsObservable(@Nullable DatabaseWrapper databaseWrapper); 26 | 27 | Observable updateAsObservable(); 28 | 29 | Observable updateAsObservable(@Nullable DatabaseWrapper databaseWrapper); 30 | } 31 | -------------------------------------------------------------------------------- /RxDbFlow-Rx1/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rxdbflow 3 | 4 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | group='com.github.roadhouse-dev' 4 | project.archivesBaseName="rxdbflow-rx2" 5 | 6 | android { 7 | compileSdkVersion 25 8 | buildToolsVersion "25.0.2" 9 | 10 | defaultConfig { 11 | minSdkVersion 14 12 | targetSdkVersion 25 13 | versionCode 10 14 | versionName "4.2.0.1" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | lintOptions { 25 | abortOnError false 26 | } 27 | } 28 | 29 | dependencies { 30 | compile fileTree(dir: 'libs', include: ['*.jar']) 31 | compile 'com.github.Raizlabs.DBFlow:dbflow:4.0.0-beta7' 32 | compile 'io.reactivex.rxjava2:rxjava:2.0.5' 33 | compile 'com.android.support:support-annotations:25.3.1' 34 | testCompile 'junit:junit:4.12' 35 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 36 | exclude group: 'com.android.support', module: 'support-annotations' 37 | }) 38 | } 39 | 40 | task sourcesJar(type: Jar) { 41 | from android.sourceSets.main.java.srcDirs 42 | classifier = 'sources' 43 | } 44 | task javadoc(type: Javadoc) { 45 | source = android.sourceSets.main.java.srcDirs 46 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 47 | failOnError false 48 | } 49 | task javadocJar(type: Jar, dependsOn: javadoc) { 50 | classifier = 'javadoc' 51 | from javadoc.destinationDir 52 | } 53 | 54 | task(type: Zip, "zipTestReport") { 55 | from("${rootDir.absolutePath}/library/build/reports/androidTests/connected/") 56 | archiveName 'test_report_library.zip' 57 | destinationDir file("${rootDir.absolutePath}/library/build/reports/") 58 | } 59 | 60 | configure(zipTestReport) { 61 | group = 'verification' 62 | description = 'Create a test report archive' 63 | } 64 | 65 | task(type: Zip, "zipLintReport") { 66 | from "${rootDir.absolutePath}/library/build/outputs/" 67 | include "lint-results-debug.html" 68 | include "lint-results-debug_files/*" 69 | archiveName 'lint_report_library.zip' 70 | destinationDir file("${rootDir.absolutePath}/library/build/reports/") 71 | } 72 | 73 | configure(zipLintReport) { 74 | group = 'verification' 75 | description = 'Create a lint report archive' 76 | } 77 | 78 | artifacts { 79 | archives sourcesJar 80 | archives javadocJar 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/dwaynehoy/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/DBFlowSchedulers.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2; 2 | 3 | import java.util.concurrent.ArrayBlockingQueue; 4 | import java.util.concurrent.BlockingQueue; 5 | import java.util.concurrent.ThreadPoolExecutor; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | import io.reactivex.Scheduler; 9 | import io.reactivex.schedulers.Schedulers; 10 | 11 | 12 | public class DBFlowSchedulers { 13 | 14 | private static Scheduler sScheduler; 15 | 16 | /** 17 | * A single threaded background scheduler, this should be used for dbflow observable operations 18 | * to avoid slamming the connection pool with multiple connection. 19 | * 20 | * @return A DBFlow background scheduler 21 | */ 22 | public static Scheduler background() { 23 | if (sScheduler == null) { 24 | BlockingQueue threadQueue = new ArrayBlockingQueue<>(200); 25 | ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, threadQueue); 26 | sScheduler = Schedulers.from(threadPoolExecutor); 27 | } 28 | 29 | return sScheduler; 30 | } 31 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/RxDbFlow.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2; 2 | 3 | import com.raizlabs.android.dbflow.config.FlowManager; 4 | 5 | import au.com.roadhouse.rxdbflow.rx2.structure.RxModelAdapter; 6 | 7 | public class RxDbFlow { 8 | 9 | public static RxModelAdapter getModelAdapter(Class modelClass) { 10 | return new RxModelAdapter<>(FlowManager.getModelAdapter(modelClass)); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/BaseModelQueriableObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.list.FlowCursorList; 4 | import com.raizlabs.android.dbflow.list.FlowQueryList; 5 | import com.raizlabs.android.dbflow.sql.language.BaseModelQueriable; 6 | import com.raizlabs.android.dbflow.sql.language.CursorResult; 7 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 8 | import com.raizlabs.android.dbflow.structure.BaseQueryModel; 9 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 10 | 11 | import java.util.List; 12 | 13 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowCursorListSingle; 14 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowCustomListSingle; 15 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowCustomModelSingle; 16 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowListSingle; 17 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowMaybe; 18 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowModelMaybe; 19 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowModelSingle; 20 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowQueryListSingle; 21 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowResultSingle; 22 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowSingle; 23 | 24 | 25 | public class BaseModelQueriableObservable extends BaseQueriableObservable implements ModelQueriableObservable { 26 | 27 | private ModelQueriable mRealModelQueriable; 28 | 29 | protected BaseModelQueriableObservable(BaseModelQueriable realModelQueriable) { 30 | super(realModelQueriable); 31 | mRealModelQueriable = realModelQueriable; 32 | } 33 | 34 | @Override 35 | public DBFlowSingle asSingle() { 36 | return new DBFlowModelSingle(mRealModelQueriable.getTable(), mRealModelQueriable, null); 37 | } 38 | 39 | @Override 40 | public DBFlowMaybe asMaybe() { 41 | return new DBFlowModelMaybe(mRealModelQueriable.getTable(), mRealModelQueriable, null); 42 | } 43 | 44 | @Override 45 | public DBFlowSingle asSingle(DatabaseWrapper databaseWrapper) { 46 | return new DBFlowModelSingle<>(mRealModelQueriable.getTable(), mRealModelQueriable, databaseWrapper); 47 | } 48 | 49 | @Override 50 | public DBFlowSingle> asListSingle() { 51 | return new DBFlowListSingle<>(mRealModelQueriable.getTable(), mRealModelQueriable, null); 52 | } 53 | 54 | @Override 55 | public DBFlowSingle> asListSingle(DatabaseWrapper databaseWrapper) { 56 | return new DBFlowListSingle<>(mRealModelQueriable.getTable(), mRealModelQueriable, databaseWrapper); 57 | } 58 | 59 | @Override 60 | public DBFlowSingle> asResultsSingle() { 61 | return new DBFlowResultSingle<>(mRealModelQueriable.getTable(), mRealModelQueriable); 62 | } 63 | 64 | @Override 65 | public DBFlowSingle> asQueryListSingle() { 66 | return new DBFlowQueryListSingle<>(mRealModelQueriable.getTable(), mRealModelQueriable); 67 | } 68 | 69 | @Override 70 | public DBFlowSingle> asCursorListSingle() { 71 | return new DBFlowCursorListSingle<>(mRealModelQueriable.getTable(), mRealModelQueriable); 72 | } 73 | 74 | @Override 75 | public DBFlowSingle asCustomSingle(Class customClazz) { 76 | return new DBFlowCustomModelSingle<>(customClazz, mRealModelQueriable.getTable(), mRealModelQueriable); 77 | } 78 | 79 | @Override 80 | public DBFlowSingle> asCustomListObservable(Class customClazz) { 81 | return new DBFlowCustomListSingle<>(customClazz, mRealModelQueriable.getTable(), mRealModelQueriable); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/BaseQueriableObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import android.database.Cursor; 4 | 5 | import com.raizlabs.android.dbflow.sql.language.BaseQueriable; 6 | import com.raizlabs.android.dbflow.sql.queriable.Queriable; 7 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 8 | 9 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowCountSingle; 10 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowCursorSingle; 11 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowExecuteCompletable; 12 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowSingle; 13 | 14 | public class BaseQueriableObservable implements QueriableObservable { 15 | private BaseQueriable mRealQueriable; 16 | 17 | public BaseQueriableObservable(BaseQueriable queriable) { 18 | mRealQueriable = queriable; 19 | } 20 | 21 | /** 22 | * {@inheritDoc} 23 | */ 24 | @Override 25 | public DBFlowSingle asCountSingle() { 26 | return new DBFlowCountSingle(mRealQueriable.getTable(), mRealQueriable, null); 27 | } 28 | 29 | /** 30 | * {@inheritDoc} 31 | */ 32 | @Override 33 | public DBFlowSingle asCountSingle(DatabaseWrapper databaseWrapper) { 34 | return new DBFlowCountSingle(mRealQueriable.getTable(), mRealQueriable, databaseWrapper); 35 | } 36 | 37 | /** 38 | * {@inheritDoc} 39 | */ 40 | @Override 41 | public DBFlowExecuteCompletable asExecuteCompletable() { 42 | return new DBFlowExecuteCompletable<>(mRealQueriable.getTable(), mRealQueriable, null); 43 | } 44 | 45 | /** 46 | * {@inheritDoc} 47 | */ 48 | @Override 49 | public DBFlowExecuteCompletable asExecuteCompletable(DatabaseWrapper databaseWrapper) { 50 | return new DBFlowExecuteCompletable<>(mRealQueriable.getTable(), mRealQueriable, databaseWrapper); 51 | } 52 | 53 | /** 54 | * {@inheritDoc} 55 | */ 56 | @Override 57 | public DBFlowSingle asQuerySingle() { 58 | return new DBFlowCursorSingle<>(mRealQueriable.getTable(), mRealQueriable, null); 59 | } 60 | 61 | public Queriable getRealQueriable() { 62 | return mRealQueriable; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/ModelQueriableObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.list.FlowCursorList; 4 | import com.raizlabs.android.dbflow.list.FlowQueryList; 5 | import com.raizlabs.android.dbflow.sql.language.CursorResult; 6 | import com.raizlabs.android.dbflow.structure.BaseQueryModel; 7 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 8 | 9 | import java.util.List; 10 | 11 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowMaybe; 12 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowSingle; 13 | 14 | 15 | public interface ModelQueriableObservable extends QueriableObservable { 16 | 17 | /** 18 | * Creates an Single that emits a single model from a query. This will be the first record 19 | * returned by the query. 20 | * @return An Single that emits a single model 21 | */ 22 | DBFlowSingle asSingle(); 23 | 24 | /** 25 | * Creates an Maybe that emits a single model or null from a query. This will be the first record 26 | * returned by the query or null if the query was empty. 27 | * @return An Single that emits a single model 28 | */ 29 | DBFlowMaybe asMaybe(); 30 | 31 | /** 32 | * Creates an Single that emits a single model from a query. This will be the first record 33 | * returned by the query. 34 | * @param databaseWrapper The database wrapper from which to run the query 35 | * @return An Single that emits a single model 36 | */ 37 | DBFlowSingle asSingle(DatabaseWrapper databaseWrapper); 38 | 39 | /** 40 | * Creates an Single that emits a list of models from a query. 41 | * @return An Single that emits a list of model 42 | */ 43 | DBFlowSingle> asListSingle(); 44 | 45 | /** 46 | * Creates an Single that emits a list of models from a query. 47 | * 48 | * @param databaseWrapper The database wrapper from which to run the query 49 | * @return An Single that emits a list of model 50 | */ 51 | DBFlowSingle> asListSingle(DatabaseWrapper databaseWrapper); 52 | 53 | /** 54 | * Creates an Single that emits the results of a query as a CursorResult. 55 | * 56 | * @return An Single that emits a CursorResult 57 | */ 58 | DBFlowSingle> asResultsSingle(); 59 | 60 | /** 61 | * Creates an Single that emits the results of a query as a FlowQueryList. 62 | * 63 | * @return An Single that emits a FlowQueryList 64 | */ 65 | DBFlowSingle> asQueryListSingle(); 66 | 67 | /** 68 | * Creates an Single that emits the results of a query as a FlowCursorList. 69 | * 70 | * @return An Single that emits a FlowCursorList 71 | */ 72 | DBFlowSingle> asCursorListSingle(); 73 | 74 | /** 75 | * Creates an Single that emits a single custom QueryModel class. This will be the first record 76 | * returned by the query. 77 | * 78 | * @return An Single that emits a single custom QueryModel class. 79 | */ 80 | DBFlowSingle asCustomSingle(Class customClazz); 81 | 82 | /** 83 | * Creates an observable that emits the results of a query as a list of custom QueryModel objects. 84 | * 85 | * @return An observable that emits a single custom QueryModel class. 86 | */ 87 | DBFlowSingle> asCustomListObservable(Class customClazz); 88 | } 89 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/NullValue.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | /** 4 | * Created by dwaynehoy on 8/3/17. 5 | */ 6 | 7 | public class NullValue { 8 | } 9 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/QueriableObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import android.database.Cursor; 4 | 5 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 6 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 7 | 8 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowExecuteCompletable; 9 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowSingle; 10 | 11 | public interface QueriableObservable { 12 | 13 | /** 14 | * Creates an Single that emits a long value. This should be paired with {@link RxSQLite#selectCountOf(IProperty[])} 15 | * or a query that returns a single long/integer value. Calling this on any other query could result in unexpected return values 16 | * 17 | * @return An Single that emits a long result 18 | */ 19 | DBFlowSingle asCountSingle(); 20 | 21 | /** 22 | * Creates an Single that emits a long value. This should be paired with {@link RxSQLite#selectCountOf(IProperty[])} 23 | * or a query that returns a single long/integer value. Calling this on any other query could result in an unexpected emitted 24 | * value 25 | * 26 | * @param databaseWrapper The database wrapper to use for the query 27 | * @return An Single that emits a long result 28 | */ 29 | DBFlowSingle asCountSingle(DatabaseWrapper databaseWrapper); 30 | 31 | /** 32 | * Creates an Single that executes a SQL statement. This is usually used with CRUD statements as 33 | * it only emits a Void value 34 | * 35 | * @return An Single that executes a SQL statement 36 | */ 37 | DBFlowExecuteCompletable asExecuteCompletable(); 38 | 39 | /** 40 | * Creates an Single that executes a SQL statement. This is usually used with CRUD statements as 41 | * it only emits a Void value 42 | * 43 | * @param databaseWrapper The database wrapper to use for the query 44 | * @return An Single that executes a SQL statement 45 | */ 46 | DBFlowExecuteCompletable asExecuteCompletable(DatabaseWrapper databaseWrapper); 47 | 48 | /** 49 | * Creates an Single that emits a cursor containing the results of the query 50 | * @return An Single that emits a cursor 51 | */ 52 | DBFlowSingle asQuerySingle(); 53 | } 54 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxDelete.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.language.Delete; 4 | 5 | /** 6 | * Creates a delete SQL statement 7 | */ 8 | public class RxDelete { 9 | private Delete mRealDelete; 10 | 11 | RxDelete(){ 12 | mRealDelete = new Delete(); 13 | } 14 | 15 | /** 16 | * Constructs a from clause for the delete statement 17 | * @param table The table to delete from 18 | * @return A RxFrom instance 19 | */ 20 | public RxFrom from(Class table){ 21 | //noinspection unchecked 22 | return new RxFrom(mRealDelete.from(table)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxIndexedBy.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.Query; 4 | import com.raizlabs.android.dbflow.sql.language.IndexedBy; 5 | import com.raizlabs.android.dbflow.sql.language.NameAlias; 6 | import com.raizlabs.android.dbflow.sql.language.OrderBy; 7 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 8 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 9 | import com.raizlabs.android.dbflow.sql.language.WhereBase; 10 | import com.raizlabs.android.dbflow.structure.BaseModel; 11 | /** 12 | * Constructs an Index By clause for a SQL query 13 | */ 14 | public class RxIndexedBy implements WhereBase, RxTransformable { 15 | 16 | private IndexedBy mRealIndexedBy; 17 | 18 | RxIndexedBy(IndexedBy realIndexedBy) { 19 | mRealIndexedBy = realIndexedBy; 20 | } 21 | 22 | /** 23 | * {@inheritDoc} 24 | */ 25 | @Override 26 | public RxWhere groupBy(NameAlias... nameAliases) { 27 | return new RxWhere<>(mRealIndexedBy.groupBy(nameAliases)); 28 | } 29 | 30 | /** 31 | * {@inheritDoc} 32 | */ 33 | @Override 34 | public RxWhere groupBy(IProperty... properties) { 35 | return new RxWhere<>(mRealIndexedBy.groupBy(properties)); 36 | } 37 | 38 | /** 39 | * {@inheritDoc} 40 | */ 41 | @Override 42 | public RxWhere orderBy(NameAlias nameAlias, boolean ascending) { 43 | return new RxWhere<>(mRealIndexedBy.orderBy(nameAlias, ascending)); 44 | } 45 | 46 | /** 47 | * {@inheritDoc} 48 | */ 49 | @Override 50 | public RxWhere orderBy(IProperty property, boolean ascending) { 51 | return new RxWhere<>(mRealIndexedBy.orderBy(property, ascending)); 52 | } 53 | 54 | /** 55 | * {@inheritDoc} 56 | */ 57 | @Override 58 | public RxWhere orderBy(OrderBy orderBy) { 59 | return new RxWhere<>(mRealIndexedBy.orderBy(orderBy)); 60 | } 61 | 62 | /** 63 | * {@inheritDoc} 64 | */ 65 | @Override 66 | public RxWhere limit(int count) { 67 | return new RxWhere<>(mRealIndexedBy.limit(count)); 68 | } 69 | 70 | /** 71 | * {@inheritDoc} 72 | */ 73 | @Override 74 | public RxWhere offset(int offset) { 75 | return new RxWhere<>(mRealIndexedBy.offset(offset)); 76 | } 77 | 78 | /** 79 | * {@inheritDoc} 80 | */ 81 | @Override 82 | public RxWhere having(SQLOperator... conditions) { 83 | return new RxWhere<>(mRealIndexedBy.having(conditions)); 84 | } 85 | 86 | /** 87 | * {@inheritDoc} 88 | */ 89 | @Override 90 | public Class getTable() { 91 | return mRealIndexedBy.getTable(); 92 | } 93 | 94 | /** 95 | * {@inheritDoc} 96 | */ 97 | @Override 98 | public Query getQueryBuilderBase() { 99 | return mRealIndexedBy.getQueryBuilderBase(); 100 | } 101 | 102 | /** 103 | * {@inheritDoc} 104 | */ 105 | @Override 106 | public String getQuery() { 107 | return mRealIndexedBy.getQuery(); 108 | } 109 | 110 | @Override 111 | public BaseModel.Action getPrimaryAction() { 112 | return mRealIndexedBy.getPrimaryAction(); 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxJoin.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.Query; 4 | import com.raizlabs.android.dbflow.sql.language.Join; 5 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 6 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 7 | 8 | /** 9 | * Constructs a join clause for a SQL statement 10 | * @param The table model class to join to 11 | * @param The table model class being joined to 12 | */ 13 | public class RxJoin implements Query { 14 | 15 | private Join mRealJoin; 16 | private RxFrom mRxFrom; 17 | 18 | RxJoin(RxFrom from, Join join){ 19 | mRxFrom = from; 20 | mRealJoin = join; 21 | } 22 | 23 | @Override 24 | public String getQuery() { 25 | return mRealJoin.getQuery(); 26 | } 27 | 28 | /** 29 | * Creates a "on" clause when joining two tables 30 | * @param onConditions The conditions to use when joining the tables 31 | * @return A RxFrom clause 32 | */ 33 | public RxFrom on(SQLOperator... onConditions){ 34 | mRealJoin.on(onConditions); 35 | return mRxFrom; 36 | } 37 | 38 | /** 39 | * Creates a using clause when joining two tables 40 | * @param columns The columns to use when joining 41 | * @return A RxFrom instance 42 | */ 43 | public RxFrom using(IProperty... columns) { 44 | mRealJoin.using(columns); 45 | return mRxFrom; 46 | } 47 | 48 | public RxJoin as(String as){ 49 | mRealJoin.as(as); 50 | return this; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxSQLite.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.raizlabs.android.dbflow.sql.language.Case; 6 | import com.raizlabs.android.dbflow.sql.language.CaseCondition; 7 | import com.raizlabs.android.dbflow.sql.language.From; 8 | import com.raizlabs.android.dbflow.sql.language.Index; 9 | import com.raizlabs.android.dbflow.sql.language.Method; 10 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 11 | import com.raizlabs.android.dbflow.sql.language.SQLite; 12 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 13 | import com.raizlabs.android.dbflow.sql.language.property.Property; 14 | import com.raizlabs.android.dbflow.sql.language.Trigger; 15 | import com.raizlabs.android.dbflow.structure.Model; 16 | 17 | /** 18 | * Provides an entry point into all RxSqlite queries 19 | */ 20 | public class RxSQLite { 21 | /** 22 | * @param properties The properties/columns to SELECT. 23 | * @return A beginning of the SELECT statement. 24 | */ 25 | public static RxSelect select(IProperty... properties) { 26 | return new RxSelect(properties); 27 | } 28 | 29 | /** 30 | * Starts a new SELECT COUNT(property1, property2, propertyn) (if properties specified) or 31 | * SELECT COUNT(*). 32 | * 33 | * @param properties Optional, if specified returns the count of non-null ROWs from a specific single/group of columns. 34 | * @return A new select statement SELECT COUNT(expression) 35 | */ 36 | public static RxSelect selectCountOf(IProperty... properties) { 37 | return new RxSelect(Method.count(properties)); 38 | } 39 | 40 | /** 41 | * @param table The tablet to update. 42 | * @param The class that implements {@link Model}. 43 | * @return A new UPDATE statement. 44 | */ 45 | public static RxUpdate update(Class table) { 46 | return new RxUpdate<>(table); 47 | } 48 | 49 | /** 50 | * @param table The table to insert. 51 | * @param The class that implements {@link Model}. 52 | * @return A new INSERT statement. 53 | */ 54 | public static RxInsert insert(Class table) { 55 | return new RxInsert<>(table); 56 | } 57 | 58 | /** 59 | * @return Begins a DELETE statement. 60 | */ 61 | public static RxDelete delete() { 62 | return new RxDelete(); 63 | } 64 | 65 | /** 66 | * Starts a DELETE statement on the specified table. 67 | * 68 | * @param table The table to delete from. 69 | * @param The class that implements {@link Model}. 70 | * @return A {@link From} with specified DELETE on table. 71 | */ 72 | @SuppressWarnings("unchecked") 73 | public static RxFrom delete(Class table) { 74 | return delete().from(table); 75 | } 76 | 77 | /** 78 | * Starts an INDEX statement on specified table. 79 | * 80 | * @param name The name of the index. 81 | * @param The class that implements {@link Model}. 82 | * @return A new INDEX statement. 83 | */ 84 | public static Index index(String name) { 85 | return new Index<>(name); 86 | } 87 | 88 | /** 89 | * Starts a TRIGGER statement. 90 | * 91 | * @param name The name of the trigger. 92 | * @return A new TRIGGER statement. 93 | */ 94 | public static Trigger createTrigger(String name) { 95 | return Trigger.create(name); 96 | } 97 | 98 | /** 99 | * Starts a CASE statement. 100 | * 101 | * @param condition The condition to check for in the WHEN. 102 | * @return A new {@link CaseCondition}. 103 | */ 104 | //TODO: Wrap Case 105 | public static CaseCondition caseWhen(@NonNull SQLOperator condition) { 106 | return SQLite.caseWhen(condition); 107 | } 108 | 109 | /** 110 | * Starts an efficient CASE statement. The value passed here is only evaulated once. A non-efficient 111 | * case statement will evaluate all of its {@link SQLOperator}. 112 | * 113 | * @param caseColumn The value 114 | * @param 115 | * @return 116 | */ 117 | public static Case _case(Property caseColumn) { 118 | return SQLite._case(caseColumn); 119 | } 120 | 121 | /** 122 | * Starts an efficient CASE statement. The value passed here is only evaulated once. A non-efficient 123 | * case statement will evaluate all of its {@link SQLOperator}. 124 | * 125 | * @param caseColumn The value 126 | * @param 127 | * @return 128 | */ 129 | public static Case _case(IProperty caseColumn) { 130 | return SQLite._case(caseColumn); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxSelect.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.Query; 4 | import com.raizlabs.android.dbflow.sql.language.Select; 5 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 6 | 7 | /** 8 | * Creates a select SQL query 9 | */ 10 | public class RxSelect implements Query{ 11 | 12 | private Select mRealSelect; 13 | 14 | RxSelect(IProperty... properties){ 15 | mRealSelect = new Select(properties); 16 | } 17 | 18 | /** 19 | * Constructs a from clause for the SQL query 20 | * @param table The table model class to run the select statement against 21 | * @return A RxFrom instance 22 | */ 23 | public RxFrom from(Class table) { 24 | return new RxFrom<>(mRealSelect.from(table)); 25 | } 26 | 27 | /** 28 | * Constructs a distinct clause for the SQL query 29 | * @return The RxSelect isntance 30 | */ 31 | public RxSelect distinct() { 32 | mRealSelect = mRealSelect.distinct(); 33 | return this; 34 | } 35 | 36 | @Override 37 | public String getQuery() { 38 | return mRealSelect.getQuery(); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return getQuery(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxSet.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | 6 | import com.raizlabs.android.dbflow.sql.Query; 7 | import com.raizlabs.android.dbflow.sql.language.NameAlias; 8 | import com.raizlabs.android.dbflow.sql.language.OrderBy; 9 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 10 | import com.raizlabs.android.dbflow.sql.language.Set; 11 | import com.raizlabs.android.dbflow.sql.language.Where; 12 | import com.raizlabs.android.dbflow.sql.language.WhereBase; 13 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 14 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 15 | import com.raizlabs.android.dbflow.structure.BaseModel; 16 | 17 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowExecuteCompletable; 18 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowSingle; 19 | 20 | /** 21 | * Provides a set block for a select statement 22 | */ 23 | public class RxSet implements WhereBase, QueriableObservable, RxTransformable{ 24 | 25 | private Set mRealSet; 26 | 27 | RxSet(Set realSet) { 28 | mRealSet = realSet; 29 | } 30 | 31 | /** 32 | * Specifies a varg of conditions to append to this SET 33 | * 34 | * @param conditions The varg of conditions 35 | * @return This instance. 36 | */ 37 | public RxSet conditions(SQLOperator... conditions) { 38 | mRealSet.conditions(conditions); 39 | return this; 40 | } 41 | 42 | /** 43 | * Specifies a set of content values to append to this SET as Conditions 44 | * 45 | * @param contentValues The set of values to append. 46 | * @return This instance. 47 | */ 48 | public RxSet conditionValues(ContentValues contentValues) { 49 | mRealSet.conditionValues(contentValues); 50 | return this; 51 | } 52 | 53 | /** 54 | * Begins completing the rest of this SET statement. 55 | * 56 | * @param conditions The conditions to fill the WHERE with. 57 | * @return The where piece of this query. 58 | */ 59 | public RxWhere where(SQLOperator... conditions) { 60 | Where where = mRealSet.where(conditions); 61 | return new RxWhere<>(where); 62 | } 63 | 64 | @Override 65 | public RxWhere groupBy(NameAlias... nameAliases) { 66 | Where where = mRealSet.groupBy(nameAliases); 67 | return new RxWhere<>(where); 68 | } 69 | 70 | @Override 71 | public RxWhere groupBy(IProperty... properties) { 72 | Where where = mRealSet.groupBy(properties); 73 | return new RxWhere<>(where); 74 | } 75 | 76 | @Override 77 | public RxWhere orderBy(NameAlias nameAlias, boolean ascending) { 78 | Where where = mRealSet.orderBy(nameAlias, ascending); 79 | return new RxWhere<>(where); 80 | } 81 | 82 | @Override 83 | public RxWhere orderBy(IProperty property, boolean ascending) { 84 | Where where = mRealSet.orderBy(property, ascending); 85 | return new RxWhere<>(where); 86 | } 87 | 88 | @Override 89 | public RxWhere orderBy(OrderBy orderBy) { 90 | Where where = mRealSet.orderBy(orderBy); 91 | return new RxWhere<>(where); 92 | } 93 | 94 | @Override 95 | public RxWhere limit(int count) { 96 | Where where = mRealSet.limit(count); 97 | return new RxWhere<>(where); 98 | } 99 | 100 | @Override 101 | public RxWhere offset(int offset) { 102 | Where where = mRealSet.offset(offset); 103 | return new RxWhere<>(where); 104 | } 105 | 106 | 107 | @Override 108 | public RxWhere having(SQLOperator... conditions) { 109 | Where where = mRealSet.having(conditions); 110 | return new RxWhere<>(where); 111 | } 112 | 113 | 114 | @Override 115 | public String getQuery() { 116 | return mRealSet.getQuery(); 117 | } 118 | 119 | @Override 120 | public Class getTable() { 121 | return mRealSet.getTable(); 122 | } 123 | 124 | @Override 125 | public Query getQueryBuilderBase() { 126 | return mRealSet.getQueryBuilderBase(); 127 | } 128 | 129 | 130 | @Override 131 | public DBFlowSingle asCountSingle() { 132 | return where().asCountSingle(); 133 | } 134 | 135 | @Override 136 | public DBFlowSingle asCountSingle(DatabaseWrapper databaseWrapper) { 137 | return where().asCountSingle(databaseWrapper); 138 | } 139 | 140 | @Override 141 | public DBFlowExecuteCompletable asExecuteCompletable() { 142 | return where().asExecuteCompletable(); 143 | } 144 | 145 | @Override 146 | public DBFlowExecuteCompletable asExecuteCompletable(DatabaseWrapper databaseWrapper) { 147 | return where().asExecuteCompletable(databaseWrapper); 148 | } 149 | 150 | @Override 151 | public DBFlowSingle asQuerySingle() { 152 | return where().asQuerySingle(); 153 | } 154 | 155 | @Override 156 | public BaseModel.Action getPrimaryAction() { 157 | return mRealSet.getPrimaryAction(); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxTransformable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.language.NameAlias; 4 | import com.raizlabs.android.dbflow.sql.language.OrderBy; 5 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 6 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 7 | 8 | /** 9 | * An interface for all SQL query classes that transform RxSQLite query results 10 | * 11 | * @param The table model class that the sqlite query will emit 12 | */ 13 | public interface RxTransformable { 14 | 15 | /** 16 | * Constructs a group by clause 17 | * @param nameAliases The fields to group the query by 18 | * @return An instance of RxWhere 19 | */ 20 | RxWhere groupBy(NameAlias... nameAliases); 21 | 22 | /** 23 | * Constructs a group by clause 24 | * @param properties The fields to group the query by 25 | * @return An instance of RxWhere 26 | */ 27 | RxWhere groupBy(IProperty... properties); 28 | 29 | /** 30 | * Constructs an order by clause 31 | * @param nameAlias The field to use for ordering 32 | * @param ascending True to sort ascending, false otherwise 33 | * @return An instance of RxWhere 34 | */ 35 | RxWhere orderBy(NameAlias nameAlias, boolean ascending); 36 | 37 | /** 38 | * Constructs an order by clause 39 | * @param property The field to use for ordering 40 | * @param ascending True to sort ascending, false otherwise 41 | * @return An instance of RxWhere 42 | */ 43 | RxWhere orderBy(IProperty property, boolean ascending); 44 | 45 | /** 46 | * Adds an order by clause to the current query 47 | * @param orderBy The order by clause to use 48 | * @return An instance of RxWhere 49 | */ 50 | RxWhere orderBy(OrderBy orderBy); 51 | 52 | /** 53 | * Constructs a limit clause, limiting the number or returned results 54 | * @param count The number of records to limit to query to 55 | * @return An instance of RxWhere 56 | */ 57 | RxWhere limit(int count); 58 | 59 | /** 60 | * Constructs an offset clause 61 | * @param offset The number of records to offset the results by 62 | * @return An instance of RxWhere 63 | */ 64 | RxWhere offset(int offset); 65 | 66 | /** 67 | * Constructs a having clause 68 | * @param conditions The conditions contained in the having clause 69 | * @return An instance of RxWhere 70 | */ 71 | RxWhere having(SQLOperator... conditions); 72 | } 73 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxUpdate.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.annotation.ConflictAction; 4 | import com.raizlabs.android.dbflow.sql.Query; 5 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 6 | import com.raizlabs.android.dbflow.sql.language.Set; 7 | import com.raizlabs.android.dbflow.sql.language.Update; 8 | 9 | /** 10 | * Creates an update SQL query 11 | */ 12 | public class RxUpdate implements Query { 13 | 14 | private final Update mRealUpdate; 15 | 16 | RxUpdate(Class clazz){ 17 | mRealUpdate = new Update<>(clazz); 18 | } 19 | 20 | @Override 21 | public String getQuery() { 22 | return mRealUpdate.getQuery(); 23 | } 24 | 25 | /** 26 | * Adds a conflict action to the update statement 27 | * @param conflictAction The action to take on conflict 28 | * @return An RxUpdate instance 29 | */ 30 | public RxUpdate conflictAction(ConflictAction conflictAction) { 31 | mRealUpdate.conflictAction(conflictAction); 32 | return this; 33 | } 34 | 35 | /** 36 | * Adds a rollback action on conflict 37 | * @return This instance. 38 | * @see ConflictAction#ROLLBACK 39 | */ 40 | public RxUpdate orRollback() { 41 | return conflictAction(ConflictAction.ROLLBACK); 42 | } 43 | 44 | /** 45 | * Adds an abort action on conflict 46 | * @return This instance. 47 | * @see ConflictAction#ABORT 48 | */ 49 | public RxUpdate orAbort() { 50 | return conflictAction(ConflictAction.ABORT); 51 | } 52 | 53 | /** 54 | * Adds a replace action on conflict 55 | * @return This instance. 56 | * @see ConflictAction#REPLACE 57 | */ 58 | public RxUpdate orReplace() { 59 | return conflictAction(ConflictAction.REPLACE); 60 | } 61 | 62 | /** 63 | * Adds a fail action on conflict 64 | * @return This instance. 65 | * @see ConflictAction#FAIL 66 | */ 67 | public RxUpdate orFail() { 68 | return conflictAction(ConflictAction.FAIL); 69 | } 70 | 71 | /** 72 | * Adds an ignore action on conflict 73 | * @return This instance. 74 | * @see ConflictAction#IGNORE 75 | */ 76 | public RxUpdate orIgnore() { 77 | return conflictAction(ConflictAction.IGNORE); 78 | } 79 | 80 | /** 81 | * Begins a SET block for the SQL query 82 | * 83 | * @param conditions The array of conditions that define this SET statement 84 | * @return A SET query piece of this statement 85 | */ 86 | public RxSet set(SQLOperator... conditions) { 87 | Set set = mRealUpdate.set(conditions); 88 | return new RxSet<>(set); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/language/RxWhere.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.language; 2 | 3 | import com.raizlabs.android.dbflow.sql.Query; 4 | import com.raizlabs.android.dbflow.sql.language.NameAlias; 5 | import com.raizlabs.android.dbflow.sql.language.OrderBy; 6 | import com.raizlabs.android.dbflow.sql.language.SQLOperator; 7 | import com.raizlabs.android.dbflow.sql.language.Where; 8 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 9 | 10 | /** 11 | * Defines the where clause of a SQL query 12 | */ 13 | public class RxWhere extends BaseModelQueriableObservable 14 | implements Query, RxTransformable { 15 | 16 | Where mRealWhere; 17 | 18 | RxWhere(Where where) { 19 | super(where); 20 | mRealWhere = where; 21 | } 22 | 23 | /** 24 | * Adds a param to the WHERE clause with the custom {@link SQLOperator} 25 | * 26 | * @param condition The {@link SQLOperator} to use 27 | * @return An instance of RxWhere 28 | */ 29 | public RxWhere and(SQLOperator condition) { 30 | mRealWhere.and(condition); 31 | 32 | return this; 33 | } 34 | 35 | /** 36 | * Adds a param to the WHERE clause with the custom {@link SQLOperator} 37 | * 38 | * @param condition The {@link SQLOperator} to use 39 | * @return An instance of RxWhere 40 | */ 41 | public RxWhere or(SQLOperator condition) { 42 | mRealWhere.or(condition); 43 | return this; 44 | } 45 | 46 | /** 47 | * Constructs a where clause which contains all of the passed in conditions 48 | * 49 | * @param conditions The conditions to add to the where clause 50 | * @return An instance of RxWhere 51 | */ 52 | public RxWhere andAll(SQLOperator[] conditions) { 53 | mRealWhere.andAll(conditions); 54 | return this; 55 | } 56 | 57 | @Override 58 | public String getQuery() { 59 | return mRealWhere.getQuery(); 60 | } 61 | 62 | /** 63 | * {@inheritDoc} 64 | */ 65 | @Override 66 | public RxWhere groupBy(NameAlias... nameAliases) { 67 | mRealWhere.groupBy(nameAliases); 68 | return this; 69 | } 70 | 71 | /** 72 | * {@inheritDoc} 73 | */ 74 | @Override 75 | public RxWhere groupBy(IProperty... properties) { 76 | mRealWhere.groupBy(properties); 77 | return this; 78 | } 79 | 80 | /** 81 | * {@inheritDoc} 82 | */ 83 | @Override 84 | public RxWhere orderBy(NameAlias nameAlias, boolean ascending) { 85 | mRealWhere.orderBy(nameAlias, ascending); 86 | return this; 87 | } 88 | 89 | /** 90 | * {@inheritDoc} 91 | */ 92 | @Override 93 | public RxWhere orderBy(IProperty property, boolean ascending) { 94 | mRealWhere.orderBy(property, ascending); 95 | return this; 96 | } 97 | 98 | /** 99 | * {@inheritDoc} 100 | */ 101 | @Override 102 | public RxWhere orderBy(OrderBy orderBy) { 103 | mRealWhere.orderBy(orderBy); 104 | return this; 105 | } 106 | 107 | /** 108 | * {@inheritDoc} 109 | */ 110 | @Override 111 | public RxWhere limit(int count) { 112 | mRealWhere.limit(count); 113 | return this; 114 | } 115 | 116 | /** 117 | * {@inheritDoc} 118 | */ 119 | @Override 120 | public RxWhere offset(int offset) { 121 | mRealWhere.offset(offset); 122 | return this; 123 | } 124 | 125 | /** 126 | * {@inheritDoc} 127 | */ 128 | @Override 129 | public RxWhere having(SQLOperator... conditions) { 130 | mRealWhere.having(conditions); 131 | return this; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowBaseCompletable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import io.reactivex.CompletableObserver; 4 | 5 | 6 | public abstract class DBFlowBaseCompletable extends DBFlowCompletable { 7 | 8 | private final Class mModelClazz; 9 | 10 | public DBFlowBaseCompletable(Class clazz) { 11 | mModelClazz = clazz; 12 | } 13 | 14 | 15 | protected Class getPrimaryModelClass() { 16 | return mModelClazz; 17 | } 18 | 19 | protected void subscribeActual(CompletableObserver observer) { 20 | run(); 21 | observer.onComplete(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowBaseSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import io.reactivex.SingleObserver; 4 | import io.reactivex.exceptions.Exceptions; 5 | import io.reactivex.internal.disposables.EmptyDisposable; 6 | 7 | public abstract class DBFlowBaseSingle extends DBFlowSingle { 8 | 9 | private final Class mModelClazz; 10 | 11 | public DBFlowBaseSingle(Class clazz) { 12 | mModelClazz = clazz; 13 | } 14 | 15 | @Override 16 | protected Class getPrimaryModelClass() { 17 | return mModelClazz; 18 | } 19 | 20 | @Override 21 | protected void subscribeActual(SingleObserver observer) { 22 | 23 | observer.onSubscribe(EmptyDisposable.INSTANCE); 24 | try { 25 | Container v = run(); 26 | if (v != null) { 27 | observer.onSuccess(v); 28 | } else { 29 | observer.onError(new NullPointerException("The callable returned a null value")); 30 | } 31 | } catch (Throwable e) { 32 | Exceptions.throwIfFatal(e); 33 | observer.onError(e); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowCompletable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import io.reactivex.Completable; 4 | 5 | public abstract class DBFlowCompletable extends Completable { 6 | public abstract void run(); 7 | } 8 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowCountSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.annotation.Nullable; 5 | 6 | import com.raizlabs.android.dbflow.sql.language.BaseQueriable; 7 | import com.raizlabs.android.dbflow.sql.language.property.IProperty; 8 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 9 | 10 | /** 11 | * Given a RxDBFlow query, creates an observable which emits a single count of results in the database table/view. 12 | * In most cases this observable should be paired with 13 | * {@link au.com.roadhouse.rxdbflow.rx2.sql.language.RxSQLite#selectCountOf(IProperty[])} 14 | */ 15 | public class DBFlowCountSingle extends DBFlowBaseSingle { 16 | 17 | private final BaseQueriable mBaseModelQueriable; 18 | private final DatabaseWrapper mDatabaseWrapper; 19 | 20 | /** 21 | * Creates a new DBFlowCountSingle. Generally this constructor is not used directly, but used 22 | * as part of a query statement. {@see au.com.roadhouse.rxdbflow.rx2.sql.language.RxSQLite} 23 | * @param clazz The class of the table/view the query effects. 24 | * @param baseModelQueriable The query to run when computing the count 25 | * @param databaseWrapper The database wrapper that the target table/view belongs too. 26 | */ 27 | public DBFlowCountSingle(@NonNull Class clazz, final BaseQueriable baseModelQueriable, 28 | @Nullable DatabaseWrapper databaseWrapper) { 29 | super(clazz); 30 | mBaseModelQueriable = baseModelQueriable; 31 | mDatabaseWrapper = databaseWrapper; 32 | } 33 | 34 | 35 | @Override 36 | public Long run() { 37 | if(mDatabaseWrapper != null){ 38 | return mBaseModelQueriable.count(mDatabaseWrapper); 39 | } else { 40 | return mBaseModelQueriable.count(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowCursorListSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import com.raizlabs.android.dbflow.list.FlowCursorList; 4 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 5 | 6 | import java.util.ArrayList; 7 | 8 | /** 9 | * Given a RxSQLite query, emits the results from the query as a FlowCursorList. 10 | */ 11 | public class DBFlowCursorListSingle extends DBFlowBaseSingle> { 12 | private final ModelQueriable mQueriable; 13 | private ArrayList mObserverDisposables = new ArrayList<>(); 14 | 15 | /** 16 | * Creates a new observable which runs a query and emits the result as a Cursor 17 | * @param clazz The table/view model in which the FlowCursorList will contain 18 | * @param queriable The query to run 19 | */ 20 | public DBFlowCursorListSingle(Class clazz, ModelQueriable queriable) { 21 | super(clazz); 22 | mQueriable = queriable; 23 | } 24 | 25 | @Override 26 | public FlowCursorList run() { 27 | return mQueriable.cursorList(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowCursorSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import android.database.Cursor; 4 | 5 | import com.raizlabs.android.dbflow.sql.queriable.Queriable; 6 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 7 | 8 | /** 9 | * Given a RxSQLite query, emits the results from the query as a FlowCursorList. 10 | */ 11 | public class DBFlowCursorSingle extends DBFlowBaseSingle { 12 | 13 | private final Queriable mQueriable; 14 | private final DatabaseWrapper mDatabaseWrapper; 15 | 16 | /** 17 | * Creates a new observable which runs a query and emits the result as a Cursor 18 | * @param clazz The table/view model in which the FlowCursorList will contain 19 | * @param queriable The query to run 20 | */ 21 | public DBFlowCursorSingle( Class clazz, Queriable queriable, DatabaseWrapper databaseWrapper) { 22 | super(clazz); 23 | mQueriable = queriable; 24 | mDatabaseWrapper = databaseWrapper; 25 | } 26 | 27 | @Override 28 | public Cursor run() { 29 | if(mDatabaseWrapper != null){ 30 | return mQueriable.query(mDatabaseWrapper); 31 | } else { 32 | return mQueriable.query(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowCustomListSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 4 | import com.raizlabs.android.dbflow.structure.BaseQueryModel; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Given a RxSQLite query, emits the results from the query as a List of custom models. 10 | */ 11 | 12 | public class DBFlowCustomListSingle extends DBFlowBaseSingle> { 13 | 14 | private final Class mQueryModelClazz; 15 | private final ModelQueriable mBaseModelQueriable; 16 | 17 | /** 18 | * Creates a new observable which runs a query and emits the result as a CustomList 19 | 20 | * @param baseModelQueriable The query to run 21 | */ 22 | public DBFlowCustomListSingle(Class queryModelClazz, Class modelClass, ModelQueriable baseModelQueriable) { 23 | super(modelClass); 24 | mBaseModelQueriable = baseModelQueriable; 25 | mQueryModelClazz = queryModelClazz; 26 | } 27 | 28 | @Override 29 | public List run() { 30 | return mBaseModelQueriable.queryCustomList(mQueryModelClazz); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowCustomModelSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 4 | import com.raizlabs.android.dbflow.structure.BaseQueryModel; 5 | 6 | /** 7 | * Given a RxSQLite query, emits the the first element from the query as a custom model. 8 | */ 9 | public class DBFlowCustomModelSingle extends DBFlowBaseSingle { 10 | 11 | private final Class mQueryModelClass; 12 | private final ModelQueriable mBaseModelQueriable; 13 | 14 | /** 15 | * Creates a new observable which runs a query and emits the first element in the result set as a CustomModel 16 | * 17 | * @param clazz The table/view model in which the FlowCursorList will contain 18 | * @param baseModelQueriable The query to run 19 | */ 20 | public DBFlowCustomModelSingle(Class clazz, Class queryClazz, ModelQueriable baseModelQueriable) { 21 | super(queryClazz); 22 | mQueryModelClass = clazz; 23 | mBaseModelQueriable = baseModelQueriable; 24 | } 25 | 26 | @Override 27 | public TQueryModel run() { 28 | return mBaseModelQueriable.queryCustomSingle(mQueryModelClass); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowExecuteCompletable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | 4 | import android.support.annotation.Nullable; 5 | 6 | import com.raizlabs.android.dbflow.sql.language.BaseQueriable; 7 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 8 | 9 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.operators.NotifyOfUpdate; 10 | import io.reactivex.Completable; 11 | 12 | /** 13 | * Given an RxSQLite query, executes a statement without a result. 14 | */ 15 | public class DBFlowExecuteCompletable extends DBFlowBaseCompletable { 16 | 17 | private final BaseQueriable mBaseQueriable; 18 | private final DatabaseWrapper mDatabaseWrapper; 19 | 20 | /** 21 | * Creates a new Observable which executes a sql statement against a table 22 | * @param clazz The model class representing the table to execute against 23 | * @param modelQueriable The query to execute 24 | * @param databaseWrapper The database in which the target table resides 25 | */ 26 | public DBFlowExecuteCompletable(Class clazz, BaseQueriable modelQueriable, @Nullable DatabaseWrapper databaseWrapper) { 27 | super(clazz); 28 | mBaseQueriable = modelQueriable; 29 | mDatabaseWrapper = databaseWrapper; 30 | } 31 | 32 | 33 | public Completable notifyOfUpdates(){ 34 | return new NotifyOfUpdate<>(this, mBaseQueriable.getQuery(), getPrimaryModelClass()); 35 | } 36 | 37 | @Override 38 | public void run() { 39 | if(mDatabaseWrapper != null) { 40 | mBaseQueriable.execute(mDatabaseWrapper); 41 | } else { 42 | mBaseQueriable.execute(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowListSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 6 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 7 | 8 | import java.util.List; 9 | 10 | public class DBFlowListSingle extends DBFlowBaseSingle> { 11 | 12 | private final ModelQueriable mBaseModelQueriable; 13 | private final DatabaseWrapper mDatabaseWrapper; 14 | 15 | public DBFlowListSingle(Class clazz, ModelQueriable baseModelQueriable, 16 | @Nullable DatabaseWrapper databaseWrapper) { 17 | super(clazz); 18 | mBaseModelQueriable = baseModelQueriable; 19 | mDatabaseWrapper = databaseWrapper; 20 | } 21 | 22 | 23 | @Override 24 | public List run() { 25 | if (mDatabaseWrapper != null) { 26 | return mBaseModelQueriable.queryList(mDatabaseWrapper); 27 | } else { 28 | return mBaseModelQueriable.queryList(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowMaybe.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.functions.ValueAction; 4 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.operators.DBFlowRestartOnChange; 5 | import io.reactivex.Maybe; 6 | import io.reactivex.Observable; 7 | 8 | /** 9 | * A base observable which provides DBFlow specific operators 10 | * @param The observable result type 11 | */ 12 | public abstract class DBFlowMaybe extends Maybe implements ValueAction { 13 | 14 | /** 15 | * Observes changes on the current table, restarting the query on change and emits the new count 16 | * to any subscribers 17 | * @return An observable which observes any changes in the current table 18 | */ 19 | public final Observable restartOnChange(){ 20 | return new DBFlowRestartOnChange<>(this.toObservable(), new Class[]{getPrimaryModelClass()}, this); 21 | } 22 | 23 | /** 24 | * Observes changes for specific tables, restarting the query on change and emits the new count 25 | * to any subscribers 26 | * @param tableToListen The tables to observe for changes 27 | * @return An observable which observes any changes in the specified tables 28 | */ 29 | public final Observable restartOnChange(Class... tableToListen){ 30 | return new DBFlowRestartOnChange<>(this.toObservable(), tableToListen, this); 31 | } 32 | 33 | /** 34 | * Returns the class for the primary table model effected by this query 35 | * @return The class for the primary table model 36 | */ 37 | protected abstract Class getPrimaryModelClass(); 38 | 39 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowModelMaybe.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | 4 | import android.support.annotation.Nullable; 5 | 6 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 7 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 8 | 9 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.operators.DBFlowBaseMaybe; 10 | 11 | /** 12 | * Given a RxSQLite query, emits the the first element from the query results. 13 | */ 14 | public class DBFlowModelMaybe extends DBFlowBaseMaybe { 15 | private final ModelQueriable mBaseModelQueriable; 16 | private final DatabaseWrapper mDatabaseWrapper; 17 | 18 | /** 19 | * Creates a new observable which runs a query and emits the first element in the result set as a CustomModel 20 | * @param clazz The table/view model in which the FlowCursorList will contain 21 | * @param baseModelQueriable The query to run 22 | */ 23 | public DBFlowModelMaybe(Class clazz, ModelQueriable baseModelQueriable, 24 | @Nullable DatabaseWrapper databaseWrapper) { 25 | super(clazz); 26 | mBaseModelQueriable = baseModelQueriable; 27 | mDatabaseWrapper = databaseWrapper; 28 | } 29 | 30 | 31 | @Override 32 | public TModel run() { 33 | if(mDatabaseWrapper == null) { 34 | return mBaseModelQueriable.querySingle(); 35 | } else { 36 | return mBaseModelQueriable.querySingle(mDatabaseWrapper); 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowModelSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | 4 | import android.support.annotation.Nullable; 5 | 6 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 7 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 8 | 9 | /** 10 | * Given a RxSQLite query, emits the the first element from the query results. 11 | */ 12 | public class DBFlowModelSingle extends DBFlowBaseSingle { 13 | private final ModelQueriable mBaseModelQueriable; 14 | private final DatabaseWrapper mDatabaseWrapper; 15 | 16 | /** 17 | * Creates a new observable which runs a query and emits the first element in the result set as a CustomModel 18 | * @param clazz The table/view model in which the FlowCursorList will contain 19 | * @param baseModelQueriable The query to run 20 | */ 21 | public DBFlowModelSingle(Class clazz, ModelQueriable baseModelQueriable, 22 | @Nullable DatabaseWrapper databaseWrapper) { 23 | super(clazz); 24 | mBaseModelQueriable = baseModelQueriable; 25 | mDatabaseWrapper = databaseWrapper; 26 | } 27 | 28 | 29 | @Override 30 | public TModel run() { 31 | if(mDatabaseWrapper == null) { 32 | return mBaseModelQueriable.querySingle(); 33 | } else { 34 | return mBaseModelQueriable.querySingle(mDatabaseWrapper); 35 | } 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowObservable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.functions.ValueAction; 4 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.operators.DBFlowRestartOnChange; 5 | import io.reactivex.Observable; 6 | 7 | /** 8 | * A base observable which provides DBFlow specific operators 9 | * @param The observable result type 10 | */ 11 | public abstract class DBFlowObservable extends Observable implements ValueAction, DisposableOwner{ 12 | 13 | /** 14 | * Observes changes on the current table, restarting the query on change and emits the new count 15 | * to any subscribers 16 | * @return An observable which observes any changes in the current table 17 | */ 18 | public final Observable restartOnChange(){ 19 | return new DBFlowRestartOnChange<>(this, new Class[]{getPrimaryModelClass()}, this); 20 | } 21 | 22 | /** 23 | * Observes changes for specific tables, restarting the query on change and emits the new count 24 | * to any subscribers 25 | * @param tableToListen The tables to observe for changes 26 | * @return An observable which observes any changes in the specified tables 27 | */ 28 | public final Observable restartOnChange(Class... tableToListen){ 29 | return new DBFlowRestartOnChange<>(this, tableToListen, this); 30 | } 31 | 32 | /** 33 | * Returns the class for the primary table model effected by this query 34 | * @return The class for the primary table model 35 | */ 36 | protected abstract Class getPrimaryModelClass(); 37 | 38 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowQueryListSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | 4 | import com.raizlabs.android.dbflow.list.FlowQueryList; 5 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 6 | 7 | public class DBFlowQueryListSingle extends DBFlowBaseSingle>{ 8 | 9 | private final ModelQueriable mBaseModelQueriable; 10 | 11 | public DBFlowQueryListSingle(Class clazz, ModelQueriable baseModelQueriable) { 12 | super(clazz); 13 | mBaseModelQueriable = baseModelQueriable; 14 | } 15 | 16 | @Override 17 | public FlowQueryList run() { 18 | return mBaseModelQueriable.flowQueryList(); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowResultSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | 4 | import com.raizlabs.android.dbflow.sql.language.CursorResult; 5 | import com.raizlabs.android.dbflow.sql.queriable.ModelQueriable; 6 | 7 | public class DBFlowResultSingle extends DBFlowBaseSingle>{ 8 | private final ModelQueriable mBaseModelQueriable; 9 | 10 | public DBFlowResultSingle(Class clazz, ModelQueriable baseModelQueriable) { 11 | super(clazz); 12 | mBaseModelQueriable = baseModelQueriable; 13 | } 14 | 15 | @Override 16 | public CursorResult run() { 17 | return mBaseModelQueriable.queryResults(); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowSingle.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.functions.ValueAction; 4 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.operators.DBFlowRestartOnChange; 5 | import io.reactivex.Observable; 6 | import io.reactivex.Single; 7 | 8 | /** 9 | * A base observable which provides DBFlow specific operators 10 | * @param The observable result type 11 | */ 12 | public abstract class DBFlowSingle extends Single implements ValueAction { 13 | 14 | /** 15 | * Observes changes on the current table, restarting the query on change and emits the new count 16 | * to any subscribers 17 | * @return An observable which observes any changes in the current table 18 | */ 19 | public final Observable restartOnChange(){ 20 | return new DBFlowRestartOnChange<>(this.toObservable(), new Class[]{getPrimaryModelClass()}, this); 21 | } 22 | 23 | /** 24 | * Observes changes for specific tables, restarting the query on change and emits the new count 25 | * to any subscribers 26 | * @param tableToListen The tables to observe for changes 27 | * @return An observable which observes any changes in the specified tables 28 | */ 29 | public final Observable restartOnChange(Class... tableToListen){ 30 | return new DBFlowRestartOnChange<>(this.toObservable(), tableToListen, this); 31 | } 32 | 33 | /** 34 | * Returns the class for the primary table model effected by this query 35 | * @return The class for the primary table model 36 | */ 37 | protected abstract Class getPrimaryModelClass(); 38 | 39 | } -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DisposableOwner.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | public interface DisposableOwner { 4 | void remove(ObserverDisposable connectionDisposable); 5 | } 6 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/ObserverDisposable.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables; 2 | 3 | import io.reactivex.Observer; 4 | import io.reactivex.disposables.Disposable; 5 | 6 | 7 | public class ObserverDisposable implements Disposable { 8 | 9 | private final Observer mObserver; 10 | private final DisposableOwner mParent; 11 | 12 | public ObserverDisposable(DisposableOwner parent, Observer observer) { 13 | mParent = parent; 14 | mObserver = observer; 15 | } 16 | 17 | private boolean mIsSelfDisposed = false; 18 | 19 | public boolean isDisposed() { 20 | return mIsSelfDisposed; 21 | } 22 | 23 | public void dispose() { 24 | mIsSelfDisposed = true; 25 | mParent.remove(this); 26 | } 27 | 28 | public void onError(Throwable e) { 29 | mObserver.onError(e); 30 | } 31 | 32 | public void onNext(T data) { 33 | mObserver.onNext(data); 34 | } 35 | 36 | public void onComplete() { 37 | mObserver.onComplete(); 38 | dispose(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/functions/ValueAction.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables.functions; 2 | 3 | public interface ValueAction { 4 | T run(); 5 | } 6 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/operators/DBFlowBaseMaybe.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables.operators; 2 | 3 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowMaybe; 4 | import io.reactivex.MaybeObserver; 5 | import io.reactivex.exceptions.Exceptions; 6 | import io.reactivex.internal.disposables.EmptyDisposable; 7 | 8 | public abstract class DBFlowBaseMaybe extends DBFlowMaybe { 9 | 10 | private final Class mModelClazz; 11 | 12 | public DBFlowBaseMaybe(Class clazz) { 13 | mModelClazz = clazz; 14 | } 15 | 16 | @Override 17 | protected Class getPrimaryModelClass() { 18 | return mModelClazz; 19 | } 20 | 21 | @Override 22 | protected void subscribeActual(MaybeObserver observer) { 23 | 24 | observer.onSubscribe(EmptyDisposable.INSTANCE); 25 | try { 26 | Container v = run(); 27 | observer.onSuccess(v); 28 | } catch (Throwable e) { 29 | Exceptions.throwIfFatal(e); 30 | observer.onError(e); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/operators/DBFlowRestartOnChange.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables.operators; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | import com.raizlabs.android.dbflow.config.FlowManager; 6 | import com.raizlabs.android.dbflow.runtime.FlowContentObserver; 7 | import com.raizlabs.android.dbflow.structure.BaseModel; 8 | 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.functions.ValueAction; 13 | import au.com.roadhouse.rxdbflow.rx2.sql.transaction.DbTransaction; 14 | import io.reactivex.Observable; 15 | import io.reactivex.ObservableSource; 16 | import io.reactivex.Observer; 17 | import io.reactivex.disposables.Disposable; 18 | import io.reactivex.internal.fuseable.HasUpstreamObservableSource; 19 | 20 | 21 | public class DBFlowRestartOnChange extends Observable implements HasUpstreamObservableSource { 22 | private final ObservableSource mSource; 23 | private final Class[] mSubscribedClasses; 24 | private final ValueAction mRestartAction; 25 | 26 | public DBFlowRestartOnChange(Observable source, Class[] subscribedClasses, ValueAction restartAction) { 27 | mSource = source; 28 | mSubscribedClasses = subscribedClasses; 29 | mRestartAction = restartAction; 30 | } 31 | 32 | @Override 33 | protected void subscribeActual(Observer observer) { 34 | final RestartOnChangeObserver disposable = new RestartOnChangeObserver(observer, mSubscribedClasses, mRestartAction); 35 | mSource.subscribe(disposable); 36 | } 37 | 38 | @Override 39 | public ObservableSource source() { 40 | return mSource; 41 | } 42 | 43 | private class RestartOnChangeObserver implements Observer, Disposable, DbTransaction.OnDbTransactionListener { 44 | private final Observer mActual; 45 | private final ValueAction mRestartAction; 46 | private List mSubscribedClasses; 47 | private boolean mIsDisposed = false; 48 | private FlowContentObserver mFlowContentObserver = new FlowContentObserver(); 49 | private boolean mIsInTransaction = false; 50 | private boolean mHasPendingChange = false; 51 | 52 | 53 | public RestartOnChangeObserver(Observer observer, Class[] subscribedClasses, ValueAction action) { 54 | mActual = observer; 55 | mSubscribedClasses = Arrays.asList(subscribedClasses); 56 | mRestartAction = action; 57 | DbTransaction.registerDbTransactionListener(mSubscribedClasses.get(0), this); 58 | } 59 | 60 | @Override 61 | public void onSubscribe(Disposable d) { 62 | mActual.onSubscribe(this); 63 | 64 | } 65 | 66 | @Override 67 | public void onError(Throwable e) { 68 | mActual.onError(e); 69 | } 70 | 71 | @Override 72 | public void onComplete() { 73 | //We capture any completes here to avoid disposing 74 | } 75 | 76 | @Override 77 | public void onNext(T o) { 78 | mActual.onNext(o); 79 | 80 | for (int i = 0; i < mSubscribedClasses.size(); i++) { 81 | mFlowContentObserver.registerForContentChanges(FlowManager.getContext(), mSubscribedClasses.get(i)); 82 | } 83 | 84 | mFlowContentObserver.addOnTableChangedListener( 85 | new FlowContentObserver.OnTableChangedListener() { 86 | @Override 87 | public void onTableChanged(@Nullable Class tableChanged, BaseModel.Action action) { 88 | if(isDisposed()){ 89 | return; 90 | } 91 | 92 | if (!mIsInTransaction) { 93 | mActual.onNext(mRestartAction.run()); 94 | } else { 95 | mHasPendingChange = true; 96 | } 97 | } 98 | }); 99 | } 100 | 101 | @Override 102 | public void dispose() { 103 | mFlowContentObserver.unregisterForContentChanges(FlowManager.getContext()); 104 | mIsDisposed = true; 105 | mActual.onComplete(); 106 | DbTransaction.unregisterDbTransactionListener(mSubscribedClasses.get(0), this); 107 | } 108 | 109 | @Override 110 | public boolean isDisposed() { 111 | return mIsDisposed; 112 | } 113 | 114 | @Override 115 | public void onTransactionStarted() { 116 | mIsInTransaction = true; 117 | } 118 | 119 | @Override 120 | public void onTransactionEnded() { 121 | mIsInTransaction = false; 122 | if(mHasPendingChange && !isDisposed()){ 123 | mHasPendingChange = false; 124 | mActual.onNext(mRestartAction.run()); 125 | } 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/operators/NotifyOfUpdate.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.observables.operators; 2 | 3 | import com.raizlabs.android.dbflow.sql.SqlUtils; 4 | import com.raizlabs.android.dbflow.structure.BaseModel; 5 | 6 | import au.com.roadhouse.rxdbflow.rx2.sql.observables.DBFlowBaseCompletable; 7 | import io.reactivex.Completable; 8 | import io.reactivex.CompletableObserver; 9 | import io.reactivex.CompletableSource; 10 | import io.reactivex.disposables.Disposable; 11 | import io.reactivex.internal.fuseable.HasUpstreamCompletableSource; 12 | 13 | 14 | public class NotifyOfUpdate extends DBFlowBaseCompletable implements HasUpstreamCompletableSource { 15 | 16 | private final Completable mSource; 17 | private final String mQuery; 18 | 19 | public NotifyOfUpdate(Completable source, String query, Class clazz) { 20 | super(clazz); 21 | mSource = source; 22 | mQuery = query; 23 | } 24 | 25 | @Override 26 | public CompletableSource source() { 27 | return mSource; 28 | } 29 | 30 | @Override 31 | protected void subscribeActual(CompletableObserver observer) { 32 | mSource.subscribe(new CompletableObservable(observer, mQuery, getPrimaryModelClass())); 33 | super.subscribeActual(observer); 34 | } 35 | 36 | @Override 37 | public void run() { 38 | if(mQuery.toLowerCase().contains("delete ")){ 39 | SqlUtils.notifyModelChanged(getPrimaryModelClass(), BaseModel.Action.DELETE, null); 40 | } else if(mQuery.toLowerCase().contains("update ")){ 41 | SqlUtils.notifyModelChanged(getPrimaryModelClass(), BaseModel.Action.UPDATE, null); 42 | } 43 | } 44 | 45 | private class CompletableObservable implements CompletableObserver, Disposable { 46 | 47 | private final CompletableObserver mActual; 48 | private final String mQuery; 49 | private final Class mModelClazz; 50 | private boolean mIsDisposed = false; 51 | 52 | public CompletableObservable(CompletableObserver observer, String query, Class modelClazz) { 53 | mActual = observer; 54 | mQuery = query; 55 | mModelClazz = modelClazz; 56 | } 57 | 58 | @Override 59 | public void onSubscribe(Disposable d) { 60 | mActual.onSubscribe(this); 61 | } 62 | 63 | @Override 64 | public void onError(Throwable e) { 65 | mActual.onError(e); 66 | } 67 | 68 | @Override 69 | public void onComplete() { 70 | if(mQuery.toLowerCase().contains("delete ")){ 71 | SqlUtils.notifyModelChanged(mModelClazz, BaseModel.Action.DELETE, null); 72 | } else if(mQuery.toLowerCase().contains("update ")){ 73 | SqlUtils.notifyModelChanged(mModelClazz, BaseModel.Action.UPDATE, null); 74 | } 75 | 76 | mActual.onComplete(); 77 | } 78 | 79 | @Override 80 | public void dispose() { 81 | mIsDisposed = true; 82 | } 83 | 84 | @Override 85 | public boolean isDisposed() { 86 | return mIsDisposed; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/transaction/DbTransaction.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.transaction; 2 | 3 | import android.util.Log; 4 | 5 | import com.raizlabs.android.dbflow.config.FlowManager; 6 | 7 | import java.util.ArrayList; 8 | import java.util.HashMap; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | /** 13 | * Provides database transaction control 14 | */ 15 | public class DbTransaction { 16 | 17 | private final Class mClass; 18 | private static Map> sTransactionListenerMap = new HashMap<>(); 19 | 20 | public DbTransaction(Class tableClass){ 21 | mClass = tableClass; 22 | } 23 | 24 | public static void registerDbTransactionListener(Class tableClass, OnDbTransactionListener listener){ 25 | String databaseName = FlowManager.getDatabaseForTable(tableClass).getDatabaseName(); 26 | if(sTransactionListenerMap.containsKey(databaseName)){ 27 | sTransactionListenerMap.get(databaseName).add(listener); 28 | } else { 29 | List listenerList = new ArrayList<>(); 30 | listenerList.add(listener); 31 | sTransactionListenerMap.put(databaseName, listenerList); 32 | } 33 | } 34 | 35 | public static void unregisterDbTransactionListener(Class tableClass, OnDbTransactionListener listener) { 36 | String databaseName = FlowManager.getDatabaseForTable(tableClass).getDatabaseName(); 37 | if(sTransactionListenerMap.containsKey(databaseName)){ 38 | sTransactionListenerMap.get(databaseName).remove(databaseName); 39 | if(sTransactionListenerMap.get(databaseName).size() == 0){ 40 | sTransactionListenerMap.remove(databaseName); 41 | } 42 | } else { 43 | Log.e("DbTransaction", "Attempting to unregister a listener which was not registered"); 44 | 45 | } 46 | } 47 | 48 | public void beginTransaction(){ 49 | FlowManager.getDatabase(mClass).getWritableDatabase().beginTransaction(); 50 | String databaseName = FlowManager.getDatabaseForTable(mClass).getDatabaseName(); 51 | if(sTransactionListenerMap.containsKey(databaseName)){ 52 | List listenerList = sTransactionListenerMap.get(databaseName); 53 | for(int i = 0; i < listenerList.size(); i++){ 54 | listenerList.get(i).onTransactionStarted(); 55 | } 56 | } 57 | } 58 | 59 | public void setTransactionSuccessful(){ 60 | FlowManager.getDatabase(mClass).getWritableDatabase().setTransactionSuccessful(); 61 | } 62 | 63 | public void endTransaction() { 64 | FlowManager.getDatabase(mClass).getWritableDatabase().endTransaction(); 65 | String databaseName = FlowManager.getDatabaseForTable(mClass).getDatabaseName(); 66 | if(sTransactionListenerMap.containsKey(databaseName)){ 67 | List listenerList = sTransactionListenerMap.get(databaseName); 68 | for(int i = 0; i < listenerList.size(); i++){ 69 | listenerList.get(i).onTransactionEnded(); 70 | } 71 | } 72 | } 73 | 74 | public interface OnDbTransactionListener { 75 | void onTransactionStarted(); 76 | void onTransactionEnded(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/transaction/RxGenericTransactionBlock.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.sql.transaction; 2 | 3 | import android.database.sqlite.SQLiteException; 4 | import android.support.annotation.NonNull; 5 | 6 | import com.raizlabs.android.dbflow.config.FlowManager; 7 | import com.raizlabs.android.dbflow.structure.Model; 8 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | import au.com.roadhouse.rxdbflow.rx2.sql.language.NullValue; 14 | import io.reactivex.Observable; 15 | import io.reactivex.Observer; 16 | 17 | 18 | /** 19 | *

Provides a transaction block that runs a series of database operations on a single database within 20 | * a single transaction. There is no limitation on what type of operations, that can run within a single transaction.

21 | *

Creation of a ModelOperationTransaction is via the use of the inner builder.

22 | */ 23 | public class RxGenericTransactionBlock extends Observable { 24 | 25 | private final List mTransactionProcessList; 26 | private final DatabaseWrapper mDatabaseWrapper; 27 | 28 | private RxGenericTransactionBlock(Builder builder) { 29 | mTransactionProcessList = builder.mTransactionProcessList; 30 | mDatabaseWrapper = builder.mDatabaseWrapper; 31 | } 32 | 33 | @Override 34 | protected void subscribeActual(Observer observer) { 35 | try { 36 | mDatabaseWrapper.beginTransaction(); 37 | 38 | for (int i = 0; i < mTransactionProcessList.size(); i++) { 39 | if(!mTransactionProcessList.get(i).onProcess(mDatabaseWrapper)){ 40 | throw new SQLiteException("A transaction process item failed"); 41 | } 42 | } 43 | observer.onNext(new NullValue()); 44 | observer.onComplete(); 45 | 46 | 47 | mDatabaseWrapper.setTransactionSuccessful(); 48 | } catch (Exception e){ 49 | observer.onError(e); 50 | } finally{ 51 | mDatabaseWrapper.endTransaction(); 52 | } 53 | } 54 | 55 | /** 56 | * A build which creates a new generic transaction block. 57 | */ 58 | public static class Builder { 59 | 60 | private final DatabaseWrapper mDatabaseWrapper; 61 | private List mTransactionProcessList; 62 | 63 | /** 64 | * Creates a new builder with a specific database wrapper. 65 | * @param databaseWrapper The database wrapper to run the transaction against 66 | */ 67 | public Builder(@NonNull DatabaseWrapper databaseWrapper){ 68 | mTransactionProcessList = new ArrayList<>(); 69 | mDatabaseWrapper = databaseWrapper; 70 | } 71 | 72 | /** 73 | * Creates a new builder, using a Model class to derive the target database. The class passed 74 | * to the parameter does not restrict the tables on which the database operations are performed. 75 | * As long as the operations run on a single database. 76 | * @param clazz The model class used to derive the target database. 77 | */ 78 | public Builder(@NonNull Class clazz){ 79 | mTransactionProcessList = new ArrayList<>(); 80 | mDatabaseWrapper = FlowManager.getDatabaseForTable(clazz).getWritableDatabase(); 81 | } 82 | 83 | /** 84 | * Adds a new transaction operation to be performed within a transaction block. 85 | * @param operation The operation to perform 86 | * @return An instance of the current builder object. 87 | */ 88 | public Builder addOperation(@NonNull TransactionOperation operation){ 89 | mTransactionProcessList.add(operation); 90 | return this; 91 | } 92 | 93 | /** 94 | * Builds a new GenericTransactionBlock observable which will run all database operations within 95 | * a single transaction once it's subscribed to. 96 | * @return A new GenericTransactionBlock containing all database operations 97 | */ 98 | public RxGenericTransactionBlock build(){ 99 | return new RxGenericTransactionBlock(this); 100 | } 101 | } 102 | 103 | /** 104 | * Represents a single transaction operation to perform within a transaction block. 105 | */ 106 | public interface TransactionOperation { 107 | boolean onProcess(DatabaseWrapper databaseWrapper); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/structure/RxBaseModel.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.structure; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | import com.raizlabs.android.dbflow.structure.BaseModel; 6 | import com.raizlabs.android.dbflow.structure.InvalidDBConfiguration; 7 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 8 | 9 | import au.com.roadhouse.rxdbflow.rx2.RxDbFlow; 10 | import io.reactivex.Single; 11 | 12 | /** 13 | * A DBFlow BaseModel implementation which provides Singles for saving, updating, inserting, and 14 | * deleting operations. 15 | */ 16 | @SuppressWarnings("unchecked") 17 | public class RxBaseModel extends BaseModel implements RxModifications { 18 | 19 | private transient RxModelAdapter mModelAdapter; 20 | 21 | /** 22 | * Returns an Single for saving the object in the database 23 | * @return An Single 24 | */ 25 | public Single saveAsSingle(){ 26 | return getRxModelAdapter().saveAsSingle(this); 27 | } 28 | 29 | /** 30 | * Returns an Single for saving the object in the database 31 | * @param databaseWrapper The database wrapper for the database holding the table 32 | * @return An Single 33 | */ 34 | @SuppressWarnings("unchecked") 35 | public Single saveAsSingle(@Nullable final DatabaseWrapper databaseWrapper){ 36 | return getRxModelAdapter().saveAsSingle(this, databaseWrapper); 37 | } 38 | 39 | /** 40 | * Returns an Single for inserting the object in the database 41 | * @return An Single 42 | */ 43 | public Single insertAsSingle(){ 44 | return getRxModelAdapter().insertAsSingle(this); 45 | } 46 | 47 | /** 48 | * Returns an Single for inserting the object in the database 49 | * @param databaseWrapper The database wrapper for the database holding the table 50 | * @return An Single 51 | */ 52 | public Single insertAsSingle(@Nullable final DatabaseWrapper databaseWrapper){ 53 | return getRxModelAdapter().insertAsSingle(this, databaseWrapper); 54 | } 55 | 56 | /** 57 | * Returns an Single for deleting the object from the database 58 | * @return An Single 59 | */ 60 | public Single deleteAsSingle(){ 61 | return getRxModelAdapter().deleteAsSingle(this); 62 | } 63 | 64 | /** 65 | * Returns an Single for deleting the object from the database 66 | * @param databaseWrapper The database wrapper for the database holding the table 67 | * @return An Single 68 | */ 69 | public Single deleteAsSingle(@Nullable final DatabaseWrapper databaseWrapper){ 70 | return getRxModelAdapter().deleteAsSingle(this, databaseWrapper); 71 | } 72 | 73 | /** 74 | * Returns an Single for update the object in the database 75 | * @return An Single 76 | */ 77 | public Single updateAsSingle(){ 78 | return getRxModelAdapter().updateAsSingle(this); 79 | } 80 | 81 | /** 82 | * Returns an Single for update in object from the database 83 | * @param databaseWrapper The database wrapper for the database holding the table 84 | * @return An Single 85 | */ 86 | public Single updateAsSingle(@Nullable final DatabaseWrapper databaseWrapper){ 87 | return getRxModelAdapter().updateAsSingle(this, databaseWrapper); 88 | } 89 | 90 | /** 91 | * Returns an Single which will refresh the model's data based on the primary key when subscribed. 92 | * @return An Single 93 | */ 94 | public Single loadAsSingle(){ 95 | return getRxModelAdapter().loadAsSingle(this); 96 | } 97 | 98 | /** 99 | * Returns an Single which will refresh the model's data based on the primary key when subscribed. 100 | * @param databaseWrapper The database wrapper for the database holding the table 101 | * @return An Single 102 | */ 103 | public Single loadAsSingle(@Nullable final DatabaseWrapper databaseWrapper){ 104 | return getRxModelAdapter().loadAsSingle(this, databaseWrapper); 105 | } 106 | 107 | /** 108 | * @return The associated {@link RxModelAdapter}. The {@link RxDbFlow} 109 | * may throw a {@link InvalidDBConfiguration} for this call if this class 110 | * is not associated with a table, so be careful when using this method. 111 | */ 112 | public RxModelAdapter getRxModelAdapter() { 113 | if (mModelAdapter == null) { 114 | mModelAdapter = RxDbFlow.getModelAdapter(getClass()); 115 | } 116 | return mModelAdapter; 117 | } 118 | } 119 | 120 | 121 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/structure/RxModifications.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.rx2.structure; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 6 | 7 | import io.reactivex.Single; 8 | 9 | 10 | /** 11 | * Provides a common interface for any 12 | * @param 13 | */ 14 | public interface RxModifications { 15 | 16 | Single saveAsSingle(); 17 | 18 | Single saveAsSingle(@Nullable DatabaseWrapper databaseWrapper); 19 | 20 | Single insertAsSingle(); 21 | 22 | Single insertAsSingle(@Nullable DatabaseWrapper databaseWrapper); 23 | 24 | Single deleteAsSingle(); 25 | 26 | Single deleteAsSingle(@Nullable DatabaseWrapper databaseWrapper); 27 | 28 | Single updateAsSingle(); 29 | 30 | Single updateAsSingle(@Nullable DatabaseWrapper databaseWrapper); 31 | } 32 | -------------------------------------------------------------------------------- /RxDbFlow-Rx2/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rxdbflow 3 | 4 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.2.0' 9 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | jcenter() 20 | maven { url "https://www.jitpack.io" } 21 | } 22 | } 23 | 24 | task clean(type: Delete) { 25 | delete rootProject.buildDir 26 | } 27 | -------------------------------------------------------------------------------- /exampleRx1/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /exampleRx1/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.neenbedankt.android-apt' 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion "25.0.0" 7 | defaultConfig { 8 | applicationId "au.com.roadhouse.rxdbflow.exampleRx1" 9 | minSdkVersion 14 10 | targetSdkVersion 25 11 | versionCode 2 12 | versionName "1.0.1" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | 22 | } 23 | 24 | 25 | task(type: Zip, "zipTestReport") { 26 | from("${rootDir.absolutePath}/example/build/reports/androidTests/connected/") 27 | archiveName 'test_report_example.zip' 28 | destinationDir file("${rootDir.absolutePath}/app/build/reports/") 29 | } 30 | 31 | configure(zipTestReport) { 32 | group = 'verification' 33 | description = 'Create a test report archive' 34 | } 35 | 36 | task(type: Zip, "zipLintReport") { 37 | from "${rootDir.absolutePath}/example/build/outputs/" 38 | include "lint-results-debug.html" 39 | include "lint-results-debug_files/*" 40 | archiveName 'lint_report_example.zip' 41 | destinationDir file("${rootDir.absolutePath}/example/build/reports/") 42 | } 43 | 44 | configure(zipLintReport) { 45 | group = 'verification' 46 | description = 'Create a lint report archive' 47 | } 48 | 49 | 50 | dependencies { 51 | compile fileTree(dir: 'libs', include: ['*.jar']) 52 | compile 'io.reactivex:rxjava:1.1.4' 53 | compile 'io.reactivex:rxandroid:1.2.1' 54 | compile 'com.android.support:appcompat-v7:25.3.1' 55 | compile 'com.github.Raizlabs.DBFlow:dbflow-core:4.0.0-beta7' 56 | compile 'com.github.Raizlabs.DBFlow:dbflow:4.0.0-beta7' 57 | compile 'com.android.support:design:25.3.1' 58 | apt 'com.github.Raizlabs.DBFlow:dbflow-processor:4.0.0-beta7' 59 | compile project(path: ':RxDbFlow-Rx1') 60 | testCompile 'junit:junit:4.12' 61 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 62 | exclude group: 'com.android.support', module: 'support-annotations' 63 | }) 64 | } 65 | -------------------------------------------------------------------------------- /exampleRx1/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/dwaynehoy/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /exampleRx1/src/androidTest/java/au/com/roadhouse/rxdbflow/ModifyingSqlTest.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow; 2 | 3 | import android.support.test.runner.AndroidJUnit4; 4 | 5 | import org.junit.runner.RunWith; 6 | 7 | import rx.subscriptions.CompositeSubscription; 8 | 9 | @RunWith(AndroidJUnit4.class) 10 | public class ModifyingSqlTest { 11 | private CompositeSubscription mDataSubscribers; 12 | 13 | private boolean mIsRunningASyncTask = false; 14 | private boolean mWasATaskSyncSuccessful = false; 15 | 16 | // @Before 17 | // public void setup() { 18 | // 19 | // mDataSubscribers = new CompositeSubscription(); 20 | // SQLite.delete(TestModel.class).execute(); 21 | // 22 | // TestModel modelOne = new TestModel(); 23 | // modelOne.setFirstName("Bob"); 24 | // modelOne.setLastName("Marley"); 25 | // modelOne.save(); 26 | // 27 | // TestModel modelTwo = new TestModel(); 28 | // modelTwo.setFirstName("John"); 29 | // modelTwo.setLastName("Doe"); 30 | // modelTwo.save(); 31 | // 32 | // TestModel modelThree = new TestModel(); 33 | // modelThree.setFirstName("Bob"); 34 | // modelThree.setLastName("Don"); 35 | // modelThree.save(); 36 | // } 37 | // 38 | // @Test 39 | // public void testUpdates() { 40 | // RxSQLite.update(TestModel.class) 41 | // .set(TestModel_Table.first_name.eq("UPDATED")) 42 | // .where(TestModel_Table.first_name.eq("Bob")) 43 | // .asExecuteObservable() 44 | // .publishTableUpdates() 45 | // .subscribe(new Action1() { 46 | // @Override 47 | // public void call(Void aVoid) { 48 | // SQLite.select() 49 | // .from(TestModel.class) 50 | // .where(TestModel_Table.first_name.eq("UPDATED")) 51 | // .queryList(); 52 | // } 53 | // }); 54 | // } 55 | // 56 | // @Test 57 | // public void testDeletes() { 58 | // 59 | // } 60 | // 61 | // @Test 62 | // public void testInserts() { 63 | // 64 | // } 65 | } 66 | -------------------------------------------------------------------------------- /exampleRx1/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /exampleRx1/src/main/java/au/com/roadhouse/rxdbflow/exampleRx1/ExampleApplication.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.exampleRx1; 2 | 3 | import android.app.Application; 4 | 5 | import com.raizlabs.android.dbflow.config.FlowConfig; 6 | import com.raizlabs.android.dbflow.config.FlowManager; 7 | 8 | public class ExampleApplication extends Application { 9 | @Override 10 | public void onCreate() { 11 | FlowManager.init(new FlowConfig.Builder(this).build()); 12 | super.onCreate(); 13 | } 14 | 15 | @Override 16 | public void onTerminate() { 17 | FlowManager.destroy(); 18 | super.onTerminate(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /exampleRx1/src/main/java/au/com/roadhouse/rxdbflow/exampleRx1/model/ExampleDatabase.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.example.model; 2 | 3 | import com.raizlabs.android.dbflow.annotation.ConflictAction; 4 | import com.raizlabs.android.dbflow.annotation.Database; 5 | 6 | 7 | @Database(name = ExampleDatabase.NAME, version = ExampleDatabase.VERSION, insertConflict = ConflictAction.REPLACE) 8 | public class ExampleDatabase { 9 | public static final String NAME = "example"; 10 | public static final int VERSION = 2; 11 | } 12 | -------------------------------------------------------------------------------- /exampleRx1/src/main/java/au/com/roadhouse/rxdbflow/exampleRx1/model/InheritanceTestModel.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.exampleRx1.model; 2 | 3 | 4 | import com.raizlabs.android.dbflow.annotation.Column; 5 | import com.raizlabs.android.dbflow.annotation.ConflictAction; 6 | import com.raizlabs.android.dbflow.annotation.PrimaryKey; 7 | import com.raizlabs.android.dbflow.annotation.Table; 8 | 9 | import au.com.roadhouse.rxdbflow.rx1.structure.RxBaseModel; 10 | 11 | @Table(database = au.com.roadhouse.rxdbflow.example.model.ExampleDatabase.class, name = InheritanceTestModel.NAME, insertConflict = ConflictAction.REPLACE) 12 | public class InheritanceTestModel extends RxBaseModel { 13 | 14 | public static final String NAME = "test-inheritance"; 15 | 16 | @PrimaryKey(autoincrement = true) 17 | @Column(name = "id", getterName = "getId", setterName = "setId") 18 | private long id; 19 | 20 | @Column(name = "first_name", getterName = "getFirstName", setterName = "setFirstName") 21 | private String firstName; 22 | 23 | @Column(name = "last_name", getterName = "getLastName", setterName = "setLastName") 24 | private String lastName; 25 | 26 | public long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getFirstName() { 35 | return firstName; 36 | } 37 | 38 | public void setFirstName(String firstName) { 39 | this.firstName = firstName; 40 | } 41 | 42 | public String getLastName() { 43 | return lastName; 44 | } 45 | 46 | public void setLastName(String lastName) { 47 | this.lastName = lastName; 48 | } 49 | } -------------------------------------------------------------------------------- /exampleRx1/src/main/java/au/com/roadhouse/rxdbflow/exampleRx1/model/TestModel.java: -------------------------------------------------------------------------------- 1 | package au.com.roadhouse.rxdbflow.exampleRx1.model; 2 | 3 | 4 | import com.raizlabs.android.dbflow.annotation.Column; 5 | import com.raizlabs.android.dbflow.annotation.ConflictAction; 6 | import com.raizlabs.android.dbflow.annotation.PrimaryKey; 7 | import com.raizlabs.android.dbflow.annotation.Table; 8 | 9 | @Table(database = au.com.roadhouse.rxdbflow.example.model.ExampleDatabase.class, name = TestModel.NAME, insertConflict = ConflictAction.REPLACE) 10 | public class TestModel { 11 | 12 | public static final String NAME = "test"; 13 | 14 | @PrimaryKey(autoincrement = true) 15 | @Column(name = "id", getterName = "getId", setterName = "setId") 16 | private long id; 17 | 18 | @Column(name = "first_name", getterName = "getFirstName", setterName = "setFirstName") 19 | private String firstName; 20 | 21 | @Column(name = "last_name", getterName = "getLastName", setterName = "setLastName") 22 | private String lastName; 23 | 24 | public long getId() { 25 | return id; 26 | } 27 | 28 | public void setId(long id) { 29 | this.id = id; 30 | } 31 | 32 | public String getFirstName() { 33 | return firstName; 34 | } 35 | 36 | public void setFirstName(String firstName) { 37 | this.firstName = firstName; 38 | } 39 | 40 | public String getLastName() { 41 | return lastName; 42 | } 43 | 44 | public void setLastName(String lastName) { 45 | this.lastName = lastName; 46 | } 47 | } -------------------------------------------------------------------------------- /exampleRx1/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | 14 | 20 | 21 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /exampleRx1/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /exampleRx1/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roadhouse-dev/RxDbflow/750deeb94c4418ac07e563e7361ce6d3050d6acc/exampleRx1/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /exampleRx1/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roadhouse-dev/RxDbflow/750deeb94c4418ac07e563e7361ce6d3050d6acc/exampleRx1/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /exampleRx1/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roadhouse-dev/RxDbflow/750deeb94c4418ac07e563e7361ce6d3050d6acc/exampleRx1/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /exampleRx1/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roadhouse-dev/RxDbflow/750deeb94c4418ac07e563e7361ce6d3050d6acc/exampleRx1/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /exampleRx1/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roadhouse-dev/RxDbflow/750deeb94c4418ac07e563e7361ce6d3050d6acc/exampleRx1/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /exampleRx1/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /exampleRx1/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /exampleRx1/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /exampleRx1/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 16dp 6 | 7 | -------------------------------------------------------------------------------- /exampleRx1/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RxDBFlow 3 | Rx1 RxDXbFlow Example 4 | 5 | -------------------------------------------------------------------------------- /exampleRx1/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 | 9 | 10 | -------------------------------------------------------------------------------- /exampleRx2/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /exampleRx2/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /exampleRx2/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 16dp 6 | 7 | -------------------------------------------------------------------------------- /exampleRx2/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RxDBFlow 3 | Rx2 RxDXbFlow Example 4 | 5 | -------------------------------------------------------------------------------- /exampleRx2/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |