Skip to content

Commit

Permalink
Merge pull request #5 from stats4sd/add-procs-and-functions
Browse files Browse the repository at this point in the history
Add procs and functions
  • Loading branch information
dave-mills authored Jun 19, 2023
2 parents 8277093 + 23c8274 commit f661e1b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 31 deletions.
99 changes: 68 additions & 31 deletions src/Commands/UpdateSqlViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -45,9 +45,46 @@ 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) {

if (Str::endsWith($file, '.sql')) {

$query = file_get_contents("{$dir_path}/{$file}");

$done = DB::unprepared($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
Expand All @@ -60,18 +97,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...
$this->processDir("${dir_path}/${file}");
$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);
Expand All @@ -82,7 +119,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);
Expand All @@ -103,7 +140,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);
}
Expand Down Expand Up @@ -136,7 +173,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];
Expand Down Expand Up @@ -187,12 +224,12 @@ 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).';';
$tempQueryStr = implode(', ', $tempQuery) . ';';

return 'SELECT '.$tempQueryStr;
return 'SELECT ' . $tempQueryStr;
}

/**
Expand All @@ -212,27 +249,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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/SqlViewsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/procedures/example-proc.sql'));
}

// Register the service the package provides.
$this->app->singleton('sqlviews', function ($app) {
return new SqlViews;
Expand Down
9 changes: 9 additions & 0 deletions src/database/procedures/example-proc.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP PROCEDURE IF EXISTS `multiply`;

CREATE PROCEDURE `multiply` (IN num_one INT, IN num_two INT, OUT result OUT)

BEGIN

SET result = num_one * num_two;

END

0 comments on commit f661e1b

Please sign in to comment.