Search in sources :

Example 26 with PersistentField

use of com.ramussoft.common.persistent.PersistentField in project ramus by Vitaliy-Yakovchuk.

the class BranchesPersistentFactory method updateInTransaction.

@Override
public void updateInTransaction(Object object, boolean cached, Class clazz) throws SQLException {
    PersistentRow row = rowHash.get(clazz);
    PersistentWrapper wrapper = rowWrappers.get(clazz);
    List<PersistentField> fields = row.getFields();
    Object[] objects = new Object[fields.size()];
    StringBuffer set = new StringBuffer("UPDATE " + row.getTableName() + " SET ");
    StringBuffer key = new StringBuffer(" WHERE ");
    boolean firstP = true;
    boolean firstS = true;
    int k = 0;
    int keyCount = 0;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (field.isPrimary()) {
            keyCount++;
            if (firstP)
                firstP = false;
            else
                key.append(" AND ");
        } else {
            if (firstS)
                firstS = false;
            else
                set.append(", ");
        }
        if (field.isPrimary()) {
            key.append(field.getDatabaseName());
            key.append("=?");
        } else {
            set.append(field.getDatabaseName());
            set.append("=?");
        }
        if (!field.isPrimary()) {
            objects[k] = wrapper.getField(object, field.getName());
            k++;
        }
    }
    for (int i = 0; i < objects.length; i++) if (fields.get(i).isPrimary()) {
        objects[k] = wrapper.getField(object, fields.get(i).getName());
        k++;
    }
    String sql = set.toString() + key.toString() + " AND created_branch_id=?";
    PreparedStatement ps = template.getPreparedStatement(sql, cached);
    synchronized (ps) {
        template.setParams(ps, objects);
        long id = getActiveBranchId();
        ps.setLong(objects.length + 1, id);
        if (ps.executeUpdate() == 0) {
            if (!cached)
                ps.close();
            sql = "UPDATE " + row.getTableName() + " SET removed_branch_id=?" + key.toString() + " AND " + getSQLBranchCondition();
            ps = template.getPreparedStatement(sql, cached);
            ps.setLong(1, id);
            for (int i = objects.length - keyCount; i < objects.length; i++) {
                template.setParam(ps, i + 2 - objects.length + keyCount, objects[i]);
            }
            Object[] objects2 = addBranchWhereObjects(id, new Object[] {});
            for (int i = 0; i < objects2.length; i++) template.setParam(ps, keyCount + 2 + i, objects2[i]);
            ps.executeUpdate();
            if (!cached)
                ps.close();
            StringBuffer insert = new StringBuffer("INSERT INTO " + row.getTableName() + " (");
            StringBuffer values = new StringBuffer(") VALUES (");
            boolean first = true;
            for (int i = 0; i < objects.length; i++) {
                PersistentField field = fields.get(i);
                if (first)
                    first = false;
                else {
                    insert.append(", ");
                    values.append(", ");
                }
                insert.append(field.getDatabaseName());
                values.append('?');
                objects[i] = wrapper.getField(object, field.getName());
            }
            insert.append(", created_branch_id");
            values.append(", ?");
            sql = insert.toString() + values.toString() + ")";
            ps = template.getPreparedStatement(sql, cached);
            template.setParams(ps, objects);
            ps.setLong(objects.length + 1, id);
            ps.executeUpdate();
            if (!cached)
                ps.close();
        } else if (!cached)
            ps.close();
    }
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PreparedStatement(java.sql.PreparedStatement) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 27 with PersistentField

use of com.ramussoft.common.persistent.PersistentField in project ramus by Vitaliy-Yakovchuk.

the class BranchesPersistentFactory method deleteInTransaction.

@Override
public void deleteInTransaction(Object object) throws SQLException {
    Class clazz = object.getClass();
    PersistentRow row = rowHash.get(clazz);
    PersistentWrapper wrapper = rowWrappers.get(clazz);
    List<PersistentField> fields = row.getKeyFields();
    Object[] objects = new Object[fields.size()];
    StringBuffer delete = new StringBuffer("UPDATE " + row.getTableName() + " SET removed_branch_id=? WHERE ");
    boolean first = true;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (first)
            first = false;
        else {
            delete.append(" AND ");
        }
        delete.append(field.getDatabaseName());
        delete.append("=?");
        objects[i] = wrapper.getField(object, field.getName());
    }
    delete.append(" AND ");
    delete.append(getSQLBranchCondition());
    String sql = delete.toString();
    PreparedStatement ps = template.getPreparedStatement(sql, true);
    long id = getActiveBranchId();
    ps.setLong(1, id);
    for (int i = 0; i < objects.length; i++) template.setParam(ps, i + 2, objects[i]);
    Object[] objects2 = addBranchWhereObjects(id, new Object[] {});
    for (int i = 0; i < objects2.length; i++) {
        template.setParam(ps, objects.length + i + 2, objects2[i]);
    }
    ps.executeUpdate();
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PreparedStatement(java.sql.PreparedStatement) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Aggregations

PersistentField (com.ramussoft.common.persistent.PersistentField)27 PersistentRow (com.ramussoft.common.persistent.PersistentRow)25 PersistentWrapper (com.ramussoft.common.persistent.PersistentWrapper)22 ArrayList (java.util.ArrayList)11 Persistent (com.ramussoft.common.persistent.Persistent)9 SQLException (java.sql.SQLException)8 PreparedStatement (java.sql.PreparedStatement)7 List (java.util.List)7 FindObject (com.ramussoft.common.attribute.FindObject)6 RowMapper (com.ramussoft.jdbc.RowMapper)5 ResultSet (java.sql.ResultSet)5 AttributePlugin (com.ramussoft.common.attribute.AttributePlugin)4 Attribute (com.ramussoft.common.Attribute)3 Attribute (com.ramussoft.common.attribute.Attribute)2 Binary (com.ramussoft.common.persistent.Binary)2 Date (com.ramussoft.common.persistent.Date)2 Element (com.ramussoft.common.persistent.Element)2 Id (com.ramussoft.common.persistent.Id)2 Qualifier (com.ramussoft.common.persistent.Qualifier)2 Table (com.ramussoft.common.persistent.Table)2