Search in sources :

Example 21 with PersistentField

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

the class IEngineImpl method executeTransaction.

private void executeTransaction(final Transaction transaction, Connection connection) throws SQLException {
    for (Persistent key : transaction.getUpdate()) {
        PersistentRow row = metadata.get(key.getClass());
        PersistentWrapper wrapper = wrappers.get(key.getClass());
        StringBuffer sb = new StringBuffer("UPDATE " + row.getTableName() + " SET ");
        boolean first = true;
        List<Object> list = new ArrayList<Object>(5);
        for (PersistentField field : row.getDataFields()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append(field.getDatabaseName());
            sb.append("=?");
            list.add(wrapper.getField(key, field.getName()));
        }
        sb.append(" WHERE ");
        first = true;
        for (PersistentField field : row.getKeyFields()) {
            if (first) {
                first = false;
            } else {
                sb.append(" AND ");
            }
            sb.append(field.getDatabaseName());
            sb.append("=?");
            list.add(wrapper.getField(key, field.getName()));
        }
        PreparedStatement ps = template.getPreparedStatement(sb.toString(), true);
        template.setParams(ps, list.toArray(new Object[list.size()]));
        ps.execute();
    // ps.close();
    }
    for (Persistent key : transaction.getDelete()) {
        PersistentRow row = metadata.get(key.getClass());
        PersistentWrapper wrapper = wrappers.get(key.getClass());
        StringBuffer sb = new StringBuffer("DELETE FROM " + row.getTableName() + " WHERE ");
        boolean first = true;
        first = true;
        List<Object> list = new ArrayList<Object>(2);
        for (PersistentField field : row.getKeyFields()) {
            if (first) {
                first = false;
            } else {
                sb.append("AND ");
            }
            sb.append(field.getDatabaseName());
            sb.append("=?");
            list.add(wrapper.getField(key, field.getName()));
        }
        PreparedStatement ps = template.getPreparedStatement(sb.toString(), // connection.prepareStatement(sb.toString());
        true);
        template.setParams(ps, list.toArray(new Object[list.size()]));
        ps.execute();
    // ps.close();
    }
    for (Persistent key : transaction.getSave()) {
        PersistentRow row = metadata.get(key.getClass());
        PersistentWrapper wrapper = wrappers.get(key.getClass());
        StringBuffer sb = new StringBuffer("INSERT INTO " + row.getTableName() + "(");
        boolean first = true;
        List<Object> list = new ArrayList<Object>(5);
        for (PersistentField field : row.getFields()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append(field.getDatabaseName());
            list.add(wrapper.getField(key, field.getName()));
        }
        sb.append(") VALUES(");
        first = true;
        for (int i = 0; i < list.size(); i++) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append("?");
        }
        sb.append(")");
        PreparedStatement ps = template.getPreparedStatement(sb.toString(), // connection.prepareStatement(sb.toString());
        true);
        template.setParams(ps, list.toArray(new Object[list.size()]));
        ps.execute();
    // ps.close();
    }
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) FindObject(com.ramussoft.common.attribute.FindObject) PreparedStatement(java.sql.PreparedStatement) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 22 with PersistentField

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

the class UniversalPersistentFactory method updateInTransaction.

public void updateInTransaction(Object object, final 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;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (field.isPrimary()) {
            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();
    PreparedStatement ps = template.getPreparedStatement(sql, cached);
    synchronized (ps) {
        template.setParams(ps, objects);
        ps.execute();
        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 23 with PersistentField

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

the class UniversalPersistentFactory method delete.

public void delete(Object object) {
    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("DELETE FROM " + row.getTableName() + " 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());
    }
    String sql = delete.toString();
    template.update(sql, objects, true);
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 24 with PersistentField

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

the class UniversalPersistentFactory method update.

public Object update(Object object, boolean cached, JDBCCallback callback, Class clazz) {
    if (object instanceof List)
        return updateList((List) object, cached, callback);
    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;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (field.isPrimary()) {
            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();
    return template.update(sql, objects, cached, callback);
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ArrayList(java.util.ArrayList) List(java.util.List) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 25 with PersistentField

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

the class BranchesPersistentFactory method getObject.

public Object getObject(Object primaryKey) {
    Class clazz = primaryKey.getClass();
    long id = getActiveBranchId();
    PersistentRow row = rowHash.get(clazz);
    PersistentWrapper wrapper = rowWrappers.get(clazz);
    List<PersistentField> fields = row.getKeyFields();
    Object[] objects = new Object[fields.size()];
    StringBuffer select = new StringBuffer("SELECT * FROM " + row.getTableName() + " WHERE ");
    boolean first = true;
    for (int i = 0; i < objects.length; i++) {
        PersistentField field = fields.get(i);
        if (first)
            first = false;
        else {
            select.append(" AND ");
        }
        select.append(field.getDatabaseName());
        select.append("=?");
        objects[i] = wrapper.getField(primaryKey, field.getName());
    }
    select.append("AND");
    select.append(getSQLBranchCondition());
    objects = addBranchWhereObjects(id, objects);
    return template.queryForObjects(select.toString(), new Mapper(clazz), objects, true);
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) 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