use of siena.SienaException in project siena by mandubian.
the class JdbcPersistenceManager method delete.
public <T> int delete(Query<T> query) {
ClassInfo info = ClassInfo.getClassInfo(query.getQueriedClass());
List<Object> parameters = new ArrayList<Object>();
StringBuilder sql = new StringBuilder("DELETE FROM ");
sql.append(info.tableName);
appendSqlWhere(query, sql, parameters);
PreparedStatement statement = null;
ResultSet rs = null;
try {
statement = createStatement(sql.toString(), parameters);
return statement.executeUpdate();
} 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 addParameters.
protected int addParameters(Object obj, List<Field> fields, PreparedStatement ps, int i) throws SQLException {
for (Field field : fields) {
Class<?> type = field.getType();
if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
JdbcClassInfo ci = JdbcClassInfo.getClassInfo(type);
Object rel = Util.readField(obj, field);
for (Field f : ci.keys) {
if (rel != null) {
Object value = Util.readField(rel, f);
if (value instanceof Json)
value = ((Json) value).toString();
setParameter(ps, i++, value);
} else {
setParameter(ps, i++, null);
}
}
} else {
Object value = Util.readField(obj, field);
if (value != null) {
if (Json.class.isAssignableFrom(type)) {
value = ((Json) value).toString();
} else if (field.getAnnotation(Embedded.class) != null) {
value = JsonSerializer.serialize(value).toString();
} else if (field.getAnnotation(Polymorphic.class) != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out;
try {
out = new ObjectOutputStream(bos);
out.writeObject(value);
out.close();
} catch (IOException e) {
throw new SienaException(e);
}
value = bos.toByteArray();
} else if (Enum.class.isAssignableFrom(type)) {
value = value.toString();
} else if (BigDecimal.class == type) {
DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
if (ann == null) {
value = (BigDecimal) value;
} else {
switch(ann.storageType()) {
case DOUBLE:
value = ((BigDecimal) value).doubleValue();
break;
case STRING:
value = ((BigDecimal) value).toPlainString();
break;
case NATIVE:
value = (BigDecimal) value;
break;
}
}
}
}
setParameter(ps, i++, value);
}
}
return i;
}
use of siena.SienaException in project siena by mandubian.
the class JdbcPersistenceManager method nextPage.
public <T> void nextPage(Query<T> query) {
QueryOptionJdbcContext jdbcCtx = (QueryOptionJdbcContext) query.option(QueryOptionJdbcContext.ID);
if (jdbcCtx == null) {
jdbcCtx = new QueryOptionJdbcContext();
query.customize(jdbcCtx);
}
// if no more data after, doesn't try to go after
if (jdbcCtx.noMoreDataAfter) {
return;
}
// if no more data before, removes flag to be able and stay there
if (jdbcCtx.noMoreDataBefore) {
jdbcCtx.noMoreDataBefore = false;
return;
}
QueryOptionPage pag = (QueryOptionPage) query.option(QueryOptionPage.ID);
if (pag.isPaginating()) {
// QueryOptionOffset offset = (QueryOptionOffset)query.option(QueryOptionOffset.ID);
// if(offset.isActive()){
jdbcCtx.realPageSize = pag.pageSize;
jdbcCtx.realOffset += pag.pageSize;
// }
} else {
// throws exception because it's impossible to reuse nextPage when paginating has been interrupted, the cases are too many
throw new SienaException("Can't use nextPage after pagination has been interrupted...");
}
}
use of siena.SienaException in project siena by mandubian.
the class JdbcPersistenceManager method insert.
public void insert(Object obj) {
JdbcClassInfo classInfo = JdbcClassInfo.getClassInfo(obj.getClass());
PreparedStatement ps = null;
try {
for (Field field : classInfo.keys) {
Id id = field.getAnnotation(Id.class);
if (id.value() == Generator.UUID) {
field.set(obj, UUID.randomUUID().toString());
}
}
if (!classInfo.generatedKeys.isEmpty()) {
insertWithAutoIncrementKey(classInfo, obj);
} else {
ps = getConnection().prepareStatement(classInfo.insertSQL);
addParameters(obj, classInfo.insertFields, ps, 1);
ps.executeUpdate();
}
} 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 appendSqlSearch.
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);
String[] columns = ClassInfo.getColumnNames(f, info.tableName);
for (String col : columns) {
cols.add(col);
}
}
QueryOption opt = qf.option;
if (opt != null) {
// only manages QueryOptionJdbcSearch
if (QueryOptionJdbcSearch.class.isAssignableFrom(opt.getClass())) {
if (((QueryOptionJdbcSearch) opt).booleanMode) {
sql.append("MATCH(" + Util.join(cols, ",") + ") AGAINST(? IN BOOLEAN MODE)");
} else {
}
} else {
sql.append("MATCH(" + Util.join(cols, ",") + ") AGAINST(?)");
}
} else {
// as mysql default search is fulltext and as it requires a FULLTEXT index,
// by default, we use boolean mode which works without fulltext index
sql.append("MATCH(" + Util.join(cols, ",") + ") AGAINST(? IN BOOLEAN MODE)");
}
parameters.add(qf.match);
} catch (Exception e) {
throw new SienaException(e);
}
}
Aggregations