提交 6216db17 authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup:

- Fix missing or incorrectly formatted license headers
- Line comments are lowercase except for complete sentences
- Remove trailing whitespace (the IDE should fix this)
上级 8a7312b8
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: James Moger * Initial Developer: James Moger
*/ */
package org.h2.test.jaqu; package org.h2.test.jaqu;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.jaqu.Db; import org.h2.jaqu.Db;
import org.h2.test.TestBase; import org.h2.test.TestBase;
/** /**
* Test annotation processing. * Test annotation processing.
*/ */
public class AnnotationsTest extends TestBase { public class AnnotationsTest extends TestBase {
/** /**
* This object represents a database (actually a connection to the database). * This object represents a database (actually a connection to the database).
*/ */
//## Java 1.5 begin ## //## Java 1.5 begin ##
private Db db; private Db db;
//## Java 1.5 end ## //## Java 1.5 end ##
/** /**
* This method is called when executing this application from the command * This method is called when executing this application from the command
* line. * line.
* *
* @param args the command line parameters * @param args the command line parameters
*/ */
public static void main(String... args) throws SQLException { public static void main(String... args) throws SQLException {
new AnnotationsTest().test(); new AnnotationsTest().test();
} }
public void test() throws SQLException { public void test() throws SQLException {
//## Java 1.5 begin ## //## Java 1.5 begin ##
db = Db.open("jdbc:h2:mem:", "sa", "sa"); db = Db.open("jdbc:h2:mem:", "sa", "sa");
db.insertAll(Product.getList()); db.insertAll(Product.getList());
db.insertAll(ProductAnnotationOnly.getList()); db.insertAll(ProductAnnotationOnly.getList());
db.insertAll(ProductMixedAnnotation.getList()); db.insertAll(ProductMixedAnnotation.getList());
testIndexCreation(); testIndexCreation();
testProductAnnotationOnly(); testProductAnnotationOnly();
testProductMixedAnnotation(); testProductMixedAnnotation();
testTrimStringAnnotation(); testTrimStringAnnotation();
testCreateTableIfRequiredAnnotation(); testCreateTableIfRequiredAnnotation();
testColumnInheritanceAnnotation(); testColumnInheritanceAnnotation();
db.close(); db.close();
//## Java 1.5 end ## //## Java 1.5 end ##
} }
private void testIndexCreation() throws SQLException { private void testIndexCreation() throws SQLException {
// test indexes are created, and columns are in the right order // test indexes are created, and columns are in the right order
DatabaseMetaData meta = db.getConnection().getMetaData(); DatabaseMetaData meta = db.getConnection().getMetaData();
ResultSet rs = meta.getIndexInfo(null, "PUBLIC", "ANNOTATEDPRODUCT", false, true); ResultSet rs = meta.getIndexInfo(null, "PUBLIC", "ANNOTATED" + "PRODUCT", false, true);
assertTrue(rs.next()); assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "PRIMARY_KEY"); assertStartsWith(rs.getString("INDEX_NAME"), "PRIMARY_KEY");
assertTrue(rs.next()); assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATEDPRODUCT_"); assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATED" + "PRODUCT_");
assertStartsWith(rs.getString("COLUMN_NAME"), "NAME"); assertStartsWith(rs.getString("COLUMN_NAME"), "NAME");
assertTrue(rs.next()); assertTrue(rs.next());
assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATEDPRODUCT_"); assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATED" + "PRODUCT_");
assertStartsWith(rs.getString("COLUMN_NAME"), "CAT"); assertStartsWith(rs.getString("COLUMN_NAME"), "CAT");
assertFalse(rs.next()); assertFalse(rs.next());
} }
private void testProductAnnotationOnly() { private void testProductAnnotationOnly() {
ProductAnnotationOnly p = new ProductAnnotationOnly(); ProductAnnotationOnly p = new ProductAnnotationOnly();
assertEquals(10, db.from(p).selectCount()); assertEquals(10, db.from(p).selectCount());
// test JQColumn.name="cat" // test JQColumn.name="cat"
assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount()); assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
// test JQTable.annotationsOnly=true // test JQTable.annotationsOnly=true
// public String unmappedField is ignored by JaQu // public String unmappedField is ignored by JaQu
assertEquals(0, db.from(p).where(p.unmappedField).is("unmapped").selectCount()); assertEquals(0, db.from(p).where(p.unmappedField).is("unmapped").selectCount());
// test JQColumn.autoIncrement=true // test JQColumn.autoIncrement=true
// 10 objects, 10 autoIncremented unique values // 10 objects, 10 autoIncremented unique values
assertEquals(10, db.from(p).selectDistinct(p.autoIncrement).size()); assertEquals(10, db.from(p).selectDistinct(p.autoIncrement).size());
// test JQTable.primaryKey=id // test JQTable.primaryKey=id
try { try {
db.insertAll(ProductAnnotationOnly.getList()); db.insertAll(ProductAnnotationOnly.getList());
} catch (RuntimeException r) { } catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause(); SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode()); assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
} }
} }
private void testProductMixedAnnotation() { private void testProductMixedAnnotation() {
ProductMixedAnnotation p = new ProductMixedAnnotation(); ProductMixedAnnotation p = new ProductMixedAnnotation();
// test JQColumn.name="cat" // test JQColumn.name="cat"
assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount()); assertEquals(2, db.from(p).where(p.category).is("Beverages").selectCount());
// test JQTable.annotationsOnly=false // test JQTable.annotationsOnly=false
// public String mappedField is reflectively mapped by JaQu // public String mappedField is reflectively mapped by JaQu
assertEquals(10, db.from(p).where(p.mappedField).is("mapped").selectCount()); assertEquals(10, db.from(p).where(p.mappedField).is("mapped").selectCount());
// test JQColumn.primaryKey=true // test JQColumn.primaryKey=true
try { try {
db.insertAll(ProductMixedAnnotation.getList()); db.insertAll(ProductMixedAnnotation.getList());
} catch (RuntimeException r) { } catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause(); SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode()); assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode());
} }
} }
private void testTrimStringAnnotation() { private void testTrimStringAnnotation() {
ProductAnnotationOnly p = new ProductAnnotationOnly(); ProductAnnotationOnly p = new ProductAnnotationOnly();
ProductAnnotationOnly prod = db.from(p).selectFirst(); ProductAnnotationOnly prod = db.from(p).selectFirst();
String oldValue = prod.category; String oldValue = prod.category;
String newValue = "01234567890123456789"; String newValue = "01234567890123456789";
// 2 chars exceeds field max // 2 chars exceeds field max
prod.category = newValue; prod.category = newValue;
db.update(prod); db.update(prod);
ProductAnnotationOnly newProd = db.from(p) ProductAnnotationOnly newProd = db.from(p)
.where(p.productId) .where(p.productId)
.is(prod.productId) .is(prod.productId)
.selectFirst(); .selectFirst();
assertEquals(newValue.substring(0, 15), newProd.category); assertEquals(newValue.substring(0, 15), newProd.category);
newProd.category = oldValue; newProd.category = oldValue;
db.update(newProd); db.update(newProd);
} }
private void testColumnInheritanceAnnotation() { private void testColumnInheritanceAnnotation() {
ProductInheritedAnnotation table = new ProductInheritedAnnotation(); ProductInheritedAnnotation table = new ProductInheritedAnnotation();
Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
List<ProductInheritedAnnotation> inserted = ProductInheritedAnnotation.getData(); List<ProductInheritedAnnotation> inserted = ProductInheritedAnnotation.getData();
db.insertAll(inserted); db.insertAll(inserted);
List<ProductInheritedAnnotation> retrieved = db.from(table).select(); List<ProductInheritedAnnotation> retrieved = db.from(table).select();
for (int j = 0; j < retrieved.size(); j++) { for (int j = 0; j < retrieved.size(); j++) {
ProductInheritedAnnotation i = inserted.get(j); ProductInheritedAnnotation i = inserted.get(j);
ProductInheritedAnnotation r = retrieved.get(j); ProductInheritedAnnotation r = retrieved.get(j);
assertEquals(i.category, r.category); assertEquals(i.category, r.category);
assertEquals(i.mappedField, r.mappedField); assertEquals(i.mappedField, r.mappedField);
assertEquals(i.unitsInStock, r.unitsInStock); assertEquals(i.unitsInStock, r.unitsInStock);
assertEquals(i.unitPrice, r.unitPrice); assertEquals(i.unitPrice, r.unitPrice);
assertEquals(i.name(), r.name()); assertEquals(i.name(), r.name());
assertEquals(i.id(), r.id()); assertEquals(i.id(), r.id());
} }
db.close(); db.close();
} }
private void testCreateTableIfRequiredAnnotation() { private void testCreateTableIfRequiredAnnotation() {
// tests JQTable.createTableIfRequired=false // tests JQTable.createTableIfRequired=false
try { try {
Db noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa"); Db noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa");
noCreateDb.insertAll(ProductNoCreateTable.getList()); noCreateDb.insertAll(ProductNoCreateTable.getList());
noCreateDb.close(); noCreateDb.close();
} catch (RuntimeException r) { } catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause(); SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode()); assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode());
} }
} }
} }
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). * (http://h2database.com/html/license.html).
* Initial Developer: James Moger * Initial Developer: James Moger
*/ */
package org.h2.test.jaqu; package org.h2.test.jaqu;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.h2.jaqu.Db; import org.h2.jaqu.Db;
import org.h2.jaqu.DbInspector; import org.h2.jaqu.DbInspector;
import org.h2.jaqu.DbUpgrader; import org.h2.jaqu.DbUpgrader;
import org.h2.jaqu.DbVersion; import org.h2.jaqu.DbVersion;
import org.h2.jaqu.Table.JQDatabase; import org.h2.jaqu.Table.JQDatabase;
import org.h2.jaqu.Validation; import org.h2.jaqu.Validation;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.jaqu.SupportedTypes.SupportedTypes2; import org.h2.test.jaqu.SupportedTypes.SupportedTypes2;
/** /**
* Test that the mapping between classes and tables is done correctly. * Test that the mapping between classes and tables is done correctly.
*/ */
public class ModelsTest extends TestBase { public class ModelsTest extends TestBase {
/** /**
* This object represents a database (actually a connection to the database). * This object represents a database (actually a connection to the database).
*/ */
//## Java 1.5 begin ## //## Java 1.5 begin ##
private Db db; private Db db;
//## Java 1.5 end ## //## Java 1.5 end ##
/** /**
* This method is called when executing this application from the command * This method is called when executing this application from the command
* line. * line.
* *
* @param args the command line parameters * @param args the command line parameters
*/ */
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
ModelsTest test = new ModelsTest(); ModelsTest test = new ModelsTest();
test.init(); test.init();
test.config.traceTest = true; test.config.traceTest = true;
test.test(); test.test();
} }
public void test() { public void test() {
//## Java 1.5 begin ## //## Java 1.5 begin ##
db = Db.open("jdbc:h2:mem:", "sa", "sa"); db = Db.open("jdbc:h2:mem:", "sa", "sa");
db.insertAll(Product.getList()); db.insertAll(Product.getList());
db.insertAll(ProductAnnotationOnly.getList()); db.insertAll(ProductAnnotationOnly.getList());
db.insertAll(ProductMixedAnnotation.getList()); db.insertAll(ProductMixedAnnotation.getList());
testValidateModels(); testValidateModels();
testSupportedTypes(); testSupportedTypes();
testModelGeneration(); testModelGeneration();
testDatabaseUpgrade(); testDatabaseUpgrade();
testTableUpgrade(); testTableUpgrade();
db.close(); db.close();
//## Java 1.5 end ## //## Java 1.5 end ##
} }
private void testValidateModels() { private void testValidateModels() {
DbInspector inspector = new DbInspector(db); DbInspector inspector = new DbInspector(db);
validateModel(inspector, new Product()); validateModel(inspector, new Product());
validateModel(inspector, new ProductAnnotationOnly()); validateModel(inspector, new ProductAnnotationOnly());
validateModel(inspector, new ProductMixedAnnotation()); validateModel(inspector, new ProductMixedAnnotation());
} }
private void validateModel(DbInspector inspector, Object o) { private void validateModel(DbInspector inspector, Object o) {
List<Validation> remarks = inspector.validateModel(o, false); List<Validation> remarks = inspector.validateModel(o, false);
if (config.traceTest && remarks.size() > 0) { if (config.traceTest && remarks.size() > 0) {
trace("Validation remarks for " + o.getClass().getName()); trace("Validation remarks for " + o.getClass().getName());
for (Validation remark : remarks) { for (Validation remark : remarks) {
trace(remark.toString()); trace(remark.toString());
} }
trace(""); trace("");
} }
for (Validation remark : remarks) { for (Validation remark : remarks) {
assertFalse(remark.toString(), remark.isError()); assertFalse(remark.toString(), remark.isError());
} }
} }
private void testSupportedTypes() { private void testSupportedTypes() {
List<SupportedTypes> original = SupportedTypes.createList(); List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original); db.insertAll(original);
List<SupportedTypes> retrieved = db.from(SupportedTypes.SAMPLE).select(); List<SupportedTypes> retrieved = db.from(SupportedTypes.SAMPLE).select();
assertEquals(original.size(), retrieved.size()); assertEquals(original.size(), retrieved.size());
for (int i = 0; i < original.size(); i++) { for (int i = 0; i < original.size(); i++) {
SupportedTypes o = original.get(i); SupportedTypes o = original.get(i);
SupportedTypes r = retrieved.get(i); SupportedTypes r = retrieved.get(i);
assertTrue(o.equivalentTo(r)); assertTrue(o.equivalentTo(r));
} }
} }
private void testModelGeneration() { private void testModelGeneration() {
DbInspector inspector = new DbInspector(db); DbInspector inspector = new DbInspector(db);
List<String> models = inspector.generateModel(null, List<String> models = inspector.generateModel(null,
"SupportedTypes", "SupportedTypes",
"org.h2.test.jaqu", true, true); "org.h2.test.jaqu", true, true);
assertEquals(1, models.size()); assertEquals(1, models.size());
// a poor test, but a start // a poor test, but a start
assertEquals(1364, models.get(0).length()); assertEquals(1364, models.get(0).length());
} }
private void testDatabaseUpgrade() { private void testDatabaseUpgrade() {
Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
// Insert a Database version record // insert a database version record
db.insert(new DbVersion(1)); db.insert(new DbVersion(1));
TestDbUpgrader dbUpgrader = new TestDbUpgrader(); TestDbUpgrader dbUpgrader = new TestDbUpgrader();
db.setDbUpgrader(dbUpgrader); db.setDbUpgrader(dbUpgrader);
List<SupportedTypes> original = SupportedTypes.createList(); List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original); db.insertAll(original);
assertEquals(1, dbUpgrader.oldVersion.get()); assertEquals(1, dbUpgrader.oldVersion.get());
assertEquals(2, dbUpgrader.newVersion.get()); assertEquals(2, dbUpgrader.newVersion.get());
db.close(); db.close();
} }
private void testTableUpgrade() { private void testTableUpgrade() {
Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
// Insert first, this will create version record automatically // insert first, this will create version record automatically
List<SupportedTypes> original = SupportedTypes.createList(); List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original); db.insertAll(original);
// Reset the dbUpgrader (clears updatecheck cache) // reset the dbUpgrader (clears the update check cache)
TestDbUpgrader dbUpgrader = new TestDbUpgrader(); TestDbUpgrader dbUpgrader = new TestDbUpgrader();
db.setDbUpgrader(dbUpgrader); db.setDbUpgrader(dbUpgrader);
SupportedTypes2 s2 = new SupportedTypes2(); SupportedTypes2 s2 = new SupportedTypes2();
List<SupportedTypes2> types = db.from(s2).select(); List<SupportedTypes2> types = db.from(s2).select();
assertEquals(10, types.size()); assertEquals(10, types.size());
assertEquals(1, dbUpgrader.oldVersion.get()); assertEquals(1, dbUpgrader.oldVersion.get());
assertEquals(2, dbUpgrader.newVersion.get()); assertEquals(2, dbUpgrader.newVersion.get());
db.close(); db.close();
} }
/** /**
* A sample database upgrader class. * A sample database upgrader class.
*/ */
@JQDatabase(version = 2) @JQDatabase(version = 2)
class TestDbUpgrader implements DbUpgrader { class TestDbUpgrader implements DbUpgrader {
final AtomicInteger oldVersion = new AtomicInteger(0); final AtomicInteger oldVersion = new AtomicInteger(0);
final AtomicInteger newVersion = new AtomicInteger(0); final AtomicInteger newVersion = new AtomicInteger(0);
public boolean upgradeTable(Db db, String schema, String table, public boolean upgradeTable(Db db, String schema, String table,
int fromVersion, int toVersion) { int fromVersion, int toVersion) {
// just claims success on upgrade request // just claims success on upgrade request
oldVersion.set(fromVersion); oldVersion.set(fromVersion);
newVersion.set(toVersion); newVersion.set(toVersion);
return true; return true;
} }
public boolean upgradeDatabase(Db db, int fromVersion, int toVersion) { public boolean upgradeDatabase(Db db, int fromVersion, int toVersion) {
// just claims success on upgrade request // just claims success on upgrade request
oldVersion.set(fromVersion); oldVersion.set(fromVersion);
newVersion.set(toVersion); newVersion.set(toVersion);
return true; return true;
} }
} }
} }
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group * (http://h2database.com/html/license.html).
*/ * Initial Developer: James Moger
package org.h2.test.jaqu; */
package org.h2.test.jaqu;
// ## Java 1.5 begin ##
import java.util.Arrays; //## Java 1.5 begin ##
import java.util.List; import java.util.Arrays;
import org.h2.jaqu.Table.JQColumn; import java.util.List;
import org.h2.jaqu.Table.JQIndex; import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable; import org.h2.jaqu.Table.JQIndex;
import org.h2.jaqu.Table.JQTable;
/**
* A table containing product data. /**
*/ * A table containing product data.
// ## Java 1.5 begin ## */
@JQTable(name = "AnnotatedProduct", primaryKey = "id") //## Java 1.5 begin ##
@JQIndex(standard = "name, cat") @JQTable(name = "AnnotatedProduct", primaryKey = "id")
public class ProductAnnotationOnly { @JQIndex(standard = "name, cat")
public class ProductAnnotationOnly {
@JQColumn(autoIncrement = true)
public Integer autoIncrement; @JQColumn(autoIncrement = true)
public Integer autoIncrement;
public String unmappedField;
public String unmappedField;
@JQColumn(name = "id")
Integer productId; @JQColumn(name = "id")
Integer productId;
@JQColumn(name = "cat", maxLength = 15, trimString = true)
String category; @JQColumn(name = "cat", maxLength = 15, trimString = true)
String category;
@JQColumn(name = "name")
private String productName; @JQColumn(name = "name")
private String productName;
@SuppressWarnings("unused")
@JQColumn @SuppressWarnings("unused")
private Double unitPrice; @JQColumn
private Double unitPrice;
@JQColumn
private Integer unitsInStock; @JQColumn
private Integer unitsInStock;
public ProductAnnotationOnly() {
// public constructor public ProductAnnotationOnly() {
} // public constructor
}
private ProductAnnotationOnly(int productId, String productName, String category, double unitPrice,
int unitsInStock, String unmappedField) { private ProductAnnotationOnly(int productId, String productName, String category, double unitPrice,
this.productId = productId; int unitsInStock, String unmappedField) {
this.productName = productName; this.productId = productId;
this.category = category; this.productName = productName;
this.unitPrice = unitPrice; this.category = category;
this.unitsInStock = unitsInStock; this.unitPrice = unitPrice;
this.unmappedField = unmappedField; this.unitsInStock = unitsInStock;
} this.unmappedField = unmappedField;
}
private static ProductAnnotationOnly create(int productId, String productName, String category, double unitPrice,
int unitsInStock, String unmappedField) { private static ProductAnnotationOnly create(int productId, String productName, String category, double unitPrice,
return new ProductAnnotationOnly(productId, productName, category, unitPrice, unitsInStock, unmappedField); int unitsInStock, String unmappedField) {
} return new ProductAnnotationOnly(productId, productName, category, unitPrice, unitsInStock, unmappedField);
}
public static List<ProductAnnotationOnly> getList() {
String unmappedField = "unmapped"; public static List<ProductAnnotationOnly> getList() {
ProductAnnotationOnly[] list = { create(1, "Chai", "Beverages", 18, 39, unmappedField), String unmappedField = "unmapped";
create(2, "Chang", "Beverages", 19.0, 17, unmappedField), ProductAnnotationOnly[] list = { create(1, "Chai", "Beverages", 18, 39, unmappedField),
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, unmappedField), create(2, "Chang", "Beverages", 19.0, 17, unmappedField),
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, unmappedField), create(3, "Aniseed Syrup", "Condiments", 10.0, 13, unmappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, unmappedField), create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, unmappedField),
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, unmappedField), create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, unmappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, unmappedField), create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, unmappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, unmappedField), create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, unmappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, unmappedField), create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, unmappedField),
create(10, "Ikura", "Seafood", 31.0, 31, unmappedField), }; create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, unmappedField),
return Arrays.asList(list); create(10, "Ikura", "Seafood", 31.0, 31, unmappedField), };
} return Arrays.asList(list);
}
public String toString() {
return productName + ": " + unitsInStock; public String toString() {
} return productName + ": " + unitsInStock;
}
}
// ## Java 1.5 end ## }
//## Java 1.5 end ##
package org.h2.test.jaqu; /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
import java.util.Arrays; * Version 1.0, and under the Eclipse Public License, Version 1.0
import java.util.List; * (http://h2database.com/html/license.html).
import org.h2.jaqu.Table.JQTable; * Initial Developer: James Moger
*/
/** package org.h2.test.jaqu;
* This class inherits all its fields from a parent class which has annotated
* columns. The JQTable annotation of the parent class is ignored and only import java.util.Arrays;
* the JQTable annotation of this class matters. import java.util.List;
* However, this table inherits JQColumns from its super class. import org.h2.jaqu.Table.JQTable;
*/
@JQTable(inheritColumns = true, annotationsOnly = false) /**
public class ProductInheritedAnnotation extends ProductMixedAnnotation { * This class inherits all its fields from a parent class which has annotated
* columns. The JQTable annotation of the parent class is ignored and only
public ProductInheritedAnnotation() { * the JQTable annotation of this class matters.
// public constructor * However, this table inherits JQColumns from its super class.
} */
@JQTable(inheritColumns = true, annotationsOnly = false)
private ProductInheritedAnnotation(int productId, String productName, String category, double unitPrice, public class ProductInheritedAnnotation extends ProductMixedAnnotation {
int unitsInStock, String mappedField) {
super(productId, productName, category, unitPrice, unitsInStock, mappedField); public ProductInheritedAnnotation() {
} // public constructor
}
private static ProductInheritedAnnotation create(int productId, String productName, String category,
double unitPrice, int unitsInStock, String mappedField) { private ProductInheritedAnnotation(int productId, String productName, String category, double unitPrice,
return new ProductInheritedAnnotation(productId, productName, category, unitPrice, unitsInStock, mappedField); int unitsInStock, String mappedField) {
} super(productId, productName, category, unitPrice, unitsInStock, mappedField);
}
public static List<ProductInheritedAnnotation> getData() {
String mappedField = "mapped"; private static ProductInheritedAnnotation create(int productId, String productName, String category,
ProductInheritedAnnotation[] list = { create(1, "Chai", "Beverages", 18, 39, mappedField), double unitPrice, int unitsInStock, String mappedField) {
create(2, "Chang", "Beverages", 19.0, 17, mappedField), return new ProductInheritedAnnotation(productId, productName, category, unitPrice, unitsInStock, mappedField);
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, mappedField), }
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, mappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, mappedField), public static List<ProductInheritedAnnotation> getData() {
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, mappedField), String mappedField = "mapped";
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, mappedField), ProductInheritedAnnotation[] list = { create(1, "Chai", "Beverages", 18, 39, mappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField), create(2, "Chang", "Beverages", 19.0, 17, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField), create(3, "Aniseed Syrup", "Condiments", 10.0, 13, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), }; create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, mappedField),
return Arrays.asList(list); create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, mappedField),
} create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, mappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, mappedField),
} create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), };
return Arrays.asList(list);
}
}
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group * (http://h2database.com/html/license.html).
*/ * Initial Developer: James Moger
package org.h2.test.jaqu; */
package org.h2.test.jaqu;
// ## Java 1.5 begin ##
import java.util.Arrays; //## Java 1.5 begin ##
import java.util.List; import java.util.Arrays;
import org.h2.jaqu.Table.JQColumn; import java.util.List;
import org.h2.jaqu.Table.JQIndex; import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable; import org.h2.jaqu.Table.JQIndex;
import org.h2.jaqu.Table.JQTable;
/**
* A table containing product data. /**
*/ * A table containing product data.
// ## Java 1.5 begin ## */
@JQTable(annotationsOnly = false) //## Java 1.5 begin ##
@JQIndex(standard = "name, cat") @JQTable(annotationsOnly = false)
public class ProductMixedAnnotation { @JQIndex(standard = "name, cat")
public class ProductMixedAnnotation {
public Double unitPrice;
public Integer unitsInStock; public Double unitPrice;
public String mappedField; public Integer unitsInStock;
public String mappedField;
@JQColumn(name = "cat", maxLength = 255)
String category; @JQColumn(name = "cat", maxLength = 255)
String category;
@JQColumn(name = "id", primaryKey = true)
private Integer productId; @JQColumn(name = "id", primaryKey = true)
private Integer productId;
@JQColumn(name = "name")
private String productName; @JQColumn(name = "name")
private String productName;
public ProductMixedAnnotation() {
// public constructor public ProductMixedAnnotation() {
} // public constructor
}
protected ProductMixedAnnotation(int productId, String productName, String category, double unitPrice,
int unitsInStock, String mappedField) { protected ProductMixedAnnotation(int productId, String productName, String category, double unitPrice,
this.productId = productId; int unitsInStock, String mappedField) {
this.productName = productName; this.productId = productId;
this.category = category; this.productName = productName;
this.unitPrice = unitPrice; this.category = category;
this.unitsInStock = unitsInStock; this.unitPrice = unitPrice;
this.mappedField = mappedField; this.unitsInStock = unitsInStock;
} this.mappedField = mappedField;
}
private static ProductMixedAnnotation create(int productId, String productName, String category, double unitPrice,
int unitsInStock, String mappedField) { private static ProductMixedAnnotation create(int productId, String productName, String category, double unitPrice,
return new ProductMixedAnnotation(productId, productName, category, unitPrice, unitsInStock, mappedField); int unitsInStock, String mappedField) {
} return new ProductMixedAnnotation(productId, productName, category, unitPrice, unitsInStock, mappedField);
}
public static List<ProductMixedAnnotation> getList() {
String mappedField = "mapped"; public static List<ProductMixedAnnotation> getList() {
ProductMixedAnnotation[] list = { create(1, "Chai", "Beverages", 18, 39, mappedField), String mappedField = "mapped";
create(2, "Chang", "Beverages", 19.0, 17, mappedField), ProductMixedAnnotation[] list = { create(1, "Chai", "Beverages", 18, 39, mappedField),
create(3, "Aniseed Syrup", "Condiments", 10.0, 13, mappedField), create(2, "Chang", "Beverages", 19.0, 17, mappedField),
create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, mappedField), create(3, "Aniseed Syrup", "Condiments", 10.0, 13, mappedField),
create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, mappedField), create(4, "Chef Anton's Cajun Seasoning", "Condiments", 22.0, 53, mappedField),
create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, mappedField), create(5, "Chef Anton's Gumbo Mix", "Condiments", 21.3500, 0, mappedField),
create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, mappedField), create(6, "Grandma's Boysenberry Spread", "Condiments", 25.0, 120, mappedField),
create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField), create(7, "Uncle Bob's Organic Dried Pears", "Produce", 30.0, 15, mappedField),
create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField), create(8, "Northwoods Cranberry Sauce", "Condiments", 40.0, 6, mappedField),
create(10, "Ikura", "Seafood", 31.0, 31, mappedField), }; create(9, "Mishi Kobe Niku", "Meat/Poultry", 97.0, 29, mappedField),
return Arrays.asList(list); create(10, "Ikura", "Seafood", 31.0, 31, mappedField), };
} return Arrays.asList(list);
}
public String toString() {
return productName + ": " + unitsInStock; public String toString() {
} return productName + ": " + unitsInStock;
}
public int id() {
return productId; public int id() {
} return productId;
}
public String name() {
return productName; public String name() {
} return productName;
}
}
// ## Java 1.5 end ## }
//## Java 1.5 end ##
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* 1.0, and under the Eclipse Public License, Version 1.0 * Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group * (http://h2database.com/html/license.html).
*/ * Initial Developer: James Moger
package org.h2.test.jaqu; */
package org.h2.test.jaqu;
// ## Java 1.5 begin ##
import java.util.Arrays; //## Java 1.5 begin ##
import java.util.List; import java.util.Arrays;
import org.h2.jaqu.Table.JQColumn; import java.util.List;
import org.h2.jaqu.Table.JQTable; import org.h2.jaqu.Table.JQColumn;
import org.h2.jaqu.Table.JQTable;
/** //## Java 1.5 end ##
* A table containing product data.
*/ /**
// ## Java 1.5 begin ## * A table containing product data.
@JQTable(createIfRequired = false) */
public class ProductNoCreateTable { //## Java 1.5 begin ##
@JQTable(createIfRequired = false)
@SuppressWarnings("unused") public class ProductNoCreateTable {
@JQColumn(name = "id")
private Integer productId; @SuppressWarnings("unused")
@JQColumn(name = "id")
@SuppressWarnings("unused") private Integer productId;
@JQColumn(name = "name")
private String productName; @SuppressWarnings("unused")
@JQColumn(name = "name")
public ProductNoCreateTable() { private String productName;
// public constructor
} public ProductNoCreateTable() {
// public constructor
private ProductNoCreateTable(int productId, String productName) { }
this.productId = productId;
this.productName = productName; private ProductNoCreateTable(int productId, String productName) {
} this.productId = productId;
this.productName = productName;
private static ProductNoCreateTable create(int productId, String productName) { }
return new ProductNoCreateTable(productId, productName);
} private static ProductNoCreateTable create(int productId, String productName) {
return new ProductNoCreateTable(productId, productName);
public static List<ProductNoCreateTable> getList() { }
ProductNoCreateTable[] list = { create(1, "Chai"), create(2, "Chang") };
return Arrays.asList(list); public static List<ProductNoCreateTable> getList() {
} ProductNoCreateTable[] list = { create(1, "Chai"), create(2, "Chang") };
return Arrays.asList(list);
} }
// ## Java 1.5 end ##
}
//## Java 1.5 end ##
...@@ -386,7 +386,7 @@ public class SamplesTest extends TestBase { ...@@ -386,7 +386,7 @@ public class SamplesTest extends TestBase {
assertEquals(1, count); assertEquals(1, count);
} }
private void testLimitOffset() { private void testLimitOffset() {
Set<Integer> ids = new HashSet<Integer>(); Set<Integer> ids = new HashSet<Integer>();
Product p = new Product(); Product p = new Product();
...@@ -398,7 +398,7 @@ public class SamplesTest extends TestBase { ...@@ -398,7 +398,7 @@ public class SamplesTest extends TestBase {
} }
} }
} }
private void testKeyRetrieval() { private void testKeyRetrieval() {
List<SupportedTypes> list = SupportedTypes.createList(); List<SupportedTypes> list = SupportedTypes.createList();
List<Long> keys = db.insertAllAndGetKeys(list); List<Long> keys = db.insertAllAndGetKeys(list);
...@@ -406,7 +406,7 @@ public class SamplesTest extends TestBase { ...@@ -406,7 +406,7 @@ public class SamplesTest extends TestBase {
for (Long l : keys) { for (Long l : keys) {
assertTrue("Failed to add key. Duplicate?", uniqueKeys.add(l)); assertTrue("Failed to add key. Duplicate?", uniqueKeys.add(l));
} }
} }
//## Java 1.5 end ## //## Java 1.5 end ##
/** /**
......
package org.h2.test.jaqu; /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
import java.math.BigDecimal; * Version 1.0, and under the Eclipse Public License, Version 1.0
import java.util.List; * (http://h2database.com/html/license.html).
import java.util.Random; * Initial Developer: James Moger
import org.h2.jaqu.Table.JQColumn; */
import org.h2.jaqu.Table.JQTable; package org.h2.test.jaqu;
import org.h2.util.New;
import java.math.BigDecimal;
/** import java.util.List;
* A data class that contains a column for each data type. import java.util.Random;
*/ import org.h2.jaqu.Table.JQColumn;
@JQTable(strictTypeMapping = true, version = 1) import org.h2.jaqu.Table.JQTable;
public class SupportedTypes { import org.h2.util.New;
static final SupportedTypes SAMPLE = new SupportedTypes(); /**
* A data class that contains a column for each data type.
@JQColumn(primaryKey = true, autoIncrement = true) */
public Integer id; @JQTable(strictTypeMapping = true, version = 1)
public class SupportedTypes {
@JQColumn
private Boolean myBool = false; static final SupportedTypes SAMPLE = new SupportedTypes();
@JQColumn @JQColumn(primaryKey = true, autoIncrement = true)
private Byte myByte = 2; public Integer id;
@JQColumn @JQColumn
private Short myShort; private Boolean myBool = false;
@JQColumn @JQColumn
private Integer myInteger; private Byte myByte = 2;
@JQColumn @JQColumn
private Long myLong; private Short myShort;
@JQColumn @JQColumn
private Float myFloat = 1.0f; private Integer myInteger;
@JQColumn @JQColumn
private Double myDouble; private Long myLong;
@JQColumn @JQColumn
private BigDecimal myBigDecimal; private Float myFloat = 1.0f;
@JQColumn @JQColumn
private String myString; private Double myDouble;
@JQColumn @JQColumn
private java.util.Date myUtilDate; private BigDecimal myBigDecimal;
@JQColumn @JQColumn
private java.sql.Date mySqlDate; private String myString;
@JQColumn @JQColumn
private java.sql.Time mySqlTime; private java.util.Date myUtilDate;
@JQColumn @JQColumn
private java.sql.Timestamp mySqlTimestamp; private java.sql.Date mySqlDate;
static List<SupportedTypes> createList() { @JQColumn
List<SupportedTypes> list = New.arrayList(); private java.sql.Time mySqlTime;
for (int i = 0; i < 10; i++) {
list.add(randomValue()); @JQColumn
} private java.sql.Timestamp mySqlTimestamp;
return list;
} static List<SupportedTypes> createList() {
List<SupportedTypes> list = New.arrayList();
static SupportedTypes randomValue() { for (int i = 0; i < 10; i++) {
Random rand = new Random(); list.add(randomValue());
SupportedTypes s = new SupportedTypes(); }
s.myBool = new Boolean(rand.nextBoolean()); return list;
s.myByte = new Byte((byte) rand.nextInt(Byte.MAX_VALUE)); }
s.myShort = new Short((short) rand.nextInt(Short.MAX_VALUE));
s.myInteger = new Integer(rand.nextInt()); static SupportedTypes randomValue() {
s.myLong = new Long(rand.nextLong()); Random rand = new Random();
s.myFloat = new Float(rand.nextFloat()); SupportedTypes s = new SupportedTypes();
s.myDouble = new Double(rand.nextDouble()); s.myBool = new Boolean(rand.nextBoolean());
s.myBigDecimal = new BigDecimal(rand.nextDouble()); s.myByte = new Byte((byte) rand.nextInt(Byte.MAX_VALUE));
s.myString = Long.toHexString(rand.nextLong()); s.myShort = new Short((short) rand.nextInt(Short.MAX_VALUE));
s.myUtilDate = new java.util.Date(rand.nextLong()); s.myInteger = new Integer(rand.nextInt());
s.mySqlDate = new java.sql.Date(rand.nextLong()); s.myLong = new Long(rand.nextLong());
s.mySqlTime = new java.sql.Time(rand.nextLong()); s.myFloat = new Float(rand.nextFloat());
s.mySqlTimestamp = new java.sql.Timestamp(rand.nextLong()); s.myDouble = new Double(rand.nextDouble());
return s; s.myBigDecimal = new BigDecimal(rand.nextDouble());
} s.myString = Long.toHexString(rand.nextLong());
s.myUtilDate = new java.util.Date(rand.nextLong());
public boolean equivalentTo(SupportedTypes s) { s.mySqlDate = new java.sql.Date(rand.nextLong());
boolean same = true; s.mySqlTime = new java.sql.Time(rand.nextLong());
same &= myBool.equals(s.myBool); s.mySqlTimestamp = new java.sql.Timestamp(rand.nextLong());
same &= myByte.equals(s.myByte); return s;
same &= myShort.equals(s.myShort); }
same &= myInteger.equals(s.myInteger);
same &= myLong.equals(s.myLong); public boolean equivalentTo(SupportedTypes s) {
same &= myFloat.equals(s.myFloat); boolean same = true;
same &= myDouble.equals(s.myDouble); same &= myBool.equals(s.myBool);
same &= myBigDecimal.equals(s.myBigDecimal); same &= myByte.equals(s.myByte);
same &= myUtilDate.getTime() == s.myUtilDate.getTime(); same &= myShort.equals(s.myShort);
same &= mySqlTimestamp.getTime() == s.mySqlTimestamp.getTime(); same &= myInteger.equals(s.myInteger);
same &= mySqlDate.toString().equals(s.mySqlDate.toString()); same &= myLong.equals(s.myLong);
same &= mySqlTime.toString().equals(s.mySqlTime.toString()); same &= myFloat.equals(s.myFloat);
same &= myString.equals(s.myString); same &= myDouble.equals(s.myDouble);
return same; same &= myBigDecimal.equals(s.myBigDecimal);
} same &= myUtilDate.getTime() == s.myUtilDate.getTime();
same &= mySqlTimestamp.getTime() == s.mySqlTimestamp.getTime();
/** same &= mySqlDate.toString().equals(s.mySqlDate.toString());
* Class to demonstrate TableUpdater same &= mySqlTime.toString().equals(s.mySqlTime.toString());
* same &= myString.equals(s.myString);
*/ return same;
@JQTable(name = "SupportedTypes", version = 2, inheritColumns = true, strictTypeMapping = true) }
public static class SupportedTypes2 extends SupportedTypes {
/**
public SupportedTypes2() { * Class to demonstrate TableUpdater
// nothing to do *
} */
} @JQTable(name = "SupportedTypes", version = 2, inheritColumns = true, strictTypeMapping = true)
} public static class SupportedTypes2 extends SupportedTypes {
public SupportedTypes2() {
// nothing to do
}
}
}
...@@ -18,7 +18,7 @@ import static java.sql.Date.valueOf; ...@@ -18,7 +18,7 @@ import static java.sql.Date.valueOf;
*/ */
public class UpdateTest extends TestBase { public class UpdateTest extends TestBase {
Db db; private Db db;
/** /**
* This method is called when executing this application from the command * This method is called when executing this application from the command
...@@ -112,11 +112,11 @@ public class UpdateTest extends TestBase { ...@@ -112,11 +112,11 @@ public class UpdateTest extends TestBase {
ourOrder.orderDate = valueOf("2007-01-02"); ourOrder.orderDate = valueOf("2007-01-02");
db.merge(ourOrder); db.merge(ourOrder);
} }
private void testSetColumns() { private void testSetColumns() {
Product p = new Product(); Product p = new Product();
Product original = db.from(p).where(p.productId).is(1).selectFirst(); Product original = db.from(p).where(p.productId).is(1).selectFirst();
// update string and double columns // update string and double columns
db.from(p) db.from(p)
.set(p.productName).to("updated") .set(p.productName).to("updated")
...@@ -125,9 +125,9 @@ public class UpdateTest extends TestBase { ...@@ -125,9 +125,9 @@ public class UpdateTest extends TestBase {
.where(p.productId) .where(p.productId)
.is(1). .is(1).
update(); update();
// confirm the data was properly updated // confirm the data was properly updated
Product revised = db.from(p).where(p.productId).is(1).selectFirst(); Product revised = db.from(p).where(p.productId).is(1).selectFirst();
assertEquals("updated", revised.productName); assertEquals("updated", revised.productName);
assertEquals(original.unitPrice + 3.14, revised.unitPrice); assertEquals(original.unitPrice + 3.14, revised.unitPrice);
assertEquals(original.unitsInStock + 2, revised.unitsInStock.intValue()); assertEquals(original.unitsInStock + 2, revised.unitsInStock.intValue());
...@@ -138,12 +138,12 @@ public class UpdateTest extends TestBase { ...@@ -138,12 +138,12 @@ public class UpdateTest extends TestBase {
.set(p.unitPrice).to(original.unitPrice) .set(p.unitPrice).to(original.unitPrice)
.increment(p.unitsInStock).by(-2) .increment(p.unitsInStock).by(-2)
.where(p.productId).is(1).update(); .where(p.productId).is(1).update();
// confirm the data was properly restored // confirm the data was properly restored
Product restored = db.from(p).where(p.productId).is(1).selectFirst(); Product restored = db.from(p).where(p.productId).is(1).selectFirst();
assertEquals(original.productName, restored.productName); assertEquals(original.productName, restored.productName);
assertEquals(original.unitPrice, restored.unitPrice); assertEquals(original.unitPrice, restored.unitPrice);
assertEquals(original.unitsInStock, restored.unitsInStock); assertEquals(original.unitsInStock, restored.unitsInStock);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论