/* * Copyright (C) 2015 The Android Open Source Project * * 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.vinidsl.googleioextended.helper; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; import com.vinidsl.googleioextended.model.Speaker; import com.vinidsl.googleioextended.model.Talk; import java.sql.SQLException; /** * Created by Vinicius on 09-06-15. */ public class DataBaseHelper extends OrmLiteSqliteOpenHelper { // name of the database file for your application -- change to something appropriate for your app private static final String DATABASE_NAME = "GdgBoliviaExtended.db"; // any time you make changes to your database objects, you may have to increase the database version private static final int DATABASE_VERSION = 1; // the DAO object we use to access the SimpleData table private Dao<Talk,String> talkDao = null; private Dao<Speaker, String> speakerDao = null; public DataBaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try{ TableUtils.createTableIfNotExists(connectionSource, Speaker.class); Log.i(DataBaseHelper.class.getName(), "created speaker table"); TableUtils.createTableIfNotExists(connectionSource, Talk.class); Log.i(DataBaseHelper.class.getName(), "created talk table"); } catch (SQLException e) { Log.e(DataBaseHelper.class.getName(), "Can't create database", e); throw new RuntimeException(e); } } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) { try{ TableUtils.dropTable(connectionSource, Talk.class, true); TableUtils.dropTable(connectionSource, Speaker.class, true); // after we drop the old databases, we create the new ones onCreate(sqLiteDatabase, connectionSource); } catch (SQLException e) { Log.e(DataBaseHelper.class.getName(), "Can't drop databases", e); throw new RuntimeException(e); } } /** * Close the database connections and clear any cached DAOs. */ @Override public void close() { super.close(); talkDao = null; speakerDao = null; } /** * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached * value. */ public Dao<Talk, String> getTalkDao() throws SQLException { if (talkDao == null) { talkDao = getDao(Talk.class); } return talkDao; } public Dao<Speaker, String> getSpeakerDao() throws SQLException { if (speakerDao == null) { speakerDao = getDao(Speaker.class); } return speakerDao; } }