Search in sources :

Example 66 with SienaException

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);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Example 67 with SienaException

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;
}
Also used : ObjectOutput(java.io.ObjectOutput) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Field(java.lang.reflect.Field) SienaException(siena.SienaException) Polymorphic(siena.core.Polymorphic)

Example 68 with SienaException

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...");
    }
}
Also used : QueryOptionPage(siena.core.options.QueryOptionPage) SienaException(siena.SienaException)

Example 69 with SienaException

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);
    }
}
Also used : Field(java.lang.reflect.Field) PreparedStatement(java.sql.PreparedStatement) Id(siena.Id) SienaException(siena.SienaException) SQLException(java.sql.SQLException) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException)

Example 70 with SienaException

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);
    }
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) QueryOption(siena.core.options.QueryOption) SienaException(siena.SienaException) SQLException(java.sql.SQLException) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException)

Aggregations

SienaException (siena.SienaException)214 Field (java.lang.reflect.Field)65 TransactionAccountFrom (siena.base.test.model.TransactionAccountFrom)52 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)40 SQLException (java.sql.SQLException)38 TransactionAccountTo (siena.base.test.model.TransactionAccountTo)34 SienaRestrictedApiException (siena.SienaRestrictedApiException)33 PreparedStatement (java.sql.PreparedStatement)25 ClassInfo (siena.ClassInfo)21 HashMap (java.util.HashMap)19 List (java.util.List)18 Discovery (siena.base.test.model.Discovery)18 Entity (com.google.appengine.api.datastore.Entity)15 Key (com.google.appengine.api.datastore.Key)14 ResultSet (java.sql.ResultSet)14 Id (siena.Id)14 AmazonClientException (com.amazonaws.AmazonClientException)12 Collection (java.util.Collection)10 DiscoveryStringId (siena.base.test.model.DiscoveryStringId)9