提交 78488f70 authored 作者: Thomas Mueller's avatar Thomas Mueller

JaQu improvements

上级 3468848d
...@@ -53,6 +53,7 @@ import org.h2.test.db.TestTransaction; ...@@ -53,6 +53,7 @@ import org.h2.test.db.TestTransaction;
import org.h2.test.db.TestTriggersConstraints; import org.h2.test.db.TestTriggersConstraints;
import org.h2.test.db.TestTwoPhaseCommit; import org.h2.test.db.TestTwoPhaseCommit;
import org.h2.test.db.TestView; import org.h2.test.db.TestView;
import org.h2.test.jaqu.SamplesTest;
import org.h2.test.jdbc.TestBatchUpdates; import org.h2.test.jdbc.TestBatchUpdates;
import org.h2.test.jdbc.TestCallableStatement; import org.h2.test.jdbc.TestCallableStatement;
import org.h2.test.jdbc.TestCancel; import org.h2.test.jdbc.TestCancel;
...@@ -268,14 +269,16 @@ java org.h2.test.TestAll timer ...@@ -268,14 +269,16 @@ java org.h2.test.TestAll timer
/* /*
JaQu
try java.lang.instrumentation.*
fix checkstyle fix checkstyle
row level locking row level locking
run the performance tests as part of the unit test embedded + clustering (feature request)
switch to JDK 1.6 by default start server with connection
integrate java queries
jazoon jazoon
...@@ -342,10 +345,6 @@ Add where required // TODO: change in version 1.1 ...@@ -342,10 +345,6 @@ Add where required // TODO: change in version 1.1
http://www.w3schools.com/sql/ http://www.w3schools.com/sql/
History:
Roadmap:
*/ */
if (args.length > 0) { if (args.length > 0) {
if ("crash".equals(args[0])) { if ("crash".equals(args[0])) {
...@@ -591,6 +590,9 @@ Roadmap: ...@@ -591,6 +590,9 @@ Roadmap:
new TestTriggersConstraints().runTest(this); new TestTriggersConstraints().runTest(this);
new TestTwoPhaseCommit().runTest(this); new TestTwoPhaseCommit().runTest(this);
new TestView().runTest(this); new TestView().runTest(this);
// jaqu
new SamplesTest().runTest(this);
// jdbc // jdbc
new TestBatchUpdates().runTest(this); new TestBatchUpdates().runTest(this);
......
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
*/ */
package org.h2.test.jaqu; package org.h2.test.jaqu;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* A table containing customer data. * A table containing customer data.
...@@ -28,7 +28,7 @@ public class Customer { ...@@ -28,7 +28,7 @@ public class Customer {
this.region = region; this.region = region;
} }
//## Java 1.6 begin ## //## Java 1.5 begin ##
public static List<Customer> getCustomerList() { public static List<Customer> getCustomerList() {
Customer[] list = new Customer[] { Customer[] list = new Customer[] {
new Customer("ALFKI", "WA"), new Customer("ALFKI", "WA"),
...@@ -36,5 +36,5 @@ public class Customer { ...@@ -36,5 +36,5 @@ public class Customer {
new Customer("ANTON", "CA") }; new Customer("ANTON", "CA") };
return Arrays.asList(list); return Arrays.asList(list);
} }
//## Java 1.6 end ## //## Java 1.5 end ##
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
package org.h2.test.jaqu; package org.h2.test.jaqu;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import static org.h2.jaqu.Define.*; import static org.h2.jaqu.Define.*;
import java.math.BigDecimal; import java.math.BigDecimal;
...@@ -15,12 +15,12 @@ import java.util.Date; ...@@ -15,12 +15,12 @@ import java.util.Date;
import java.util.List; import java.util.List;
import org.h2.jaqu.Table; import org.h2.jaqu.Table;
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* A table containing order data. * A table containing order data.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class Order implements Table { public class Order implements Table {
public String customerId; public String customerId;
...@@ -59,4 +59,4 @@ public class Order implements Table { ...@@ -59,4 +59,4 @@ public class Order implements Table {
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -6,18 +6,19 @@ ...@@ -6,18 +6,19 @@
*/ */
package org.h2.test.jaqu; package org.h2.test.jaqu;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.h2.jaqu.Table; import org.h2.jaqu.Table;
import static org.h2.jaqu.Define.*; import static org.h2.jaqu.Define.*;
//## Java 1.5 end ##
/** /**
* A table containing product data. * A table containing product data.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class Product implements Table { public class Product implements Table {
public Integer productId; public Integer productId;
...@@ -134,5 +135,4 @@ public class Product implements Table { ...@@ -134,5 +135,4 @@ public class Product implements Table {
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -6,37 +6,45 @@ ...@@ -6,37 +6,45 @@
*/ */
package org.h2.test.jaqu; package org.h2.test.jaqu;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.List; import java.util.List;
import org.h2.jaqu.Db; import org.h2.jaqu.Db;
//## Java 1.5 end ##
import org.h2.test.TestBase; import org.h2.test.TestBase;
//## Java 1.6 end ##
/** /**
* Implementation of the 101 LINQ Samples as described in * Implementation of the 101 LINQ Samples as described in
* http://msdn2.microsoft.com/en-us/vcsharp/aa336760.aspx * http://msdn2.microsoft.com/en-us/vcsharp/aa336760.aspx
*/ */
//## Java 1.6 begin ##
public class SamplesTest extends TestBase { public class SamplesTest extends TestBase {
//## Java 1.5 begin ##
private Db db; private Db db;
public static void main(String[] args) throws Exception {
new SamplesTest().test();
}
//## Java 1.5 end ##
public void test() throws Exception { public void test() throws Exception {
//## Java 1.5 begin ##
db = Db.open("jdbc:h2:mem:", "sa", "sa"); db = Db.open("jdbc:h2:mem:", "sa", "sa");
db.insertAll(Product.getProductList()); db.insertAll(Product.getProductList());
db.insertAll(Customer.getCustomerList()); db.insertAll(Customer.getCustomerList());
db.insertAll(Order.getOrderList()); db.insertAll(Order.getOrderList());
testSelectManyCompoundFrom2(); testSelectManyCompoundFrom2();
testAnonymousTypes3();
testWhereSimple4(); testWhereSimple4();
testSelectSimple2();
testAnonymousTypes3();
testWhereSimple2(); testWhereSimple2();
testWhereSimple3(); testWhereSimple3();
testSelectSimple2();
db.close(); db.close();
//## Java 1.5 end ##
} }
//## Java 1.5 begin ##
private void testWhereSimple2() throws Exception { private void testWhereSimple2() throws Exception {
// var soldOutProducts = // var soldOutProducts =
...@@ -44,7 +52,7 @@ public class SamplesTest extends TestBase { ...@@ -44,7 +52,7 @@ public class SamplesTest extends TestBase {
// where p.UnitsInStock == 0 // where p.UnitsInStock == 0
// select p; // select p;
Product p = db.alias(Product.class); Product p = new Product();
List<Product> soldOutProducts = List<Product> soldOutProducts =
db.from(p). db.from(p).
where(p.unitsInStock).is(0). where(p.unitsInStock).is(0).
...@@ -66,11 +74,11 @@ public class SamplesTest extends TestBase { ...@@ -66,11 +74,11 @@ public class SamplesTest extends TestBase {
// && p.UnitPrice > 3.00M // && p.UnitPrice > 3.00M
// select p; // select p;
Product p = db.alias(Product.class); Product p = new Product();
List<Product> expensiveInStockProducts = List<Product> expensiveInStockProducts =
db.from(p). db.from(p).
where(p.unitsInStock).isBigger(0). where(p.unitsInStock).bigger(0).
and(p.unitPrice).isBigger(3.0). and(p.unitPrice).bigger(3.0).
orderBy(p.productId).select(); orderBy(p.productId).select();
String result = ""; String result = "";
...@@ -114,7 +122,7 @@ public class SamplesTest extends TestBase { ...@@ -114,7 +122,7 @@ public class SamplesTest extends TestBase {
// where c.Region == "WA" // where c.Region == "WA"
// select c; // select c;
Customer c = db.alias(Customer.class); Customer c = new Customer();
List<Customer> waCustomers = List<Customer> waCustomers =
db.from(c). db.from(c).
where(c.region).is("WA"). where(c.region).is("WA").
...@@ -131,7 +139,7 @@ public class SamplesTest extends TestBase { ...@@ -131,7 +139,7 @@ public class SamplesTest extends TestBase {
// from p in products // from p in products
// select p.ProductName; // select p.ProductName;
Product p = db.alias(Product.class); Product p = new Product();
List<String> productNames = List<String> productNames =
db.from(p). db.from(p).
orderBy(p.productId).select(p.productName); orderBy(p.productId).select(p.productName);
...@@ -141,12 +149,12 @@ public class SamplesTest extends TestBase { ...@@ -141,12 +149,12 @@ public class SamplesTest extends TestBase {
assertEquals(products.get(i).productName, productNames.get(i)); assertEquals(products.get(i).productName, productNames.get(i));
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* A result set class containing the product name and price. * A result set class containing the product name and price.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public static class ProductPrice { public static class ProductPrice {
public String productName; public String productName;
public String category; public String category;
...@@ -163,7 +171,7 @@ public class SamplesTest extends TestBase { ...@@ -163,7 +171,7 @@ public class SamplesTest extends TestBase {
// Price = p.UnitPrice // Price = p.UnitPrice
// }; // };
final Product p = db.alias(Product.class); final Product p = new Product();
List<ProductPrice> productInfos = List<ProductPrice> productInfos =
db.from(p).orderBy(p.productId). db.from(p).orderBy(p.productId).
select(new ProductPrice() { { select(new ProductPrice() { {
...@@ -182,19 +190,19 @@ public class SamplesTest extends TestBase { ...@@ -182,19 +190,19 @@ public class SamplesTest extends TestBase {
assertEquals(p2.unitPrice, pr.price); assertEquals(p2.unitPrice, pr.price);
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* A result set class containing customer data and the order total. * A result set class containing customer data and the order total.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public static class CustOrder { public static class CustOrder {
public String customerId; public String customerId;
public Integer orderId; public Integer orderId;
public BigDecimal total; public BigDecimal total;
} }
private void testSelectManyCompoundFrom2() { private void testSelectManyCompoundFrom2() throws Exception {
// var orders = // var orders =
// from c in customers, // from c in customers,
...@@ -206,12 +214,12 @@ public class SamplesTest extends TestBase { ...@@ -206,12 +214,12 @@ public class SamplesTest extends TestBase {
// o.Total // o.Total
// }; // };
final Customer c = db.alias(Customer.class); final Customer c = new Customer();
final Order o = db.alias(Order.class); final Order o = new Order();
List<CustOrder> orders = List<CustOrder> orders =
db.from(c). db.from(c).
innerJoin(o).on(c.customerId).is(o.customerId). innerJoin(o).on(c.customerId).is(o.customerId).
where(o.total).isSmaller(new BigDecimal("500.00")). where(o.total).smaller(new BigDecimal("500.00")).
select(new CustOrder() { { select(new CustOrder() { {
customerId = c.customerId; customerId = c.customerId;
orderId = o.orderId; orderId = o.orderId;
...@@ -227,10 +235,10 @@ public class SamplesTest extends TestBase { ...@@ -227,10 +235,10 @@ public class SamplesTest extends TestBase {
buff.append(';'); buff.append(';');
} }
String s = buff.toString(); String s = buff.toString();
System.out.println(s); assertEquals("c:ALFKI/o:10702;c:ALFKI/o:10952;c:ANATR/o:10308;" +
// int todoImplementListResolution; "c:ANATR/o:10625;c:ANATR/o:10759;c:ANTON/o:10355;" +
// int todoVerifyResult; "c:ANTON/o:10365;c:ANTON/o:10682;", s);
} }
//## Java 1.5 end ##
} }
//## Java 1.6 end ##
...@@ -7,10 +7,10 @@ ...@@ -7,10 +7,10 @@
package org.h2.jaqu; package org.h2.jaqu;
/** /**
* A enumeration of compare operations. * An enumeration of compare operations.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public enum CompareType { enum CompareType {
EQUAL("=", true), EQUAL("=", true),
BIGGER(">", true), BIGGER(">", true),
BIGGER_EQUAL(">=", true), BIGGER_EQUAL(">=", true),
...@@ -29,14 +29,14 @@ public enum CompareType { ...@@ -29,14 +29,14 @@ public enum CompareType {
this.hasRightExpression = hasRightExpression; this.hasRightExpression = hasRightExpression;
} }
public String toString() { String getString() {
return text; return text;
} }
public boolean hasRightExpression() { boolean hasRightExpression() {
return hasRightExpression; return hasRightExpression;
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -11,8 +11,8 @@ package org.h2.jaqu; ...@@ -11,8 +11,8 @@ package org.h2.jaqu;
* *
* @param <A> the operand type * @param <A> the operand type
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class Condition<A> implements ConditionToken { class Condition<A> implements ConditionToken {
Query< ? > query; Query< ? > query;
CompareType compareType; CompareType compareType;
A x, y; A x, y;
...@@ -24,11 +24,11 @@ public class Condition<A> implements ConditionToken { ...@@ -24,11 +24,11 @@ public class Condition<A> implements ConditionToken {
this.y = y; this.y = y;
} }
public String toString() { public String getString() {
if (compareType.hasRightExpression()) { if (compareType.hasRightExpression()) {
return query.getString(x) + compareType.toString() + query.getString(y); return query.getString(x) + compareType.getString() + query.getString(y);
} }
return query.getString(x) + compareType.toString(); return query.getString(x) + compareType.getString();
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -9,8 +9,8 @@ package org.h2.jaqu; ...@@ -9,8 +9,8 @@ package org.h2.jaqu;
/** /**
* An OR or an AND condition. * An OR or an AND condition.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public enum ConditionAndOr implements ConditionToken { enum ConditionAndOr implements ConditionToken {
AND("AND"), AND("AND"),
OR("OR"); OR("OR");
...@@ -20,9 +20,9 @@ public enum ConditionAndOr implements ConditionToken { ...@@ -20,9 +20,9 @@ public enum ConditionAndOr implements ConditionToken {
this.text = text; this.text = text;
} }
public String toString() { public String getString() {
return text; return text;
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -10,6 +10,6 @@ package org.h2.jaqu; ...@@ -10,6 +10,6 @@ package org.h2.jaqu;
* Classes implementing this interface can be used as a token in a conditional * Classes implementing this interface can be used as a token in a conditional
* expression. * expression.
*/ */
public interface ConditionToken { interface ConditionToken {
String toString(); String getString();
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
package org.h2.jaqu; package org.h2.jaqu;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
...@@ -15,28 +15,24 @@ import java.util.List; ...@@ -15,28 +15,24 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.h2.jaqu.TableDefinition.FieldDefinition;
import org.h2.jaqu.util.Utils; import org.h2.jaqu.util.Utils;
import org.h2.jaqu.util.WeakIdentityHashMap;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* This class represents a connection to a database. * This class represents a connection to a database.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class Db { public class Db {
private final Connection conn; private final Connection conn;
private final Map<Class, TableDefinition> classMap = Utils.newHashMap(); private final Map<Class, TableDefinition> classMap = Utils.newHashMap();
private final WeakIdentityHashMap<Object, FieldDefinition> aliasMap =
Utils.newWeakIdentityHashMap();
Db(Connection conn) { Db(Connection conn) {
this.conn = conn; this.conn = conn;
} }
public static <T> T instance(Class<T> clazz) { private static <T> T instance(Class<T> clazz) {
try { try {
return clazz.newInstance(); return clazz.newInstance();
} catch (Exception e) { } catch (Exception e) {
...@@ -75,10 +71,10 @@ public class Db { ...@@ -75,10 +71,10 @@ public class Db {
} }
public <T extends Object> Query<T> from(T alias) { public <T extends Object> Query<T> from(T alias) {
return new Query<T>(this, alias); return Query.from(this, alias);
} }
public <T> void createTable(Class<T> clazz) { <T> void createTable(Class<T> clazz) {
define(clazz).createTableIfRequired(this); define(clazz).createTableIfRequired(this);
} }
...@@ -97,12 +93,13 @@ public class Db { ...@@ -97,12 +93,13 @@ public class Db {
return def; return def;
} }
public <T> T alias(Class<T> clazz) { // private <T> T alias(Class<T> clazz) {
TableDefinition def = define(clazz); // TableDefinition def = define(clazz);
T alias = instance(clazz); // T alias = instance(clazz);
def.initObject(alias, aliasMap); // SelectTable table = new SelectTable(this, null, alias, false);
return alias; // def.initSelectObject(table, alias, aliasMap);
} // return alias;
// }
public void close() { public void close() {
try { try {
...@@ -126,7 +123,7 @@ public class Db { ...@@ -126,7 +123,7 @@ public class Db {
} }
} }
public PreparedStatement prepare(String sql) { PreparedStatement prepare(String sql) {
try { try {
return conn.prepareStatement(sql); return conn.prepareStatement(sql);
} catch (SQLException e) { } catch (SQLException e) {
...@@ -138,7 +135,7 @@ public class Db { ...@@ -138,7 +135,7 @@ public class Db {
return classMap.get(clazz); return classMap.get(clazz);
} }
public ResultSet executeQuery(String sql) { ResultSet executeQuery(String sql) {
try { try {
return conn.createStatement().executeQuery(sql); return conn.createStatement().executeQuery(sql);
} catch (SQLException e) { } catch (SQLException e) {
...@@ -146,9 +143,13 @@ public class Db { ...@@ -146,9 +143,13 @@ public class Db {
} }
} }
public <X> FieldDefinition<X> getFieldDefinition(X x) { // <X> FieldDefinition<X> getFieldDefinition(X x) {
return aliasMap.get(x); // return aliasMap.get(x).getFieldDefinition();
} // }
//
// <X> SelectColumn<X> getSelectColumn(X x) {
// return aliasMap.get(x);
// }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -10,7 +10,7 @@ package org.h2.jaqu; ...@@ -10,7 +10,7 @@ package org.h2.jaqu;
* This class provides utility methods to define primary keys, indexes, and set * This class provides utility methods to define primary keys, indexes, and set
* the name of the table. * the name of the table.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class Define { public class Define {
private static TableDefinition currentTableDefinition; private static TableDefinition currentTableDefinition;
...@@ -35,7 +35,6 @@ public class Define { ...@@ -35,7 +35,6 @@ public class Define {
currentTable = table; currentTable = table;
tableDefinition.mapObject(table); tableDefinition.mapObject(table);
table.define(); table.define();
tableDefinition.apply();
currentTable = null; currentTable = null;
} }
...@@ -48,4 +47,4 @@ public class Define { ...@@ -48,4 +47,4 @@ public class Define {
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -6,49 +6,50 @@ ...@@ -6,49 +6,50 @@
*/ */
package org.h2.jaqu; package org.h2.jaqu;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import org.h2.jaqu.TableDefinition.FieldDefinition; import org.h2.jaqu.TableDefinition.FieldDefinition;
import org.h2.jaqu.util.Utils; import org.h2.jaqu.util.Utils;
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* This class represents a query. * This class represents a query.
* *
* @param <T> the return type * @param <T> the return type
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class Query<T> { public class Query<T> {
private Db db; private Db db;
private T alias; private SelectTable<T> from;
private TableDefinition aliasDef;
private ArrayList<ConditionToken> conditions = Utils.newArrayList(); private ArrayList<ConditionToken> conditions = Utils.newArrayList();
// private HashMap<Object, TableDefinition> join = Utils.newHashMap(); private ArrayList<SelectTable> joins = Utils.newArrayList();
private final HashMap<Object, SelectColumn> aliasMap = Utils.newHashMap();
public Query(Db db, T alias) { Query(Db db) {
this.db = db; this.db = db;
this.alias = alias;
aliasDef = db.getTableDefinition(alias.getClass());
} }
@SuppressWarnings("unchecked") static <T> Query<T> from(Db db, T alias) {
private <X> Class<X> getClass(X x) { Query<T> query = new Query<T>(db);
return (Class<X>) x.getClass(); TableDefinition def = db.define(alias.getClass());
query.from = new SelectTable(db, query, alias, false);
def.initSelectObject(query.from, alias, query.aliasMap);
return query;
} }
public List<T> select() { public List<T> select() {
List<T> result = Utils.newArrayList(); List<T> result = Utils.newArrayList();
ResultSet rs = db.executeQuery(toString()); ResultSet rs = db.executeQuery(getString());
Class<T> aliasClass = getClass(alias);
try { try {
while (rs.next()) { while (rs.next()) {
T item = Utils.newObject(aliasClass); T item = from.newObject();
aliasDef.readRow(item, rs); from.getAliasDefinition().readRow(item, rs);
result.add(item); result.add(item);
} }
} catch (SQLException e) { } catch (SQLException e) {
...@@ -69,14 +70,19 @@ public class Query<T> { ...@@ -69,14 +70,19 @@ public class Query<T> {
private <X> List<X> select(Class<X> clazz, X x) { private <X> List<X> select(Class<X> clazz, X x) {
List<X> result = Utils.newArrayList(); List<X> result = Utils.newArrayList();
TableDefinition<X> def = db.define(clazz); TableDefinition<X> def = db.define(clazz);
ResultSet rs = db.executeQuery(toString()); ResultSet rs = db.executeQuery(getString());
Class<T> aliasClass = getClass(alias);
try { try {
while (rs.next()) { while (rs.next()) {
T item = Utils.newObject(aliasClass); T item = from.newObject();
aliasDef.readRow(item, rs); from.getAliasDefinition().readRow(item, rs);
from.setCurrent(item);
for (SelectTable s: joins) {
Object item2 = s.newObject();
s.getAliasDefinition().readRow(item2, rs);
s.setCurrent(item2);
}
X item2 = Utils.newObject(clazz); X item2 = Utils.newObject(clazz);
def.copyAttributeValues(db, item, item2, x); def.copyAttributeValues(this, item2, x);
result.add(item2); result.add(item2);
} }
} catch (SQLException e) { } catch (SQLException e) {
...@@ -85,10 +91,10 @@ public class Query<T> { ...@@ -85,10 +91,10 @@ public class Query<T> {
return result; return result;
} }
public <X> List<X> selectSimple(X x) { private <X> List<X> selectSimple(X x) {
List<X> result = Utils.newArrayList(); List<X> result = Utils.newArrayList();
ResultSet rs = db.executeQuery(toString()); ResultSet rs = db.executeQuery(getString());
FieldDefinition<X> def = db.getFieldDefinition(x); FieldDefinition<X> def = aliasMap.get(x).getFieldDefinition();
try { try {
while (rs.next()) { while (rs.next()) {
X item; X item;
...@@ -108,7 +114,7 @@ public class Query<T> { ...@@ -108,7 +114,7 @@ public class Query<T> {
public <A> QueryCondition<T, A> where(A x) { public <A> QueryCondition<T, A> where(A x) {
return new QueryCondition<T, A>(this, x); return new QueryCondition<T, A>(this, x);
} }
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* Order by a number of columns. * Order by a number of columns.
...@@ -116,36 +122,39 @@ public class Query<T> { ...@@ -116,36 +122,39 @@ public class Query<T> {
* @param columns the columns * @param columns the columns
* @return the query * @return the query
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public Query<T> orderBy(Integer... columns) { public Query<T> orderBy(Integer... columns) {
return this; return this;
} }
String getString(Object x) { String getString(Object x) {
FieldDefinition def = db.getFieldDefinition(x); SelectColumn col = aliasMap.get(x);
if (def != null) { if (col != null) {
return def.columnName; return col.getString();
} }
return Utils.quoteSQL(x); return Utils.quoteSQL(x);
} }
public void addConditionToken(ConditionToken condition) { void addConditionToken(ConditionToken condition) {
conditions.add(condition); conditions.add(condition);
} }
public String toString() { String getString() {
StringBuffer buff = new StringBuffer("SELECT * FROM "); StringBuilder buff = new StringBuilder("SELECT * FROM ");
buff.append(aliasDef.tableName); buff.append(from.getString());
if (conditions.size() > 0) { for (SelectTable join : joins) {
buff.append(join.getStringAsJoin());
}
if (!conditions.isEmpty()) {
buff.append(" WHERE "); buff.append(" WHERE ");
for (ConditionToken token : conditions) { for (ConditionToken token : conditions) {
buff.append(token.toString()); buff.append(token.getString());
buff.append(' '); buff.append(' ');
} }
} }
return buff.toString(); return buff.toString();
} }
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* Join another table. * Join another table.
...@@ -153,10 +162,26 @@ public class Query<T> { ...@@ -153,10 +162,26 @@ public class Query<T> {
* @param u an alias for the table to join * @param u an alias for the table to join
* @return the joined query * @return the joined query
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public QueryJoin innerJoin(Object u) { public QueryJoin innerJoin(Object alias) {
return new QueryJoin(this); TableDefinition def = db.define(alias.getClass());
SelectTable join = new SelectTable(db, this, alias, false);
def.initSelectObject(join, alias, aliasMap);
joins.add(join);
return new QueryJoin(this, join);
}
Db getDb() {
return db;
}
boolean isJoin() {
return !joins.isEmpty();
}
SelectColumn getSelectColumn(Object obj) {
return aliasMap.get(obj);
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -12,13 +12,13 @@ package org.h2.jaqu; ...@@ -12,13 +12,13 @@ package org.h2.jaqu;
* @param <T> the return type of the query * @param <T> the return type of the query
* @param <A> the incomplete condition data type * @param <A> the incomplete condition data type
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class QueryCondition<T, A> { public class QueryCondition<T, A> {
private Query<T> query; private Query<T> query;
private A x; private A x;
public QueryCondition(Query<T> query, A x) { QueryCondition(Query<T> query, A x) {
this.query = query; this.query = query;
this.x = x; this.x = x;
} }
...@@ -28,15 +28,15 @@ public class QueryCondition<T, A> { ...@@ -28,15 +28,15 @@ public class QueryCondition<T, A> {
return new QueryWhere<T>(query); return new QueryWhere<T>(query);
} }
public QueryWhere<T> isBigger(A y) { public QueryWhere<T> bigger(A y) {
query.addConditionToken(new Condition<A>(query, x, y, CompareType.BIGGER)); query.addConditionToken(new Condition<A>(query, x, y, CompareType.BIGGER));
return new QueryWhere<T>(query); return new QueryWhere<T>(query);
} }
public QueryWhere<T> isSmaller(A y) { public QueryWhere<T> smaller(A y) {
query.addConditionToken(new Condition<A>(query, x, y, CompareType.SMALLER)); query.addConditionToken(new Condition<A>(query, x, y, CompareType.SMALLER));
return new QueryWhere<T>(query); return new QueryWhere<T>(query);
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -9,17 +9,19 @@ package org.h2.jaqu; ...@@ -9,17 +9,19 @@ package org.h2.jaqu;
/** /**
* This class represents a query with a join. * This class represents a query with a join.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class QueryJoin { public class QueryJoin {
Query< ? > query; private Query< ? > query;
private SelectTable join;
QueryJoin(Query< ? > query) { QueryJoin(Query< ? > query, SelectTable join) {
this.query = query; this.query = query;
this.join = join;
} }
public <A> QueryJoinCondition<A> on(A x) { public <A> QueryJoinCondition<A> on(A x) {
return new QueryJoinCondition<A>(query, x); return new QueryJoinCondition<A>(query, join, x);
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -11,20 +11,22 @@ package org.h2.jaqu; ...@@ -11,20 +11,22 @@ package org.h2.jaqu;
* *
* @param <A> the incomplete condition data type * @param <A> the incomplete condition data type
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class QueryJoinCondition<A> { public class QueryJoinCondition<A> {
private Query< ? > query; private Query< ? > query;
private SelectTable join;
private A x; private A x;
public QueryJoinCondition(Query< ? > query, A x) { QueryJoinCondition(Query< ? > query, SelectTable join, A x) {
this.query = query; this.query = query;
this.join = join;
this.x = x; this.x = x;
} }
public Query< ? > is(A y) { public Query< ? > is(A y) {
query.addConditionToken(new Condition<A>(query, x, y, CompareType.EQUAL)); join.addConditionToken(new Condition<A>(query, x, y, CompareType.EQUAL));
return query; return query;
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -6,16 +6,16 @@ ...@@ -6,16 +6,16 @@
*/ */
package org.h2.jaqu; package org.h2.jaqu;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.util.List; import java.util.List;
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* This class represents a query with a condition. * This class represents a query with a condition.
* *
* @param <T> the return type * @param <T> the return type
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class QueryWhere<T> { public class QueryWhere<T> {
Query<T> query; Query<T> query;
...@@ -41,7 +41,7 @@ public class QueryWhere<T> { ...@@ -41,7 +41,7 @@ public class QueryWhere<T> {
public List<T> select() { public List<T> select() {
return query.select(); return query.select();
} }
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* Order by a number of columns. * Order by a number of columns.
...@@ -49,10 +49,10 @@ public class QueryWhere<T> { ...@@ -49,10 +49,10 @@ public class QueryWhere<T> {
* @param columns the columns * @param columns the columns
* @return the query * @return the query
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public QueryWhere<T> orderBy(Integer... columns) { public QueryWhere<T> orderBy(Integer... columns) {
return this; return this;
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
package org.h2.jaqu; package org.h2.jaqu;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
...@@ -16,7 +16,7 @@ import java.util.IdentityHashMap; ...@@ -16,7 +16,7 @@ import java.util.IdentityHashMap;
import java.util.Map; import java.util.Map;
import org.h2.jaqu.util.Utils; import org.h2.jaqu.util.Utils;
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* A table definition contains the index definitions of a table, the field * A table definition contains the index definitions of a table, the field
...@@ -24,40 +24,43 @@ import org.h2.jaqu.util.Utils; ...@@ -24,40 +24,43 @@ import org.h2.jaqu.util.Utils;
* *
* @param <T> the table type * @param <T> the table type
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
class TableDefinition<T> { class TableDefinition<T> {
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* The meta data of an index. * The meta data of an index.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
static class IndexDefinition { static class IndexDefinition {
boolean unique; boolean unique;
String indexName; String indexName;
String[] columnNames; String[] columnNames;
} }
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* The meta data of a field. * The meta data of a field.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
static class FieldDefinition<X> { static class FieldDefinition<X> {
String columnName; String columnName;
Field field; Field field;
String dataType; String dataType;
public Object getValue(Object obj) {
Object getValue(Object obj) {
try { try {
return field.get(obj); return field.get(obj);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void initWithNewObject(Object obj) {
void initWithNewObject(Object obj) {
Object o = Utils.newObject(field.getType()); Object o = Utils.newObject(field.getType());
setValue(obj, o); setValue(obj, o);
} }
void setValue(Object obj, Object o) { void setValue(Object obj, Object o) {
try { try {
field.set(obj, o); field.set(obj, o);
...@@ -67,7 +70,7 @@ class TableDefinition<T> { ...@@ -67,7 +70,7 @@ class TableDefinition<T> {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public X read(ResultSet rs) { X read(ResultSet rs) {
try { try {
return (X) rs.getObject(columnName); return (X) rs.getObject(columnName);
} catch (Exception e) { } catch (Exception e) {
...@@ -93,7 +96,7 @@ class TableDefinition<T> { ...@@ -93,7 +96,7 @@ class TableDefinition<T> {
this.tableName = tableName; this.tableName = tableName;
} }
public void setPrimaryKey(Object[] primaryKeyColumns) { void setPrimaryKey(Object[] primaryKeyColumns) {
this.primaryKeyColumnNames = mapColumnNames(primaryKeyColumns); this.primaryKeyColumnNames = mapColumnNames(primaryKeyColumns);
} }
...@@ -111,19 +114,14 @@ class TableDefinition<T> { ...@@ -111,19 +114,14 @@ class TableDefinition<T> {
return columnNames; return columnNames;
} }
public void addIndex(Object[] columns) { void addIndex(Object[] columns) {
IndexDefinition index = new IndexDefinition(); IndexDefinition index = new IndexDefinition();
index.indexName = tableName + "_" + indexes.size(); index.indexName = tableName + "_" + indexes.size();
index.columnNames = mapColumnNames(columns); index.columnNames = mapColumnNames(columns);
indexes.add(index); indexes.add(index);
} }
public void apply() { void mapFields() {
// TODO Auto-generated method stub
}
public void mapFields() {
Field[] classFields = clazz.getFields(); Field[] classFields = clazz.getFields();
for (Field f : classFields) { for (Field f : classFields) {
FieldDefinition fieldDef = new FieldDefinition(); FieldDefinition fieldDef = new FieldDefinition();
...@@ -142,6 +140,16 @@ class TableDefinition<T> { ...@@ -142,6 +140,16 @@ class TableDefinition<T> {
return "VARCHAR"; return "VARCHAR";
} else if (clazz == Double.class) { } else if (clazz == Double.class) {
return "DOUBLE"; return "DOUBLE";
} else if (clazz == java.math.BigDecimal.class) {
return "DECIMAL";
} else if (clazz == java.util.Date.class) {
return "DATE";
} else if (clazz == java.sql.Date.class) {
return "DATE";
} else if (clazz == java.sql.Time.class) {
return "TIME";
} else if (clazz == java.sql.Timestamp.class) {
return "TIMESTAMP";
} }
return "VARCHAR"; return "VARCHAR";
// TODO add more data types // TODO add more data types
...@@ -180,7 +188,7 @@ class TableDefinition<T> { ...@@ -180,7 +188,7 @@ class TableDefinition<T> {
} }
} }
public TableDefinition createTableIfRequired(Db db) { TableDefinition createTableIfRequired(Db db) {
StringBuilder buff = new StringBuilder("CREATE TABLE IF NOT EXISTS "); StringBuilder buff = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
buff.append(tableName); buff.append(tableName);
buff.append('('); buff.append('(');
...@@ -209,7 +217,7 @@ class TableDefinition<T> { ...@@ -209,7 +217,7 @@ class TableDefinition<T> {
return this; return this;
} }
public void mapObject(Object obj) { void mapObject(Object obj) {
fieldMap.clear(); fieldMap.clear();
initObject(obj, fieldMap); initObject(obj, fieldMap);
} }
...@@ -221,6 +229,14 @@ class TableDefinition<T> { ...@@ -221,6 +229,14 @@ class TableDefinition<T> {
} }
} }
void initSelectObject(SelectTable table, Object obj, Map<Object, SelectColumn> map) {
for (FieldDefinition def : fields) {
def.initWithNewObject(obj);
SelectColumn column = new SelectColumn(table, def);
map.put(def.getValue(obj), column);
}
}
void readRow(Object item, ResultSet rs) { void readRow(Object item, ResultSet rs) {
for (FieldDefinition def : fields) { for (FieldDefinition def : fields) {
Object o = def.read(rs); Object o = def.read(rs);
...@@ -228,14 +244,14 @@ class TableDefinition<T> { ...@@ -228,14 +244,14 @@ class TableDefinition<T> {
} }
} }
<U, X> void copyAttributeValues(Db db, U from, X to, X map) { <U, X> void copyAttributeValues(Query query, X to, X map) {
for (FieldDefinition def : fields) { for (FieldDefinition def : fields) {
Object obj = def.getValue(map); Object obj = def.getValue(map);
FieldDefinition fd = db.getFieldDefinition(obj); SelectColumn col = query.getSelectColumn(obj);
Object value = fd.getValue(from); Object value = col.getCurrentValue();
def.setValue(to, value); def.setValue(to, value);
} }
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -17,7 +17,14 @@ public class ClassUtils { ...@@ -17,7 +17,14 @@ public class ClassUtils {
// utility class // utility class
} }
//## Java 1.6 begin ## //## Java 1.5 begin ##
@SuppressWarnings("unchecked")
public
static <X> Class<X> getClass(X x) {
return (Class<X>) x.getClass();
}
public static Class< ? > loadClass(String className) { public static Class< ? > loadClass(String className) {
try { try {
return Class.forName(className); return Class.forName(className);
...@@ -25,5 +32,5 @@ public class ClassUtils { ...@@ -25,5 +32,5 @@ public class ClassUtils {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
} }
...@@ -6,11 +6,9 @@ ...@@ -6,11 +6,9 @@
*/ */
package org.h2.jaqu.util; package org.h2.jaqu.util;
//## Java 1.6 begin ##
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException; import java.sql.SQLException;
//## Java 1.6 end ##
/** /**
* Messages used in the database engine. * Messages used in the database engine.
...@@ -30,22 +28,26 @@ public class Message { ...@@ -30,22 +28,26 @@ public class Message {
* @param e the root cause * @param e the root cause
* @return the SQL exception object * @return the SQL exception object
*/ */
//## Java 1.6 begin ##
public static SQLException convert(Throwable e) { public static SQLException convert(Throwable e) {
if (e instanceof SQLException) { if (e instanceof SQLException) {
return (SQLException) e; return (SQLException) e;
} else if (e instanceof InvocationTargetException) { }
String message;
if (e instanceof InvocationTargetException) {
InvocationTargetException te = (InvocationTargetException) e; InvocationTargetException te = (InvocationTargetException) e;
Throwable t = te.getTargetException(); Throwable t = te.getTargetException();
if (t instanceof SQLException) { if (t instanceof SQLException) {
return (SQLException) t; return (SQLException) t;
} }
return new SQLException("Invocation exception: " + t.toString(), e); message = "Invocation exception";
} else if (e instanceof IOException) { } else if (e instanceof IOException) {
return new SQLException("IO exception: " + e.toString(), e); message = "IO exception";
} else {
message = "General exception";
} }
return new SQLException("General exception: " + e.toString(), e); SQLException e2 = new SQLException(message + ": " + e.toString());
e2.initCause(e);
return e2;
} }
//## Java 1.6 end ##
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
package org.h2.jaqu.util; package org.h2.jaqu.util;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
...@@ -18,12 +18,12 @@ import java.util.List; ...@@ -18,12 +18,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* Generic utility methods. * Generic utility methods.
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class Utils { public class Utils {
private static volatile long counter; private static volatile long counter;
...@@ -129,4 +129,4 @@ public class Utils { ...@@ -129,4 +129,4 @@ public class Utils {
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
*/ */
package org.h2.jaqu.util; package org.h2.jaqu.util;
//## Java 1.6 begin ## //## Java 1.5 begin ##
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
//## Java 1.6 end ## //## Java 1.5 end ##
/** /**
* This hash map uses weak references, so that elements that are no longer * This hash map uses weak references, so that elements that are no longer
...@@ -21,7 +21,7 @@ import java.util.Set; ...@@ -21,7 +21,7 @@ import java.util.Set;
* @param <K> the keys * @param <K> the keys
* @param <V> the value * @param <V> the value
*/ */
//## Java 1.6 begin ## //## Java 1.5 begin ##
public class WeakIdentityHashMap<K, V> implements Map<K, V> { public class WeakIdentityHashMap<K, V> implements Map<K, V> {
private static final int MAX_LOAD = 90; private static final int MAX_LOAD = 90;
private static final WeakReference DELETED_KEY = new WeakReference(null); private static final WeakReference DELETED_KEY = new WeakReference(null);
...@@ -224,4 +224,4 @@ public class WeakIdentityHashMap<K, V> implements Map<K, V> { ...@@ -224,4 +224,4 @@ public class WeakIdentityHashMap<K, V> implements Map<K, V> {
} }
} }
//## Java 1.6 end ## //## Java 1.5 end ##
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论