package com.salvadordalvik.fastlibrary.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * binfeed
 * Created by Matthew Shepard on 11/25/13.
 */
public abstract class FastDatabase extends SQLiteOpenHelper {

    public FastDatabase(Context context, String name, int version) {
        this(context, name, null, version);
    }

    public FastDatabase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public int insertRows(String table, ContentValues... values){
        return insertRows(table, SQLiteDatabase.CONFLICT_ABORT, values);
    }

    public int insertRows(String table, int conflictAction, ContentValues... values){
        int rows = 0;
        synchronized (this){
            SQLiteDatabase db = getWritableDatabase();
            db.beginTransaction();
            for(ContentValues value : values){
                if(db.insertWithOnConflict(table, null, null, conflictAction) >= 0){
                    rows++;
                }
            }
            if(rows > 0){
                db.setTransactionSuccessful();
            }
            db.endTransaction();
        }
        return rows;
    }

    /**
     * Query call for a dynamic number of arguments. It's a convenience call for HipQueryTask.<br/>
     * Can take any number of arguments, in this order:<br/>
     * table/view<br/>
     * sort, optional - can be null<br/>
     * where string<br/>
     * where argument 1<br/>
     * where argument 2<br/>
     * where argument N...<br/>
     * <br/>
     * Any arguments after the first (table) are optional and can be skipped.<br/>
     * This call does not support group/having/column arguments, use a DB view or the other query calls.
     * @param args
     * @return
     */
    public Cursor query(String... args){
        switch (args.length){
            case 0:
                throw new IllegalArgumentException("You must include at least one argument (table). See HipDatabase.query(string[])");
            case 1:
                return query(args[0], null, null, null, null, null, null);
            case 2:
                return query(args[0], null, null, null, null, null, args[1]);
            case 3:
                return query(args[0], null, args[2], null, null, null, args[1]);
        }
        String[] whereArgs = new String[args.length-3];
        //we could use Arrays.copyOf here, but it's only available from API9, this app targets API8
        for(int ix=3; ix<args.length; ix++){
            whereArgs[ix-3] = args[ix];
        }
        return query(args[0], null, args[2], whereArgs, null, null, args[1]);
    }

    public Cursor query(String tableView, String[] columns, String where, String[] whereArgs, String group, String having, String sort){
        synchronized (this){
            Cursor result = getReadableDatabase().query(tableView, columns, where, whereArgs, group, having, sort);
            return result;
        }
    }

    public int deleteRows(String table, String where, String... whereArgs) {
        synchronized (this){
            int rows = getWritableDatabase().delete(table, where, whereArgs);
            return rows;
        }
    }

    public Cursor rawQuery(String query, String... selectArgs){
        synchronized (this){
            return getWritableDatabase().rawQuery(query, selectArgs);
        }
    }

    public void execSQL(String sql){
        synchronized (this){
            getWritableDatabase().execSQL(sql);
        }
    }
}

@@ -238,16 +238,40 @@
 elper;%0A%0A
+import java.util.List;%0A%0A
 /**%0A * b
@@ -661,32 +661,181 @@
 ersion);%0A    %7D%0A%0A
+    public int insertRows(String table, List%3CContentValues%3E values)%7B%0A        return insertRows(table, SQLiteDatabase.CONFLICT_ABORT, values);%0A    %7D%0A%0A
     public int i
@@ -1294,20 +1294,602 @@
 , null, 
-null
+value, conflictAction) %3E= 0)%7B%0A                    rows++;%0A                %7D%0A            %7D%0A            if(rows %3E 0)%7B%0A                db.setTransactionSuccessful();%0A            %7D%0A            db.endTransaction();%0A        %7D%0A        return rows;%0A    %7D%0A%0A    public int insertRows(String table, int conflictAction, List%3CContentValues%3E values)%7B%0A        int rows = 0;%0A        synchronized (this)%7B%0A            SQLiteDatabase db = getWritableDatabase();%0A            db.beginTransaction();%0A            for(ContentValues value : values)%7B%0A                if(db.insertWithOnConflict(table, null, value
 , confli

Add db list insert.