|
| 1 | +# StellarWP DB - Claude Instructions |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +StellarWP DB is a WordPress database wrapper and query builder library that provides a fluent interface for constructing and executing database queries. It's built on top of WordPress's `$wpdb` global and adds a modern, chainable API with proper error handling. |
| 6 | + |
| 7 | +**Key Features:** |
| 8 | +- Fluent query builder interface |
| 9 | +- SQL injection protection via WordPress's prepared statements |
| 10 | +- Support for complex queries (joins, unions, subqueries, etc.) |
| 11 | +- Meta table abstractions for WordPress-style data |
| 12 | +- Error handling with exceptions |
| 13 | +- Full compatibility with `$wpdb` methods |
| 14 | + |
| 15 | +## Architecture |
| 16 | + |
| 17 | +### Core Classes |
| 18 | + |
| 19 | +1. **DB** (`src/DB/DB.php`) |
| 20 | + - Static facade/decorator for `$wpdb` |
| 21 | + - Entry point for creating query builders via `DB::table()` |
| 22 | + - Provides static methods that wrap `$wpdb` methods with error checking |
| 23 | + - Throws `DatabaseQueryException` on SQL errors |
| 24 | + |
| 25 | +2. **QueryBuilder** (`src/DB/QueryBuilder/QueryBuilder.php`) |
| 26 | + - Main query building class |
| 27 | + - Uses traits to organize functionality: |
| 28 | + - `Aggregate` - COUNT, SUM, AVG, MIN, MAX operations |
| 29 | + - `CRUD` - INSERT, UPDATE, DELETE, UPSERT operations |
| 30 | + - `FromClause` - FROM table handling |
| 31 | + - `WhereClause` - WHERE conditions |
| 32 | + - `JoinClause` - JOIN operations |
| 33 | + - `SelectStatement` - SELECT column handling |
| 34 | + - `MetaQuery` - WordPress meta table helpers |
| 35 | + - And more... |
| 36 | + |
| 37 | +3. **Config** (`src/DB/Config.php`) |
| 38 | + - Configuration management |
| 39 | + - Allows setting custom exception classes |
| 40 | + - Hook prefix configuration for WordPress integration |
| 41 | + |
| 42 | +### Directory Structure |
| 43 | + |
| 44 | +``` |
| 45 | +/home/matt/git/db/ |
| 46 | +├── src/DB/ # Main source code |
| 47 | +│ ├── Config.php # Configuration class |
| 48 | +│ ├── DB.php # Main DB facade |
| 49 | +│ ├── Database/ # Database-specific classes |
| 50 | +│ │ ├── Actions/ # Database actions (e.g., EnableBigSqlSelects) |
| 51 | +│ │ ├── Exceptions/ # Custom exceptions |
| 52 | +│ │ └── Provider.php # Service provider |
| 53 | +│ └── QueryBuilder/ # Query builder components |
| 54 | +│ ├── Clauses/ # SQL clause representations |
| 55 | +│ ├── Concerns/ # Traits for QueryBuilder |
| 56 | +│ ├── Types/ # Type definitions (JoinType, Operator, etc.) |
| 57 | +│ ├── QueryBuilder.php # Main query builder |
| 58 | +│ ├── JoinQueryBuilder.php |
| 59 | +│ └── WhereQueryBuilder.php |
| 60 | +├── tests/ # Test suite |
| 61 | +│ ├── wpunit/ # WordPress unit tests |
| 62 | +│ └── _support/ # Test support classes |
| 63 | +├── .github/workflows/ # GitHub Actions CI/CD |
| 64 | +├── composer.json # PHP dependencies |
| 65 | +├── phpstan.neon.dist # PHPStan configuration |
| 66 | +└── README.md # Documentation |
| 67 | +``` |
| 68 | + |
| 69 | +## Development Commands |
| 70 | + |
| 71 | +### Composer Scripts |
| 72 | + |
| 73 | +```bash |
| 74 | +# Run static analysis with PHPStan |
| 75 | +composer test:analysis |
| 76 | +``` |
| 77 | + |
| 78 | +### Testing |
| 79 | + |
| 80 | +The project uses Codeception with WordPress testing framework (wp-browser) and a tool called "slic" for managing the test environment: |
| 81 | + |
| 82 | +```bash |
| 83 | +# Run tests using slic (see .github/workflows/tests-php.yml) |
| 84 | +${SLIC_BIN} run wpunit --ext DotReporter |
| 85 | +``` |
| 86 | + |
| 87 | +### Static Analysis |
| 88 | + |
| 89 | +PHPStan is configured at level 5 with WordPress-specific rules: |
| 90 | +- Configuration: `phpstan.neon.dist` |
| 91 | +- Includes WordPress stubs via `szepeviktor/phpstan-wordpress` |
| 92 | +- Analyzes only the `src/` directory |
| 93 | + |
| 94 | +## Code Conventions |
| 95 | + |
| 96 | +### Namespacing |
| 97 | +- Root namespace: `StellarWP\DB` |
| 98 | +- Follow PSR-4 autoloading standard |
| 99 | +- Tests use `StellarWP\DB\Tests` namespace |
| 100 | + |
| 101 | +### WordPress Integration |
| 102 | +- All table names are automatically prefixed with `$wpdb->prefix` |
| 103 | +- Use `DB::raw()` to bypass automatic prefixing |
| 104 | +- Methods match `$wpdb` signatures where applicable |
| 105 | + |
| 106 | +### Error Handling |
| 107 | +- SQL errors throw `DatabaseQueryException` by default |
| 108 | +- Custom exception classes can be configured via `Config::setDatabaseQueryException()` |
| 109 | +- All database operations should be wrapped in try-catch blocks in production code |
| 110 | + |
| 111 | +### Query Building Patterns |
| 112 | + |
| 113 | +```php |
| 114 | +// Basic query |
| 115 | +DB::table('posts') |
| 116 | + ->select('ID', 'post_title') |
| 117 | + ->where('post_status', 'publish') |
| 118 | + ->orderBy('post_date', 'DESC') |
| 119 | + ->limit(10) |
| 120 | + ->getAll(); |
| 121 | + |
| 122 | +// Complex query with joins and meta |
| 123 | +DB::table('posts', 'p') |
| 124 | + ->select(['p.ID', 'id'], ['p.post_title', 'title']) |
| 125 | + ->attachMeta('postmeta', 'p.ID', 'post_id', |
| 126 | + ['_thumbnail_id', 'thumbnailId'], |
| 127 | + ['_custom_field', 'customValue'] |
| 128 | + ) |
| 129 | + ->leftJoin('term_relationships', 'p.ID', 'tr.object_id', 'tr') |
| 130 | + ->where('p.post_type', 'post') |
| 131 | + ->where('p.post_status', 'publish') |
| 132 | + ->getAll(); |
| 133 | +``` |
| 134 | + |
| 135 | +## Important Notes |
| 136 | + |
| 137 | +### For Strauss Integration |
| 138 | +The library is designed to be included via Strauss for namespace isolation in WordPress plugins. The README examples assume a namespace prefix like `Boom\Shakalaka\`. |
| 139 | + |
| 140 | +### WordPress Compatibility |
| 141 | +- Requires WordPress with `$wpdb` global |
| 142 | +- Compatible with WordPress coding standards |
| 143 | +- Follows WordPress database structure conventions (posts, postmeta, etc.) |
| 144 | + |
| 145 | +### Performance Considerations |
| 146 | +- Queries are not executed until terminal methods are called (`get()`, `getAll()`, `count()`, etc.) |
| 147 | +- Use `attachMeta()` for efficient meta queries instead of multiple joins |
| 148 | +- The library generates SQL using WordPress's `prepare()` method for security |
| 149 | + |
| 150 | +### Recent Updates (v1.0.8) |
| 151 | +- Added `DB::generate_results()` and `DB::generate_col()` for handling large result sets with bounded queries |
| 152 | +- PHP 8.1 compatibility improvements |
| 153 | +- Better type safety in docblocks |
| 154 | + |
| 155 | +## CI/CD |
| 156 | + |
| 157 | +### GitHub Actions Workflows |
| 158 | + |
| 159 | +1. **Tests** (`.github/workflows/tests-php.yml`) |
| 160 | + - Runs on every push |
| 161 | + - Uses `stellarwp/slic` for test environment setup |
| 162 | + - Executes wpunit test suite |
| 163 | + |
| 164 | +2. **Static Analysis** (`.github/workflows/static-analysis.yml`) |
| 165 | + - Runs on every push |
| 166 | + - PHP 8.0 environment |
| 167 | + - Executes PHPStan via `composer test:analysis` |
| 168 | + |
| 169 | +## Common Tasks |
| 170 | + |
| 171 | +### Adding New Query Features |
| 172 | +1. Create a new trait in `src/DB/QueryBuilder/Concerns/` |
| 173 | +2. Add the trait to the `QueryBuilder` class |
| 174 | +3. Implement the SQL generation method (e.g., `getYourFeatureSQL()`) |
| 175 | +4. Add it to the `getSQL()` method if needed |
| 176 | +5. Write tests in `tests/wpunit/QueryBuilder/` |
| 177 | + |
| 178 | +### Debugging Queries |
| 179 | +```php |
| 180 | +// Get the generated SQL without executing |
| 181 | +$sql = DB::table('posts') |
| 182 | + ->where('post_status', 'publish') |
| 183 | + ->getSQL(); |
| 184 | + |
| 185 | +// Use wpdb's last_query after execution |
| 186 | +$results = DB::table('posts')->getAll(); |
| 187 | +$lastQuery = $wpdb->last_query; |
| 188 | +``` |
| 189 | + |
| 190 | +### Meta Table Patterns |
| 191 | +The library provides special handling for WordPress meta tables: |
| 192 | +- `attachMeta()` - Efficiently join and select meta values |
| 193 | +- `configureMetaTable()` - Customize meta table column names |
| 194 | +- Automatic LEFT JOIN generation for each meta key |
| 195 | +- Support for multiple meta values with the same key |
| 196 | + |
| 197 | +## Resources |
| 198 | + |
| 199 | +- [WordPress $wpdb Documentation](https://developer.wordpress.org/reference/classes/wpdb/) |
| 200 | +- [Codeception Documentation](https://codeception.com/) |
| 201 | +- [PHPStan Documentation](https://phpstan.org/) |
| 202 | +- [WordPress Coding Standards](https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/) |
0 commit comments