Skip to content

Commit

Permalink
Merge pull request #1 from pistolla/master
Browse files Browse the repository at this point in the history
support androidx.sqlite
  • Loading branch information
eddmash committed May 4, 2023
2 parents d9e5302 + e94329c commit f541b3a
Show file tree
Hide file tree
Showing 47 changed files with 542 additions and 145 deletions.
24 changes: 20 additions & 4 deletions activerecord/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'

apply plugin: 'android-maven'
apply plugin: "maven-publish"

group='com.github.eddmash.androidcomponents'

Expand All @@ -13,7 +13,7 @@ android {
versionCode project.ext.versionCode
versionName "${project.ext.versionName}"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

}
buildTypes {
Expand All @@ -26,7 +26,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
implementation "androidx.sqlite:sqlite:2.3.1"
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testImplementation 'junit:junit:4.12'
Expand All @@ -35,4 +36,19 @@ dependencies {
//Add these lines to publish library to bintray
//Place it at the end of the file
//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
// You can then customize attributes of the publication as shown below.
groupId = "${group}"
artifactId = 'activerecord'
version = project.ext.versionName
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.eddmash.db;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
237 changes: 237 additions & 0 deletions activerecord/src/main/java/com/eddmash/db/SupportActiveRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/*
* This file is part of the com.eddmash.querer package.
* <p>
* (c) Eddilbert Macharia (http://eddmash.com)<edd.cowan@gmail.com>
* <p>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package com.eddmash.db;

import android.database.Cursor;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteQueryBuilder;

/**
* A simple set of helper methods to query for data on android sqlite database.
* <p>
* To get the instance of theis Active record use the getInstance method,
* this method takes just one parameter. an instance of SQLiteDatabase.
* <p>
* this class is implemented as a singleton meaning only one instance of ActiveRecord ever exists
* in your application life time.
* <p>
* NB:: the instance of SQLiteDatabase passed in getInstance method is destroyed once the garbage
* collector destroys the instance of the ActiveRecord.
*/

public class SupportActiveRecord {

private static SupportActiveRecord instance;

/**
* REturn instance of SQLiteDatabase that the activerecord instance is using.
*
* @return
*/
public SupportSQLiteDatabase getDb() {
return db;
}

private SupportSQLiteDatabase db;


private SupportActiveRecord(SupportSQLiteDatabase database) {
db = database;
}


/**
* Returns an instance of the the activerecord class
*
* @param database
* @return
*/
public static SupportActiveRecord getInstance(SupportSQLiteDatabase database) {
if (instance == null) {
instance = new SupportActiveRecord(database);
}
return instance;
}

/**
* Returns an list of maps, where the map represents each record in the database.
* <p>
* with keys of the map as the column name and values of the map as the values of the
* respective columns.
* <p>
* something like this:
* <p>
* [{id:1, username:ken, age:50}, {id:2, username:matt, age:70}]
*
* @param sql
* @param args
* @return
*/
public List<Map> find(String sql, String[] args) {

Cursor mycursor = db.query(sql, args);
HashMap map;

List<Map> list = new ArrayList<>();
while (mycursor.moveToNext()) {
map = new HashMap();
for (String colName : mycursor.getColumnNames()) {
map.put(colName, mycursor.getString(mycursor.getColumnIndexOrThrow(colName)));
}
list.add(map);
}

mycursor.close();
return list;
}

/**
* Returns an list of maps, where the map represents each record in the database.
* <p>
* with keys of the map as the column name and values of the map as the values of the
* respective columns.
* <p>
* something like this:
* <p>
* [{id:1, username:ken, age:50}, {id:2, username:matt, age:70}]
*
* @param tableName
* @param queryCols
* @return
*/
public List<Map> all(String tableName, String[] queryCols) {
Cursor mycursor = db.query(SupportSQLiteQueryBuilder.builder(tableName).columns(queryCols).create());
HashMap map;

if (queryCols == null) {
queryCols = mycursor.getColumnNames();
}
Log.e(getClass().getName(), "COLS :: " + Arrays.toString(queryCols));
List<Map> list = new ArrayList<>();
while (mycursor.moveToNext()) {
map = new HashMap();
for (String colName : queryCols) {

map.put(colName, mycursor.getString(mycursor.getColumnIndexOrThrow(colName)));
}
list.add(map);
}

mycursor.close();
return list;
}

/**
* Returns an list of maps, where the map represents each record in the database.
* <p>
* with keys of the map as the column name and values of the map as the values of the
* respective columns.
* <p>
* something like this:
* <p>
* [{id:1, username:ken, age:50}, {id:2, username:matt, age:70}]
*
* @param tableName
* @return
*/
public List<Map> all(String tableName) {
Cursor mycursor = db.query(SupportSQLiteQueryBuilder.builder(tableName).create());
HashMap map;

String queryCols[] = mycursor.getColumnNames();
List<Map> list = new ArrayList<>();
while (mycursor.moveToNext()) {
map = new HashMap();
for (String colName : queryCols) {

map.put(colName, mycursor.getString(mycursor.getColumnIndexOrThrow(colName)));
}
list.add(map);
}

mycursor.close();
return list;
}

/**
* Returns value of the single column requested.
*/
public boolean exists(String tableName, String field, String value) {

Cursor cursor = db.query(SupportSQLiteQueryBuilder.builder(tableName).selection(field, new String[]{value}).create());

if (cursor.getCount() <= 0) {
cursor.close();
return false;
}
cursor.close();
return true;
}

/**
* Returns value of the single column requested.
*/
public boolean exists(String sql, String[] params) {

Cursor cursor = db.query(sql, params);

if (cursor.getCount() <= 0) {
cursor.close();
return false;
}
cursor.close();
return true;
}

/**
* Returns a Map representing a single record based on the query.
*
* @param sql
* @param params parameters to bind to the query
* @return
*/
public HashMap get(String sql, String[] params) {
Cursor c = db.query(sql, params);
String selection = "";
HashMap record = new HashMap();
if (c.moveToFirst()) {
// we use the first column in the sql.
for (String col : c.getColumnNames()) {
record.put(col, c.getString(c.getColumnIndexOrThrow(col)));
}
}
c.close();
return record;
}

@Override
protected void finalize() throws Throwable {
super.finalize();
db.close();
}

public int getScalarInt(String sql, String[] params) {
Cursor c = db.query(sql, params);
int count = -1;
if (c.moveToFirst()) {
count = c.getInt(0);
}
c.close();

return count;
}
}
21 changes: 18 additions & 3 deletions adapter/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'

apply plugin: 'android-maven'
apply plugin: 'maven-publish'

group='com.github.eddmash.androidcomponents'

Expand All @@ -13,7 +13,7 @@ android {
versionCode project.ext.versionCode
versionName "${project.ext.versionName}"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

}
buildTypes {
Expand All @@ -26,7 +26,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})

Expand All @@ -38,3 +38,18 @@ dependencies {
//Place it at the end of the file
//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
// You can then customize attributes of the publication as shown below.
groupId = "${group}"
artifactId = 'adapter'
version = project.ext.versionName
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.eddmash.filterableadapter;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/


import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
targetSdkVersion project.ext.targetSdkVersion
versionCode project.ext.versionCode
versionName "${project.ext.versionName}"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
Expand All @@ -23,10 +23,10 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project.ext.supportDependencies.appCompat
implementation project.ext.supportDependencies.design
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
// implementation project(':android-activerecord')
implementation project(':activerecord')
implementation project(':validation')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.eddmash.androidcomponents;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down

0 comments on commit f541b3a

Please sign in to comment.