https://github.com/robindumontchaponet/reflexivecore
[WIP] Lazy PDO connection and some enums…
https://github.com/robindumontchaponet/reflexivecore
database pdo php8 work-in-progress
Last synced: 2 months ago
JSON representation
[WIP] Lazy PDO connection and some enums…
- Host: GitHub
- URL: https://github.com/robindumontchaponet/reflexivecore
- Owner: RobinDumontChaponet
- License: mit
- Created: 2021-01-02T15:31:18.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-11T21:34:15.000Z (over 3 years ago)
- Last Synced: 2025-02-10T23:36:20.950Z (over 1 year ago)
- Topics: database, pdo, php8, work-in-progress
- Language: PHP
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ReflexiveCore
$$ {{∀ x ∈ X : x R x}} $$
Shared building blocks for the Reflexive PHP packages.
This package contains:
- `Reflexive\Core\Database`: a lazy `PDO` wrapper that opens the connection on first real use.
- `Reflexive\Core\Condition` and `Reflexive\Core\ConditionGroup`: abstract condition primitives used by higher-level packages.
- `Reflexive\Core\Comparator` and `Reflexive\Core\Operator`: enums for SQL comparisons and boolean chaining.
- `Reflexive\Core\Strings::quote()`: a small helper that backticks SQL identifiers.
## Requirements
- PHP `^8.4`
## Installation
```bash
composer require reflexive/core
```
## Lazy database connections
`Database` extends `PDO`, but it does not call the parent constructor until you actually use the connection.
That means you can pass a configured database object around without opening the socket immediately.
```php
use Reflexive\Core\Database;
$db = new Database(
'mysql:host=127.0.0.1;dbname=app;charset=utf8mb4',
'user',
'secret',
);
// No connection yet.
$stmt = $db->prepare('SELECT 1');
$stmt->execute();
```
The class also exposes `Database::once()` to keep one lazy instance per DSN:
```php
$db = Database::once('sqlite:/tmp/app.sqlite');
```
Default PDO options applied on connect:
- `PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ`
- `PDO::ATTR_EMULATE_PREPARES => false`
- `PDO::ATTR_PERSISTENT => false`
- `PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION`
## Conditions and operators
The abstract condition API is intentionally small and is meant to be subclassed by query layers.
```php
use Reflexive\Core\Comparator;
use Reflexive\Core\Operator;
```
Available comparators:
- `EQUAL`
- `NOTEQUAL`
- `GREATER`
- `LESS`
- `GREATEROREQUAL`
- `LESSOREQUAL`
- `IN`
- `BETWEEN`
- `LIKE`
- `NULL`
- `NOTNULL`
Available boolean operators:
- `AND`
- `OR`
`ConditionGroup` enforces explicit chaining: once a condition has been added, the next one must be preceded by `and()` or `or()`.
## Identifier quoting
`Strings::quote()` wraps bare identifiers in backticks while leaving SQL functions and already-quoted names alone.
```php
use Reflexive\Core\Strings;
Strings::quote('users.email'); // `users`.`email`
Strings::quote('COUNT(*)'); // COUNT(*)
```
## Scope
This package is mostly infrastructure. On its own it does not build SQL or hydrate models; those features live in:
- `reflexive/query`
- `reflexive/model`