├── Library ├── gradle.properties ├── src │ ├── androidTest │ │ ├── assets │ │ │ └── Chinook.sqlite │ │ └── java │ │ │ └── com │ │ │ ├── bingzer │ │ │ └── android │ │ │ │ └── dbv │ │ │ │ ├── PersonList.java │ │ │ │ ├── OrmPerson.java │ │ │ │ ├── PreloadedDatabaseTest.java │ │ │ │ ├── CreateIndexTest.java │ │ │ │ ├── ConfigTest.java │ │ │ │ ├── AlterTest.java │ │ │ │ ├── IdNamingConventionTest.java │ │ │ │ ├── UtilsTest.java │ │ │ │ ├── Person.java │ │ │ │ ├── ForeignKeyTest.java │ │ │ │ ├── ForeignKeyTest2.java │ │ │ │ └── ReadOnlyDbTest.java │ │ │ └── example │ │ │ └── TestUsage.java │ └── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── bingzer │ │ └── android │ │ └── dbv │ │ ├── queries │ │ ├── Max.java │ │ ├── Min.java │ │ ├── Sum.java │ │ ├── Total.java │ │ ├── Average.java │ │ ├── Delete.java │ │ ├── Insert.java │ │ ├── IQuery.java │ │ ├── ISequence.java │ │ ├── InsertInto.java │ │ ├── InnerJoin.java │ │ ├── LeftJoin.java │ │ ├── OuterJoin.java │ │ ├── Union.java │ │ ├── Having.java │ │ ├── IFunction.java │ │ ├── GroupBy.java │ │ ├── Update.java │ │ └── Select.java │ │ ├── internal │ │ ├── DeleteImpl.java │ │ ├── DropImpl.java │ │ ├── ContentSet.java │ │ ├── QueryImpl.java │ │ ├── MaxImpl.java │ │ ├── MinImpl.java │ │ ├── SumImpl.java │ │ ├── InsertImpl.java │ │ ├── TotalImpl.java │ │ ├── AverageImpl.java │ │ ├── LeftJoinImpl.java │ │ ├── InnerJoinImpl.java │ │ ├── OuterJoinImpl.java │ │ ├── View.java │ │ ├── InsertIntoImpl.java │ │ ├── TransactionImpl.java │ │ ├── Config.java │ │ ├── UpdateImpl.java │ │ └── FunctionImpl.java │ │ ├── contracts │ │ ├── PrimaryKeyIdentifier.java │ │ ├── Alterable.java │ │ ├── SqlExecutable.java │ │ ├── Pagination.java │ │ ├── Droppable.java │ │ ├── Groupable.java │ │ ├── RawQueryable.java │ │ ├── Countable.java │ │ ├── Unionable.java │ │ ├── Tangible.java │ │ ├── SelectIdentifiable.java │ │ ├── CursorEnumerable.java │ │ ├── EntitySelectable.java │ │ ├── Selectable.java │ │ ├── ColumnSelectable.java │ │ ├── Distinguishable.java │ │ ├── Insertable.java │ │ ├── Deletable.java │ │ ├── Joinable.java │ │ └── Updatable.java │ │ ├── IEnvironment.java │ │ ├── utils │ │ ├── CursorUtils.java │ │ ├── CollectionUtils.java │ │ └── DelegateUtils.java │ │ ├── IEntityList.java │ │ ├── Environment.java │ │ ├── IBaseEntity.java │ │ └── DbQuery.java └── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── Sample ├── src │ ├── main │ │ ├── assets │ │ │ └── Chinook.sqlite │ │ ├── res │ │ │ ├── drawable-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── menu │ │ │ │ └── main.xml │ │ │ ├── values-v11 │ │ │ │ └── styles.xml │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ ├── values-v14 │ │ │ │ └── styles.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── bingzer │ │ │ │ └── android │ │ │ │ └── dbv │ │ │ │ └── sample │ │ │ │ ├── activities │ │ │ │ ├── AlbumDetail.java │ │ │ │ └── MainActivity.java │ │ │ │ ├── ChinookDataProvider.java │ │ │ │ └── SampleApp.java │ │ └── AndroidManifest.xml │ └── androidTest │ │ └── java │ │ └── com │ │ └── bingzer │ │ └── android │ │ └── dbv │ │ ├── providers │ │ ├── FakeTest.java │ │ └── BasicDataProviderTest.java │ │ └── content │ │ ├── WordList.java │ │ ├── Word.java │ │ └── ResolverTest.java ├── README.md └── build.gradle ├── Extensions ├── DataProvider │ ├── src │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ └── java │ │ │ └── com │ │ │ └── bingzer │ │ │ └── android │ │ │ └── dbv │ │ │ └── providers │ │ │ ├── ReadOnlyDataProvider.java │ │ │ └── IDataProvider.java │ ├── gradle.properties │ ├── README.md │ └── build.gradle ├── ContentQuery │ ├── gradle.properties │ ├── src │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ └── java │ │ │ └── com │ │ │ └── bingzer │ │ │ └── android │ │ │ └── dbv │ │ │ ├── content │ │ │ ├── contracts │ │ │ │ ├── IResolver.java │ │ │ │ ├── IStrictResolver.java │ │ │ │ ├── IContentQueryConfig.java │ │ │ │ ├── IBaseResolver.java │ │ │ │ └── StrictSelectable.java │ │ │ ├── utils │ │ │ │ └── UriUtils.java │ │ │ ├── TestUsage.java │ │ │ └── ContentQuery.java │ │ │ └── internal │ │ │ ├── ContentDeleteImpl.java │ │ │ ├── ContentInsertImpl.java │ │ │ ├── ContentInsertIntoImpl.java │ │ │ ├── ContentUpdateImpl.java │ │ │ ├── ContentConfig.java │ │ │ └── ContentStrictSelectImpl.java │ ├── build.gradle │ └── README.md └── README.md ├── .travis.yml ├── .gitattributes ├── CHANGELOG.md ├── wait-for-emulator ├── gradle.properties ├── settings.gradle ├── gradlew.bat └── README.md /Library/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=DbQuery 2 | POM_ARTIFACT_ID=dbquery 3 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingzer/DbQuery/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Sample/src/main/assets/Chinook.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingzer/DbQuery/HEAD/Sample/src/main/assets/Chinook.sqlite -------------------------------------------------------------------------------- /Library/src/androidTest/assets/Chinook.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingzer/DbQuery/HEAD/Library/src/androidTest/assets/Chinook.sqlite -------------------------------------------------------------------------------- /Extensions/DataProvider/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Sample/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingzer/DbQuery/HEAD/Sample/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Sample/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingzer/DbQuery/HEAD/Sample/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Sample/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingzer/DbQuery/HEAD/Sample/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Extensions/ContentQuery/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_DESCRIPTION=Allows DbQuery style of query while querying data from ContentProvider 2 | POM_NAME=content-query 3 | POM_ARTIFACT_ID=content-query 4 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /Library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Extensions/DataProvider/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_DESCRIPTION=Easy wrapper for ContentProvider that exposes data from IDatabase object to ContentProvider 2 | POM_NAME=data-provider 3 | POM_ARTIFACT_ID=data-provider 4 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jun 27 16:00:29 EDT 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /Sample/src/androidTest/java/com/bingzer/android/dbv/providers/FakeTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.providers; 2 | 3 | import android.test.AndroidTestCase; 4 | 5 | /** 6 | * Created by ricky on 6/8/2016. 7 | */ 8 | public class FakeTest extends AndroidTestCase { 9 | public void testFake(){ 10 | assertTrue(true); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/PersonList.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * Created by Ricky on 8/9/13. 7 | */ 8 | public class PersonList extends LinkedList implements IEntityList{ 9 | @Override 10 | public Person newEntity() { 11 | return new Person(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Sample/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | -------------------------------------------------------------------------------- /Sample/src/main/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | DbQuery-Sample 5 | Hello world! 6 | 7 | Start Test 8 | Show All 9 | With Albums 10 | 11 | 12 | -------------------------------------------------------------------------------- /Sample/src/androidTest/java/com/bingzer/android/dbv/content/WordList.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.content; 2 | 3 | import com.bingzer.android.dbv.IEntityList; 4 | 5 | import java.util.LinkedList; 6 | 7 | /** 8 | * Created by Ricky Tobing on 8/23/13. 9 | */ 10 | public class WordList extends LinkedList implements IEntityList{ 11 | 12 | @Override 13 | public Word newEntity() { 14 | return new Word(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Sample/src/main/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Sample/README.md: -------------------------------------------------------------------------------- 1 | #Contributing? 2 | 3 | If you're interested implementing a sample app, that will be awesome. 4 | I'm trying to use Chinook database sample found here 5 | [http://chinookdatabase.codeplex.com/wikipage?title=Chinook_Schema&referringTitle=Documentation](http://chinookdatabase.codeplex.com/wikipage?title=Chinook_Schema&referringTitle=Documentation) 6 | 7 | The `db` file is located in 'assets' folder. see [`SampleApp.java`](https://github.com/bingzer/DbQuery/blob/master/Sample/src/main/java/com/bingzer/android/dbv/sample/SampleApp.java) 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | android: 3 | components: 4 | - tools 5 | - platform-tools 6 | - build-tools-23.0.3 7 | - android-23 8 | # - sys-img-armeabi-v7a-android-23 9 | 10 | before_install: 11 | - chmod +x ./gradlew 12 | 13 | # before_script: 14 | # - echo no | android create avd --force -n test -t android-23 --abi armeabi-v7a 15 | # - emulator -avd test -no-skin -no-audio -no-window & 16 | # - chmod u+x ./wait-for-emulator 17 | # - ./wait-for-emulator 18 | # - adb shell input keyevent 82 & 19 | 20 | script: 21 | - ./gradlew build -------------------------------------------------------------------------------- /Extensions/DataProvider/README.md: -------------------------------------------------------------------------------- 1 | `DataProvider` 2 | ``` java 3 | public class ChinookDataProvider extends BasicDataProvider { 4 | 5 | @Override 6 | public IDatabase openDatabase() { 7 | File dbFile = ... 8 | IDatabase db = DbQuery.getDatabase("Chinook.sqlitex"); 9 | db.open(1, dbFile.getAbsolutePath(), new SQLiteBuilder() { 10 | ... 11 | }); 12 | 13 | return db; 14 | } 15 | 16 | @Override 17 | public String getAuthority() { 18 | return "com.bingzer.android.dbv.data.test"; 19 | } 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /Extensions/README.md: -------------------------------------------------------------------------------- 1 | # EXTENSIONS for `DbQuery` 2 | 3 | * [`ContentQuery`](https://github.com/bingzer/DbQuery/tree/master/Extensions/ContentQuery) 4 | Allows `DbQuery` style of query while querying data from [`ContentProvider`](http://developer.android.com/guide/topics/providers/content-providers.html) 5 | * [`DataProvider`](https://github.com/bingzer/DbQuery/tree/master/Extensions/DataProvider) 6 | Easy wrapper for ContentProvider that exposes data from `IDatabase` object to `ContentProvider` 7 | * [`Test`] 8 | Unit Test for `ContentQuery` and `DataProvider`. This test project uses `UserDictionary`'s `ContentProvider` to test 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | VERSION 2.1.0 (07/26/2014) 2 | ============= 3 | * Built-in supports for ORM (`BaseEntity` + `IEnvironment`) 4 | * Iterables changed to Collection 5 | * Smarter error handler 6 | * Various bug fixes 7 | 8 | VERSION 2.0.0 (04/30/2014) 9 | ============= 10 | * Various bug fixes 11 | * Read-only database: `db.getConfig().setReadOnly(true)` 12 | * `UPDATE` and `INSERT` Refactoring 13 | * Column `Id` is now `Long` type (was `Integer`) 14 | * Support function with condition: (i.e: `db.get("Products").avg("Prices", "ProductName LIKE ?", "Key%")`) 15 | * Major code and API refactoring 16 | 17 | VERSION 1.0.0 (12/01/2013) 18 | ============= 19 | Initial Release 20 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/build.gradle: -------------------------------------------------------------------------------- 1 | project.ext.set("archivesBaseName", "content-query"); 2 | 3 | apply plugin: 'com.android.library' 4 | 5 | dependencies { 6 | compile project(':Library') 7 | } 8 | 9 | android { 10 | compileSdkVersion Integer.parseInt(project.VERSION_SDK) 11 | buildToolsVersion project.VERSION_TOOLS 12 | defaultConfig { 13 | minSdkVersion Integer.parseInt(project.VERSION_SDK_MIN) 14 | targetSdkVersion Integer.parseInt(project.VERSION_SDK_TARGET) 15 | versionCode Integer.parseInt(project.VERSION_CODE) 16 | versionName project.VERSION_NAME 17 | } 18 | 19 | } 20 | 21 | apply from: 'https://raw.github.com/bingzer/gradle-mvn-push/master/gradle-mvn-push.gradle' 22 | -------------------------------------------------------------------------------- /Extensions/DataProvider/build.gradle: -------------------------------------------------------------------------------- 1 | project.ext.set("archivesBaseName", "data-provider"); 2 | 3 | apply plugin: 'com.android.library' 4 | 5 | dependencies { 6 | compile project(':Library') 7 | } 8 | 9 | android { 10 | compileSdkVersion Integer.parseInt(project.VERSION_SDK) 11 | buildToolsVersion project.VERSION_TOOLS 12 | 13 | defaultConfig { 14 | minSdkVersion Integer.parseInt(project.VERSION_SDK_MIN) 15 | targetSdkVersion Integer.parseInt(project.VERSION_SDK_TARGET) 16 | versionCode Integer.parseInt(project.VERSION_CODE) 17 | versionName project.VERSION_NAME 18 | } 19 | } 20 | 21 | apply from: 'https://raw.github.com/bingzer/gradle-mvn-push/master/gradle-mvn-push.gradle' 22 | -------------------------------------------------------------------------------- /wait-for-emulator: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Originally written by Ralf Kistner , but placed in the public domain 4 | 5 | set +e 6 | 7 | bootanim="" 8 | failcounter=0 9 | timeout_in_sec=360 10 | 11 | until [[ "$bootanim" =~ "stopped" ]]; do 12 | bootanim=`adb -e shell getprop init.svc.bootanim 2>&1 &` 13 | if [[ "$bootanim" =~ "device not found" || "$bootanim" =~ "device offline" 14 | || "$bootanim" =~ "running" ]]; then 15 | let "failcounter += 1" 16 | echo "Waiting for emulator to start" 17 | if [[ $failcounter -gt timeout_in_sec ]]; then 18 | echo "Timeout ($timeout_in_sec seconds) reached; failed to start emulator" 19 | exit 1 20 | fi 21 | fi 22 | sleep 1 23 | done 24 | 25 | echo "Emulator is ready" -------------------------------------------------------------------------------- /Sample/build.gradle: -------------------------------------------------------------------------------- 1 | project.ext.set("archivesBaseName", "dbquery-sample"); 2 | 3 | apply plugin: 'com.android.application' 4 | 5 | dependencies { 6 | compile project(':Library') 7 | compile project(':Extensions:ContentQuery') 8 | compile project(':Extensions:DataProvider') 9 | } 10 | 11 | android { 12 | compileSdkVersion Integer.parseInt(project.VERSION_SDK) 13 | buildToolsVersion project.VERSION_TOOLS 14 | 15 | defaultConfig { 16 | minSdkVersion Integer.parseInt(project.VERSION_SDK_MIN) 17 | targetSdkVersion Integer.parseInt(project.VERSION_SDK_TARGET) 18 | versionCode Integer.parseInt(project.VERSION_CODE) 19 | versionName project.VERSION_NAME 20 | } 21 | 22 | lintOptions { 23 | abortOnError false 24 | } 25 | } -------------------------------------------------------------------------------- /Sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | MAVEN_RELEASE=true 2 | 3 | VERSION_TOOLS=23.0.3 4 | VERSION_SDK=23 5 | VERSION_SDK_MIN=7 6 | VERSION_SDK_TARGET=19 7 | 8 | VERSION_NAME=2.2.0-SNAPSHOT 9 | VERSION_CODE=6 10 | GROUP=com.bingzer.android.dbv 11 | 12 | POM_DESCRIPTION=A fluent SQLite Query API for Android 13 | POM_URL=https://github.com/bingzer/dbquery 14 | POM_SCM_URL=https://github.com/bingzer/dbquery 15 | POM_SCM_CONNECTION=scm:git@github.com:bingzer/dbquery.git 16 | POM_SCM_DEV_CONNECTION=scm:git@github.com:bingzer/dbquery.git 17 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 18 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 19 | POM_LICENCE_DIST=repo 20 | POM_DEVELOPER_ID=bingzer 21 | POM_DEVELOPER_NAME=Ricky Tobing 22 | 23 | POM_NAME=DbQuery 24 | POM_ARTIFACT_ID=dbquery 25 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Max.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Represents MAX 20 | */ 21 | public interface Max extends IFunction { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Min.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Represents MIN 20 | */ 21 | public interface Min extends IFunction { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Sum.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Represents SUM 20 | */ 21 | public interface Sum extends IFunction { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Library/build.gradle: -------------------------------------------------------------------------------- 1 | project.ext.set("archivesBaseName", "dbquery"); 2 | 3 | apply plugin: 'com.android.library' 4 | 5 | android { 6 | compileSdkVersion Integer.parseInt(project.VERSION_SDK) 7 | buildToolsVersion project.VERSION_TOOLS 8 | 9 | defaultConfig { 10 | minSdkVersion Integer.parseInt(project.VERSION_SDK_MIN) 11 | targetSdkVersion Integer.parseInt(project.VERSION_SDK_TARGET) 12 | versionCode Integer.parseInt(project.VERSION_CODE) 13 | versionName project.VERSION_NAME 14 | } 15 | } 16 | 17 | dependencies{ 18 | // -- Mockito 19 | androidTestCompile 'org.mockito:mockito-core:1.9.5' 20 | androidTestCompile 'com.google.dexmaker:dexmaker:1.1' 21 | androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1' 22 | } 23 | 24 | apply from: 'https://raw.github.com/bingzer/gradle-mvn-push/master/gradle-mvn-push.gradle' 25 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Total.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Represents Total 20 | */ 21 | public interface Total extends IFunction { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Average.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Represents AVG functions. 20 | */ 21 | public interface Average extends IFunction{ 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/DeleteImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.Delete; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | class DeleteImpl extends QueryImpl implements Delete { 24 | } 25 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/DropImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.IQuery; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | class DropImpl extends QueryImpl implements IQuery { 24 | } 25 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * You may comment out libraries that you wish to exclude 19 | */ 20 | 21 | // Main DbQuery Library 22 | include ':Library' 23 | 24 | // Extensions 25 | include ':Extensions:ContentQuery' 26 | include ':Extensions:DataProvider' 27 | 28 | // sample application 29 | include ':Sample' -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/PrimaryKeyIdentifier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.contracts; 17 | 18 | /** 19 | * Must be able to identify primary key column 20 | */ 21 | public interface PrimaryKeyIdentifier { 22 | 23 | /** 24 | * Returns the Column Id string for this table 25 | * @return the string 26 | */ 27 | String getPrimaryKeyColumn(); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/ContentSet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import android.content.ContentValues; 19 | 20 | import com.bingzer.android.dbv.queries.IQuery; 21 | 22 | /** 23 | * Created by Ricky on 4/26/2014. 24 | */ 25 | interface ContentSet { 26 | 27 | void onContentValuesSet(E query, ContentValues contentValues); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/content/contracts/IResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.content.contracts; 17 | 18 | import com.bingzer.android.dbv.content.contracts.Selectable; 19 | import com.bingzer.android.dbv.content.contracts.IBaseResolver; 20 | 21 | /** 22 | * Created by Ricky Tobing on 8/20/13. 23 | */ 24 | public interface IResolver extends IBaseResolver, Selectable { 25 | } 26 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/IEnvironment.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv; 17 | 18 | /** 19 | * Represents a contract interface for an {@code Environment} . 20 | * Environment is a virtual place where an {@code IBaseEntity} 21 | * will live in. 22 | */ 23 | public interface IEnvironment { 24 | 25 | /** 26 | * Returns the database 27 | */ 28 | IDatabase getDatabase(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/QueryImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.IQuery; 19 | 20 | class QueryImpl implements IQuery { 21 | protected T value; 22 | 23 | public void setValue(T value){ 24 | this.value = value; 25 | } 26 | 27 | @Override 28 | public T query(){ 29 | return value; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/MaxImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.Max; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | class MaxImpl extends FunctionImpl implements Max { 24 | public MaxImpl(String tableName, String columnName, String condition){ 25 | super("MAX", tableName, columnName, condition); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/MinImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.Min; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | class MinImpl extends FunctionImpl implements Min { 24 | public MinImpl(String tableName, String columnName, String condition){ 25 | super("MIN", tableName, columnName, condition); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/SumImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.Sum; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | class SumImpl extends FunctionImpl implements Sum { 24 | public SumImpl(String tableName, String columnName, String condition){ 25 | super("SUM", tableName, columnName, condition); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Delete.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Represents a delete statement 20 | *

21 | * Find a complete Wiki and documentation here:
22 | * https://github.com/bingzer/DbQuery/wiki 23 | *

24 | */ 25 | public interface Delete extends IQuery { 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/InsertImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.Insert; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | class InsertImpl extends QueryImpl implements Insert { 24 | public InsertImpl(){ 25 | // default if any error. 26 | // -1 should be returned 27 | this.value = -1l; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/content/contracts/IStrictResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.content.contracts; 17 | 18 | import com.bingzer.android.dbv.content.contracts.StrictSelectable; 19 | import com.bingzer.android.dbv.content.contracts.IBaseResolver; 20 | 21 | /** 22 | * Created by Ricky Tobing on 8/23/13. 23 | */ 24 | public interface IStrictResolver extends IBaseResolver, StrictSelectable { 25 | } 26 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/TotalImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.Total; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | class TotalImpl extends FunctionImpl implements Total { 24 | public TotalImpl(String tableName, String columnName, String condition){ 25 | super("TOTAL", tableName, columnName, condition); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/internal/ContentDeleteImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | /** 19 | * Created by Ricky on 8/20/13. 20 | */ 21 | class ContentDeleteImpl extends DeleteImpl { 22 | 23 | public ContentDeleteImpl val(int value){ 24 | this.value = value; 25 | return this; 26 | } 27 | 28 | public int value(){ 29 | return value; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/AverageImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.Average; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | class AverageImpl extends FunctionImpl implements Average { 24 | public AverageImpl(String tableName, String columnName, String condition){ 25 | super("AVG", tableName, columnName, condition); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Insert.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Represents an insert statement 20 | *

21 | * Find a complete Wiki and documentation here:
22 | * https://github.com/bingzer/DbQuery/wiki 23 | *

24 | * 25 | * @see InsertInto 26 | */ 27 | public interface Insert extends IQuery { 28 | } 29 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/LeftJoinImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.LeftJoin; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | abstract class LeftJoinImpl extends JoinImpl implements LeftJoin { 24 | public LeftJoinImpl(Table table, String tableNameToJoin, String onClause) { 25 | super(table, "LEFT JOIN", tableNameToJoin, onClause); 26 | } 27 | } -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/InnerJoinImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.InnerJoin; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | abstract class InnerJoinImpl extends JoinImpl implements InnerJoin { 24 | public InnerJoinImpl(Table table, String tableNameToJoin, String onClause) { 25 | super(table, "INNER JOIN", tableNameToJoin, onClause); 26 | } 27 | } -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/OuterJoinImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.OuterJoin; 19 | 20 | /** 21 | * Created by Ricky on 4/26/2014. 22 | */ 23 | abstract class OuterJoinImpl extends JoinImpl implements OuterJoin { 24 | public OuterJoinImpl(Table table, String tableNameToJoin, String onClause) { 25 | super(table, "OUTER JOIN", tableNameToJoin, onClause); 26 | } 27 | } -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Alterable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.contracts; 17 | 18 | import com.bingzer.android.dbv.ITable; 19 | 20 | /** 21 | * Alter the content 22 | */ 23 | public interface Alterable { 24 | 25 | /** 26 | * Alter the content of a table (i.e: rename columns, modify column definition, etc...) 27 | * @return {@link com.bingzer.android.dbv.ITable.Alter} object 28 | */ 29 | ITable.Alter alter(); 30 | } 31 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/SqlExecutable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | /** 20 | * Created by Ricky Tobing on 7/23/13. 21 | */ 22 | public interface SqlExecutable { 23 | 24 | /** 25 | * Execute sql 26 | * @param sql sql to execute 27 | */ 28 | void execSql(String sql); 29 | 30 | /** 31 | * Execute sql 32 | * @param sql sql 33 | * @param args arguments 34 | */ 35 | void execSql(String sql, Object... args); 36 | } 37 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/content/contracts/IContentQueryConfig.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.content.contracts; 2 | 3 | import com.bingzer.android.dbv.IConfig; 4 | 5 | public interface IContentQueryConfig extends IConfig { 6 | 7 | /** 8 | * Sets the default projections (columns) unless if otherwise 9 | * specified with {@link com.bingzer.android.dbv.queries.Select#columns(String...)} 10 | * in a select statement. 11 | * By default the default projections is 12 | * {@link com.bingzer.android.dbv.IConfig#getIdNamingConvention()} 13 | * @param columns the columns to set 14 | */ 15 | void setDefaultProjections(String... columns); 16 | 17 | /** 18 | * Returns the default projections 19 | * @return projections 20 | */ 21 | String[] getDefaultProjections(); 22 | 23 | /** 24 | * Sets the default Authority 25 | * @param authority authority to set 26 | */ 27 | void setDefaultAuthority(String authority); 28 | 29 | /** 30 | * Returns the authority 31 | * @return Authority 32 | */ 33 | String getDefaultAuthority(); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/IQuery.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | *

20 | * Find a complete Wiki and documentation here:
21 | * https://github.com/bingzer/DbQuery/wiki 22 | *

23 | * Created by Ricky Tobing on 7/16/13. 24 | */ 25 | public interface IQuery { 26 | 27 | /** 28 | * Build the sql and return a cursor 29 | * @return T 30 | */ 31 | T query(); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Pagination.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.contracts; 17 | 18 | import com.bingzer.android.dbv.queries.Paging; 19 | 20 | /** 21 | * Represents a pagination on a query 22 | * 23 | * Created by Ricky on 8/11/13. 24 | */ 25 | public interface Pagination { 26 | 27 | /** 28 | * Added paging to a query. 29 | * 30 | * @see com.bingzer.android.dbv.queries.Paging 31 | * @param row the row number 32 | * @return paging 33 | */ 34 | Paging paging(int row); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/ISequence.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Provides a pseudo-read-only sequence of a 20 | * given type. 21 | * @param the type 22 | */ 23 | public interface ISequence { 24 | 25 | /** 26 | * Provides the next sequence of type T. The enumerator/provider 27 | * will continuously enumerate until the it finishes enumeration 28 | * or until the method return false. 29 | * 30 | * @param sequence the sequence 31 | */ 32 | boolean next(T sequence); 33 | } 34 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/InsertInto.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Represents an InsertInto statement 20 | *

21 | * Find a complete Wiki and documentation here:
22 | * https://github.com/bingzer/DbQuery/wiki 23 | *

24 | */ 25 | public interface InsertInto { 26 | 27 | /** 28 | * Values to set 29 | * @return {@link com.bingzer.android.dbv.queries.IQuery} 30 | */ 31 | IQuery val(Object... values); 32 | } 33 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Droppable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import com.bingzer.android.dbv.queries.IQuery; 20 | 21 | /** 22 | * Created by Ricky on 7/18/13. 23 | */ 24 | public interface Droppable { 25 | 26 | /** 27 | * Drop this table. 28 | *

29 | * Sample Code: To drop a table 30 | *

31 |      * db.from("TableName").drop().query();
32 |      * 
33 |      * 
34 | * 35 | * @return true if success, false if otherwise. 36 | */ 37 | IQuery drop(); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Sample/src/main/java/com/bingzer/android/dbv/sample/activities/AlbumDetail.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.sample.activities; 2 | 3 | import android.app.ListActivity; 4 | import android.database.Cursor; 5 | import android.os.Bundle; 6 | import android.widget.SimpleCursorAdapter; 7 | 8 | import com.bingzer.android.dbv.DbQuery; 9 | import com.bingzer.android.dbv.IDatabase; 10 | 11 | public class AlbumDetail extends ListActivity { 12 | 13 | @SuppressWarnings("deprecation") 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | 18 | long artistId = getIntent().getExtras().getLong("ArtistId"); 19 | 20 | IDatabase db = DbQuery.getDatabase("Chinook"); 21 | 22 | Cursor cursor = db.from("Album").select("ArtistId = ?", artistId).columns("*", "rowid as _id").query(); 23 | SimpleCursorAdapter adapter = 24 | new SimpleCursorAdapter(this, 25 | android.R.layout.simple_list_item_1, 26 | cursor, 27 | new String[]{"Title"}, 28 | new int[]{ android.R.id.text1}); 29 | 30 | // Bind to our new adapter. 31 | setListAdapter(adapter); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sample/src/main/java/com/bingzer/android/dbv/sample/ChinookDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.sample; 2 | 3 | import android.content.Context; 4 | 5 | import com.bingzer.android.dbv.DbQuery; 6 | import com.bingzer.android.dbv.IDatabase; 7 | import com.bingzer.android.dbv.SQLiteBuilder; 8 | import com.bingzer.android.dbv.providers.DataProvider; 9 | 10 | import java.io.File; 11 | import java.io.IOException; 12 | 13 | public class ChinookDataProvider extends DataProvider { 14 | 15 | @Override 16 | public IDatabase openDatabase() { 17 | IDatabase db; 18 | File dbFile = new File(getContext().getFilesDir(), "Chinook.sqlite"); 19 | 20 | if(!dbFile.exists()){ 21 | try{ 22 | IOHelper.copyFile(getContext().getResources().getAssets().open("Chinook.sqlite"), dbFile); 23 | } 24 | catch (IOException e){ 25 | throw new Error(e); 26 | } 27 | } 28 | 29 | db = DbQuery.getDatabase("Chinook"); 30 | db.open(1, dbFile.getAbsolutePath(), new SQLiteBuilder.WithoutModeling(getContext())); 31 | 32 | return db; 33 | } 34 | 35 | @Override 36 | public String getAuthority() { 37 | return "com.bingzer.android.dbv.providers.sample"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/README.md: -------------------------------------------------------------------------------- 1 | ContentQuery 2 | ============== 3 | 4 | `ContentQuery` allows querying data from `ContentProvider` using `DbQuery` style 5 | 6 | # Concept 7 | ``` java 8 | ... 9 | String uri = ... 10 | IResolver resolver = ContentQuery.resolve(uri, getApplicationContext()); 11 | Cursor cursor = resolver.select("Name = ?", "John Doe") 12 | .columns("_id", "Name", "DisplayName") 13 | .query(); 14 | ... 15 | ``` 16 | 17 | # Documentation 18 | See [https://github.com/bingzer/DbQuery/wiki/ContentQuery](https://github.com/bingzer/DbQuery/wiki/ContentQuery) 19 | 20 | # License 21 | ``` java 22 | /** 23 | * Copyright 2013 Ricky Tobing 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | ``` 38 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/content/utils/UriUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.content.utils; 17 | 18 | import android.net.Uri; 19 | 20 | /** 21 | * Created by Ricky on 8/21/13. 22 | */ 23 | public class UriUtils { 24 | 25 | public static long parseIdFromUri(Uri uri){ 26 | String uriString = uri.toString(); 27 | String valueString = uriString.substring(uriString.lastIndexOf("/") + 1, uriString.length()); 28 | try{ 29 | return Long.parseLong(valueString); 30 | } 31 | catch (NumberFormatException e){ 32 | return -1; 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Groupable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import com.bingzer.android.dbv.queries.GroupBy; 20 | 21 | /** 22 | * Created by Ricky Tobing on 8/15/13. 23 | */ 24 | public interface Groupable { 25 | 26 | /** 27 | * Group by columns. 28 | * It's possible to put function as the column's name: 29 | *
30 |      * groupBy("SUM(Price) AS TotalPrice", "Name",...)
31 |      * 
32 | * @param columns column names. 33 | * @return GroupBy object 34 | * @see com.bingzer.android.dbv.queries.GroupBy 35 | */ 36 | GroupBy groupBy(String... columns); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Sample/src/main/java/com/bingzer/android/dbv/sample/SampleApp.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.sample; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | import com.bingzer.android.dbv.DbQuery; 7 | import com.bingzer.android.dbv.IDatabase; 8 | import com.bingzer.android.dbv.SQLiteBuilder; 9 | 10 | import java.io.File; 11 | import java.io.IOException; 12 | 13 | /** 14 | * Created by Ricky on 8/17/13. 15 | */ 16 | public class SampleApp extends Application { 17 | 18 | File dbFile = null; 19 | 20 | @Override 21 | public void onCreate() { 22 | super.onCreate(); 23 | 24 | extractDbFile(); 25 | IDatabase db = DbQuery.getDatabase("Chinook"); 26 | db.getConfig().setReadOnly(true); 27 | db.getConfig().setAppendTableNameForId(true); 28 | db.open(1, dbFile.getAbsolutePath(), new SQLiteBuilder.WithoutModeling(this)); 29 | } 30 | 31 | private void extractDbFile(){ 32 | // transfer the db file to sd card 33 | dbFile = new File(getBaseContext().getFilesDir(), "Chinook.sqlite"); 34 | if(!dbFile.exists()){ 35 | try{ 36 | IOHelper.copyFile(getResources().getAssets().open("Chinook.sqlite"), dbFile); 37 | } 38 | catch (IOException e){ 39 | throw new Error(e); 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/InnerJoin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | import com.bingzer.android.dbv.contracts.Distinguishable; 19 | import com.bingzer.android.dbv.contracts.Joinable; 20 | import com.bingzer.android.dbv.contracts.Selectable; 21 | 22 | /** 23 | * Represents an inner join statement 24 | *

25 | * Find a complete Wiki and documentation here:
26 | * https://github.com/bingzer/DbQuery/wiki 27 | *

28 | * 29 | * @see OuterJoin 30 | */ 31 | public interface InnerJoin extends 32 | Joinable, Joinable.Inner, Joinable.Outer, Joinable.Left, Selectable, Select, Distinguishable { 33 | 34 | } -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/LeftJoin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | import com.bingzer.android.dbv.contracts.Distinguishable; 19 | import com.bingzer.android.dbv.contracts.Joinable; 20 | import com.bingzer.android.dbv.contracts.Selectable; 21 | 22 | /** 23 | * Represents an inner join statement 24 | *

25 | * Find a complete Wiki and documentation here:
26 | * https://github.com/bingzer/DbQuery/wiki 27 | *

28 | * 29 | * @see OuterJoin 30 | */ 31 | public interface LeftJoin extends 32 | Joinable, Joinable.Inner, Joinable.Outer, Joinable.Left, Selectable, Select, Distinguishable { 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/OuterJoin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | import com.bingzer.android.dbv.contracts.Distinguishable; 19 | import com.bingzer.android.dbv.contracts.Joinable; 20 | import com.bingzer.android.dbv.contracts.Selectable; 21 | 22 | /** 23 | * Represents an outer join statement 24 | *

25 | * Find a complete Wiki and documentation here:
26 | * https://github.com/bingzer/DbQuery/wiki 27 | *

28 | * 29 | * @see com.bingzer.android.dbv.queries.InnerJoin 30 | */ 31 | public interface OuterJoin extends 32 | Joinable, Joinable.Inner, Joinable.Outer, Joinable.Left, Selectable, Select, Distinguishable { 33 | 34 | } -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Union.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | import android.database.Cursor; 19 | 20 | import com.bingzer.android.dbv.contracts.ColumnSelectable; 21 | import com.bingzer.android.dbv.contracts.EntitySelectable; 22 | import com.bingzer.android.dbv.contracts.Selectable; 23 | 24 | /** 25 | * Represents a union 26 | *

27 | * Find a complete Wiki and documentation here:
28 | * https://github.com/bingzer/DbQuery/wiki 29 | *

30 | */ 31 | public interface Union extends IQuery, 32 | EntitySelectable, ColumnSelectable, 33 | Selectable { 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/internal/ContentInsertImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import android.net.Uri; 19 | 20 | import com.bingzer.android.dbv.content.utils.UriUtils; 21 | 22 | /** 23 | * Created by Ricky on 8/20/13. 24 | */ 25 | class ContentInsertImpl extends com.bingzer.android.dbv.internal.InsertImpl { 26 | 27 | Uri uri; 28 | 29 | public ContentInsertImpl setUri(Uri value){ 30 | this.uri = value; 31 | this.value = UriUtils.parseIdFromUri(uri); 32 | return this; 33 | } 34 | 35 | @Override 36 | public Long query(){ 37 | return UriUtils.parseIdFromUri(uri); 38 | } 39 | 40 | @Override 41 | public String toString(){ 42 | return uri.toString(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/RawQueryable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import android.database.Cursor; 20 | 21 | import com.bingzer.android.dbv.queries.IQuery; 22 | 23 | /** 24 | * Created by Ricky Tobing on 7/18/13. 25 | */ 26 | public interface RawQueryable { 27 | 28 | /** 29 | * Runs raw sql. Call query() to from the cursor. 30 | * @param sql sql string to raw 31 | * @return {@link IQuery} 32 | */ 33 | IQuery raw(String sql); 34 | 35 | /** 36 | * Runs raw sql. Call query() to from the cursor. 37 | * @param sql sql string to raw 38 | * @param args arguments 39 | * @return {@link IQuery} 40 | */ 41 | IQuery raw(String sql, Object... args); 42 | } 43 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/example/TestUsage.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import android.content.Context; 4 | 5 | import com.bingzer.android.dbv.DbQuery; 6 | import com.bingzer.android.dbv.Delegate; 7 | import com.bingzer.android.dbv.Environment; 8 | import com.bingzer.android.dbv.IDatabase; 9 | import com.bingzer.android.dbv.IEntity; 10 | import com.bingzer.android.dbv.IEnvironment; 11 | import com.bingzer.android.dbv.SQLiteBuilder; 12 | 13 | @SuppressWarnings("ALL") 14 | public class TestUsage { 15 | 16 | private void init(){ 17 | IDatabase db = null; 18 | IEnvironment customEnvironment = new Environment(db); 19 | 20 | db.open(1, new SQLiteBuilder(customEnvironment) { 21 | @Override 22 | public Context getContext() { 23 | return null; 24 | } 25 | 26 | @Override 27 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 28 | 29 | } 30 | }); 31 | db.begin(new IDatabase.Batch() { 32 | @Override 33 | public void exec(IDatabase database) { 34 | 35 | } 36 | }); 37 | 38 | db.from("").select(); 39 | } 40 | 41 | public class EntityTest implements IEntity { 42 | 43 | @Override 44 | public long getId() { 45 | return 0; 46 | } 47 | 48 | @Override 49 | public void map(Mapper mapper) { 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/content/TestUsage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.content; 17 | 18 | import android.content.Context; 19 | 20 | import com.bingzer.android.dbv.DbQuery; 21 | import com.bingzer.android.dbv.IDatabase; 22 | import com.bingzer.android.dbv.content.contracts.IResolver; 23 | import com.bingzer.android.dbv.content.contracts.IStrictResolver; 24 | 25 | /** 26 | * This will be removed 27 | * Created by Ricky Tobing on 8/20/13. 28 | */ 29 | class TestUsage { 30 | String uri = ""; 31 | Context context = null; 32 | 33 | void test(){ 34 | 35 | IDatabase db = DbQuery.getDatabase(""); 36 | 37 | IResolver resolver = ContentQuery.resolve(uri, context); 38 | 39 | IStrictResolver strictResolver = ContentQuery.strictlyResolve(uri, context); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Countable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | /** 20 | * Created by Ricky Tobing on 7/18/13. 21 | */ 22 | public interface Countable { 23 | 24 | /** 25 | * Returns the count of the specified condition 26 | * @param condition the condition 27 | * @return the count 28 | */ 29 | int count(String condition); 30 | 31 | /** 32 | * Returns the count of row from the whereClause 33 | * (condition) 34 | * @param whereClause whereClause 35 | * @param whereArgs arguments 36 | * @return the count 37 | */ 38 | int count(String whereClause, Object... whereArgs); 39 | 40 | /** 41 | * Returns the total row available in this table 42 | * @return the count of all rows 43 | */ 44 | int count(); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/View.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.IView; 19 | import com.bingzer.android.dbv.queries.IQuery; 20 | 21 | /** 22 | * Created by Ricky Tobing on 8/19/13. 23 | */ 24 | class View extends Table implements IView { 25 | public View (Database db, String name){ 26 | super(db, name); 27 | } 28 | 29 | @Override 30 | public IQuery drop() { 31 | db.enforceReadOnly(); 32 | 33 | DropImpl query = new DropImpl(); 34 | try{ 35 | db.execSql("DROP VIEW " + getName()); 36 | query.setValue(true); 37 | } 38 | catch (Exception e){ 39 | query.setValue(false); 40 | } 41 | 42 | if(query.query()) db.removeTable(this); 43 | return query; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 29 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Unionable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import com.bingzer.android.dbv.queries.Select; 20 | import com.bingzer.android.dbv.queries.Union; 21 | 22 | /** 23 | * Created by Ricky Tobing on 8/16/13. 24 | */ 25 | public interface Unionable { 26 | 27 | /** 28 | * Union with other SELECT statement 29 | * @param select {@link com.bingzer.android.dbv.queries.Select} object 30 | * @return {@link com.bingzer.android.dbv.queries.Union} object 31 | */ 32 | Union union(Select select); 33 | 34 | /** 35 | * Union All with other SELECT statement 36 | * @param select {@link com.bingzer.android.dbv.queries.Select} object 37 | * @return {@link com.bingzer.android.dbv.queries.Union} object 38 | */ 39 | Union unionAll(Select select); 40 | } 41 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Having.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | import android.database.Cursor; 19 | 20 | import com.bingzer.android.dbv.contracts.ColumnSelectable; 21 | import com.bingzer.android.dbv.contracts.CursorEnumerable; 22 | import com.bingzer.android.dbv.contracts.EntitySelectable; 23 | import com.bingzer.android.dbv.contracts.Pagination; 24 | 25 | /** 26 | * Represents HAVING 27 | *

28 | * Find a complete Wiki and documentation here:
29 | * https://github.com/bingzer/DbQuery/wiki 30 | *

31 | * 32 | * @see com.bingzer.android.dbv.queries.GroupBy 33 | */ 34 | public interface Having extends IQuery, 35 | EntitySelectable, ColumnSelectable, 36 | Pagination, CursorEnumerable { 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Tangible.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.contracts; 17 | 18 | /** 19 | * Has something. 20 | */ 21 | public interface Tangible { 22 | 23 | /** 24 | * Check to see if this table has row add the specified condition 25 | * @param condition any condition 26 | * @return true if it returns any row false otherwise 27 | */ 28 | boolean has(String condition); 29 | 30 | /** 31 | * has row add id 32 | * @param id id 33 | * @return true if it returns any row false otherwise 34 | */ 35 | boolean has(long id); 36 | 37 | /** 38 | * Check to see if this table has row add the specified clause and condition 39 | * @param whereClause whereClause 40 | * @param whereArgs arguments 41 | * @return true if it returns any row false otherwise 42 | */ 43 | boolean has(String whereClause, Object... whereArgs); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/SelectIdentifiable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.contracts; 17 | 18 | /** 19 | * Select and returns Id. 20 | * A table must follow {@link com.bingzer.android.dbv.IConfig#getIdNamingConvention()} 21 | * to make this work 22 | * 23 | * Created by Ricky Tobing on 7/20/13. 24 | * 25 | * @see com.bingzer.android.dbv.IConfig#getIdNamingConvention() 26 | */ 27 | public interface SelectIdentifiable { 28 | 29 | /** 30 | * Returns id with specified condition 31 | * @param condition the condition 32 | * @return the id that matches the condition 33 | */ 34 | long selectId(String condition); 35 | 36 | /** 37 | * Returns id with specified condition 38 | * @param whereClause 'where' clause 39 | * @param args arguments 40 | * @return the id that matches the condition 41 | */ 42 | long selectId(String whereClause, Object... args); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Sample/src/androidTest/java/com/bingzer/android/dbv/content/Word.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.content; 2 | 3 | import com.bingzer.android.dbv.Delegate; 4 | import com.bingzer.android.dbv.IEntity; 5 | 6 | /** 7 | * Created by Ricky Tobing on 8/23/13. 8 | */ 9 | public class Word implements IEntity{ 10 | private long id; 11 | private String word; 12 | 13 | Word(){ 14 | this(null); 15 | } 16 | 17 | Word(String word){ 18 | this(-1, word); 19 | } 20 | 21 | Word(long id, String word){ 22 | this.id = id; 23 | this.word = word; 24 | } 25 | 26 | void setId(long id) { 27 | this.id = id; 28 | } 29 | 30 | String getWord() { 31 | return word; 32 | } 33 | 34 | void setWord(String word) { 35 | this.word = word; 36 | } 37 | 38 | @Override 39 | public long getId() { 40 | return id; 41 | } 42 | 43 | @Override 44 | public void map(Mapper mapper) { 45 | mapper.mapId(new Delegate.TypeLong() { 46 | @Override 47 | public void set(Long value) { 48 | setId(value); 49 | } 50 | 51 | @Override 52 | public Long get() { 53 | return getId(); 54 | } 55 | }); 56 | 57 | mapper.map("word", new Delegate.TypeString(){ 58 | 59 | @Override 60 | public void set(String value) { 61 | setWord(value); 62 | } 63 | 64 | @Override 65 | public String get() { 66 | return getWord(); 67 | } 68 | }); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/IFunction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | /** 19 | * Created by Ricky Tobing on 7/20/13. 20 | */ 21 | public interface IFunction { 22 | 23 | /** 24 | * Returns value as int 25 | * @return value as int 26 | */ 27 | int asInt(); 28 | 29 | /** 30 | * Returns value as long 31 | * @return value as long 32 | */ 33 | long asLong(); 34 | 35 | /** 36 | * Returns value as float 37 | * @return value as float 38 | */ 39 | float asFloat(); 40 | 41 | /** 42 | * Returns value as double 43 | * @return value as double 44 | */ 45 | double asDouble(); 46 | 47 | /** 48 | * Returns value as String 49 | * @return value as String 50 | */ 51 | String asString(); 52 | 53 | /** 54 | * Returns the 'raw' value 55 | * @return raw value 56 | */ 57 | Object value(); 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/content/contracts/IBaseResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.content.contracts; 17 | 18 | 19 | import android.net.Uri; 20 | 21 | import com.bingzer.android.dbv.contracts.PrimaryKeyIdentifier; 22 | import com.bingzer.android.dbv.contracts.Countable; 23 | import com.bingzer.android.dbv.contracts.Deletable; 24 | import com.bingzer.android.dbv.contracts.Insertable; 25 | import com.bingzer.android.dbv.contracts.SelectIdentifiable; 26 | import com.bingzer.android.dbv.contracts.Tangible; 27 | import com.bingzer.android.dbv.contracts.Updatable; 28 | 29 | /** 30 | * Created by Ricky Tobing on 8/23/13. 31 | */ 32 | public interface IBaseResolver extends 33 | PrimaryKeyIdentifier, 34 | SelectIdentifiable, Insertable, Updatable, Deletable, Tangible, Countable { 35 | 36 | /** 37 | * Returns the URI 38 | * @return the URI 39 | */ 40 | Uri getUri(); 41 | 42 | /** 43 | * Returns the default config 44 | * @return the config 45 | */ 46 | IContentQueryConfig getContentConfig(); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/CursorEnumerable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */package com.bingzer.android.dbv.contracts; 16 | 17 | import android.database.Cursor; 18 | 19 | import com.bingzer.android.dbv.queries.ISequence; 20 | 21 | /** 22 | * Cursor enumerable. 23 | */ 24 | public interface CursorEnumerable { 25 | 26 | /** 27 | * A convenient method iterate through cursor. 28 | * The provider will call this method as long as 29 | * cursor.moveNext() is true. Cursor will be open and closed 30 | * automatically after this method returns 31 | *

32 | * Sample code: Print and LOG all customers names in the customers table 33 | *

34 |      * db.from("Customers")
35 |      *       .select().columns("Name")
36 |      *       .query(new IEnumerable<Cursor>(){
37 |      *           public void next(Cursor cursor){
38 |      *               Log.i("TAG", "Name: " + cursor.getString(0));
39 |      *           }
40 |      *       });
41 |      * 
42 |      * 
43 | * @param cursor the cursor 44 | */ 45 | void query(ISequence cursor); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Extensions/DataProvider/src/main/java/com/bingzer/android/dbv/providers/ReadOnlyDataProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.providers; 17 | 18 | import android.content.ContentValues; 19 | import android.net.Uri; 20 | 21 | /** 22 | * This is a 'read-only' provider. If you implemented this, 23 | * the third-party will not be able to perform 24 | * INSERT,UPDATE and DELETE 25 | * operations. They are, however, allowed to query or perform 26 | * SELECT 27 | * 28 | */ 29 | public abstract class ReadOnlyDataProvider extends DataProvider { 30 | 31 | @Override 32 | public Uri insert(Uri uri, ContentValues contentValues) { 33 | throw new UnsupportedOperationException("Read-only"); 34 | } 35 | 36 | @Override 37 | public int delete(Uri uri, String selection, String[] selectionArgs) { 38 | throw new UnsupportedOperationException("Read-only"); 39 | } 40 | 41 | @Override 42 | public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) { 43 | throw new UnsupportedOperationException("Read-only"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/InsertIntoImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import android.content.ContentValues; 19 | 20 | import com.bingzer.android.dbv.queries.IQuery; 21 | import com.bingzer.android.dbv.queries.InsertInto; 22 | import com.bingzer.android.dbv.utils.ContentValuesUtils; 23 | 24 | /** 25 | * Created by Ricky on 4/26/2014. 26 | */ 27 | class InsertIntoImpl extends InsertImpl implements InsertInto { 28 | 29 | private ContentSet query; 30 | private String[] columnNames; 31 | 32 | public InsertIntoImpl(ContentSet query, String... columnNames){ 33 | this.query = query; 34 | this.columnNames = columnNames; 35 | } 36 | 37 | @Override 38 | public IQuery val(Object... values) { 39 | ContentValues contentValues = new ContentValues(); 40 | for(int i = 0; i < columnNames.length; i++){ 41 | ContentValuesUtils.mapContentValuesFromGenericObject(contentValues, columnNames[i], values[i]); 42 | } 43 | 44 | query.onContentValuesSet(this, contentValues); 45 | 46 | return this; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Extensions/DataProvider/src/main/java/com/bingzer/android/dbv/providers/IDataProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.providers; 17 | 18 | import com.bingzer.android.dbv.IDatabase; 19 | 20 | /** 21 | * IDataProvider 22 | */ 23 | public interface IDataProvider { 24 | 25 | /** 26 | * Returns an {@link IDatabase}. 27 | * You must also call {@link IDatabase#open(int, com.bingzer.android.dbv.IDatabase.Builder)} 28 | * 29 | * Example of implementation: 30 | * 31 | *
32 |      *     ...
33 |      *     public IDatabase openDatabase(){
34 |      *         IDatabase db = DbQuery.getDatabase("MyDatabase");
35 |      *         db.open(1, new SQLiteBuilder(){
36 |      *             ...
37 |      *         });
38 |      *
39 |      *         return db;
40 |      *     }
41 |      *     ...
42 |      * 
43 | *
44 | * 45 | * @see {@link IDatabase} 46 | * @return {@link IDatabase} 47 | */ 48 | IDatabase openDatabase(); 49 | 50 | /** 51 | * Returns the authority string. Android documentation suggested to use 52 | * package style name (i.e: com.company.data.x). 53 | * 54 | * @return Authority string 55 | */ 56 | String getAuthority(); 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/OrmPerson.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | public class OrmPerson extends BaseEntity { 4 | 5 | private String name; 6 | private int age; 7 | 8 | public OrmPerson(){ 9 | this(null, -1); 10 | } 11 | 12 | public OrmPerson(String name, int age){ 13 | this.name = name; 14 | this.age = age; 15 | } 16 | 17 | public OrmPerson(IEnvironment environment){ 18 | super(environment); 19 | } 20 | 21 | public int getAge() { 22 | return age; 23 | } 24 | 25 | public void setAge(int age) { 26 | this.age = age; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | /** 38 | * The table name that this entity represents 39 | */ 40 | @Override 41 | public String getTableName() { 42 | return "Person"; 43 | } 44 | 45 | /** 46 | * Determines how to map column and the class variable. 47 | * 48 | * @param mapper the mapper object 49 | */ 50 | @Override 51 | public void map(Mapper mapper) { 52 | mapId(mapper); 53 | mapper.map("Name", new Delegate.TypeString(){ 54 | @Override 55 | public void set(String value) { 56 | setName(value); 57 | } 58 | @Override 59 | public String get() { 60 | return getName(); 61 | } 62 | }); 63 | 64 | mapper.map("Age", new Delegate.TypeInteger(){ 65 | @Override 66 | public void set(Integer value) { 67 | setAge(value); 68 | } 69 | @Override 70 | public Integer get() { 71 | return getAge(); 72 | } 73 | }); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/utils/CursorUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.utils; 17 | 18 | import android.database.Cursor; 19 | 20 | /** 21 | * Collection of utility methods that's cursor related 22 | */ 23 | public final class CursorUtils { 24 | 25 | /** 26 | * Try to return value from cursor 27 | * @param cursor the target cursor 28 | * @param columnName the column index 29 | * @param any type 30 | * @return value 31 | */ 32 | public static T getValueFromCursor(Cursor cursor, String columnName){ 33 | return getValueFromCursor(cursor, cursor.getColumnIndex(columnName)); 34 | } 35 | 36 | /** 37 | * Try to return values from cursor 38 | * @param cursor the target cursor 39 | * @param columnIndex the column index 40 | * @param any type 41 | * @return value 42 | */ 43 | @SuppressWarnings("unchecked") 44 | public static T getValueFromCursor(Cursor cursor, int columnIndex){ 45 | return (T) DelegateUtils.getObjectFromCursor(cursor, columnIndex); 46 | } 47 | 48 | ////////////////////////////////////////////////////////////////////////////////////////// 49 | 50 | private CursorUtils(){ 51 | // 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/PreloadedDatabaseTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.content.Context; 4 | import android.test.AndroidTestCase; 5 | 6 | import com.bingzer.android.dbv.utils.CollectionUtils; 7 | 8 | import java.io.File; 9 | import java.io.IOException; 10 | 11 | /** 12 | * Created by Ricky on 8/16/13. 13 | */ 14 | public class PreloadedDatabaseTest extends AndroidTestCase { 15 | IDatabase db; 16 | File dbFile; 17 | 18 | @Override 19 | public void setUp(){ 20 | // http://chinookdatabase.codeplex.com/wikipage?title=Chinook_Schema&referringTitle=Home 21 | // chinook sample readonlyDb 22 | dbFile = new File(getContext().getFilesDir(), "Chinook.sqlite"); 23 | 24 | if(!dbFile.exists()){ 25 | try{ 26 | IOHelper.copyFile(getContext().getResources().getAssets().open("Chinook.sqlite"), dbFile); 27 | } 28 | catch (IOException e){ 29 | throw new Error(e); 30 | } 31 | } 32 | 33 | db = DbQuery.getDatabase("Chinook.sqlitex"); 34 | db.open(1, dbFile.getAbsolutePath(), new SQLiteBuilder() { 35 | @Override 36 | public Context getContext() { 37 | return PreloadedDatabaseTest.this.getContext(); 38 | } 39 | 40 | @Override 41 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 42 | 43 | } 44 | }); 45 | } 46 | 47 | public void testDatabaseContent(){ 48 | assertTrue(CollectionUtils.size(db.getTables()) > 0); 49 | assertTrue(db.getVersion() == 1); 50 | } 51 | 52 | public void testGetName(){ 53 | assertEquals("Chinook.sqlitex", db.getName()); 54 | } 55 | 56 | @Override 57 | public void tearDown(){ 58 | db.close(); 59 | assertTrue("Cannot delete file", dbFile.delete()); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/CreateIndexTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.content.Context; 4 | import android.test.AndroidTestCase; 5 | 6 | /** 7 | * Created by Ricky Tobing on 8/12/13. 8 | */ 9 | public class CreateIndexTest extends AndroidTestCase { 10 | 11 | IDatabase db; 12 | ITable.Model tableModel; 13 | 14 | @Override 15 | public void setUp(){ 16 | 17 | db = DbQuery.getDatabase("CreateIndexDb"); 18 | db.open(1, new SQLiteBuilder() { 19 | @Override 20 | public Context getContext() { 21 | return CreateIndexTest.this.getContext(); 22 | } 23 | 24 | @Override 25 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 26 | tableModel = modeling.add("Person") 27 | .addPrimaryKey("Id") 28 | .add("Name", "String") 29 | .add("Age", "Integer") 30 | .add("Address", "Blob") 31 | .index("Id", "Name"); 32 | 33 | } 34 | }); 35 | 36 | db.from("Person").delete(); 37 | } 38 | 39 | @Override 40 | protected void tearDown() throws Exception { 41 | db.close(); 42 | getContext().deleteDatabase("CreateIndexDb"); 43 | } 44 | 45 | public void testTableModel_ShouldContainIndex_OnId(){ 46 | assertTrue(tableModel != null); 47 | assertTrue(tableModel.toString().toLowerCase().contains("person_id_idx")); 48 | } 49 | 50 | public void testTableModel_ShouldContainIndex_OnName(){ 51 | assertTrue(tableModel != null); 52 | assertTrue(tableModel.toString().toLowerCase().contains("person_name_idx")); 53 | } 54 | 55 | public void testTableModel_ShouldContainIndex_OnAddress(){ 56 | assertTrue(tableModel != null); 57 | assertTrue(!tableModel.toString().toLowerCase().contains("person_address_idx")); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/GroupBy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | import android.database.Cursor; 19 | 20 | import com.bingzer.android.dbv.contracts.ColumnSelectable; 21 | import com.bingzer.android.dbv.contracts.CursorEnumerable; 22 | import com.bingzer.android.dbv.contracts.EntitySelectable; 23 | import com.bingzer.android.dbv.contracts.Pagination; 24 | 25 | /** 26 | * Represents a group by statement 27 | *

28 | * Find a complete Wiki and documentation here:
29 | * https://github.com/bingzer/DbQuery/wiki 30 | *

31 | * 32 | * @see Having 33 | */ 34 | public interface GroupBy extends IQuery, 35 | EntitySelectable, ColumnSelectable, 36 | Pagination, CursorEnumerable { 37 | 38 | /** 39 | * Adds a HAVING statement 40 | * with the specified condition 41 | * @param condition the condition to set 42 | * @return Having object 43 | */ 44 | Having having(String condition); 45 | 46 | /** 47 | * Adds a HAVING statement 48 | * with the specified condition 49 | * @param clause clause 50 | * @param args args 51 | * @return Having object 52 | */ 53 | Having having(String clause, Object... args); 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/TransactionImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.internal; 18 | 19 | import com.bingzer.android.dbv.IDatabase; 20 | import com.bingzer.android.dbv.internal.Database; 21 | 22 | /** 23 | * Implements of {@link com.bingzer.android.dbv.IDatabase.Transaction} 24 | * 25 | * Created by Ricky Tobing on 8/12/13. 26 | */ 27 | class TransactionImpl implements IDatabase.Transaction { 28 | 29 | final Database database; 30 | final IDatabase.Batch batch; 31 | 32 | public TransactionImpl(Database database, IDatabase.Batch batch){ 33 | this.database = database; 34 | this.batch = batch; 35 | } 36 | 37 | @Override 38 | public void commit() { 39 | synchronized (this){ 40 | database.begin(); 41 | batch.exec(database); 42 | database.commit(); 43 | } 44 | } 45 | 46 | @Override 47 | public void rollback() { 48 | database.rollback(); 49 | } 50 | 51 | @Override 52 | public void end() { 53 | database.end(); 54 | } 55 | 56 | @Override 57 | public boolean execute() { 58 | try{ 59 | commit(); 60 | return true; 61 | } 62 | catch (Throwable e){ 63 | rollback(); 64 | return false; 65 | } 66 | finally { 67 | end(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/EntitySelectable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import com.bingzer.android.dbv.IEntity; 20 | import com.bingzer.android.dbv.IEntityList; 21 | 22 | /** 23 | * Extension for an IEntity 24 | * 25 | * @see IEntity 26 | */ 27 | public interface EntitySelectable { 28 | 29 | /** 30 | * Query and store the result to an {@link com.bingzer.android.dbv.IEntity} 31 | *

32 | * Sample Code: Populate customer data whose name is John into a Customer object 33 | *

34 |      * Customer customer = new Customer();
35 |      * ...
36 |      * db.from("TableName").select("Name = ?", "John").query(customer);
37 |      * 
38 |      * 
39 | * @see IEntity 40 | * @param entity the IEntity object 41 | */ 42 | void query(IEntity entity); 43 | 44 | /** 45 | * Query and store the result to an {@link com.bingzer.android.dbv.IEntityList} 46 | *

47 | * Sample Code: Populate a customer list whose country is US 48 | *

49 |      * CustomerList customerList = new CustomerList();
50 |      * ...
51 |      * db.from("TableName").select("Country = ?", "US").query(customerList);
52 |      * 
53 |      * 
54 | * @see IEntityList 55 | * @param entityList the IEntityList object 56 | * @param IEntity 57 | */ 58 | void query(IEntityList entityList); 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/IEntityList.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv; 17 | 18 | /** 19 | * Represents a collection of {@link com.bingzer.android.dbv.IEntity}. 20 | * While {@link com.bingzer.android.dbv.IEntity} is a runtime object mapper, 21 | * {@link com.bingzer.android.dbv.IEntityList} is a collection of {@link com.bingzer.android.dbv.IEntity} 22 | * 23 | *

24 | * Sample Code: 25 | *


26 |  * //---- Person.java
27 |  * public Person implements IEntity {
28 |  *     ...
29 |  * }
30 |  *
31 |  * //---- PersonList.java
32 |  * public PersonList extends ArrayList<Person> implements IEntityList<Person> {
33 |  *     ...
34 |  * }
35 |  *
36 |  * //---- Other.java
37 |  * IDatabase db = ...
38 |  * PersonList personList = new PersonList();
39 |  *
40 |  * // Get all customers from US
41 |  * db.from("Customers").select("Country = ?", "US").query(personList);
42 |  *
43 |  * 
44 | * 45 | * @version 2.0 46 | * @see com.bingzer.android.dbv.IEntity 47 | * @see com.bingzer.android.dbv.queries.Select#query(IEntity) 48 | * @see com.bingzer.android.dbv.ITable#insert(IEntity) 49 | */ 50 | public interface IEntityList extends Iterable{ 51 | 52 | /** 53 | * Adds an entity 54 | * @param entity entity to add 55 | * @return true if added, false otherwise 56 | */ 57 | boolean add(T entity); 58 | 59 | /** 60 | * Creates a new entity 61 | * @return T 62 | */ 63 | T newEntity(); 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Sample/src/androidTest/java/com/bingzer/android/dbv/content/ResolverTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.content; 2 | 3 | import android.net.Uri; 4 | import android.test.AndroidTestCase; 5 | import android.test.suitebuilder.annotation.Suppress; 6 | 7 | import com.bingzer.android.dbv.content.contracts.IResolver; 8 | 9 | /** 10 | * Basic resolve setter/getter test 11 | */ 12 | @Suppress 13 | public class ResolverTest extends AndroidTestCase { 14 | 15 | IResolver resolver; 16 | 17 | @Override 18 | public void setUp(){ 19 | resolver = ContentQuery.resolve("content://fake", getContext()); 20 | } 21 | 22 | //////////////////////////////////////////////////////// 23 | 24 | public void testGetUri(){ 25 | assertNotNull(resolver.getUri()); 26 | assertEquals(resolver.getUri(), Uri.parse("content://fake")); 27 | } 28 | 29 | public void testGetConfig(){ 30 | assertNotNull(resolver.getContentConfig()); 31 | } 32 | 33 | public void testGetDefaultProjections(){ 34 | assertNotNull(resolver.getContentConfig().getDefaultProjections()); 35 | assertTrue(resolver.getContentConfig().getDefaultProjections().length > 0); 36 | assertEquals(resolver.getContentConfig().getDefaultProjections()[0], resolver.getContentConfig().getIdNamingConvention()); 37 | } 38 | 39 | public void testGetAuthority(){ 40 | // defaults 41 | assertNull(resolver.getContentConfig().getDefaultAuthority()); 42 | 43 | resolver.getContentConfig().setDefaultAuthority("MyAuthority"); 44 | assertNotNull(resolver.getContentConfig().getDefaultAuthority()); 45 | assertEquals("MyAuthority", resolver.getContentConfig().getDefaultAuthority()); 46 | } 47 | 48 | /////////////////////// config ///////////////////////////////// 49 | 50 | public void test_ConfigGetIdNamingConvention(){ 51 | // default 52 | assertEquals("_id", resolver.getContentConfig().getIdNamingConvention()); 53 | 54 | resolver.getContentConfig().setIdNamingConvention("XXID"); 55 | assertEquals("XXID", resolver.getContentConfig().getIdNamingConvention()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Update.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | import android.content.ContentValues; 19 | 20 | /** 21 | * Represents an update statement 22 | *

23 | * Find a complete Wiki and documentation here:
24 | * https://github.com/bingzer/DbQuery/wiki 25 | *

26 | */ 27 | public interface Update extends IQuery { 28 | 29 | /** 30 | * Multiple columns 31 | * @param columns column names 32 | * @return num updated 33 | */ 34 | Columns columns(String... columns); 35 | 36 | /** 37 | * ContentValues 38 | * @param values content values 39 | * @return num updated 40 | */ 41 | IQuery val(ContentValues values); 42 | 43 | /** 44 | * For a single column update 45 | * @param column the column name 46 | * @param value the value 47 | * @return integer 48 | */ 49 | IQuery val(String column, Object value); 50 | 51 | /** 52 | * For a multiple column update 53 | * @param columnNames column names 54 | * @param values values 55 | * @return integer 56 | */ 57 | IQuery val(String[] columnNames, Object[] values); 58 | 59 | /** 60 | * The columns 61 | */ 62 | static interface Columns { 63 | 64 | /** 65 | * Sets values 66 | */ 67 | IQuery val(Object... values); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Sample/src/androidTest/java/com/bingzer/android/dbv/providers/BasicDataProviderTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.providers; 17 | 18 | import android.database.Cursor; 19 | import android.test.AndroidTestCase; 20 | import android.test.suitebuilder.annotation.Suppress; 21 | 22 | import com.bingzer.android.dbv.content.ContentQuery; 23 | import com.bingzer.android.dbv.content.contracts.IResolver; 24 | 25 | /** 26 | * Created by Ricky Tobing on 8/28/13. 27 | */ 28 | @Suppress 29 | public class BasicDataProviderTest extends AndroidTestCase { 30 | 31 | 32 | public void testConnect(){ 33 | IResolver resolver = ContentQuery.resolve("content://com.bingzer.android.dbv.providers.sample/Album", getContext()); 34 | resolver.getContentConfig().setAppendTableNameForId(true); 35 | resolver.getContentConfig().setIdNamingConvention("Id"); 36 | 37 | Cursor cursor = resolver.select().query(); 38 | assertTrue(cursor.getCount() > 0); 39 | cursor.close(); 40 | } 41 | 42 | 43 | public void testConnect2(){ 44 | IResolver resolver = ContentQuery.resolve("content://com.bingzer.android.dbv.providers.sample/Album", getContext()); 45 | resolver.getContentConfig().setDefaultProjections("_Id", "Title"); 46 | resolver.getContentConfig().setAppendTableNameForId(true); 47 | resolver.getContentConfig().setIdNamingConvention("Id"); 48 | 49 | Cursor cursor = resolver.select().query(); 50 | assertTrue(cursor.getCount() > 0); 51 | cursor.close(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/ConfigTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.test.AndroidTestCase; 4 | 5 | /** 6 | * Created by Ricky Tobing on 8/16/13. 7 | */ 8 | public class ConfigTest extends AndroidTestCase { 9 | 10 | IDatabase db = DbQuery.getDatabase("X"); 11 | 12 | public void testIdNamingConvention(){ 13 | // default 14 | assertTrue(db.getConfig().getIdNamingConvention().equals("Id")); 15 | 16 | db.getConfig().setIdNamingConvention("OID"); 17 | assertTrue(db.getConfig().getIdNamingConvention().equals("OID")); 18 | db.getConfig().setIdNamingConvention("LOL"); 19 | assertTrue(db.getConfig().getIdNamingConvention().equals("LOL")); 20 | } 21 | 22 | public void testAppendTableNameForId(){ 23 | // default 24 | assertTrue(!db.getConfig().getAppendTableNameForId()); 25 | 26 | db.getConfig().setAppendTableNameForId(true); 27 | assertTrue(db.getConfig().getAppendTableNameForId()); 28 | db.getConfig().setAppendTableNameForId(false); 29 | assertTrue(!db.getConfig().getAppendTableNameForId()); 30 | } 31 | 32 | public void testSetForeignKeySupport(){ 33 | // default 34 | assertTrue(!db.getConfig().getForeignKeySupport()); 35 | 36 | db.getConfig().setForeignKeySupport(true); 37 | assertTrue(db.getConfig().getForeignKeySupport()); 38 | db.getConfig().setForeignKeySupport(false); 39 | assertTrue(!db.getConfig().getForeignKeySupport()); 40 | } 41 | 42 | public void testSetDebug(){ 43 | // default 44 | assertFalse(db.getConfig().getDebug()); 45 | 46 | db.getConfig().setDebug(true); 47 | assertTrue(db.getConfig().getDebug()); 48 | db.getConfig().setDebug(false); 49 | assertTrue(!db.getConfig().getDebug()); 50 | } 51 | 52 | public void testReadOnly(){ 53 | // default 54 | assertFalse(db.getConfig().isReadOnly()); 55 | 56 | db.getConfig().setReadOnly(true); 57 | assertTrue(db.getConfig().isReadOnly()); 58 | db.getConfig().setReadOnly(false); 59 | assertTrue(!db.getConfig().isReadOnly()); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/Environment.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv; 17 | 18 | /** 19 | * Default implementation of {@link IEnvironment} 20 | */ 21 | public class Environment implements IEnvironment { 22 | 23 | private static final IEnvironment environment = new Environment(); 24 | 25 | /** 26 | * Returns the {@code local} environment. 27 | * Local environment is the default environment 28 | */ 29 | public static IEnvironment getLocalEnvironment(){ 30 | return environment; 31 | } 32 | 33 | /////////////////////////////////////////////////////////////////////////////////////////// 34 | 35 | private IDatabase database = null; 36 | 37 | /** 38 | * Private Constructor to create a local environment 39 | */ 40 | private Environment(){ 41 | this(null); 42 | } 43 | 44 | /** 45 | * Creates an environment 46 | * @param database the database 47 | */ 48 | public Environment(IDatabase database){ 49 | this.database = database; 50 | } 51 | 52 | ///////////////////////////////////////////////////////////////////////////////// 53 | 54 | /** 55 | * Sets the database 56 | */ 57 | protected void setDatabase(IDatabase db) { 58 | database = db; 59 | } 60 | 61 | ///////////////////////////////////////////////////////////////////////////////// 62 | 63 | /** 64 | * Returns the database 65 | */ 66 | @Override 67 | public IDatabase getDatabase(){ 68 | return database; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/IBaseEntity.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv; 17 | 18 | import android.database.Cursor; 19 | 20 | /** 21 | * Represents contract for ORM Entity. 22 | * IBaseEntity should be able: 23 | * 1. Persist the changes to the database. 24 | * 2. Delete itself 25 | * 26 | */ 27 | public interface IBaseEntity extends IEntity { 28 | 29 | /** 30 | * Sets this Id 31 | */ 32 | void setId(long id); 33 | 34 | /** 35 | * Save any changes. 36 | * If {@code getId()} is anything lower than or equal to 0 37 | * then, an INSERTION operation will be performed. 38 | * Otherwise, it is an UPDATE operation. 39 | */ 40 | boolean save(); 41 | 42 | /** 43 | * Delete itself from the database. 44 | * If successful, {@code getId()} will be automatically set to -1. 45 | */ 46 | boolean delete(); 47 | 48 | /** 49 | * Reload data from the database. Useful if other code has make some changes 50 | * and you need to rehydrate the data - fresh - from the db 51 | */ 52 | boolean load(); 53 | 54 | /** 55 | * Load by Id. 56 | */ 57 | boolean load(long id); 58 | 59 | /** 60 | * Convenient method to load data from a {@code Cursor} object 61 | */ 62 | boolean load(Cursor cursor); 63 | 64 | /** 65 | * The table name that this entity represents 66 | */ 67 | String getTableName(); 68 | 69 | /** 70 | * Returns the environment. 71 | * @see IEnvironment 72 | */ 73 | IEnvironment getEnvironment(); 74 | 75 | } 76 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/internal/ContentInsertIntoImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import android.content.ContentValues; 19 | import android.net.Uri; 20 | 21 | import com.bingzer.android.dbv.content.utils.UriUtils; 22 | import com.bingzer.android.dbv.queries.IQuery; 23 | import com.bingzer.android.dbv.queries.InsertInto; 24 | import com.bingzer.android.dbv.utils.ContentValuesUtils; 25 | 26 | /** 27 | * Created by Ricky on 8/20/13. 28 | */ 29 | class ContentInsertIntoImpl extends QueryImpl implements InsertInto { 30 | 31 | private Uri value; 32 | private ContentSet query; 33 | private String[] columnNames; 34 | 35 | public ContentInsertIntoImpl(ContentSet query, String... columnNames){ 36 | this.query = query; 37 | this.columnNames = columnNames; 38 | } 39 | 40 | @Override 41 | public IQuery val(Object... values) { 42 | ContentValues contentValues = new ContentValues(); 43 | for(int i = 0; i < columnNames.length; i++){ 44 | ContentValuesUtils.mapContentValuesFromGenericObject(contentValues, columnNames[i], values[i]); 45 | } 46 | 47 | query.onContentValuesSet(this, contentValues); 48 | 49 | return this; 50 | } 51 | 52 | @Override 53 | public Long query() { 54 | return UriUtils.parseIdFromUri(value); 55 | } 56 | 57 | public void setUri(Uri value){ 58 | this.value = value; 59 | } 60 | 61 | public static interface ContentSet { 62 | 63 | void onContentValuesSet(ContentInsertIntoImpl query, ContentValues contentValues); 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/AlterTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.content.Context; 4 | import android.test.AndroidTestCase; 5 | 6 | import com.bingzer.android.dbv.utils.CollectionUtils; 7 | 8 | /** 9 | * Created by Ricky Tobing on 8/16/13. 10 | */ 11 | public class AlterTest extends AndroidTestCase { 12 | 13 | IDatabase db; 14 | 15 | @Override 16 | public void setUp(){ 17 | db = DbQuery.getDatabase("AlterDb"); 18 | db.open(1, new SQLiteBuilder() { 19 | @Override 20 | public Context getContext() { 21 | return AlterTest.this.getContext(); 22 | } 23 | 24 | @Override 25 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 26 | modeling.add("Person") 27 | .addPrimaryKey("Id") 28 | .addText("Name"); 29 | } 30 | }); 31 | } 32 | 33 | @Override 34 | public void tearDown(){ 35 | db.close(); 36 | getContext().deleteDatabase("AlterDb"); 37 | } 38 | 39 | public void testRenameColumn(){ 40 | assertNotNull(db.from("Person")); 41 | // alter 42 | ITable table = db.from("Person"); 43 | table.alter().rename("PersonAltered"); 44 | assertTrue(table.getName().equals("PersonAltered")); 45 | 46 | // re check person 47 | assertNull(db.from("Person")); 48 | // should not be null 49 | assertNotNull(db.from("PersonAltered")); 50 | 51 | // renamed to Person 52 | table.alter().rename("Person"); 53 | } 54 | 55 | public void testAddColumn(){ 56 | // don't have address yet 57 | assertFalse(CollectionUtils.contains(db.from("Person").getColumns(), "Address")); 58 | 59 | // we'll add address column 60 | db.from("Person").alter().addColumn("Address", "Text"); 61 | 62 | assertTrue(CollectionUtils.contains(db.from("Person").getColumns(), "Address")); 63 | } 64 | 65 | public void testRemoveColumn(){ 66 | try{ 67 | // should throw exception 68 | db.from("Person").alter().removeColumn("Name"); 69 | assertTrue(false); 70 | } 71 | catch (Exception e){ 72 | assertTrue(true); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Selectable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.contracts; 17 | 18 | import com.bingzer.android.dbv.queries.Select; 19 | 20 | /** 21 | * Created by Ricky Tobing on 7/17/13. 22 | */ 23 | public interface Selectable { 24 | 25 | /** 26 | * Select top (x) add the specified condition 27 | * @param top number to return 28 | * @param condition the condition 29 | * @return {@link com.bingzer.android.dbv.queries.Select} 30 | */ 31 | Select select(int top, String condition); 32 | 33 | /** 34 | * Select some condition 35 | * @param condition the condition 36 | * @return {@link com.bingzer.android.dbv.queries.Select} 37 | */ 38 | Select select(String condition); 39 | 40 | /** 41 | * Select id. Id column must be defined in the naming convention 42 | * specified in {@link com.bingzer.android.dbv.IConfig} 43 | * @param id id to search 44 | * @see com.bingzer.android.dbv.IConfig#setIdNamingConvention(String) 45 | * @return {@link com.bingzer.android.dbv.queries.Select} 46 | */ 47 | Select select(long id); 48 | 49 | /** 50 | * Select multiple ids 51 | * @param ids array id 52 | * @return {@link com.bingzer.android.dbv.queries.Select} 53 | */ 54 | Select select(long... ids); 55 | 56 | /** 57 | * Select along with whereClause 58 | * @param whereClause 'where' clause 59 | * @param args arguments 60 | * @return {@link com.bingzer.android.dbv.queries.Select} 61 | */ 62 | Select select(String whereClause, Object... args); 63 | 64 | /** 65 | * Select 66 | * @param whereClause 'where' clause 67 | * @param args arguments 68 | * @return {@link com.bingzer.android.dbv.queries.Select} 69 | */ 70 | Select select(int top, String whereClause, Object... args); 71 | 72 | } 73 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/ColumnSelectable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.contracts; 17 | 18 | /** 19 | * Created by Ricky on 4/28/2014. 20 | */ 21 | public interface ColumnSelectable { 22 | 23 | /** 24 | * Single query a column value. 25 | * This is a convenient method to from a value of a single column 26 | * specified by its column index. 27 | * 28 | *

29 | * Sample code to from the customer name from a customer table 30 | *


31 |      * String customerName = db.from("customers").select().columns("Name", "Age").query(0);
32 |      * 
33 |      * 
34 | * 35 | * Sample code to from the customer's age from a customer table 36 | *

37 |      * int customerAge = db.from("customers").select().columns("Name", "Age").query(1);
38 |      * 
39 |      * 
40 | * @param columnIndex the column index 41 | * @param any type to return 42 | * @return any type 43 | */ 44 | T query(int columnIndex); 45 | 46 | /** 47 | * Single query a column value. 48 | * This is a convenient method to from a value of a single column 49 | * specified by its column name. 50 | * 51 | *

52 | * Sample code to from the customer name from a customer table 53 | *


54 |      * String customerName = db.from("customers").select().columns("Name", "Age").query("Name");
55 |      * 
56 |      * 
57 | * Sample code to from the customer's age from a customer table 58 | *

59 |      * int customerAge = db.from("customers").select().columns("Name", "Age").query("Age");
60 |      * 
61 |      * 
62 | * @param columnName the column name 63 | * @param any type to return 64 | * @return any type 65 | */ 66 | T query(String columnName); 67 | 68 | } 69 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Distinguishable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import com.bingzer.android.dbv.queries.Select; 20 | 21 | /** 22 | * Created by Ricky Tobing on 8/21/13. 23 | */ 24 | public interface Distinguishable { 25 | 26 | /** 27 | * Select distinct all. 28 | * Equivalent of calling selectDistinct(null) 29 | * @return {@link com.bingzer.android.dbv.queries.Select} 30 | */ 31 | Select selectDistinct(); 32 | 33 | /** 34 | * Select distinct 35 | * @param condition the condition 36 | * @return {@link com.bingzer.android.dbv.queries.Select} 37 | */ 38 | Select selectDistinct(String condition); 39 | 40 | /** 41 | * Select distinct add condition 42 | * @param whereClause 'where' clause 43 | * @param args arguments 44 | * @return {@link com.bingzer.android.dbv.queries.Select} 45 | */ 46 | Select selectDistinct(String whereClause, Object... args); 47 | 48 | /** 49 | * Select distinct with limit (top) 50 | * @param top the limit to return 51 | * @return {@link com.bingzer.android.dbv.queries.Select} 52 | */ 53 | Select selectDistinct(int top); 54 | 55 | /** 56 | * Select distinct with limit (top) with the specified condition 57 | * @param top the limit to return 58 | * @param condition the condition 59 | * @return {@link com.bingzer.android.dbv.queries.Select} 60 | */ 61 | Select selectDistinct(int top, String condition); 62 | 63 | /** 64 | * Select distinct with limit (top) with the specified condition 65 | * @param top the limit to return 66 | * @param whereClause the where clause 67 | * @param args arguments 68 | * @return {@link com.bingzer.android.dbv.queries.Select} 69 | */ 70 | Select selectDistinct(int top, String whereClause, Object... args); 71 | } 72 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/IdNamingConventionTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.content.Context; 4 | import android.test.AndroidTestCase; 5 | 6 | import com.bingzer.android.dbv.queries.InsertInto; 7 | 8 | /** 9 | * Created by Ricky Tobing on 8/13/13. 10 | */ 11 | public class IdNamingConventionTest extends AndroidTestCase { 12 | 13 | IDatabase db; 14 | 15 | @Override 16 | public void setUp(){ 17 | db = DbQuery.getDatabase("NamingConventionDb"); 18 | db.getConfig().setIdNamingConvention("IDX"); 19 | db.getConfig().setAppendTableNameForId(true); 20 | db.open(1, new SQLiteBuilder() { 21 | @Override 22 | public Context getContext() { 23 | return IdNamingConventionTest.this.getContext(); 24 | } 25 | 26 | @Override 27 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 28 | modeling.add("Person") 29 | .addPrimaryKey("PersonIDX") 30 | .add("Name", "String") 31 | .add("Age", "Integer") 32 | .add("Address", "Blob"); 33 | } 34 | }); 35 | 36 | db.from("Person").delete(); 37 | 38 | 39 | InsertInto insert = db.from("Person").insertInto("Name", "Age", "Address"); 40 | insert.val("John", 23, "Washington DC".getBytes()); 41 | insert.val("Ronaldo", 40, "Madrid".getBytes()); 42 | insert.val("Messi", 25, "Barcelona".getBytes()); 43 | } 44 | 45 | public void testSelect(){ 46 | Person person = new Person(); 47 | 48 | db.from("Person").select("Name = ?", "John").query(person); 49 | assertTrue(person.getId() > 0); 50 | assertTrue(person.getName().equals("John")); 51 | db.from("Person").select("Name = ?", "Messi").query(person); 52 | assertTrue(person.getId() > 0); 53 | assertTrue(new String(person.getAddressBytes()).equals("Barcelona")); 54 | } 55 | 56 | public void testUpdate(){ 57 | Person person = new Person(); 58 | db.from("Person").select("Name = ?", "John").query(person); // john 59 | long id = person.getId(); 60 | // modify 61 | person.setName("MOD"); 62 | person.setAge(20); 63 | db.from("Person").update(person); 64 | // reget 65 | db.from("Person").select(id).query(person); 66 | assertTrue(person.getName().equals("MOD")); 67 | assertTrue(person.getAge() == 20); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/queries/Select.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.queries; 17 | 18 | import android.database.Cursor; 19 | 20 | import com.bingzer.android.dbv.contracts.ColumnSelectable; 21 | import com.bingzer.android.dbv.contracts.CursorEnumerable; 22 | import com.bingzer.android.dbv.contracts.EntitySelectable; 23 | import com.bingzer.android.dbv.contracts.Groupable; 24 | import com.bingzer.android.dbv.contracts.Pagination; 25 | 26 | /** 27 | * For select statement 28 | *

29 | * Find a complete Wiki and documentation here:
30 | * https://github.com/bingzer/DbQuery/wiki 31 | *

32 | */ 33 | public interface Select extends IQuery, EntitySelectable, 34 | CursorEnumerable, ColumnSelectable, 35 | Pagination, Groupable /*,Unionable*/ { 36 | 37 | /** 38 | * Specified the column to return. 39 | * default or null will produce SELECT * FROM 40 | * @param columns column names 41 | * @return {@link com.bingzer.android.dbv.queries.Select} 42 | */ 43 | Select columns(String... columns); 44 | 45 | /** 46 | * Order by. To create multiple orderBy ASC or DESC or both, 47 | * this is possible 48 | * 49 | *

50 |      *   db.from("Table").select().orderBy("Id", "Name", "Price DESC");
51 |      * 
52 |      * 
53 | * @param columns column names 54 | * @return {@link com.bingzer.android.dbv.queries.Select} 55 | */ 56 | OrderBy orderBy(String... columns); 57 | 58 | ///////////////////////////////////////////////////////////////////////////////////////// 59 | 60 | /** 61 | * Order By 62 | */ 63 | public static interface OrderBy extends IQuery, EntitySelectable, 64 | CursorEnumerable, ColumnSelectable, 65 | Pagination, Groupable { 66 | 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/UtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.content.Context; 4 | import android.database.Cursor; 5 | import android.test.AndroidTestCase; 6 | 7 | import com.bingzer.android.dbv.utils.Utils; 8 | 9 | /** 10 | * Created by Ricky on 8/9/13. 11 | */ 12 | public class UtilsTest extends AndroidTestCase{ 13 | 14 | public void testSafeEscape(){ 15 | String bad = "'hello'"; 16 | String good = "hello"; 17 | 18 | String newBad = Utils.safeEscape(bad); 19 | String newGood = Utils.safeEscape(good); 20 | 21 | assertEquals(newBad, "'''hello'''"); 22 | assertTrue(newGood.equalsIgnoreCase("'hello'")); 23 | } 24 | 25 | public void testJoin(){ 26 | assertTrue(Utils.join(",", "a", "b").equals("a,b")); 27 | assertTrue(Utils.join(".", "a", "b").equals("a.b")); 28 | } 29 | 30 | //////////////////////////////////////////////////////////////////////////////// 31 | 32 | public void testBindArgs(){ 33 | String expected = "Name = 'Whatever' AND Age = 2 OR Address is null"; 34 | String actual = Utils.bindArgs("Name = ? AND Age = ? OR Address is ?", "Whatever", 2, null); 35 | assertEquals(expected, actual); 36 | 37 | expected = "Name = 'I''m fine. You?' AND Age = 2 OR Address is 'Wallaby Way, Sidney?'"; 38 | actual = Utils.bindArgs("Name = ? AND Age = ? OR Address is ?", "I'm fine. You?", 2, "Wallaby Way, Sidney?"); 39 | assertEquals(expected, actual); 40 | } 41 | 42 | public void testBindArgs_WithMissingArgs(){ 43 | String expected = "Hello 'World', how are you ?"; 44 | String actual = Utils.bindArgs("Hello ?, how are you ?", "World"); 45 | assertEquals(expected, actual); 46 | 47 | } 48 | 49 | //////////////////////////////////////////////////////////////////////////////// 50 | 51 | public void testArguments(){ 52 | IDatabase db = DbQuery.getDatabase("DbUtilsTest"); 53 | db.open(1, new SQLiteBuilder(){ 54 | 55 | @Override 56 | public Context getContext() { 57 | return UtilsTest.this.getContext(); 58 | } 59 | 60 | @Override 61 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 62 | modeling.add("Customers") 63 | .addPrimaryKey("Id") 64 | .addText("Name") 65 | .ifNotExists(); 66 | } 67 | }); 68 | 69 | Cursor cursor = db.from("Customers") 70 | .select("Name = ?", "$Yo$") 71 | .query(); 72 | cursor.close(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/bingzer/DbQuery.svg?branch=master)](https://travis-ci.org/bingzer/DbQuery) 2 | 3 | DbQuery 4 | ============== 5 | 6 | `DbQuery` is a lightweight and fluent SQLite Query API for Android. The API provides a new and simpler way to query data by minimizing the need to write SQL string inside the code. 7 | 8 | See complete **wiki** and **documentation** here: 9 | [https://github.com/bingzer/DbQuery/wiki](https://github.com/bingzer/DbQuery/wiki) 10 | 11 | Download 12 | ======== 13 | Download the latest stable binary (Master branch) 14 | ```groovy 15 | 16 | repositories { 17 | mavenCentral() 18 | } 19 | 20 | dependencies { 21 | compile (group:'com.bingzer.android.dbv', name: 'dbquery', version:'2.2.0') 22 | } 23 | ``` 24 | 25 | Download snapshots (Dev branch). 26 | ```groovy 27 | 28 | repositories { 29 | maven { 30 | url "https://oss.sonatype.org/content/repositories/snapshots/" 31 | } 32 | } 33 | 34 | dependencies { 35 | compile (group:'com.bingzer.android.dbv', name: 'dbquery', version:'2.2.0-SNAPSHOT', changing: true) 36 | } 37 | ``` 38 | 39 | Why 40 | === 41 | Sometimes we are stuck using the following code in Android development. 42 | ``` java 43 | Cursor cursor = db.query("Customers", // table name 44 | new String[] {"Id", "Address", "Age"}, // columns 45 | "Id IN (?,?,?)", // whereClause 46 | new String[]{"" + customerId1, "" + customerId2, "" + customerId3}, // whereArgs 47 | null, // groupBy 48 | null, // having 49 | "Age"); // orderBy 50 | ``` 51 | The purpose of `DbQuery` is to be able to write this query **fluently** 52 | ``` java 53 | Cursor cursor = db.from("Customers") 54 | .select("Id In (?,?,?)", customerId1, customerId2, customerId3) // whereClause 55 | .columns("Id", "Address", "Age") // columns 56 | .orderBy("Age") // orderBy 'Age' 57 | .query(); 58 | ``` 59 | 60 | LICENSE 61 | ======= 62 | ``` java 63 | Copyright 2014 Ricky Tobing 64 | 65 | Licensed under the Apache License, Version 2.0 (the "License"); 66 | you may not use this file except in compliance with the License. 67 | You may obtain a copy of the License at 68 | 69 | http://www.apache.org/licenses/LICENSE-2.0 70 | 71 | Unless required by applicable law or agreed to in writing, software 72 | distributed under the License is distributed on an "AS IS" BASIS, 73 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 74 | See the License for the specific language governing permissions and 75 | limitations under the License. 76 | ``` 77 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/Config.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.IConfig; 19 | 20 | /** 21 | * Default configuration. By default the Id column will be named 22 | * Id. Id with the following scheme is not yet supported: 23 | *
    24 | *
  • {TABLE_NAME}Id
  • 25 | *
26 | * That will be supported in the future 27 | * 28 | * Created by Ricky Tobing on 7/19/13. 29 | */ 30 | class Config implements IConfig { 31 | private String idNamingConvention; 32 | private boolean appendTableName; 33 | private boolean foreignKeySupport; 34 | private boolean debug; 35 | private boolean readOnly; 36 | 37 | public Config(){ 38 | this.idNamingConvention = "Id"; 39 | this.appendTableName = false; 40 | this.foreignKeySupport = false; 41 | this.debug = false; 42 | this.readOnly = false; 43 | } 44 | 45 | @Override 46 | public void setIdNamingConvention(String id) { 47 | idNamingConvention = id; 48 | } 49 | 50 | @Override 51 | public String getIdNamingConvention() { 52 | return idNamingConvention; 53 | } 54 | 55 | @Override 56 | public void setAppendTableNameForId(boolean appendTableName) { 57 | this.appendTableName = appendTableName; 58 | } 59 | 60 | @Override 61 | public boolean getAppendTableNameForId() { 62 | return appendTableName; 63 | } 64 | 65 | @Override 66 | public void setDebug(boolean on) { 67 | this.debug = on; 68 | } 69 | 70 | @Override 71 | public boolean getDebug() { 72 | return debug; 73 | } 74 | 75 | @Override 76 | public void setForeignKeySupport(boolean on) { 77 | this.foreignKeySupport = on; 78 | } 79 | 80 | @Override 81 | public boolean getForeignKeySupport() { 82 | return foreignKeySupport; 83 | } 84 | 85 | @Override 86 | public void setReadOnly(boolean readOnly) { 87 | this.readOnly = readOnly; 88 | } 89 | 90 | @Override 91 | public boolean isReadOnly() { 92 | return readOnly; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/UpdateImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import android.content.ContentValues; 19 | 20 | import com.bingzer.android.dbv.queries.IQuery; 21 | import com.bingzer.android.dbv.queries.Update; 22 | import com.bingzer.android.dbv.utils.ContentValuesUtils; 23 | 24 | /** 25 | * Created by Ricky on 4/26/2014. 26 | */ 27 | class UpdateImpl extends QueryImpl implements Update, IQuery { 28 | private ContentSet query; 29 | protected ContentValues contentValues; 30 | 31 | public UpdateImpl(){ 32 | this(null); 33 | } 34 | 35 | public UpdateImpl(ContentSet query){ 36 | this.query = query; 37 | this.value = 0; 38 | } 39 | 40 | @Override 41 | public Columns columns(final String... columns) { 42 | return new Columns() { 43 | @Override 44 | public IQuery val(Object... values) { 45 | return UpdateImpl.this.val(columns, values); 46 | } 47 | }; 48 | } 49 | 50 | @Override 51 | public IQuery val(ContentValues values) { 52 | contentValues = values; 53 | 54 | return notifyContentValuesSet(); 55 | } 56 | 57 | @Override 58 | public IQuery val(String column, Object value) { 59 | contentValues = new ContentValues(); 60 | ContentValuesUtils.mapContentValuesFromGenericObject(contentValues, column, value); 61 | 62 | return notifyContentValuesSet(); 63 | } 64 | 65 | @Override 66 | public IQuery val(String[] columnNames, Object[] values) { 67 | contentValues = new ContentValues(); 68 | for(int i = 0; i < columnNames.length; i++){ 69 | ContentValuesUtils.mapContentValuesFromGenericObject(contentValues, columnNames[i], values[i]); 70 | } 71 | 72 | return notifyContentValuesSet(); 73 | } 74 | 75 | // notify so that we can execute the update 76 | private IQuery notifyContentValuesSet(){ 77 | if(query != null) 78 | query.onContentValuesSet(this, contentValues); 79 | return this; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Insertable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import android.content.ContentValues; 20 | 21 | import com.bingzer.android.dbv.IEntity; 22 | import com.bingzer.android.dbv.IEntityList; 23 | import com.bingzer.android.dbv.queries.Insert; 24 | import com.bingzer.android.dbv.queries.InsertInto; 25 | 26 | /** 27 | * Created by Ricky Tobing on 7/17/13. 28 | */ 29 | public interface Insertable { 30 | 31 | /** 32 | * Insert content val 33 | * @param contents content values 34 | * @return Insert object 35 | */ 36 | Insert insert(ContentValues contents); 37 | 38 | /** 39 | * Insert multiple columns and multiple objects 40 | * @param columns column names 41 | * @param values the values 42 | * @return Insert object 43 | */ 44 | Insert insert(String[] columns, Object[] values); 45 | 46 | /** 47 | * Insert a single column and object 48 | * @param column column to insert 49 | * @param value object value 50 | * @return Insert object 51 | */ 52 | Insert insert(String column, Object value); 53 | 54 | /** 55 | * InsertWith follows by val(Object... values) 56 | * to specify the values 57 | * @param columns column names 58 | * @return an InsertWith object 59 | */ 60 | InsertInto insertInto(String... columns); 61 | 62 | ///////////////////////////////////////////////////////////////////////////////// 63 | ///////////////////////////////////////////////////////////////////////////////// 64 | 65 | /** 66 | * Insert an entity. 67 | * @param entity entity 68 | * @return an Insert object 69 | */ 70 | Insert insert(IEntity entity); 71 | 72 | /** 73 | * Bulk-insert an entity list. The returned value, (via Insert.query()) 74 | * is an integer and returns the number of entity successfully inserted. 75 | * Ids are automatically populated inside the IEntity object 76 | * @param entityList the entity list to insert 77 | * @param extends IEntity 78 | * @return an Insert object 79 | */ 80 | Insert insert(IEntityList entityList); 81 | } 82 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/Person.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | /** 4 | * Created by Ricky on 8/9/13. 5 | */ 6 | public class Person implements IEntity{ 7 | 8 | private long id = -1; 9 | private String name; 10 | private int age; 11 | private byte[] addressBytes; 12 | 13 | public Person(){ 14 | this(null, -1, null); 15 | } 16 | 17 | public Person(String name, int age, byte[] addressBytes){ 18 | this.name = name; 19 | this.age = age; 20 | this.addressBytes = addressBytes; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public int getAge() { 32 | return age; 33 | } 34 | 35 | public void setAge(int age) { 36 | this.age = age; 37 | } 38 | 39 | public void setId(long id){ 40 | this.id = id; 41 | } 42 | 43 | public byte[] getAddressBytes() { 44 | return addressBytes; 45 | } 46 | 47 | public void setAddressBytes(byte[] addressBytes) { 48 | this.addressBytes = addressBytes; 49 | } 50 | 51 | @Override 52 | public long getId() { 53 | return id; 54 | } 55 | 56 | @Override 57 | public void map(Mapper mapper) { 58 | mapper.map("Name", new Delegate.TypeString() { 59 | 60 | @Override 61 | public void set(String value) { 62 | setName(value); 63 | } 64 | 65 | @Override 66 | public String get() { 67 | return getName(); 68 | } 69 | }); 70 | 71 | mapper.map("Address", new Delegate.TypeBytes() { 72 | /** 73 | * Sets the value 74 | * 75 | * @param value the value to set 76 | */ 77 | @Override 78 | public void set(byte[] value) { 79 | setAddressBytes(value); 80 | } 81 | 82 | /** 83 | * Returns the value 84 | * 85 | * @return the value 86 | */ 87 | @Override 88 | public byte[] get() { 89 | return getAddressBytes(); 90 | } 91 | 92 | }); 93 | 94 | mapper.map("Age", new Delegate.TypeInteger(){ 95 | 96 | @Override 97 | public void set(Integer value) { 98 | setAge(value); 99 | } 100 | 101 | @Override 102 | public Integer get() { 103 | return getAge(); 104 | } 105 | }); 106 | 107 | mapper.mapId(new Delegate.TypeId(this){ 108 | @Override 109 | public void set(Long id) { 110 | setId(id); 111 | } 112 | }); 113 | 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/utils/CollectionUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.utils; 17 | 18 | import java.util.Collection; 19 | import java.util.Iterator; 20 | 21 | /** 22 | * Provides a collection utility tools 23 | */ 24 | public final class CollectionUtils { 25 | 26 | /** 27 | * Count the 'size' of base iterable 28 | * @param iterable the iterable 29 | * @return the size 30 | */ 31 | public static int size(Iterable iterable){ 32 | if(iterable instanceof Collection) 33 | return ((Collection) iterable).size(); 34 | return size(iterable.iterator()); 35 | } 36 | 37 | /** 38 | * Count the 'size' of base iterator 39 | * @param iterator the iterator 40 | * @return the size 41 | */ 42 | public static int size(Iterator iterator){ 43 | int count = 0; 44 | while (iterator.hasNext()) { 45 | iterator.next(); 46 | count++; 47 | } 48 | return count; 49 | } 50 | 51 | /** 52 | * Check to see if iterable contains a value. 53 | * The equals() will be performed to check the equality 54 | * @param iterable the target iterable 55 | * @param value the value 56 | * @param generic type of value 57 | * @return true if it has value, false otherwise 58 | */ 59 | public static boolean contains(Iterable iterable, T value){ 60 | if(iterable instanceof Collection) 61 | return ((Collection) iterable).contains(value); 62 | return contains(iterable.iterator(), value); 63 | } 64 | 65 | /** 66 | * Check to see if iterable contains a value. 67 | * The equals() will be performed to check the equality 68 | * @param iterator the target iterator 69 | * @param value the value 70 | * @param generic type of value 71 | * @return true if it has value, false otherwise 72 | */ 73 | public static boolean contains(Iterator iterator, T value){ 74 | while (iterator.hasNext()) { 75 | T next = iterator.next(); 76 | if(next.equals(value)) return true; 77 | } 78 | 79 | return false; 80 | } 81 | 82 | ////////////////////////////////////////////////////////////////////////////////////////// 83 | 84 | private CollectionUtils(){ 85 | // nothing 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/internal/ContentUpdateImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import android.content.ContentValues; 19 | 20 | import com.bingzer.android.dbv.queries.IQuery; 21 | import com.bingzer.android.dbv.queries.Update; 22 | import com.bingzer.android.dbv.utils.ContentValuesUtils; 23 | 24 | /** 25 | * Created by Ricky on 8/20/13. 26 | */ 27 | class ContentUpdateImpl implements Update { 28 | int value = 0; 29 | private ContentSet query; 30 | protected ContentValues contentValues; 31 | 32 | public ContentUpdateImpl(){ 33 | this(null); 34 | } 35 | 36 | public ContentUpdateImpl(ContentSet query){ 37 | this.query = query; 38 | } 39 | 40 | public void setValue(int value){ 41 | this.value = value; 42 | } 43 | 44 | @Override 45 | public Columns columns(final String... columns) { 46 | return new Columns() { 47 | @Override 48 | public IQuery val(Object... values) { 49 | return ContentUpdateImpl.this.val(columns, values); 50 | } 51 | }; 52 | } 53 | 54 | @Override 55 | public IQuery val(ContentValues values) { 56 | contentValues = values; 57 | 58 | return notifyContentValuesSet(); 59 | } 60 | 61 | @Override 62 | public IQuery val(String column, Object value) { 63 | contentValues = new ContentValues(); 64 | ContentValuesUtils.mapContentValuesFromGenericObject(contentValues, column, value); 65 | 66 | return notifyContentValuesSet(); 67 | } 68 | 69 | @Override 70 | public IQuery val(String[] columnNames, Object[] values) { 71 | contentValues = new ContentValues(); 72 | for(int i = 0; i < columnNames.length; i++){ 73 | ContentValuesUtils.mapContentValuesFromGenericObject(contentValues, columnNames[i], values[i]); 74 | } 75 | 76 | return notifyContentValuesSet(); 77 | } 78 | 79 | @Override 80 | public final Integer query() { 81 | return value; 82 | } 83 | 84 | // notify so that we can execute the update 85 | private IQuery notifyContentValuesSet(){ 86 | if(query != null) 87 | query.onContentValuesSet(this, contentValues); 88 | return this; 89 | } 90 | 91 | 92 | public static interface ContentSet { 93 | 94 | void onContentValuesSet(ContentUpdateImpl query, ContentValues contentValues); 95 | 96 | } 97 | } -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Deletable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import com.bingzer.android.dbv.IEntity; 20 | import com.bingzer.android.dbv.IEntityList; 21 | import com.bingzer.android.dbv.queries.Delete; 22 | 23 | import java.util.Collection; 24 | 25 | /** 26 | * Created by Ricky Tobing on 7/17/13. 27 | */ 28 | public interface Deletable { 29 | 30 | 31 | /** 32 | * Delete by id 33 | * @param id the id to delete 34 | * @return Delete object 35 | */ 36 | Delete delete(long id); 37 | 38 | /** 39 | * Bulk-remove by multiple ids 40 | * @param ids array of ids to delete 41 | * @return Delete object 42 | */ 43 | Delete delete(long... ids); 44 | 45 | /** 46 | * Bulk-remove by multiple ids 47 | * @param ids collection of ids to delete 48 | * @return Delete object 49 | */ 50 | Delete delete(Collection ids); 51 | 52 | ////////////////////////////////////////////////////////////////////////////// 53 | ////////////////////////////////////////////////////////////////////////////// 54 | 55 | /** 56 | * Delete add specified condition 57 | * @param condition the condition 58 | * @return Delete object 59 | */ 60 | Delete delete(String condition); 61 | 62 | /** 63 | * Delete add specified where clause 64 | * @param whereClause where clause 65 | * @param whereArgs arguments 66 | * @return Delete object 67 | */ 68 | Delete delete(String whereClause, Object... whereArgs); 69 | 70 | ////////////////////////////////////////////////////////////////////////////// 71 | ////////////////////////////////////////////////////////////////////////////// 72 | 73 | /** 74 | * Delete an entity. 75 | * This is equivalent of calling 76 | * delete(entity.getId()) 77 | * @param entity entity to delete 78 | * @return Delete object 79 | */ 80 | Delete delete(IEntity entity); 81 | 82 | /** 83 | * Bulk-delete several entities. 84 | * This is equivalent of calling 85 | * delete(list-of-ids) 86 | * @param entityList the entity list 87 | * @param extends IEntity 88 | * @return Delete object 89 | */ 90 | Delete delete(IEntityList entityList); 91 | 92 | ////////////////////////////////////////////////////////////////////////////// 93 | ////////////////////////////////////////////////////////////////////////////// 94 | } 95 | -------------------------------------------------------------------------------- /Sample/src/main/java/com/bingzer/android/dbv/sample/activities/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv.sample.activities; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.database.Cursor; 6 | import android.os.Bundle; 7 | import android.view.Menu; 8 | import android.view.MenuItem; 9 | import android.view.View; 10 | import android.widget.AdapterView; 11 | import android.widget.ListView; 12 | import android.widget.SimpleCursorAdapter; 13 | 14 | import com.bingzer.android.dbv.DbQuery; 15 | import com.bingzer.android.dbv.IDatabase; 16 | import com.bingzer.android.dbv.sample.R; 17 | 18 | /** 19 | * Chinook sample db is based on 20 | * 21 | * http://chinookdatabase.codeplex.com/wikipage?title=Chinook_Schema&referringTitle=Home 22 | */ 23 | public class MainActivity extends Activity { 24 | 25 | ListView mListView; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | 31 | setContentView(R.layout.activity_main); 32 | mListView = (ListView) findViewById(R.id.listView); 33 | mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 34 | @Override 35 | public void onItemClick(AdapterView adapterView, View view, int position, long id) { 36 | Intent intent = new Intent(getBaseContext(), AlbumDetail.class); 37 | intent.putExtra("ArtistId", id); 38 | startActivity(intent); 39 | } 40 | }); 41 | } 42 | 43 | @Override 44 | protected void onResume() { 45 | super.onResume(); 46 | 47 | populate(false); 48 | } 49 | 50 | @Override 51 | public boolean onCreateOptionsMenu(Menu menu) { 52 | getMenuInflater().inflate(R.menu.main, menu); 53 | return true; 54 | } 55 | 56 | @Override 57 | public boolean onOptionsItemSelected(MenuItem item) { 58 | if(item.getItemId() == R.id.action_all) 59 | return populate(false); 60 | if(item.getItemId() == R.id.action_album_only) 61 | return populate(true); 62 | return false; 63 | } 64 | 65 | 66 | private boolean populate(boolean filtered){ 67 | IDatabase db = DbQuery.getDatabase("Chinook"); 68 | Cursor cursor; 69 | 70 | if(!filtered) 71 | cursor = db.from("Artist") 72 | .select() 73 | .columns("*", "rowid as _id") 74 | .orderBy("Name") 75 | .query(); 76 | else 77 | cursor = db.from("Artist A") 78 | .join("Album B", "A.ArtistId = B.ArtistId") 79 | .select("B.Title is not null") 80 | .columns("A.Name", "A.ArtistId as _id") 81 | .groupBy("A.Name") 82 | .query(); 83 | 84 | SimpleCursorAdapter adapter = 85 | new SimpleCursorAdapter(this, 86 | android.R.layout.simple_list_item_1, 87 | cursor, 88 | new String[]{"Name"}, 89 | new int[]{ android.R.id.text1}); 90 | mListView.setAdapter(adapter); 91 | 92 | return true; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/ForeignKeyTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.content.Context; 4 | import android.test.AndroidTestCase; 5 | 6 | /** 7 | * Created by Ricky Tobing on 8/15/13. 8 | */ 9 | public class ForeignKeyTest extends AndroidTestCase { 10 | 11 | IDatabase db; 12 | 13 | @Override 14 | public void setUp(){ 15 | db = DbQuery.getDatabase("ForeignKeytest"); 16 | db.getConfig().setForeignKeySupport(true); 17 | db.open(1, new SQLiteBuilder() { 18 | @Override 19 | public Context getContext() { 20 | return ForeignKeyTest.this.getContext(); 21 | } 22 | 23 | @Override 24 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 25 | modeling.add("Customers") 26 | .addPrimaryKey("Id") 27 | .add("Name", "String") 28 | .add("Address", "Text") 29 | .index("Name") 30 | .index("Address"); 31 | 32 | modeling.add("Products") 33 | .addPrimaryKey("Id") 34 | .add("Name", "text") 35 | .addNumeric("Price") 36 | .index("Name"); 37 | 38 | modeling.add("Orders") 39 | .addPrimaryKey("Id") 40 | .add("Quantity", "Integer") 41 | .add("CustomerId", "Integer") 42 | .add("ProductId", "Integer") 43 | .index("CustomerId") 44 | .index("ProductId") 45 | .foreignKey("CustomerId", "Customers.Id") 46 | .foreignKey("ProductId", "Products", "Id", null); 47 | } 48 | 49 | @Override 50 | public void onError(Throwable error) { 51 | super.onError(error); 52 | assertFalse("Error should never be thrown out", true); 53 | } 54 | }); 55 | 56 | db.getConfig().setForeignKeySupport(true); 57 | db.from("Orders").delete(); 58 | db.from("Products").delete(); 59 | db.from("Customers").delete(); 60 | 61 | // two customers 62 | db.from("Customers").insertInto("Name", "Address").val("Baloteli", "Italy"); 63 | db.from("Customers").insertInto("Name", "Address").val("Pirlo", "Italy"); 64 | // two products 65 | db.from("Products").insertInto("Name", "Price").val("Computer", 1000); 66 | db.from("Products").insertInto("Name", "Price").val("Cellphone", 500); 67 | } 68 | 69 | // test away.. 70 | public void testInsertOrder_Fail(){ 71 | // right customerId wrong productId 72 | long custId = db.from("Customers").selectId("Name = ?", "Baloteli"); 73 | int prodId = 99999; 74 | 75 | try{ 76 | long id = db.from("Orders").insertInto("Quantity", "CustomerId", "ProductId").val(10, custId, prodId).query(); 77 | assertTrue(id < 0); 78 | assertTrue("Should throw error", false); 79 | } 80 | catch (Exception e){ 81 | assertTrue(true); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Joinable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bingzer.android.dbv.contracts; 18 | 19 | import com.bingzer.android.dbv.queries.InnerJoin; 20 | import com.bingzer.android.dbv.queries.LeftJoin; 21 | import com.bingzer.android.dbv.queries.OuterJoin; 22 | 23 | /** 24 | * Created by Ricky Tobing on 7/17/13. 25 | */ 26 | public interface Joinable { 27 | 28 | /** 29 | * Inner-Join 30 | */ 31 | public static interface Inner extends Joinable{ 32 | 33 | /** 34 | * Inner join a table 35 | * @param tableName table name to join 36 | * @param onClause the on clause 37 | * @return {@link com.bingzer.android.dbv.queries.InnerJoin} 38 | */ 39 | InnerJoin join(String tableName, String onClause); 40 | 41 | /** 42 | * Inner join a table 43 | * @param tableName table name to join 44 | * @param column1 first column 45 | * @param column2 second column 46 | * @return {@link com.bingzer.android.dbv.queries.InnerJoin} 47 | */ 48 | InnerJoin join(String tableName, String column1, String column2); 49 | } 50 | 51 | public static interface Left extends Joinable{ 52 | 53 | /** 54 | * Left join a table 55 | * @param tableName table name to join 56 | * @param onClause the on clause 57 | * @return {@link com.bingzer.android.dbv.queries.LeftJoin} 58 | */ 59 | LeftJoin leftJoin(String tableName, String onClause); 60 | 61 | /** 62 | * Left join a table 63 | * @param tableName table name to join 64 | * @param column1 first column 65 | * @param column2 second column 66 | * @return {@link com.bingzer.android.dbv.queries.LeftJoin} 67 | */ 68 | LeftJoin leftJoin(String tableName, String column1, String column2); 69 | } 70 | 71 | /** 72 | * Outer-Join 73 | */ 74 | public static interface Outer extends Joinable{ 75 | 76 | /** 77 | * Outer join a table 78 | * @param tableName table name to join 79 | * @param onClause the on clause 80 | * @return {@link com.bingzer.android.dbv.queries.OuterJoin} 81 | */ 82 | OuterJoin outerJoin(String tableName, String onClause); 83 | 84 | /** 85 | * Inner join a table 86 | * @param tableName table name to join 87 | * @param column1 first column 88 | * @param column2 second column 89 | * @return {@link com.bingzer.android.dbv.queries.OuterJoin} 90 | */ 91 | OuterJoin outerJoin(String tableName, String column1, String column2); 92 | } 93 | } -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/internal/ContentConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.content.contracts.IContentQueryConfig; 19 | 20 | /** 21 | * Created by Ricky on 8/22/13. 22 | */ 23 | class ContentConfig extends Config implements IContentQueryConfig{ 24 | 25 | private String[] defaultProjections; 26 | private String authority; 27 | 28 | public ContentConfig(){ 29 | setIdNamingConvention("_id"); 30 | setDefaultProjections(); 31 | } 32 | 33 | /** 34 | * Sets the default projections (columns) unless if otherwise 35 | * specified with {@link com.bingzer.android.dbv.queries.Select#columns(String...)} 36 | * in a select statement. 37 | * By default the default projections is 38 | * {@link com.bingzer.android.dbv.IConfig#getIdNamingConvention()} 39 | * @param columns the columns to set 40 | */ 41 | @Override 42 | public void setDefaultProjections(String... columns){ 43 | if(columns == null || columns.length == 0) 44 | defaultProjections = new String[] { getIdNamingConvention() }; 45 | else { 46 | for (String column : columns) { 47 | if (column.equalsIgnoreCase(getIdNamingConvention())) { 48 | setIdNamingConvention(column); 49 | } 50 | } 51 | defaultProjections = columns; 52 | } 53 | } 54 | 55 | /** 56 | * Returns the default projections 57 | * @return projections 58 | */ 59 | @Override 60 | public String[] getDefaultProjections(){ 61 | return defaultProjections; 62 | } 63 | 64 | /** 65 | * Sets the default Authority 66 | * @param authority authority to set 67 | */ 68 | @Override 69 | public void setDefaultAuthority(String authority){ 70 | this.authority = authority; 71 | } 72 | 73 | /** 74 | * Returns the authority 75 | * @return Authority 76 | */ 77 | @Override 78 | public String getDefaultAuthority(){ 79 | return authority; 80 | } 81 | 82 | @Override 83 | public void setIdNamingConvention(String id) { 84 | String oldId = getIdNamingConvention(); 85 | super.setIdNamingConvention(id); 86 | 87 | if(defaultProjections != null && defaultProjections.length > 0){ 88 | for(int i = 0; i < defaultProjections.length; i++){ 89 | if(defaultProjections[i].equalsIgnoreCase(oldId)){ 90 | defaultProjections[i] = id; 91 | } 92 | } 93 | } 94 | else setDefaultProjections(); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/content/ContentQuery.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.content; 17 | 18 | import android.content.Context; 19 | import android.net.Uri; 20 | 21 | import com.bingzer.android.dbv.content.contracts.IResolver; 22 | import com.bingzer.android.dbv.content.contracts.IStrictResolver; 23 | import com.bingzer.android.dbv.internal.Resolver; 24 | import com.bingzer.android.dbv.internal.StrictResolver; 25 | 26 | /** 27 | * ContentQuery allows DbQuery style while 28 | * querying data from ContentProvider. 29 | * 30 | * For complete documentation please refer to: 31 | * https://github.com/bingzer/DbQuery/wiki/ContentQuery 32 | * 33 | * @see com.bingzer.android.dbv.content.contracts.IResolver 34 | * @see com.bingzer.android.dbv.content.contracts.IStrictResolver 35 | */ 36 | public final class ContentQuery { 37 | 38 | /** 39 | * Creates an {@link com.bingzer.android.dbv.content.contracts.IResolver} for the specified URI 40 | * @param uri the uri string 41 | * @param context GOD-object {@link Context} 42 | * @return {@link com.bingzer.android.dbv.content.contracts.IResolver} 43 | */ 44 | public static IResolver resolve(String uri, Context context){ 45 | return resolve(Uri.parse(uri), context); 46 | } 47 | 48 | /** 49 | * Creates an {@link IResolver} for the specified URI 50 | * @param uri the uri object 51 | * @param context GOD-object {@link Context} 52 | * @return {@link IResolver} 53 | */ 54 | public static IResolver resolve(Uri uri, Context context){ 55 | return new Resolver(uri, context); 56 | } 57 | 58 | /** 59 | * Creates an {@link com.bingzer.android.dbv.content.contracts.IStrictResolver} for the specified URI 60 | * @param uri the uri string 61 | * @param context GOD-object {@link Context} 62 | * @return {@link com.bingzer.android.dbv.content.contracts.IStrictResolver} 63 | */ 64 | public static IStrictResolver strictlyResolve(String uri, Context context){ 65 | return strictlyResolve(Uri.parse(uri), context); 66 | } 67 | 68 | /** 69 | * Creates an {@link IStrictResolver} for the specified URI 70 | * @param uri the uri object 71 | * @param context GOD-object {@link Context} 72 | * @return {@link IStrictResolver} 73 | */ 74 | public static IStrictResolver strictlyResolve(Uri uri, Context context){ 75 | return new StrictResolver(uri, context); 76 | } 77 | 78 | ////////////////////////////////////////////////////////////////////////////////////////////// 79 | 80 | private ContentQuery(){ 81 | // nothing 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/internal/FunctionImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.queries.IFunction; 19 | 20 | import java.util.Locale; 21 | 22 | /** 23 | * Created by Ricky Tobing on 7/20/13. 24 | */ 25 | class FunctionImpl implements IFunction { 26 | 27 | private double value; 28 | private final StringBuilder builder; 29 | 30 | FunctionImpl(String functionName, String tableName, String columnName, String condition){ 31 | builder = new StringBuilder("SELECT ") 32 | .append(functionName).append("(").append(columnName).append(") AS FN ") 33 | .append(" FROM ").append(tableName); 34 | 35 | if(condition != null){ 36 | // append where if necessary 37 | if(!condition.toLowerCase(Locale.getDefault()).startsWith("where")) 38 | builder.append(" WHERE "); 39 | // safely prepare the where part 40 | builder.append(condition); 41 | } 42 | } 43 | 44 | //////////////////////////////////////////////////////////////////////////////////////// 45 | 46 | public void setValue(double value){ 47 | this.value = value; 48 | } 49 | 50 | //////////////////////////////////////////////////////////////////////////////////////// 51 | 52 | @Override 53 | public int asInt() { 54 | try{ 55 | return (int) value; 56 | } 57 | catch (NumberFormatException e){ 58 | throw new IllegalArgumentException(e); 59 | } 60 | } 61 | 62 | @Override 63 | public long asLong() { 64 | try{ 65 | return (long) value; 66 | } 67 | catch (NumberFormatException e){ 68 | throw new IllegalArgumentException(e); 69 | } 70 | } 71 | 72 | @Override 73 | public float asFloat() { 74 | try{ 75 | return (float) value; 76 | } 77 | catch (NumberFormatException e){ 78 | throw new IllegalArgumentException(e); 79 | } 80 | } 81 | 82 | @Override 83 | public double asDouble() { 84 | try{ 85 | return value; 86 | } 87 | catch (NumberFormatException e){ 88 | throw new IllegalArgumentException(e); 89 | } 90 | } 91 | 92 | @Override 93 | public String asString() { 94 | return value() == null ? null : value().toString(); 95 | } 96 | 97 | @Override 98 | public Object value() { 99 | return value; 100 | } 101 | 102 | @Override 103 | public String toString(){ 104 | return builder.toString(); 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/content/contracts/StrictSelectable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.content.contracts; 17 | 18 | import android.database.Cursor; 19 | 20 | import com.bingzer.android.dbv.contracts.EntitySelectable; 21 | import com.bingzer.android.dbv.queries.IQuery; 22 | 23 | /** 24 | * Created by Ricky Tobing on 8/23/13. 25 | */ 26 | public interface StrictSelectable { 27 | 28 | /** 29 | * Select some condition 30 | * @param condition the condition 31 | * @return {@link com.bingzer.android.dbv.queries.Select} 32 | */ 33 | Select select(String condition); 34 | 35 | /** 36 | * Select id. Id column must be defined in the naming convention 37 | * specified in {@link com.bingzer.android.dbv.IConfig} 38 | * @param id id to search 39 | * @see com.bingzer.android.dbv.IConfig#setIdNamingConvention(String) 40 | * @return {@link com.bingzer.android.dbv.queries.Select} 41 | */ 42 | Select select(long id); 43 | 44 | /** 45 | * Select multiple ids 46 | * @param ids array id 47 | * @return {@link com.bingzer.android.dbv.queries.Select} 48 | */ 49 | Select select(long... ids); 50 | 51 | /** 52 | * Select along with whereClause 53 | * @param whereClause 'where' clause 54 | * @param args arguments 55 | * @return {@link com.bingzer.android.dbv.queries.Select} 56 | */ 57 | Select select(String whereClause, Object... args); 58 | 59 | /** 60 | * For select statement 61 | *

62 | * Find a complete Wiki and documentation here:
63 | * https://github.com/bingzer/DbQuery/wiki 64 | *

65 | */ 66 | public static interface Select extends IQuery, EntitySelectable { 67 | 68 | /** 69 | * Specified the column to return. 70 | * default or null will produce SELECT * FROM 71 | * @param columns column names 72 | * @return {@link com.bingzer.android.dbv.queries.Select} 73 | */ 74 | Select columns(String... columns); 75 | 76 | /** 77 | * Order by. To create multiple orderBy ASC or DESC or both, 78 | * this is possible 79 | *
80 |          * 
81 |          *   db.from("Table").select().orderBy("Id", "Name", "Price DESC");
82 |          * 
83 |          * 
84 | * @param columns column names 85 | * @return {@link com.bingzer.android.dbv.queries.Select} 86 | */ 87 | OrderBy orderBy(String... columns); 88 | 89 | /** 90 | * Order By 91 | */ 92 | public static interface OrderBy extends IQuery, EntitySelectable { 93 | 94 | } 95 | 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/DbQuery.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv; 17 | 18 | import com.bingzer.android.dbv.internal.Database; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Collection; 22 | 23 | /** 24 | * {@linkplain com.bingzer.android.dbv.DbQuery} is your gateway to having 25 | * a fluent way of querying SQL in Android. 26 | *

27 | * DbQuery provides access to {@link IDatabase}. To use DbQuery, 28 | * you must follow special conventions when it comes to "Id" as primary key 29 | * in every table in your database. 30 | *

31 | * 32 | *

33 | * Warning:
34 | * DbQuery will assume 35 | * that every table will follow a naming convention for 36 | * their identifier scheme. By default, "Id" is assigned 37 | * automatically. For more information see {@link IConfig} 38 | *

39 | * 40 | *

41 | * Sample Code: 42 | *


 43 |  * IDatabase db = DbQuery.getDatabase("{database-name}");
 44 |  * ...
 45 |  * 
46 | * 47 | * 48 | *

49 | * DbQuery is totally open! 50 | *

60 | * 61 | * 62 | *

63 | * Important: This entire javadoc is not read-proof. Some may be up to date, others may not 64 | * 65 | * 66 | * @version 2.0 67 | * @see IConfig 68 | * @see IDatabase 69 | * @author Ricky Tobing 70 | */ 71 | public final class DbQuery { 72 | 73 | private static Collection databaseList = new ArrayList(); 74 | 75 | public static final String Version = "2.2"; 76 | 77 | /** 78 | * Returns the {@link IDatabase} object with the specified name. 79 | * 80 | * @param databaseName the database 81 | * @return {@link IDatabase} object 82 | */ 83 | public static IDatabase getDatabase(String databaseName){ 84 | for(IDatabase db : databaseList){ 85 | if(db.getName().equalsIgnoreCase(databaseName)){ 86 | return db; 87 | } 88 | } 89 | 90 | // else... 91 | IDatabase db = new Database(databaseName); 92 | databaseList.add(db); 93 | 94 | return getDatabase(databaseName); 95 | } 96 | 97 | 98 | ///////////////////////////////////////////////////////////////////////////////// 99 | private DbQuery(){ 100 | // nothing 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/utils/DelegateUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.utils; 17 | 18 | import android.annotation.TargetApi; 19 | import android.database.AbstractWindowedCursor; 20 | import android.database.Cursor; 21 | import android.os.Build; 22 | 23 | import com.bingzer.android.dbv.Delegate; 24 | 25 | /** 26 | * Created by Ricky on 4/26/2014. 27 | */ 28 | public final class DelegateUtils { 29 | /** 30 | * Map action from a cursor. Based on what type of an action is, this method will 31 | * map the value from cursor.getXXX() where XXX is a type. 32 | * 33 | * @param delegate the action to map 34 | * @param cursor the target cursor 35 | * @param index the index in the cursor 36 | */ 37 | @SuppressWarnings("unchecked") 38 | public static void mapDelegateFromCursor(Delegate delegate, Cursor cursor, int index){ 39 | if(delegate.getType() == String.class) delegate.set(cursor.getString(index)); 40 | else if(delegate.getType() == Integer.class) delegate.set(cursor.getInt(index)); 41 | else if(delegate.getType() == Boolean.class) delegate.set(cursor.getInt(index) == 1); 42 | else if(delegate.getType() == Double.class) delegate.set(cursor.getDouble(index)); 43 | else if(delegate.getType() == Long.class) delegate.set(cursor.getLong(index)); 44 | else if(delegate.getType() == Short.class) delegate.set(cursor.getShort(index)); 45 | else if(delegate.getType() == Float.class) delegate.set(cursor.getFloat(index)); 46 | else if(delegate.getType() == byte[].class) delegate.set(cursor.getBlob(index)); 47 | else if(delegate.getType() == Object.class) delegate.set(getObjectFromCursor(cursor, index)); 48 | 49 | // TODO: Fix the exception message 50 | else throw new IllegalArgumentException("Unmapped"); 51 | } 52 | 53 | /** 54 | * Try to from an 'object' from a cursor 55 | * @param cursor the target cursor 56 | * @param index the index in the cursor 57 | * @return an object 58 | */ 59 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 60 | protected static Object getObjectFromCursor(Cursor cursor, int index){ 61 | switch (cursor.getType(index)){ 62 | case Cursor.FIELD_TYPE_BLOB: 63 | return cursor.getBlob(index); 64 | case Cursor.FIELD_TYPE_FLOAT: 65 | return cursor.getDouble(index); 66 | case Cursor.FIELD_TYPE_INTEGER: 67 | return cursor.getLong(index); 68 | case Cursor.FIELD_TYPE_STRING: 69 | return cursor.getString(index); 70 | default: 71 | case Cursor.FIELD_TYPE_NULL: 72 | return null; 73 | } 74 | } 75 | 76 | /////////////////////////////////////////////////////////////////////////////////////////////// 77 | 78 | private DelegateUtils() { 79 | // nothing 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/ForeignKeyTest2.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.content.Context; 4 | import android.test.AndroidTestCase; 5 | 6 | /** 7 | * Created by Ricky Tobing on 8/15/13. 8 | */ 9 | public class ForeignKeyTest2 extends AndroidTestCase { 10 | 11 | IDatabase db; 12 | 13 | @Override 14 | public void setUp(){ 15 | db = DbQuery.getDatabase("ForeignKeytest2"); 16 | db.getConfig().setForeignKeySupport(true); 17 | db.open(1, new SQLiteBuilder() { 18 | @Override 19 | public Context getContext() { 20 | return ForeignKeyTest2.this.getContext(); 21 | } 22 | 23 | @Override 24 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 25 | modeling.add("Customers") 26 | .addPrimaryKey("Id") 27 | .add("Name", "String") 28 | .add("Address", "Text") 29 | .index("Name") 30 | .index("Address"); 31 | 32 | modeling.add("Products") 33 | .addPrimaryKey("Id") 34 | .add("Name", "text") 35 | .addNumeric("Price") 36 | .index("Name"); 37 | 38 | modeling.add("Orders") 39 | .addPrimaryKey("Id") 40 | .add("Quantity", "Integer") 41 | .add("CustomerId", "Integer") 42 | .add("ProductId", "Integer") 43 | .index("CustomerId") 44 | .index("ProductId") 45 | .foreignKey("CustomerId", "Customers.Id", "ON DELETE CASCADE") 46 | .foreignKey("ProductId", "Products", "Id", "ON DELETE CASCADE"); 47 | } 48 | 49 | @Override 50 | public void onError(Throwable error) { 51 | super.onError(error); 52 | assertFalse("Error should never be thrown out", true); 53 | } 54 | }); 55 | 56 | db.from("Orders").delete(); 57 | db.from("Products").delete(); 58 | db.from("Customers").delete(); 59 | 60 | // two customers 61 | db.from("Customers").insertInto("Name", "Address").val("Baloteli", "Italy"); 62 | db.from("Customers").insertInto("Name", "Address").val("Pirlo", "Italy"); 63 | // two products 64 | db.from("Products").insertInto("Name", "Price").val("Computer", 1000); 65 | db.from("Products").insertInto("Name", "Price").val("Cellphone", 500); 66 | 67 | long baloteliId = db.from("Customers").selectId("Name = ?", "Baloteli"); 68 | long computerId = db.from("Products").selectId("Name = ?", "Computer"); 69 | // orders 70 | db.from("Orders").insertInto("Quantity", "CustomerId", "ProductId") 71 | .val(10, baloteliId, computerId); 72 | } 73 | 74 | // test away.. 75 | public void testDeleteCascade(){ 76 | long baloteliId = db.from("Customers").selectId("Name = ?", "Baloteli"); 77 | long computerId = db.from("Products").selectId("Name = ?", "Computer"); 78 | 79 | assertTrue(db.from("Orders").has("CustomerId = ? AND ProductId = ?", baloteliId, computerId)); 80 | 81 | // delete baloteli 82 | assertEquals(1, (int) db.from("Customers").delete("Name = ?", "Baloteli").query()); 83 | // baloteli should be deleted 84 | assertFalse(db.from("Customers").has(baloteliId)); 85 | 86 | assertFalse(db.from("Orders").has("CustomerId = ? AND ProductId = ?", baloteliId, computerId)); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Library/src/androidTest/java/com/bingzer/android/dbv/ReadOnlyDbTest.java: -------------------------------------------------------------------------------- 1 | package com.bingzer.android.dbv; 2 | 3 | import android.content.Context; 4 | import android.test.AndroidTestCase; 5 | 6 | /** 7 | * Created by Ricky on 8/17/13. 8 | */ 9 | public class ReadOnlyDbTest extends AndroidTestCase{ 10 | 11 | IDatabase db; 12 | 13 | @Override 14 | public void setUp(){ 15 | db = DbQuery.getDatabase("ReadOnlyDbTest"); 16 | db.getConfig().setReadOnly(true); 17 | db.open(1, new SQLiteBuilder() { 18 | @Override 19 | public Context getContext() { 20 | return ReadOnlyDbTest.this.getContext(); 21 | } 22 | 23 | @Override 24 | public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) { 25 | modeling.add("Person") 26 | .addPrimaryKey("Id") 27 | .add("Name", "String") 28 | .add("Age", "Integer") 29 | .addBlob("Address"); 30 | } 31 | }); 32 | } 33 | 34 | @Override 35 | protected void tearDown() throws Exception { 36 | super.tearDown(); 37 | db.close(); 38 | } 39 | 40 | public void testDbIsReadOnly(){ 41 | assertTrue(db.getConfig().isReadOnly()); 42 | } 43 | 44 | public void testQuery_ColumnCount(){ 45 | assertTrue(db.from("Person").getColumnCount() == 4); 46 | } 47 | 48 | public void testInsert_Throw(){ 49 | try{ 50 | db.from("Person").insert("Name", "Ricky").query(); 51 | fail("Should throw error"); 52 | } 53 | catch (IllegalAccessError e){ 54 | // good 55 | } 56 | } 57 | 58 | public void testUpdate_Throw(){ 59 | try{ 60 | db.from("Person").update("Name = ? ", "Ricky").columns("Name").val().query(); 61 | fail("Should throw error"); 62 | } 63 | catch (IllegalAccessError e){ 64 | // good 65 | } 66 | } 67 | 68 | public void testDelete_Throw(){ 69 | try{ 70 | db.from("Person").delete(); 71 | fail("Should throw error"); 72 | } 73 | catch (IllegalAccessError e){ 74 | // good 75 | } 76 | } 77 | 78 | public void testDrop_Throw(){ 79 | try{ 80 | db.from("Person").drop(); 81 | fail("Should throw error"); 82 | } 83 | catch (IllegalAccessError e){ 84 | // good 85 | } 86 | } 87 | 88 | 89 | public void testInsert_Update_Delete_OK(){ 90 | db.getConfig().setReadOnly(false); 91 | 92 | try{ 93 | long id = db.from("Person").insert("Name", "Ricky").query(); 94 | assertTrue(db.from("Person").has(id)); 95 | 96 | int numUpdated = db.from("Person").update("Name = ? ", "Ricky").columns("Name").val("Edited").query(); 97 | assertTrue(numUpdated > 0); 98 | assertTrue(db.from("Person").has("Name = ?", "Edited")); 99 | 100 | int numDeleted = db.from("Person").delete("Name = ?", "Edited").query(); 101 | assertTrue(numDeleted > 0); 102 | assertFalse(db.from("Person").has("Name = ?", "Edited")); 103 | } 104 | catch (IllegalAccessError e){ 105 | fail("Should NOT throw error"); 106 | } 107 | } 108 | 109 | public void testInsert_OK(){ 110 | db.getConfig().setReadOnly(false); 111 | long newId = db.from("Person").insert("Name", "Ricky").query(); 112 | assertTrue(newId > 0); 113 | newId = db.from("Person").insert("Name", "Ricky").query(); 114 | assertTrue(newId > 0); 115 | 116 | db.close(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Extensions/ContentQuery/src/main/java/com/bingzer/android/dbv/internal/ContentStrictSelectImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.internal; 17 | 18 | import com.bingzer.android.dbv.content.contracts.StrictSelectable; 19 | import com.bingzer.android.dbv.utils.Utils; 20 | import com.bingzer.android.dbv.content.contracts.IBaseResolver; 21 | 22 | /** 23 | * Created by Ricky Tobing on 8/23/13. 24 | */ 25 | abstract class ContentStrictSelectImpl implements StrictSelectable.Select, StrictSelectable.Select.OrderBy{ 26 | 27 | private final IBaseResolver resolver; 28 | private StringBuilder columnString; 29 | private String orderByString; 30 | private String whereString; 31 | private Object[] whereArgs; 32 | 33 | public ContentStrictSelectImpl(IBaseResolver resolver){ 34 | this.resolver = resolver; 35 | this.columnString = new StringBuilder(); 36 | this.columnString.append(Utils.join(", ", generateDefaultProjections())); 37 | } 38 | 39 | @Override 40 | public ContentStrictSelectImpl columns(String... columns) { 41 | columnString.delete(0, columnString.length()); 42 | if(columns != null){ 43 | columnString.append(Utils.join(", ", columns)); 44 | } 45 | else{ 46 | columnString.append(Utils.join(", ", generateDefaultProjections())); 47 | } 48 | 49 | return this; 50 | } 51 | 52 | @Override 53 | public OrderBy orderBy(String... columns) { 54 | orderByString = Utils.join(",", columns); 55 | return this; 56 | } 57 | 58 | public ContentStrictSelectImpl where(String whereClause, Object... args){ 59 | this.whereString = whereClause; 60 | this.whereArgs = args; 61 | return this; 62 | } 63 | 64 | /** 65 | * This is columns 66 | * @return array of columns names 67 | */ 68 | public String[] getProjections(){ 69 | String[] projections = columnString.toString().split(","); 70 | for(int i = 0; i < projections.length; i++){ 71 | projections[i] = projections[i].trim(); 72 | } 73 | return projections; 74 | } 75 | 76 | /** 77 | * This is the where string 78 | * @return where clause 79 | */ 80 | public String getSelection(){ 81 | return whereString; 82 | } 83 | 84 | /** 85 | * Where args 86 | * @return selection args 87 | */ 88 | public String[] getSelectionArgs(){ 89 | return Utils.toStringArray(whereArgs); 90 | } 91 | 92 | /** 93 | * Order by 94 | * @return sorting order as string 95 | */ 96 | public String getSortingOrder(){ 97 | return orderByString; 98 | } 99 | 100 | String[] generateDefaultProjections(){ 101 | String[] projections = resolver.getContentConfig().getDefaultProjections(); 102 | for(int i = 0; i < projections.length; i++){ 103 | if(projections[i].equals(resolver.getContentConfig().getIdNamingConvention())){ 104 | projections[i] = resolver.getPrimaryKeyColumn(); 105 | } 106 | } 107 | 108 | return projections; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /Library/src/main/java/com/bingzer/android/dbv/contracts/Updatable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Ricky Tobing 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance insert the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bingzer.android.dbv.contracts; 17 | 18 | import android.content.ContentValues; 19 | 20 | import com.bingzer.android.dbv.IEntity; 21 | import com.bingzer.android.dbv.IEntityList; 22 | import com.bingzer.android.dbv.queries.IQuery; 23 | import com.bingzer.android.dbv.queries.Update; 24 | 25 | /** 26 | * Created by Ricky Tobing on 7/17/13. 27 | */ 28 | public interface Updatable { 29 | 30 | /** 31 | * Update using id 32 | * @param id the id 33 | * @return Update object 34 | */ 35 | Update update(long id); 36 | 37 | /** 38 | * Bulk-update using ids 39 | * @param ids the ids 40 | * @return Update object 41 | */ 42 | Update update(long... ids); 43 | 44 | /** 45 | * Update using a condition 46 | * @param condition the condition 47 | * @return Update object 48 | */ 49 | Update update(String condition); 50 | 51 | /** 52 | * Update using a condition 53 | * @param whereClause whereClause 54 | * @param whereArgs arguments 55 | * @return Update object 56 | */ 57 | Update update(String whereClause, Object... whereArgs); 58 | 59 | /////////////////////////////////////////////////////////////////////////////////////////////// 60 | /////////////////////////////////////////////////////////////////////////////////////////////// 61 | 62 | /** 63 | * Update using an {@link IEntity} object 64 | * @param entity the entity to update 65 | * @return Update object 66 | */ 67 | IQuery update(IEntity entity); 68 | 69 | /** 70 | * Bulk-update using {@link IEntityList} object. 71 | * query() method will return -1 if there's an error updating and the 72 | * transaction is rollback-ed. Otherwise, it will return the number of records updated 73 | * 74 | * @param entityList IEntityList object 75 | * @param extends IEntity 76 | * @return Update object 77 | */ 78 | IQuery update(IEntityList entityList); 79 | 80 | /////////////////////////////////////////////////////////////////////////////////////////////// 81 | /////////////////////////////////////////////////////////////////////////////////////////////// 82 | 83 | /** 84 | * Update using {@link ContentValues} insert specified id 85 | * @param contents the ContentValues 86 | * @param id the id 87 | * @return Update object 88 | */ 89 | IQuery update(ContentValues contents, long id); 90 | 91 | /** 92 | * Update using the {@link ContentValues} 93 | * @param contents the ContentValues 94 | * @param condition the condition 95 | * @return Update object 96 | */ 97 | IQuery update(ContentValues contents, String condition); 98 | 99 | /** 100 | * Update using the {@link ContentValues} 101 | * @param contents the ContentValues 102 | * @param whereClause whereClause 103 | * @param whereArgs arguments 104 | * @return Update object 105 | */ 106 | IQuery update(ContentValues contents, String whereClause, Object... whereArgs); 107 | } 108 | --------------------------------------------------------------------------------