Search in sources :

Example 6 with PersistentRow

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

the class PersistentFactory method store.

protected void store() throws SQLException {
    for (Object object : rows) {
        final PersistentRow row = (PersistentRow) object;
        if (row.getStatus() == CREATED) {
            final long next = template.nextVal(prefix + "persistents_sequence");
            row.setId((int) next);
            template.update("INSERT INTO " + prefix + "persistents(PERSISTENT_ID, TABLE_NAME, TABLE_TYPE, CLASS_NAME, PLUGIN_NAME, TYPE_NAME) " + "VALUES(?, ?, ?, ?, ?, ?);\n" + "CREATE TABLE " + row.getTableName() + " ();", new Object[] { next, row.getTableName(), row.getTableType(), row.getClassName(), row.getPluginName(), row.getTypeName() }, false);
        }
        List<PersistentField> primaryKays = new ArrayList<PersistentField>();
        boolean addPrimry = false;
        for (Object object2 : row.getFields()) {
            PersistentField field = (PersistentField) object2;
            if (field.getStatus() == CREATED) {
                final long next = template.nextVal(prefix + "persistent_fields_sequence");
                field.setId((int) next);
                template.update("INSERT INTO " + prefix + "persistent_fields(PERSISTENT_FIELD_ID, PERSISTENT_ID, FIELD_NAME, FIELD_DATABASE_NAME, FIELD_ID, FIELD_TYPE, FIELD_AUTOSET, FIELD_PRIMARY) VALUES" + "(?, ?, ?, ?, ?, ?, ?, ?);\n" + extracted(row, field), new Object[] { next, (long) row.getId(), field.getName(), field.getDatabaseName(), field.getFieldId(), field.getType(), field.isAutoset(), field.isPrimary() }, false);
                field.setStatus(LOADED);
                if (field.isPrimary()) {
                    addPrimry = true;
                }
            }
            if (field.isPrimary()) {
                primaryKays.add(field);
            }
        }
        if ((addPrimry) && (primaryKays.size() > 0)) {
            String pkeyName = row.getTableName() + "_pkey";
            String indexName = row.getTableName() + "_pkey_index";
            if (row.getStatus() == LOADED) {
                try {
                    template.execute("ALTER TABLE " + row.getTableName() + " DROP CONSTRAINT " + pkeyName + ";\n" + "DROP INDEX " + indexName + ";\n");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            StringBuffer sb = new StringBuffer("(" + primaryKays.get(0).getDatabaseName());
            for (int i = 1; i < primaryKays.size(); i++) {
                sb.append(", ");
                sb.append(primaryKays.get(i).getDatabaseName());
            }
            sb.append(")");
            String keyFields = sb.toString();
            try {
                template.execute("ALTER TABLE " + row.getTableName() + " ADD CONSTRAINT " + pkeyName + " PRIMARY KEY " + keyFields + ";\n" + "CREATE UNIQUE INDEX " + indexName + " ON " + row.getTableName() + keyFields + ";\n");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        row.setStatus(LOADED);
    }
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException)

Example 7 with PersistentRow

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

the class PersistentFactory method dropTables.

public void dropTables() throws SQLException {
    load();
    template.execute(new JDBCCallback() {

        @Override
        public Object execute(Connection connection) throws SQLException {
            Statement st = null;
            try {
                st = connection.createStatement();
                for (Object object : rows) {
                    PersistentRow row = (PersistentRow) object;
                    if (row.getStatus() == LOADED) {
                        for (PersistentField field : row.getFields()) {
                            if (field.getType() == PersistentField.ID) {
                                st.execute("DROP SEQUENCE " + field.getSequenceName(row.getTableName()) + ";");
                            }
                        }
                        st.execute("DROP TABLE " + row.getTableName() + ";");
                        st.execute("DELETE FROM " + prefix + "persistent_fields");
                        st.execute("DELETE FROM " + prefix + "persistents");
                    }
                }
            } finally {
                if (st != null)
                    st.close();
            }
            return null;
        }
    });
}
Also used : PersistentField(com.ramussoft.common.persistent.PersistentField) SQLException(java.sql.SQLException) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Statement(java.sql.Statement) JDBCCallback(com.ramussoft.jdbc.JDBCCallback) Connection(java.sql.Connection)

Example 8 with PersistentRow

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

the class PersistentFactory method checkClass.

protected void checkClass(ClassLoader classLoader, Class<?> p, String pluginName, String typeName) {
    PersistentWrapper wrapper = new PersistentWrapper(p);
    Table table = p.getAnnotation(Table.class);
    String className = p.getName();
    PersistentRow row = getRow(className);
    if (row == null) {
        row = new PersistentRow(classLoader);
        rows.add(row);
    } else
        row.setClassLoader(classLoader);
    row.setClassName(className);
    row.setExists(true);
    row.setPluginName(pluginName);
    fillTable(table, row, p);
    if (table != null)
        row.setTableType(table.type().ordinal());
    row.setTypeName(typeName);
    String[] fields = wrapper.getFields();
    fields = Arrays.copyOf(fields, fields.length + 1);
    String valueBranchId = "valueBranchId";
    fields[fields.length - 1] = valueBranchId;
    for (String fieldName : fields) {
        String databaseField = wrapper.toDatabaseField(fieldName);
        PersistentField field = getField(row, databaseField);
        if (field == null) {
            field = new PersistentField();
            row.getFields().add(field);
        }
        int type = wrapper.getAnnotationType(fieldName);
        if (valueBranchId.equals(fieldName)) {
            field.setAutoset(true);
            field.setFieldId(-2);
            field.setPrimary(true);
            field.setDefaultValue("0");
        } else {
            Class<? extends Annotation> annotation = PersistentWrapper.ANNOTATIONS[type];
            if (Attribute.class.isAssignableFrom(annotation)) {
                Attribute attr = wrapper.getAnnotation(fieldName, Attribute.class);
                field.setAutoset(attr.autoset());
                field.setFieldId(attr.id());
                field.setPrimary(attr.primary());
            } else if (Element.class.isAssignableFrom(annotation)) {
                Element attr = wrapper.getAnnotation(fieldName, Element.class);
                field.setAutoset(attr.autoset());
                field.setFieldId(attr.id());
                field.setPrimary(attr.primary());
            } else if (com.ramussoft.common.persistent.Long.class.isAssignableFrom(annotation)) {
                com.ramussoft.common.persistent.Long attr = wrapper.getAnnotation(fieldName, com.ramussoft.common.persistent.Long.class);
                field.setFieldId(attr.id());
                field.setPrimary(attr.primary());
                if (attr.setDefaultValue()) {
                    field.setDefaultValue(Long.toString(attr.defaultValue()));
                }
            } else if (com.ramussoft.common.persistent.Double.class.isAssignableFrom(annotation)) {
                com.ramussoft.common.persistent.Double attr = wrapper.getAnnotation(fieldName, com.ramussoft.common.persistent.Double.class);
                field.setFieldId(attr.id());
                field.setPrimary(attr.primary());
            } else if (Date.class.isAssignableFrom(annotation)) {
                Date attr = wrapper.getAnnotation(fieldName, Date.class);
                field.setFieldId(attr.id());
                field.setPrimary(attr.primary());
            } else if (Text.class.isAssignableFrom(annotation)) {
                Text attr = wrapper.getAnnotation(fieldName, Text.class);
                field.setFieldId(attr.id());
                field.setPrimary(attr.primary());
            } else if (Id.class.isAssignableFrom(annotation)) {
                Id attr = wrapper.getAnnotation(fieldName, Id.class);
                field.setFieldId(attr.id());
                field.setPrimary(true);
                field.setAutoset(true);
            } else if (Qualifier.class.isAssignableFrom(annotation)) {
                Qualifier attr = wrapper.getAnnotation(fieldName, Qualifier.class);
                field.setFieldId(attr.id());
                field.setPrimary(attr.primary());
            } else if (Binary.class.isAssignableFrom(annotation)) {
                Binary binary = wrapper.getAnnotation(fieldName, Binary.class);
                field.setFieldId(binary.id());
            } else if (com.ramussoft.common.persistent.Integer.class.isAssignableFrom(annotation)) {
                com.ramussoft.common.persistent.Integer integer = wrapper.getAnnotation(fieldName, com.ramussoft.common.persistent.Integer.class);
                field.setFieldId(integer.id());
                field.setPrimary(integer.primary());
            } else {
                throw new RuntimeException("Unknown annotation class: " + annotation.getName());
            }
        }
        field.setDatabaseName(databaseField);
        field.setType(type);
        field.setName(fieldName);
    }
}
Also used : Table(com.ramussoft.common.persistent.Table) PersistentField(com.ramussoft.common.persistent.PersistentField) PersistentRow(com.ramussoft.common.persistent.PersistentRow) Attribute(com.ramussoft.common.attribute.Attribute) Element(com.ramussoft.common.persistent.Element) Text(com.ramussoft.common.persistent.Text) Date(com.ramussoft.common.persistent.Date) Qualifier(com.ramussoft.common.persistent.Qualifier) Id(com.ramussoft.common.persistent.Id) Binary(com.ramussoft.common.persistent.Binary) PersistentWrapper(com.ramussoft.common.persistent.PersistentWrapper)

Example 9 with PersistentRow

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

the class UniversalPersistentFactory method save.

public Object save(Object object, boolean cached, JDBCCallback callback) {
    if (object instanceof List)
        saveList((List) object, cached, callback);
    Class clazz = object.getClass();
    PersistentRow row = rowHash.get(clazz);
    PersistentWrapper wrapper = rowWrappers.get(clazz);
    List<PersistentField> fields = row.getFields();
    Object[] objects = new Object[fields.size()];
    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());
    }
    String sql = insert.toString() + values.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 10 with PersistentRow

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

the class UniversalPersistentFactory method deleteInTransaction.

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("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();
    PreparedStatement ps = template.getPreparedStatement(sql, true);
    template.setParams(ps, objects);
    ps.execute();
}
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

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