use of com.ramussoft.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class UniversalPersistentFactory method dropTables.
@Override
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 " + getPersistentFieldsTableName());
st.execute("DELETE FROM " + getPersistentClassesTableName());
} finally {
if (st != null)
st.close();
}
return null;
}
});
}
use of com.ramussoft.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class UniversalPersistentFactory method saveInTransaction.
public void saveInTransaction(Object object, final boolean cached) throws SQLException {
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() + ")";
PreparedStatement ps = template.getPreparedStatement(sql, cached);
synchronized (ps) {
template.setParams(ps, objects);
if (!cached)
ps.close();
ps.execute();
}
}
use of com.ramussoft.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class UniversalPersistentFactory method load.
@Override
protected void load() throws SQLException {
List list = template.query("SELECT * FROM " + getPersistentClassesTableName(), 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.setTableName(rs.getString("TABLE_NAME").trim());
row.setExists(rs.getBoolean("PERSISTENT_EXISTS"));
row.setFields(c(loadFields(row.getClassName())));
return row;
}
});
rows = list;
}
use of com.ramussoft.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class UniversalPersistentFactory method checkClass.
@Override
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();
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);
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);
}
}
use of com.ramussoft.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class BranchesPersistentFactory method query.
public List<Object> query(Class clazz, String[] databaseFields, Object[] objects, boolean cached) {
StringBuffer sb = new StringBuffer();
PersistentRow row = rowHash.get(clazz);
sb.append("SELECT * FROM " + row.getTableName() + " WHERE ");
boolean fist = true;
for (String f : databaseFields) {
if (fist)
fist = false;
else
sb.append("AND ");
sb.append(f);
sb.append("=? ");
}
if (!fist)
sb.append("AND ");
sb.append(getSQLBranchCondition());
long id = getActiveBranchId();
objects = addBranchWhereObjects(id, objects);
return template.query(sb.toString(), new Mapper(clazz), objects, cached);
}
Aggregations