added basic Derby support

This commit is contained in:
Timo Westkämper 2009-04-29 19:20:48 +00:00
parent aed6f68289
commit 1254bfb09f
13 changed files with 180 additions and 15 deletions

View File

@ -393,8 +393,13 @@ public class AbstractColQuery<SubType extends AbstractColQuery<SubType>> extends
close();
}
}
public Object[] uniqueResult(Expr<?> first, Expr<?> second, Expr<?>... rest) {
try {
return super.uniqueResult(first, second, rest);
} finally {
close();
}
}
}

View File

@ -111,6 +111,15 @@ public interface Projectable {
* @return the result or null for an empty result
*/
<RT> RT uniqueResult(Expr<RT> projection);
/**
*
* @param first
* @param second
* @param rest
* @return
*/
Object[] uniqueResult(Expr<?> first, Expr<?> second, Expr<?>... rest);
}

View File

@ -72,6 +72,10 @@ public abstract class ProjectableAdapter implements Projectable{
return projectable.uniqueResult(expr);
}
public Object[]uniqueResult(Expr<?> first, Expr<?> second, Expr<?>... rest) {
return projectable.uniqueResult(first, second, rest);
}
public void setProjectable(Projectable projectable) {
this.projectable = Assert.notNull(projectable);
}

View File

@ -72,4 +72,9 @@ public abstract class QueryBaseWithProjection<JoinMeta,SubType
Iterator<RT> it = iterate(expr);
return it.hasNext() ? it.next() : null;
}
public Object[] uniqueResult(Expr<?> first, Expr<?> second, Expr<?>... rest) {
Iterator<Object[]> it = iterate(first, second, rest);
return it.hasNext() ? it.next() : null;
}
}

View File

@ -33,6 +33,13 @@
<scope>test</scope>
<!-- license : TODO -->
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.4.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>

View File

@ -14,6 +14,7 @@ import java.math.BigInteger;
* Querydsl SQL supports the following database systems :
* <ul>
* <li>HSQLDB</li>
* <li>Derby</li>
* <li>MySQL</li>
* <li>Oracle</li>
* <li>PostgreSQL</li>
@ -26,13 +27,35 @@ import java.math.BigInteger;
public class Dialect {
// tested
public static SqlOps forHqlsdb(){
public static SqlOps forHSQLDB(){
return new SqlOps(){{
add(Ops.OpMath.ROUND, "round(%s,0)");
add(Ops.TRIM, "trim(both from %s)");
}};
}
// tested
public static SqlOps forDerby(){
return new SqlOps(){{
add(Ops.CONCAT, "%s || %s");
add(Ops.OpMath.ROUND, "floor(%s)");
add(Ops.SUBSTR1ARG, "substr(%s,%s+1)");
add(Ops.SUBSTR2ARGS, "substr(%s,%s+1,%s+1)");
add(Ops.STARTSWITH, "%s like (%s || '%%')");
add(Ops.ENDSWITH, "%s like ('%%' || %s)");
add(Ops.STARTSWITH_IC, "lower(%s) like (lower(%s) || '%%')");
add(Ops.ENDSWITH_IC, "lower(%s) like ('%%' || lower(%s))");
add(Ops.OpDateTime.YEAR, "year(%s)");
add(Ops.OpDateTime.MONTH, "month(%s)");
add(Ops.OpDateTime.HOUR, "hour(%s)");
add(Ops.OpDateTime.MINUTE, "minute(%s)");
add(Ops.OpDateTime.SECOND, "second(%s)");
}};
}
// tested
public static SqlOps forMySQL(){
return new SqlOps(){{

View File

@ -75,8 +75,8 @@ public class SqlOps extends OperationPatterns {
add(Ops.OpDateTime.CURRENT_TIME, "current_timestamp");
// string
add(Ops.SUBSTR1ARG, "locate(%s,%s) > 0");
add(Ops.SUBSTR2ARGS, "substr(%s,%s,%s)");
add(Ops.SUBSTR1ARG, "substr(%s,%s)");
add(Ops.SUBSTR2ARGS,"substr(%s,%s,%s)");
add(Ops.STARTSWITH, "%s like concat(%s,'%%')");
add(Ops.ENDSWITH, "%s like concat('%%',%s)");

View File

@ -21,5 +21,5 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Inherited
public @interface ExcludeIn {
String value();
String[] value();
}

View File

@ -6,6 +6,7 @@
package com.mysema.query;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.JUnit4ClassRunner;
@ -61,8 +62,8 @@ public class FilteringTestRunner extends JUnit4ClassRunner{
if (label != null){
ExcludeIn ex = method.getAnnotation(ExcludeIn.class);
IncludeIn in = method.getAnnotation(IncludeIn.class);
ignore |= ex != null && ex.value().contains(label);
ignore |= in != null && !in.value().contains(label);
ignore |= ex != null && Arrays.asList(ex.value()).contains(label);
ignore |= in != null && !Arrays.asList(in.value()).contains(label);
}
return ignore;
}

View File

@ -21,5 +21,5 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Inherited
public @interface IncludeIn {
String value();
String[] value();
}

View File

@ -0,0 +1,107 @@
package com.mysema.query.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import com.mysema.query.FilteringTestRunner;
import com.mysema.query.Label;
import com.mysema.query.grammar.Dialect;
@RunWith(FilteringTestRunner.class)
@Label("derby")
public class DerbyTest extends SqlQueryTest {
@BeforeClass
public static void setUp() throws Exception{
String sql;
Connection c = getDerbyConnection();
Statement stmt = c.createStatement();
connHolder.set(c);
stmtHolder.set(stmt);
// survey
// stmt.execute("drop table survey if exists");
safeExecute(stmt, "drop table survey");
stmt.execute("create table survey (id int,name varchar(30))");
stmt.execute("insert into survey values (1, 'Hello World')");
// test
// stmt.execute("drop table test if exists");
safeExecute(stmt, "drop table test");
stmt.execute("create table test(name varchar(255))");
sql = "insert into test values(?)";
PreparedStatement pstmt = c.prepareStatement(sql);
for (int i = 0; i < 10000; i++) {
pstmt.setString(1, "name" + i);
pstmt.addBatch();
}
pstmt.executeBatch();
// employee
// stmt.execute("drop table employee if exists");
safeExecute(stmt, "drop table employee");
stmt.execute("create table employee(id int, "
+ "firstname VARCHAR(50), " + "lastname VARCHAR(50), "
+ "salary decimal(10, 2), " + "superior_id int, "
+ "CONSTRAINT PK_employee PRIMARY KEY (id), "
+ "CONSTRAINT FK_superior FOREIGN KEY (superior_id) "
+ "REFERENCES employee(ID))");
addEmployee(1, "Mike", "Smith", 160000, -1);
addEmployee(2, "Mary", "Smith", 140000, -1);
// Employee under Mike
addEmployee(10, "Joe", "Divis", 50000, 1);
addEmployee(11, "Peter", "Mason", 45000, 1);
addEmployee(12, "Steve", "Johnson", 40000, 1);
addEmployee(13, "Jim", "Hood", 35000, 1);
// Employee under Mike
addEmployee(20, "Jennifer", "Divis", 60000, 2);
addEmployee(21, "Helen", "Mason", 50000, 2);
addEmployee(22, "Daisy", "Johnson", 40000, 2);
addEmployee(23, "Barbara", "Hood", 30000, 2);
// date_test and time_test
// stmt.execute("drop table time_test if exists");
// stmt.execute("drop table date_test if exists");
safeExecute(stmt, "drop table time_test");
safeExecute(stmt, "drop table date_test");
stmt.execute("create table time_test(time_test time)");
stmt.execute("create table date_test(date_test date)");
}
private static void safeExecute(Statement stmt, String sql){
try {
stmt.execute(sql);
} catch (SQLException e) {
// do nothing
}
}
@Before
public void setUpForTest(){
dialect = Dialect.forDerby().newLineToSingleSpace();
}
private static void addEmployee(int id, String firstName, String lastName, double salary,
int superiorId) throws Exception {
stmtHolder.get().execute("insert into employee values(" + id + ", '" + firstName
+ "', '" + lastName + "', " + salary + ", "
+ (superiorId <= 0 ? "null" : ("" + superiorId)) + ")");
}
private static Connection getDerbyConnection() throws Exception{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
String url = "jdbc:derby:demoDB;create=true";
return DriverManager.getConnection(url, "", "");
}
}

View File

@ -85,7 +85,7 @@ public class HsqldbTest extends SqlQueryTest {
@Before
public void setUpForTest(){
dialect = Dialect.forHqlsdb().newLineToSingleSpace();
dialect = Dialect.forHSQLDB().newLineToSingleSpace();
}
private static void addEmployee(int id, String firstName, String lastName, double salary,

View File

@ -137,14 +137,14 @@ public abstract class SqlQueryTest {
}
@Test
@ExcludeIn("oracle")
@ExcludeIn({"oracle","derby"})
public void testSelectBooleanExpr() throws SQLException{
// TODO : FIXME
System.out.println(q().from(survey).list(survey.id.eq(0)));
}
@Test
@ExcludeIn("oracle")
@ExcludeIn({"oracle","derby"})
public void testSelectBooleanExpr2() throws SQLException{
// TODO : FIXME
System.out.println(q().from(survey).list(survey.id.gt(0)));
@ -254,7 +254,7 @@ public abstract class SqlQueryTest {
}
@Test
@ExcludeIn("oracle")
@ExcludeIn({"oracle","derby"})
public void testLimitAndOffset() throws SQLException{
// limit offset
expectedQuery = "select employee.id from employee employee limit 4 offset 3";
@ -326,7 +326,7 @@ public abstract class SqlQueryTest {
}
@Test
@ExcludeIn("hsqldb")
@ExcludeIn({"hsqldb","derby"})
public void testQueryWithoutFrom() throws SQLException{
q().list(add(new Expr.EConstant<Integer>(1),1));
}
@ -339,6 +339,7 @@ public abstract class SqlQueryTest {
}
@Test
@ExcludeIn({"derby"})
public void testMathFunctions() throws SQLException{
Expr<Integer> i = new Expr.EConstant<Integer>(1);
Expr<Double> d = new Expr.EConstant<Double>(1.0);
@ -367,6 +368,7 @@ public abstract class SqlQueryTest {
}
@Test
@ExcludeIn({"derby"})
public void testStringFunctions() throws SQLException{
EString s = employee.firstname;
for (EString e : Arrays.<EString>asList(
@ -395,6 +397,7 @@ public abstract class SqlQueryTest {
}
@Test
@ExcludeIn({"derby"})
public void testDateTimeFunctions() throws SQLException{
Expr<Date> d = new Expr.EConstant<Date>(new Date());
Expr<Time> t = new Expr.EConstant<Time>(new Time(0));
@ -420,6 +423,7 @@ public abstract class SqlQueryTest {
}
@Test
@ExcludeIn({"derby"})
public void testCasts() throws SQLException{
ENumber<?> num = employee.id;
Expr<?>[] expr = new Expr[]{