GenSql.ps1
cd $PSScriptRoot function Is-FileNewer ($f1,$f2) { (Test-Path $f2) -or ((dir $f1).LastWriteTime -gt (dir $f2).LastWriteTime) } function Upd-PsmFile ($fn) { $1 = '.\'+$Fn+'.ps1'; $2 = '.\'+$Fn+'.psm1'; if(Is-filenewer $1 $2) { copy $1 $2 } } Upd-PsmFil GenSql enum LinTy{L1;L12;L123;L2;L23;L3;Rmk;Blank; Er_L1_ShouldHave_1or2or3_Term Er_L2_ShouldHave_1or2_Term Er_L3_ShouldHave_1Term Er_Lin_ShouldBe_OneOf_L1_L2_L3_Rmk_Blank} ############################################################################################### function Gen-SqlPth { process { $p = Get-Dft $_ ($pwd.Path) Get-FfnAy $p '*.sql3' | Gen-SqlFil } } ############################################################################################### function Gen-SqlFil { process { $isEr,$ly = Get-FtLy $input | Vdt-Sql3 $fn = if($isEr) {$_} else { Repl-Ext $sql3Fn ".sql" } $ly = if($isEr) {$ly} else { $ly = Get-Sql $ly } Wrt-Obj $ly $fn } } ############################################################################################### function Vdt-Sql3 { begin{$isEr=false;$o=@()} process{ $lin = $_ | Rmv-3DashRmk | Add-IndtErMsg | Add-LvlErMsg | Add-ExprErMsg if(Has-Sub $lin "---"){$isEr=$true} $o+=$lin } end{$isEr,$o} } function Get-Sql { $input | Get-3Lvl | Get-Hash | Get-RootSql } ############################################################################################## function Rmv-3DashRmk { process{ Tak-FstOrAll $_ "---" } } function Add-IndtErMsg { process{ $_ + (Get-LinTyErMsg $_) } } function Add-LvlErMsg { process{ $_ + (Get-LvlErMsg $_) } } function Add-ExprErMsg { process{ $_ + (Get-ExprErmsg $_) } } #--------------------------------------------------------------------------------------------- function Get-3Lvl {} function Get-Hash {} function Get-RootSql {} ############################################################################################## function Get-Hash ($sql3Ly) { [HashTable]$wip = .{ $wip = @{} foreach($lin in (Get-3Lvl $sql3Ly)) { $1,$2,$3 = Get-Term $lin $key = $1 + "." + $2 $val = $3 wip[$key] = $val } $wip.GetEnumerator() $wip } [HashTable]$done = .{ $done = @{} $some = $true while($some) { $some = $false foreach($kv in $wip.GetEnumerator()) { $k = $kv.Key $v = $kv.Value $fnd, $val = Eval-Value $v,$done if($fnd) { $wip.Remove($k) $done[$k] = $val $some = $true } } } $done } $done } function Get-3LvlLy {param([Parameter(valueFromPipeLine=$true)]$sql3Ly) begin {$1=""; $2=""} process {$sql3Ly|foreach{$s,$1,$2=Get-3LvlLy--1 $_ $1 $2; if(Is-NonEmpty $s){$s}}}} function Get-3LvlLy--1 ($s,$1,$2){if(Is-Rmk $s) {"",$l1,$l2} if(is-l1Only $s) {"",$s,""} if(is-l2Only $s) {"",$l1,$s} if(is-l3Only $s) {($l1,$l2 -join " "),$l1,$l2} throw "impossible for a validated line to be not one of (rmk l1 l2 l3)`r`nLine:[$s]" } function Get-LinTyErMsg($s) { $o = .{ switch(Get-LinTy $s) { [LinTy]::Er_Impossible { return "Impossible" } [LinTy]::Er_L2_ShouldHave_1or2_Term { return "L2 should have 1 or 2 Terms" } [LinTy]::Er_Impossible { return "Impossible" } [LinTy]::Er_Impossible { return "Impossible" } [LinTy]::Er_Impossible { return "Impossible" } [LinTy]::Er_Impossible { return "Impossible" } [LinTy]::Er_Impossible { return "Impossible" } } } if(Is-Empty $o) {$o} else {' -- ' + $o} } function Get-LinTy ($s) { if(Is-Rmk $s) {return [LinTy]::Rmk} $s = Rmv-2DashRmk (Trim $s) $n = (Get-3Term $s).Count if($n -eq 0) {return [LinTy]::BlankLin} if(Is-L1 $s) { switch($n) {1 {return [LinTy]::L1}; 2 {return [LinTy]::L12}; 3 {return [LinTy]::L123}; default{return [LinTy]::Er_L1_ShouldHave_1or2or3_Term} }} if(Is-L2 $s) { switch($n) {1 {return [LinTy]::L2}; 2 {return [LinTy]::L23}; default{return [LinTy]::Er_L2_ShouldHave_1or2_Term} }} if(Is-L3 $s) { switch($n) {1 {return [LinTy]::L3}; default{return [LinTy]::Er_L3_ShouldHave_1_Term} }} return [LinTy]::Er_Lin_ShouldBe_OneOf_L1_L2_L3_Rmk_Blank } function Is-L3 ($s) { (Is-Pfx $s (Spc 8)) -and ((Mid $s 9 1) -ne " ")} function Is-L2 ($s) { (Is-Pfx $s (Spc 4)) -and ((Mid $s 5 1) -ne " ")} function Is-L1 ($s) { (-not(Is-Rmk $s)) -and ((FstChr $s) -ne " ")} function Get-IsEr ($ly) {$ly | foreach { if(Has-3Dash $_) {return $true} } } function Eval-Value ($v,$hash){ $fnd,$val } |