Skip to content

Commit 3fd8220

Browse files
committed
Improve documentation slightly
1 parent 608f8e9 commit 3fd8220

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

README.md

+23-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Squirrel Queries
55

66
Provides a slimmed down concise interface for low level database queries and transactions (DBInterface) as well as a query builder to make it easier and more expressive to create queries (DBBuilderInterface). The interfaces are limited to avoid confusion/misuse and encourage fail-safe usage.
77

8-
Doctrine is used as the underlying connection (and abstraction), what we add are an insertOrUpdate functionality (known as UPSERT), structured queries which are easier to write and read (and for which the query builder can be used), and the possibility to layer database concerns (like actual implementation, connections retries, performance measurements, logging, etc.). This library also smoothes over some differences between MySQL, Postgres and SQLite.
8+
Doctrine DBAL is used for the underlying connection (and abstraction) handling, what we add are an insertOrUpdate functionality (known as UPSERT), structured queries which are easier to write and read (and for which the query builder can be used), and the possibility to layer database concerns (like actual implementation, connections retries, performance measurements, logging, etc.). This library also smoothes over some differences between MySQL, Postgres and SQLite. While DBAL is a dependency for now, when using this library you only need to configure/create the necessary DBAL connections in your code, no other parts of DBAL are relevant.
99

1010
By default this library provides two layers, one dealing with Doctrine DBAL (passing the queries, processing and returning the results) and one dealing with errors (DBErrorHandler). DBErrorHandler catches deadlocks and connection problems and tries to repeat the query or transaction, and it unifies the exceptions coming from DBAL so the originating call to DBInterface is provided and the error can easily be found.
1111

@@ -29,7 +29,7 @@ Setting up
2929

3030
Use Squirrel\Queries\DBInterface as a type hint in your services for the low-level interface, and/or Squirrel\Queries\DBBuilderInterface for the query builder. The low-level interface options are based upon Doctrine and PDO with some tweaks, and the builder interface is an expressive way to write structured (and not too complex) queries.
3131

32-
If you know Doctrine or PDO you should be able to use this library easily. You should especially have an extra look at structured queries and UPSERT, as these are an addition to the low-level interface, helping you to make readable queries and taking care of your column field names and parameters automatically, making it easier to write secure queries.
32+
If you know Doctrine or PDO you should be able to use this library easily. You should especially have an extra look at structured queries and UPSERT, as these are additions to the low-level interface, helping you to make readable queries and taking care of your column field names and parameters automatically, making it easier to write secure queries.
3333

3434
For a solution which integrates easily with the Symfony framework, check out [squirrelphp/queries-bundle](https://github.com/squirrelphp/queries-bundle), and for entity and repository support check out [squirrelphp/entities](https://github.com/squirrelphp/entities) and [squirrelphp/entities-bundle](https://github.com/squirrelphp/entities-bundle).
3535

@@ -67,7 +67,7 @@ If you want to assemble a DBInterface object yourself, something like the follow
6767
// anywhere you need it. Typehint it with
6868
// \Squirrel\Queries\DBInterface
6969

70-
$fetchEntry = function(DBInterface $db) {
70+
$fetchEntry = function(DBInterface $db): array {
7171
return $db->fetchOne('SELECT * FROM table');
7272
};
7373

@@ -108,7 +108,7 @@ This library has support for the three main open-source databases:
108108

109109
- MySQL, all versions (at least 5.5+ is recommended)
110110
- MariaDB, all versions (MariaDB behaves almost identical to MySQL)
111-
- SQLite, all versions, although native UPSERT queries are only supported in SQLite 3.24+ (which is included in PHP 7.3+), the functionality is emulated in lower versions
111+
- SQLite, all versions, although native UPSERT queries are only supported in SQLite 3.24+, the functionality is emulated in lower versions
112112
- Postgres version 9.5 and above, because UPSERT queries were implemented in 9.5
113113

114114
The functionality in this library has been tested against real versions of these databases to make sure it works, although there might be edge cases which warrant adjustments. If you find any issues please report them.
@@ -215,7 +215,7 @@ $selectStatement = $db->select('SELECT `fufumama`,`b`.`lalala`,`a`.`setting_valu
215215
- If an expression contains something like :fieldname: it is assumed that it is a field or table name which will then be escaped. For simple WHERE restrictions or fields definitions field names are escaped automatically.
216216
- You can use "field" if there is just one field, or "fields" for multiple fields. The same with "table" and "tables".
217217
- If you set "lock" to true "FOR UPDATE" is added to the query, so the results are locked within the current transaction.
218-
- The arguments are checked as much as possible and if an option/expression is not valid, a DBInvalidOptionException is thrown. This does not include SQL errors, as the SQL components knows nothing of the allowed field names, table names or what constitutes a valid SQL expression.
218+
- The arguments are checked as much as possible and if an option/expression is not valid, a DBInvalidOptionException is thrown. This does not include SQL errors, as the SQL components know nothing of the allowed field names, table names or what constitutes a valid SQL expression.
219219

220220
You can pass a structured SELECT query directly to `fetchOne` and `fetchAll` to retrieve one or all results.
221221

@@ -377,7 +377,7 @@ Just pass a callable/function to the `transaction` method and DBInterface will t
377377
#### Examples
378378

379379
```php
380-
$db->transaction(function(){
380+
$db->transaction(function() {
381381
// Do queries in here as much as you want, it will all be one transaction
382382
// and committed as soon as this function ends
383383
});
@@ -392,9 +392,9 @@ $db->transaction(function() use ($db) {
392392
], 'tableId');
393393

394394
$db->update('otherTable', [
395-
'tableName' => 'Henry',
396-
], [
397395
'tableId' => $tableId,
396+
], [
397+
'tableName' => 'Henry',
398398
]);
399399
});
400400
```
@@ -415,30 +415,30 @@ $db->transaction(function() use ($db) {
415415
// transaction function, which is what you would want / expect, so it starts
416416
// with the Henry insert again
417417
$db->update('otherTable', [
418-
'tableName' => 'Henry',
419-
], [
420418
'tableId' => $tableId,
419+
], [
420+
'tableName' => 'Henry',
421421
]);
422422
});
423423
});
424424
```
425425

426426
If there is a deadlock or connection problem, the error handler (DBErrorHandler) will roll back the transaction and attempt to retry it 10 times, with increasing wait times inbetween. Only if there are 10 failures within about 30 seconds will the exception be escalated with a DBException.
427427

428-
If you want to pass arguments to $func, this would be an example:
428+
If you want to pass arguments to $func, this would be an example (you can also add them to the `use` part):
429429

430430
```php
431-
$db->transaction(function($db, $table, $tableName) {
431+
$db->transaction(function(string $table, string $tableName) use ($db) {
432432
$tableId = $db->insert('myTable', [
433433
'tableName' => 'Henry',
434434
], 'tableId');
435435

436436
$db->update('otherTable', [
437-
'tableName' => $tableName,
438-
], [
439437
'tableId' => $tableId,
438+
], [
439+
'tableName' => $tableName,
440440
]);
441-
}, $db, 'myTable', 'Henry');
441+
}, 'myTable', 'Henry');
442442
```
443443

444444
When using SELECT queries within a transaction you should always remember that the results are usually not locked (so not protected from UPDATE or DELETE), except if you apply "... FOR UPDATE" (in a string SELECT query) or by setting `lock` to true in a structured SELECT.
@@ -479,13 +479,14 @@ DBBuilderInterface offers the following functions:
479479
- transaction (to do a function within a transaction)
480480
- getDBInterface (to get the underlying DBInterface object)
481481

482-
All except the last two return a builder object which helps you easily create a query and get the results. Compared to DBInterface you do not have to remember what data can be contained in a structured query - your IDE will suggest whatever is available. You can also have a look at the builder objects themselves - they are all very short.
482+
All except the last two return a builder object which helps you easily create a query and get the results. Compared to DBInterface you do not have to remember what data can be contained in a structured query - your IDE will suggest whatever is available.
483483

484484
Looking at some examples should make the usage quite clear - here are some for each of the 6 builder functions:
485485

486486
### Count
487487

488488
```php
489+
// $usersNumber will be an integer
489490
$usersNumber = $dbBuilder
490491
->count()
491492
->inTables([
@@ -675,6 +676,8 @@ $rowsAffected = $dbBuilder
675676
->writeAndReturnAffectedNumber();
676677
```
677678

679+
This explicit confirmation clause is needed to avoid executing queries where the `where` part was omitted by accident, which is a common mistake when writing/executing queries.
680+
678681
### Insert or Update
679682

680683
This makes the insertOrUpdate functionality in DBInterface a bit easier to digest, using the same information:
@@ -738,6 +741,8 @@ $rowsAffected = $dbBuilder
738741
->writeAndReturnAffectedNumber();
739742
```
740743

744+
This explicit confirmation clause is needed to avoid executing queries where the `where` part was omitted by accident, which is a common mistake when writing/executing queries.
745+
741746
### Transaction
742747

743748
The transaction function works the same as the one in DBInterface - in fact, DBBuilderInterface just passes it as-is to DBInterface.
@@ -806,7 +811,7 @@ This should make it easy to read and write queries, even if you don't know much
806811
BLOB handling for Postgres
807812
--------------------------
808813

809-
For MySQL and SQLite retrieving or inserting/updating BLOBs (Binary Large Objects) works just the same as with shorter/non-binary string fields. Postgres needs some adjustments, but these are made very easy bis this library:
814+
For MySQL and SQLite retrieving or inserting/updating BLOBs (Binary Large Objects) works just the same as with shorter/non-binary string fields. Postgres needs some adjustments, but these are streamlined by this library:
810815

811816
- For SELECT queries, streams returned by Postgres are automatically converted into strings, mimicking how MySQL and SQLite are doing it
812817
- For INSERT/UPDATE queries, you need to wrap BLOB values with an instance of LargeObject provided by this library.
@@ -905,6 +910,6 @@ Sometimes a complex query can make more sense, but it should be the rare excepti
905910

906911
[squirrelphp/queries-bundle](https://github.com/squirrelphp/queries-bundle) is an integration of this library into Symfony, so you can get started quickly.
907912

908-
[squirrelphp/entities](https://github.com/squirrelphp/entities) is a library built on top of `squirrelphp/queries` and offers support for entities and repositories while following all the above guidelines.
913+
[squirrelphp/entities](https://github.com/squirrelphp/entities) is a library built on top of `squirrelphp/queries` and offers support for typed entities and repositories while following all the above guidelines.
909914

910915
[squirrelphp/entities-bundle](https://github.com/squirrelphp/entities-bundle) is the Symfony bundle integrating entities and repositories into a Symfony project.

0 commit comments

Comments
 (0)