Reputation: 2254
I'm writing method wrapping db SELECT statement, that receives SQL query and runs it. Result is collected in Object[][]
. Every cell in database may be Integer or String.
I have problem with return array. It's Object[][]
, so I loose type.
public Object[][] select(String query) {
Object[][] result = new Object[cursor.getCount()][cursor.getColumnCount()];
Cursor cursor = db.rawQuery(query, null);
//Iterate over cursor (rows)
do {
//Iterate over columns (cells with data of different type)
for(int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
int type = cursor.getType(columnIndex);
//assume, that there are only two types - String or Integer (actualy there are 3 more types)
if(type == Cursor.FIELD_TYPE_INTEGER) {
result[cursor.getPosition()][columnIndex] = cursor.getInt(columnIndex);
}
else if(type == Cursor.FIELD_TYPE_STRING) {
result[cursor.getPosition()][columnIndex] = cursor.getString(columnIndex);
}
}
} while (cursor.moveToNext());
return result;
}
I do not want to loose type. E.g. want to be able do like this (pseudocode):
//pseudocode:
result = select("SELECT age, name FROM people")
print( "Hello, " + result[0][0] + ". In ten years you'll be " )
print( result[0][0] + 10 )
//Prints: Hello, John. In ten years you'll be 56
Every book about Java says, that I can't have array of different types. It's clear.
But how should I hold all this Integers and Strings from DB? I tried to use Collections and Generics, but no luck - new to Java.
Also tried to use some kind of Result value holder:
private class Result {
private Class type;
private Object value;
...
public ??? get() { //??? - int or string - here I have some problems :)
return this.type.cast(this.value);
}
//do NOT want to have these:
public int getInt() { return (int) value; }
public int getString() { return (String) value; }
}
Method select(...)
returns Result[][]
in this case.
Answered myself: Understood, that I was trying to code in Java like I did in php. With strong typing I can not make straightforward method, returning all types without explicit casting.
After all I decided to create common data objects and fill them in on selects.
Upvotes: 0
Views: 899
Reputation: 29646
I would probably do something like...
public final class Row {
private final Map<String, ?> columns;
protected Row(final Map<String, ?> columns) {
this.columns = columns;
}
public <T> T get(final String name) {
return (T) columns.get(name);
}
public <T> T get(final String name, final Class<T> type) {
return (T) columns.get(name);
}
}
public final Iterable<Row> select(final String query) {
final Cursor cursor = db.rawQuery(query, null);
final int numRow = cursor.getCount();
final int numCol = cursor.getColumnCount();
final Row[] rows = new Row[numRow];
for (int row = 0; row < numRow; ++row) {
final Map<String, Object> cols = new HashMap<String, Object>(numCol);
for (int col = 0; col < numCol; ++col) {
final String name = cursor.getColumnName(col);
final Object val = null;
final int type = cursor.getType(col);
switch (type) {
case Cursor.FIELD_TYPE_INTEGER:
val = cursor.getInt(col);
break;
case Cursor.FIELD_TYPE_STRING:
val = cursor.getString(col);
break;
...
}
map.put(name, val);
}
rows[row] = new Row(cols);
cursor.moveToNext();
}
return Arrays.asList(rows);
}
private final Row selectFirst(final String query) {
return select(query).iterator().next();
}
Followed by...
final Row result = selectFirst("SELECT age, name FROM people");
print("Hello, " + result.get("name") + ". In ten years you'll be "
+ (result.<Integer>get("age") + 10)).
... or, equivalently:
final Row result = selectFirst("SELECT age, name FROM people");
final String name = result.get("name");
final int age = result.get("age");
print("Hello, " + name + ". In ten years you'll be " + (age + 10));
Using variables allows javac to infer the type parameters for get
(String
and Integer
, respectively).
Since select
returns Iterable<Row>
, you can use it in a for
loop:
for (final Row row : select(...)) {
/* operate on the row */
}
Note that I'm not sure if this will even compile without a tweak or two. Haven't tested it, sorry -- but the general idea should.
Upvotes: 2
Reputation: 1833
Return a List of Maps with the columns names or indexes as keys like this:
public List<Map<Integer,Result>> select(String query)
When extracting your data do this:
List<Map<Integer,Result>> data = new ArrayList<Map<Integer,Result>>();
...
Map<String,Result> elem = new...
elem.put(columnIndex, new Result(..));
data.add(elem);
Collections are surely more powerful than Arrays.
EDIT
Used the Result
holder class you provided.
Upvotes: 0
Reputation: 1132
use a Map<String,List<Result>>
The Result class can store the actual value and its type
class Result {
Object value;
DataType type;
}
enum DataType {
String,
Integer
}
Upvotes: 0
Reputation: 5157
Why dont you a create a Result class which wraps whatever you want and instantiate it?
Upvotes: 3
Reputation: 66657
Best way may be create a java bean with getter/setter with the properties you will get from database. For example if you are getting employee
class Employee
{
int empID;
String empName;
public void setEmpID(String eID)
{
empID= eID;
}
public int getEmpID()
{
return eID;
}
.......
}
Upvotes: 1