From 24aa80f63ec2ca72d9c6ccf1b3230c20313d7932 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 2 Nov 2017 11:42:25 +0100 Subject: [PATCH] Created locale database contract and helper --- .../comunic/client/data/DatabaseContract.java | 32 ++++++++ .../comunic/client/data/DatabaseHelper.java | 73 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseContract.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseHelper.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseContract.java b/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseContract.java new file mode 100644 index 0000000..dc5a4ff --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseContract.java @@ -0,0 +1,32 @@ +package org.communiquons.android.comunic.client.data; + +import android.provider.BaseColumns; + +/** + * Database contract schema. Contains the structure of the application database + * + * @author Pierre HUBERT + * Created by pierre on 11/2/17. + */ + +public final class DatabaseContract { + + //Empty constructor : avoid accidentally instantiations of the class + public DatabaseContract(){} + + /* Database basic information */ + public static final int DATABASE_VERSION = 1; + public static final String DATABASE_NAME = "database.db"; + + /* Users info table */ + public static abstract class UsersInfo implements BaseColumns { + public static final String TABLE_NAME = "users_info"; + + public static final String COLUMN_NAME_USER_ID = "user_id"; + public static final String COLUMN_NAME_USER_FIRSTNAME = "first_name"; + public static final String COLUMN_NAME_USER_LASTNAME = "last_name"; + public static final String COLUMN_NAME_USER_ACCOUNT_IMAGE = "account_image"; + + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseHelper.java new file mode 100644 index 0000000..8c7f150 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/DatabaseHelper.java @@ -0,0 +1,73 @@ +package org.communiquons.android.comunic.client.data; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfo; + +/** + * Database helper. This file handles the creation / upgrade of the local database + * + * @author Pierre HUBERT + * Created by pierre on 11/2/17. + */ + +public class DatabaseHelper extends SQLiteOpenHelper { + + /** + * Generic definitions + */ + private static final String TEXT_TYPE = " TEXT"; + private static final String INTEGER_TYPE = " INTEGER"; + private static final String COMMA_SEP = ","; + + /** + * Creation and deletion of the users info table + */ + private static final String SQL_CREATE_USERS_INFOS_TABLE = + "CREATE TABLE " + UsersInfo.TABLE_NAME + " (" + + UsersInfo._ID + " INTEGER PRIMARY KEY," + + UsersInfo.COLUMN_NAME_USER_ID + INTEGER_TYPE + COMMA_SEP + + UsersInfo.COLUMN_NAME_USER_FIRSTNAME + TEXT_TYPE + COMMA_SEP + + UsersInfo.COLUMN_NAME_USER_LASTNAME + TEXT_TYPE + COMMA_SEP + + UsersInfo.COLUMN_NAME_USER_ACCOUNT_IMAGE + TEXT_TYPE + + " )"; + + private static final String SQL_DELETE_USERS_INFOS_TABLE = "DROP TABLE IF EXISTS " + + UsersInfo.TABLE_NAME; + + + + + + /** + * Public constructor + * @param context The context where the database is used + */ + public DatabaseHelper(Context context){ + super(context, DatabaseContract.DATABASE_NAME, null, DatabaseContract.DATABASE_VERSION); + } + + /** + * Handles database creation + * @param db The database + */ + @Override + public void onCreate(SQLiteDatabase db) { + //Create user informations table + db.execSQL(SQL_CREATE_USERS_INFOS_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + //Delete users informations table + db.execSQL(SQL_DELETE_USERS_INFOS_TABLE); + onCreate(db); + } + + @Override + public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { + onUpgrade(db, oldVersion, newVersion); + } +}