Search in sources :

Example 16 with PersistentRow

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

the class TransactionStorageCommand method storePersistent.

static void storePersistent(JournaledEngine engine, BinaryDataOutput output, Persistent persistent) throws IOException {
    Class<? extends Persistent> key = persistent.getClass();
    byte id = (byte) engine.getPersistentClassId(key);
    output.writeByte(id);
    PersistentRow row = engine.getPersistentMetadata(key);
    List<PersistentField> fields = new ArrayList<PersistentField>();
    for (PersistentField field : row.getFields()) if (!field.isAutoset())
        fields.add(field);
    output.write(fields.size());
    for (PersistentField field : fields) {
        output.writeByte(field.getFieldId());
        String fieldName = field.getName();
        PersistentWrapper wrapper = engine.getWrapper(key);
        Object object = wrapper.getField(persistent, fieldName);
        if (object == null)
            output.write(0);
        else {
            output.write(1);
            switch(field.getType()) {
                case ATTRIBUTE:
                case ELEMENT:
                case QUALIFIER:
                case ID:
                case LONG:
                    output.writeLong((Long) object);
                    break;
                case TEXT:
                    output.writeSwimedString((String) object);
                    break;
                case DATE:
                    output.writeLong(((Timestamp) object).getTime());
                    break;
                case DOUBLE:
                    output.writeDouble((Double) object);
                    break;
                case PersistentField.BINARY:
                    output.writeBinary((byte[]) object);
                    break;
                case PersistentField.INTEGER:
                    output.writeInt((Integer) object);
                    break;
                default:
                    throw new RuntimeException("Unknown field type: " + field.getType());
            }
        }
    }
    output.writeLong(persistent.getValueBranchId());
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ArrayList(java.util.ArrayList) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 17 with PersistentRow

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

the class TransactionStorageCommand method loadPersistent.

static Persistent loadPersistent(JournaledEngine engine, BinaryDataInput input) throws IOException, InstantiationException, IllegalAccessException {
    Class<? extends Persistent> key = engine.getPersistentClassById(input.read());
    PersistentRow row = engine.getPersistentMetadata(key);
    Persistent persistent = key.newInstance();
    int size = input.read();
    for (int i = 0; i < size; i++) {
        int fieldId = input.read();
        PersistentField field = row.getFieldById(fieldId);
        String fieldName = field.getName();
        PersistentWrapper wrapper = engine.getWrapper(key);
        if (input.read() == 1)
            switch(field.getType()) {
                case ATTRIBUTE:
                case ELEMENT:
                case QUALIFIER:
                case ID:
                case LONG:
                    wrapper.setField(persistent, fieldName, input.readLong());
                    break;
                case TEXT:
                    wrapper.setField(persistent, fieldName, input.readSwimedString());
                    break;
                case DATE:
                    wrapper.setField(persistent, fieldName, new Timestamp(input.readLong()));
                    break;
                case DOUBLE:
                    wrapper.setField(persistent, fieldName, input.readDouble());
                    break;
                case BINARY:
                    wrapper.setField(persistent, fieldName, input.readBinary());
                    break;
                case INTEGER:
                    wrapper.setField(persistent, fieldName, input.readInt());
                    break;
                default:
                    throw new RuntimeException("Unknown field type: " + field.getType());
            }
    }
    persistent.setValueBranchId(input.readLong());
    return persistent;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Persistent(com.ramussoft.common.persistent.Persistent) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper) Timestamp(java.sql.Timestamp)

Example 18 with PersistentRow

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

the class AbstractJournaledEngine method equalsPrimaryKeys.

private boolean equalsPrimaryKeys(Persistent a, Persistent b) {
    PersistentWrapper wrapper = getWrapper(a.getClass());
    PersistentRow row = getPersistentMetadata(a.getClass());
    List<PersistentField> fields = row.getFields();
    for (PersistentField field : fields) if (field.isPrimary()) {
        if (!equals(wrapper.getField(a, field.getName()), wrapper.getField(b, field.getName())))
            return false;
    }
    return true;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 19 with PersistentRow

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

the class AbstractJournaledEngine method equalsAllFields.

private boolean equalsAllFields(PersistentPare pare) {
    Persistent a = pare.oldPersistent;
    Persistent b = pare.newPersistent;
    PersistentWrapper wrapper = getWrapper(a.getClass());
    PersistentRow row = getPersistentMetadata(a.getClass());
    for (PersistentField f : row.getFields()) {
        if (!f.isAutoset()) {
            String field = f.getName();
            if (!equals(wrapper.getField(a, field), wrapper.getField(b, field)))
                return false;
        }
    }
    return true;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Persistent(com.ramussoft.common.persistent.Persistent) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 20 with PersistentRow

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

the class IEngineImpl method getBinaryAttribute.

@Override
@SuppressWarnings("unchecked")
public List<Persistent>[] getBinaryAttribute(long elementId, long attributeId) {
    throwExaptionIfNotCan(getAccessor().canReadElement(elementId, attributeId), "Can not get attribute.");
    Attribute attribute = getAttribute(attributeId);
    AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
    final Class<? extends Persistent>[] classes;
    if (elementId >= 0)
        classes = plugin.getAttributePersistents();
    else
        classes = plugin.getAttributePropertyPersistents();
    List<Persistent>[] lists = new List[classes.length];
    for (int i = 0; i < lists.length; i++) {
        final Class<? extends Persistent> clazz = classes[i];
        final PersistentRow row = metadata.get(clazz);
        final PersistentWrapper wrapper = wrappers.get(clazz);
        ArrayList<Object> params = new ArrayList<Object>(2);
        ArrayList<String> paramFields = new ArrayList<String>(2);
        plugin.fillAttributeQuery(row, attributeId, elementId, params, paramFields, this);
        if (elementId >= 0l && attribute.getAttributeType().getTypeName().equals("ElementList") && attribute.getAttributeType().getPluginName().equals("Core")) {
            return getEListFixed(row, clazz, wrapper, paramFields, params, getActiveBranchId());
        }
        long branchId = 0l;
        try {
            branchId = getBranch(prefix + "attributes_data_metadata", new Object[] { "element_id", "attribute_id" }, new Object[] { elementId, attributeId }, getActiveBranchId());
        } catch (SQLException e1) {
            e1.printStackTrace();
            throw new RuntimeException(e1);
        }
        params.add(branchId);
        paramFields.add("value_branch_id");
        StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName());
        if (params.size() > 0) {
            sb.append(" WHERE ");
        }
        boolean first = true;
        for (int j = 0; j < params.size(); j++) {
            if (first) {
                first = false;
            } else {
                sb.append(" AND ");
            }
            sb.append(paramFields.get(j));
            sb.append("=?");
        }
        List<Persistent> list = template.query(sb.toString(), new RowMapper() {

            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                try {
                    Persistent persistent = clazz.newInstance();
                    for (PersistentField field : row.getFields()) {
                        wrapper.setDatabaseField(persistent, field, rs);
                    }
                    return persistent;
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                throw new RuntimeException();
            }
        }, params.toArray(new Object[params.size()]), true);
        lists[i] = list;
    }
    return lists;
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) Attribute(com.ramussoft.common.Attribute) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) RowMapper(com.ramussoft.jdbc.RowMapper) AttributePlugin(com.ramussoft.common.attribute.AttributePlugin) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Persistent(com.ramussoft.common.persistent.Persistent) FindObject(com.ramussoft.common.attribute.FindObject) 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