com.j256.ormlite.stmt.RawRowMapperImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ormlite-core Show documentation
Show all versions of ormlite-core Show documentation
Lightweight Object Relational Model (ORM) for persisting objects to SQL databases.
package com.j256.ormlite.stmt;
import java.sql.SQLException;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RawRowMapper;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.table.TableInfo;
/**
* Default row mapper when you are using the {@link Dao#queryRaw(String, RawRowMapper, String...)}.
*
* @author graywatson
*/
public class RawRowMapperImpl implements RawRowMapper {
private final Dao dao;
private final TableInfo tableInfo;
public RawRowMapperImpl(Dao dao) {
this.dao = dao;
this.tableInfo = dao.getTableInfo();
}
@Override
public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
// create our object
T rowObj = dao.createObjectInstance();
for (int i = 0; i < columnNames.length; i++) {
// sanity check, prolly will never happen but let's be careful out there
if (i >= resultColumns.length) {
continue;
}
// run through and convert each field
FieldType fieldType = tableInfo.getFieldTypeByColumnName(columnNames[i]);
Object fieldObj = fieldType.convertStringToJavaField(resultColumns[i], i);
// assign it to the row object
fieldType.assignField(dao.getConnectionSource(), rowObj, fieldObj, false, null);
}
return rowObj;
}
}