Search in sources :

Example 6 with QueryFilterSearch

use of siena.QueryFilterSearch in project siena by mandubian.

the class H2PersistenceManager method doSearchCount.

protected <T> int doSearchCount(Query<T> query) {
    // TODO this is a very raw impl: need some work certainly 
    try {
        Connection conn = this.getConnection();
        ClassInfo ci = ClassInfo.getClassInfo(query.getQueriedClass());
        // doesn't index a table that has already been indexed
        if (!tableIndexMap.containsKey(ci.tableName)) {
            List<String> colList = ci.getUpdateFieldsColumnNames();
            String cols = null;
            if (!colList.isEmpty()) {
                cols = "";
                // removes auto generated IDs from index
                int sz = colList.size();
                for (int i = 0; i < sz; i++) {
                    if ("h2".equals(dbMode))
                        cols += colList.get(i).toUpperCase();
                    else // !!! mysql mode means case INsensitive to lowercase !!!!
                    if ("mysql".equals(dbMode))
                        cols += colList.get(i).toLowerCase();
                    else
                        cols += colList.get(i).toUpperCase();
                    if (i < sz - 1)
                        cols += ",";
                }
            }
            // creates the index
            FullText.createIndex(conn, "PUBLIC", ci.tableName.toUpperCase(), cols);
            tableIndexMap.put(ci.tableName, true);
        }
        String searchString = "";
        Iterator<QueryFilterSearch> it = query.getSearches().iterator();
        boolean first = true;
        while (it.hasNext()) {
            if (!first) {
                searchString += " ";
            } else {
                first = false;
            }
            searchString += it.next().match;
        }
        ResultSet rs = FullText.searchData(conn, searchString, 0, 0);
        int count = 0;
        while (rs.next()) {
            //String queryStr = rs.getString("QUERY");
            //String score = rs.getString("SCORE");
            //Array columns = rs.getArray("COLUMNS");
            Object[] keys = (Object[]) rs.getArray("KEYS").getArray();
            count += keys.length;
        }
        return count;
    } catch (SQLException e) {
        throw new SienaException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryFilterSearch(siena.QueryFilterSearch) ResultSet(java.sql.ResultSet) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Example 7 with QueryFilterSearch

use of siena.QueryFilterSearch in project siena by mandubian.

the class H2PersistenceManager method doSearchKeys.

protected <T> List<T> doSearchKeys(Query<T> query, int limit, int offset) {
    // TODO this is a very raw impl: need some work certainly 
    try {
        Connection conn = this.getConnection();
        ClassInfo ci = ClassInfo.getClassInfo(query.getQueriedClass());
        // doesn't index a table that has already been indexed
        if (!tableIndexMap.containsKey(ci.tableName)) {
            List<String> colList = ci.getUpdateFieldsColumnNames();
            String cols = null;
            if (!colList.isEmpty()) {
                cols = "";
                // removes auto generated IDs from index
                int sz = colList.size();
                for (int i = 0; i < sz; i++) {
                    if ("h2".equals(dbMode))
                        cols += colList.get(i).toUpperCase();
                    else // !!! mysql mode means case INsensitive to lowercase !!!!
                    if ("mysql".equals(dbMode))
                        cols += colList.get(i).toLowerCase();
                    else
                        cols += colList.get(i).toUpperCase();
                    if (i < sz - 1)
                        cols += ",";
                }
            }
            // creates the index
            FullText.createIndex(conn, "PUBLIC", ci.tableName.toUpperCase(), cols);
            tableIndexMap.put(ci.tableName, true);
        }
        String searchString = "";
        Iterator<QueryFilterSearch> it = query.getSearches().iterator();
        boolean first = true;
        while (it.hasNext()) {
            if (!first) {
                searchString += " ";
            } else {
                first = false;
            }
            searchString += it.next().match;
        }
        ResultSet rs = FullText.searchData(conn, searchString, limit, offset);
        List<T> res = new ArrayList<T>();
        Class<T> clazz = query.getQueriedClass();
        while (rs.next()) {
            //String queryStr = rs.getString("QUERY");
            //String score = rs.getString("SCORE");
            //Array columns = rs.getArray("COLUMNS");
            Object[] keys = (Object[]) rs.getArray("KEYS").getArray();
            for (Object key : keys) {
                T obj = Util.createObjectInstance(clazz);
                for (Field field : JdbcClassInfo.getClassInfo(clazz).keys) {
                    JdbcMappingUtils.setFromObject(obj, field, key);
                }
                res.add(obj);
            }
        }
        return res;
    } catch (SQLException e) {
        throw new SienaException(e);
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) SienaException(siena.SienaException) QueryFilterSearch(siena.QueryFilterSearch) Field(java.lang.reflect.Field) ResultSet(java.sql.ResultSet) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Aggregations

QueryFilterSearch (siena.QueryFilterSearch)7 SienaException (siena.SienaException)6 Field (java.lang.reflect.Field)5 SQLException (java.sql.SQLException)4 ClassInfo (siena.ClassInfo)4 QueryFilter (siena.QueryFilter)4 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 QueryFilterSimple (siena.QueryFilterSimple)3 SienaRestrictedApiException (siena.SienaRestrictedApiException)3 IOException (java.io.IOException)2 Pattern (java.util.regex.Pattern)2 QueryOrder (siena.QueryOrder)2 Key (com.google.appengine.api.datastore.Key)1 FilterOperator (com.google.appengine.api.datastore.Query.FilterOperator)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1