package com.chaincloud.chaincloudv.dao;

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

import com.chaincloud.chaincloudv.model.Address;
import com.chaincloud.chaincloudv.model.AddressBatch;
import com.chaincloud.chaincloudv.model.Channel;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/**
 * Created by zhumingu on 16/6/16.
 */
public class ORMLiteDBHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "chaincloud-v.db";
    private static final int DATABASE_VERSION = 3;

    /**
     * constructor
     * @param context
     */
    public ORMLiteDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, Channel.class);
            TableUtils.createTable(connectionSource, AddressBatch.class);
            TableUtils.createTable(connectionSource, Address.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource, Address.class, true);
            TableUtils.dropTable(connectionSource, AddressBatch.class, true);

            TableUtils.createTable(connectionSource, AddressBatch.class);
            TableUtils.createTable(connectionSource, Address.class);

            onCreate(database, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

@@ -332,16 +332,93 @@
 Helper;%0A
+import com.j256.ormlite.dao.Dao;%0Aimport com.j256.ormlite.stmt.UpdateBuilder;%0A
 import c
@@ -759,9 +759,9 @@
 N = 
-3
+4
 ;%0A%0A 
@@ -1509,66 +1509,99 @@
     
-TableUtils.dropTable(connectionSource, 
+if (oldVersion == 3) %7B%0A                Dao%3CAddressBatch, ?%3E dao = getDao(
 Address
+Batch
 .class
-, true
 );%0A 
@@ -1615,219 +1615,202 @@
     
-TableUtils.dropTable(connectionSource, AddressBatch.class, true);%0A%0A            TableUtils.createTable(connectionSource, AddressBatch.class);%0A            TableUtils.createTabl
+    dao.executeRaw(%22ALTER TABLE %60AddressBatch%60 ADD COLUMN coin STRING;%22);%0A%0A                UpdateBuilder builder = dao.updateBuilder();%0A                builder.updateColumnValu
 e(
+%22
 co
-nnectionSource, Address.class
+in%22, %22BTC%22
 );%0A
-%0A
     
@@ -1821,45 +1821,43 @@
     
-onCreate(database, connectionSource);
+    builder.update();%0A            %7D
 %0A   

upgrade db bugs