Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mpyw/laravel-mysql-system-variable-manager
A tiny extension of MySqlConnection that manages session system variables
https://github.com/mpyw/laravel-mysql-system-variable-manager
laravel-mysql mysql pdo pdo-mysql php sql variables
Last synced: 3 months ago
JSON representation
A tiny extension of MySqlConnection that manages session system variables
- Host: GitHub
- URL: https://github.com/mpyw/laravel-mysql-system-variable-manager
- Owner: mpyw
- License: mit
- Created: 2019-07-12T02:48:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-21T17:55:20.000Z (about 1 year ago)
- Last Synced: 2024-05-12T02:40:57.675Z (8 months ago)
- Topics: laravel-mysql, mysql, pdo, pdo-mysql, php, sql, variables
- Language: PHP
- Homepage:
- Size: 61.5 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel MySQL System Variable Manager
[![Build Status](https://github.com/mpyw/laravel-mysql-system-variable-manager/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/mpyw/laravel-mysql-system-variable-manager/actions) [![Coverage Status](https://coveralls.io/repos/github/mpyw/laravel-mysql-system-variable-manager/badge.svg?branch=migrate-ci)](https://coveralls.io/github/mpyw/laravel-mysql-system-variable-manager?branch=migrate-ci) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mpyw/laravel-mysql-system-variable-manager/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mpyw/laravel-mysql-system-variable-manager/?branch=master)A tiny extension of `MySqlConnection` that manages **session** system variables
## Requirements
| Package | Version | Mandatory |
|:--------|:--------------------------------------|:---------:|
| PHP |^8.0
| ✅ |
| Laravel |^9.0 || ^10.0
| ✅ |
| PHPStan |>=1.1
| |## Installing
```bash
composer require mpyw/laravel-mysql-system-variable-manager
```## Basic Usage
> [!IMPORTANT]
> The default implementation is provided by `MySqlConnectionServiceProvider`, however, **package discovery is not available**.
Be careful that you MUST register it in **`config/app.php`** by yourself.```php
[
/* ... */Mpyw\LaravelMySqlSystemVariableManager\MySqlConnectionServiceProvider::class,
/* ... */
],];
``````php
10.0, 'transaction_isolation' => 'read-committed']);// Assign a variable on a different connection
DB::connection('other_mysql_connection')->setSystemVariable('long_query_time', 10.0);// Run callback temporarily assigning a variable
DB::usingSystemVariable('long_query_time', 10.0, function () {
/* ... */
});// Run callback temporarily assigning multiple variables
DB::usingSystemVariables(['long_query_time' => 10.0, 'transaction_isolation' => 'read-committed'], function () {
/* ... */
});// Run callback replacing current value
// NOTE: You MUST declare closure return types.
DB::usingSystemVariables(
[
'long_query_time' => function (float $currentValue): float {
return $currentValue + 5.0;
},
'sql_mode' => function (string $currentValue): string {
return str_replace('ONLY_FULL_GROUP_BY', '', $currentValue);
},
],
function () {
/* ... */
}
);
```> [!CAUTION]
> Don't use `DB::disconnect()` directly or auto-recovery won't be fired.
> Use **`DB::connection()->disconnect()`** instead.## Advanced Usage
> [!TIP]
> You can extend `MySqlConnection` with `ManagesSystemVariables` trait by yourself.```php
usingSystemVariable('foreign_key_checks', false, $callback, ...$args);
}
public function allowingPartialGroupBy(callable $callback, ...$args)
{
return $this->usingSystemVariable('sql_mode', function (string $mode): string {
return str_replace('ONLY_FULL_GROUP_BY', '', $mode);
}, $callback, ...$args);
}
}
``````php
user()->associate(Auth::user());
$post->save();DB::withoutForeignKeyChecks(function () use ($post) {
$post->user()->associate(null);
$post->save();
});
```