/*
 * Copyright (C) 2012 Brian Muramatsu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.database;

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

public class DbHelper extends SQLiteOpenHelper {

    public static final String DATABASE_REDDIT = "reddit";
    public static final String DATABASE_TEST = "test";
    public static final int LATEST_VERSION = 2;

    /** Version kept to control what tables are created mostly for testing. */
    private final int version;

    public DbHelper(Context context, String name, int version) {
        super(context, name, null, version);
        this.version = version;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.beginTransaction();
        try {
            if (version > 1) {
                Subreddits.createSubredditsV2(db);
                Things.createTable(db);
                Comments.createTable(db);
                Replies.createTable(db);
                Votes.createTable(db);
                SubredditSearches.createTable(db);
            } else {
                Subreddits.createSubredditsV1(db);
            }
            Subreddits.insertDefaultSubreddits(db);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion == 1 && newVersion == 2) {
            db.beginTransaction();
            try {
                Subreddits.upgradeSubredditsV2(db);
                Things.createTable(db);
                Comments.createTable(db);
                Replies.createTable(db);
                Votes.createTable(db);
                SubredditSearches.createTable(db);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }
    }
}
@@ -1309,57 +1309,8 @@
 ) %7B%0A
-        db.beginTransaction();%0A        try %7B%0A    
     
@@ -1336,36 +1336,32 @@
 ) %7B%0A            
- 
-   
 Subreddits.creat
@@ -1371,36 +1371,32 @@
 bredditsV2(db);%0A
-    
             Thin
@@ -1419,36 +1419,32 @@
 b);%0A            
-    
 Comments.createT
@@ -1457,36 +1457,32 @@
 b);%0A            
-  
-  
 Replies.createTa
@@ -1482,36 +1482,32 @@
 reateTable(db);%0A
-    
             Vote
@@ -1529,36 +1529,32 @@
 b);%0A            
-    
 SubredditSearche
@@ -1580,20 +1580,16 @@
         
-  
-  
 %7D else %7B
@@ -1589,20 +1589,16 @@
  else %7B%0A
-    
         
@@ -1648,24 +1648,18 @@
     
-    %7D%0A  
+%7D%0A
         
   Su
@@ -1646,34 +1646,32 @@
       %7D%0A        
-  
 Subreddits.inser
@@ -1698,114 +1698,8 @@
 b);%0A
-            db.setTransactionSuccessful();%0A        %7D finally %7B%0A            db.endTransaction();%0A        %7D%0A
     
@@ -1848,65 +1848,8 @@
 ) %7B%0A
-            db.beginTransaction();%0A            try %7B%0A    
     
@@ -1896,36 +1896,32 @@
 b);%0A            
-    
 Things.createTab
@@ -1920,36 +1920,32 @@
 reateTable(db);%0A
-    
             Comm
@@ -1970,36 +1970,32 @@
 b);%0A            
-    
 Replies.createTa
@@ -2007,36 +2007,32 @@
 b);%0A            
-    
 Votes.createTabl
@@ -2030,36 +2030,32 @@
 reateTable(db);%0A
-    
             Subr
@@ -2089,130 +2089,8 @@
 b);%0A
-                db.setTransactionSuccessful();%0A            %7D finally %7B%0A                db.endTransaction();%0A            %7D%0A
     

Remove nested transactions in DbHelper