Search in sources :

Example 26 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManagerAsync method insert.

public SienaFuture<Integer> insert(final Iterable<?> objects) {
    List<Entity> entities = new ArrayList<Entity>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        Field idField = info.getIdField();
        Entity entity = GaeMappingUtils.createEntityInstance(idField, info, obj);
        GaeMappingUtils.fillEntity(obj, entity);
        entities.add(entity);
    }
    Future<List<Key>> future = ds.put(entities);
    Future<Integer> wrapped = new SienaFutureWrapper<List<Key>, Integer>(future) {

        @Override
        protected Integer wrap(List<Key> generatedKeys) throws Exception {
            int i = 0;
            for (Object obj : objects) {
                Class<?> clazz = obj.getClass();
                ClassInfo info = ClassInfo.getClassInfo(clazz);
                Field idField = info.getIdField();
                GaeMappingUtils.setIdFromKey(idField, obj, generatedKeys.get(i++));
            }
            return generatedKeys.size();
        }
    };
    return new SienaFutureContainer<Integer>(wrapped);
}
Also used : SienaFutureWrapper(siena.core.async.SienaFutureWrapper) Entity(com.google.appengine.api.datastore.Entity) SienaFutureContainer(siena.core.async.SienaFutureContainer) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) QueryResultList(com.google.appengine.api.datastore.QueryResultList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 27 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class DdlGenerator method addTable.

public Table addTable(Class<?> clazz) {
    if (Modifier.isAbstract(clazz.getModifiers())) {
        return null;
    }
    Table table = new Table();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    table.setName(info.tableName);
    table.setType("MyISAM");
    database.addTable(table);
    Map<String, UniqueIndex> uniques = new HashMap<String, UniqueIndex>();
    Map<String, NonUniqueIndex> indexes = new HashMap<String, NonUniqueIndex>();
    /* columns */
    for (Field field : info.allFields) {
        String[] columns = ClassInfo.getColumnNames(field);
        boolean notNull = field.getAnnotation(NotNull.class) != null;
        Class<?> type = field.getType();
        if (!ClassInfo.isModel(type) || (ClassInfo.isModel(type) && ClassInfo.isEmbedded(field))) {
            Column column = createColumn(clazz, field, columns[0]);
            if (notNull || type.isPrimitive()) {
                column.setRequired(true);
                if (type.isPrimitive() && !ClassInfo.isId(field)) {
                    // TODO: add also Boolean, Long, Double,... ?
                    if (type == Boolean.TYPE) {
                        column.setDefaultValue("false");
                    } else {
                        column.setDefaultValue("0");
                    }
                }
            }
            Id id = field.getAnnotation(Id.class);
            if (id != null) {
                column.setPrimaryKey(true);
                column.setRequired(true);
                // auto_increments managed ONLY for long
                if (id.value() == Generator.AUTO_INCREMENT && (Long.TYPE == type || Long.class.isAssignableFrom(type)))
                    column.setAutoIncrement(true);
            // adds index on primary key
            /*UniqueIndex i = uniques.get(columns[0]);
					if(i == null) {
						i = new UniqueIndex();
						i.setName(columns[0]);
						uniques.put(columns[0], i);
						table.addIndex(i);
					}
					fillIndex(i, field);*/
            }
            table.addColumn(column);
        } else {
            List<Field> keys = ClassInfo.getClassInfo(type).keys;
            for (int i = 0; i < columns.length; i++) {
                Field f = keys.get(i);
                Column column = createColumn(clazz, f, columns[i]);
                if (notNull)
                    column.setRequired(true);
                table.addColumn(column);
            }
        }
    }
    /* indexes */
    for (Field field : info.updateFields) {
        Index index = field.getAnnotation(Index.class);
        if (index != null) {
            String[] names = index.value();
            for (String name : names) {
                NonUniqueIndex i = indexes.get(name);
                if (i == null) {
                    i = new NonUniqueIndex();
                    i.setName(name);
                    indexes.put(name, i);
                    table.addIndex(i);
                }
                fillIndex(i, field);
            }
        }
        Unique unique = field.getAnnotation(Unique.class);
        if (unique != null) {
            String[] names = unique.value();
            for (String name : names) {
                UniqueIndex i = uniques.get(name);
                if (i == null) {
                    i = new UniqueIndex();
                    i.setName(name);
                    uniques.put(name, i);
                    table.addIndex(i);
                }
                fillIndex(i, field);
            }
        }
    }
    tables.put(table.getName(), table);
    return table;
}
Also used : Table(org.apache.ddlutils.model.Table) NonUniqueIndex(org.apache.ddlutils.model.NonUniqueIndex) HashMap(java.util.HashMap) NonUniqueIndex(org.apache.ddlutils.model.NonUniqueIndex) Index(siena.Index) UniqueIndex(org.apache.ddlutils.model.UniqueIndex) NotNull(siena.NotNull) Field(java.lang.reflect.Field) IndexColumn(org.apache.ddlutils.model.IndexColumn) Column(org.apache.ddlutils.model.Column) Unique(siena.Unique) Id(siena.Id) NonUniqueIndex(org.apache.ddlutils.model.NonUniqueIndex) UniqueIndex(org.apache.ddlutils.model.UniqueIndex) ClassInfo(siena.ClassInfo)

Example 28 with ClassInfo

use of siena.ClassInfo 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 29 with ClassInfo

use of siena.ClassInfo 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 30 with ClassInfo

use of siena.ClassInfo 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

ClassInfo (siena.ClassInfo)68 Field (java.lang.reflect.Field)33 ArrayList (java.util.ArrayList)24 Key (com.google.appengine.api.datastore.Key)23 SienaException (siena.SienaException)21 Entity (com.google.appengine.api.datastore.Entity)17 QueryResultList (com.google.appengine.api.datastore.QueryResultList)10 List (java.util.List)10 DeletableItem (com.amazonaws.services.simpledb.model.DeletableItem)6 Item (com.amazonaws.services.simpledb.model.Item)6 ReplaceableItem (com.amazonaws.services.simpledb.model.ReplaceableItem)6 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 SienaRestrictedApiException (siena.SienaRestrictedApiException)5 Relation (siena.core.Relation)5 IOException (java.io.IOException)4 QueryFilterSearch (siena.QueryFilterSearch)4 Many4PM (siena.core.Many4PM)4 SienaFutureContainer (siena.core.async.SienaFutureContainer)4