From 7e71b46e02b93ec548c32948d72547a0aadb62c8 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 9 Jan 2018 23:05:52 -0600 Subject: [PATCH] Enable ATTR_EMULATE_PREPARES for MariaDB specifically #132 --- app/Console/Commands/DevCommands.php | 26 +++++++++++--- config/database.php | 2 +- .../Resources/views/stubs/env.blade.php | 1 + .../Installer/Services/EnvironmentService.php | 34 ++++++++++++++++--- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/app/Console/Commands/DevCommands.php b/app/Console/Commands/DevCommands.php index 3146934f..31f0f0f9 100644 --- a/app/Console/Commands/DevCommands.php +++ b/app/Console/Commands/DevCommands.php @@ -2,12 +2,11 @@ namespace App\Console\Commands; -use App\Models\Airline; -use App\Models\Permission; -use App\Models\Role; -use App\Models\User; -use App\Services\DatabaseService; use DB; +use PDO; + +use App\Models\Airline; +use App\Models\User; use App\Console\BaseCommand; use App\Models\Acars; @@ -34,6 +33,7 @@ class DevCommands extends BaseCommand 'clear-acars' => 'clearAcars', 'clear-users' => 'clearUsers', 'compile-assets' => 'compileAssets', + 'db-attrs' => 'dbAttrs', ]; if(!array_key_exists($command, $commands)) { @@ -92,4 +92,20 @@ class DevCommands extends BaseCommand $this->runCommand('npm update'); $this->runCommand('npm run dev'); } + + /** + * Output DB prepares versions + */ + protected function dbAttrs() + { + $pdo = DB::connection()->getPdo(); + $emulate_prepares_below_version = '5.1.17'; + $server_version = $pdo->getAttribute(PDO::ATTR_SERVER_VERSION); + $emulate_prepares = version_compare($server_version, $emulate_prepares_below_version, '<'); + + $this->info('Server Version: '. $server_version); + $this->info('Emulate Prepares: '.$emulate_prepares); + + $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $emulate_prepares); + } } diff --git a/config/database.php b/config/database.php index 0e71c0ba..1ca9134c 100755 --- a/config/database.php +++ b/config/database.php @@ -19,7 +19,7 @@ return [ 'strict' => false, 'engine' => null, 'options' => [ - PDO::ATTR_EMULATE_PREPARES => false, + PDO::ATTR_EMULATE_PREPARES => env('DB_EMULATE_PREPARES', false), #PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ], ], diff --git a/modules/Installer/Resources/views/stubs/env.blade.php b/modules/Installer/Resources/views/stubs/env.blade.php index ca093249..32111a22 100644 --- a/modules/Installer/Resources/views/stubs/env.blade.php +++ b/modules/Installer/Resources/views/stubs/env.blade.php @@ -25,6 +25,7 @@ DB_PORT={!! $DB_PORT !!} DB_DATABASE={!! $DB_NAME !!} DB_USERNAME={!! $DB_USER !!} DB_PASSWORD={!! $DB_PASS !!} +DB_EMULATE_PREPARES={!! $DB_EMULATE_PREPARES !!} DB_PREFIX= MAIL_DRIVER=smtp diff --git a/modules/Installer/Services/EnvironmentService.php b/modules/Installer/Services/EnvironmentService.php index d07a6a71..be546b1b 100644 --- a/modules/Installer/Services/EnvironmentService.php +++ b/modules/Installer/Services/EnvironmentService.php @@ -2,18 +2,21 @@ namespace Modules\Installer\Services; -use Illuminate\Encryption\Encrypter; use Log; -use Symfony\Component\Filesystem\Exception\IOException; +use PDO; +use Illuminate\Encryption\Encrypter; use Symfony\Component\HttpFoundation\File\Exception\FileException; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Filesystem\Exception\IOExceptionInterface; +/** + * Class EnvironmentService + * @package Modules\Installer\Services + */ class EnvironmentService { /** * Create the .env file * @return boolean + * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException */ public function createEnvFile($driver, $host, $port, $name, $user, $pass) { @@ -26,6 +29,7 @@ class EnvironmentService 'DB_NAME' => $name, 'DB_USER' => $user, 'DB_PASS' => $pass, + 'DB_EMULATE_PREPARES' => false, ]; $opts = $this->getCacheDriver($opts); @@ -45,6 +49,28 @@ class EnvironmentService return base64_encode(Encrypter::generateKey(config('app.cipher'))); } + /** + * @param $opts + * @return mixed + */ + protected function determinePdoOptions($opts) + { + $dsn = "mysql:host=$opts[DB_HOST];port=$opts[DB_PORT];"; + Log::info('Connection string: ' . $dsn); + + $conn = new PDO($dsn, $opts['DB_USER'], $opts['DB_PASS']); + $version = strtolower($conn->getAttribute(PDO::ATTR_SERVER_VERSION)); + + # If it's mariadb, enable the emulation for prepared statements + # seems to be throwing a problem on 000webhost + # https://github.com/nabeelio/phpvms/issues/132 + if(strpos($version, 'mariadb') !== false) { + $opts['DB_EMULATE_PREPARES'] = true; + } + + return $opts; + } + /** * Determine is APC is installed, if so, then use it as a cache driver * @param $opts