summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java43
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java6
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java15
-rw-r--r--sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java1
4 files changed, 50 insertions, 15 deletions
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java
index c4367fd..c260a48 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java
@@ -17,6 +17,7 @@ import io.trygvis.persistence.sql.SqlDao;
import io.trygvis.persistence.sql.SqlEntityMeta;
import java.io.IOException;
+import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
@@ -87,14 +88,7 @@ public class DaoGenerator {
exception(sqlException));
}
- {
- p = new Parameters();
- Parameters.ParameterRef id = p.addParameter(entity.idType, "id");
- g.add(new MethodRef(PUBLIC, entity.type, "selectById", p,
- "return Utils.select" + entity.type.className + "ById(super.session.c, " + id.name + ");").
- exception(sqlException));
- }
-
+ g.add(selectById(g.imports));
g.add(selectWhere(g.imports));
{
@@ -215,6 +209,34 @@ public class DaoGenerator {
return "DELETE FROM " + entity.tableName + " WHERE " + join(ss, " AND ") + ";";
}
+ public MethodRef selectById(Imports imports) {
+ TypeRef preparedStatementType = imports.add(PreparedStatement.class);
+ TypeRef sqlExceptionType = imports.add(SQLException.class);
+ Parameters p = new Parameters();
+ Parameters.ParameterRef idP = p.addParameter(entity.idType, "id");
+ TypeRef entityListType = imports.add(List.class).args(entity.type);
+ List<String> body = new ArrayList<>();
+ FieldMirror id = entity.getIdField();
+ body.add("String sql = \"SELECT \" + desc.defaultFields + \" FROM " + entity.tableName + " WHERE " + id.sqlName + "=?\";");
+ body.add(entityListType + " list = runQuery(new Preparator() {");
+ body.add(" @Override");
+ body.add(" public void prepare(" + preparedStatementType.plainName + " stmt) throws " + sqlExceptionType + " {");
+ List<FieldMirror> idFields = entity.idFields;
+ for (int i = 0; i < idFields.size(); i++) {
+ FieldMirror field = idFields.get(i);
+ TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type);
+ body.add(" stmt." + typeHandler.setter(i + 1, idP.name) + ";");
+ }
+ body.add(" }");
+ body.add("}, sql);");
+ body.add("if (list.isEmpty()) {");
+ body.add(" return null;");
+ body.add("}");
+ body.add("return list.get(0);");
+ return new MethodRef(PUBLIC, entity.type, "selectById", p, body).
+ exception(imports.add(SQLException.class));
+ }
+
public MethodRef selectWhere(Imports imports) {
TypeRef sqlExceptionType = imports.add(SQLException.class);
Parameters p = new Parameters();
@@ -272,10 +294,9 @@ public class DaoGenerator {
FieldMirror field = this.entity.fields.get(i);
String value;
- if(field.fieldType == PRIMITIVE) {
+ if (field.fieldType == PRIMITIVE) {
value = row.name + "." + field.javaName;
- }
- else {
+ } else {
EntityMirror referencedEntity = unit.getEntities().get(field.type);
// String x = field.type.plainName + " " + field.javaName + " = " +
value = "session." + toFieldName(referencedEntity.type) + ".selectById(" + row.name + "." + field.javaName + ")";
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java
index 2848c8e..51b3879 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java
@@ -43,7 +43,7 @@ public class DaoUtilsGenerator {
public ClassG utils() {
ClassG g = new ClassG(PUBLIC | STATIC, entity.utilsType);
g.add(insertInto(g.imports));
- g.add(selectById(g.imports));
+// g.add(selectById(g.imports));
// g.add(selectWhere(g.imports));
g.add(update(g.imports));
g.add(delete(g.imports));
@@ -66,7 +66,7 @@ public class DaoUtilsGenerator {
List<String> body = new ArrayList<>();
- body.add("try(" + psType + " stmt = " + con.name + ".prepareStatement(insertIntoSql)) {");
+ body.add("try (" + psType + " stmt = " + con.name + ".prepareStatement(insertIntoSql)) {");
int i = 0;
for (FieldMirror field : entity.fields) {
// Assume all ID fields are generated for now.
@@ -153,7 +153,7 @@ public class DaoUtilsGenerator {
List<String> body = new ArrayList<>();
- body.add("try(" + psType + " stmt = " + con.name + ".prepareStatement(deleteFromSql)) {");
+ body.add("try (" + psType + " stmt = " + con.name + ".prepareStatement(deleteFromSql)) {");
for (int i = 0; i < entity.idFields.size(); i++) {
FieldMirror field = entity.idFields.get(i);
p.addParameter(field.type, field.javaName);
diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java
index c253229..361fd95 100644
--- a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java
+++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlDao.java
@@ -36,10 +36,19 @@ public abstract class SqlDao<Id, T, S extends SqlSession, Row> {
protected abstract T newEntity(Row row) throws SQLException;
protected List<T> runQuery(final String sql) throws SQLException {
+ return runQuery(new Preparator() {
+ @Override
+ public void prepare(PreparedStatement stmt) throws SQLException {
+ }
+ }, sql);
+ }
+
+ protected List<T> runQuery(final Preparator preparator, final String sql) throws SQLException {
return session.query(new SqlExecutor.QueryCommand<T>() {
@Override
public List<T> run(Connection c) throws SQLException {
try (PreparedStatement stmt = c.prepareStatement(sql)) {
+ preparator.prepare(stmt);
ResultSet rs = stmt.executeQuery();
List<Row> rows = new ArrayList<>();
@@ -47,7 +56,7 @@ public abstract class SqlDao<Id, T, S extends SqlSession, Row> {
rows.add(newRow(rs));
}
- List<T> entities = new ArrayList<T>(rows.size());
+ List<T> entities = new ArrayList<>(rows.size());
for (Row row : rows) {
entities.add(newEntity(row));
}
@@ -57,4 +66,8 @@ public abstract class SqlDao<Id, T, S extends SqlSession, Row> {
}
});
}
+
+ protected static interface Preparator {
+ void prepare(PreparedStatement stmt) throws SQLException;
+ }
}
diff --git a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java
index b5e58eb..43f1206 100644
--- a/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java
+++ b/sql-persistence/src/main/java/io/trygvis/persistence/sql/SqlSession.java
@@ -6,6 +6,7 @@ import java.sql.Statement;
import java.util.List;
public class SqlSession {
+ // TODO: Make private
public final Connection c;
public SqlSession(Connection c) {