package net.beshkenadze.beutils.db;


import android.database.SQLException;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.table.TableUtils;

import java.util.List;

/**
 * Created by akira on 30.09.13.
 */
public class DatabaseTableHelper {
    private Dao mDao = null;
    private DatabaseHelper mHelper;
    private Class<?> mClass;
    protected Dao<Object, Integer> getDao() {
        return mDao;
    }
    public DatabaseTableHelper(DatabaseHelper h, Class<?> c) {

        mHelper = h;
        mClass = c;

        try {
            mDao = mHelper.getDao(mClass);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean add(Object mObject) {
        try {
            getDao().create(mObject);
            return true;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void update(Object mObject) {
        try {
            getDao().update(mObject);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public List<?> getAll() {
        try {
            return getDao().queryForAll();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void flush() {
        try {
            TableUtils.clearTable(mHelper.getConnectionSource(), mClass);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void remove(Object mObject) {
        if (mObject != null) {
            try {
                getDao().delete(mObject);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (java.sql.SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

@@ -340,16 +340,17 @@
 mClass;%0A
+%0A
     prot
@@ -410,24 +410,25 @@
 mDao;%0A    %7D%0A
+%0A
     public D
@@ -1051,32 +1051,33 @@
         %7D%0A    %7D%0A
+%0A
     public void 
@@ -1326,32 +1326,571 @@
         %7D%0A    %7D%0A
+%0A    public Object findById(int id) %7B%0A        try %7B%0A            return getDao().queryForId(id);%0A        %7D catch (SQLException e) %7B%0A            throw new RuntimeException(e);%0A        %7D catch (java.sql.SQLException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    public long count() %7B%0A        try %7B%0A            return getDao().countOf();%0A        %7D catch (SQLException e) %7B%0A            throw new RuntimeException(e);%0A        %7D catch (java.sql.SQLException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A
     public List%3C
@@ -2431,22 +2431,23 @@
       %7D%0A
-
     %7D%0A
+%0A
     publ

Add findById and count methods.