blob: a623e989db1272c74a64616aa8c9530c51327bd2 [file] [log] [blame]
Adrien BĂ©raud04d822c2015-04-02 17:44:36 -04001/*
2 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31
32
33package cx.ring.history;
34
35import android.content.Context;
36import android.database.sqlite.SQLiteDatabase;
37import android.util.Log;
38import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
39import com.j256.ormlite.dao.Dao;
40import com.j256.ormlite.support.ConnectionSource;
41import com.j256.ormlite.table.TableUtils;
42
43import java.sql.SQLException;
44
45/**
46 * Database helper class used to manage the creation and upgrading of your database. This class also usually provides
47 * the DAOs used by the other classes.
48 */
49public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
50
51 // name of the database file for your application -- change to something appropriate for your app
52 private static final String DATABASE_NAME = "history.db";
53 // any time you make changes to your database objects, you may have to increase the database version
54 private static final int DATABASE_VERSION = 2;
55
56 // the DAO object we use to access the SimpleData table
57 private Dao<HistoryCall, Integer> historyDao = null;
58
59 public DatabaseHelper(Context context) {
60 super(context, DATABASE_NAME, null, DATABASE_VERSION);
61 }
62
63 /**
64 * This is called when the database is first created. Usually you should call createTable statements here to create
65 * the tables that will store your data.
66 */
67 @Override
68 public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
69 try {
70 Log.i(DatabaseHelper.class.getName(), "onCreate");
71 TableUtils.createTable(connectionSource, HistoryCall.class);
72 } catch (SQLException e) {
73 Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
74 throw new RuntimeException(e);
75 }
76 }
77
78 /**
79 * This is called when your application is upgraded and it has a higher version number. This allows you to adjust
80 * the various data to match the new version number.
81 */
82 @Override
83 public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
84 try {
85 Log.i(DatabaseHelper.class.getName(), "onUpgrade");
86 TableUtils.dropTable(connectionSource, HistoryCall.class, true);
87 // after we drop the old databases, we create the new ones
88 onCreate(db, connectionSource);
89 } catch (SQLException e) {
90 Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
91 throw new RuntimeException(e);
92 }
93 }
94
95 /**
96 * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached
97 * value.
98 */
99 public Dao<HistoryCall, Integer> getHistoryDao() throws SQLException {
100 if (historyDao == null) {
101 historyDao = getDao(HistoryCall.class);
102 }
103 return historyDao;
104 }
105
106 /**
107 * Close the database connections and clear any cached DAOs.
108 */
109 @Override
110 public void close() {
111 super.close();
112 historyDao = null;
113 }
114}