提交 32e14584 authored 作者: Thomas Mueller's avatar Thomas Mueller

JaQu: the order of the fields in the database no longer needs to match the order in the database.

上级 07063c2c
......@@ -6,14 +6,14 @@
*/
package org.h2.test.jaqu;
import static org.h2.jaqu.Function.count;
import static org.h2.jaqu.Function.isNull;
import static org.h2.jaqu.Function.length;
import static org.h2.jaqu.Function.*;
import java.math.BigDecimal;
//## Java 1.5 begin ##
import java.util.List;
import org.h2.jaqu.Db;
import org.h2.jaqu.Filter;
import static org.h2.jaqu.Function.*;
//## Java 1.5 end ##
import org.h2.test.TestBase;
/**
......@@ -52,8 +52,8 @@ public class SamplesTest extends TestBase {
db.insertAll(Order.getList());
db.insertAll(ComplexObject.getList());
// TODO support JavaBeans specification
// TODO support all relevant data types (byte[],...)
// TODO nested AND/OR, >, <,...
// TODO support all relevant data types (byte[], ...)
// TODO nested AND/OR, >, <, ...
// TODO NOT
// TODO +, -, *, /, ||, nested operations
// TODO LIKE ESCAPE...
......@@ -77,10 +77,34 @@ public class SamplesTest extends TestBase {
testAnonymousTypes3();
testWhereSimple2();
testWhereSimple3();
testReverseColumns();
db.close();
//## Java 1.5 end ##
}
/**
* A simple test table. The columns are in a different order than in the
* database.
*/
public static class TestReverse {
public String name;
public Integer id;
}
//## Java 1.5 begin ##
private void testReverseColumns() {
db.executeUpdate("create table TestReverse(id int, name varchar)");
TestReverse t = new TestReverse();
t.id = 10;
t.name = "Hello";
db.insert(t);
TestReverse check = db.from(new TestReverse()).selectFirst();
assertEquals(t.name, check.name);
assertEquals(t.id, check.id);
}
//## Java 1.5 end ##
//## Java 1.5 begin ##
private void testWhereSimple2() {
......
......@@ -140,7 +140,13 @@ public class Db {
return (TableDefinition<T>) classMap.get(clazz);
}
ResultSet executeQuery(String sql) {
/**
* Run a SQL query directly against the database.
*
* @param sql the SQL statement
* @return the result set
*/
public ResultSet executeQuery(String sql) {
try {
return conn.createStatement().executeQuery(sql);
} catch (SQLException e) {
......@@ -148,7 +154,13 @@ public class Db {
}
}
int executeUpdate(String sql) {
/**
* Run a SQL statement directly against the database.
*
* @param sql the SQL statement
* @return the update count
*/
public int executeUpdate(String sql) {
try {
return conn.createStatement().executeUpdate(sql);
} catch (SQLException e) {
......
......@@ -63,6 +63,10 @@ public class Query<T> {
return select(false);
}
public T selectFirst() {
return select(false).get(0);
}
public List<T> selectDistinct() {
return select(true);
}
......@@ -81,8 +85,8 @@ public class Query<T> {
private List<T> select(boolean distinct) {
List<T> result = Utils.newArrayList();
SQLStatement selectList = new SQLStatement(db);
selectList.setSQL("*");
TableDefinition<T> def = from.getAliasDefinition();
SQLStatement selectList = def.getSelectList(db);
ResultSet rs = prepare(selectList, distinct).executeQuery();
try {
while (rs.next()) {
......@@ -123,10 +127,10 @@ public class Query<T> {
}
private <X> List<X> select(Class<X> clazz, X x, boolean distinct) {
List<X> result = Utils.newArrayList();
TableDefinition<X> def = db.define(clazz);
SQLStatement selectList = def.getSelectList(this, x);
ResultSet rs = prepare(selectList, distinct).executeQuery();
List<X> result = Utils.newArrayList();
try {
while (rs.next()) {
X row = Utils.newObject(clazz);
......
......@@ -168,7 +168,13 @@ class TableDefinition<T> {
void insert(Db db, Object obj) {
SQLStatement stat = new SQLStatement(db);
StatementBuilder buff = new StatementBuilder("INSERT INTO ");
buff.append(tableName).append(" VALUES(");
buff.append(tableName).append('(');
for (FieldDefinition field : fields) {
buff.appendExceptFirst(", ");
buff.append(field.columnName);
}
buff.append(") VALUES(");
buff.resetCount();
for (FieldDefinition field : fields) {
buff.appendExceptFirst(", ");
buff.append('?');
......@@ -236,6 +242,18 @@ class TableDefinition<T> {
}
}
SQLStatement getSelectList(Db db) {
SQLStatement selectList = new SQLStatement(db);
for (int i = 0; i < fields.size(); i++) {
if (i > 0) {
selectList.appendSQL(", ");
}
FieldDefinition def = fields.get(i);
selectList.appendSQL(def.columnName);
}
return selectList;
}
<Y, X> SQLStatement getSelectList(Query<Y> query, X x) {
SQLStatement selectList = new SQLStatement(query.getDb());
for (int i = 0; i < fields.size(); i++) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论