Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F24682539
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/README.md b/README.md
index bbead20..ed3e87d 100644
--- a/README.md
+++ b/README.md
@@ -1,115 +1,115 @@
# keruald/database
This library offers a simple layer of abstraction for database operations.
## Configuration
To get a database instance, you need to pass configuration as an array.
The properties and values depend on the engine you want to use.
### MySQLi
| Key | Value | |
|------------|--------------------------------------|:---------------|
| engine | MySQLiEngine class reference | |
| host | The MySQL hostname, e.g. "localhost" | |
| username | The MySQL user to use for connection | |
| password | The clear text password to use | |
| database | The default db to select for queries | (optional) |
| fetch_mode | The default mode to fetch rows | `MYSQLI_ASSOC` |
For example:
```php
[
'engine' => Keruald\Database\Engines\MySQLiEngine::class,
'host' => 'localhost',
'username' => 'app',
'password' => 'someSecret',
'database' => 'app', // optional
]
```
#### About fetch_mode parameter
The `fetch_mode` parameter is used to determine how to represent results:
* `MYSQLI_ASSOC` will use column names
* `MYSQLI_NUM` will use an enumerated array (0, 1, 2, …)
* `MYSQLI_BOTH` will use both of them
The code offers `MYSQLI_ASSOC` as default value to allow to directly represent
a row result as API output and encourage to take care of the column names for
better code maintenance. If you wish to switch to default MySQLi behavior,
use `MYSQLI_BOTH` instead.
Those constants are defined by the MySQLi extension.
## Legacy drivers
The mysql extension has been deprecated in PHP 5.7 and removed in PHP 7.
As such, this extension isn't supported anymore. You can use straightforwardly
replace 'MySQL' by 'MySQLi' as engine.
## Specialized drivers for tests
### Blackhole
-The blackhole engine does nothing and always returns `true` as query result.
+The black hole engine does nothing and always returns `true` as query result.
This engine can be used for mocks:
- directly, when database behavior does not matter
- to build a mock by overriding behavior of query() or any other method
It can also be used with the loader, without any engine-specific configuration:
```php
[
'engine' => Keruald\Database\Engines\BlackholeEngine::class,
]
```
### MockDatabaseEngine
-The mock database is a simple implementation of the blackhole engine as mocking
+The mock database is a simple implementation of the black hole engine as mocking
service to use when you want to return a deterministic response to known
queries.
A benefit is you don't need a running database server for your unit tests.
You can pass to the `withQueries` method an array with one item per query:
- key: the SQL query
- value: an array with all rows for that query
-
For example:
```php
public function testGetFruits () : void {
$queries = [
"SELECT name, color FROM fruits" => [
[ "name" => "strawberry", "color" => "red" ],
[ "name" => "blueberry", "color" => "violet" ],
],
];
$db = (new MockDatabaseEngine())
->withQueries($queries);
// Inject $db to a class and test it
}
```
To return only one row, you can use `[[…]]` to represent an array of one array:
```php
$queries = [
"SELECT 1+1" => [[ "1+1" => 2 ]],
];
```
The queries results are then wrapped in the MockDatabaseResult class.
When the query doesn't exist, an exception is thrown.
We recommend a mixed approach of the Blackhole engine when results don't matter,
and of this class when you need some control on it.
diff --git a/src/Engines/BlackholeEngine.php b/src/Engines/BlackholeEngine.php
index efe83d7..55670dc 100644
--- a/src/Engines/BlackholeEngine.php
+++ b/src/Engines/BlackholeEngine.php
@@ -1,62 +1,62 @@
<?php
namespace Keruald\Database\Engines;
use Keruald\Database\DatabaseEngine;
use Keruald\Database\Result\DatabaseResult;
class BlackholeEngine extends DatabaseEngine {
public function escape (string $expression) : string {
return $expression;
}
public function query (string $query) : bool|DatabaseResult {
return true;
}
public function nextId () : int|string {
return 0;
}
public function countAffectedRows () : int {
return 0;
}
protected function getExceptionContext () : array {
return [];
}
public static function load (array $config): DatabaseEngine {
return new self;
}
public function getUnderlyingDriver () : mixed {
return null;
}
public function isExistingTable (string $database, string $table) : bool {
- // Everything and nothing exists in a blackhole
+ // Everything and nothing exists in a black hole
return true;
}
///
/// Error handling
///
public function error () : array {
return [];
}
///
/// Events
///
protected function onCantConnectToHost () : void {
// We can always connect to a black hole engine.
}
protected function onQueryError (string $query) : void {
// Queries are always valid.
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Mar 7, 01:32 (6 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3500266
Default Alt Text
(5 KB)
Attached To
Mode
rKDB Keruald Database
Attached
Detach File
Event Timeline
Log In to Comment