Skip to content
Browse files

Convert QueryExecutorImpl to Gosu

  • Loading branch information...
1 parent 0f385bc commit 6b1c3eaf2298e90fd1aadce73959ad1f5865e531 @akeefer committed Feb 16, 2012
Showing with 110 additions and 143 deletions.
  1. +110 −0 tosa-runtime/src/tosa/impl/QueryExecutorImpl.gs
  2. +0 −143 tosa-runtime/src/tosa/impl/QueryExecutorImpl.java
View
110 tosa-runtime/src/tosa/impl/QueryExecutorImpl.gs
@@ -0,0 +1,110 @@
+package tosa.impl
+
+uses tosa.api.IDatabase
+uses tosa.api.IPreparedStatementParameter
+uses tosa.loader.Util
+uses java.util.Arrays
+uses java.lang.IllegalStateException
+uses tosa.api.IDBObject
+uses tosa.loader.IDBType
+uses java.sql.ResultSet
+
+/**
+ *
+ */
+class QueryExecutorImpl implements QueryExecutor {
+
+ private var _db : IDatabase
+
+ public construct(db : IDatabase) {
+ _db = db;
+ }
+
+ // TODO - AHK - Clean up the query execution API for reals . . .
+
+ // TODO - AHK - Should this be a long instead?
+ override function count(profilerTag : String, sqlStatement : String, parameters : IPreparedStatementParameter[]) : int {
+ // TODO - AHK - Verify that it starts with "SELECT count(*) as count"
+ var profiler = Util.newProfiler(profilerTag);
+ profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
+ try {
+ var results = _db.getDBExecutionKernel().executeSelect(
+ sqlStatement,
+ \r -> r.getInt("count"),
+ parameters);
+ if (results.size() == 0) {
+ return 0;
+ } else if (results.size() == 1) {
+ return results.get(0);
+ } else {
+ throw new IllegalStateException("Expected count query " + sqlStatement + " to return 0 or 1 result, but got " + results.size());
+ }
+ } finally {
+ profiler.stop();
+ }
+ }
+
+ override function selectEntity(profilerTag : String, type : IDBType, sqlStatement : String, parameters : IPreparedStatementParameter[]) : List<IDBObject> {
+ // TODO - AHK - Ensure that it starts with SELECT * ?
+ // TODO - AHK - Verify that the db type is from the database we have?
+ var profiler = Util.newProfiler(profilerTag);
+ profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
+ try {
+ return _db.getDBExecutionKernel().executeSelect(sqlStatement,
+ \r -> buildObject(type, r),
+ parameters);
+ } finally {
+ profiler.stop();
+ }
+ }
+
+ override function update(profilerTag : String, sqlStatement : String, parameters : IPreparedStatementParameter[]) {
+ // TODO - AHK - Verify it starts with UPDATE ?
+ var profiler = Util.newProfiler(profilerTag);
+ profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
+ try {
+ _db.getDBExecutionKernel().executeUpdate(sqlStatement,
+ parameters);
+ } finally {
+ profiler.stop();
+ }
+ }
+
+ override function insert(profilerTag : String, sqlStatement : String, parameters : IPreparedStatementParameter[]) : Object {
+ // TODO - AHK - Verify it starts with INSERT ?
+ var profiler = Util.newProfiler(profilerTag);
+ profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
+ try {
+ return _db.getDBExecutionKernel().executeInsert(sqlStatement,
+ parameters);
+ } finally {
+ profiler.stop();
+ }
+ }
+
+ override function delete(profilerTag : String, sqlStatement : String, parameters : IPreparedStatementParameter[]) {
+ // TODO - AHK - Verify it starts with DELETE ?
+ var profiler = Util.newProfiler(profilerTag);
+ profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
+ try {
+ _db.getDBExecutionKernel().executeDelete(sqlStatement,
+ parameters);
+ } finally {
+ profiler.stop();
+ }
+ }
+
+ // TODO - AHK - Find a home for this function. Maybe on CachedDBObject? Or somewhere else?
+
+ static function buildObject(type : IDBType, resultSet : ResultSet) : IDBObject {
+ var obj = new CachedDBObject(type, false)
+ var table = type.getTable();
+ for (column in table.getColumns()) {
+ // TODO - AHK - This is a little sketch, perhaps
+ var resultObject = column.getColumnType().readFromResultSet(resultSet, table.getName() + "." + column.getName());
+ obj.setColumnValue(column.getName(), resultObject);
+ }
+ return obj;
+ }
+
+}
View
143 tosa-runtime/src/tosa/impl/QueryExecutorImpl.java
@@ -1,143 +0,0 @@
-package tosa.impl;
-
-import org.slf4j.profiler.Profiler;
-import tosa.api.IDBColumn;
-import tosa.api.IDBObject;
-import tosa.api.IDBTable;
-import tosa.api.IDatabase;
-import tosa.api.IPreparedStatementParameter;
-import tosa.api.IQueryResultProcessor;
-import tosa.loader.IDBType;
-import tosa.loader.Util;
-
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * Created by IntelliJ IDEA.
- * User: alan
- * Date: 5/8/11
- * Time: 3:41 PM
- * To change this template use File | Settings | File Templates.
- */
-public class QueryExecutorImpl implements QueryExecutor {
-
- private IDatabase _db;
-
- public QueryExecutorImpl(IDatabase db) {
- _db = db;
- }
-
- // TODO - AHK - Clean up the query execution API for reals . . .
-
- @Override
- // TODO - AHK - Should this be a long instead?
- public int count(String profilerTag, String sqlStatement, IPreparedStatementParameter... parameters) {
- // TODO - AHK - Verify that it starts with "SELECT count(*) as count"
- Profiler profiler = Util.newProfiler(profilerTag);
- profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
- try {
- List<Integer> results = _db.getDBExecutionKernel().executeSelect(
- sqlStatement,
- new CountQueryResultProcessor(),
- parameters);
- if (results.size() == 0) {
- return 0;
- } else if (results.size() == 1) {
- return results.get(0);
- } else {
- throw new IllegalStateException("Expected count query " + sqlStatement + " to return 0 or 1 result, but got " + results.size());
- }
- } finally {
- profiler.stop();
- }
- }
-
- private static class CountQueryResultProcessor implements IQueryResultProcessor<Integer> {
- @Override
- public Integer processResult(ResultSet result) throws SQLException {
- return result.getInt("count");
- }
- }
-
- @Override
- public List<IDBObject> selectEntity(String profilerTag, IDBType type, String sqlStatement, IPreparedStatementParameter... parameters) {
- // TODO - AHK - Ensure that it starts with SELECT * ?
- // TODO - AHK - Verify that the db type is from the database we have?
- Profiler profiler = Util.newProfiler(profilerTag);
- profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
- try {
- return _db.getDBExecutionKernel().executeSelect(sqlStatement,
- new CachedDBQueryResultProcessor(type),
- parameters);
- } finally {
- profiler.stop();
- }
- }
-
- @Override
- public void update(String profilerTag, String sqlStatement, IPreparedStatementParameter... parameters) {
- // TODO - AHK - Verify it starts with UPDATE ?
- Profiler profiler = Util.newProfiler(profilerTag);
- profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
- try {
- _db.getDBExecutionKernel().executeUpdate(sqlStatement,
- parameters);
- } finally {
- profiler.stop();
- }
- }
-
- @Override
- public Object insert(String profilerTag, String sqlStatement, IPreparedStatementParameter... parameters) {
- // TODO - AHK - Verify it starts with INSERT ?
- Profiler profiler = Util.newProfiler(profilerTag);
- profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
- try {
- return _db.getDBExecutionKernel().executeInsert(sqlStatement,
- parameters);
- } finally {
- profiler.stop();
- }
- }
-
- @Override
- public void delete(String profilerTag, String sqlStatement, IPreparedStatementParameter... parameters) {
- // TODO - AHK - Verify it starts with DELETE ?
- Profiler profiler = Util.newProfiler(profilerTag);
- profiler.start(sqlStatement + " (" + Arrays.asList(parameters) + ")");
- try {
- _db.getDBExecutionKernel().executeDelete(sqlStatement,
- parameters);
- } finally {
- profiler.stop();
- }
- }
-
- // TODO - AHK - This is a duplicate AND it's public
- // TODO - AHK The general query execution API here just needs a weeeee bit of help
- public static class CachedDBQueryResultProcessor implements IQueryResultProcessor<IDBObject> {
- private IDBType _type;
-
- public CachedDBQueryResultProcessor(IDBType type) {
- _type = type;
- }
-
- @Override
- public IDBObject processResult(ResultSet result) throws SQLException {
- return buildObject(_type, result);
- }
- }
-
- public static IDBObject buildObject(IDBType type, ResultSet resultSet) throws SQLException {
- IDBObject obj = RuntimeBridge.createDBObject(type, false);
- IDBTable table = type.getTable();
- for (IDBColumn column : table.getColumns()) {
- Object resultObject = column.getColumnType().readFromResultSet(resultSet, table.getName() + "." + column.getName());
- obj.setColumnValue(column.getName(), resultObject);
- }
- return obj;
- }
-}

0 comments on commit 6b1c3ea

Please sign in to comment.
Something went wrong with that request. Please try again.