Search in sources :

Example 11 with ClassMetaData

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

the class StatementHelper method getDeleteStatementString.

/**
 * Returns a SQL String for use in Statements for deleting records from the table corresponding to the input Persistent object.
 * @param object The object to be deleted.
 * @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 deleting records.
 */
public static String getDeleteStatementString(IPersistent object, String engineType) throws IllegalAccessException, InvocationTargetException, IOException {
    ClassMetaData classMetaData = getClassMetaData(object);
    StringBuffer buf = new StringBuffer(DELETE_FROM);
    buf.append(' ');
    buf.append(classMetaData.getTable());
    buf.append(' ');
    buf.append(WHERE);
    buf.append(' ');
    boolean first = true;
    for (Iterator i = classMetaData.getAllKeyFieldNames().iterator(); i.hasNext(); ) {
        if (first) {
            first = false;
        } else {
            buf.append(' ');
            buf.append(AND);
            buf.append(' ');
        }
        String attributeName = (String) i.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, attributeName);
        buf.append(formatSqlName(classMetaData.getSqlName(attributeName), engineType));
        if (value == null)
            buf.append(' ').append(IS_NULL);
        else
            buf.append('=').append(DataTranslator.getDml(value, classMetaData.getSqlType(attributeName), engineType));
    }
    return buf.toString();
}
Also used : Iterator(java.util.Iterator) ClassMetaData(org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)

Example 12 with ClassMetaData

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

the class StatementHelper method getUpdateStatementString.

/**
 * Returns a SQL String for use in Statements for updating records in the table corresponding to the input Persistent object.
 * @param object The object to be updated.
 * @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 updating records.
 */
public static String getUpdateStatementString(IPersistent object, String engineType) throws IllegalAccessException, InvocationTargetException, IOException {
    ClassMetaData classMetaData = getClassMetaData(object);
    StringBuffer buf = new StringBuffer(UPDATE);
    buf.append(' ');
    buf.append(classMetaData.getTable());
    buf.append(' ');
    buf.append(SET);
    buf.append(' ');
    boolean first = true;
    for (Iterator i = classMetaData.getAttributes().iterator(); i.hasNext(); ) {
        if (first) {
            first = false;
        } else {
            buf.append(',');
        }
        String attributeName = (String) i.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, attributeName);
        buf.append(formatSqlName(classMetaData.getSqlName(attributeName), engineType));
        buf.append('=');
        buf.append(DataTranslator.getDml(value, classMetaData.getSqlType(attributeName), engineType));
    }
    buf.append(' ');
    buf.append(WHERE);
    buf.append(' ');
    first = true;
    for (Iterator i = classMetaData.getAllKeyFieldNames().iterator(); i.hasNext(); ) {
        if (first) {
            first = false;
        } else {
            buf.append(' ');
            buf.append(AND);
            buf.append(' ');
        }
        String attributeName = (String) i.next();
        Object value = MoldingService.getInstanceValue(object, classMetaData, attributeName);
        buf.append(formatSqlName(classMetaData.getSqlName(attributeName), engineType));
        if (value == null)
            buf.append(' ').append(IS_NULL);
        else
            buf.append('=').append(DataTranslator.getDml(value, classMetaData.getSqlType(attributeName), engineType));
    }
    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