package mil.nga.geopackage.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.AndroidConnectionSource;
import com.j256.ormlite.support.ConnectionSource;

/**
 * GeoPackage Android Connection wrapper
 *
 * @author osbornb
 */
public class GeoPackageConnection extends GeoPackageCoreConnection {

    /**
     * Database connection
     */
    private final SQLiteDatabase db;

    /**
     * Connection source
     */
    private final ConnectionSource connectionSource;

    /**
     * Constructor
     *
     * @param db
     */
    public GeoPackageConnection(SQLiteDatabase db) {
        this.db = db;
        this.connectionSource = new AndroidConnectionSource(db);
    }

    /**
     * Get the database connection
     *
     * @return
     */
    public SQLiteDatabase getDb() {
        return db;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ConnectionSource getConnectionSource() {
        return connectionSource;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execSQL(String sql) {
        db.execSQL(sql);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int delete(String table, String whereClause, String[] whereArgs) {
        return db.delete(table, whereClause, whereArgs);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int count(String table, String where, String[] args) {

        StringBuilder countQuery = new StringBuilder();
        countQuery.append("select count(*) from " + table);
        if (where != null) {
            countQuery.append(" where ").append(where);
        }

        Cursor countCursor = db.rawQuery(countQuery.toString(), args);
        countCursor.moveToFirst();
        int count = countCursor.getInt(0);
        countCursor.close();
        return count;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void close() {
        connectionSource.closeQuietly();
        db.close();
    }

}

@@ -1661,88 +1661,1672 @@
   %7D%0A
-%0A        Cursor countCursor = db.rawQuery(countQuery.toString(), args);%0A        
+        String sql = countQuery.toString();%0A%0A        int count = singleResultQuery(sql, args);%0A%0A        return count;%0A    %7D%0A%0A    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public Integer min(String table, String column, String where, String%5B%5D args) %7B%0A%0A        Integer min = null;%0A        if (count(table, where, args) %3E 0) %7B%0A            StringBuilder minQuery = new StringBuilder();%0A            minQuery.append(%22select min(%22).append(column).append(%22) from %22)%0A                    .append(table);%0A            if (where != null) %7B%0A                minQuery.append(%22 where %22).append(where);%0A            %7D%0A            String sql = minQuery.toString();%0A%0A            min = singleResultQuery(sql, args);%0A        %7D%0A%0A        return min;%0A    %7D%0A%0A    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public Integer max(String table, String column, String where, String%5B%5D args) %7B%0A%0A        Integer max = null;%0A        if (count(table, where, args) %3E 0) %7B%0A            StringBuilder maxQuery = new StringBuilder();%0A            maxQuery.append(%22select max(%22).append(column).append(%22) from %22)%0A                    .append(table);%0A            if (where != null) %7B%0A                maxQuery.append(%22 where %22).append(where);%0A            %7D%0A            String sql = maxQuery.toString();%0A%0A            max = singleResultQuery(sql, args);%0A        %7D%0A%0A        return max;%0A    %7D%0A%0A    /**%0A     * Query the SQL for a single integer result%0A     *%0A     * @param sql%0A     * @param args%0A     * @return int result%0A     */%0A    private int singleResultQuery(String sql, String%5B%5D args) %7B%0A%0A        Cursor countCursor = db.rawQuery(sql, args);%0A%0A        int result = 0;%0A        try %7B%0A            if (
 coun
@@ -3346,26 +3346,30 @@
 oFirst()
-;%0A
+) %7B%0A  
         
 int coun
@@ -3356,32 +3356,35 @@
 %7B%0A          
-int coun
+      resul
 t = countCur
@@ -3398,16 +3398,54 @@
 Int(0);%0A
+            %7D%0A        %7D finally %7B%0A    
         
@@ -3461,24 +3461,35 @@
 or.close();%0A
+        %7D%0A%0A
         retu
@@ -3483,36 +3483,37 @@
 %0A        return 
-coun
+resul
 t;%0A    %7D%0A%0A    /*

Support min and max queries on a column, FIXES #15