ENH: added SqlRow factory method to RawSqlService (#1312)

* ENH: added SqlRow factory method to RawSqlService

* compile fixes

* ADD: sqlRow factory also to RawSqlBuilder
This commit is contained in:
Roland Praml
2018-03-02 12:13:16 +13:00
committed by Rob Bygrave
parent 78628ffad0
commit aea7d937eb
3 changed files with 38 additions and 0 deletions
+10
View File
@@ -1,6 +1,7 @@
package io.ebean;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Builds RawSql instances from a SQL string and column mappings.
@@ -24,6 +25,15 @@ public interface RawSqlBuilder {
return XServiceProvider.rawSql().resultSet(resultSet, propertyNames);
}
/**
* Create and return a SqlRow based on the resultSet and the dbTrueValue.
* @throws SQLException
*/
static SqlRow sqlRow(ResultSet resultSet, final String dbTrueValue) throws SQLException {
return XServiceProvider.rawSql().sqlRow(resultSet, dbTrueValue);
}
/**
* Return an unparsed RawSqlBuilder. Unlike a parsed one this query can not be
* modified - so no additional WHERE or HAVING expressions can be added to
@@ -2,8 +2,10 @@ package io.ebean.service;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
import io.ebean.SqlRow;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Service provided by Ebean for parsing and column mapping raw SQL queries.
@@ -24,4 +26,10 @@ public interface SpiRawSqlService {
* Unparsed SQL so explicit column mapping expected.
*/
RawSqlBuilder unparsed(String sql);
/**
* Create based on a JDBC ResultSet.
* @throws SQLException
*/
SqlRow sqlRow(ResultSet resultSet, String dbTrueValue) throws SQLException;
}
@@ -2,9 +2,13 @@ package io.ebeaninternal.server.rawsql;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
import io.ebean.SqlRow;
import io.ebean.service.SpiRawSqlService;
import io.ebeaninternal.server.query.DefaultSqlRow;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class DRawSqlService implements SpiRawSqlService {
@@ -28,4 +32,20 @@ public class DRawSqlService implements SpiRawSqlService {
SpiRawSql.Sql s = new SpiRawSql.Sql(sql);
return new DRawSqlBuilder(s, new SpiRawSql.ColumnMapping());
}
@Override
public SqlRow sqlRow(ResultSet resultSet, String dbTrueValue) throws SQLException {
ResultSetMetaData meta = resultSet.getMetaData();
int estCap = (int) (meta.getColumnCount() / 0.7f) + 1;
DefaultSqlRow ret = new DefaultSqlRow(estCap, 0.75f, dbTrueValue);
for (int i = 1; i <= meta.getColumnCount(); i++) {
String name = meta.getColumnLabel(i);
if (name == null) {
name = meta.getColumnName(i);
}
ret.put(name, resultSet.getObject(i));
}
return ret;
}
}