Search in sources :

Example 86 with SienaException

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

Example 87 with SienaException

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);
    }
}
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 88 with SienaException

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);
    }
}
Also used : Field(java.lang.reflect.Field) ResultSet(java.sql.ResultSet) 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 89 with SienaException

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);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement) SienaException(siena.SienaException) SQLException(java.sql.SQLException) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException)

Example 90 with SienaException

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

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