-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPSIS.psm1
305 lines (268 loc) · 8.86 KB
/
PSIS.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
function Generate-StarSchemaPopulator([string]$cs,[string]$src,[string]$dst,$shortname)
{
$csb = New-Object Data.SqlClient.SqlConnectionStringBuilder -ArgumentList $cs
$null = sqlmetal /conn:""$cs"" /views /code:"GeneratedETL\$($csb.InitialCatalog).cs" /namespace:PSIS
$strings =InternalGenerate-StarSchemaPopulator $cs $src $dst $shortname
$strings | Out-File -FilePath $PSScriptRoot\GeneratedETL\$shortname.cs
$files = ls $PSScriptRoot\GeneratedETL\*.cs
$files += ls $PSScriptRoot\CSSupport\Runtime\*.cs
$code2 = [string]::Join("`n", ( $files | % { Get-Content $_} ))
Add-Type -TypeDefinition $code2 -Language CSharpVersion3 -ReferencedAssemblies "C:\Program Files (x86)\Microsoft Reactive Extensions\Redist\DesktopV2\System.Threading.dll",System.Data.Linq,System.Data,System.Xml
}
function InternalGenerate-StarSchemaPopulator([string]$cs,[string]$src,[string]$dst,$shortname)
{
$m = New-Object PSIS.StarSchemaModel -ArgumentList $cs,$src,$dst
"namespace PSIS
{
using System.Collections.Generic;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Data.SqlClient;
public static class PSS
{
public static Dictionary<string, long> Populate$($shortname) ()
{
var cs = ""$cs"";
Func<DestSQLDatabase> CreateDC = () => new DestSQLDatabase(cs);
"
foreach ($dcm in $m.DimColMap)
{
" var new$($dcm.Name)Items = new List<$($dcm.TableNameEntity)>();"
}
foreach ($dcm in $m.DimColMap)
{
" var $($dcm.Name)KeyTask =Task.Factory.StartNew( SSRuntime.Using( CreateDC,
(dc) => dc.$($dcm.TableNameEntity).Max(d=> (int?) d.$($dcm.KeyName)).GetValueOrDefault(1) ));"
}
"`n"
foreach ($dcm in $m.DimColMap)
{
$AKcols = $dcm.AK | % { "d.$($_)" }
$AKcolsstr = [string]::Join(", ",$AKcols)
" var $($dcm.Name)DictTask = Task.Factory.StartNew(SSRuntime.Using( CreateDC,
(dc) => SSRuntime.TryCatchThrow(
() => dc.$($dcm.TableNameEntity).ToDictionary(d => new { $AKcolsstr }),
(ArgumentException ex) => Console.WriteLine(""Failed to create lookup for $($dcm.TableNameDB)"",ex))));"
}
"`n"
foreach ($dcm in $m.DimColMap)
{
" var $($dcm.Name)Key = $($dcm.Name)KeyTask.Result;"
}
"`n"
foreach ($dcm in $m.DimColMap)
{
" $($dcm.Name)KeyTask.Dispose(); $($dcm.Name)KeyTask=null;"
}
foreach ($dcm in $m.DimColMap)
{
$AKcols = $dcm.AK | % { "$_ = frow.$($dcm.Name)_$($_)" }
$AKcolsstr = [string]::Join(", ",$AKcols)
" Func<$($m.SourceViewEntity),int> Get$($dcm.Name)Key = (frow) =>
{
$($dcm.TableNameEntity) dim;
var lookup = new { $AKcolsstr };
var dict = $($dcm.Name)DictTask.Result;
lock(new$($dcm.Name)Items)
if (!dict.TryGetValue(lookup,out dim))
{
dim = new $($dcm.TableNameEntity)() { $AKcolsstr } ;
dim.$($dcm.KeyName) = Interlocked.Increment(ref $($dcm.Name)Key);
dict.Add(lookup,dim);
new$($dcm.Name)Items.Add(dim);
}
return dim.$($dcm.KeyName);
};"
}
"
var restbl = new Dictionary<string, long>();
List<Task> tasks = new List<Task>();
using (var dc = CreateDC())
{
var newFacts = dc.$($m.SourceViewEntity)
.AsParallel()
.Select(src =>
{
var fact = new $($m.FactTableNameEntity)();"
foreach ($fm in $m.FactMapping)
{
" fact.{0} = src.{1};" -f $fm.DestinationName,$fm.SourceName
}
foreach ($dcm in $m.DimColMap)
{
" fact.$($dcm.KeyName) = Get$($dcm.Name)Key(src);"
}
"
return fact;
});
restbl[""$dst""] = PSIS.SSRuntime.SubmitChangesInBulk(newFacts, cs, ""$dst"",SqlBulkCopyOptions.Default,15*60);
}"
foreach ($dcm in $m.DimColMap)
{
" if (new$($dcm.Name)Items.Any())
{
Task t = Task.Factory.StartNew(() => PSIS.SSRuntime.SubmitChangesInBulk(new$($dcm.Name)Items, cs, ""$($dcm.TableNameDB)"",SqlBulkCopyOptions.KeepIdentity,15*60));
tasks.Add(t);
restbl[""$($dcm.TableNameDB)""] = new$($dcm.Name)Items.Count;
}
"
}
" Task.WaitAll(tasks.ToArray());
return restbl;
}
}
}
"
}
function qw { $args }
filter coalesce( [scriptblock] $predicate={ $_ } )
{
begin
{
$script:found = $false;
$script:item = $null;
}
process
{
if( ! $script:found -and ( &$predicate ) )
{
$script:found = $true;
$script:item = $_;
}
}
end
{
return $script:item;
}
}
set-alias ?: Invoke-Ternary -Option AllScope -Description "PSCX filter alias"
filter Invoke-Ternary ([scriptblock]$decider, [scriptblock]$ifTrue, [scriptblock]$ifFalse)
{
if (&$decider) {
&$ifTrue
} else {
&$ifFalse
}
}
function Get-TempFileName([string] $ext)
{
if (-not $ext)
{
return [IO.Path]::GetTempFileName()
}
for ($ii=0;$ii -lt 50;$ii++)
{
$origTemp = [IO.Path]::GetTempFileName()
$rename = [IO.Path]::Combine([IO.Path]::GetTempPath(), ([IO.Path]::GetFileNameWithoutExtension($origTemp) + "." + ( $ext -replace "^\.","")))
if (-not (Test-Path -PathType Leaf $rename))
{
[IO.File]::Move($origTemp,$rename)
return $rename
}
}
throw "unable to get temp file with requested extention"
}
## From Windows PowerShell Cookbook (O'Reilly) by Lee Holmes (http://www.leeholmes.com/guide)
function Invoke-SqlCommand(
[string] $dataSource = "(local)",
[string] $database = $(throw "Please specify a database"),
[string] $sqlCommand = $(throw "Please specify a query."),
[System.Management.Automation.PsCredential] $credential)
{
$authentication = "Integrated Security=SSPI;"
if($credential)
{
$plainCred = $credential.GetNetworkCredential()
$authentication = ("uid={0};pwd={1};" -f $plainCred.Username,$plainCred.Password)
}
$connectionString = "Provider=sqloledb;Data Source=$dataSource;Initial Catalog=$database;$authentication;"
if($dataSource -match '\.xls$|\.mdb$')
{
$connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dataSource; "
if($dataSource -match '\.xls$')
{
$connectionString += 'Extended Properties="Excel 8.0"; '
if($sqlCommand -notmatch '\[.+\$\]')
{
Write-Error 'Sheet names should be surrounded by square brackets, and have a dollar sign at the end: [Sheet1$]'
return
}
}
}
if($dataSource -match '\.csv$')
{
$path = split-path $dataSource
$connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$path;Extended Properties='text;HDR=NO;FMT=Delimited'"
$sqlCommand = "select * from " + (split-path -leaf $dataSource)
}
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand,$connection
$command.CommandTimeout = 300
$connection.Open()
## Fetch the results, and close the connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()
## Return all of the rows from their query
$dataSet.Tables | Select-Object -Expand Rows
}
function Invoke-MSSqlCommand([string] $ConnectionString = "Data Source=(local);Integrated Security=SSPI",[string] $sqlCommand = $(throw "Please specify a query."), [int] $timeout = 60 * 45, [string] $database, [hashtable] $inputparams, [bool] $ExpandResults=$True )
{
if ($database)
{
$connectionString += ";Initial Catalog=$database"
}
$connection = New-Object Data.SqlClient.SqlConnection $connectionString
$command = New-Object Data.SqlClient.SqlCommand $sqlCommand,$connection
if ($timeout -gt -1)
{
$command.CommandTimeout = $timeout
}
else
{
$command.CommandTimeout = 0
}
$regex = New-Object Text.RegularExpressions.Regex -ArgumentList "[ = > < ( , ]@([\w_]+)", "Multiline"
foreach ($match in $regex.Matches($sqlCommand))
{
$pName = $match.Groups[1].Value
if (!$command.Parameters.Contains("@" + $pName))
{
$param = $command.CreateParameter()
$param.ParameterName = "@" + $pName
$param.Value = $inputparams[$pName]
$null = $command.Parameters.Add($param);
}
}
$connection.Open()
## Fetch the results, and close the connection
$adapter = New-Object Data.SqlClient.SqlDataAdapter $command
$dataset = New-Object Data.DataSet
[void] $adapter.Fill($dataSet)
$command.Dispose()
$connection.Close()
if ($ExpandResults)
{
## Return all of the rows from their query
$dataSet.Tables | Select-Object -Expand Rows
}
else
{
$dataSet
}
}
$dll = ls -Recurse -Include PSIS.dll -Path $PSScriptRoot | ? {$_ -notmatch "obj" } | sort -desc LastWriteTime | select -first 1
if ($dll)
{
$shadowDll = Get-TempFileName ".dll"
Copy-Item $dll $shadowDll -Force
add-type -Path $shadowDll -PassThru
Export-ModuleMember -Function Generate-StarSchemaPopulator
}
#$l2Code = [string]::Join("`n", ( Get-Content -ReadCount 0 $PSScriptRoot\GeneratedETL\DestSQLDatabase.cs))
#Add-Type -TypeDefinition $l2Code -Language CSharpVersion3 -ReferencedAssemblies System.Data.Linq,System.Data
Export-ModuleMember -Alias ?: -Function qw,coalesce,Invoke-Ternary,Get-TempFileName,Invoke-SqlCommand,Invoke-MSSqlCommand