Search in sources :

Example 6 with ClassMetaData

use of org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData in project jaffa-framework by jaffa-projects.

the class JdbcBridge method executeDeleteWithPreparedStatement.

private static void executeDeleteWithPreparedStatement(IPersistent object, DataSource dataSource) throws SQLException, IllegalAccessException, InvocationTargetException {
    ClassMetaData classMetaData = ConfigurationService.getInstance().getMetaData(PersistentInstanceFactory.getActualPersistentClass(object).getName());
    String sql = PreparedStatementHelper.getDeletePreparedStatementString(classMetaData, dataSource.getEngineType());
    PreparedStatement pstmt = dataSource.getPreparedStatement(sql);
    int i = 0;
    for (Iterator itr = classMetaData.getAllKeyFieldNames().iterator(); itr.hasNext(); ) {
        ++i;
        String fieldName = (String) itr.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, fieldName);
        DataTranslator.setAppObject(pstmt, i, value, classMetaData.getSqlType(fieldName), dataSource.getEngineType());
    }
    dataSource.executeUpdate(pstmt);
}
Also used : PreparedStatement(java.sql.PreparedStatement) ClassMetaData(org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)

Example 7 with ClassMetaData

use of org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData in project jaffa-framework by jaffa-projects.

the class JdbcBridge method executeAddWithPreparedStatement.

private static void executeAddWithPreparedStatement(IPersistent object, DataSource dataSource) throws SQLException, IllegalAccessException, InvocationTargetException {
    ClassMetaData classMetaData = ConfigurationService.getInstance().getMetaData(PersistentInstanceFactory.getActualPersistentClass(object).getName());
    String sql = PreparedStatementHelper.getInsertPreparedStatementString(classMetaData, dataSource.getEngineType());
    PreparedStatement pstmt = dataSource.getPreparedStatement(sql);
    int i = 0;
    for (Iterator itr = classMetaData.getNonAutoKeyFieldNames().iterator(); itr.hasNext(); ) {
        ++i;
        String fieldName = (String) itr.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, fieldName);
        DataTranslator.setAppObject(pstmt, i, value, classMetaData.getSqlType(fieldName), dataSource.getEngineType());
    }
    for (Iterator itr = classMetaData.getAttributes().iterator(); itr.hasNext(); ) {
        ++i;
        String fieldName = (String) itr.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, fieldName);
        DataTranslator.setAppObject(pstmt, i, value, classMetaData.getSqlType(fieldName), dataSource.getEngineType());
    }
    dataSource.executeUpdate(pstmt);
}
Also used : PreparedStatement(java.sql.PreparedStatement) ClassMetaData(org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)

Example 8 with ClassMetaData

use of org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData in project jaffa-framework by jaffa-projects.

the class JdbcBridge method executeLockWithPreparedStatement.

private static void executeLockWithPreparedStatement(IPersistent object, DataSource dataSource) throws SQLException, IllegalAccessException, InvocationTargetException {
    ClassMetaData classMetaData = ConfigurationService.getInstance().getMetaData(PersistentInstanceFactory.getActualPersistentClass(object).getName());
    String sql = PreparedStatementHelper.getLockPreparedStatementString(classMetaData, dataSource.getEngineType());
    PreparedStatement pstmt = dataSource.getPreparedStatement(sql);
    int i = 0;
    for (Iterator itr = classMetaData.getAllKeyFieldNames().iterator(); itr.hasNext(); ) {
        ++i;
        String fieldName = (String) itr.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, fieldName);
        DataTranslator.setAppObject(pstmt, i, value, classMetaData.getSqlType(fieldName), dataSource.getEngineType());
    }
    // Added for MS-Sql-Server as 'NO-WAIT" is not implemented like in Oracle
    pstmt.setQueryTimeout(QUERY_TIMEOUT_FOR_LOCKING);
    dataSource.executeUpdate(pstmt);
}
Also used : PreparedStatement(java.sql.PreparedStatement) ClassMetaData(org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)

Example 9 with ClassMetaData

use of org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData in project jaffa-framework by jaffa-projects.

the class QueryStatementHelper method getSQLInternal.

/**
 * This parses a <code>Criteria</code> object for constructing relevant SQL.
 * @param criteria the object containing query data.
 * @param engineType The engine type as defined in init.xml
 * @param pagingPlugin The IPagingPlugin implementation that may be used to return a page of Results.
 * @param psArguments If a PreparedStatement is to be generated, pass an empty List. This will be filled with instances of PreparedStatementArgument.
 * @throws IOException if any error occurs while extracting the String from the criteria.
 * @return a String representing SQL statement.
 */
private static String getSQLInternal(Criteria criteria, String engineType, IPagingPlugin pagingPlugin, List<PreparedStatementArgument> psArguments, boolean indexArguments) throws IOException {
    ClassMetaData meta = criteria.getTable() != null ? ConfigurationService.getInstance().getMetaData(criteria.getTable()) : null;
    // This buffer will hold the SQL
    StringBuffer buf = new StringBuffer(SELECT);
    // This buffer will hold a wrapper SQL when using functions against a JOIN criteria.
    // In such a scenario, buf will contain the SQL with the JOIN criteria,
    // while the functionWrapperBuf will contain a wrapping SQL of the type:
    // SELECT f1, count(f2), max(f3) from (SELECT distinct f1, f2, f3, f4 from T1, T2 WHERE ....) GROUP BY f1
    StringBuffer functionWrapperBuf = null;
    if (criteria.getGroupBys() != null || criteria.getFunctionEntries() != null) {
        // Initialize the functionWrapperBuf only against a JOIN criteria
        if ((criteria.getAggregates() != null && criteria.getAggregates().size() > 0) || hasAtomicAggregates(criteria))
            functionWrapperBuf = new StringBuffer(SELECT);
        // Add the fields specified in the group-by clause and the function-list
        StringBuffer fieldBuf = new StringBuffer();
        if (criteria.getGroupBys() != null) {
            String groupBySelectFieldList = getGroupBySelectFieldList(criteria, meta, engineType);
            if (groupBySelectFieldList != null && groupBySelectFieldList.length() > 0)
                fieldBuf.append(groupBySelectFieldList);
        }
        if (criteria.getFunctionEntries() != null) {
            String functionList = getFunctionList(criteria, meta, engineType);
            if (functionList != null && functionList.length() > 0) {
                if (fieldBuf.length() > 0)
                    fieldBuf.append(',');
                fieldBuf.append(functionList);
            }
        }
        if (fieldBuf.length() > 0) {
            StringBuffer targetBuf = functionWrapperBuf != null ? functionWrapperBuf : buf;
            targetBuf.append(' ').append(fieldBuf);
        }
        // Just add the optional FROM clause and return
        if (criteria.getTable() == null) {
            StringBuffer targetBuf = functionWrapperBuf != null ? functionWrapperBuf : buf;
            String deftableName = Variant.getProperty(engineType, Variant.PROP_DEFAULT_FUNCTION_TABLE_NAME);
            if (deftableName != null && deftableName.length() > 0)
                targetBuf.append(' ').append(FROM).append(' ').append(deftableName);
            return targetBuf.toString();
        }
    }
    if (functionWrapperBuf != null || (criteria.getGroupBys() == null && criteria.getFunctionEntries() == null)) {
        // This will ensure distinct records are returned during JOINs
        if ((criteria.getAggregates() != null && criteria.getAggregates().size() > 0) || hasAtomicAggregates(criteria))
            buf.append(' ').append(DISTINCT);
        // Add the field-list
        buf.append(' ').append(getFieldList(meta));
    }
    // Create 2 buffers for holding the From and Where clauses.
    // And then invoke the recursive 'parse' routine to fill up the 2 buffers
    StringBuffer fromBuf = new StringBuffer();
    StringBuffer whereBuf = new StringBuffer();
    doFromAndWhere(criteria, meta, null, null, null, new Counter(), fromBuf, whereBuf, engineType, psArguments, indexArguments);
    // Add the From clause
    if (fromBuf.length() > 0)
        buf.append(' ').append(FROM).append(' ').append(fromBuf);
    // Added for supplying Locking 'Hints' used by MS-Sql-Server
    if (criteria.getLocking() == Criteria.LOCKING_PARANOID && meta.isLockable())
        buf.append(' ').append(Variant.getProperty(engineType, Variant.PROP_LOCK_CONSTRUCT_IN_FROM_SELECT_STATEMENT));
    // Add the Where clause
    if (whereBuf.length() > 0)
        buf.append(' ').append(WHERE).append(' ').append(whereBuf);
    // Add the GroupBy clause, but only if functionWrapperBuf has not been initialized
    if (criteria.getGroupBys() != null && functionWrapperBuf == null) {
        String groupByFieldList = getGroupByFieldList(criteria, meta, engineType);
        if (groupByFieldList != null && groupByFieldList.length() > 0)
            buf.append(' ').append(GROUP_BY).append(' ').append(groupByFieldList);
    }
    // Append the ordering information, if any
    Collection orderBys = criteria.getOrderBys();
    if (orderBys != null && orderBys.size() > 0) {
        StringBuffer orderByBuf = new StringBuffer();
        for (Iterator itr = orderBys.iterator(); itr.hasNext(); ) {
            Criteria.OrderBy orderBy = (Criteria.OrderBy) itr.next();
            String sqlName = formatSqlName(meta.getSqlName(orderBy.getOrderByElement()), engineType);
            if (sqlName != null) {
                if (orderByBuf.length() > 0)
                    orderByBuf.append(',');
                orderByBuf.append(meta.getTable()).append('.').append(sqlName);
                if (orderBy.getOrdering() == Criteria.ORDER_BY_ASC)
                    orderByBuf.append(' ').append(ASC);
                else
                    orderByBuf.append(' ').append(DESC);
            } else if (criteria.getFunctionEntries() != null) {
                for (Iterator i = criteria.getFunctionEntries().iterator(); i.hasNext(); ) {
                    Criteria.FunctionEntry fe = (Criteria.FunctionEntry) i.next();
                    if (orderBy.getOrderByElement().equals(fe.getId())) {
                        if (orderByBuf.length() > 0)
                            orderByBuf.append(',');
                        orderByBuf.append(orderBy.getOrderByElement());
                        if (orderBy.getOrdering() == Criteria.ORDER_BY_ASC)
                            orderByBuf.append(' ').append(ASC);
                        else
                            orderByBuf.append(' ').append(DESC);
                        break;
                    }
                }
            } else if (log.isDebugEnabled())
                log.debug("Will ignore the unmapped OrderByElement " + meta.getTable() + '.' + orderBy.getOrderByElement());
        }
        if (orderByBuf.length() > 0)
            buf.append(' ').append(ORDER_BY).append(' ').append(orderByBuf);
    }
    // check the locking strategy
    if (criteria.getLocking() == Criteria.LOCKING_PARANOID && meta.isLockable()) {
        buf.append(' ');
        buf.append(Variant.getProperty(engineType, Variant.PROP_LOCK_CONSTRUCT_IN_SELECT_STATEMENT));
    }
    // build the functionWrapperBuf
    if (functionWrapperBuf != null) {
        functionWrapperBuf.append(' ').append(FROM).append(' ').append('(').append(buf).append(')').append(' ').append(meta.getTable());
        // Add the GroupBy clause
        if (criteria.getGroupBys() != null) {
            String groupByFieldList = getGroupByFieldList(criteria, meta, engineType);
            if (groupByFieldList != null && groupByFieldList.length() > 0)
                functionWrapperBuf.append(' ').append(GROUP_BY).append(' ').append(groupByFieldList);
        }
    }
    String sql = functionWrapperBuf != null ? functionWrapperBuf.toString() : buf.toString();
    if (pagingPlugin != null)
        sql = pagingPlugin.preQuery(sql);
    return sql;
}
Also used : AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria) ClassMetaData(org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)

Example 10 with ClassMetaData

use of org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData in project jaffa-framework by jaffa-projects.

the class StatementHelper method getInsertStatementString.

/**
 * Returns a SQL String for use in Statements for inserting records into the table corresponding to the input Persistent object.
 * @param object The object to be inserted.
 * @param engineType The engine type as defined in init.xml
 * @throws IllegalAccessException if the accessor Method for an attribute enforces Java language access control and the underlying method is inaccessible.
 * @throws InvocationTargetException if the accessor method for an attribute throws an exception.
 * @throws IOException if any error occurs while extracting the value for an attribute.
 * @return a SQL String for use in Statements for inserting records.
 */
public static String getInsertStatementString(IPersistent object, String engineType) throws IllegalAccessException, InvocationTargetException, IOException {
    ClassMetaData classMetaData = getClassMetaData(object);
    // the fieldlist buffer
    StringBuffer buf1 = new StringBuffer();
    // the fieldlist buffer
    buf1.append('(');
    // the Values buffer
    StringBuffer buf2 = new StringBuffer();
    // the Values buffer
    buf2.append('(');
    boolean first = true;
    for (Iterator i = classMetaData.getNonAutoKeyFieldNames().iterator(); i.hasNext(); ) {
        if (first) {
            first = false;
        } else {
            buf1.append(',');
            buf2.append(',');
        }
        String attributeName = (String) i.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, attributeName);
        buf1.append(formatSqlName(classMetaData.getSqlName(attributeName), engineType));
        buf2.append(DataTranslator.getDml(value, classMetaData.getSqlType(attributeName), engineType));
    }
    for (Iterator i = classMetaData.getAttributes().iterator(); i.hasNext(); ) {
        if (first) {
            first = false;
        } else {
            buf1.append(',');
            buf2.append(',');
        }
        String attributeName = (String) i.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, attributeName);
        buf1.append(formatSqlName(classMetaData.getSqlName(attributeName), engineType));
        buf2.append(DataTranslator.getDml(value, classMetaData.getSqlType(attributeName), engineType));
    }
    buf1.append(')');
    buf2.append(')');
    StringBuffer buf = new StringBuffer(INSERT_INTO);
    buf.append(' ');
    buf.append(classMetaData.getTable());
    buf.append(buf1);
    buf.append(' ');
    buf.append(VALUES);
    buf.append(buf2);
    return buf.toString();
}
Also used : Iterator(java.util.Iterator) ClassMetaData(org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)

Aggregations

ClassMetaData (org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)12 PreparedStatement (java.sql.PreparedStatement)5 Iterator (java.util.Iterator)4 Criteria (org.jaffa.persistence.Criteria)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 CallableStatement (java.sql.CallableStatement)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)1 IPersistent (org.jaffa.persistence.IPersistent)1 IPagingPlugin (org.jaffa.persistence.engines.jdbcengine.paging.IPagingPlugin)1