aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/datasets/ReaderSearchTable.java
blob: 77c54cf987eb9ed506f0751f451b4a7a7d1b6f79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.wordpress.android.datasets;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import org.wordpress.android.util.DateTimeUtils;
import org.wordpress.android.util.SqlUtils;

import java.util.Date;

/**
 * search suggestion table - populated by user's reader search history
 */
public class ReaderSearchTable {

    public static final String COL_ID    = "_id";
    public static final String COL_QUERY = "query_string";

    protected static void createTables(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE tbl_search_suggestions ("
                 + "    _id            INTEGER PRIMARY KEY AUTOINCREMENT,"
                 + "	query_string   TEXT NOT NULL COLLATE NOCASE,"
                 + "    date_used      TEXT)");
        db.execSQL("CREATE UNIQUE INDEX idx_search_suggestions_query ON tbl_search_suggestions(query_string)");
    }

    protected static void dropTables(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS tbl_search_suggestions");
    }

    /*
     * adds the passed query string, updating the usage date
     */
    public static void addOrUpdateQueryString(@NonNull String query) {
        String date = DateTimeUtils.iso8601FromDate(new Date());

        SQLiteStatement stmt = ReaderDatabase.getWritableDb().compileStatement(
                "INSERT OR REPLACE INTO tbl_search_suggestions (query_string, date_used) VALUES (?1,?2)");
        try {
            stmt.bindString(1, query);
            stmt.bindString(2, date);
            stmt.execute();
        } finally {
            SqlUtils.closeStatement(stmt);
        }
    }

    public static void deleteQueryString(@NonNull String query) {
        String[]args = new String[]{query};
        ReaderDatabase.getWritableDb().delete("tbl_search_suggestions", "query_string=?", args);
    }

    public static void deleteAllQueries() {
        SqlUtils.deleteAllRowsInTable(ReaderDatabase.getWritableDb(), "tbl_search_suggestions");
    }

    /**
     * Returns a cursor containing query strings previously typed by the user
     * @param filter - filters the list using LIKE syntax (pass null for no filter)
     * @param max - limit the list to this many items (pass zero for no limit)
     */
    public static Cursor getQueryStringCursor(String filter, int max) {
        String sql;
        String[] args;
        if (TextUtils.isEmpty(filter)) {
            sql = "SELECT * FROM tbl_search_suggestions";
            args = null;
        } else {
            sql = "SELECT * FROM tbl_search_suggestions WHERE query_string LIKE ?";
            args = new String[]{filter + "%"};
        }

        sql += " ORDER BY date_used DESC";

        if (max > 0) {
            sql += " LIMIT " + max;
        }

        return ReaderDatabase.getReadableDb().rawQuery(sql, args);
    }
}