├── Library
├── app
│ ├── .gitignore
│ ├── src
│ │ └── main
│ │ │ ├── AndroidManifest.xml
│ │ │ └── java
│ │ │ └── com
│ │ │ └── yayandroid
│ │ │ └── databasemanager
│ │ │ ├── annotation
│ │ │ └── ColumnName.java
│ │ │ ├── model
│ │ │ ├── Query.java
│ │ │ └── Database.java
│ │ │ ├── utility
│ │ │ └── InjectionManager.java
│ │ │ ├── listener
│ │ │ └── QueryListener.java
│ │ │ └── DatabaseManager.java
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── app.iml
├── settings.gradle
├── .gitignore
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── build.gradle
├── Library.iml
├── DatabaseManager.iml
├── gradle.properties
├── gradlew.bat
├── maven_push.gradle
└── gradlew
├── Sample
├── app
│ ├── .gitignore
│ ├── src
│ │ ├── main
│ │ │ ├── res
│ │ │ │ ├── mipmap-hdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── mipmap-mdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── mipmap-xhdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── mipmap-xxhdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── mipmap-xxxhdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── values
│ │ │ │ │ ├── colors.xml
│ │ │ │ │ ├── dimens.xml
│ │ │ │ │ ├── strings.xml
│ │ │ │ │ └── styles.xml
│ │ │ │ ├── values-w820dp
│ │ │ │ │ └── dimens.xml
│ │ │ │ └── layout
│ │ │ │ │ ├── recycler_item_ticket.xml
│ │ │ │ │ ├── activity_dblist.xml
│ │ │ │ │ └── activity_main.xml
│ │ │ ├── assets
│ │ │ │ └── databases
│ │ │ │ │ └── tickets_assets.s3db
│ │ │ ├── java
│ │ │ │ └── com
│ │ │ │ │ └── yayandroid
│ │ │ │ │ └── databasemanager
│ │ │ │ │ └── sample
│ │ │ │ │ ├── helper
│ │ │ │ │ ├── DbOpenHelperAssets.java
│ │ │ │ │ └── DbOpenHelperLocal.java
│ │ │ │ │ ├── Constants.java
│ │ │ │ │ ├── activity
│ │ │ │ │ ├── BaseActivity.java
│ │ │ │ │ ├── MainActivity.java
│ │ │ │ │ └── DBListActivity.java
│ │ │ │ │ ├── model
│ │ │ │ │ └── Ticket.java
│ │ │ │ │ ├── SampleApplication.java
│ │ │ │ │ └── adapter
│ │ │ │ │ └── TicketAdapter.java
│ │ │ └── AndroidManifest.xml
│ │ ├── test
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── yayandroid
│ │ │ │ └── databasemanager
│ │ │ │ └── ExampleUnitTest.java
│ │ └── androidTest
│ │ │ └── java
│ │ │ └── com
│ │ │ └── yayandroid
│ │ │ └── databasemanager
│ │ │ └── ApplicationTest.java
│ ├── proguard-rules.pro
│ ├── build.gradle
│ └── app.iml
├── settings.gradle
├── .gitignore
├── disc_database
│ └── tickets_disc.s3db
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── build.gradle
├── gradle.properties
├── Sample.iml
├── DatabaseManager.iml
├── gradlew.bat
└── gradlew
└── README.md
/Library/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/Sample/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/Library/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/Sample/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/Library/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | .idea
3 | .DS_Store
4 | /local.properties
5 | /build
6 | /captures
7 |
--------------------------------------------------------------------------------
/Sample/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | .idea
3 | .DS_Store
4 | /local.properties
5 | /build
6 | /captures
7 |
--------------------------------------------------------------------------------
/Sample/disc_database/tickets_disc.s3db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Sample/disc_database/tickets_disc.s3db
--------------------------------------------------------------------------------
/Library/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Sample/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Sample/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/Library/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Library/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/Sample/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Sample/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Sample/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Sample/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Sample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Sample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/app/src/main/assets/databases/tickets_assets.s3db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yayaa/DatabaseManager/HEAD/Sample/app/src/main/assets/databases/tickets_assets.s3db
--------------------------------------------------------------------------------
/Sample/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/Library/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Dec 29 09:10:08 EET 2015
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.4-all.zip
7 |
--------------------------------------------------------------------------------
/Sample/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Dec 29 09:10:08 EET 2015
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.4-all.zip
7 |
--------------------------------------------------------------------------------
/Sample/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 |
6 | 16sp
7 | 12sp
8 |
9 | 1dp
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Sample/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/Sample/app/src/test/java/com/yayandroid/databasemanager/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/Sample/app/src/androidTest/java/com/yayandroid/databasemanager/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/Library/app/src/main/java/com/yayandroid/databasemanager/annotation/ColumnName.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.annotation;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | /**
9 | * Created by Yahya Bayramoglu on 30/12/15.
10 | */
11 | @Retention(RetentionPolicy.RUNTIME)
12 | @Target(ElementType.FIELD)
13 | public @interface ColumnName {
14 | String value();
15 | }
--------------------------------------------------------------------------------
/Sample/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:1.3.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/Library/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:1.3.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/helper/DbOpenHelperAssets.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample.helper;
2 |
3 | import android.content.Context;
4 |
5 | import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
6 |
7 | /**
8 | * Created by Yahya Bayramoglu on 31/12/15.
9 | */
10 | public class DbOpenHelperAssets extends SQLiteAssetHelper {
11 |
12 | private static final String DATABASE_NAME = "tickets_assets.s3db";
13 | private static final int DATABASE_VERSION = 1;
14 |
15 | public DbOpenHelperAssets(Context context) {
16 | super(context, DATABASE_NAME, null, DATABASE_VERSION);
17 | }
18 |
19 | }
--------------------------------------------------------------------------------
/Library/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 23
10 | versionCode 4
11 | versionName "1.0.3"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 | compile 'com.android.support:support-annotations:23.1.1'
24 | }
25 |
26 | apply from: '../maven_push.gradle'
--------------------------------------------------------------------------------
/Library/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/ybayram/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/Sample/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/ybayram/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/Sample/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | DatabaseManager
4 |
5 | Add New Ticket
6 | ReadOnly!
7 | Delete
8 | Use Reflection
9 |
10 | Local Database
11 | Assets Database
12 | Disc Database
13 |
14 | Compare databases in DISC and in Assets,\nFind tickets in Disc but not in Assets
15 |
16 | No Tickets Yet!
17 | No Difference Found!
18 |
19 |
--------------------------------------------------------------------------------
/Sample/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Sample/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/Sample/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 | defaultConfig {
7 | applicationId 'com.yayandroid.databasemanager.sample'
8 | minSdkVersion 14
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | ext {
22 | supportLibVersion = '23.1.1'
23 | }
24 |
25 | dependencies {
26 | compile fileTree(include: ['*.jar'], dir: 'libs')
27 |
28 | compile "com.android.support:appcompat-v7:${supportLibVersion}"
29 | compile "com.android.support:recyclerview-v7:${supportLibVersion}"
30 | compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
31 |
32 | compile 'com.yayandroid:DatabaseManager:1.0.3'
33 | }
--------------------------------------------------------------------------------
/Library/Library.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/Sample/Sample.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/Sample/DatabaseManager.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/Library/DatabaseManager.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/Sample/app/src/main/res/layout/recycler_item_ticket.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
25 |
26 |
--------------------------------------------------------------------------------
/Sample/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
19 |
20 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Sample/app/src/main/res/layout/activity_dblist.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
18 |
19 |
23 |
24 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/Constants.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample;
2 |
3 | /**
4 | * Created by Yahya Bayramoglu on 30/12/15.
5 | */
6 | public class Constants {
7 |
8 | public static final String DB_TAG_LOCAL = "localDB";
9 | public static final String DB_TAG_ASSETS = "assetsDB";
10 | public static final String DB_TAG_DISC = "discDB";
11 |
12 | public static final String TABLE_TICKET = "tbl_Ticket";
13 |
14 | public static final String INTENT_KEY_TAG = "databaseTag";
15 | public static final String INTENT_KEY_LIST = "ticketList";
16 |
17 | public static final String SELECT_TICKETS = "SELECT * FROM " + Constants.TABLE_TICKET;
18 | public static final String INSERT_TICKET = "INSERT INTO " + Constants.TABLE_TICKET + " VALUES (?, ?)";
19 | public static final String DELETE_TICKET = "DELETE FROM " + Constants.TABLE_TICKET + " WHERE ticketId=?";
20 |
21 | public static final String COMPARISON_QUERY = "SELECT discTickets.TicketId, discTickets.CreateDate"
22 | + " FROM " + Constants.TABLE_TICKET + " AS discTickets"
23 | + " LEFT JOIN " + Constants.DB_TAG_ASSETS + "." + Constants.TABLE_TICKET + " AS assetsTickets"
24 | + " ON discTickets.TicketId = assetsTickets.TicketId"
25 | + " WHERE assetsTickets.TicketId IS NULL";
26 |
27 | }
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/helper/DbOpenHelperLocal.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample.helper;
2 |
3 | import android.content.Context;
4 | import android.database.sqlite.SQLiteDatabase;
5 | import android.database.sqlite.SQLiteOpenHelper;
6 |
7 | import com.yayandroid.databasemanager.sample.Constants;
8 |
9 | /**
10 | * Created by Yahya Bayramoglu on 30.12.2015.
11 | */
12 | public class DbOpenHelperLocal extends SQLiteOpenHelper {
13 |
14 | private static final String DATABASE_NAME = "tickets_local.db";
15 | private static final int DATABASE_VERSION = 1;
16 |
17 | private final String TABLE_CREATE_LISTING = "CREATE TABLE "
18 | + Constants.TABLE_TICKET + " ( \"TicketId\" TEXT, \"CreateDate\" TEXT )";
19 |
20 | public DbOpenHelperLocal(Context context) {
21 | super(context, DATABASE_NAME, null, DATABASE_VERSION);
22 | }
23 |
24 | @Override
25 | public void onCreate(SQLiteDatabase db) {
26 | db.execSQL(TABLE_CREATE_LISTING);
27 | }
28 |
29 | @Override
30 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
31 | /**
32 | * This will be needed only when table's columns have changed by version
33 | * and this is up to DATABASE_VERSION code
34 | */
35 | // db.execSQL("DROP TABLE IF EXISTS " + TABLE_CREATE_LISTING);
36 | // onCreate(db);
37 | }
38 |
39 | }
--------------------------------------------------------------------------------
/Library/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
19 |
20 | POM_NAME=DatabaseManager Library
21 | POM_ARTIFACT_ID=DatabaseManager
22 | POM_PACKAGING=aar
23 | VERSION_NAME=1.0.3
24 | VERSION_CODE=4
25 | GROUP=com.yayandroid
26 |
27 | POM_DESCRIPTION=DatabaseManager Library
28 | POM_URL=https://github.com/yayaa/DatabaseManager
29 | POM_SCM_URL=https://github.com/yayaa/DatabaseManager
30 | POM_SCM_CONNECTION=scm:https://github.com/yayaa/DatabaseManager.git
31 | POM_SCM_DEV_CONNECTION=scm:https://github.com/yayaa/DatabaseManager.git
32 | POM_LICENCE_NAME=MIT License
33 | POM_LICENCE_URL=http://www.opensource.org/licenses/mit-license.php
34 | POM_LICENCE_DIST=repo
35 | POM_DEVELOPER_ID=yayaa
36 | POM_DEVELOPER_NAME=Yahya BAYRAMOGLU
37 |
38 | SNAPSHOT_REPOSITORY_URL=https://oss.sonatype.org/content/repositories/snapshots
39 | RELEASE_REPOSITORY_URL=https://oss.sonatype.org/service/local/staging/deploy/maven2
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/activity/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample.activity;
2 |
3 | import android.app.ProgressDialog;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.view.Window;
6 |
7 | import com.yayandroid.databasemanager.DatabaseManager;
8 | import com.yayandroid.databasemanager.sample.SampleApplication;
9 |
10 | /**
11 | * Created by Yahya Bayramoglu on 30/12/15.
12 | */
13 | public class BaseActivity extends AppCompatActivity {
14 |
15 | private ProgressDialog progressDialog;
16 | private final boolean PROGRESS_CANCELABLE = false;
17 |
18 | protected DatabaseManager getDatabaseManager() {
19 | return ((SampleApplication) getApplication()).getDatabaseManager();
20 | }
21 |
22 | protected void setUseReflection(boolean enabled) {
23 | ((SampleApplication) getApplication()).setUseReflection(enabled);
24 | }
25 |
26 | protected boolean getUseReflection() {
27 | return ((SampleApplication) getApplication()).getUseReflection();
28 | }
29 |
30 | public void displayProgress() {
31 | if (progressDialog == null) {
32 | progressDialog = new ProgressDialog(this);
33 | progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
34 | }
35 |
36 | progressDialog.setMessage("Progress...");
37 | progressDialog.setCancelable(PROGRESS_CANCELABLE);
38 | progressDialog.setCanceledOnTouchOutside(PROGRESS_CANCELABLE);
39 |
40 | if (!progressDialog.isShowing())
41 | progressDialog.show();
42 | }
43 |
44 | public void dismissProgress() {
45 | if (progressDialog != null && progressDialog.isShowing()) {
46 | progressDialog.dismiss();
47 | }
48 | }
49 |
50 | }
--------------------------------------------------------------------------------
/Sample/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
16 |
17 |
18 |
19 |
24 |
25 |
26 |
27 |
32 |
33 |
34 |
39 |
40 |
41 |
42 |
47 |
48 |
49 |
50 |
56 |
57 |
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/model/Ticket.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample.model;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 |
6 | import com.yayandroid.databasemanager.annotation.ColumnName;
7 |
8 | import java.text.SimpleDateFormat;
9 | import java.util.Date;
10 | import java.util.Locale;
11 |
12 | /**
13 | * Created by Yahya Bayramoglu on 30/12/15.
14 | */
15 | public class Ticket implements Parcelable {
16 |
17 | @ColumnName("TicketId")
18 | private String id;
19 |
20 | @ColumnName("CreateDate")
21 | private String date;
22 |
23 | public Ticket() {
24 | // Required by library to create a newInstance of this class
25 | }
26 |
27 | public Ticket(String id, String date) {
28 | this.id = id;
29 | this.date = date;
30 | }
31 |
32 | public Ticket(Parcel in) {
33 | this.id = in.readString();
34 | this.date = in.readString();
35 | }
36 |
37 | public void setId(String id) {
38 | this.id = id;
39 | }
40 |
41 | public String getId() {
42 | return id;
43 | }
44 |
45 | public void setDate(String date) {
46 | this.date = date;
47 | }
48 |
49 | public String getDate() {
50 | return date;
51 | }
52 |
53 | public static Ticket createRandom(int id) {
54 | Date date = new Date();
55 | String createdDate = new SimpleDateFormat("dd.MM.yyyy hh:mm", Locale.getDefault()).format(date);
56 | return new Ticket("ticketID_" + id, createdDate);
57 | }
58 |
59 | @Override
60 | public int describeContents() {
61 | return 0;
62 | }
63 |
64 | @Override
65 | public void writeToParcel(Parcel dest, int flags) {
66 | dest.writeString(id);
67 | dest.writeString(date);
68 | }
69 |
70 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
71 | public Ticket createFromParcel(Parcel in) {
72 | return new Ticket(in);
73 | }
74 |
75 | public Ticket[] newArray(int size) {
76 | return new Ticket[size];
77 | }
78 | };
79 |
80 | }
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/SampleApplication.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample;
2 |
3 | import android.app.Application;
4 | import android.database.sqlite.SQLiteDatabase;
5 | import android.os.Environment;
6 |
7 | import com.yayandroid.databasemanager.DatabaseManager;
8 | import com.yayandroid.databasemanager.model.Database;
9 | import com.yayandroid.databasemanager.sample.helper.DbOpenHelperAssets;
10 | import com.yayandroid.databasemanager.sample.helper.DbOpenHelperLocal;
11 |
12 | import java.io.File;
13 |
14 | /**
15 | * Created by Yahya Bayramoglu on 30/12/15.
16 | */
17 | public class SampleApplication extends Application {
18 |
19 | private boolean useReflection = true;
20 | private DatabaseManager dbManager;
21 |
22 | @Override
23 | public void onCreate() {
24 | super.onCreate();
25 |
26 | dbManager = new DatabaseManager();
27 |
28 | // Add database from disc
29 | // TODO: Please place the given tickets_disc.s3db file into your root folder
30 | // So the sample application reach that out, otherwise it will crash!
31 | dbManager.addDatabase(new Database.Builder(Database.DISC, Constants.DB_TAG_DISC)
32 | .path(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "tickets_disc.s3db")
33 | .openFlags(SQLiteDatabase.OPEN_READONLY)
34 | .build());
35 |
36 | // Add database from assets folder, because we will be providing an openHelper
37 | // it is fine to use Database.LOCAL type
38 | dbManager.addDatabase(new Database.Builder(Database.LOCAL, Constants.DB_TAG_ASSETS)
39 | .openWith(new DbOpenHelperAssets(getApplicationContext()))
40 | .path(getApplicationInfo().dataDir + "/databases/tickets_assets.s3db")
41 | .build());
42 |
43 | // Add local database to manager
44 | dbManager.addDatabase(new Database.Builder(Database.LOCAL, Constants.DB_TAG_LOCAL)
45 | .openWith(new DbOpenHelperLocal(getApplicationContext()))
46 | .build());
47 | }
48 |
49 | public DatabaseManager getDatabaseManager() {
50 | return dbManager;
51 | }
52 |
53 | public void setUseReflection(boolean enabled) {
54 | this.useReflection = enabled;
55 | }
56 |
57 | public boolean getUseReflection() {
58 | return useReflection;
59 | }
60 |
61 | }
--------------------------------------------------------------------------------
/Sample/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/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/app/src/main/java/com/yayandroid/databasemanager/model/Query.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.model;
2 |
3 | import com.yayandroid.databasemanager.listener.QueryListener;
4 |
5 | /**
6 | * Created by Yahya Bayramoglu on 21.10.2015.
7 | */
8 | public class Query {
9 |
10 | private String databaseTag;
11 | private String query;
12 | private String[] args;
13 | private QueryListener listener;
14 |
15 | public Query(String from) {
16 | this.databaseTag = from;
17 | }
18 |
19 | /**
20 | * SELECT, DELETE, UPDATE, INSERT, and so on... All query formats,
21 | * which are supported by SqlLite, are possible to use. But be aware,
22 | * you have to put ? in place of variables and set them via {@link Query#withArgs(String...)}
23 | *
24 | * @param query Any executable query as in string format
25 | */
26 | public Query set(String query) {
27 | this.query = query;
28 | return this;
29 | }
30 |
31 | /**
32 | * Insert method has been covered specially, because it is mostly same
33 | *
34 | * @param tableName Which table you want to insert values
35 | * @param columnCount Column count to determine how many ? is required
36 | */
37 | public Query insert(String tableName, int columnCount) {
38 | this.query = "INSERT INTO " + tableName + " VALUES (" + getPlaceHolders(columnCount) + ")";
39 | return this;
40 | }
41 |
42 | /**
43 | * Any query, which requires variables, needs to call this method
44 | * with args by exact ? count and in exact order declared in query string
45 | *
46 | * @param args Required variables in query string
47 | */
48 | public Query withArgs(String... args) {
49 | this.args = args.clone();
50 | for (int i = 0; i < args.length; i++) {
51 | if (args[i] == null) {
52 | args[i] = "";
53 | }
54 | }
55 | return this;
56 | }
57 |
58 | /**
59 | * Set a listener to get notified whenever database process ends with this query object
60 | */
61 | public Query setListener(QueryListener listener) {
62 | this.listener = listener;
63 | return this;
64 | }
65 |
66 | public String getDatabaseTag() {
67 | return databaseTag;
68 | }
69 |
70 | public String getQuery() {
71 | return query;
72 | }
73 |
74 | public String[] getArgs() {
75 | return args;
76 | }
77 |
78 | public QueryListener getListener() {
79 | return listener;
80 | }
81 |
82 | private String getPlaceHolders(int count) {
83 | StringBuilder builder = new StringBuilder();
84 | for (int i = 0; i < count; i++) {
85 | builder.append("?");
86 | if (i != count - 1) {
87 | builder.append(",");
88 | }
89 | }
90 | return builder.toString();
91 | }
92 |
93 | }
--------------------------------------------------------------------------------
/Library/app/src/main/java/com/yayandroid/databasemanager/utility/InjectionManager.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.utility;
2 |
3 | import android.database.Cursor;
4 |
5 | import com.yayandroid.databasemanager.annotation.ColumnName;
6 | import com.yayandroid.databasemanager.listener.QueryListener;
7 |
8 | import java.lang.reflect.Field;
9 | import java.util.ArrayList;
10 |
11 | /**
12 | * Created by Yahya Bayramoglu on 02/01/16.
13 | */
14 | public class InjectionManager {
15 |
16 | private QueryListener listener;
17 | private Class clazz;
18 | private Field[] fields;
19 | private String[] columnNames;
20 | private Cursor cursor;
21 | private boolean manualInjectionIsOn = true;
22 |
23 | public InjectionManager(Class clazz, QueryListener listener, Cursor cursor) {
24 | this.listener = listener;
25 | this.clazz = clazz;
26 | this.fields = clazz.getDeclaredFields();
27 | this.cursor = cursor;
28 | this.columnNames = cursor.getColumnNames();
29 | }
30 |
31 | public ArrayList getListFromCursor() {
32 | ArrayList list = new ArrayList<>();
33 | if (cursor.moveToFirst()) {
34 | do {
35 | try {
36 | T item = clazz.newInstance();
37 |
38 | boolean objectHandledManually = false;
39 | if (manualInjectionIsOn && listener != null) {
40 | objectHandledManually = listener.manualInjection(item, cursor);
41 | }
42 |
43 | if (!objectHandledManually) {
44 | manualInjectionIsOn = false;
45 | deserializeInto(item, cursor);
46 | }
47 |
48 | list.add(item);
49 | } catch (InstantiationException e) {
50 | e.printStackTrace();
51 |
52 | // If creating new instance is not possible, then break the loop
53 | break;
54 | } catch (IllegalAccessException e) {
55 | e.printStackTrace();
56 |
57 | // If creating new instance is not possible, then break the loop
58 | break;
59 | }
60 | } while (cursor.moveToNext());
61 |
62 | if (cursor != null && !cursor.isClosed()) {
63 | cursor.close();
64 | }
65 | }
66 | return list;
67 | }
68 |
69 | public void deserializeInto(T item, Cursor cursor) {
70 | for (int i = 0; i < columnNames.length; i++) {
71 | for (int j = 0; j < fields.length; j++) {
72 | if (compareFieldColumn(fields[j], columnNames[i])) {
73 | try {
74 | fields[j].setAccessible(true);
75 | fields[j].set(item, cursor.getString(i));
76 | break;
77 | } catch (IllegalAccessException e) {
78 | e.printStackTrace();
79 | }
80 | }
81 | }
82 | }
83 | }
84 |
85 | private boolean compareFieldColumn(Field field, String columnName) {
86 | if (field.getName().equals(columnName)) {
87 | return true;
88 | }
89 |
90 | ColumnName annotation = field.getAnnotation(ColumnName.class);
91 | if (annotation == null)
92 | return false;
93 | else
94 | return annotation.value().equals(columnName);
95 | }
96 |
97 | }
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample.activity;
2 |
3 | import android.content.Intent;
4 | import android.database.Cursor;
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.widget.CheckBox;
8 | import android.widget.CompoundButton;
9 | import android.widget.Toast;
10 |
11 | import com.yayandroid.databasemanager.listener.QueryListener;
12 | import com.yayandroid.databasemanager.model.Query;
13 | import com.yayandroid.databasemanager.sample.Constants;
14 | import com.yayandroid.databasemanager.sample.R;
15 | import com.yayandroid.databasemanager.sample.model.Ticket;
16 |
17 | import java.util.ArrayList;
18 |
19 | /**
20 | * Created by Yahya Bayramoglu on 31/12/15.
21 | */
22 | public class MainActivity extends BaseActivity {
23 |
24 | private CheckBox reflectionCheck;
25 |
26 | @Override
27 | protected void onCreate(Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_main);
30 |
31 | reflectionCheck = (CheckBox) findViewById(R.id.useReflectionCheck);
32 | reflectionCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
33 | @Override
34 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
35 | setUseReflection(isChecked);
36 | }
37 | });
38 | }
39 |
40 | public void databaseClick(View v) {
41 | String databaseTag = "";
42 | switch (v.getId()) {
43 | case R.id.dbTextDisc: {
44 | databaseTag = Constants.DB_TAG_DISC;
45 | break;
46 | }
47 | case R.id.dbTextAssets: {
48 | databaseTag = Constants.DB_TAG_ASSETS;
49 | break;
50 | }
51 | case R.id.dbTextLocal: {
52 | databaseTag = Constants.DB_TAG_LOCAL;
53 | break;
54 | }
55 | }
56 |
57 | Intent intent = new Intent(this, DBListActivity.class);
58 | intent.putExtra(Constants.INTENT_KEY_TAG, databaseTag);
59 | startActivity(intent);
60 | }
61 |
62 | public void compareClick(View v) {
63 | displayProgress();
64 | Query combinedComparisonQuery = new Query(Constants.DB_TAG_DISC)
65 | .set(Constants.COMPARISON_QUERY)
66 | .setListener(comparisonListener);
67 | getDatabaseManager().selectByMerged(combinedComparisonQuery, Constants.DB_TAG_ASSETS);
68 | }
69 |
70 | private QueryListener comparisonListener = new QueryListener(true) {
71 |
72 | @Override
73 | public boolean manualInjection(Ticket object, Cursor cursor) {
74 | if (getUseReflection()) {
75 | return false;
76 | } else {
77 | object.setId(cursor.getString(0));
78 | object.setDate(cursor.getString(1));
79 | return true;
80 | }
81 | }
82 |
83 | @Override
84 | public void onListReceived(ArrayList result) {
85 | dismissProgress();
86 |
87 | Intent intent = new Intent(MainActivity.this, DBListActivity.class);
88 | intent.putParcelableArrayListExtra(Constants.INTENT_KEY_LIST, result);
89 | startActivity(intent);
90 | }
91 |
92 | @Override
93 | public void noDataFound() {
94 | dismissProgress();
95 | Toast.makeText(getApplicationContext(), R.string.toast_no_difference, Toast.LENGTH_SHORT).show();
96 | }
97 |
98 | };
99 |
100 | }
--------------------------------------------------------------------------------
/Library/app/src/main/java/com/yayandroid/databasemanager/listener/QueryListener.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.listener;
2 |
3 | import android.database.Cursor;
4 | import android.os.Handler;
5 | import android.os.Looper;
6 |
7 | import com.yayandroid.databasemanager.model.Query;
8 | import com.yayandroid.databasemanager.utility.InjectionManager;
9 |
10 | import java.lang.reflect.ParameterizedType;
11 | import java.util.ArrayList;
12 |
13 | /**
14 | * Created by Yahya Bayramoglu on 29/12/15.
15 | */
16 | public class QueryListener {
17 |
18 | private boolean returnAsList = false;
19 | private Class type;
20 |
21 | public QueryListener() {
22 | // returnAsList will be false as default
23 | }
24 |
25 | public QueryListener(boolean returnAsList) {
26 | this.returnAsList = returnAsList;
27 | }
28 |
29 | /**
30 | * This method will be called as soon as query has completed, even after
31 | * {@link QueryListener#noDataFound()} || {@link QueryListener#onSingleItemReceived(Object)} ||
32 | * {@link QueryListener#onListReceived(ArrayList)} methods.
33 | */
34 | public void onComplete(Query query) {
35 |
36 | }
37 |
38 | /**
39 | * This method will be called only with SELECT queries and if there are more than one item
40 | * corresponding the given select query. If there is only one item and returnAsList flag
41 | * is not true then it would call {@link QueryListener#onSingleItemReceived(Object)}
42 | */
43 | public void onListReceived(ArrayList result) {
44 |
45 | }
46 |
47 | /**
48 | * This method will be called only when a SELECT query gets single item as result
49 | * and while returnAsList flag is false
50 | */
51 | public void onSingleItemReceived(T result) {
52 |
53 | }
54 |
55 | /**
56 | * This method will be called only with SELECT queries when there is no item corresponding it
57 | */
58 | public void noDataFound() {
59 |
60 | }
61 |
62 | /**
63 | * This method is provided just because if you don't want to use reflection
64 | * to set your custom objects in concern of performance
65 | *
66 | * @param object Current object which needs to be set its params
67 | * @param cursor Current cursor object to get values and set it to given object
68 | * @return True if you handle, false otherwise. And if you return false,
69 | * this method will not be called again for the running query
70 | */
71 | public boolean manualInjection(T object, Cursor cursor) {
72 | return false;
73 | }
74 |
75 | /**
76 | * Do not override unless you know what to do!
77 | */
78 | public void onCursorReceived(final Query query, Cursor cursor) {
79 | final ArrayList list = new InjectionManager(getClassByType(), this, cursor).getListFromCursor();
80 |
81 | new Handler(Looper.getMainLooper()).post(new Runnable() {
82 | @Override
83 | public void run() {
84 | if (list.size() == 0) {
85 | noDataFound();
86 | } else {
87 | if (!returnAsList && list.size() == 1) {
88 | onSingleItemReceived(list.get(0));
89 | } else {
90 | onListReceived(list);
91 | }
92 | }
93 | onComplete(query);
94 | }
95 | });
96 | }
97 |
98 | private Class getClassByType() {
99 | if (type == null) {
100 | this.type = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
101 | }
102 | return type;
103 | }
104 |
105 | }
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/adapter/TicketAdapter.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample.adapter;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.TextView;
9 |
10 | import com.yayandroid.databasemanager.sample.R;
11 | import com.yayandroid.databasemanager.sample.model.Ticket;
12 |
13 | import java.util.ArrayList;
14 | import java.util.Collection;
15 |
16 | /**
17 | * Created by Yahya Bayramoglu on 31/12/15.
18 | */
19 | public class TicketAdapter extends RecyclerView.Adapter {
20 |
21 | private ArrayList tickets;
22 | private LayoutInflater inflater;
23 | private TicketClickListener listener;
24 | private boolean isReadOnly = false;
25 |
26 | public interface TicketClickListener {
27 | void ticketClick(Ticket ticket);
28 |
29 | void deleteClick(int position);
30 | }
31 |
32 | public TicketAdapter(Context context, TicketClickListener listener) {
33 | this.tickets = new ArrayList<>();
34 | this.inflater = LayoutInflater.from(context);
35 | this.listener = listener;
36 | }
37 |
38 | public void setReadOnly(boolean isReadOnly) {
39 | this.isReadOnly = isReadOnly;
40 | }
41 |
42 | public boolean isReadOnly() {
43 | return isReadOnly;
44 | }
45 |
46 | public void addAll(Collection extends Ticket> list) {
47 | tickets.addAll(list);
48 | notifyDataSetChanged();
49 | }
50 |
51 | public void add(Ticket ticket) {
52 | tickets.add(ticket);
53 | notifyItemInserted(tickets.size() - 1);
54 | }
55 |
56 | public Ticket remove(int position) {
57 | Ticket removedTicket = tickets.remove(position);
58 | notifyItemRemoved(position);
59 | return removedTicket;
60 | }
61 |
62 | @Override
63 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
64 | return new ViewHolder(inflater.inflate(R.layout.recycler_item_ticket, parent, false));
65 | }
66 |
67 | @Override
68 | public void onBindViewHolder(ViewHolder holder, int position) {
69 | holder.ticketText.setText(tickets.get(position).getId());
70 | if (isReadOnly) {
71 | holder.deleteText.setVisibility(View.GONE);
72 | }
73 | }
74 |
75 | @Override
76 | public int getItemCount() {
77 | if (tickets == null)
78 | return 0;
79 | return tickets.size();
80 | }
81 |
82 | protected class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
83 |
84 | public TextView ticketText, deleteText;
85 |
86 | public ViewHolder(View itemView) {
87 | super(itemView);
88 | this.ticketText = (TextView) itemView.findViewById(R.id.ticketText);
89 | this.deleteText = (TextView) itemView.findViewById(R.id.deleteText);
90 |
91 | ticketText.setOnClickListener(this);
92 | deleteText.setOnClickListener(this);
93 | }
94 |
95 | @Override
96 | public void onClick(View v) {
97 | if (listener != null) {
98 | int position = getAdapterPosition();
99 | switch (v.getId()) {
100 | case R.id.deleteText: {
101 | listener.deleteClick(position);
102 | break;
103 | }
104 | case R.id.ticketText: {
105 | listener.ticketClick(tickets.get(position));
106 | break;
107 | }
108 | }
109 | }
110 | }
111 | }
112 |
113 | }
--------------------------------------------------------------------------------
/Library/maven_push.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Chris Banes
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 | apply plugin: 'maven'
18 | apply plugin: 'signing'
19 |
20 | def isReleaseBuild() {
21 | return VERSION_NAME.contains("SNAPSHOT") == false
22 | }
23 |
24 | def getReleaseRepositoryUrl() {
25 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
26 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
27 | }
28 |
29 | def getSnapshotRepositoryUrl() {
30 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
31 | : "https://oss.sonatype.org/content/repositories/snapshots/"
32 | }
33 |
34 | def getRepositoryUsername() {
35 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
36 | }
37 |
38 | def getRepositoryPassword() {
39 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
40 | }
41 |
42 | afterEvaluate { project ->
43 | uploadArchives {
44 | repositories {
45 | mavenDeployer {
46 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
47 |
48 | pom.groupId = GROUP
49 | pom.artifactId = POM_ARTIFACT_ID
50 | pom.version = VERSION_NAME
51 |
52 | repository(url: getReleaseRepositoryUrl()) {
53 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
54 | }
55 | snapshotRepository(url: getSnapshotRepositoryUrl()) {
56 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
57 | }
58 |
59 | pom.project {
60 | name POM_NAME
61 | packaging POM_PACKAGING
62 | description POM_DESCRIPTION
63 | url POM_URL
64 |
65 | scm {
66 | url POM_SCM_URL
67 | connection POM_SCM_CONNECTION
68 | developerConnection POM_SCM_DEV_CONNECTION
69 | }
70 |
71 | licenses {
72 | license {
73 | name POM_LICENCE_NAME
74 | url POM_LICENCE_URL
75 | distribution POM_LICENCE_DIST
76 | }
77 | }
78 |
79 | developers {
80 | developer {
81 | id POM_DEVELOPER_ID
82 | name POM_DEVELOPER_NAME
83 | }
84 | }
85 | }
86 | }
87 | }
88 | }
89 |
90 | signing {
91 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
92 | sign configurations.archives
93 | }
94 |
95 | //task androidJavadocs(type: Javadoc) {
96 | //source = android.sourceSets.main.allJava
97 | //}
98 |
99 | //task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
100 | //classifier = 'javadoc'
101 | //from androidJavadocs.destinationDir
102 | //}
103 |
104 | task androidSourcesJar(type: Jar) {
105 | classifier = 'sources'
106 | from android.sourceSets.main.java.sourceFiles
107 | }
108 |
109 | artifacts {
110 | archives androidSourcesJar
111 | }
112 | }
--------------------------------------------------------------------------------
/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/activity/DBListActivity.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.sample.activity;
2 |
3 | import android.database.Cursor;
4 | import android.os.Bundle;
5 | import android.support.v7.widget.LinearLayoutManager;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.text.TextUtils;
8 | import android.view.View;
9 | import android.widget.Button;
10 | import android.widget.Toast;
11 |
12 | import com.yayandroid.databasemanager.listener.QueryListener;
13 | import com.yayandroid.databasemanager.model.Query;
14 | import com.yayandroid.databasemanager.sample.Constants;
15 | import com.yayandroid.databasemanager.sample.R;
16 | import com.yayandroid.databasemanager.sample.model.Ticket;
17 | import com.yayandroid.databasemanager.sample.adapter.TicketAdapter;
18 |
19 | import java.util.ArrayList;
20 |
21 | /**
22 | * Created by Yahya Bayramoglu on 30/12/15.
23 | */
24 | public class DBListActivity extends BaseActivity {
25 |
26 | private TicketAdapter adapter;
27 | private String databaseTag = "";
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_dblist);
33 |
34 | adapter = new TicketAdapter(this, adapterListener);
35 | RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
36 | recyclerView.setHasFixedSize(true);
37 | recyclerView.setLayoutManager(new LinearLayoutManager(this));
38 | recyclerView.setAdapter(adapter);
39 |
40 | Bundle extras = getIntent().getExtras();
41 | if (extras != null) {
42 | databaseTag = extras.getString(Constants.INTENT_KEY_TAG);
43 |
44 | if (extras.containsKey(Constants.INTENT_KEY_LIST)) {
45 | ArrayList list = extras.getParcelableArrayList(Constants.INTENT_KEY_LIST);
46 | if (list != null) {
47 | adapter.setReadOnly(true);
48 | adapter.addAll(list);
49 | }
50 | }
51 | }
52 |
53 | if (!TextUtils.isEmpty(databaseTag)) {
54 | adapter.setReadOnly(getDatabaseManager().isReadOnly(databaseTag));
55 | getTickets();
56 | }
57 |
58 | if (adapter.isReadOnly()) {
59 | Button addButton = (Button) findViewById(R.id.addButton);
60 | addButton.setText(getString(R.string.read_only));
61 | addButton.setEnabled(false);
62 | }
63 | }
64 |
65 | private void getTickets() {
66 | displayProgress();
67 | getDatabaseManager().select(new Query(databaseTag)
68 | .set(Constants.SELECT_TICKETS)
69 | .setListener(ticketsListener));
70 | }
71 |
72 | public void addClick(View v) {
73 | displayProgress();
74 |
75 | Ticket newTicket = Ticket.createRandom(adapter.getItemCount() + 1);
76 | adapter.add(newTicket);
77 |
78 | Query insertQuery = new Query(databaseTag)
79 | .insert(Constants.TABLE_TICKET, 2) // .set(Constants.INSERT_TICKET);
80 | .withArgs(newTicket.getId(), newTicket.getDate())
81 | .setListener(new QueryListener() {
82 | @Override
83 | public void onComplete(Query query) {
84 | dismissProgress();
85 | }
86 | });
87 |
88 | getDatabaseManager().insert(insertQuery);
89 | }
90 |
91 | private void removeTicket(int position) {
92 | dismissProgress();
93 |
94 | Ticket removedTicket = adapter.remove(position);
95 |
96 | Query deleteQuery = new Query(databaseTag)
97 | .set(Constants.DELETE_TICKET)
98 | .withArgs(removedTicket.getId())
99 | .setListener(new QueryListener() {
100 | @Override
101 | public void onComplete(Query query) {
102 | dismissProgress();
103 | }
104 | });
105 |
106 | getDatabaseManager().delete(deleteQuery);
107 | }
108 |
109 | private TicketAdapter.TicketClickListener adapterListener = new TicketAdapter.TicketClickListener() {
110 | @Override
111 | public void ticketClick(Ticket ticket) {
112 | Toast.makeText(getApplicationContext(), ticket.getDate(), Toast.LENGTH_SHORT).show();
113 | }
114 |
115 | @Override
116 | public void deleteClick(int position) {
117 | removeTicket(position);
118 | }
119 | };
120 |
121 | private QueryListener ticketsListener = new QueryListener(true) {
122 |
123 | @Override
124 | public boolean manualInjection(Ticket object, Cursor cursor) {
125 | if (getUseReflection()) {
126 | return false;
127 | } else {
128 | object.setId(cursor.getString(0));
129 | object.setDate(cursor.getString(1));
130 | return true;
131 | }
132 | }
133 |
134 | @Override
135 | public void onListReceived(ArrayList result) {
136 | adapter.addAll(result);
137 | adapter.notifyDataSetChanged();
138 | }
139 |
140 | @Override
141 | public void noDataFound() {
142 | Toast.makeText(getApplicationContext(), R.string.toast_no_ticket, Toast.LENGTH_SHORT).show();
143 | }
144 |
145 | @Override
146 | public void onComplete(Query query) {
147 | dismissProgress();
148 | }
149 | };
150 |
151 | }
--------------------------------------------------------------------------------
/Library/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/Sample/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/Library/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | generateDebugSources
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/Library/app/src/main/java/com/yayandroid/databasemanager/model/Database.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager.model;
2 |
3 | import android.database.sqlite.SQLiteDatabase;
4 | import android.database.sqlite.SQLiteOpenHelper;
5 | import android.support.annotation.IntDef;
6 | import android.text.TextUtils;
7 | import android.util.Log;
8 |
9 | import com.yayandroid.databasemanager.DatabaseManager;
10 |
11 | import java.io.File;
12 |
13 | /**
14 | * Created by Yahya Bayramoglu on 29/12/15.
15 | */
16 | public class Database {
17 |
18 | private int openFlags;
19 | private int type;
20 | private String path;
21 | private String tag;
22 | private SQLiteDatabase database;
23 | private SQLiteOpenHelper openHelper;
24 |
25 | /**
26 | * Database is stored in external / internal storage
27 | */
28 | public static final int DISC = 1;
29 |
30 | /**
31 | * Database is stored in application's database folder,
32 | * and will be reached out by provided SQLiteOpenHelper
33 | */
34 | public static final int LOCAL = 2;
35 |
36 | @IntDef({DISC, LOCAL})
37 | public @interface Type {
38 | }
39 |
40 | private Database(Builder builder) {
41 | this.type = builder.type;
42 | this.path = builder.path;
43 | this.tag = builder.tag;
44 | this.openFlags = builder.openFlags;
45 | this.openHelper = builder.openHelper;
46 | this.database = builder.database;
47 | }
48 |
49 | public String getTag() {
50 | return tag;
51 | }
52 |
53 | public String getPath() {
54 | return path;
55 | }
56 |
57 | public boolean isReadOnly() {
58 | return openFlags == SQLiteDatabase.OPEN_READONLY;
59 | }
60 |
61 | public SQLiteDatabase get() {
62 | if (!isOpen()) {
63 | // If there is an instance but somehow not accessible, close that first
64 | close();
65 | database = null;
66 |
67 | switch (type) {
68 | case DISC: {
69 | if (TextUtils.isEmpty(path)) {
70 | throw new RuntimeException("You have to specify the required database path in storage");
71 | }
72 |
73 | if (exists()) {
74 | database = SQLiteDatabase.openDatabase(path, null, openFlags);
75 | } else {
76 | throw new NullPointerException("No Database found in path: " + path);
77 | }
78 |
79 | break;
80 | }
81 | case LOCAL: {
82 | if (openHelper == null) {
83 | throw new RuntimeException("You have to specify a SQLiteOpenHelper class in order to use LOCAL database");
84 | }
85 |
86 | if (openFlags == SQLiteDatabase.OPEN_READONLY) {
87 | database = openHelper.getReadableDatabase();
88 | } else if (openFlags == SQLiteDatabase.OPEN_READWRITE) {
89 | database = openHelper.getWritableDatabase();
90 | } else {
91 | throw new IllegalArgumentException("Defined openFlags are not acceptable. Please use one of SQLiteDatabase.OPEN_READONLY or SQLiteDatabase.OPEN_READWRITE");
92 | }
93 |
94 | break;
95 | }
96 | }
97 |
98 | if (database == null) {
99 | throw new RuntimeException("Check the database configurations, it fails to open any database with given attributes.");
100 | }
101 | }
102 | return database;
103 | }
104 |
105 | /**
106 | * To close database instance manually
107 | */
108 | public void close() {
109 | if (database != null) {
110 | database.close();
111 | }
112 | }
113 |
114 | private boolean isOpen() {
115 | return database != null && database.isOpen();
116 | }
117 |
118 | private boolean exists() {
119 | File file = new File(path);
120 | if (file.exists()) {
121 | Log.i("DatabaseManager", "Database found in path: " + path);
122 | return true;
123 | } else {
124 | return false;
125 | }
126 | }
127 |
128 | public static class Builder {
129 |
130 | private int openFlags = SQLiteDatabase.OPEN_READWRITE;
131 | private int type;
132 | private String path;
133 | private String tag;
134 | private SQLiteDatabase database;
135 | private SQLiteOpenHelper openHelper;
136 |
137 | /**
138 | * @param databaseTag Needs to be unique because it will be used in DatabaseManager
139 | * to figure out which is target database and if you try to add
140 | * multiple databases with same tag it will replace
141 | * @param type Declare where the required database is stored
142 | * {@link Database#DISC} || {@link Database#LOCAL}
143 | */
144 | public Builder(@Type int type, String databaseTag) {
145 | this.type = type;
146 | this.tag = databaseTag;
147 | }
148 |
149 | /**
150 | * @param path full path of the required database
151 | * if type is LOCAL normally no need to set, but if you need to use
152 | * {@link DatabaseManager#selectByMerged(Query, String)} or derivatives
153 | * then you have to specify full path as well, because manager will use this
154 | * to reach out required database to merge within given query.
155 | */
156 | public Builder path(String path) {
157 | this.path = path;
158 | return this;
159 | }
160 |
161 | /**
162 | * If the required database is in LOCAL type
163 | * then you have to specify your custom SQLiteOpenHelper class
164 | */
165 | public Builder openWith(T helper) {
166 | this.openHelper = helper;
167 | return this;
168 | }
169 |
170 | /**
171 | * This will be used to determine the required database object needs to open as readOnly
172 | * or writable as well. It accepts {@link SQLiteDatabase#OPEN_READONLY} ||
173 | * {@link SQLiteDatabase#OPEN_READWRITE} while READWRITE as default
174 | */
175 | public Builder openFlags(int flags) {
176 | this.openFlags = flags;
177 | return this;
178 | }
179 |
180 | /**
181 | * Do not use this method unless you have to!
182 | */
183 | public Builder asDatabase(SQLiteDatabase database) {
184 | /**
185 | * For instance, if you have your database in assets folder,
186 | * then you can use SQLite AssetsHelper library and get database out of that
187 | * to pass here so you can still use DatabaseManager.
188 | *
189 | * https://github.com/jgilfelt/android-sqlite-asset-helper
190 | */
191 | this.database = database;
192 | return this;
193 | }
194 |
195 | public Database build() {
196 | return new Database(this);
197 | }
198 |
199 | }
200 |
201 | }
--------------------------------------------------------------------------------
/Sample/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | generateDebugSources
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DatabaseManager
2 |
3 |
4 |
5 |
6 |
7 | [](http://unmaintained.tech/)
8 |
9 | Database usage on mobile platforms may not be extensive but at some point, it is required. Android platform supports SQLite usage in your code base, but you have a lot to do! So i created this library to make database management easier.
10 |
11 | P.S.: I strongly recommend you to use [Realm][1] if it suits your requirements though.
12 |
13 | # Usage
14 |
15 | ## Database
16 | Create Database object(s) and add them to the DatabaseManager in order to reach out them later on by only their tags. DatabaseManager will not open your databases until you actually run a query on them, so you can add databases when you first create your databaseManager.
17 |
18 | ```java
19 | DatabaseManager databaseManager = new DatabaseManager();
20 |
21 | Database localDB = new Database.Builder(Database.LOCAL, "localDbTag")
22 | .openWith(new DbOpenHelperLocal(getApplicationContext()))
23 | .build()
24 |
25 | databaseManager.addDatabase(localDB);
26 | ```
27 |
28 | Databases can have 2 types: `Database.LOCAL` and `Database.DISC`
29 |
30 | - If you create database object with `LOCAL` type, then you need to set `openWith` method to declare which `SQLiteOpenHelper` will be used while database open process.
31 | - If you create database object with `DISC` type, then you do not need to set `openWith` because it won’t be used. Instead, you need to specify the `path` of required database in Disc.
32 |
33 | `path` method is not obligatory for `LOCAL` type, but if you are using `getByMerged` or its derivatives, library will need your databases' path to attach it to the query, so only then you need to set the `path` with `LOCAL` type as well.
34 |
35 | And if you need to declare your path with `LOCAL` type, you may find the path such as:
36 |
37 | ```java
38 | // if local database
39 | context.getDatabasePath(databaseName).getPath();
40 |
41 | // if database in assets folder (via Android-Sqlite-Asset-Helper)
42 | context.getApplicationInfo().dataDir + "/databases/" + databaseName;
43 | ```
44 |
45 | P.S.: If your database is not saved in `DISC` nor as it is `LOCAL`, but in `ASSETS` folder then i would suggest you to use [Android-Sqlite-Asset-Helper-Library][3]
46 |
47 | ```java
48 | .openFlags(int flags)
49 | ```
50 |
51 | Via this method, you can determine database to open as readOnly or readWrite. But be aware, library will accept only `SQLiteDatabase.OPEN_READWRITE` and `SQLiteDatabase.OPEN_READONLY` flags. As default, library will open databases with `SQLiteDatabase.OPEN_READWRITE` flag.
52 |
53 | ## Query
54 | Create a Query object and pass that to relevant method in DatabaseManager and it will take care of the rest. Query object does not have builder pattern, but still uses chaining so you can link all methods to each other to create more readable code pattern.
55 |
56 | ```java
57 | new Query(databaseTag)
58 | .set("SELECT * FROM someTable WHERE bla=? AND otherBla=?")
59 | .withArgs("blaEquivalent", "secondBlaEquivalent")
60 | .setListener(queryListener) // Defined below
61 | ```
62 | Here, given `databaseTag` needs to be same within database object so that manager can find and run the query on it. And before you run any query make sure you add required database object to the manager, otherwise it will crash.
63 |
64 | Query object has 2 main method: `set` and `insert`.
65 | `insert` method is actually creates set methods inside, nothing special but it is there just because INSERT query is mostly same. Use that if only matches your requirements, if not, if you need to create a complex insert query then use `set` method which accepts every query statements.
66 |
67 | You do not have to use `.withArgs` method, but be aware if you are going to type your parameters in queryText as well then you have to use inside of `'` marks. Otherwise, just put `?` in place of the variables and call `.withArgs(String... values)` method in same order and same count with your `?` marks.
68 |
69 | ## QueryListener
70 | DatabaseManager will return to QueryListener if you call one of the async methods on it, if not there is no need to set any listener.
71 |
72 | ``` java
73 | private QueryListener queryListener = new QueryListener() {
74 |
75 | public void onComplete(Query query) { }
76 |
77 | public void onListReceived(ArrayList
148 |
149 | ```
150 |
151 | ## Disclaimer
152 | - This library has no static singleton structure, because it keeps your database objects so attaching that to static instance may cause leak. Instead, create a single DatabaseManager instance in your application class and reach that out in your activities. You can see usage in [sample application][2].
153 | - To prevent having `java.lang.IllegalStateException: SQLiteDatabase created and never closed` it is recommended to create database object with application context, so that it will live as long as application lives and you won’t need to close it manually.
154 |
155 | ## Download
156 | Add library dependency to your `build.gradle` file:
157 |
158 | ```groovy
159 | dependencies {
160 | compile 'com.yayandroid:DatabaseManager:1.0.3'
161 | }
162 | ```
163 |
164 | ## License
165 | ```
166 | The MIT License (MIT)
167 |
168 | Copyright (c) 2016 yayandroid
169 |
170 | Permission is hereby granted, free of charge, to any person obtaining a copy
171 | of this software and associated documentation files (the "Software"), to deal
172 | in the Software without restriction, including without limitation the rights
173 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
174 | copies of the Software, and to permit persons to whom the Software is
175 | furnished to do so, subject to the following conditions:
176 |
177 | The above copyright notice and this permission notice shall be included in
178 | all copies or substantial portions of the Software.
179 |
180 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
181 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
182 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
183 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
184 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
185 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
186 | THE SOFTWARE.
187 | ```
188 |
189 | [1]: https://github.com/realm/realm-java
190 | [2]: https://github.com/yayaa/DatabaseManager/blob/master/Sample/app/src/main/java/com/yayandroid/databasemanager/sample/SampleApplication.java
191 | [3]: https://github.com/jgilfelt/android-sqlite-asset-helper
192 |
--------------------------------------------------------------------------------
/Library/app/src/main/java/com/yayandroid/databasemanager/DatabaseManager.java:
--------------------------------------------------------------------------------
1 | package com.yayandroid.databasemanager;
2 |
3 | import android.database.Cursor;
4 | import android.database.sqlite.SQLiteStatement;
5 | import android.os.Handler;
6 | import android.os.Looper;
7 | import android.util.Log;
8 |
9 | import com.yayandroid.databasemanager.model.Database;
10 | import com.yayandroid.databasemanager.model.Query;
11 | import com.yayandroid.databasemanager.utility.InjectionManager;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 | import java.util.concurrent.BlockingQueue;
16 | import java.util.concurrent.Executor;
17 | import java.util.concurrent.LinkedBlockingQueue;
18 | import java.util.concurrent.ThreadPoolExecutor;
19 | import java.util.concurrent.TimeUnit;
20 |
21 | /**
22 | * Create a single instance of DatabaseManager, most eligible way in application class,
23 | * and reach out that instance to interact. This manager doesn't have a static singleton instance
24 | * to prevent having memory leak while keeping database instances which has attached to contexts.
25 | * Please keep it that way.
26 | *
27 | * Created by Yahya Bayramoglu on 29/12/15
28 | */
29 | public class DatabaseManager {
30 |
31 | private final List databases;
32 | private ThreadPoolExecutor executor;
33 | private static final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
34 |
35 | public DatabaseManager() {
36 | databases = new ArrayList<>();
37 | }
38 |
39 | /**
40 | * Create a Database object and add to the manager so when you declare it from any query
41 | * it'll find and run query on it. Do not forget to add database
42 | * before you attempt to run a query on it, otherwise it will crash.
43 | *
44 | * ATTENTION: To prevent having "java.lang.IllegalStateException:
45 | * SQLiteDatabase created and never closed" it is recommended to create database object
46 | * with application context, so that it will live as long as application lives and
47 | * you won't need to close it manually.
48 | */
49 | public void addDatabase(Database database) {
50 | synchronized (databases) {
51 | // Remove if there is a database with same tag
52 | for (int i = 0; i < databases.size(); i++) {
53 | if (databases.get(i).getTag().equals(database.getTag())) {
54 | databases.remove(i);
55 | break;
56 | }
57 | }
58 | // add given database object
59 | databases.add(database);
60 | }
61 | }
62 |
63 | /**
64 | * Returns database object if there is one matches with given tag name,
65 | * if not throws an exception, so ensure you add that database into this manager first
66 | *
67 | * @param databaseTag Database tag which is declared in Database object
68 | * @return Database object with given tagName
69 | */
70 | public Database getDatabaseByTag(String databaseTag) {
71 | synchronized (databases) {
72 | for (int i = 0; i < databases.size(); i++) {
73 | if (databases.get(i).getTag().equals(databaseTag)) {
74 | return databases.get(i);
75 | }
76 | }
77 | }
78 |
79 | throw new RuntimeException("You have to create your database and add it to databaseManager before attempting to use it.");
80 | }
81 |
82 | /**
83 | * Flag to determine database, with given tag name, is in readOnly mode or not
84 | *
85 | * @param databaseTag Database tag which is declared in Database object
86 | * @return True if database is readOnly, false otherwise
87 | */
88 | public boolean isReadOnly(String databaseTag) {
89 | return getDatabaseByTag(databaseTag).isReadOnly();
90 | }
91 |
92 | /**
93 | * Call this method to log every table name in given database
94 | *
95 | * @param databaseTag Database tag which is declared in Database object
96 | */
97 | public void logTablesInDatabase(String databaseTag) {
98 | String tableQuery = "SELECT name FROM sqlite_master WHERE type='table'";
99 | Cursor c = getDatabaseByTag(databaseTag).get().rawQuery(tableQuery, null);
100 |
101 | ArrayList tables = new ArrayList<>();
102 | if (c.moveToFirst()) {
103 | do {
104 | tables.add(c.getString(0));
105 | } while (c.moveToNext());
106 | }
107 |
108 | Log.i("DatabaseManager", "## Tables in " + databaseTag + "##\n" + tables.toString());
109 | }
110 |
111 | /**
112 | * Executes a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
113 | */
114 | public void execute(final Query query) {
115 | getExecutor().execute(new Runnable() {
116 | @Override
117 | public void run() {
118 | getDatabaseByTag(query.getDatabaseTag()).get().execSQL(query.getQuery(), query.getArgs());
119 | notifyListenerAsCompleted(query);
120 | }
121 | });
122 | }
123 |
124 | /**
125 | * SYNC - Executes a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
126 | */
127 | public void executeSync(Query query) {
128 | getDatabaseByTag(query.getDatabaseTag()).get().execSQL(query.getQuery(), query.getArgs());
129 | }
130 |
131 | /**
132 | * Executes a SELECT query and returns to queryListener if set
133 | */
134 | public void select(final Query query) {
135 | getExecutor().execute(new Runnable() {
136 | @Override
137 | public void run() {
138 | Cursor cursor = getDatabaseByTag(query.getDatabaseTag()).get()
139 | .rawQuery(query.getQuery(), query.getArgs());
140 | if (query.getListener() != null) {
141 | query.getListener().onCursorReceived(query, cursor);
142 | }
143 | }
144 | });
145 | }
146 |
147 | /**
148 | * SYNC - Executes a SELECT query and returns result
149 | *
150 | * @param clazz Expected deserialization object
151 | * @param query Query object which contains necessary data - no need to set listener
152 | * @return Returns an arrayList which consists of instances of given class
153 | */
154 | public ArrayList selectSync(Class clazz, Query query) {
155 | Cursor cursor = getDatabaseByTag(query.getDatabaseTag()).get().rawQuery(query.getQuery(), query.getArgs());
156 | return new InjectionManager(clazz, query.getListener(), cursor).getListFromCursor();
157 | }
158 |
159 | /**
160 | * Executes a SELECT query which requires to reach out different database. To do that,
161 | * the other database is also needed to be added into DatabaseManager. And because it will
162 | * be attached with its tag, while creating query it needs to be used with tag.
163 | *
164 | * @param query Query object which contains necessary data
165 | * @param databaseTagToMerge Database tag which is required for this query
166 | */
167 | public void selectByMerged(final Query query, final String databaseTagToMerge) {
168 | getExecutor().execute(new Runnable() {
169 | @Override
170 | public void run() {
171 | Database queryDB = getDatabaseByTag(query.getDatabaseTag());
172 | Database mergeDB = getDatabaseByTag(databaseTagToMerge);
173 |
174 | queryDB.get().execSQL("attach database ? as " + databaseTagToMerge, new String[]{mergeDB.getPath()});
175 |
176 | Cursor cursor = queryDB.get().rawQuery(query.getQuery(), query.getArgs());
177 | if (query.getListener() != null) {
178 | query.getListener().onCursorReceived(query, cursor);
179 | }
180 |
181 | queryDB.get().execSQL("detach database ?", new String[]{databaseTagToMerge});
182 | }
183 | });
184 | }
185 |
186 | /**
187 | * SYNC - Executes a SELECT query which requires to reach out different database. To do that,
188 | * the other database is also needed to be added into DatabaseManager. And because it will
189 | * be attached with its tag, while creating query it needs to be used with tag.
190 | *
191 | * @param clazz Expected deserialization object
192 | * @param query Query object which contains necessary data - no need to set listener
193 | * @param databaseTagToMerge Database tag which is required for this query
194 | * @return Returns an arrayList which consists of instances of given class
195 | */
196 | public ArrayList selectByMergedSync(Class clazz, Query query, String databaseTagToMerge) {
197 | Database queryDB = getDatabaseByTag(query.getDatabaseTag());
198 | Database mergeDB = getDatabaseByTag(databaseTagToMerge);
199 |
200 | queryDB.get().execSQL("attach database ? as " + databaseTagToMerge, new String[]{mergeDB.getPath()});
201 |
202 | ArrayList list = selectSync(clazz, query);
203 |
204 | queryDB.get().execSQL("detach database ?", new String[]{databaseTagToMerge});
205 | return list;
206 | }
207 |
208 | /**
209 | * This method will ensure that data is inserted to database
210 | * unlikely update method which does nothing if there is no match.
211 | *
212 | * This method has no SYNC version, because you can simply call
213 | * {@link DatabaseManager#deleteSync(Query)} && {@link DatabaseManager#insertSync(Query)}
214 | *
215 | * @param deleteQuery Query object to delete from database
216 | * @param insertQuery Query object to insert to database
217 | */
218 | public void deleteAndInsert(final Query deleteQuery, final Query insertQuery) {
219 | getExecutor().execute(new Runnable() {
220 | @Override
221 | public void run() {
222 | deleteSync(deleteQuery);
223 | insertSync(insertQuery);
224 | notifyListenerAsCompleted(insertQuery);
225 | }
226 | });
227 | }
228 |
229 | /**
230 | * Executes given INSERT statement
231 | */
232 | public void insert(final Query query) {
233 | getExecutor().execute(new Runnable() {
234 | @Override
235 | public void run() {
236 | insertSync(query);
237 | notifyListenerAsCompleted(query);
238 | }
239 | });
240 | }
241 |
242 | /**
243 | * SYNC - Executes given INSERT statement and returns the ID of the row inserted due to this call.
244 | */
245 | public long insertSync(Query query) {
246 | SQLiteStatement insertStatement = getDatabaseByTag(query.getDatabaseTag()).get()
247 | .compileStatement(query.getQuery());
248 | insertStatement.bindAllArgsAsStrings(query.getArgs());
249 | return insertStatement.executeInsert();
250 | }
251 |
252 | /**
253 | * Executes given UPDATE statement
254 | */
255 | public void update(Query query) {
256 | updateOrDelete(query);
257 | }
258 |
259 | /**
260 | * SYNC - Executes given UPDATE statement and returns the number of rows affected by this SQL statement execution.
261 | */
262 | public int updateSync(Query query) {
263 | return updateOrDeleteSync(query);
264 | }
265 |
266 | /**
267 | * Executes given DELETE statement
268 | */
269 | public void delete(Query query) {
270 | updateOrDelete(query);
271 | }
272 |
273 | /**
274 | * SYNC - Executes given DELETE statement and returns the number of rows affected by this SQL statement execution.
275 | */
276 | public int deleteSync(Query query) {
277 | return updateOrDeleteSync(query);
278 | }
279 |
280 | private void updateOrDelete(final Query query) {
281 | getExecutor().execute(new Runnable() {
282 | @Override
283 | public void run() {
284 | updateOrDeleteSync(query);
285 | notifyListenerAsCompleted(query);
286 | }
287 | });
288 | }
289 |
290 | private int updateOrDeleteSync(Query query) {
291 | SQLiteStatement updateOrDeleteStatement = getDatabaseByTag(query.getDatabaseTag()).get()
292 | .compileStatement(query.getQuery());
293 | updateOrDeleteStatement.bindAllArgsAsStrings(query.getArgs());
294 | return updateOrDeleteStatement.executeUpdateDelete();
295 | }
296 |
297 | private void notifyListenerAsCompleted(final Query query) {
298 | if (query.getListener() != null) {
299 | new Handler(Looper.getMainLooper()).post(new Runnable() {
300 | @Override
301 | public void run() {
302 | query.getListener().onComplete(query);
303 | }
304 | });
305 | }
306 | }
307 |
308 | private Executor getExecutor() {
309 | if (executor == null) {
310 | // LinkedBlockingQueue is because we need FIFO
311 | BlockingQueue taskQueue = new LinkedBlockingQueue<>();
312 | executor = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES, 0L, TimeUnit.MILLISECONDS, taskQueue);
313 | }
314 | return executor;
315 | }
316 |
317 | }
--------------------------------------------------------------------------------