From 142b2e456554e12a28cae0c83507e273e28deb8d Mon Sep 17 00:00:00 2001 From: Dave Mills Date: Fri, 16 Jun 2023 11:00:13 +0100 Subject: [PATCH 1/5] add proc handling --- src/Commands/UpdateSqlViews.php | 84 +++++++++++++++++------- src/database/procedures/example-proc.sql | 7 ++ 2 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 src/database/procedures/example-proc.sql diff --git a/src/Commands/UpdateSqlViews.php b/src/Commands/UpdateSqlViews.php index 2f330d2..09b3a79 100644 --- a/src/Commands/UpdateSqlViews.php +++ b/src/Commands/UpdateSqlViews.php @@ -25,7 +25,7 @@ class UpdateSqlViews extends Command * * @var string */ - protected $description = 'Creates / Replaces all mySQL Views in the database with those in the database/views folder'; + protected $description = 'Creates / Replaces all mySQL Views in the database with those in the database/views folder, and runs any additional functions in the procedures folder.'; /** * Create a new command instance. @@ -45,9 +45,43 @@ public function __construct() public function handle() { $countViews = $this->processDir(base_path('database/views')); - $this->info($countViews.' views created'); + $this->info($countViews . ' views created'); + + + $countProcs = $this->processProcsDir(base_path('database/procedures')); + $this->info($countProcs . ' procedures run'); + } + + public function processProcsDir(string $dir_path) + { + + // process procs + $files = scandir(base_path('database/procedures')); + $countProcs = 0; + + //iterate through subfolders and add to main set of files to check + foreach ($files as $file) { + if (is_dir("{$dir_path}/{$file}") && $file != '.' && $file != '..') { + $folder_files = scandir("{$dir_path}/{$file}"); + + // engage recursion... + $this->processDir("{$dir_path}/{$file}"); + } + } + + foreach ($files as $file) { + + $query = file_get_contents("${dir_path}/${file}"); + + $done = DB::statement($query); + + if($done) { + $countProcs++; + } + } } + /** * Takes a directory path and scans it (and all subfolders) for .sql files to turn into MySQL Views. * @param string $dir_path path/to/mysql_view/storage @@ -64,7 +98,7 @@ public function processDir(string $dir_path) $folder_files = scandir("${dir_path}/${file}"); // engage recursion... - $this->processDir("${dir_path}/${file}"); + $done = $this->processDir("${dir_path}/${file}"); } } @@ -136,7 +170,7 @@ public function makePlaceholderView(string $name, string $query) $query = ''; while ($selectCount > 0) { - if (! isset($test[$pos])) { + if (!isset($test[$pos])) { dd($file, $test); } $query .= $test[$pos]; @@ -190,9 +224,9 @@ public function makePlaceholderView(string $name, string $query) return "1 AS `${item}`"; }, $columnNames); - $tempQueryStr = implode(', ', $tempQuery).';'; + $tempQueryStr = implode(', ', $tempQuery) . ';'; - return 'SELECT '.$tempQueryStr; + return 'SELECT ' . $tempQueryStr; } /** @@ -212,27 +246,27 @@ public function parseBrackets(string $str, $substr) for ($i = 0; $i < $len; $i++) { $char = $str[$i]; switch ($char) { - case '(': - $depth++; - break; - case $substr: - if (! $depth) { - if ($buffer !== '') { - $stack[] = $buffer; + case '(': + $depth++; + break; + case $substr: + if (!$depth) { + if ($buffer !== '') { + $stack[] = $buffer; + $buffer = ''; + } + continue 2; + } + break; + case ')': + if ($depth) { + $depth--; + } else { + $stack[] = $buffer . $char; $buffer = ''; + continue 2; } - continue 2; - } - break; - case ')': - if ($depth) { - $depth--; - } else { - $stack[] = $buffer.$char; - $buffer = ''; - continue 2; - } - break; + break; } $buffer .= $char; } diff --git a/src/database/procedures/example-proc.sql b/src/database/procedures/example-proc.sql new file mode 100644 index 0000000..08ae001 --- /dev/null +++ b/src/database/procedures/example-proc.sql @@ -0,0 +1,7 @@ +CREATE PROCEDURE `multiply`(IN num_one INT, IN num_two INT, OUT result OUT) + +BEGIN + +SET result = num_one * num_two; + +END \ No newline at end of file From 89ad28be184684eb9ddeb67fccc2957393dccf94 Mon Sep 17 00:00:00 2001 From: Dave Mills Date: Fri, 16 Jun 2023 11:11:43 +0100 Subject: [PATCH 2/5] add proc folder to install; update string syntax --- src/Commands/UpdateSqlViews.php | 16 ++++++++-------- src/SqlViewsServiceProvider.php | 5 +++++ src/database/procedures/example-proc.sql | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Commands/UpdateSqlViews.php b/src/Commands/UpdateSqlViews.php index 09b3a79..ad2806a 100644 --- a/src/Commands/UpdateSqlViews.php +++ b/src/Commands/UpdateSqlViews.php @@ -71,7 +71,7 @@ public function processProcsDir(string $dir_path) foreach ($files as $file) { - $query = file_get_contents("${dir_path}/${file}"); + $query = file_get_contents("{$dir_path}/{$file}"); $done = DB::statement($query); @@ -94,18 +94,18 @@ public function processDir(string $dir_path) //iterate through subfolders and add to main set of files to check foreach ($files as $file) { - if (is_dir("${dir_path}/${file}") && $file != '.' && $file != '..') { - $folder_files = scandir("${dir_path}/${file}"); + if (is_dir("{$dir_path}/{$file}") && $file != '.' && $file != '..') { + $folder_files = scandir("{$dir_path}/{$file}"); // engage recursion... - $done = $this->processDir("${dir_path}/${file}"); + $done = $this->processDir("{$dir_path}/{$file}"); } } //reset all views to "placeholder" views - to avoid problems when a view relies on another view that is not yet created; foreach ($files as $file) { if (Str::endsWith($file, '.sql')) { - $query = file_get_contents("${dir_path}/${file}"); + $query = file_get_contents("{$dir_path}/{$file}"); $name = Str::replaceLast('.sql', '', $file); $query = $this->makePlaceholderView($name, $query); @@ -116,7 +116,7 @@ public function processDir(string $dir_path) //Need to iterate through all the files twice, to avoid creating a "final" view before all "placeholder" views are created; foreach ($files as $file) { if (Str::endsWith($file, '.sql')) { - $query = file_get_contents("${dir_path}/${file}"); + $query = file_get_contents("{$dir_path}/{$file}"); $name = Str::replaceLast('.sql', '', $file); $done = $this->makeView($name, $query); @@ -137,7 +137,7 @@ public function processDir(string $dir_path) */ public function makeView(string $name, string $query) { - $view = "CREATE OR REPLACE VIEW ${name} AS \n${query}"; + $view = "CREATE OR REPLACE VIEW {$name} AS \n{$query}"; return DB::statement($view); } @@ -221,7 +221,7 @@ public function makePlaceholderView(string $name, string $query) //now, define a placeholder view using the exact column-names to be used in the final view: $tempQuery = array_map(function ($item) { - return "1 AS `${item}`"; + return "1 AS `{$item}`"; }, $columnNames); $tempQueryStr = implode(', ', $tempQuery) . ';'; diff --git a/src/SqlViewsServiceProvider.php b/src/SqlViewsServiceProvider.php index 7f7cde0..5085b43 100644 --- a/src/SqlViewsServiceProvider.php +++ b/src/SqlViewsServiceProvider.php @@ -43,6 +43,11 @@ public function register() copy(__DIR__.'/database/views/example.sql', base_path('database/views/example.sql')); } + if (!is_dir(base_path('database/procedures'))) { + mkdir(base_path('database/procedures')); + copy(__DIR__ . '/database/procedures/example-proc.sql', base_path('database/views/example-proc.sql')); + } + // Register the service the package provides. $this->app->singleton('sqlviews', function ($app) { return new SqlViews; diff --git a/src/database/procedures/example-proc.sql b/src/database/procedures/example-proc.sql index 08ae001..df677cb 100644 --- a/src/database/procedures/example-proc.sql +++ b/src/database/procedures/example-proc.sql @@ -1,4 +1,4 @@ -CREATE PROCEDURE `multiply`(IN num_one INT, IN num_two INT, OUT result OUT) +CREATE PROCEDURE `multiply` (IN num_one INT, IN num_two INT, OUT result OUT) BEGIN From b2e8c8c1114d0fca43ab1126b684b8cdd1719aeb Mon Sep 17 00:00:00 2001 From: Dave Mills Date: Fri, 16 Jun 2023 11:27:53 +0100 Subject: [PATCH 3/5] fix example paths --- src/SqlViewsServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SqlViewsServiceProvider.php b/src/SqlViewsServiceProvider.php index 5085b43..396deb3 100644 --- a/src/SqlViewsServiceProvider.php +++ b/src/SqlViewsServiceProvider.php @@ -45,7 +45,7 @@ public function register() if (!is_dir(base_path('database/procedures'))) { mkdir(base_path('database/procedures')); - copy(__DIR__ . '/database/procedures/example-proc.sql', base_path('database/views/example-proc.sql')); + copy(__DIR__ . '/database/procedures/example-proc.sql', base_path('database/procedures/example-proc.sql')); } // Register the service the package provides. From 94a96b2741ea479a7502646334dd2710a5ad0c15 Mon Sep 17 00:00:00 2001 From: Dave Mills Date: Fri, 16 Jun 2023 12:07:51 +0100 Subject: [PATCH 4/5] tweaks --- src/Commands/UpdateSqlViews.php | 13 ++++++++----- src/database/procedures/example-proc.sql | 2 ++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Commands/UpdateSqlViews.php b/src/Commands/UpdateSqlViews.php index ad2806a..aec1422 100644 --- a/src/Commands/UpdateSqlViews.php +++ b/src/Commands/UpdateSqlViews.php @@ -71,12 +71,15 @@ public function processProcsDir(string $dir_path) foreach ($files as $file) { - $query = file_get_contents("{$dir_path}/{$file}"); + if (Str::endsWith($file, '.sql')) { + + $query = file_get_contents("{$dir_path}/{$file}"); - $done = DB::statement($query); + $done = DB::statement($query); - if($done) { - $countProcs++; + if ($done) { + $countProcs++; + } } } } @@ -98,7 +101,7 @@ public function processDir(string $dir_path) $folder_files = scandir("{$dir_path}/{$file}"); // engage recursion... - $done = $this->processDir("{$dir_path}/{$file}"); + $this->processDir("{$dir_path}/{$file}"); } } diff --git a/src/database/procedures/example-proc.sql b/src/database/procedures/example-proc.sql index df677cb..2897c8f 100644 --- a/src/database/procedures/example-proc.sql +++ b/src/database/procedures/example-proc.sql @@ -1,3 +1,5 @@ +DROP PROCEDURE IF EXISTS `multiply`; + CREATE PROCEDURE `multiply` (IN num_one INT, IN num_two INT, OUT result OUT) BEGIN From 23c8274b7c9400edcad8e7bd1495228480f6c5f5 Mon Sep 17 00:00:00 2001 From: Dave Mills Date: Fri, 16 Jun 2023 12:23:17 +0100 Subject: [PATCH 5/5] swap to db::unprepared - db::statement appears to fail on creating procedures --- src/Commands/UpdateSqlViews.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/UpdateSqlViews.php b/src/Commands/UpdateSqlViews.php index aec1422..d3e135b 100644 --- a/src/Commands/UpdateSqlViews.php +++ b/src/Commands/UpdateSqlViews.php @@ -75,7 +75,7 @@ public function processProcsDir(string $dir_path) $query = file_get_contents("{$dir_path}/{$file}"); - $done = DB::statement($query); + $done = DB::unprepared($query); if ($done) { $countProcs++;