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();
}
}
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;
}
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();
}
}
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);
}
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);
}
Aggregations