This repository contains selected source code files from the Database module of the greenDAO open-source project. These files were selected for software architecture analysis to evaluate code structure, understand component responsibilities, and identify opportunities for applying SOLID principles.
- Project Name: greenDAO
- Original GitHub Link: https://github.com/greenrobot/greenDAO
- Selected Module Path:
DaoCore/src/main/java/org/greenrobot/greendao/database
greenDAO/
└── database/
├── DatabaseStatement.java
├── EncryptedDatabaseStatement.java
├── Database.java
├── StandardDatabaseStatement.java
├── EncryptedDatabase.java
├── StandardDatabase.java
└── DatabaseOpenHelper.java
| # | File Name | Code Size | Short Role | Primary SOLID Focus |
|---|---|---|---|---|
| 1 | DatabaseStatement.java |
Small | SQL Statement Abstraction | Interface Segregation (ISP) |
| 2 | EncryptedDatabaseStatement.java |
Small | SQLCipher Statement Adapter | Dependency Inversion (DIP) |
| 3 | Database.java |
Medium | Core Database Interface | Single Responsibility (SRP) / ISP |
| 4 | StandardDatabaseStatement.java |
Medium | Native Android SQLite Wrapper | Liskov Substitution (LSP) |
| 5 | EncryptedDatabase.java |
Medium | Encrypted SQLCipher DB Wrapper | Single Responsibility (SRP) |
| 6 | StandardDatabase.java |
Large | Standard SQLite Execution Engine | Open/Closed Principle (OCP) |
| 7 | DatabaseOpenHelper.java |
Large | DB Lifecycle & Migration Manager | Single Responsibility (SRP) / OCP |
- Description: A core interface that represents a pre-compiled SQL statement abstraction in greenDAO. It abstracts execution logic away from specific database drivers (like standard Android SQLite or encrypted SQLCipher), allowing safe and flexible query execution with parameter binding.
- Main Methods:
execute(),simpleQueryForLong(),bindString(),bindLong(),close() - SOLID Principles to Apply:
- Single Responsibility Principle (SRP): The interface handles both statement execution and raw parameter binding. Parameter binding could be extracted into a dedicated
StatementBinderinterface. - Interface Segregation Principle (ISP): Clients that only need basic execution are forced to depend on binding methods. Splitting this into
ExecutableStatementandBindableStatementwould follow ISP better.
- Single Responsibility Principle (SRP): The interface handles both statement execution and raw parameter binding. Parameter binding could be extracted into a dedicated
- Description: An adapter class that delegates statement operations to SQLCipher's native
SQLiteStatement. It enables greenDAO to run encrypted database statements transparently using the same interface as unencrypted statements. - Main Methods:
execute(),simpleQueryForLong(),bindString(),getRawStatement() - SOLID Principles to Apply:
- Single Responsibility Principle (SRP): Manages both method delegation and native SQLCipher parameter mapping. Mapping logic can be isolated into a separate wrapper adapter.
- Dependency Inversion Principle (DIP): Depends directly on concrete SQLCipher classes (
net.sqlcipher.database.SQLiteStatement). Depending on a broader encrypted statement interface would reduce tight coupling.
- Description: The primary interface abstracting low-level database connection operations for greenDAO. It provides a unified API for managing transactions, compiling raw SQL statements, executing direct queries, and handling underlying database connections regardless of encryption.
- Main Methods:
beginTransaction(),endTransaction(),compileStatement(),rawQuery() - SOLID Principles to Apply:
- Interface Segregation Principle (ISP): Contains methods for query execution, transaction control, and raw SQL compilation. Splitting this into
TransactionManager,QueryExecutor, andStatementCompilerwould allow clients to depend only on what they use. - Dependency Inversion Principle (DIP): Provides a common abstraction over Android SQLite and SQLCipher, decoupling high-level DAOs from low-level database drivers.
- Interface Segregation Principle (ISP): Contains methods for query execution, transaction control, and raw SQL compilation. Splitting this into
- Description: The standard, unencrypted implementation of the
DatabaseStatementinterface. It wraps Android’s nativeandroid.database.sqlite.SQLiteStatementto bridge native Android SQLite operations with greenDAO’s framework. - Main Methods:
execute(),simpleQueryForLong(),bindLong(),getRawStatement() - SOLID Principles to Apply:
- Single Responsibility Principle (SRP): Handles both delegation to native SQLite and native object extraction. Resource lifetime management could be moved to a helper wrapper class.
- Liskov Substitution Principle (LSP): Honors LSP by providing a full, predictable implementation of
DatabaseStatementwithout modifying expected interface behavior when substituted for native database contexts.
- Description: Implements the
Databaseinterface using SQLCipher’sSQLiteDatabase. It allows greenDAO to perform transparent end-to-end encryption by wrapping SQLCipher calls while maintaining the same public interface as standard database drivers. - Main Methods:
beginTransaction(),compileStatement(),rawQuery(),getRawDatabase() - SOLID Principles to Apply:
- Single Responsibility Principle (SRP): Manages transaction controls, statement compilation, and SQLCipher database instance delegation. Extracting transaction management into a dedicated
EncryptedTransactionHandlerwould adhere strictly to SRP. - Dependency Inversion Principle (DIP): Directly references concrete SQLCipher imports. Wrapping SQLCipher initialization behind a factory or provider abstraction would reduce direct library dependency.
- Single Responsibility Principle (SRP): Manages transaction controls, statement compilation, and SQLCipher database instance delegation. Extracting transaction management into a dedicated
- Description: The main unencrypted implementation of the
Databaseinterface. It wraps Android's nativeandroid.database.sqlite.SQLiteDatabaseto manage query compilation, execution, and transaction states for standard Android apps. - Main Methods:
beginTransaction(),compileStatement(),rawQuery(),getRawDatabase() - SOLID Principles to Apply:
- Single Responsibility Principle (SRP): Mixes transaction lifecycle management with query execution and statement compilation. Separating transaction handling into a
NativeTransactionManagerwould improve maintainability. - Open/Closed Principle (OCP): Modifying how statements are created requires editing this class directly. Implementing a
StatementFactoryinterface would allow new statement compilation behaviors without changing existing code.
- Single Responsibility Principle (SRP): Mixes transaction lifecycle management with query execution and statement compilation. Separating transaction handling into a
- Description: An abstract helper class extending Android’s
SQLiteOpenHelper. It manages database creation, schema upgrades, and context-aware connections, supporting both standard unencrypted and SQLCipher-encrypted database instances. - Main Methods:
getWritableDb(),getEncryptedWritableDb(),onCreate(),onUpgrade() - SOLID Principles to Apply:
- Single Responsibility Principle (SRP): Handles database lifecycle callbacks (
onCreate/onUpgrade), connection creation, and encryption configuration. Migration logic should be delegated to a dedicatedSchemaMigratorclass. - Open/Closed Principle (OCP): When adding new migration strategies,
onUpgrademust be modified directly. Using a Strategy Pattern (MigrationStrategy) would allow adding migration rules without altering the helper class. - Dependency Inversion Principle (DIP): Instead of directly instantiating encrypted or standard database wrappers, an abstract
DatabaseFactoryshould be injected to handle connection instantiation.
- Single Responsibility Principle (SRP): Handles database lifecycle callbacks (