| /* |
| * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
| * |
| * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License as published by |
| * the Free Software Foundation; either version 3 of the License, or |
| * (at your option) any later version. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| * |
| * Additional permission under GNU GPL version 3 section 7: |
| * |
| * If you modify this program, or any covered work, by linking or |
| * combining it with the OpenSSL project's OpenSSL library (or a |
| * modified version of that library), containing parts covered by the |
| * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| * grants you additional permission to convey the resulting work. |
| * Corresponding Source for a non-source form of such a combination |
| * shall include the source code for the parts of OpenSSL used as well |
| * as that of the covered work. |
| */ |
| |
| |
| package org.sflphone.history; |
| |
| 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 java.sql.SQLException; |
| |
| /** |
| * Database helper class used to manage the creation and upgrading of your database. This class also usually provides |
| * the DAOs used by the other classes. |
| */ |
| 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 = "history.db"; |
| // any time you make changes to your database objects, you may have to increase the database version |
| private static final int DATABASE_VERSION = 2; |
| |
| // the DAO object we use to access the SimpleData table |
| private Dao<HistoryCall, Integer> historyDao = null; |
| |
| public DatabaseHelper(Context context) { |
| super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| } |
| |
| /** |
| * This is called when the database is first created. Usually you should call createTable statements here to create |
| * the tables that will store your data. |
| */ |
| @Override |
| public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { |
| try { |
| Log.i(DatabaseHelper.class.getName(), "onCreate"); |
| TableUtils.createTable(connectionSource, HistoryCall.class); |
| } catch (SQLException e) { |
| Log.e(DatabaseHelper.class.getName(), "Can't create database", e); |
| throw new RuntimeException(e); |
| } |
| } |
| |
| /** |
| * This is called when your application is upgraded and it has a higher version number. This allows you to adjust |
| * the various data to match the new version number. |
| */ |
| @Override |
| public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) { |
| try { |
| Log.i(DatabaseHelper.class.getName(), "onUpgrade"); |
| TableUtils.dropTable(connectionSource, HistoryCall.class, true); |
| // after we drop the old databases, we create the new ones |
| onCreate(db, connectionSource); |
| } catch (SQLException e) { |
| Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e); |
| throw new RuntimeException(e); |
| } |
| } |
| |
| /** |
| * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached |
| * value. |
| */ |
| public Dao<HistoryCall, Integer> getHistoryDao() throws SQLException { |
| if (historyDao == null) { |
| historyDao = getDao(HistoryCall.class); |
| } |
| return historyDao; |
| } |
| |
| /** |
| * Close the database connections and clear any cached DAOs. |
| */ |
| @Override |
| public void close() { |
| super.close(); |
| historyDao = null; |
| } |
| } |