ADR-002 - Database Abstraction
ADR-002: Database Abstraction
Section titled “ADR-002: Database Abstraction”Architecture Decision Record for XOOPS’s object-oriented database access pattern.
Status
Section titled “Status”Accepted - Core pattern since XOOPS 2.0
Context
Section titled “Context”XOOPS needed a database interaction strategy that would:
- Abstract away database-specific SQL syntax
- Provide consistent CRUD operations across all modules
- Enable automatic data sanitization and escaping
- Support future database engine changes
- Simplify common operations for developers
The alternatives were:
- Raw SQL throughout the codebase
- Full ORM (Doctrine, Eloquent)
- Custom lightweight abstraction
Decision Diagram
Section titled “Decision Diagram”graph TB subgraph "Application Layer" A[Module Code] B[Handler Methods] end
subgraph "Abstraction Layer" C[XoopsObjectHandler] D[XoopsPersistableObjectHandler] E[Criteria System] end
subgraph "Database Layer" F[XoopsDatabase] G[MySQLDatabase] H[PDO Wrapper] end
subgraph "Storage" I[(MySQL/MariaDB)] end
A --> B B --> C B --> D C --> E D --> E E --> F F --> G F --> H G --> I H --> IDecision
Section titled “Decision”We will implement a Handler Pattern with:
1. XoopsObject - Data Container
Section titled “1. XoopsObject - Data Container”Each data entity extends XoopsObject:
class Item extends XoopsObject{ public function __construct() { $this->initVar('id', XOBJ_DTYPE_INT, null, false); $this->initVar('title', XOBJ_DTYPE_TXTBOX, '', true, 255); $this->initVar('content', XOBJ_DTYPE_TXTAREA, '', false); $this->initVar('status', XOBJ_DTYPE_INT, 0, false); }}2. Handler - Operations Manager
Section titled “2. Handler - Operations Manager”Each object has a corresponding handler:
class ItemHandler extends XoopsPersistableObjectHandler{ public function __construct($db) { parent::__construct($db, 'mymodule_items', Item::class, 'id', 'title'); }
// CRUD methods inherited: // - create(), get(), insert(), delete() // - getObjects(), getCount(), getAll()}3. Criteria - Query Builder
Section titled “3. Criteria - Query Builder”Object-oriented query conditions:
$criteria = new CriteriaCompo();$criteria->add(new Criteria('status', 1));$criteria->add(new Criteria('created', time() - 86400, '>='));$criteria->setSort('created');$criteria->setOrder('DESC');$criteria->setLimit(10);
$items = $handler->getObjects($criteria);Data Type Constants
Section titled “Data Type Constants”// Variable types with automatic sanitizationXOBJ_DTYPE_INT // IntegerXOBJ_DTYPE_TXTBOX // Single-line text (escaped)XOBJ_DTYPE_TXTAREA // Multi-line text (escaped)XOBJ_DTYPE_EMAIL // Email validationXOBJ_DTYPE_URL // URL validationXOBJ_DTYPE_ARRAY // Serialized arrayXOBJ_DTYPE_OTHER // No processingXOBJ_DTYPE_FLOAT // Floating pointHandler Inheritance
Section titled “Handler Inheritance”classDiagram class XoopsObjectHandler { <<abstract>> #db: XoopsDatabase +create(): XoopsObject +get(id): XoopsObject +insert(object): bool +delete(object): bool }
class XoopsPersistableObjectHandler { +getObjects(criteria): array +getCount(criteria): int +getAll(criteria): array +deleteAll(criteria): bool +updateAll(field, value, criteria): bool }
class ItemHandler { +getPublishedItems(): array +getByCategory(catId): array }
XoopsObjectHandler <|-- XoopsPersistableObjectHandler XoopsPersistableObjectHandler <|-- ItemHandlerConsequences
Section titled “Consequences”Positive
Section titled “Positive”- Consistency: All modules use same patterns
- Security: Automatic escaping prevents SQL injection
- Simplicity: Common operations require minimal code
- Maintainability: Changes to database layer don’t affect modules
- Testability: Handlers can be mocked for testing
Negative
Section titled “Negative”- Performance: Extra abstraction overhead
- Complexity: Learning curve for new developers
- Limitations: Complex queries may need raw SQL
- N+1 Problem: No built-in eager loading
Mitigations
Section titled “Mitigations”- Performance: Cache frequently accessed objects
- Complex queries: Allow raw SQL when needed
- N+1: Use getAll() with proper criteria
Evolution to XOOPS 4.0
Section titled “Evolution to XOOPS 4.0”flowchart LR subgraph "Current (2.5.x)" A[XoopsDatabase] B[Handlers] C[Criteria] end
subgraph "Future (4.0.x)" D[PDO/DBAL] E[Repository Pattern] F[Query Builder] G[DI Container] end
A --> D B --> E C --> F D --> G E --> GXOOPS 4.0 plans:
- Doctrine DBAL for database abstraction
- Repository pattern replacing handlers
- Query builder for complex queries
- Full PSR-11 container integration
Code Examples
Section titled “Code Examples”Basic CRUD
Section titled “Basic CRUD”$helper = Helper::getInstance();$handler = $helper->getHandler('Item');
// Create$item = $handler->create();$item->setVar('title', 'New Item');$handler->insert($item);
// Read$item = $handler->get($id);$title = $item->getVar('title');
// Update$item->setVar('title', 'Updated Title');$handler->insert($item);
// Delete$handler->delete($item);Complex Query
Section titled “Complex Query”$criteria = new CriteriaCompo();$criteria->add(new Criteria('status', 'published'));$criteria->add(new Criteria('category_id', '(1,2,3)', 'IN'));$criteria->add(new Criteria('created', strtotime('-30 days'), '>='));$criteria->setSort('views');$criteria->setOrder('DESC');$criteria->setLimit(10);$criteria->setStart(0);
$items = $handler->getObjects($criteria);$total = $handler->getCount($criteria);Related Decisions
Section titled “Related Decisions”- ADR-001: Modular Architecture
- ADR-003: Smarty Template Engine
References
Section titled “References”- Martin Fowler - Patterns of Enterprise Application Architecture
- Domain-Driven Design concepts
- Active Record vs Data Mapper patterns
#xoops #architecture #adr #database #handler #design-decision