Overview
TheDatabase class provides a singleton-based PDO wrapper for MySQL database operations. It offers secure query execution with prepared statements, CRUD operations, and full transaction support.
The Database class uses the Singleton pattern to ensure a single connection instance throughout the application lifecycle.
Connection Management
Initialization
The database connection is established on first access and reused for all subsequent operations.Connection Features
The database connection is configured with:- Error Mode: Exception-based error handling (
PDO::ERRMODE_EXCEPTION) - Fetch Mode: Associative arrays by default (
PDO::FETCH_ASSOC) - Prepared Statements: Native prepared statements (
PDO::ATTR_EMULATE_PREPARES => false)
Query Methods
Raw Queries
Execute custom SQL queries with parameter binding:CRUD Operations
Insert Records
Update Records
Delete Records
Transaction Support
The Database class provides full transaction control for atomic operations:Database Schema
The application uses the following core tables:Main Tables
| Table | Description |
|---|---|
documents | Stores uploaded documents with metadata and extracted text |
vectors | Contains embeddings and chunks for RAG retrieval |
conversations | Tracks WhatsApp conversations and their status |
messages | Stores all messages with context and confidence scores |
settings | Application-wide configuration key-value pairs |
bot_credentials | Encrypted API credentials for WhatsApp and OpenAI |
Documents Table
Conversations Table
The full database schema is available in
database/schema.sql with all indexes and foreign key constraints.API Reference
getInstance(array $config = null): Database
Returns the singleton Database instance. First call requires configuration.
Location: src/Core/Database.php:20
Parameters:
$config(array, optional): Database configuration array with keys:host,port,name,charset,user,password
Database instance
Throws: RuntimeException if configuration is missing on first call
query(string $sql, array $params = []): PDOStatement
Executes a prepared statement with parameter binding.
Location: src/Core/Database.php:62
Parameters:
$sql(string): SQL query with placeholders$params(array): Parameter bindings
PDOStatement object
Throws: RuntimeException on query failure
insert(string $table, array $data): string
Inserts a new record and returns the auto-increment ID.
Location: src/Core/Database.php:85
Parameters:
$table(string): Table name$data(array): Associative array of column => value pairs
update(string $table, array $data, string $where, array $whereParams = []): int
Updates records matching the WHERE clause.
Location: src/Core/Database.php:108
Parameters:
$table(string): Table name$data(array): Columns to update$where(string): WHERE clause condition$whereParams(array): Parameters for WHERE clause
delete(string $table, string $where, array $params = []): int
Deletes records matching the WHERE clause.
Location: src/Core/Database.php:130
Parameters:
$table(string): Table name$where(string): WHERE clause condition$params(array): Parameters for WHERE clause
Transaction Methods
beginTransaction(): bool- Starts a new transaction (src/Core/Database.php:137)commit(): bool- Commits the active transaction (src/Core/Database.php:142)rollback(): bool- Rolls back the active transaction (src/Core/Database.php:147)
Best Practices
Use Prepared Statements
Always use parameter binding to prevent SQL injection attacks. Never concatenate user input into SQL queries.
Handle Exceptions
Wrap database operations in try-catch blocks to handle PDOExceptions gracefully.
Use Transactions
For operations that span multiple tables, use transactions to ensure data consistency.
Close Cursors
For large result sets, close PDOStatement cursors when done to free memory.
Related Services
Logger
Log database operations and errors
Encryption
Encrypt sensitive database credentials