Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ada1d3a7 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Improve SyncedFileStateDAO

Rewrite getBySyncedFolderID(List<Long>)
Rewrite getByPath(String, boolean)
Remove useless 'this.'
Remove useless access modified (public|private)
Rewrite countFileWaitingForUploadForSyncedFolder() to replace double quote by simple quote
parent 97371a6a
Loading
Loading
Loading
Loading
+38 −60
Original line number Diff line number Diff line
@@ -38,12 +38,23 @@ import static foundation.e.drive.database.SyncedFileStateContract.SYNCEDFOLDER_I
 * @author Vincent Bourgmayer
 * Offers Query to CRUD operation for SyncedFIleState Object
 */
class SyncedFileStateDAO {
/* package */ class SyncedFileStateDAO {
    private SQLiteDatabase mDB;
    private final DbHelper mHelper;

    SyncedFileStateDAO(Context context){
        this.mHelper = new DbHelper(context);
    private static final String[] allColumns = new String[] { SyncedFileStateContract._ID,
            FILE_NAME,
            LOCAL_PATH,
            REMOTE_PATH,
            LAST_ETAG,
            LOCAL_LAST_MODIFIED,
            SYNCEDFOLDER_ID,
            IS_MEDIA_TYPE,
            SCANNABLE
    };


    /* package */ SyncedFileStateDAO(Context context){
        mHelper = new DbHelper(context);
        Timber.tag(SyncedFileStateDAO.class.getSimpleName());
    }

@@ -52,14 +63,14 @@ class SyncedFileStateDAO {
    * @param writeMod if true, open DB in write mod, else open it in read mod
    * @throws SQLException throw an SQL Exception
    */
    void open(Boolean writeMod ) throws SQLException {
        this.mDB = ( writeMod ) ? mHelper.getWritableDatabase(): mHelper.getReadableDatabase();
    /* package */ void open(Boolean writeMod ) throws SQLException {
        mDB = ( writeMod ) ? mHelper.getWritableDatabase(): mHelper.getReadableDatabase();
    }

    /**
     * Close DB connexion.
     */
    void close() {
    /* package */ void close() {
        mHelper.close();
    }

@@ -86,7 +97,7 @@ class SyncedFileStateDAO {
    * @param syncedFileState element to register
    * @return id of the last added element
    */
    long insert(SyncedFileState syncedFileState){
    /* package */ long insert(SyncedFileState syncedFileState){
        return mDB.insertWithOnConflict(SyncedFileStateContract.TABLE_NAME,
                null,
                toContentValues(syncedFileState),
@@ -97,7 +108,7 @@ class SyncedFileStateDAO {
     * Delete SyncedFileState
     * @param id syncedFile's id in DB
     */
    public int delete(long id) {
    /* package */ int delete(long id) {
        return mDB.delete(SyncedFileStateContract.TABLE_NAME, SyncedFileStateContract._ID
                + " = " + id, null);
    }
@@ -106,7 +117,7 @@ class SyncedFileStateDAO {
     * Remove SyncedFileState for hidden files (starting with '.')
     * @return number of deleted input
     */
    public int deleteHiddenFileStates() {
    /* package */ int deleteHiddenFileStates() {
        return mDB.delete(TABLE_NAME, FILE_NAME + " LIKE ?", new String[]{".%"});
    }

@@ -116,7 +127,7 @@ class SyncedFileStateDAO {
     * to remove them
     * @return number of deleted input
     */
    public int updateUnscannableMediaFiles() {
    /* package */ int updateUnscannableMediaFiles() {
        final ContentValues value = new ContentValues();
        value.put(SCANNABLE, SyncedFileState.DEVICE_SCANNABLE);
        final String whereClause = IS_MEDIA_TYPE + " =1 AND " + SCANNABLE + " ="+SyncedFileState.NOT_SCANNABLE;
@@ -129,7 +140,7 @@ class SyncedFileStateDAO {
    * @param syncedFileState SyncedFileState to update
    * @return number of row affected
    */
    int update(SyncedFileState syncedFileState){
    /* package */ int update(SyncedFileState syncedFileState){
        return  mDB.update(TABLE_NAME,
                    toContentValues(syncedFileState),
                    SyncedFileStateContract._ID+" = "+syncedFileState.getId(),
@@ -144,11 +155,11 @@ class SyncedFileStateDAO {
     * @return number of file waiting to be uploaded
     * @throws SQLiteDoneException SQL return 0 rows
     */
    long countFileWaitingForUploadForSyncedFolder(long syncedFolderId) throws SQLiteDoneException {
    /* package */ long countFileWaitingForUploadForSyncedFolder(long syncedFolderId) throws SQLiteDoneException {
        final String query = "SELECT COUNT(*) FROM "
            + TABLE_NAME
            + " WHERE " + SCANNABLE + " >= 2"
            + " AND " + LAST_ETAG + " = \"\""
            + " AND " + LAST_ETAG + " = ''"
            + " AND " + SYNCEDFOLDER_ID + " = "+syncedFolderId;

        final SQLiteStatement statement = mDB.compileStatement(query);
@@ -162,7 +173,7 @@ class SyncedFileStateDAO {
     * @return number of file waiting to be downloaded
     * @throws SQLiteDoneException SQL return 0 rows
     */
    long countFileWaitingForDownloadForSyncedFolder(int syncedFolderId) throws SQLiteDoneException{
    /* package */ long countFileWaitingForDownloadForSyncedFolder(int syncedFolderId) throws SQLiteDoneException{
        final String query = "SELECT COUNT(*) FROM "
                + TABLE_NAME
                + " WHERE " + SCANNABLE + " IN (1,3)"
@@ -179,31 +190,15 @@ class SyncedFileStateDAO {
    * @param syncedFolderIds List<Long> of id of parent syncedFolder.
    * @return List<SyncedFileState> List of SyncedFileState filtered on syncedFolder ID.
    */
    List<SyncedFileState> getBySyncedFolderID(List<Long> syncedFolderIds) {
         String query = "Select "
            +SyncedFileStateContract._ID+", "
            +FILE_NAME+", "
            +LOCAL_PATH+", "
            +REMOTE_PATH+", "
            +LAST_ETAG+", "
            +LOCAL_LAST_MODIFIED+", "
            + SYNCEDFOLDER_ID+", "
            + IS_MEDIA_TYPE+", "
            + SCANNABLE
            +" FROM "
            +TABLE_NAME;
         if (syncedFolderIds.size() > 0) {
             query+=" WHERE ";
    /* package */ List<SyncedFileState> getBySyncedFolderID(List<Long> syncedFolderIds) {
        final String whereClause = SYNCEDFOLDER_ID + " IN (?)";
        final String[] whereValue = new String[] {
             syncedFolderIds.toString()
                .replace("[", "(")
                .replace("]", ")") };

        final Cursor cursor = mDB.query(TABLE_NAME, allColumns, whereClause, whereValue, null, null, null);

             for (int i = -1, idsSize = syncedFolderIds.size(); ++i < idsSize; ) {
                 query += SYNCEDFOLDER_ID + " = " + syncedFolderIds.get(i);
                 if (i < idsSize - 1) {
                     query += " OR ";
                 }
             }
         }
        Timber.v("getBySyncedFolderID's query: %s", query);
        final Cursor cursor = mDB.rawQuery(query, null);
        cursor.moveToFirst();
        final List<SyncedFileState> result = new ArrayList<>();
        while(!cursor.isAfterLast() ) {
@@ -219,27 +214,10 @@ class SyncedFileStateDAO {
    * @param path local path or remote path
    * @return SyncedFileState obtain by the query or null if none has been found
    */
    SyncedFileState getByPath(String path, boolean isLocalPath) {
        String query = "Select "
                +SyncedFileStateContract._ID+", "
                +FILE_NAME+", "
                +LOCAL_PATH+", "
                +REMOTE_PATH+", "
                +LAST_ETAG+", "
                +LOCAL_LAST_MODIFIED+", "
                + SYNCEDFOLDER_ID+", "
                + IS_MEDIA_TYPE+", "
                + SCANNABLE+
                " FROM "
                +TABLE_NAME+" WHERE ";
        if (isLocalPath)
            query+=LOCAL_PATH ;
        else
            query+=REMOTE_PATH ;

        query +=" like \""+path+"\"";

        final Cursor cursor = mDB.rawQuery(query, null);
    /* package */ SyncedFileState getByPath(String path, boolean isLocalPath) {
        final String whereClause = (isLocalPath ? LOCAL_PATH : REMOTE_PATH) + " LIKE ?";
        final String[] whereValue = new String[] {path};
        final Cursor cursor = mDB.query(TABLE_NAME, allColumns, whereClause, whereValue, null, null, null);
        cursor.moveToFirst();
        SyncedFileState syncedFileState = null;
        if (!cursor.isAfterLast()) {