From 1e0c867217070975d6145db9786d50704174b860 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 2 Nov 2017 14:27:56 +0100 Subject: [PATCH] Check if a user is present in the local database or not --- .../comunic/client/data/DatabaseContract.java | 2 +- .../comunic/client/data/DatabaseHelper.java | 16 ++-- .../client/data/{ => UsersInfo}/UserInfo.java | 2 +- .../data/UsersInfo/UsersInfosDbHelper.java | 73 +++++++++++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-) rename app/src/main/java/org/communiquons/android/comunic/client/data/{ => UsersInfo}/UserInfo.java (96%) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.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 index dc5a4ff..e4090ca 100644 --- 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 @@ -19,7 +19,7 @@ public final class DatabaseContract { public static final String DATABASE_NAME = "database.db"; /* Users info table */ - public static abstract class UsersInfo implements BaseColumns { + public static abstract class UsersInfoSchema implements BaseColumns { public static final String TABLE_NAME = "users_info"; public static final String COLUMN_NAME_USER_ID = "user_id"; 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 index 8c7f150..4f8b365 100644 --- 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 @@ -4,7 +4,7 @@ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfo; +import org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfoSchema; /** * Database helper. This file handles the creation / upgrade of the local database @@ -26,16 +26,16 @@ public class DatabaseHelper extends SQLiteOpenHelper { * 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 + "CREATE TABLE " + UsersInfoSchema.TABLE_NAME + " (" + + UsersInfoSchema._ID + " INTEGER PRIMARY KEY," + + UsersInfoSchema.COLUMN_NAME_USER_ID + INTEGER_TYPE + COMMA_SEP + + UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME + TEXT_TYPE + COMMA_SEP + + UsersInfoSchema.COLUMN_NAME_USER_LASTNAME + TEXT_TYPE + COMMA_SEP + + UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE + TEXT_TYPE + " )"; private static final String SQL_DELETE_USERS_INFOS_TABLE = "DROP TABLE IF EXISTS " + - UsersInfo.TABLE_NAME; + UsersInfoSchema.TABLE_NAME; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UserInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java similarity index 96% rename from app/src/main/java/org/communiquons/android/comunic/client/data/UserInfo.java rename to app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java index 696586a..2b00fb5 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UserInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java @@ -1,4 +1,4 @@ -package org.communiquons.android.comunic.client.data; +package org.communiquons.android.comunic.client.data.UsersInfo; /** * This class contains the informations about a single user diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java new file mode 100644 index 0000000..1dc89fa --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java @@ -0,0 +1,73 @@ +package org.communiquons.android.comunic.client.data.UsersInfo; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +import org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfoSchema; +import org.communiquons.android.comunic.client.data.DatabaseHelper; + +/** + * Users informations helpers + * + * Makes the interface between the UsersInfo object and the SQLite object + * + * @author Pierre HUBERT + * Created by pierre on 11/2/17. + */ + +public class UsersInfosDbHelper { + + /** + * Database helper + */ + private DatabaseHelper dbHelper; + + /** + * Class constructor + * + * @param dbHelper Database helper object + */ + public UsersInfosDbHelper(DatabaseHelper dbHelper){ + this.dbHelper = dbHelper; + } + + /** + * Check wether a user is present in the database or not + * + * @param userID The user to research on the database + * @return boolean True if the user exists / false else + */ + public boolean exists(int userID){ + + //Get the database + SQLiteDatabase db = dbHelper.getReadableDatabase(); + + //Define the condition of the research + String projection[] = { + UsersInfoSchema._ID + }; + + String selection = UsersInfoSchema.COLUMN_NAME_USER_ID + " = ?"; + String[] selectionArgs = {""+userID}; + + String sortOrder = UsersInfoSchema._ID; + + //Perform the request on the database + Cursor c = db.query( + UsersInfoSchema.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + sortOrder + ); + + int number_entries = c.getCount(); + + //Close cursor + c.close(); + + return number_entries > 0; + } +}