use of com.ramussoft.common.persistent.PersistentRow 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);
}
use of com.ramussoft.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class BranchesPersistentFactory method updateInTransaction.
@Override
public void updateInTransaction(Object object, 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;
int keyCount = 0;
for (int i = 0; i < objects.length; i++) {
PersistentField field = fields.get(i);
if (field.isPrimary()) {
keyCount++;
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() + " AND created_branch_id=?";
PreparedStatement ps = template.getPreparedStatement(sql, cached);
synchronized (ps) {
template.setParams(ps, objects);
long id = getActiveBranchId();
ps.setLong(objects.length + 1, id);
if (ps.executeUpdate() == 0) {
if (!cached)
ps.close();
sql = "UPDATE " + row.getTableName() + " SET removed_branch_id=?" + key.toString() + " AND " + getSQLBranchCondition();
ps = template.getPreparedStatement(sql, cached);
ps.setLong(1, id);
for (int i = objects.length - keyCount; i < objects.length; i++) {
template.setParam(ps, i + 2 - objects.length + keyCount, objects[i]);
}
Object[] objects2 = addBranchWhereObjects(id, new Object[] {});
for (int i = 0; i < objects2.length; i++) template.setParam(ps, keyCount + 2 + i, objects2[i]);
ps.executeUpdate();
if (!cached)
ps.close();
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());
}
insert.append(", created_branch_id");
values.append(", ?");
sql = insert.toString() + values.toString() + ")";
ps = template.getPreparedStatement(sql, cached);
template.setParams(ps, objects);
ps.setLong(objects.length + 1, id);
ps.executeUpdate();
if (!cached)
ps.close();
} else if (!cached)
ps.close();
}
}
use of com.ramussoft.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class BranchesPersistentFactory method deleteInTransaction.
@Override
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("UPDATE " + row.getTableName() + " SET removed_branch_id=? 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());
}
delete.append(" AND ");
delete.append(getSQLBranchCondition());
String sql = delete.toString();
PreparedStatement ps = template.getPreparedStatement(sql, true);
long id = getActiveBranchId();
ps.setLong(1, id);
for (int i = 0; i < objects.length; i++) template.setParam(ps, i + 2, objects[i]);
Object[] objects2 = addBranchWhereObjects(id, new Object[] {});
for (int i = 0; i < objects2.length; i++) {
template.setParam(ps, objects.length + i + 2, objects2[i]);
}
ps.executeUpdate();
}
Aggregations