Search in sources :

Example 11 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class CreateTableClause method execute.

/**
     * 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(" + COMMA_JOINER.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(" + COMMA_JOINER.join(foreignKey.getForeignColumns()) + ") ");
        line.append("REFERENCES " + foreignKey.getTable() + "(" + COMMA_JOINER.join(foreignKey.getParentColumns()) + ")");
        lines.add(line.toString());
    }
    builder.append("  " + Joiner.on(",\n  ").join(lines));
    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 = COMMA_JOINER.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) {
        System.err.println(builder.toString());
        throw new QueryException(e.getMessage(), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new QueryException(e);
            }
        }
    }
}
Also used : QueryException(com.querydsl.core.QueryException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList)

Example 12 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class DropTableClause method execute.

@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);
            }
        }
    }
}
Also used : QueryException(com.querydsl.core.QueryException) SQLException(java.sql.SQLException) Statement(java.sql.Statement)

Aggregations

QueryException (com.querydsl.core.QueryException)12 SQLException (java.sql.SQLException)5 Field (java.lang.reflect.Field)3 Path (com.querydsl.core.types.Path)2 RelationalPath (com.querydsl.sql.RelationalPath)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 Column (com.querydsl.sql.Column)1 IntrospectionException (java.beans.IntrospectionException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Query (org.hibernate.Query)1 Test (org.junit.Test)1