Skip to content

Commit

Permalink
Merge pull request #75 from qq157755587/feature-load-sql-from-raw
Browse files Browse the repository at this point in the history
Add two methods to query sql loading from raw text file
  • Loading branch information
emilsjolander committed May 31, 2014
2 parents 16b83fc + 77e0598 commit 80a9de1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
48 changes: 48 additions & 0 deletions library/src/main/java/se/emilsjolander/sprinkles/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ public static <T extends QueryResult> OneQuery<T> one(Class<T> clazz, String sql
return query;
}

/**
* Start a query for a single instance of type T
*
* @param clazz
* The class representing the type of the model you want returned
*
* @param sqlResId
* The raw sql resource id that should be executed.
*
* @param sqlArgs
* The array of arguments to insert instead of ? in the placeholderQuery statement.
* Strings are automatically placeholderQuery escaped.
*
* @param <T>
* The type of the model you want returned
*
* @return the query to execute
*/
public static <T extends QueryResult> OneQuery<T> one(Class<T> clazz, int sqlResId,
Object... sqlArgs) {
String sql = Utils.readRawText(sqlResId);
return one(clazz, sql, sqlArgs);
}

/**
* Start a query for a list of instance of type T
*
Expand Down Expand Up @@ -62,6 +86,30 @@ public static <T extends QueryResult> ManyQuery<T> many(Class<T> clazz, String s
return query;
}

/**
* Start a query for a list of instance of type T
*
* @param clazz
* The class representing the type of the list you want returned
*
* @param sqlResId
* The raw sql resource id that should be executed.
*
* @param sqlArgs
* The array of arguments to insert instead of ? in the placeholderQuery statement.
* Strings are automatically placeholderQuery escaped.
*
* @param <T>
* The type of the list you want returned
*
* @return the query to execute
*/
public static <T extends QueryResult> ManyQuery<T> many(Class<T> clazz, int sqlResId,
Object... sqlArgs) {
String sql = Utils.readRawText(sqlResId);
return many(clazz, sql, sqlArgs);
}

/**
* Start a query for the entire list of instance of type T
*
Expand Down
25 changes: 25 additions & 0 deletions library/src/main/java/se/emilsjolander/sprinkles/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import android.database.Cursor;
import android.net.Uri;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Arrays;
Expand Down Expand Up @@ -132,4 +136,25 @@ static <T> T[] concatArrays(T[] one, T[] two) {
return result;
}

static String readRawText(int rawId) {
final InputStream inputStream = Sprinkles.sInstance.mContext
.getResources().openRawResource(rawId);
final InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
final BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);

String line;
final StringBuilder body = new StringBuilder();
try {
while ((line = bufferedReader.readLine()) != null)
{
body.append(line);
body.append('\n');
}
} catch (IOException e) {
return null;
}
return body.toString();
}
}

0 comments on commit 80a9de1

Please sign in to comment.