Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

#106 worked on codegen refactoring

  • Loading branch information...
commit e3bf5a491782b833eb20ed494ac58753a5fa6fe7 1 parent c68789f
@timowest timowest authored
Showing with 732 additions and 106 deletions.
  1. +3 −2 querydsl-apt/pom.xml
  2. +4 −11 querydsl-core/pom.xml
  3. +1 −1  querydsl-hibernate-search/src/main/java/com/mysema/query/{ → hibernate}/search/SearchQuery.java
  4. +1 −1  querydsl-hibernate-search/src/main/java/com/mysema/query/{ → hibernate}/search/SearchSerializer.java
  5. +1 −1  querydsl-hibernate-search/src/main/java/com/mysema/query/{ → hibernate}/search/package-info.java
  6. +1 −1  querydsl-hibernate-search/src/test/java/com/mysema/query/{ → hibernate}/search/AbstractQueryTest.java
  7. +1 −1  querydsl-hibernate-search/src/test/java/com/mysema/query/{ → hibernate}/search/QUser.java
  8. +2 −1  querydsl-hibernate-search/src/test/java/com/mysema/query/{ → hibernate}/search/SearchQueryTest.java
  9. +3 −1 querydsl-hibernate-search/src/test/java/com/mysema/query/{ → hibernate}/search/SearchSerializerTest.java
  10. +1 −1  querydsl-hibernate-search/src/test/java/com/mysema/query/{ → hibernate}/search/User.java
  11. +7 −1 querydsl-jdo/pom.xml
  12. +2 −1  querydsl-jdo/src/main/assembly.xml
  13. +2 −2 querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/codegen/HibernateDomainExporterTest.java
  14. +19 −0 querydsl-jpa-codegen/src/test/resources/contact.hbm.xml
  15. +19 −0 querydsl-jpa-codegen/src/test/resources/contact2.hbm.xml
  16. +12 −0 querydsl-jpa-codegen/src/test/resources/hibernate.cfg.xml
  17. +52 −0 querydsl-jpa-codegen/src/test/resources/store.hbm.xml
  18. +1 −1  querydsl-jpa/pom.xml
  19. +2 −1  querydsl-jpa/src/main/assembly-hibernate.xml
  20. +2 −1  querydsl-jpa/src/main/assembly.xml
  21. +0 −1  querydsl-jpa/template.mf
  22. +1 −2  querydsl-mongodb/pom.xml
  23. +10 −26 querydsl-sql-codegen/pom.xml
  24. +12 −31 querydsl-sql/pom.xml
  25. +18 −16 querydsl-sql/src/test/java/com/mysema/query/Connections.java
  26. +61 −0 querydsl-sql/src/test/java/com/mysema/query/ddl/ColumnData.java
  27. +228 −0 querydsl-sql/src/test/java/com/mysema/query/ddl/CreateTableClause.java
  28. +52 −0 querydsl-sql/src/test/java/com/mysema/query/ddl/DropTableClause.java
  29. +49 −0 querydsl-sql/src/test/java/com/mysema/query/ddl/ForeignKeyBuilder.java
  30. +53 −0 querydsl-sql/src/test/java/com/mysema/query/ddl/ForeignKeyData.java
  31. +42 −0 querydsl-sql/src/test/java/com/mysema/query/ddl/IndexData.java
  32. +24 −0 querydsl-sql/src/test/java/com/mysema/query/ddl/KeyData.java
  33. +46 −0 querydsl-sql/src/test/java/com/mysema/query/ddl/PrimaryKeyData.java
  34. +0 −2  querydsl-sql/template.mf
View
5 querydsl-apt/pom.xml
@@ -16,7 +16,8 @@
<packaging>jar</packaging>
<properties>
- <jdo.version>2.3-eb</jdo.version>
+ <jdo.version>2.3-eb</jdo.version>
+ <morphia.version>0.99</morphia.version>
</properties>
<dependencies>
@@ -50,7 +51,7 @@
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
- </dependency>
+ </dependency>
<!-- test -->
<dependency>
View
15 querydsl-core/pom.xml
@@ -16,18 +16,16 @@
<packaging>jar</packaging>
<dependencies>
-
<dependency>
<groupId>net.sourceforge.collections</groupId>
<artifactId>collections-generic</artifactId>
<version>${commons.collections.version}</version>
- </dependency>
+ </dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
-
<dependency>
<groupId>com.mysema.commons</groupId>
<artifactId>mysema-commons-lang</artifactId>
@@ -45,31 +43,26 @@
</dependency>
<!-- alias dependencies -->
-
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
- <!-- test -->
-
+ <!-- test -->
<dependency>
<groupId>jdepend</groupId>
<artifactId>jdepend</artifactId>
<version>2.9.1</version>
<scope>test</scope>
- </dependency>
-
+ </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
<!-- license : Apache License 2.0 -->
<scope>test</scope>
- </dependency>
-
-
+ </dependency>
</dependencies>
<build>
View
2  ...ain/java/com/mysema/query/search/SearchQuery.java → ...om/mysema/query/hibernate/search/SearchQuery.java
@@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mysema.query.search;
+package com.mysema.query.hibernate.search;
import java.util.List;
View
2  ...ava/com/mysema/query/search/SearchSerializer.java → ...sema/query/hibernate/search/SearchSerializer.java
@@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mysema.query.search;
+package com.mysema.query.hibernate.search;
import org.hibernate.search.annotations.Field;
View
2  ...in/java/com/mysema/query/search/package-info.java → ...m/mysema/query/hibernate/search/package-info.java
@@ -12,4 +12,4 @@
* limitations under the License.
*/
-package com.mysema.query.search;
+package com.mysema.query.hibernate.search;
View
2  ...va/com/mysema/query/search/AbstractQueryTest.java → ...ema/query/hibernate/search/AbstractQueryTest.java
@@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mysema.query.search;
+package com.mysema.query.hibernate.search;
import java.io.File;
import java.io.IOException;
View
2  .../src/test/java/com/mysema/query/search/QUser.java → ...java/com/mysema/query/hibernate/search/QUser.java
@@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mysema.query.search;
+package com.mysema.query.hibernate.search;
import com.mysema.query.types.PathMetadataFactory;
import com.mysema.query.types.path.EntityPathBase;
View
3  ...java/com/mysema/query/search/SearchQueryTest.java → ...ysema/query/hibernate/search/SearchQueryTest.java
@@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mysema.query.search;
+package com.mysema.query.hibernate.search;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -27,6 +27,7 @@
import com.mysema.query.NonUniqueResultException;
import com.mysema.query.SearchResults;
+import com.mysema.query.hibernate.search.SearchQuery;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.expr.BooleanExpression;
View
4 ...com/mysema/query/search/SearchSerializerTest.java → .../query/hibernate/search/SearchSerializerTest.java
@@ -11,12 +11,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mysema.query.search;
+package com.mysema.query.hibernate.search;
import static org.junit.Assert.*;
import org.junit.Test;
+import com.mysema.query.hibernate.search.SearchSerializer;
+
public class SearchSerializerTest {
@Test
View
2  ...h/src/test/java/com/mysema/query/search/User.java → .../java/com/mysema/query/hibernate/search/User.java
@@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mysema.query.search;
+package com.mysema.query.hibernate.search;
import java.io.Serializable;
View
8 querydsl-jdo/pom.xml
@@ -38,7 +38,13 @@
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>${project.parent.version}</version>
- </dependency>
+ </dependency>
+ <dependency>
+ <groupId>com.mysema.querydsl</groupId>
+ <artifactId>querydsl-codegen</artifactId>
+ <version>${project.parent.version}</version>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
View
3  querydsl-jdo/src/main/assembly.xml
@@ -31,7 +31,8 @@
<unpack>true</unpack>
<scope>provided</scope>
<includes>
- <include>com.mysema.querydsl:querydsl-apt</include>
+ <include>com.mysema.querydsl:*</include>
+ <include>com.mysema.codegen:*</include>
</includes>
</dependencySet>
</dependencySets>
View
4 querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/codegen/HibernateDomainExporterTest.java
@@ -90,7 +90,7 @@ public void Execute_Multiple() throws IOException{
for (File file : new File("target/gen3/com/mysema/query/jpa/domain").listFiles()){
String result1 = FileUtils.readFileToString(file, "UTF-8");
String result2 = FileUtils.readFileToString(
- new File("target/generated-test-sources/java/com/mysema/query/jpa/domain", file.getName()));
+ new File("../querydsl-jpa/target/generated-test-sources/java/com/mysema/query/jpa/domain", file.getName()));
if (!result1.equals(result2)){
System.err.println(file.getName());
failures.add(file.getName());
@@ -117,7 +117,7 @@ public void Execute_Multiple2() throws IOException{
for (File file : new File("target/gen4/com/mysema/query/jpa/domain2").listFiles()){
String result1 = FileUtils.readFileToString(file, "UTF-8");
String result2 = FileUtils.readFileToString(
- new File("target/generated-test-sources/java/com/mysema/query/jpa/domain2", file.getName()));
+ new File("../querydsl-jpa/target/generated-test-sources/java/com/mysema/query/jpa/domain2", file.getName()));
if (!result1.equals(result2)){
System.err.println(file.getName());
failures.add(file.getName());
View
19 querydsl-jpa-codegen/src/test/resources/contact.hbm.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+<hibernate-mapping package="com.mysema.query.jpa.domain2">
+ <class name="Contact" table="CONTACT">
+ <id name="id" type="long" column="ID" >
+ <generator class="assigned"/>
+ </id>
+
+ <property name="firstName">
+ <column name="FIRSTNAME" />
+ </property>
+ <property name="lastName">
+ <column name="LASTNAME"/>
+ </property>
+ <property name="email">
+ <column name="EMAIL"/>
+ </property>
+ </class>
+</hibernate-mapping>
View
19 querydsl-jpa-codegen/src/test/resources/contact2.hbm.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+<hibernate-mapping>
+ <class name="com.mysema.query.jpa.domain2.Contact" table="CONTACT">
+ <id name="id" type="long" column="ID" >
+ <generator class="assigned"/>
+ </id>
+
+ <property name="firstName">
+ <column name="FIRSTNAME" />
+ </property>
+ <property name="lastName">
+ <column name="LASTNAME"/>
+ </property>
+ <property name="email">
+ <column name="EMAIL"/>
+ </property>
+ </class>
+</hibernate-mapping>
View
12 querydsl-jpa-codegen/src/test/resources/hibernate.cfg.xml
@@ -0,0 +1,12 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!DOCTYPE hibernate-configuration PUBLIC
+"-//Hibernate/Hibernate Configuration DTD//EN"
+"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+
+<hibernate-configuration>
+<session-factory>
+ <property name="hibernate.dialect">com.mysema.query.jpa.support.ExtendedDerbyDialect</property>
+ <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
+ <property name="hibernate.connection.url">jdbc:derby:target/derbydb;create=true</property>
+</session-factory>
+</hibernate-configuration>
View
52 querydsl-jpa-codegen/src/test/resources/store.hbm.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+<hibernate-mapping>
+
+ <class name="com.mysema.query.jpa.domain3.Store" table="STORE" discriminator-value="S">
+
+<!--
+ <discriminator column="subclass" type="character"/>
+ -->
+ <id name="code" type="java.lang.String" column="CODE">
+ <generator class="assigned"/>
+ </id>
+
+ <property name="name">
+ <column name="NAME"/>
+ </property>
+
+ <property name="address">
+ <column name="ADDRESS"/>
+ </property>
+
+ <property name="city">
+ <column name="CITY"/>
+ </property>
+
+ <property name="phoneDetails">
+ <column name="PHONE_DETAILS" length="30"/>
+ </property>
+
+ <property name="faxDetails">
+ <column name="FAX_DETAILS" length="30"/>
+ </property>
+
+ <property name="zipCode">
+ <column name="ZIP_CODE" length="15"/>
+ </property>
+
+ <property name="chainCode">
+ <column name="CHAIN_CODE"/>
+ </property>
+
+ <subclass name="com.mysema.query.jpa.domain3.HardwareStore" discriminator-value="D">
+
+ <property name="storeCode">
+ <column name="STORE_CODE"/>
+ </property>
+
+ </subclass>
+
+ </class>
+
+</hibernate-mapping>
View
2  querydsl-jpa/pom.xml
@@ -67,7 +67,7 @@
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>${project.parent.version}</version>
- </dependency>
+ </dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
View
3  querydsl-jpa/src/main/assembly-hibernate.xml
@@ -37,7 +37,8 @@
<unpack>true</unpack>
<scope>provided</scope>
<includes>
- <include>com.mysema.querydsl:querydsl-apt</include>
+ <include>com.mysema.querydsl:*</include>
+ <include>com.mysema.codegen:*</include>
<include>org.hibernate:hibernate-core</include>
</includes>
</dependencySet>
View
3  querydsl-jpa/src/main/assembly.xml
@@ -37,7 +37,8 @@
<unpack>true</unpack>
<scope>provided</scope>
<includes>
- <include>com.mysema.querydsl:querydsl-apt</include>
+ <include>com.mysema.querydsl:*</include>
+ <include>com.mysema.codegen:*</include>
</includes>
</dependencySet>
</dependencySets>
View
1  querydsl-jpa/template.mf
@@ -3,7 +3,6 @@ Bundle-Name: Querydsl JPA
Bundle-Vendor: Mysema
Bundle-ManifestVersion: 2
Import-Template:
- com.mysema.codegen.*;version="${codegen.version}",
com.mysema.commons.lang.*;version="${mysema.lang.version}",
com.mysema.query.*;version="${project.version}",
com.mysema.util.*;version="${project.version}",
View
3  querydsl-mongodb/pom.xml
@@ -25,8 +25,7 @@
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongodb.version}</version>
- </dependency>
-
+ </dependency>
<dependency>
<groupId>com.google.code.morphia</groupId>
<artifactId>morphia</artifactId>
View
36 querydsl-sql-codegen/pom.xml
@@ -20,7 +20,6 @@
</properties>
<dependencies>
-
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-codegen</artifactId>
@@ -31,13 +30,7 @@
<artifactId>querydsl-sql</artifactId>
<version>${project.parent.version}</version>
</dependency>
- <dependency>
- <groupId>com.mysema.querydsl</groupId>
- <artifactId>querydsl-sql</artifactId>
- <version>${project.parent.version}</version>
- <scope>test</scope>
- <type>test-jar</type>
- </dependency>
+
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@@ -48,22 +41,7 @@
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>joda-time</groupId>
- <artifactId>joda-time</artifactId>
- <version>1.6</version>
- </dependency>
-
- <!-- code generation -->
-
- <dependency>
- <groupId>javax.validation</groupId>
- <artifactId>validation-api</artifactId>
- <version>1.0.0.GA</version>
- </dependency>
-
- <!-- ANT integration -->
-
+ <!-- ANT integration -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
@@ -71,8 +49,14 @@
<scope>provided</scope>
</dependency>
- <!-- test -->
-
+ <!-- test -->
+ <dependency>
+ <groupId>com.mysema.querydsl</groupId>
+ <artifactId>querydsl-sql</artifactId>
+ <version>${project.parent.version}</version>
+ <scope>test</scope>
+ <type>test-jar</type>
+ </dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
View
43 querydsl-sql/pom.xml
@@ -15,18 +15,23 @@
<description>SQL support for Querydsl</description>
<packaging>jar</packaging>
- <properties>
- <ant.version>1.8.1</ant.version>
- </properties>
-
<dependencies>
-
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>${project.parent.version}</version>
- </dependency>
-
+ </dependency>
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>1.6</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.validation</groupId>
+ <artifactId>validation-api</artifactId>
+ <version>1.0.0.GA</version>
+ </dependency>
+
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@@ -37,31 +42,7 @@
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>joda-time</groupId>
- <artifactId>joda-time</artifactId>
- <version>1.6</version>
- </dependency>
-
- <!-- code generation -->
-
- <dependency>
- <groupId>javax.validation</groupId>
- <artifactId>validation-api</artifactId>
- <version>1.0.0.GA</version>
- </dependency>
-
- <!-- ANT integration -->
-
- <dependency>
- <groupId>org.apache.ant</groupId>
- <artifactId>ant</artifactId>
- <version>${ant.version}</version>
- <scope>provided</scope>
- </dependency>
-
<!-- test -->
-
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
View
34 querydsl-sql/src/test/java/com/mysema/query/Connections.java
@@ -23,6 +23,8 @@
import org.hsqldb.types.Types;
+import com.mysema.query.ddl.CreateTableClause;
+import com.mysema.query.ddl.DropTableClause;
import com.mysema.query.sql.DerbyTemplates;
import com.mysema.query.sql.H2Templates;
import com.mysema.query.sql.HSQLDBTemplates;
@@ -120,12 +122,12 @@ private static Connection getSQLServer() throws ClassNotFoundException, SQLExcep
return DriverManager.getConnection(url, "querydsl","querydsl");
}
-// private static CreateTableClause createTable(SQLTemplates templates, String table){
-// return new CreateTableClause(connHolder.get(), templates, table);
-// }
+ private static CreateTableClause createTable(SQLTemplates templates, String table){
+ return new CreateTableClause(connHolder.get(), templates, table);
+ }
- private static void dropTable(SQLTemplates templates, String table){
-// new DropTableClause(connHolder.get(), templates, table).execute();
+ private static void dropTable(SQLTemplates templates, String table) throws SQLException{
+ new DropTableClause(connHolder.get(), templates, table).execute();
}
public static Statement getStatement(){
@@ -133,17 +135,17 @@ public static Statement getStatement(){
}
private static void createEmployeeTable(SQLTemplates templates) {
-// createTable(templates, "EMPLOYEE")
-// .column("ID", Integer.class)
-// .column("FIRSTNAME", String.class).size(50)
-// .column("LASTNAME", String.class).size(50)
-// .column("SALARY",Double.class)
-// .column("DATEFIELD",Date.class)
-// .column("TIMEFIELD",Time.class)
-// .column("SUPERIOR_ID",Integer.class)
-// .primaryKey("PK_EMPLOYEE", "ID")
-// .foreignKey("FK_SUPERIOR","SUPERIOR_ID").references("EMPLOYEE","ID")
-// .execute();
+ createTable(templates, "EMPLOYEE")
+ .column("ID", Integer.class)
+ .column("FIRSTNAME", String.class).size(50)
+ .column("LASTNAME", String.class).size(50)
+ .column("SALARY",Double.class)
+ .column("DATEFIELD",Date.class)
+ .column("TIMEFIELD",Time.class)
+ .column("SUPERIOR_ID",Integer.class)
+ .primaryKey("PK_EMPLOYEE", "ID")
+ .foreignKey("FK_SUPERIOR","SUPERIOR_ID").references("EMPLOYEE","ID")
+ .execute();
}
public static void initDerby() throws SQLException, ClassNotFoundException{
View
61 querydsl-sql/src/test/java/com/mysema/query/ddl/ColumnData.java
@@ -0,0 +1,61 @@
+/*
+* Copyright (c) 2010 Mysema Ltd.
+* All rights reserved.
+*
+*/
+package com.mysema.query.ddl;
+
+/**
+* @author tiwe
+*
+*/
+public class ColumnData {
+
+ private final String name;
+
+ private final String type;
+
+ private boolean nullAllowed = true;
+
+ private boolean autoIncrement;
+
+ private Integer size;
+
+ public ColumnData(String name, String type) {
+ this.name = name;
+ this.type = type;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public boolean isNullAllowed() {
+ return nullAllowed;
+ }
+
+ public void setNullAllowed(boolean nullAllowed) {
+ this.nullAllowed = nullAllowed;
+ }
+
+ public void setSize(Integer size) {
+ this.size = size;
+ }
+
+ public Integer getSize() {
+ return size;
+ }
+
+ public boolean isAutoIncrement() {
+ return autoIncrement;
+ }
+
+ public void setAutoIncrement(boolean autoIncrement) {
+ this.autoIncrement = autoIncrement;
+ }
+
+}
View
228 querydsl-sql/src/test/java/com/mysema/query/ddl/CreateTableClause.java
@@ -0,0 +1,228 @@
+/*
+ * Copyright (c) 2010 Mysema Ltd.
+ * All rights reserved.
+ *
+ */
+package com.mysema.query.ddl;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.mysema.commons.lang.Assert;
+import com.mysema.query.QueryException;
+import com.mysema.query.sql.SQLTemplates;
+
+/**
+ * CreateTableClause defines a CREATE TABLE clause
+ *
+ * @author tiwe
+ *
+ */
+public class CreateTableClause {
+
+ private static final Logger logger = LoggerFactory.getLogger(CreateTableClause.class);
+
+ private final Connection connection;
+
+ private final SQLTemplates templates;
+
+ private final String table;
+
+ private final List<ColumnData> columns = new ArrayList<ColumnData>();
+
+ private final List<IndexData> indexes = new ArrayList<IndexData>();
+
+ private PrimaryKeyData primaryKey;
+
+ private final List<ForeignKeyData> foreignKeys = new ArrayList<ForeignKeyData>();
+
+ public CreateTableClause(Connection conn, SQLTemplates templates, String table) {
+ this.connection = conn;
+ this.templates = templates;
+ this.table = templates.quoteIdentifier(table);
+ }
+
+ /**
+ * Add a new column definition
+ *
+ * @param name
+ * @param type
+ * @return
+ */
+ public CreateTableClause column(String name, Class<?> type) {
+ Assert.notNull(name,"name");
+ Assert.notNull(type,"type");
+ columns.add(new ColumnData(templates.quoteIdentifier(name), templates.getTypeForClass(type)));
+ return this;
+ }
+
+ private ColumnData lastColumn(){
+ return columns.get(columns.size()-1);
+ }
+
+ /**
+ * Set the last added column to not null
+ *
+ * @return
+ */
+ public CreateTableClause notNull() {
+ lastColumn().setNullAllowed(false);
+ return this;
+ }
+
+ /**
+ * Set the size of the last column's type
+ *
+ * @param size
+ * @return
+ */
+ public CreateTableClause size(int size) {
+ lastColumn().setSize(size);
+ return this;
+ }
+
+ /**
+ * Set the last column to auto increment
+ *
+ * @return
+ */
+ public CreateTableClause autoIncrement(){
+ lastColumn().setAutoIncrement(true);
+ return this;
+ }
+
+ /**
+ * Set the primary key
+ *
+ * @param name
+ * @param columns
+ * @return
+ */
+ public CreateTableClause primaryKey(String name, String... columns) {
+ Assert.notNull(name,"name");
+ Assert.notEmpty(columns,"columns");
+ for (int i = 0; i < columns.length; i++){
+ columns[i] = templates.quoteIdentifier(columns[i]);
+ }
+ primaryKey = new PrimaryKeyData(templates.quoteIdentifier(name), columns);
+ return this;
+ }
+
+ /**
+ * Add an index
+ *
+ * @param name
+ * @param columns
+ * @return
+ */
+ public CreateTableClause index(String name, String... columns){
+ Assert.notNull(name,"name");
+ Assert.notEmpty(columns,"columns");
+ indexes.add(new IndexData(name, columns));
+ return this;
+ }
+
+ /**
+ * Set the last added index to unique
+ *
+ * @return
+ */
+ public CreateTableClause unique(){
+ indexes.get(indexes.size()-1).setUnique(true);
+ return this;
+ }
+
+ /**
+ * Add a foreign key
+ *
+ * @param name
+ * @param columns
+ * @return
+ */
+ public ForeignKeyBuilder foreignKey(String name, String... columns) {
+ Assert.notNull(name,"name");
+ Assert.notEmpty(columns,"columns");
+ return new ForeignKeyBuilder(this, templates, foreignKeys, templates.quoteIdentifier(name), columns);
+ }
+
+ /**
+ * Execute the clause
+ */
+ @SuppressWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
+ public void execute() {
+ StringBuilder builder = new StringBuilder();
+ builder.append(templates.getCreateTable() + table + " (\n");
+ List<String> lines = new ArrayList<String>(columns.size() + foreignKeys.size() + 1);
+ // columns
+ for (ColumnData column : columns){
+ StringBuilder line = new StringBuilder();
+ line.append(column.getName() + " " + column.getType().toUpperCase());
+ if (column.getSize() != null){
+ line.append("(" + column.getSize() + ")");
+ }
+ if (!column.isNullAllowed()){
+ line.append(templates.getNotNull().toUpperCase());
+ }
+ if (column.isAutoIncrement()){
+ line.append(templates.getAutoIncrement().toUpperCase());
+ }
+ lines.add(line.toString());
+ }
+
+ // primary key
+ if (primaryKey != null){
+ StringBuilder line = new StringBuilder();
+ line.append("CONSTRAINT " + primaryKey.getName()+ " ");
+ line.append("PRIMARY KEY(" + StringUtils.join(primaryKey.getColumns(), ", ") +")");
+ lines.add(line.toString());
+ }
+
+ // foreign keys
+ for (ForeignKeyData foreignKey : foreignKeys){
+ StringBuilder line = new StringBuilder();
+ line.append("CONSTRAINT " + foreignKey.getName()+ " ");
+ line.append("FOREIGN KEY(" + StringUtils.join(foreignKey.getForeignColumns(), ", ")+ ") ");
+ line.append("REFERENCES " + foreignKey.getTable() + "(" + StringUtils.join(foreignKey.getParentColumns(),", ")+ ")");
+ lines.add(line.toString());
+ }
+ builder.append(" " + StringUtils.join(lines, ",\n "));
+ builder.append("\n)\n");
+ logger.info(builder.toString());
+
+ Statement stmt = null;
+ try{
+ stmt = connection.createStatement();
+ stmt.execute(builder.toString());
+
+ // indexes
+ for (IndexData index : indexes){
+ String indexColumns = StringUtils.join(index.getColumns(),", ");
+ String prefix = templates.getCreateIndex();
+ if (index.isUnique()){
+ prefix = templates.getCreateUniqueIndex();
+ }
+ String sql = prefix + index.getName() + templates.getOn() + table + "(" + indexColumns+ ")";
+ logger.info(sql);
+ stmt.execute(sql);
+ }
+ } catch (SQLException e) {
+ throw new QueryException(e.getMessage(), e);
+ }finally{
+ if (stmt != null){
+ try {
+ stmt.close();
+ } catch (SQLException e) {
+ throw new QueryException(e);
+ }
+ }
+ }
+ }
+
+}
View
52 querydsl-sql/src/test/java/com/mysema/query/ddl/DropTableClause.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2010 Mysema Ltd.
+ * All rights reserved.
+ *
+ */
+package com.mysema.query.ddl;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import com.mysema.query.QueryException;
+import com.mysema.query.sql.SQLTemplates;
+
+/**
+ * DropTableClause defines a DROP TABLE clause
+ *
+ * @author tiwe
+ *
+ */
+public class DropTableClause {
+
+ private final Connection connection;
+
+ private final String table;
+
+ public DropTableClause(Connection conn, SQLTemplates templates, String table) {
+ this.connection = conn;
+ this.table = templates.quoteIdentifier(table);
+ }
+
+ @SuppressWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
+ public void execute(){
+ Statement stmt = null;
+ try{
+ stmt = connection.createStatement();
+ stmt.execute("DROP TABLE " + table);
+ } catch (SQLException e) {
+ // do not rethrow
+ }finally{
+ if (stmt != null){
+ try {
+ stmt.close();
+ } catch (SQLException e) {
+ throw new QueryException(e);
+ }
+ }
+ }
+ }
+
+}
+
View
49 querydsl-sql/src/test/java/com/mysema/query/ddl/ForeignKeyBuilder.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2010 Mysema Ltd.
+ * All rights reserved.
+ *
+ */
+package com.mysema.query.ddl;
+
+import java.util.List;
+
+import com.mysema.query.sql.SQLTemplates;
+
+/**
+ * ForeignKeyBuilder is part of the fluent interface of CreateTableClause
+ *
+ * @author tiwe
+ *
+ */
+public class ForeignKeyBuilder {
+
+ private final List<ForeignKeyData> foreignKeys;
+
+ private final CreateTableClause clause;
+
+ private final String name;
+
+ private final String[] foreignColumns;
+
+ private final SQLTemplates templates;
+
+ public ForeignKeyBuilder(CreateTableClause clause, SQLTemplates templates, List<ForeignKeyData> foreignKeys, String name, String[] columns) {
+ this.clause = clause;
+ this.templates = templates;
+ this.foreignKeys = foreignKeys;
+ this.name = name;
+ this.foreignColumns = columns;
+ }
+
+ public CreateTableClause references(String table, String... parentColumns) {
+ ForeignKeyData foreignKey = new ForeignKeyData(name, templates.quoteIdentifier(table));
+ for (int i = 0; i < parentColumns.length; i++){
+ foreignKey.add(
+ templates.quoteIdentifier(foreignColumns[i]),
+ templates.quoteIdentifier(parentColumns[i]));
+ }
+ foreignKeys.add(foreignKey);
+ return clause;
+ }
+
+}
View
53 querydsl-sql/src/test/java/com/mysema/query/ddl/ForeignKeyData.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2010 Mysema Ltd.
+ * All rights reserved.
+ *
+ */
+package com.mysema.query.ddl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.mysema.commons.lang.Assert;
+
+/**
+ * @author tiwe
+ *
+ */
+public class ForeignKeyData implements KeyData {
+
+ private final String name;
+
+ private final String table;
+
+ private final List<String> foreignColumns = new ArrayList<String>();
+
+ private final List<String> parentColumns = new ArrayList<String>();
+
+ public ForeignKeyData(String name, String parentTable) {
+ this.name = Assert.hasLength(name,"name");
+ this.table = Assert.hasLength(parentTable,"parentTable");
+ }
+
+ public void add(String foreignColumn, String parentColumn){
+ foreignColumns.add(foreignColumn);
+ parentColumns.add(parentColumn);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getTable() {
+ return table;
+ }
+
+ public List<String> getForeignColumns() {
+ return foreignColumns;
+ }
+
+ public List<String> getParentColumns() {
+ return parentColumns;
+ }
+
+}
View
42 querydsl-sql/src/test/java/com/mysema/query/ddl/IndexData.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2010 Mysema Ltd.
+ * All rights reserved.
+ *
+ */
+package com.mysema.query.ddl;
+
+/**
+ * @author tiwe
+ *
+ */
+public class IndexData {
+
+ private final String name;
+
+ private final String[] columns;
+
+ private boolean unique;
+
+ public IndexData(String name, String[] columns) {
+ this.name = name;
+ this.columns = columns;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String[] getColumns() {
+ return columns;
+ }
+
+ public boolean isUnique() {
+ return unique;
+ }
+
+ public void setUnique(boolean unique) {
+ this.unique = unique;
+ }
+
+
+}
View
24 querydsl-sql/src/test/java/com/mysema/query/ddl/KeyData.java
@@ -0,0 +1,24 @@
+/*
+ *
+ */
+package com.mysema.query.ddl;
+
+import java.util.List;
+
+/**
+ * Common interface for ForeignKeyData and InverseForeignKeyData
+ *
+ * @author tiwe
+ *
+ */
+public interface KeyData {
+
+ String getName();
+
+ String getTable();
+
+ List<String> getForeignColumns();
+
+ List<String> getParentColumns();
+
+}
View
46 querydsl-sql/src/test/java/com/mysema/query/ddl/PrimaryKeyData.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2010 Mysema Ltd.
+ * All rights reserved.
+ *
+ */
+package com.mysema.query.ddl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.mysema.commons.lang.Assert;
+
+/**
+ * @author tiwe
+ *
+ */
+public class PrimaryKeyData {
+
+ private final String name;
+
+ private final List<String> columns = new ArrayList<String>();
+
+ public PrimaryKeyData(String name) {
+ this.name = Assert.notNull(name,"name");
+ }
+
+ public PrimaryKeyData(String name, String[] c){
+ this.name = name;
+ for (String column : c){
+ columns.add(column);
+ }
+ }
+
+ public void add(String column){
+ columns.add(column);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public List<String> getColumns() {
+ return columns;
+ }
+
+}
View
2  querydsl-sql/template.mf
@@ -3,7 +3,6 @@ Bundle-Name: Querydsl SQL
Bundle-Vendor: Mysema
Bundle-ManifestVersion: 2
Import-Template:
- com.mysema.codegen.*;version="${codegen.version}",
com.mysema.commons.lang.*;version="${mysema.lang.version}",
com.mysema.query.*;version="${project.version}",
com.mysema.util.*;version="${project.version}",
@@ -13,7 +12,6 @@ Import-Template:
org.apache.commons.io;version=${commons.io.version},
org.apache.commons.collections15.*;version="${commons.collections.version}",
org.apache.commons.lang3.*;version="${commons.lang.version}",
- org.apache.tools.ant.*;version="${ant.version}",
org.joda.time.*;version="1.6",
org.slf4j.*;version="${slf4j.version}"
Please sign in to comment.
Something went wrong with that request. Please try again.