Search in sources :

Example 1 with QueryOption

use of siena.core.options.QueryOption 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)

Example 2 with QueryOption

use of siena.core.options.QueryOption 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)

Example 3 with QueryOption

use of siena.core.options.QueryOption in project siena by mandubian.

the class AbstractPersistenceManager method release.

public <T> void release(Query<T> query) {
    QueryOptionOffset offset = (QueryOptionOffset) query.option(QueryOptionOffset.ID);
    QueryOption state = query.option(QueryOptionState.ID);
    // resets offset
    if (offset.isActive())
        offset.offset = 0;
    // disables reusable and cludge
    if (state.isActive()) {
        state.passivate();
    }
}
Also used : QueryOptionOffset(siena.core.options.QueryOptionOffset) QueryOption(siena.core.options.QueryOption)

Example 4 with QueryOption

use of siena.core.options.QueryOption in project siena by mandubian.

the class AbstractPersistenceManagerAsync method release.

public <T> void release(QueryAsync<T> query) {
    QueryOptionOffset offset = (QueryOptionOffset) query.option(QueryOptionOffset.ID);
    QueryOption reuse = query.option(QueryOptionState.ID);
    // resets offset
    if (offset.isActive())
        offset.offset = 0;
    // disables reusable and cludge
    if (reuse.isActive()) {
        reuse.passivate();
    }
}
Also used : QueryOptionOffset(siena.core.options.QueryOptionOffset) QueryOption(siena.core.options.QueryOption)

Example 5 with QueryOption

use of siena.core.options.QueryOption in project siena by mandubian.

the class BaseTest method testRestoreQueryOption.

public void testRestoreQueryOption() {
    QueryOption optRestored = (QueryOption) JsonSerializer.deserialize(QueryOption.class, Json.loads("{\"type\":\"" + QueryOptionPage.class.getName() + "\", \"value\": {\"pageType\": \"TEMPORARY\", \"state\": \"PASSIVE\", \"pageSize\": 0, \"type\": 1} }"));
    Query<PersonLongAutoID> query = pm.createQuery(PersonLongAutoID.class);
    QueryOption opt = query.option(QueryOptionPage.ID);
    assertEquals(opt, optRestored);
}
Also used : QueryOptionPage(siena.core.options.QueryOptionPage) QueryOption(siena.core.options.QueryOption) PersonLongAutoID(siena.base.test.model.PersonLongAutoID)

Aggregations

QueryOption (siena.core.options.QueryOption)6 Field (java.lang.reflect.Field)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 SienaException (siena.SienaException)2 PersonLongAutoID (siena.base.test.model.PersonLongAutoID)2 QueryOptionOffset (siena.core.options.QueryOptionOffset)2 QueryOptionPage (siena.core.options.QueryOptionPage)2 IOException (java.io.IOException)1 Date (java.util.Date)1 ClassInfo (siena.ClassInfo)1 Json (siena.Json)1 SienaRestrictedApiException (siena.SienaRestrictedApiException)1