Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SupportSQLite instead of the framework version #907

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ ext.libraries = [
recyclerView : "com.android.support:recyclerview-v7:$supportLibsVersion",
appCompatX : "androidx.appcompat:appcompat:1.0.0",
recyclerViewX : "androidx.recyclerview:recyclerview:1.0.0",
sqlite : "androidx.sqlite:sqlite:2.0.0",
sqliteFramework : "androidx.sqlite:sqlite-framework:2.0.0",

timber : "com.jakewharton.timber:timber:4.6.0", // https://github.com/JakeWharton/timber/blob/master/CHANGELOG.md
leakCanary : "com.squareup.leakcanary:leakcanary-android:1.5.4", // https://github.com/square/leakcanary/blob/master/CHANGELOG.md
Expand Down
1 change: 1 addition & 0 deletions storio-basic-sample-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ android {

// libraries declared in root build.gradle
dependencies {
implementation libraries.sqliteFramework
implementation libraries.appCompatX
implementation libraries.recyclerViewX

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.pushtorefresh.storio3.basic_sample;

import androidx.annotation.NonNull;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;

public class DbOpenCallback extends SupportSQLiteOpenHelper.Callback {

public static final String DB_NAME = "sample_db";

public DbOpenCallback() {
super(1);
}

@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
TweetTable.createTable(db);
}

@Override
public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) {
TweetTable.updateTable(db, oldVersion);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.pushtorefresh.storio3.basic_sample;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.Toast;

import com.pushtorefresh.storio3.StorIOException;
Expand All @@ -15,6 +11,14 @@
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory;


public class MainActivity extends AppCompatActivity {

private StorIOSQLite storIOSQLite;
Expand All @@ -26,8 +30,14 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportSQLiteOpenHelper.Configuration configuration = SupportSQLiteOpenHelper.Configuration
.builder(this)
.name(DbOpenCallback.DB_NAME)
.callback(new DbOpenCallback())
.build();

storIOSQLite = DefaultStorIOSQLite.builder()
.sqliteOpenHelper(new DbOpenHelper(this))
.sqliteOpenHelper(new FrameworkSQLiteOpenHelperFactory().create(configuration))
.addTypeMapping(Tweet.class, new TweetSQLiteTypeMapping())
.build();

Expand Down
1 change: 1 addition & 0 deletions storio-sample-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ dependencies {

implementation libraries.rxJava2
implementation libraries.rxAndroid2
implementation libraries.sqliteFramework
implementation libraries.appCompat
implementation libraries.recyclerView
implementation libraries.timber
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.pushtorefresh.storio3.sample.db;

import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;

import com.pushtorefresh.storio3.sample.db.entities.Tweet;
Expand Down Expand Up @@ -31,6 +30,8 @@

import javax.inject.Singleton;

import androidx.sqlite.db.SupportSQLiteOpenHelper;
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory;
import dagger.Module;
import dagger.Provides;

Expand All @@ -45,7 +46,7 @@ public class DbModule {
@Provides
@NonNull
@Singleton
public StorIOSQLite provideStorIOSQLite(@NonNull SQLiteOpenHelper sqLiteOpenHelper) {
public StorIOSQLite provideStorIOSQLite(@NonNull SupportSQLiteOpenHelper sqLiteOpenHelper) {
final CarStorIOSQLitePutResolver carStorIOSQLitePutResolver = new CarStorIOSQLitePutResolver();
final CarStorIOSQLiteGetResolver carStorIOSQLiteGetResolver = new CarStorIOSQLiteGetResolver();

Expand Down Expand Up @@ -82,7 +83,13 @@ public StorIOSQLite provideStorIOSQLite(@NonNull SQLiteOpenHelper sqLiteOpenHelp
@Provides
@NonNull
@Singleton
public SQLiteOpenHelper provideSQLiteOpenHelper(@NonNull Context context) {
return new DbOpenHelper(context);
public SupportSQLiteOpenHelper provideSQLiteOpenHelper(@NonNull Context context) {
SupportSQLiteOpenHelper.Configuration configuration = SupportSQLiteOpenHelper.Configuration
.builder(context)
.name(DbOpenCallback.DB_NAME)
.callback(new DbOpenCallback())
.build();

return new FrameworkSQLiteOpenHelperFactory().create(configuration);
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.pushtorefresh.storio3.sample.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;

import com.pushtorefresh.storio3.sample.db.tables.TweetsTable;
import com.pushtorefresh.storio3.sample.db.tables.UsersTable;
import com.pushtorefresh.storio3.sample.many_to_many_sample.entities.CarTable;
import com.pushtorefresh.storio3.sample.many_to_many_sample.entities.PersonCarRelationTable;
import com.pushtorefresh.storio3.sample.many_to_many_sample.entities.PersonTable;

import androidx.annotation.NonNull;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
//import com.pushtorefresh.storio3.sample.sqldelight.entities.Customer;

public class DbOpenHelper extends SQLiteOpenHelper {
public class DbOpenCallback extends SupportSQLiteOpenHelper.Callback {

public static final String DB_NAME = "sample_db";

public DbOpenHelper(@NonNull Context context) {
super(context, "sample_db", null, 1);
public static final int VERSION = 1;

public DbOpenCallback() {
super(VERSION);
}

@Override
public void onCreate(@NonNull SQLiteDatabase db) {
public void onCreate(@NonNull SupportSQLiteDatabase db) {
db.execSQL(TweetsTable.getCreateTableQuery());
db.execSQL(UsersTable.getCreateTableQuery());

Expand All @@ -31,7 +34,7 @@ public void onCreate(@NonNull SQLiteDatabase db) {
}

@Override
public void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion) {
// no impl
public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;

Expand All @@ -14,6 +14,9 @@

import javax.inject.Inject;

import androidx.sqlite.db.SupportSQLiteOpenHelper;
import androidx.sqlite.db.SupportSQLiteQueryBuilder;

public class SampleContentProvider extends ContentProvider {

@NonNull
Expand All @@ -29,7 +32,7 @@ public class SampleContentProvider extends ContentProvider {
}

@Inject
SQLiteOpenHelper sqLiteOpenHelper;
SupportSQLiteOpenHelper sqLiteOpenHelper;

/**
* {@inheritDoc}
Expand All @@ -46,14 +49,11 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
case URI_MATCHER_CODE_TWEETS:
return sqLiteOpenHelper
.getReadableDatabase()
.query(
TweetsTable.TABLE,
projection,
selection,
selectionArgs,
null,
null,
sortOrder
.query(SupportSQLiteQueryBuilder.builder(TweetsTable.TABLE)
.columns(projection)
.selection(selection, selectionArgs)
.orderBy(sortOrder)
.create()
);

default:
Expand All @@ -76,7 +76,7 @@ public Uri insert(Uri uri, ContentValues values) {
.getWritableDatabase()
.insert(
TweetsTable.TABLE,
null,
SQLiteDatabase.CONFLICT_NONE,
values
);
break;
Expand All @@ -102,6 +102,7 @@ public int update(Uri uri, ContentValues values, String selection, String[] sele
.getWritableDatabase()
.update(
TweetsTable.TABLE,
SQLiteDatabase.CONFLICT_NONE,
values,
selection,
selectionArgs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.pushtorefresh.storio3.sample.db;

import android.database.sqlite.SQLiteDatabase;

import com.pushtorefresh.storio3.contentresolver.BuildConfig;
import com.pushtorefresh.storio3.sample.SampleRobolectricTestRunner;

Expand All @@ -10,15 +8,25 @@
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory;

import static org.assertj.core.api.Java6Assertions.assertThat;

@RunWith(SampleRobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public final class DbOpenHelperTest {
public final class DbOpenCallbackTest {

@Test
public void shouldCreateDb() {
SQLiteDatabase database = new DbOpenHelper(RuntimeEnvironment.application)
SupportSQLiteOpenHelper.Configuration configuration = SupportSQLiteOpenHelper.Configuration
.builder(RuntimeEnvironment.application)
.name(DbOpenCallback.DB_NAME)
.callback(new DbOpenCallback())
.build();

SupportSQLiteDatabase database = new FrameworkSQLiteOpenHelperFactory().create(configuration)
.getWritableDatabase();

assertThat(database).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.pushtorefresh.storio3.sqlite.annotations;

import android.database.sqlite.SQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;
import java.lang.String;


public final class WithGeneratedTableMultipleKeysTable {
public static final String NAME = "table";

Expand All @@ -29,7 +30,7 @@ public final class WithGeneratedTableMultipleKeysTable {
private WithGeneratedTableTable() {
}

public static void createTable(SQLiteDatabase db) {
public static void createTable(SupportSQLiteDatabase db) {
db.execSQL("CREATE TABLE table (field1 INTEGER,\n"
+ "field2 INTEGER,\n"
+ "field3 INTEGER,\n"
Expand All @@ -43,12 +44,12 @@ public static void createTable(SQLiteDatabase db) {
+ "PRIMARY KEY(field4, field7));");
}

public static void updateTable(SQLiteDatabase db, int oldVersion) {
public static void updateTable(SupportSQLiteDatabase db, int oldVersion) {
if (oldVersion < 2) {
db.execSQL("ALTER TABLE table ADD COLUMN new_field_1 INTEGER");
}
if (oldVersion < 3) {
db.execSQL("ALTER TABLE table ADD COLUMN new_field_2 TEXT NOT NULL");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.pushtorefresh.storio3.sqlite.annotations;

import android.database.sqlite.SQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;
import java.lang.String;


public final class WithGeneratedTableTable {
public static final String NAME = "table";

Expand All @@ -29,7 +30,7 @@ public final class WithGeneratedTableTable {
private WithGeneratedTableTable() {
}

public static void createTable(SQLiteDatabase db) {
public static void createTable(SupportSQLiteDatabase db) {
db.execSQL("CREATE TABLE table (field1 INTEGER,\n"
+ "field2 INTEGER,\n"
+ "field3 INTEGER,\n"
Expand All @@ -42,12 +43,12 @@ public static void createTable(SQLiteDatabase db) {
+ "new_field_2 TEXT NOT NULL);");
}

public static void updateTable(SQLiteDatabase db, int oldVersion) {
public static void updateTable(SupportSQLiteDatabase db, int oldVersion) {
if (oldVersion < 2) {
db.execSQL("ALTER TABLE table ADD COLUMN new_field_1 INTEGER");
}
if (oldVersion < 3) {
db.execSQL("ALTER TABLE table ADD COLUMN new_field_2 TEXT NOT NULL");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private const val OLD_VERSION_PARAM = "oldVersion"

object TableGenerator : Generator<StorIOSQLiteTypeMeta> {

private val sqliteDatabase = ClassName.get("android.database.sqlite", "SQLiteDatabase")
private val sqliteDatabase = ClassName.get("androidx.sqlite.db", "SupportSQLiteDatabase")

override fun generateJavaFile(typeMeta: StorIOSQLiteTypeMeta): JavaFile {
val tableSpec = TypeSpec.classBuilder("${typeMeta.simpleName}Table")
Expand Down Expand Up @@ -107,4 +107,4 @@ object TableGenerator : Generator<StorIOSQLiteTypeMeta> {
}
return false
}
}
}
Loading