Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
Laravel 7.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
friendzapp committed Jul 24, 2020
1 parent d156ca2 commit 070c8df
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 44 deletions.
53 changes: 41 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
# What is this?
A minimal extension of Laravel default behaviour to minimize interactions after connection towards a MySql Server. Compatible with Laravel 5.7+, 6.x and 7.x .

A minimal extension of Laravel default behaviour to minimize interactions after connection towards a MySql Server.
## Why?
* ```set names ...``` statement affects only the server, causing possible differences on operations that involve charsets, especially while enabling prepared statement emulation.<br>Setting connection charset via PDO DSN, instead, affect both client and server.
* Declaring the connection database into PDO DSN, allow to automatically select the db, without execute more statements.<br>Also, some Connections Proxies do not allow execution of ```use ${dbname}``` statement.
* ```set names ...``` statement could be necessary if you need to specify a collation different from the server default for the specified charset. If you can stick with the default (ie. for utf8mb4, is utf8mb4_general_ci on MySQL 5.7 and utf8mb4_0900_ai_ci on MySQL 8.0), you should unset ```database.connections.mysql.collation```.
* Such configuration statements, can cause connection pinning on Proxies.
* When using Cluster with read-only Replicas, any script execution that also writes to the DB, handles 2 connections. If using stickiness, the read-only handle is unused and unnecessary occupying a MySQL thread.

## What are the differences?
#### MySQL general log output with default driver and config:
```
2020-07-24T12:55:15.580117Z 3 Connect [email protected] on db_schema using TCP/IP
2020-07-24T12:55:15.581060Z 3 Query use `db_schema`
2020-07-24T12:55:15.582896Z 3 Prepare set names 'utf8mb4'
2020-07-24T12:55:15.583003Z 3 Execute set names 'utf8mb4'
2020-07-24T12:55:15.583159Z 3 Close stmt
2020-07-24T12:55:15.583256Z 3 Prepare set session sql_mode='NO_ENGINE_SUBSTITUTION'
2020-07-24T12:55:15.583331Z 3 Execute set session sql_mode='NO_ENGINE_SUBSTITUTION'
2020-07-24T12:55:15.583448Z 3 Close stmt
2020-07-24T12:55:15.583531Z 3 Query select true
```

#### MySQL general log output with micro driver:
```
2020-07-24T12:51:18.507740Z 2 Connect [email protected] on db_schema using TCP/IP
2020-07-24T12:51:18.509693Z 2 Query select true
```
This result can be obtained with all ```database.connections.mysql.collation```, ```database.connections.mysql.strict``` and ```database.connections.mysql.timezone``` set to ```NULL```.

## What are the differences?
* Set charset into PDO DSN.
* Do not execute the ```use ${dbname}``` statement, already declared into PDO DSN.
* Do not override server sql_mode by default. Any custom mode set with database.connections.mysql.modes or database.connections.mysql.strict is preserved.
* Do not execute ```set names ...``` unless a collation is provided.
* Avoid prepared statements creation.
* When using Cluster with Replicas, and connection stickiness is enabled, close immediately the read-only connection after a write.
* Do not execute the ```use ${dbname}``` statement, read from PDO DSN.
* Do not execute ```set names ...``` unless you provide an explicit ```database.connections.mysql.collation```.
* When using Cluster with read-only Replicas, and ```database.connections.mysql.sticky``` is true, close immediately the read-only connection after the switch to the read-write instance.

## How to use

* ```composer require coppolafab/micro-laravel-mysql-driver```
* Add these to your mysql database config: ```'microOverrideDriver' => env('DB_MICROMYSQL_OVERRIDE_DRIVER', false), 'microCloseReadConnectionAfterWrite' => env('DB_MICROMYSQL_CLOSE_READ_CONNECTION_AFTER_WRITE', false),```

Enable those via .env file.
* Run ```composer require coppolafab/micro-laravel-mysql-driver```
* Add database config options to the mysql section:
```
'microOverrideDriver' => env('DB_MICROMYSQL_OVERRIDE_DRIVER', false),
'microCloseReadConnectionAfterWrite' => env('DB_MICROMYSQL_CLOSE_READ_CONNECTION_AFTER_WRITE', false),
```
* Enable new config options via .env file:
```
DB_MICROMYSQL_OVERRIDE_DRIVER=true
DB_MICROMYSQL_CLOSE_READ_CONNECTION_AFTER_WRITE=true
```
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coppolafab/micro-laravel-mysql-driver",
"description": "Lighter Laravel MySQL driver",
"description": "A minimal extension of Laravel default behaviour to minimize interactions after connection towards a MySql Server",
"keywords": [
"database",
"mysql",
Expand All @@ -12,7 +12,7 @@
"license": "ISC",
"authors": [
{
"homepage": "http://github.com/coppolafab"
"homepage": "https://github.com/coppolafab"
}
],
"autoload": {
Expand All @@ -23,8 +23,8 @@
"require": {
"php": ">=7.2",
"ext-pdo": "*",
"illuminate/support": "^5.7|^6.0",
"illuminate/database": "^5.7|^6.0"
"illuminate/support": "^5.7|^7.0",
"illuminate/database": "^5.7|^7.0"
},
"extra": {
"laravel": {
Expand Down
8 changes: 3 additions & 5 deletions src/MicroMySqlConnection.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?php

declare(strict_types=1);

namespace coppolafab\MicroMySqlDriver;

use Illuminate\Database\MySqlConnection as BaseMysqlConnection;
use Illuminate\Database\MySqlConnection;

class MicroMySqlConnection extends BaseMysqlConnection
class MicroMySqlConnection extends MySqlConnection
{
public function recordsHaveBeenModified($value = true)
{
parent::recordsHaveBeenModified($value);
parent::recordsHaveBeenModified($value);

if ($this->recordsModified && $this->readPdo !== null && $this->readPdo !== $this->pdo) {
$this->setReadPdo(null);
}
}
}

30 changes: 9 additions & 21 deletions src/MicroMySqlConnector.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php

declare(strict_types=1);

namespace coppolafab\MicroMySqlDriver;

use Illuminate\Database\Connectors\MySqlConnector;
use PDO;

class MicroMySqlConnector extends MySqlConnector
{
Expand All @@ -19,38 +17,28 @@ public function connect(array $config)
$this->setModes($connection, $config);
return $connection;
}

protected function getDsn(array $config)
{
$dsn = parent::getDsn($config);
$dsn = parent::getDsn($config);

if (isset($config['charset'])) {
if (isset($config['charset'])) {
$dsn .= ';charset=' . $config['charset'];
}
}

return $dsn;
return $dsn;
}

protected function configureEncoding($connection, array $config)
{
if (!isset($config['charset'])) {
return;
}

$collation = $this->getCollation($config);
}

if ($collation) {
$connection->exec("set names '{$config['charset']}'" . $collation);
}
}
$collation = $this->getCollation($config);

protected function setModes(PDO $connection, array $config)
{
if (isset($config['modes'])) {
$this->setCustomModes($connection, $config);
} elseif (!empty($config['strict'])) {
$connection->prepare($this->strictMode($connection))->execute();
if ($collation) {
$connection->exec("set names '{$config['charset']}'" . $collation);
}
}
}

3 changes: 1 addition & 2 deletions src/MicroMySqlDriverServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

declare(strict_types=1);

namespace coppolafab\MicroMySqlDriver;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Connection;
use Illuminate\Support\ServiceProvider;

class MicroMySqlDriverServiceProvider extends ServiceProvider
{
Expand Down

0 comments on commit 070c8df

Please sign in to comment.