// Path: easysqlite/src/main/java/com/tonilopezmr/easysqlite/exception/SQLiteHelperException.java
// public class SQLiteHelperException extends Exception {
// 
//     public SQLiteHelperException(String detailMessage) {
//         super(detailMessage);
//     }
// }


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.tonilopezmr.easysqlite.exception.SQLiteHelperException;
/*
 * Copyright 2015 Antonio López Marín <tonilopezmr.com>
 *
 * 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.tonilopezmr.easysqlite;



/**
 * @author Antonio López Marín
 */
public final class SQLiteHelper extends SQLiteOpenHelper{

    private Builder builder;

    private SQLiteHelper(Builder builder) {
        super(builder.context, builder.databaseName, builder.factory, builder.databaseVersion);

        this.builder = builder;
    }

    /**
     * Database version.
     *
     * @return the version of database SQLite.
     */
    public int getDatabaseVersion(){
        return builder.databaseVersion;
    }

    /**
     * If the foreign keys in SQLite are enable.
     *
     * @return true if the foreign keys are enable or false if it is not the case.
     */
    public boolean isOnForeignKey(){
        return builder.isOnForeignKey;
    }

    private void executePragma(SQLiteDatabase db){
        if (builder.isOnForeignKey) db.execSQL(Builder.FOREIGN_KEY_ON);
    }

    /**
     * Called when the database is created for the first time. This is where the
     * creation of tables and the initial population of the tables should happen.
     *
     * @param db The database.
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            if(builder.tables == null){
// Path: easysqlite/src/main/java/com/tonilopezmr/easysqlite/exception/SQLiteHelperException.java
// public class SQLiteHelperException extends Exception {
// 
//     public SQLiteHelperException(String detailMessage) {
//         super(detailMessage);
//     }
// }


// Path: easysqlite/src/main/java/com/tonilopezmr/easysqlite/SQLiteHelper.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.tonilopezmr.easysqlite.exception.SQLiteHelperException;

/*
 * Copyright 2015 Antonio López Marín <tonilopezmr.com>
 *
 * 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.tonilopezmr.easysqlite;



/**
 * @author Antonio López Marín
 */
public final class SQLiteHelper extends SQLiteOpenHelper{

    private Builder builder;

    private SQLiteHelper(Builder builder) {
        super(builder.context, builder.databaseName, builder.factory, builder.databaseVersion);

        this.builder = builder;
    }

    /**
     * Database version.
     *
     * @return the version of database SQLite.
     */
    public int getDatabaseVersion(){
        return builder.databaseVersion;
    }

    /**
     * If the foreign keys in SQLite are enable.
     *
     * @return true if the foreign keys are enable or false if it is not the case.
     */
    public boolean isOnForeignKey(){
        return builder.isOnForeignKey;
    }

    private void executePragma(SQLiteDatabase db){
        if (builder.isOnForeignKey) db.execSQL(Builder.FOREIGN_KEY_ON);
    }

    /**
     * Called when the database is created for the first time. This is where the
     * creation of tables and the initial population of the tables should happen.
     *
     * @param db The database.
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            if(builder.tables == null){
                throw new SQLiteHelperException("The array of String tables can't be null!!");