Skip to content

Conversation

@codeOwlAI
Copy link
Owner

@codeOwlAI codeOwlAI commented Jun 29, 2025

PR Summary

Implement Factory Pattern for SQLite Database Access with Encryption Support

Overview

This PR refactors the database access layer to use a factory pattern, enabling support for both encrypted and unencrypted SQLite databases. The implementation adds new factory classes and modifies existing components to work with this abstraction.

Change Types

Type Description
Feature Added support for encrypted SQLite databases
Refactor Implemented factory pattern for database access
Enhancement Modified existing components to work with the new abstraction

Affected Modules

Module / File Change Description
src/main/java/com/amitshekhar/DebugDB.java Added import for DBFactory
src/main/java/com/amitshekhar/server/ClientServer.java Modified constructor to accept DBFactory parameter
src/main/java/com/amitshekhar/server/RequestHandler.java Refactored to use DBFactory instead of direct DebugSQLiteDB
src/main/java/com/amitshekhar/debug/encrypt/sqlite/DebugDBEncryptFactory.java Added new factory for encrypted SQLite databases
src/main/java/com/amitshekhar/debug/sqlite/DebugDBFactory.java Added new factory for standard SQLite databases
src/main/java/com/amitshekhar/debug/sqlite/DebugSQLiteDB.java Switched from SQLCipher to standard SQLite
src/main/java/com/sample/encrypt/database/PersonDBHelper.java Moved to encryption-specific package
src/test/java/com/sample/encrypt/ExampleUnitTest.java Added basic unit test for encryption package
src/main/java/com/sample/MainActivity.java Added sample implementation with SharedPreferences and test data

Notes for Reviewers

  • The factory pattern implementation allows for runtime selection between encrypted and unencrypted database access
  • Verify that both encrypted and unencrypted database operations work correctly
  • Check that the RequestHandler properly uses the injected factory

private HashMap<String, SupportSQLiteDatabase> mRoomInMemoryDatabases = new HashMap<>();

public RequestHandler(Context context) {
public RequestHandler(Context context, DBFactory dbFactory) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Breaking API Change: Constructor Parameter Added.

The RequestHandler constructor now requires a DBFactory parameter, which will break all existing code that instantiates this class.

Current Code (Diff):

- public RequestHandler(Context context) {
+ public RequestHandler(Context context, DBFactory dbFactory) {


@Override
public SQLiteDB create(Context context, String path, String password) {
return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, null));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Security Issue

Security: Ignored Password Parameter.

The password parameter is accepted but ignored when opening the database, which could lead to unencrypted database access in production if encryption is expected.

Current Code (Diff):

-         return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, null));
+         return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, password, null));
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, null));
return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, password, null));

Comment on lines +12 to +14
public SQLiteDB create(Context context, String path, String password) {
return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, null));
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Correctness Issue

Correctness: No Error Handling.

SQLiteDatabase.openOrCreateDatabase can throw exceptions for invalid paths or permission issues with no error handling, causing application crashes.

Current Code (Diff):

-     public SQLiteDB create(Context context, String path, String password) {
-         return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, null));
-     }
+     public SQLiteDB create(Context context, String path, String password) {
+         try {
+             return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, password, null));
+         } catch (Exception e) {
+             throw new RuntimeException("Failed to open database at " + path, e);
+         }
+     }
📝 Committable suggestion

‼️ IMPORTANT
Trust, but verify! 🕵️ Please review this suggestion with the care of a code archaeologist - check that it perfectly replaces the highlighted code, preserves all lines, maintains proper indentation, and won't break anything in production. Your future self will thank you! 🚀

Suggested change
public SQLiteDB create(Context context, String path, String password) {
return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, null));
}
public SQLiteDB create(Context context, String path, String password) {
try {
return new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, password, null));
} catch (Exception e) {
throw new RuntimeException("Failed to open database at " + path, e);
}
}

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Security Issue

Security: Database Encryption Removed.

Switching from SQLCipher to standard SQLite removes encryption, potentially exposing sensitive data if this debug implementation is used with real user data.

Current Code (Diff):

- import android.database.sqlite.SQLiteDatabase;

+ import net.sqlcipher.database.SQLiteDatabase;
+ 
+ /**
+  * Created by anandgaurav on 12/02/18.
+  */

🔄 Dependencies Affected

debug-db/src/main/java/com/amitshekhar/debug/sqlite/DebugDBFactory.java

Function: DebugDBFactory.openOrCreateDatabase

Issue: Database factory creates unencrypted database instances

Suggestion: Either ensure this implementation is only used for debugging or restore encryption capability


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants