Search in sources :

Example 1 with QueryExpression

use of org.sql.generation.api.grammar.query.QueryExpression in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method isReindexingNeeded.

// This method assume that the schema exists
private Boolean isReindexingNeeded(Connection connection) throws SQLException {
    Boolean result = true;
    String schemaName = this._state.schemaName().get();
    Statement stmt = connection.createStatement();
    try {
        QueryExpression getAppVersionQuery = this._vendor.getQueryFactory().simpleQueryBuilder().select(APP_VERSION_PK_COLUMN_NAME).from(this._vendor.getTableReferenceFactory().tableName(schemaName, APP_VERSION_TABLE_NAME)).createExpression();
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(this._vendor.toString(getAppVersionQuery));
        } catch (SQLException sqle) {
        // Sometimes meta data claims table exists, even when it really doesn't exist
        }
        if (rs != null) {
            result = !rs.next();
            if (!result) {
                String dbAppVersion = rs.getString(1);
                if (this._reindexingStrategy != null) {
                    result = this._reindexingStrategy.reindexingNeeded(dbAppVersion, this._app.version());
                }
            }
        }
    } finally {
        SQLUtil.closeQuietly(stmt);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) InsertStatement(org.sql.generation.api.grammar.modification.InsertStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) QueryExpression(org.sql.generation.api.grammar.query.QueryExpression)

Example 2 with QueryExpression

use of org.sql.generation.api.grammar.query.QueryExpression in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method readAppVersionFromDB.

private String readAppVersionFromDB(Connection connection, String schemaName) throws SQLException {
    Statement stmt = connection.createStatement();
    String result = null;
    try {
        QueryExpression getAppVersionQuery = this._vendor.getQueryFactory().simpleQueryBuilder().select(APP_VERSION_PK_COLUMN_NAME).from(this._vendor.getTableReferenceFactory().tableName(schemaName, APP_VERSION_TABLE_NAME)).createExpression();
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(getAppVersionQuery.toString());
            if (rs.next()) {
                result = rs.getString(1);
            }
        } catch (SQLException sqle) {
        // Sometimes meta data claims table exists, even when it really doesn't exist
        } finally {
            SQLUtil.closeQuietly(rs);
        }
    } finally {
        SQLUtil.closeQuietly(stmt);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) InsertStatement(org.sql.generation.api.grammar.modification.InsertStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) QueryExpression(org.sql.generation.api.grammar.query.QueryExpression)

Example 3 with QueryExpression

use of org.sql.generation.api.grammar.query.QueryExpression in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method constructQuery.

@Override
public //
String constructQuery(//
Class<?> resultType, //
Specification<Composite> whereClause, //
OrderBy[] orderBySegments, //
Integer firstResult, //
Integer maxResults, //
Map<String, Object> variables, //
List<Object> values, //
List<Integer> valueSQLTypes, //
Boolean countOnly) throws EntityFinderException {
    SQLVendor vendor = this.descriptor.metaInfo(SQLVendor.class);
    QueryFactory q = vendor.getQueryFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    ColumnReference mainColumn = c.colName(TABLE_NAME_PREFIX + "0", DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME);
    if (countOnly) {
        mainColumn = c.colExp(l.func(SQLFunctions.COUNT, mainColumn));
    }
    QueryBuilder innerBuilder = this.processBooleanExpression(whereClause, false, vendor, this.createTypeCondition(resultType, vendor), variables, values, valueSQLTypes);
    QuerySpecificationBuilder mainQuery = q.querySpecificationBuilder();
    mainQuery.getSelect().addUnnamedColumns(mainColumn);
    mainQuery.getFrom().addTableReferences(t.tableBuilder(t.table(q.createQuery(innerBuilder.createExpression()), t.tableAlias(TABLE_NAME_PREFIX + "0"))));
    this.processOrderBySegments(orderBySegments, vendor, mainQuery);
    QueryExpression finalMainQuery = this.finalizeQuery(vendor, mainQuery, resultType, whereClause, orderBySegments, firstResult, maxResults, variables, values, valueSQLTypes, countOnly);
    String result = vendor.toString(finalMainQuery);
    LOGGER.info("SQL query:\n" + result);
    return result;
}
Also used : TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) QueryFactory(org.sql.generation.api.grammar.factories.QueryFactory) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) QuerySpecificationBuilder(org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder) SQLVendor(org.sql.generation.api.vendor.SQLVendor) QueryBuilder(org.sql.generation.api.grammar.builders.query.QueryBuilder) QueryExpression(org.sql.generation.api.grammar.query.QueryExpression) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory) ColumnReference(org.sql.generation.api.grammar.query.ColumnReference)

Aggregations

QueryExpression (org.sql.generation.api.grammar.query.QueryExpression)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 InsertStatement (org.sql.generation.api.grammar.modification.InsertStatement)2 QueryBuilder (org.sql.generation.api.grammar.builders.query.QueryBuilder)1 QuerySpecificationBuilder (org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder)1 ColumnsFactory (org.sql.generation.api.grammar.factories.ColumnsFactory)1 LiteralFactory (org.sql.generation.api.grammar.factories.LiteralFactory)1 QueryFactory (org.sql.generation.api.grammar.factories.QueryFactory)1 TableReferenceFactory (org.sql.generation.api.grammar.factories.TableReferenceFactory)1 ColumnReference (org.sql.generation.api.grammar.query.ColumnReference)1 SQLVendor (org.sql.generation.api.vendor.SQLVendor)1