mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Check if a user is present in the local database or not
This commit is contained in:
		@@ -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";
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user