package com.littlepanpc.downloader.db;

import com.littlepanpc.downloader.util.LogUtils;

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

/**
 * @class: DownloadDBHelper
 * @Description: TODO
 * @author: Peter Pan
 * @email: happychinapc@gmail.com
 * @date: 2014-5-19 下午6:07:39
 * @since: 1.0.0
 *
 */
public class DownloadDBHelper extends SQLiteOpenHelper {
	private static final String TAG = DownloadDBHelper.class.getSimpleName();

	public static final String DB_NAME = "downloads.db";
	public static final String DB_TABLE_NAME = "download_task";
	public static final int DB_VERSION = 1;

	private static DownloadDBHelper instance;

	/**
	 * @Title: getInstance
	 * @Description: concurrent singleTon
	 * @param context
	 * @return
	 * @return DownloadDBHelper
	 * @throws 
	 */
	public static DownloadDBHelper getInstance(Context context) {
		if (instance == null) {
			synchronized (DownloadDBHelper.class) {
				if (instance == null) {
					instance = new DownloadDBHelper(context);
				}
			}
		}
		LogUtils.i(TAG, "getInstance---"+instance);
		return instance;
	}

	/**
	 * <p>Title: </p>
	 * <p>Description: </p>
	 *
	 * @param context
	 */
	private DownloadDBHelper(Context context) {
		// TODO Auto-generated constructor stub
		super(context, DB_NAME, null, DB_VERSION);
		LogUtils.i(TAG, "DownloadDBHelper constructor");
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		LogUtils.i(TAG, "onCreate start");
		String createSQL = "create table " + DB_TABLE_NAME + "("
				+ "_id integer primary key autoincrement, "
				+ "url text unique, " + "parent_path text, "
				+ "file_name text, " + "downloaded_size integer, "
				+ "total_size integer, " + "status integer)";
		LogUtils.i(TAG, "createsql---"+createSQL);
		LogUtils.i(TAG, "onCreate before execute");
		db.execSQL(createSQL);
		LogUtils.i(TAG, "onCreate after execute");
		LogUtils.i(TAG, "onCreate finish");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		String dropSQL = "drop table if exists " + DB_TABLE_NAME;
		LogUtils.i(TAG, "dropsql---"+dropSQL);
		LogUtils.i(TAG, "onUpgrade before execute");
		db.execSQL(dropSQL);
		LogUtils.i(TAG, "onUpgrade after execute");
		onCreate(db);
		LogUtils.i(TAG, "onUpgrade finish");
	}

}

@@ -261,20 +261,66 @@
 iption: 
-TODO
+create or update database table---%5C%22downloads.db%5C%22
 %0A * @aut
@@ -823,16 +823,47 @@
 ingleTon
+---double checking lock pattern
 %0A%09 * @pa
@@ -1268,16 +1268,27 @@
 iption: 
+Constructor
 %3C/p%3E%0A%09 *
@@ -1494,25 +1494,235 @@
 uctor%22);%0A%09%7D%0A
+%09%0A%09/* (non-Javadoc)%0A%09 * %3Cp%3ETitle: onCreate%3C/p%3E%0A%09 * %3Cp%3EDescription: create database table%3C/p%3E%0A%09 * @param db%0A%09 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)%0A%09 */
 %0A
-
 %09@Override%0A%09
@@ -2301,16 +2301,281 @@
 %22);%0A%09%7D%0A%0A
+%09/* (non-Javadoc)%0A%09 * %3Cp%3ETitle: onUpgrade%3C/p%3E%0A%09 * %3Cp%3EDescription: update database table%3C/p%3E%0A%09 * @param db%0A%09 * @param oldVersion%0A%09 * @param newVersion%0A%09 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)%0A%09 */%0A
 %09@Overri

Update DownloadDBHelper.java