Search in sources :

Example 21 with PersistentRow

use of com.ramussoft.common.persistent.PersistentRow 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 PersistentRow

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

the class PersistentFactory method load.

@SuppressWarnings("unchecked")
protected void load() throws SQLException {
    List list = template.query("SELECT * FROM " + prefix + "persistents", new RowMapper() {

        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            PersistentRow row = new PersistentRow();
            row.setStatus(LOADED);
            row.setClassName(rs.getString("CLASS_NAME").trim());
            row.setExists(rs.getBoolean("PERSISTENT_EXISTS"));
            row.setId((int) rs.getLong("PERSISTENT_ID"));
            row.setTableName(rs.getString("TABLE_NAME").trim());
            row.setTableType(rs.getInt("TABLE_TYPE"));
            row.setPluginName(rs.getString("PLUGIN_NAME").trim());
            row.setTypeName(rs.getString("TYPE_NAME").trim());
            row.setFields(c(loadFields(row.getId())));
            return row;
        }
    });
    rows = list;
}
Also used : SQLException(java.sql.SQLException) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) RowMapper(com.ramussoft.jdbc.RowMapper)

Example 23 with PersistentRow

use of com.ramussoft.common.persistent.PersistentRow 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 24 with PersistentRow

use of com.ramussoft.common.persistent.PersistentRow 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 25 with PersistentRow

use of com.ramussoft.common.persistent.PersistentRow 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)

Aggregations

PersistentRow (com.ramussoft.common.persistent.PersistentRow)28 PersistentField (com.ramussoft.common.persistent.PersistentField)25 PersistentWrapper (com.ramussoft.common.persistent.PersistentWrapper)22 ArrayList (java.util.ArrayList)12 SQLException (java.sql.SQLException)9 Persistent (com.ramussoft.common.persistent.Persistent)8 List (java.util.List)8 PreparedStatement (java.sql.PreparedStatement)7 RowMapper (com.ramussoft.jdbc.RowMapper)6 ResultSet (java.sql.ResultSet)6 FindObject (com.ramussoft.common.attribute.FindObject)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