// Path: app/src/main/java/com/zulip/android/database/DatabaseHelper.java
// public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
// 
//     // name of the database file for your application -- change to something
//     // appropriate for your app
//     private static final String DATABASE_NAME = "zulip-%s.db";
//     // any time you make changes to your database objects, you may have to
//     // increase the database version
//     private static final int DATABASE_VERSION = 11;
// 
//     public DatabaseHelper(ZulipApp app, String email) {
//         super(app, String.format(DATABASE_NAME, MD5Util.md5Hex(email)), null,
//                 DATABASE_VERSION, R.raw.ormlite_config);
//     }
// 
//     /**
//      * Escape LIKE wildcards with a backslash. Must also use ESCAPE clause
//      *
//      * @param likeClause string to escape
//      * @return Escaped string
//      */
//     public static String likeEscape(String likeClause) {
//         return likeClause.replace("%", "\\%").replace("_", "\\_");
//     }
// 
//     /**
//      * This is called when the database is first created. Usually you should
//      * call createTable statements here to create the tables that will store
//      * your data.
//      */
//     @Override
//     public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
//         try {
//             Log.i(DatabaseHelper.class.getName(), "onCreate");
//             // Create the databases we're using
//             TableUtils.createTable(connectionSource, Person.class);
//             TableUtils.createTable(connectionSource, Stream.class);
//             TableUtils.createTable(connectionSource, Message.class);
//             TableUtils.createTable(connectionSource, MessageRange.class);
//             TableUtils.createTable(connectionSource, Emoji.class);
//             TableUtils.createTable(connectionSource, Reaction.class);
//             TableUtils.createTable(connectionSource, UserReaction.class);
//         } catch (SQLException e) {
//             Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
//             throw new RuntimeException(e);
//         }
//     }
// 
//     public void resetDatabase(SQLiteDatabase db) {
//         ZulipApp.get().onResetDatabase();
//         Log.i("resetDatabase", "Resetting database");
// 
//         boolean inTransaction = db.inTransaction();
// 
//         if (!inTransaction) {
//             db.beginTransaction();
//         }
// 
//         Cursor c = db.rawQuery(
//                 "SELECT name FROM sqlite_master WHERE type='table'"
//                         + "AND name NOT LIKE 'sqlite_%'"
//                         + "AND name NOT LIKE 'android_%';", null);
//         while (c.moveToNext()) {
//             String name = c.getString(0);
//             Log.i("resetDatabase", "Dropping " + name);
//             db.execSQL("DROP TABLE " + name);
//         }
// 
//         c.close();
// 
//         db.setTransactionSuccessful();
//         db.endTransaction();
//         db.execSQL("VACUUM;");
// 
//         if (inTransaction) {
//             db.beginTransaction();
//         }
// 
//         onCreate(db);
//     }
// 
//     /**
//      * This is called when your application is upgraded and it has a higher
//      * version number. This allows you to adjust the various data to match the
//      * new version number.
//      */
//     @Override
//     public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
//                           int oldVersion, int newVersion) {
//         resetDatabase(db);
//     }
// 
//     @Override
//     public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//         resetDatabase(db);
//     }
// }


import android.os.Parcel;
import com.j256.ormlite.stmt.SelectArg;
import com.j256.ormlite.stmt.Where;
import com.zulip.android.database.DatabaseHelper;
import com.zulip.android.models.Message;
import com.zulip.android.models.Stream;
import org.json.JSONArray;
import org.json.JSONException;
import java.sql.SQLException;
import java.util.Arrays;
package com.zulip.android.filters;





/**
 * Narrow based on search terms
 * {@link NarrowFilterSearch#query} is the search query
 * {@link NarrowFilterSearch#filter} indicates the current narrow filter
 */
public class NarrowFilterSearch implements NarrowFilter {
    public static final Creator<NarrowFilterSearch> CREATOR = new Creator<NarrowFilterSearch>() {
        @Override
        public NarrowFilterSearch createFromParcel(Parcel parcel) {
            return new NarrowFilterSearch(parcel.readString(), null);
        }

        @Override
        public NarrowFilterSearch[] newArray(int i) {
            return new NarrowFilterSearch[i];
        }
    };

    private String query;
    private NarrowFilter filter;

    public NarrowFilterSearch(String query, NarrowFilter filter) {
        this.query = query;
        this.filter = filter;
    }

    @Override
    public Where<Message, Object> modWhere(Where<Message, Object> where)
            throws SQLException {
        where.raw(
                Message.CONTENT_FIELD + " LIKE ? COLLATE NOCASE",
                new SelectArg(Message.CONTENT_FIELD, "%"
// Path: app/src/main/java/com/zulip/android/database/DatabaseHelper.java
// public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
// 
//     // name of the database file for your application -- change to something
//     // appropriate for your app
//     private static final String DATABASE_NAME = "zulip-%s.db";
//     // any time you make changes to your database objects, you may have to
//     // increase the database version
//     private static final int DATABASE_VERSION = 11;
// 
//     public DatabaseHelper(ZulipApp app, String email) {
//         super(app, String.format(DATABASE_NAME, MD5Util.md5Hex(email)), null,
//                 DATABASE_VERSION, R.raw.ormlite_config);
//     }
// 
//     /**
//      * Escape LIKE wildcards with a backslash. Must also use ESCAPE clause
//      *
//      * @param likeClause string to escape
//      * @return Escaped string
//      */
//     public static String likeEscape(String likeClause) {
//         return likeClause.replace("%", "\\%").replace("_", "\\_");
//     }
// 
//     /**
//      * This is called when the database is first created. Usually you should
//      * call createTable statements here to create the tables that will store
//      * your data.
//      */
//     @Override
//     public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
//         try {
//             Log.i(DatabaseHelper.class.getName(), "onCreate");
//             // Create the databases we're using
//             TableUtils.createTable(connectionSource, Person.class);
//             TableUtils.createTable(connectionSource, Stream.class);
//             TableUtils.createTable(connectionSource, Message.class);
//             TableUtils.createTable(connectionSource, MessageRange.class);
//             TableUtils.createTable(connectionSource, Emoji.class);
//             TableUtils.createTable(connectionSource, Reaction.class);
//             TableUtils.createTable(connectionSource, UserReaction.class);
//         } catch (SQLException e) {
//             Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
//             throw new RuntimeException(e);
//         }
//     }
// 
//     public void resetDatabase(SQLiteDatabase db) {
//         ZulipApp.get().onResetDatabase();
//         Log.i("resetDatabase", "Resetting database");
// 
//         boolean inTransaction = db.inTransaction();
// 
//         if (!inTransaction) {
//             db.beginTransaction();
//         }
// 
//         Cursor c = db.rawQuery(
//                 "SELECT name FROM sqlite_master WHERE type='table'"
//                         + "AND name NOT LIKE 'sqlite_%'"
//                         + "AND name NOT LIKE 'android_%';", null);
//         while (c.moveToNext()) {
//             String name = c.getString(0);
//             Log.i("resetDatabase", "Dropping " + name);
//             db.execSQL("DROP TABLE " + name);
//         }
// 
//         c.close();
// 
//         db.setTransactionSuccessful();
//         db.endTransaction();
//         db.execSQL("VACUUM;");
// 
//         if (inTransaction) {
//             db.beginTransaction();
//         }
// 
//         onCreate(db);
//     }
// 
//     /**
//      * This is called when your application is upgraded and it has a higher
//      * version number. This allows you to adjust the various data to match the
//      * new version number.
//      */
//     @Override
//     public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
//                           int oldVersion, int newVersion) {
//         resetDatabase(db);
//     }
// 
//     @Override
//     public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//         resetDatabase(db);
//     }
// }


// Path: app/src/main/java/com/zulip/android/filters/NarrowFilterSearch.java
import android.os.Parcel;
import com.j256.ormlite.stmt.SelectArg;
import com.j256.ormlite.stmt.Where;
import com.zulip.android.database.DatabaseHelper;
import com.zulip.android.models.Message;
import com.zulip.android.models.Stream;
import org.json.JSONArray;
import org.json.JSONException;
import java.sql.SQLException;
import java.util.Arrays;

package com.zulip.android.filters;





/**
 * Narrow based on search terms
 * {@link NarrowFilterSearch#query} is the search query
 * {@link NarrowFilterSearch#filter} indicates the current narrow filter
 */
public class NarrowFilterSearch implements NarrowFilter {
    public static final Creator<NarrowFilterSearch> CREATOR = new Creator<NarrowFilterSearch>() {
        @Override
        public NarrowFilterSearch createFromParcel(Parcel parcel) {
            return new NarrowFilterSearch(parcel.readString(), null);
        }

        @Override
        public NarrowFilterSearch[] newArray(int i) {
            return new NarrowFilterSearch[i];
        }
    };

    private String query;
    private NarrowFilter filter;

    public NarrowFilterSearch(String query, NarrowFilter filter) {
        this.query = query;
        this.filter = filter;
    }

    @Override
    public Where<Message, Object> modWhere(Where<Message, Object> where)
            throws SQLException {
        where.raw(
                Message.CONTENT_FIELD + " LIKE ? COLLATE NOCASE",
                new SelectArg(Message.CONTENT_FIELD, "%"
                        + DatabaseHelper.likeEscape(query) + "%"));