From ada1d3a70c496617037ed7232ba0c2e4192aa6a8 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 12 Oct 2022 11:04:22 +0200 Subject: [PATCH 1/2] Improve SyncedFileStateDAO Rewrite getBySyncedFolderID(List) Rewrite getByPath(String, boolean) Remove useless 'this.' Remove useless access modified (public|private) Rewrite countFileWaitingForUploadForSyncedFolder() to replace double quote by simple quote --- .../e/drive/database/SyncedFileStateDAO.java | 98 +++++++------------ 1 file changed, 38 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java b/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java index c25ff2eb..ee507044 100644 --- a/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java +++ b/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java @@ -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 of id of parent syncedFolder. * @return List List of SyncedFileState filtered on syncedFolder ID. */ - List getBySyncedFolderID(List 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 getBySyncedFolderID(List 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 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()) { -- GitLab From 7aca44950666b0a8ff13b29f52b46b537008a0de Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 12 Oct 2022 11:34:04 +0200 Subject: [PATCH 2/2] Improve SyncedFolderDAO Remove useless access modifier (public | private) Rewrite getSyncedFolderByLocalPath(String) to avoid usage of escaped double quotes --- .../e/drive/database/SyncedFolderDAO.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java b/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java index ec5c818d..c026ade5 100644 --- a/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java +++ b/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java @@ -35,13 +35,12 @@ import com.owncloud.android.lib.resources.files.FileUtils; /** * @author Vincent Bourgmayer - * //todo put consistency in method's visibility * Source: https://vogella.developpez.com/tutoriels/android/utilisation-base-donnees-sqlite/ */ -class SyncedFolderDAO { +/* package */ class SyncedFolderDAO { private SQLiteDatabase mDB; private final DbHelper mHelper; - private final String[] allColumns = { SyncedFolderContract._ID, + private static final String[] allColumns = { SyncedFolderContract._ID, CATEGORIE_LABEL, LOCAL_PATH, REMOTE_PATH, @@ -53,7 +52,7 @@ class SyncedFolderDAO { IS_MEDIA_TYPE }; - SyncedFolderDAO(Context context){ + /* package */ SyncedFolderDAO(Context context){ Timber.tag(SyncedFolderDAO.class.getSimpleName()); this.mHelper = new DbHelper(context); } @@ -63,14 +62,14 @@ class SyncedFolderDAO { * @param writeMod Connexion mod. If set to true, database is open in writable mod, else it is opened in read mod. * @throws SQLException SqlException */ - void open(Boolean writeMod) throws SQLException { + /* package */ void open(Boolean writeMod) throws SQLException { mDB = (writeMod)? mHelper.getWritableDatabase() : mHelper.getReadableDatabase(); } /** * Close Database connexion */ - void close() { + /* package */ void close() { mHelper.close(); } @@ -98,7 +97,7 @@ class SyncedFolderDAO { * @param syncedFolder element to register * @return Id of the last inserted value */ - long insert(SyncedFolder syncedFolder){ + /* package */ long insert(SyncedFolder syncedFolder){ return mDB.insertWithOnConflict(TABLE_NAME, null, toContentValues(syncedFolder), SQLiteDatabase.CONFLICT_IGNORE); @@ -108,7 +107,7 @@ class SyncedFolderDAO { * Delete all syncedFolder with the same categorie's label * @param id syncedFolder's id */ - public int delete(long id) { + /* package */ int delete(long id) { return mDB.delete(TABLE_NAME, SyncedFolderContract._ID + " = " + id, null); } @@ -118,7 +117,7 @@ class SyncedFolderDAO { * @param syncedFolder syncedFolder to update * @return Number of row affected */ - public int update(SyncedFolder syncedFolder){ + /* package */ int update(SyncedFolder syncedFolder){ return mDB.update(TABLE_NAME, toContentValues(syncedFolder), SyncedFolderContract._ID+" = "+ syncedFolder.getId(), @@ -131,8 +130,8 @@ class SyncedFolderDAO { * todo rewrite this method and at least its description because it's not clear... * @return List of all syncedFolder */ - List getSyncedFolderList(String selection, String[] args) { - final List syncedFolders = new ArrayList(); + /* package */ List getSyncedFolderList(String selection, String[] args) { + final List syncedFolders = new ArrayList<>(); final Cursor cursor = mDB.query(TABLE_NAME, allColumns, selection,args, null, null, null); @@ -157,7 +156,7 @@ class SyncedFolderDAO { * @param syncFolderID id of the synced folder to update * @return number of row affected */ - int reduceLastModifiedValue(int syncFolderID) { + /* package */ int reduceLastModifiedValue(int syncFolderID) { final ContentValues values = new ContentValues(); values.put( LOCAL_LAST_MODIFIED, 1 ); @@ -166,8 +165,10 @@ class SyncedFolderDAO { null); } - SyncedFolder getSyncedFolderByLocalPath(String localPath){ - final Cursor cursor = mDB.query(TABLE_NAME, allColumns, LOCAL_PATH + " like \"" + localPath + FileUtils.PATH_SEPARATOR + "\"", new String[0], null, null, null); + /* package */ SyncedFolder getSyncedFolderByLocalPath(String localPath){ + final String whereClause = LOCAL_PATH + " LIKE ?"; + final String[] whereValue = new String[] { localPath + FileUtils.PATH_SEPARATOR }; + final Cursor cursor = mDB.query(TABLE_NAME, allColumns, whereClause, whereValue, null, null, null); cursor.moveToFirst(); SyncedFolder result = null; if (!cursor.isAfterLast()) { -- GitLab