use of siena.SienaException in project siena by mandubian.
the class JdbcPersistenceManager method deleteByKeys.
public <T> int deleteByKeys(Class<T> clazz, Iterable<?> keys) {
JdbcClassInfo classInfo = JdbcClassInfo.getClassInfo(clazz);
PreparedStatement ps = null;
try {
ps = getConnection().prepareStatement(classInfo.deleteSQL);
for (Object key : keys) {
setParameter(ps, 1, key);
ps.addBatch();
}
// TODO what to do with results of executeBatch ??????
int[] res = ps.executeBatch();
return res.length;
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
} finally {
JdbcDBUtils.closeStatementAndConnection(this, ps);
}
}
use of siena.SienaException in project siena by mandubian.
the class JdbcPersistenceManager method count.
public <T> int count(Query<T> query) {
ClassInfo info = ClassInfo.getClassInfo(query.getQueriedClass());
List<Object> parameters = new ArrayList<Object>();
StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM ");
sql.append(info.tableName);
appendSqlWhere(query, sql, parameters);
PreparedStatement statement = null;
ResultSet rs = null;
try {
statement = createStatement(sql.toString(), parameters);
rs = statement.executeQuery();
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
throw new SienaException(e);
} finally {
JdbcDBUtils.closeResultSet(rs);
JdbcDBUtils.closeStatementAndConnection(this, statement);
}
}
use of siena.SienaException in project siena by mandubian.
the class JdbcPersistenceManager method save.
public void save(Object obj) {
JdbcClassInfo classInfo = JdbcClassInfo.getClassInfo(obj.getClass());
PreparedStatement ps = null;
try {
Field idField = classInfo.info.getIdField();
Object idVal = Util.readField(obj, idField);
if (idVal == null) {
for (Field field : classInfo.keys) {
Id id = field.getAnnotation(Id.class);
if (id.value() == Generator.UUID) {
field.set(obj, UUID.randomUUID().toString());
}
}
}
if (idVal == null && !classInfo.generatedKeys.isEmpty()) {
ps = getConnection().prepareStatement(classInfo.insertOrUpdateSQL, Statement.RETURN_GENERATED_KEYS);
// insertWithAutoIncrementKey(classInfo, obj);
} else {
ps = getConnection().prepareStatement(classInfo.insertOrUpdateSQL);
}
int i = 1;
i = addParameters(obj, classInfo.allFields, ps, i);
addParameters(obj, classInfo.updateFields, ps, i);
ps.executeUpdate();
if (idVal == null && !classInfo.generatedKeys.isEmpty()) {
ResultSet gk = ps.getGeneratedKeys();
if (!gk.next())
throw new SienaException("No such generated keys");
i = 1;
for (Field field : classInfo.generatedKeys) {
field.setAccessible(true);
JdbcMappingUtils.setFromObject(obj, field, gk.getObject(i));
// field.set(obj, gk.getObject(i));
i++;
}
}
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
} finally {
JdbcDBUtils.closeStatementAndConnection(this, ps);
}
}
use of siena.SienaException in project siena by mandubian.
the class JdbcPersistenceManager method update.
public <T> int update(Iterable<T> objects) {
// throw new NotImplementedException("update not implemented for JDBC yet");
Map<JdbcClassInfo, List<Object>> objMap = new HashMap<JdbcClassInfo, List<Object>>();
PreparedStatement ps = null;
for (Object obj : objects) {
JdbcClassInfo classInfo = JdbcClassInfo.getClassInfo(obj.getClass());
if (!objMap.containsKey(classInfo)) {
List<Object> l = new ArrayList<Object>();
l.add(obj);
objMap.put(classInfo, l);
} else {
objMap.get(classInfo).add(obj);
}
}
int total = 0;
try {
for (JdbcClassInfo classInfo : objMap.keySet()) {
ps = getConnection().prepareStatement(classInfo.updateSQL);
for (Object obj : objMap.get(classInfo)) {
int i = 1;
i = addParameters(obj, classInfo.updateFields, ps, i);
addParameters(obj, classInfo.keys, ps, i);
ps.addBatch();
}
// TODO what to do with results of executeBatch ??????
int[] res = ps.executeBatch();
total += res.length;
}
return total;
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
} finally {
JdbcDBUtils.closeStatementAndConnection(this, ps);
}
}
use of siena.SienaException in project siena by mandubian.
the class PostgresqlPersistenceManager method appendSqlSearch.
@Override
public <T> void appendSqlSearch(QueryFilterSearch qf, Class<?> clazz, JdbcClassInfo info, StringBuilder sql, List<Object> parameters) {
List<String> cols = new ArrayList<String>();
try {
for (String field : qf.fields) {
Field f = Util.getField(clazz, field);
Class<?> cl = f.getType();
// if a number or date, doesn't try to coalesce
if (Number.class.isAssignableFrom(cl) || Date.class.isAssignableFrom(cl)) {
String[] columns = ClassInfo.getColumnNames(f, info.tableName);
for (String col : columns) {
cols.add(col);
}
} else // if is model, gets the key type and does the same as herebefore
if (ClassInfo.isModel(cl)) {
ClassInfo ci = ClassInfo.getClassInfo(cl);
if (ci.keys.size() == 1) {
Field key = ci.keys.get(0);
if (Number.class.isAssignableFrom(key.getType()) || Date.class.isAssignableFrom(key.getType())) {
cols.add(f.getName());
} else {
cols.add("coalesce(" + f.getName() + ", '')");
}
} else {
for (Field key : ci.keys) {
String[] columns = ClassInfo.getColumnNamesWithPrefix(key, f.getName() + "_");
if (Number.class.isAssignableFrom(key.getType()) || Date.class.isAssignableFrom(key.getType())) {
for (String col : columns) {
cols.add(col);
}
} else {
for (String col : columns) {
cols.add("coalesce(" + col + ", '')");
}
}
}
}
} else {
String[] columns = ClassInfo.getColumnNames(f, info.tableName);
for (String col : columns) {
cols.add("coalesce(" + col + ", '')");
}
}
}
QueryOption opt = qf.option;
if (opt != null) {
// only manages QueryOptionJdbcSearch
if (QueryOptionPostgresqlSearch.class.isAssignableFrom(opt.getClass())) {
String lang = ((QueryOptionPostgresqlSearch) opt).language;
if (lang != null && !"".equals(lang)) {
sql.append("to_tsvector('" + lang + "', " + Util.join(cols, " || ' ' || ") + ") @@ to_tsquery(?)");
} else {
sql.append("to_tsvector('english', " + Util.join(cols, " || ' ' || ") + ") @@ to_tsquery(?)");
}
} else {
}
} else {
sql.append("to_tsvector('english', " + Util.join(cols, " || ' ' || ") + ") @@ to_tsquery(?)");
}
parameters.add(qf.match);
} catch (Exception e) {
throw new SienaException(e);
}
}
Aggregations