Skip to content

Commit

Permalink
no issue - much more consistent vendor name and version api
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Apr 17, 2024
1 parent e8e32cc commit 1d6c2e6
Show file tree
Hide file tree
Showing 18 changed files with 361 additions and 92 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Next

* [feature] ⭐️ Added `DatabaseSession::getVendorName()`, `DatabaseSession::getVendorVersion()`,
`DatabaseSession::vendorIs()` and `DatabaseSession::vendorVersionIs()` methods.
* [deprecated] ⚠️ Deprecated `Bridge::getServerFlavor()`, `Bridge::getServerVersion()`,
`Bridge::isVersionLessThan()` and `Bridge::isVersionGreaterOrEqualThan()` methods,
they will be removed in 2.0 version.

## 1.3.3

* [feature] ⭐️ Experimental table index list in schema manager.
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default defineConfig({
{ text: 'PDO setup', link: '/introduction/getting-started#pdo-setup' },
{ text: 'Symfony setup', link: '/introduction/getting-started#symfony-setup' },
{ text: 'Error handling', link: '/bridges/error' },
{ text: 'Vendor conditionals', link: '/introduction/vendor-conditionals' },
]
},
{ text: 'Samples usages', link: '/introduction/usage' },
Expand Down
4 changes: 2 additions & 2 deletions docs/content/bridges/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ $queryBuilder = new DoctrineQueryBuilder($connection);
$queryBuilder->disableErrorConverter();
```

:::note
:::info
This works with all bridges.
:::

:::note
:::info
The `makinacorpus/query-builder-bundle` will per default configure the `doctrine/dbal`
bridge to disable specific error handling in order to be completely transparent for
users that will usually expect Doctrine DBAL errors.
Expand Down
2 changes: 1 addition & 1 deletion docs/content/introduction/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ See [the value converter documentation](../converter/converter) for supported da
Install it using composer:

```sh
composer require makinacorpus/query-builder doctrine/dbal:^3.7
composer require makinacorpus/query-builder doctrine/dbal:'^3.7|^4.0'
```

### 2. Setup the query builder
Expand Down
139 changes: 139 additions & 0 deletions docs/content/introduction/vendor-conditionals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Database vendor version conditionals

## Introduction

When you write portable code, you might want to dynamically branch your code
depending upon the database server vendor name or version. This library exposes
a few helpers method for doing that gracefully.

:::info
Version string must always be a semantic version string, in the `x.y.z`
(major, minor, patch) form where `x`, `y` and `z` are integers.
You can reduce it to either `x.y` or simply `x` whenever it suits you,
missing digits will always be replaced by `0`.
:::

This section documents methods on the `MakinaCorpus\QueryBuilder\DatabaseSession`
interface. All bridges implement it.

## Compare database vendor name

Simply use the `DatabaseSession::vendorIs()` method, such as:

```php
use MakinaCorpus\QueryBuilder\DatabaseSession;
use MakinaCorpus\QueryBuilder\Platform;

\assert($session instanceof DatabaseSession);

if ($session->vendorIs(Platform::MYSQL)) {
// Write code for MySQL.
}
if ($session->vendorIs(Platform::POSTGRESQL)) {
// Write code for PostgreSQL.
}
if ($session->vendorIs('postgresql')) {
// Variant with a user given raw string.
}
if ($session->vendorIs('mysql')) {
// Variant with a user given raw string.
}
```

And more importantly, all vendor name comparison methods accept a value
array for checking if the vendor is any of:

```php
use MakinaCorpus\QueryBuilder\DatabaseSession;
use MakinaCorpus\QueryBuilder\Platform;

\assert($session instanceof DatabaseSession);

if ($session->vendorIs([Platform::MARIADB, Platform::MYSQL)) {
// Write code for MariaDB and MySQL.
}
```

:::info
It is advised to use the `MakinaCorpus\QueryBuilder\Platform` constants for
checking against the vendor name, yet is not mandatory: user given vendor
name is a raw string, which will be lowercased and reduced to only latin
alphabet characters prior to being compared against.

For example the `SQL Server 2022` value will be reduced to `sqlserver`.

Values will be compared against a set of known synonyms in order to be
tolerant to user conventions, though it is not advised to rely upon these
tolerances since the known synonyms are not part of the public API and
might change.
:::

## Compare database version

Simply use the `DatabaseSession::vendorVersionIs()` method, such as:

```php
use MakinaCorpus\QueryBuilder\DatabaseSession;
use MakinaCorpus\QueryBuilder\Platform;

\assert($session instanceof DatabaseSession);

if ($session->vendorVersionIs('1.2.3')) {
// Server version is greater or equal to '1.2.3'.
}
if ($session->vendorVersionIs('1.2.3', '>=')) {
// Server version is greater or equal to '1.2.3' (explicit operator).
}
if ($session->vendorVersionIs('1.2')) {
// Server version is greater or equal to '1.2.0'.
}
if ($session->vendorVersionIs('1')) {
// Server version is greater or equal to '1.0.0'.
}
if ($session->vendorVersionIs('1.2.3', '<')) {
// Server version is less than '1.2.3'.
}
// ... See list of operators below.
```

Supported operators are:

- `<`: server version is lower than given value,
- `<=`: server version is lower or equal than given value,
- `=`: server version is equal to given value,
- `>=`: server version is greater or equal than given value,
- `>`: server version is greater than given value.

:::info
Version comparison operators are raw user given strings, nevertheless
giving an invalid value will raise an exception.
:::

:::info
Later, composer version notation such as `~1.2.3` or `^1.2|^2.0` might be
implemented for finer conditions to be written by user.
:::

## Compare both at once

Common use case is to check both vendor name and version at once:

Simply use the `DatabaseSession::vendorIs()` method, such as:

```php
use MakinaCorpus\QueryBuilder\DatabaseSession;
use MakinaCorpus\QueryBuilder\Platform;

\assert($session instanceof DatabaseSession);

if ($session->vendorIs(Platform::MYSQL, '8.0')) {
// Server version is MySQL with version greater or equal to '8.0'.
}
if ($session->vendorIs(Platform::MYSQL, '8.0', '>=')) {
// Server version is MySQL with version greater or equal to '8.0' (explicit operator).
}
```

:::info
All operators accepted by the `vendorVersionIs()` methods are supported.
:::
2 changes: 1 addition & 1 deletion docs/content/query/transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ $someSavepoint->rollback();
In all cases, it's your job to handle errors via exception catching.
:::

:::note
:::info
You can nest savepoints.
:::

Expand Down
Loading

0 comments on commit 1d6c2e6

Please sign in to comment.