From 070c8df22046474fef963479a3265d1a86de2d7b Mon Sep 17 00:00:00 2001 From: coppolafdz Date: Fri, 24 Jul 2020 16:17:24 +0200 Subject: [PATCH] Laravel 7.x support --- README.md | 53 +++++++++++++++++++------ composer.json | 8 ++-- src/MicroMySqlConnection.php | 8 ++-- src/MicroMySqlConnector.php | 30 +++++--------- src/MicroMySqlDriverServiceProvider.php | 3 +- 5 files changed, 58 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 54549bb..dd23c6e 100644 --- a/README.md +++ b/README.md @@ -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.
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.
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 db_user@127.0.0.1 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 db_user@127.0.0.1 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 + ``` diff --git a/composer.json b/composer.json index 6a34d74..353c491 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -12,7 +12,7 @@ "license": "ISC", "authors": [ { - "homepage": "http://github.com/coppolafab" + "homepage": "https://github.com/coppolafab" } ], "autoload": { @@ -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": { diff --git a/src/MicroMySqlConnection.php b/src/MicroMySqlConnection.php index 618eb6d..6a8170c 100644 --- a/src/MicroMySqlConnection.php +++ b/src/MicroMySqlConnection.php @@ -1,20 +1,18 @@ recordsModified && $this->readPdo !== null && $this->readPdo !== $this->pdo) { $this->setReadPdo(null); } } } - diff --git a/src/MicroMySqlConnector.php b/src/MicroMySqlConnector.php index ae274dc..fef177e 100644 --- a/src/MicroMySqlConnector.php +++ b/src/MicroMySqlConnector.php @@ -1,11 +1,9 @@ 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); } } } - diff --git a/src/MicroMySqlDriverServiceProvider.php b/src/MicroMySqlDriverServiceProvider.php index 5e19e30..e11c70d 100644 --- a/src/MicroMySqlDriverServiceProvider.php +++ b/src/MicroMySqlDriverServiceProvider.php @@ -1,11 +1,10 @@