-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pistolla/master
support androidx.sqlite
- Loading branch information
Showing
47 changed files
with
542 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
activerecord/src/androidTest/java/com/eddmash/db/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
activerecord/src/main/java/com/eddmash/db/SupportActiveRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
adapter/src/androidTest/java/com/eddmash/filterableadapter/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
app/src/androidTest/java/com/eddmash/androidcomponents/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.