Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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();
});
```