/*
 * Copyright (c) 2004-2019, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.hisp.dhis.android.core.data.database;

import android.database.Cursor;

final public class SqliteCheckerUtility {

    private SqliteCheckerUtility() {
    }

    public static boolean isTableEmpty(DatabaseAdapter databaseAdapter, String table) {
        boolean isTableEmpty = true;
        Cursor cursor = databaseAdapter.query(" SELECT * FROM "+table);
        int value = cursor.getCount();
        if (value > 0) {
            isTableEmpty = false;
        }
        cursor.close();
        return isTableEmpty;
    }

    public static boolean isDatabaseEmpty(DatabaseAdapter databaseAdapter) {
        boolean isDatabaseEmpty = true;
        Cursor cursor = databaseAdapter.query(" SELECT name FROM sqlite_master WHERE "
                + "type='table' and name!='android_metadata' and name!='sqlite_sequence'");
        int value = cursor.getColumnIndex("name");
        if (value != -1) {
            while (cursor.moveToNext()){
                String tableName = cursor.getString(value);
                Cursor resTable = databaseAdapter.query(
                        "SELECT * from " + tableName, null);
                if (resTable.getCount() > 0) {
                    isDatabaseEmpty = false;
                    break;
                }
            }
        }
        cursor.close();
        return isDatabaseEmpty;
    }

    public static Boolean ifValueExist(String tableName, String fieldName, String fieldValue,
            DatabaseAdapter db) {
        boolean isExist = false;
        Cursor cursor = db.query(
                "SELECT " + fieldName + " from " + tableName + " where " + fieldName + " = '"
                        + fieldValue + "'", null);
        int value = cursor.getCount();
        if (value == 1) {
            isExist = true;
        }
        cursor.close();
        return isExist;
    }

    public static boolean isFieldExist(String tableName, String fieldName,  DatabaseAdapter db) {
        boolean isExist = false;
        Cursor cursor = db.query("PRAGMA table_info(" + tableName + ")", null);
        int value = cursor.getColumnIndex("name");
        if (value != -1) {
            while (cursor.moveToNext()) {
                if (cursor.getString(value).equals(fieldName)) {
                    isExist = true;
                    break;
                }
            }
        }
        cursor.close();
        return isExist;
    }

    public static boolean ifTableExist(String table, DatabaseAdapter db) {
        boolean isExist = false;
        Cursor cursor = db.query("PRAGMA table_info(" + table + ")");
        int itemsCount = cursor.getCount();
        if (itemsCount > 0) {
            isExist = true;
        }
        cursor.close();
        return isExist;
    }
}
@@ -2622,33 +2622,8 @@
 ery(
-%0A                        
 %22SEL
@@ -2646,22 +2646,16 @@
 ableName
-, null
 );%0A     
@@ -2826,32 +2826,32 @@
 cursor.close();%0A
+
         return i
@@ -2877,1062 +2877,8 @@
  %7D%0A%0A
-    public static Boolean ifValueExist(String tableName, String fieldName, String fieldValue,%0A            DatabaseAdapter db) %7B%0A        boolean isExist = false;%0A        Cursor cursor = db.query(%0A                %22SELECT %22 + fieldName + %22 from %22 + tableName + %22 where %22 + fieldName + %22 = '%22%0A                        + fieldValue + %22'%22, null);%0A        int value = cursor.getCount();%0A        if (value == 1) %7B%0A            isExist = true;%0A        %7D%0A        cursor.close();%0A        return isExist;%0A    %7D%0A%0A    public static boolean isFieldExist(String tableName, String fieldName,  DatabaseAdapter db) %7B%0A        boolean isExist = false;%0A        Cursor cursor = db.query(%22PRAGMA table_info(%22 + tableName + %22)%22, null);%0A        int value = cursor.getColumnIndex(%22name%22);%0A        if (value != -1) %7B%0A            while (cursor.moveToNext()) %7B%0A                if (cursor.getString(value).equals(fieldName)) %7B%0A                    isExist = true;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        cursor.close();%0A        return isExist;%0A    %7D%0A%0A
     

Merge remote-tracking branch 'origin/develop' into androsdk-848