package com.usai.util;

//import android.R.array;
import java.util.ArrayList;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SqlOpenHelper extends SQLiteOpenHelper
{

	private ArrayList<String>	initSQL	= new ArrayList<String>();

	public void addInitSQL(String sql)
	{
		initSQL.add(sql);

	}

	public int get_RecordCount(String tablename)
	{
		return 0;

	}

	public SQLiteDatabase OpenDB(boolean writable)
	{
		if (writable)
			return getWritableDatabase();
		else
			return getReadableDatabase();
	}

	private void initDB(SQLiteDatabase db)
	{
		for (int i = 0; i < initSQL.size(); i++)
		{
			db.execSQL(initSQL.get(i));
		}

	}

	public SqlOpenHelper(Context context, String name, CursorFactory factory,
			int version)
	{
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		// TODO Auto-generated method stub
		initDB(db);

	}

	@Override
	public void onOpen(SQLiteDatabase db)
	{
		// TODO Auto-generated method stub
		super.onOpen(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		Log.d("DB upgrade", "from " + oldVersion + " to " + newVersion);

		if (newVersion == 2)
		{
			db.execSQL("CREATE TABLE push_message ("
					+ "_id INTEGER PRIMARY KEY," + "s_id VARCHAR(20),"
					+ "e_id VARCHAR(20)," + "msgcount INTEGER,"
					+ "message VARCHAR(20)," + "h_time timestamp,"
					+ "create_time timestamp," + "user VARCHAR(20),"
					+ "read BOOLEAN)");
			db.execSQL("CREATE TABLE favorites (" + "_id INTEGER PRIMARY KEY,"
					+ "name VARCHAR(20)," + "params VARCHAR(20),"
					+ "action VARCHAR(20)," + "module_name VARCHAR(20),"
					+ "detail_id VARCHAR(20)," + "action_type VARCHAR(20),"
					+ "create_time timestamp," + "user VARCHAR(20))");
			db.execSQL("CREATE TABLE history (" + "_id INTEGER PRIMARY KEY,"
					+ "name VARCHAR(20)," + "params VARCHAR(20),"
					+ "action VARCHAR(20)," + "module_name VARCHAR(20),"
					+ "criterion VARCHAR(20)," + "create_time timestamp,"
					+ "user VARCHAR(20))");
			db.execSQL("ALTER TABLE search_history ADD level INTEGER");
		}

	}

}
