/* * Copyright 2018 Daniel Underhay & Matthew Daley. * * This file is part of Walrus. * * Walrus 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. * * Walrus 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 Walrus. If not, see <http://www.gnu.org/licenses/>. */ package com.bugfuzz.android.projectwalrus.card; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.support.annotation.Keep; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.dao.RuntimeExceptionDao; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; import java.sql.SQLException; public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_NAME = "project-walrus.db"; private static final int DATABASE_VERSION = 1; private RuntimeExceptionDao<Card, Integer> cardDao; @Keep public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, Card.class); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) { } @Override public void close() { cardDao = null; super.close(); } public RuntimeExceptionDao<Card, Integer> getCardDao() { if (cardDao == null) { try { cardDao = RuntimeExceptionDao.createDao(getConnectionSource(), Card.class); } catch (SQLException e) { throw new RuntimeException(e); } } return cardDao; } }