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

JaQu improvements

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