-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCType.ps1
More file actions
1103 lines (1020 loc) · 34.2 KB
/
Copy pathCType.ps1
File metadata and controls
1103 lines (1020 loc) · 34.2 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# CODEWORK Need better parameter validation
[Hashtable]$CType_AddedTypes = @{}
Write-Verbose 'CType.ps1: adding CType base types'
Add-Type -ReferencedAssemblies System.Data -TypeDefinition @'
namespace CType
{
public class TypeElement
{
}
public class Property : TypeElement
{
public string PropertyName;
public string PropertyType;
public string PropertyTableWidth;
public string[] PropertyKeywords;
}
public class Parent : TypeElement
{
public string ParentTypeName;
}
public class Using : TypeElement
{
public string Namespace;
}
public class ReferencedAssembly : TypeElement
{
public string AssemblyName;
}
public class SqlWrapper : TypeElement
{
}
public class SqlTemplateObject : TypeElement
{
public System.Data.DataRow DataRow;
}
public class NoFormatData : TypeElement
{
}
}
'@
function Add-CType
{
<#
.SYNOPSIS
Add a locally-defined type to this runspace.
.DESCRIPTION
Add a locally-defined type to this runspace, and optionally define table formatting for the type.
These types can be used as parameter types and in OutputType([typename]) declarations.
.PARAMETER TypeName
Name for the new type. You can include the namespace here or else specify it separately.
.PARAMETER Namespace
Namespace containing the new type. If not specified, the namespace is extracted from TypeName.
.PARAMETER ParentTypeName
Parent class of the new class, default is System.Object
.PARAMETER Using
Other namespaces, typically those containing types specified in PropertyType
.PARAMETER ReferencedAssemblies
Other assemblies, typically those containing types specified in PropertyType
.PARAMETER SqlWrapper
Make the new type a SQL wrapper class for use in ConvertTo-CTypeSqlWrapper.
This is set automatically if any PropertyKeyword contains SQLWrapper,
or if SQLTemplateObject is set.
.PARAMETER SqlTemplateObject
If specified, include all properties of this SQL template object in the SQL wrapper class.
Properties already specified in PropertyName are not affected.
SqlWrapper is automatically set if this is specified.
.PARAMETER NoFormatData
Skip defining formatting metadata for this type.
For example, you might want to combine all the formatting for the types in your module
into a single file.
.PARAMETER PropertyName
Name of a property of the type being defined
.PARAMETER PropertyType
Type of a property of the type being defined
.PARAMETER PropertyTableWidth
Table width in characters of a property of the type being defined.
If one or more properties specify PropertyTableWidth, a formatting directive
will be generated for this type and entered into the runspace.
If not specified, this property is not in the default table format.
This can also be set to '*', in which case this property is displayed at full width;
this should generally only be used for the last property with defined PropertyTableWidth.
.PARAMETER PropertyKeywords
Other keywords. The only keywords currently implemented are "Trim"
which trims leading/trailing whitespace from the output string in the table formatting,
and "SQLWrapper" which designates a read-only property read from the wrapped SQL object.
.EXAMPLE
# Define a new type
PS> @"
PropertyName,PropertyType,PropertyTableWidth,PropertyKeywords
PrimaryTarget,string,25,Trim
SecondaryTarget,string,25,Trim
Weight,int,10
IsPrimary,Boolean,10
Description,String,*
MainConnection,SqlConnection
SecondaryConnection,SqlConnection
"@ | ConvertFrom-Csv | Add-CType -TypeName MyType `
-Namespace MyCompany.Namespace `
-Using System.Data.SqlClient `
-ReferencedAssemblies System.Data
# You can now use this as a full-fledged type, for example define parameters as
# param( [MyCompany.Namespace.MyType]$MyParameter )
# Create an instance of the type
PS> New-Object -TypeName MyCompany.Namespace.MyType -Property @{
PrimaryTarget = "String1";
SecondaryTarget = "String2";
Weight = 4;
Description = "This is a very long description"
}
PrimaryTarget SecondaryTarget Weight IsPrimary Description
------------- --------------- ------ --------- -----------
String1 String2 4 False This is a very long description
.EXAMPLE
# Define a new CType Class
ConvertFrom-Csv @'
PropertyType,PropertyName,PropertyTableWidth,PropertyKeywords
string,Name,20
string,HostName,20
DateTime,WhenCreated,25
DateTime,WhenDeleted,25
'@ | Add-CType -TypeName MyCompany.MyNamespace.MyClass
# Create wrapped object
New-Object -Type MyCompany.MyNamespace.MyClass -Property @{
Name = "MyName"
WhenCreated = (get-date)
}
.EXAMPLE
# Define a new SQL Wrapper Class
ConvertFrom-Csv @'
PropertyType,PropertyName,PropertyTableWidth,PropertyKeywords
string,Name,20,SQLWrapper
string,HostName,20,SQLWrapper
DateTime,WhenCreated,25,SQLWrapper
DateTime,WhenDeleted,25,SQLWrapper
'@ | Add-CType -TypeName MyCompany.MyNamespace.MyClass -SqlWrapper
# Create wrapped objects
$SqlWrappedObjects = $SqlDataRows | ConvertTo-CTypeSqlWrapper -TypeName MyCompany.MyNamespace.MyClass
.NOTES
This method should only be called once for any class. Calling a second time
is generally harmless as long as the type definition is identical,
otherwise an error will be reported. Types cannot be altered once added to
a process (AppDomain?).
.LINK
ConvertTo-CTypeSqlWrapper
Add-Type
Update-CTypeFormatData
Update-FormatData
#>
[CmdletBinding()]
param(
[string][parameter(Mandatory=$true)]$TypeName,
[string]$Namespace,
[string]$ParentTypeName,
[string[]]$Using,
[string[]]$ReferencedAssemblies,
[switch]$SqlWrapper,
[System.Data.DataRow]$SqlTemplateObject,
[switch]$NoFormatData,
[string][parameter(ValueFromPipelineByPropertyName=$true)]$PropertyName,
[string][parameter(ValueFromPipelineByPropertyName=$true)]$PropertyType,
[string][parameter(ValueFromPipelineByPropertyName=$true)]$PropertyTableWidth,
[string[]][parameter(ValueFromPipelineByPropertyName=$true)]$PropertyKeywords
)
begin
{
Write-Verbose -Message @"
$($MyInvocation.InvocationName): TypeName $TypeName
$($MyInvocation.InvocationName): Namespace $Namespace
$($MyInvocation.InvocationName): ParentTypeName $ParentTypeName
$($MyInvocation.InvocationName): Using $Using
$($MyInvocation.InvocationName): ReferencedAssemblies $ReferencedAssemblies
$($MyInvocation.InvocationName): SqlWrapper $SqlWrapper
$($MyInvocation.InvocationName): SqlTemplateObject $SqlTemplateObject
"@
if (-not $Namespace)
{
$nameComponents = $TypeName.Split('.')
if ($nameComponents.Count -lt 2)
{
throw 'You must specify a Namespace or else a TypeName which contains a Namespace'
}
$TypeName = $nameComponents[-1]
$Namespace = $nameComponents[0..($nameComponents.Count - 2)] -join '.'
}
if ($CType_AddedTypes["$Namespace.$TypeName"])
{
Write-Warning "$($MyInvocation.InvocationName): Class $Namespace.$TypeName was already added; trying anyhow"
}
if ($SqlTemplateObject)
{
$SqlWrapper = $true
}
$propertyList = @()
}
process
{
if ($PropertyName)
{
Write-Verbose -Message @"
$($MyInvocation.InvocationName): PropertyName $PropertyName
$($MyInvocation.InvocationName): PropertyType $PropertyType
$($MyInvocation.InvocationName): PropertyTableWidth $PropertyTableWidth
$($MyInvocation.InvocationName): PropertyKeywords $PropertyKeywords
"@
if (-not $PropertyType)
{
throw 'PropertyType must be specified for all properties'
}
if ($PropertyTableWidth)
{
if ($PropertyTableWidth -ne '*')
{
$w = $PropertyTableWidth -as [int]
if ($w -lt 1)
{
throw "PropertyTableWidth must be either a positive integer or '*' if it is specified"
}
}
}
$propertyList += New-Object -TypeName CType.Property -Property @{
PropertyName = $PropertyName
PropertyType = $PropertyType
PropertyTableWidth = $PropertyTableWidth
PropertyKeywords = $PropertyKeywords
}
if ('SQLWrapper' -in $PropertyKeywords)
{
$SqlWrapper = $true
}
}
}
end
{
if ($SqlTemplateObject)
{
Write-Verbose "$($MyInvocation.InvocationName): SqlTemplateObject specified, adding additional properties"
foreach ($column in $SqlTemplateObject.Table.Columns)
{
$name = $column.ColumnName
$type = $column.DataType
if ($name -in $propertyList.PropertyName)
{
Write-Verbose "$($MyInvocation.InvocationName): SqlTemplateObject column $type $name already in property list"
}
else
{
Write-Verbose "$($MyInvocation.InvocationName): SqlTemplateObject column $type $name must be added property list"
$propertyList += New-Object -TypeName CType.Property -Property @{
PropertyName = $name;
PropertyType = $type;
PropertyTableWidth = 0;
PropertyKeywords = @('SQLWrapper');
}
}
}
$SqlWrapper = $true
}
if ($SqlWrapper)
{
if ($Using -notcontains 'System.Data')
{
$Using += 'System.Data'
}
if ($ReferencedAssemblies -notcontains 'System.Data')
{
$ReferencedAssemblies += 'System.Data'
}
}
Write-Verbose "$($MyInvocation.InvocationName): building C# type definition"
$typeDefinition = New-Object -TypeName System.Text.StringBuilder
$Using = @("System") + ($Using | Where-Object {$_})
$Using | % {
$null = $typeDefinition.AppendLine("using $_;")
}
$null = $typeDefinition.AppendLine(@"
namespace $Namespace
{
public class $TypeName $(if ($ParentTypeName) {": $ParentTypeName"})
{
"@)
if ($SqlWrapper)
{
$null = $typeDefinition.AppendLine(@"
private System.Data.DataRow WrappedSqlObject;
public $TypeName(System.Data.DataRow obj)
{
this.WrappedSqlObject = obj;
}
"@)
}
foreach ($property in $propertyList)
{
$name = $property.PropertyName
$type = $property.PropertyType
Write-Verbose "$($MyInvocation.InvocationName) Property $type $name"
# CSharp seems to be picky about spelling and capitalization of these types
$typeObject = $type -as [Type]
if ($typeObject.FullName)
{
$type = $typeObject.FullName
}
$propertyType = $type
if ('SqlWrapper' -in $property.PropertyKeywords)
{
$valueExpression = "($type)val";
if ($typeObject.IsValueType)
{
$propertytype = "Nullable<$type>"
}
$null = $typeDefinition.AppendLine(@"
public $propertyType $name
{
get {
object val = this.WrappedSqlObject["$name"];
if ((val == null) || (val.GetType().FullName == "System.DBNull"))
{
return null;
}
return $valueExpression;
} // get $propertyType $name
} // property $propertyType $name
"@)
}
else # -not $SqlClassWrapper
{
$null = $typeDefinition.AppendLine(@"
public $propertyType $name { get; set; }
"@)
}
}
$null = $typeDefinition.AppendLine(@"
}
}
"@)
Write-Verbose "$($MyInvocation.InvocationName): Adding type:`n$($typeDefinition.ToString())"
Add-Type -TypeDefinition $typeDefinition.ToString() -ReferencedAssemblies $ReferencedAssemblies
$CType_AddedTypes["$Namespace.$classname"] = $true
if (-not $NoFormatData)
{
$tableProperties = $propertyList | Where-Object {$_.PropertyTableWidth}
if ($tableProperties)
{
$text = $tableProperties | Get-CTypeFormatPS1XML -TypeName "$Namespace.$TypeName"
$text | Update-CTypeFormatData -TypeName "$Namespace.$TypeName"
}
}
}
}
function Update-CTypeFormatData
{
<#
.SYNOPSIS
Add the specified formatting metadata to the current runspace.
.PARAMETER TypeName
Full typename for the type including namespace
.PARAMETER FormatDataString
Format data string from Get-CTypeFormatPS1XML
.EXAMPLE
$tableProperties = ConvertFrom-Csv @'
PropertyType,PropertyName,PropertyTableWidth
string,Name,20
string,HostName,20
DateTime,WhenCreated,25
DateTime,WhenDeleted,25
'@
$text = $tableProperties | Get-CTypeFormatPS1XML -TypeName "$Namespace.$TypeName"
Update-CTypeFormatData -TypeName "$Namespace.$TypeName" -FormatDataString $text
.LINK
Get-CTypeFormatPS1XML
Add-CType
Update-FormatData
#>
[CmdletBinding()]
param(
[string][parameter(Mandatory=$true,Position=0)]$TypeName,
[string][parameter(Mandatory=$true, ValueFromPipeline=$true)]$FormatDataString
)
begin
{
$activity = "$($MyInvocation.InvocationName) $TypeName"
$tempfile = Join-Path $env:temp "$TypeName.$(get-date -Format 'yyyyMMdd-hhmmss-fffffff').format.ps1xml"
[System.Text.StringBuilder]$text = New-Object -TypeName System.Text.StringBuilder
}
process
{
$null = $text.AppendLine($FormatDataString)
}
end
{
Write-Verbose "$activity`: Adding format data to temporary file $tempfile from formatting directive:`n$FormatDataString"
$FormatDataString | Set-Content -Path $tempFile
try
{
Write-Verbose "$activity`: Updating format data"
Update-FormatData -PrependPath $tempfile
}
catch
{
if (Test-Path $tempfile -ErrorAction SilentlyContinue)
{
Write-Verbose "$activity`: Deleting temporary file $tempfile"
Remove-Item $tempfile
}
}
}
}
function Get-CTypeFormatPS1XML
{
<#
.SYNOPSIS
Returns the text of a PS1XML which defines output formatting for a type.
.DESCRIPTION
Returns the text of a PS1XML which defines output formatting for a type.
Pass this to Update-CTypeFormatData to apply this formatting to the current session.
You can also save this into a PS1XML file associated with your module.
.PARAMETER TypeName
Name for the type
.PARAMETER PropertyName
Name of a property of the type being defined
.PARAMETER PropertyType
Type of a property of the type being defined
.PARAMETER PropertyTableWidth
Table width of a property of the type being defined, per Add-CType.
.PARAMETER PropertyKeywords
Other keywords per Add-CType.
.LINK
Add-CType
Update-CTypeFormatData
#>
[OutputType([string])]
[CmdletBinding()]
param(
[string][parameter(Mandatory=$true,Position=0)]$TypeName,
[string][parameter(ValueFromPipelineByPropertyName=$true)]$PropertyName,
[string][parameter(ValueFromPipelineByPropertyName=$true)]$PropertyType,
[string][parameter(ValueFromPipelineByPropertyName=$true)]$PropertyTableWidth,
[string[]][parameter(ValueFromPipelineByPropertyName=$true)]$PropertyKeywords
)
begin
{
$activity = "$($MyInvocation.InvocationName) $TypeName"
Write-Verbose $activity
# You could also build this using the [xml] class
$fileHeader = @"
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
"@
$templateViewHeader = @"
<View>
<Name>{0}</Name>
<ViewSelectedBy>
<TypeName>{0}</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
"@
$templateColumnHeader = @"
<TableColumnHeader>
<Label>{0}</Label>
</TableColumnHeader>
"@
$templateColumnHeaderWithWidth = @"
<TableColumnHeader>
<Label>{0}</Label>
<Width>{1}</Width>
</TableColumnHeader>
"@
$headerItemSeparator = @"
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
"@
$templateColumnItem = @"
<TableColumnItem>
<PropertyName>{0}</PropertyName>
</TableColumnItem>
"@
$templateColumnItemWithTrim = @"
<TableColumnItem>
<ScriptBlock>
(Out-String -InputObject `$_.{0}).Trim()
</ScriptBlock>
</TableColumnItem>
"@
$viewFooter = @"
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
"@
$fileFooter = @"
</ViewDefinitions>
</Configuration>
"@
$propertyList = New-Object System.Collections.ArrayList
$fileContent = New-Object System.Text.StringBuilder -ArgumentList $fileHeader
$null = $fileContent.AppendLine( ($templateViewHeader -f $TypeName) )
}
process
{
Write-Verbose -Message @"
$activity`: PropertyName $PropertyName
$activity`: PropertyType $PropertyType
$activity`: PropertyTableWidth $PropertyTableWidth
$activity`: PropertyKeywords $PropertyKeywords
"@
if ($PropertyName -and $PropertyTableWidth)
{
if (-not $PropertyType)
{
throw 'PropertyType must be specified for all properties'
}
if ($PropertyTableWidth)
{
if ($PropertyTableWidth -ne '*')
{
$w = $PropertyTableWidth -as [int]
if ($w -lt 1)
{
throw "PropertyTableWidth must be either a positive integer or '*' if it is specified"
}
}
}
$obj = New-Object -TypeName CType.Property -Property @{
PropertyName = $PropertyName
PropertyType = $PropertyType
PropertyTableWidth = $PropertyTableWidth
PropertyKeywords = $PropertyKeywords
}
$null = $propertyList.Add($obj)
}
else
{
Write-Verbose "$activity`: Skipping due to null name or width"
}
}
end
{
foreach ($p in $propertyList)
{
if ($p.PropertyTableWidth)
{
if ($p.PropertyTableWidth -eq '*')
{
$null = $fileContent.AppendLine( ($templateColumnHeader -f $p.PropertyName) )
}
else
{
$null = $fileContent.AppendLine( ($templateColumnHeaderWithWidth -f $p.PropertyName,$p.PropertyTableWidth) )
}
}
}
$null = $fileContent.AppendLine( $headerItemSeparator )
foreach ($p in $propertyList)
{
if ($p.PropertyTableWidth)
{
if ('Trim' -in $p.PropertyKeywords)
{
$null = $fileContent.AppendLine( ($templateColumnItemWithTrim -f $p.PropertyName) )
}
else
{
$null = $fileContent.AppendLine( ($templateColumnItem -f $p.PropertyName) )
}
}
}
$null = $fileContent.AppendLine( $viewFooter )
$null = $fileContent.AppendLine( $fileFooter )
Write-Output $fileContent.ToString()
}
}
function ConvertTo-CTypeSqlWrapper
{
<#
.SYNOPSIS
Wraps a SQL row in a wrapper object.
.DESCRIPTION
Wraps a SQL row in a wrapper object. The wrapper class must first be defined by
Add-CType -SqlWrapper. The wrapped object has several advantages over the
raw System.Data.DataRow object:
(1) Property value System.DBNull is automatically converted to $null so that it is boolean-false in scripting
(2) Type can be use in [OutputType([typename])] attributes
(3) Type can be assigned formatting directives and other PowerShell type decorations
(4) All properties are read-only
(5) Hides unneeded properties of base SQL object
.PARAMETER InputObject
System.Data.DataRow object to be wrapped
.PARAMETER TypeName
Name of wrapper class, should be the same as for Add-CType
.PARAMETER AddCType
If specified, the type will be created automatically
the first time an input object of this type is received,
where the first object is a template.
.PARAMETER AddCTypeScriptBlock
If specified, the type will be created automatically
the first time an input object of this type is received,
where the first object is a template and this script block
defines additional properties of the type. This implies -AddCType.
Note the limitations in Notes below.
.EXAMPLE
$connection = New-Object System.Data.SqlClient.SqlConnection $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand $commandString,$connection
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$null = $adapter.Fill($dataSet)
$result = $tables[0].Rows
$wrappedResult = $result | ConvertTo-CTypeSqlWrapper -TypeName MyCompany.MyNamespace.MyWrapperClass
.NOTES
Note that the type will be defined based on the first InputObject encountered.
Even if subsequent objects have a different property list, the type cannot be changed.
Note that you can define a type using AddCTypeScriptBlock, but because that type
cannot be defined until the first template object is created, you may have difficulty
using the type in an OutputType([typename]) block or a parameter definition.
Consider generating the type definition with ConvertTo-CTypeDefinition
and pasting the CType definition directly into your code.
.LINK
Add-CType
CType
ConvertTo-CTypeSqlDefinition
#>
[CmdletBinding()]
param(
[parameter(ValueFromPipeline=$true)][System.Data.DataRow]$InputObject,
[parameter(Mandatory=$true,Position=0)][string]$TypeName,
[switch]$AddCType,
[ScriptBlock[]][parameter(Position=1)]$AddCTypeScriptBlock
)
process
{
if ($AddCType -or $AddCTypeScriptBlock)
{
if (-not $CType_AddedTypes[$TypeName])
{
CType -TypeName $TypeName -TypeElement $AddCTypeScriptBlock,(SqlTemplateObject $InputObject)
}
}
New-Object -TypeName $TypeName -ArgumentList $InputObject
}
}
function CType
{
<#
.SYNOPSIS
Create a new type
.PARAMETER TypeName
Name of the new type. Include namespace e.g. "MyCompany.MyNamespace.MyClassName"
.PARAMETER TypeElement
One or more script blocks or type elements which define class properties and other elements of the class.
The type elements may only contain objects on the list
property, sqlproperty, parent, use, ref,SqlWrapper, SqlTemplateObject, and NoFormatData,
and the script blocks must evaluate to objects of those types.
The script block definition must begin on the same line as the CType invocation,
or must be linked with backtick.
.EXAMPLE
# Creates a type with 4 properties, three of which are displayed in the default formatter.
CType MyCompany.MyNamespace.MyClassName {
parent MyCompany.MyNamespace.MyParentClass
property string PropertyName1 -PropertyTableWidth 20 -PropertyKeywords trim
if ($true)
{
property int PropertyName2 -PropertyTableWidth 15
}
property string PropertyName3 -PropertyTableWidth *
property bool PropertyName4
}
.EXAMPLE
# Creates a type with 4 properties, three SQL wrappers and one non-wrapped property.
# The non-wrapped property can be set like any other read/write property, the others
# always read from the equivalent property of the wrapped DataRow object.
CType MyCompany.MyNamespace.MyClassName {
parent MyCompany.MyNamespace.MyParentClass
sqlproperty string PropertyName1 -PropertyTableWidth 20 -PropertyKeywords trim
sqlproperty int PropertyName2 -PropertyTableWidth 15
property string PropertyName3 -PropertyTableWidth *
sqlproperty bool PropertyName4
}
.NOTES
A type with a given name can only be defined once per PowerShell runspace.
If you need to change it you will need to exit the runspace where it is defined.
.LINK
Add-CType
#>
[CmdletBinding()]
param(
[string][parameter(Mandatory=$true,Position=0)]$TypeName,
[parameter(Position=1)]$TypeElement
)
$typeElements = foreach ($element in $TypeElement)
{
if ($element)
{
if ($element -is [ScriptBlock])
{
Invoke-Command -ScriptBlock $element
}
elseif ($element -is [ScriptBlock[]])
{
$element | % {Invoke-Command -ScriptBlock $_}
}
else
{
$element
}
}
}
foreach ($element in $typeElements)
{
if (-not ($element -is [CType.TypeElement]))
{
throw "$($MyInvocation.InvocationName): Invalid TypeElement $($element.GetType().FullName): TypeElement may only contain property, parent, use, ref,SqlWrapper, SqlTemplateObject, and NoFormatData, or script blocks generate them"
}
}
[CType.Property[]]$properties = $typeElements | Where-Object {$_ -is [CType.Property]}
[string[]]$parentTypes = $typeElements | Where-Object {$_ -is [CType.Parent]} | Select-Object -ExpandProperty ParentTypeName
[string[]]$using = $typeElements | Where-Object {$_ -is [CType.Using]} | Select-Object -ExpandProperty Namespace
[string[]]$referencedAssemblies = $typeElements | Where-Object {$_ -is [CType.ReferencedAssembly]} | Select-Object -ExpandProperty AssemblyName
[System.Data.DataRow]$sqlTemplateObject = $null
[System.Data.DataRow[]]$sqlTemplateObjects = @($typeElements | Where-Object {$_ -is [CType.SqlTemplateObject]} | Select-Object -ExpandProperty DataRow)
if ($parentTypes)
{
if ($parentTypes.Count -gt 1)
{
throw 'You may only specify one parent type'
}
$parentType = $parentTypes[0]
}
else
{
$parentType = $null
}
if ($sqlTemplateObjects)
{
if ($sqlTemplateObjects.Count -gt 1)
{
throw 'You may only specify one SqlTemplateObject'
}
$sqlTemplateObject = $sqlTemplateObjects[0]
}
else
{
$sqlTemplateObject = $null
}
[bool]$noFormatData = [bool]($typeElements | Where-Object {$_ -is [CType.NoFormatData]} )
[bool]$sqlWrapper = [bool]($typeElements | Where-Object {$_ -is [CType.SqlWrapper]} )
$split = $TypeName.Split('.')
if ($split.Count -lt 2)
{
throw 'You must specify full classname including namespace'
}
$classname = $split[-1]
$namespace = $split[0..($split.Count-2)] -join '.'
$properties | Add-CType -TypeName $TypeName `
-ParentTypeName $parentType `
-Using $using `
-ReferencedAssemblies $referencedAssemblies `
-SqlWrapper:$sqlWrapper `
-SqlTemplateObject $SqlTemplateObject `
-NoFormatData:$NoFormatData
}
function property
{
<#
.SYNOPSIS
Define a property for use in CType
#>
[CmdletBinding()]
[OutputType([CType.Property])]
param(
[string][parameter(Mandatory=$true,Position=0)]$PropertyType,
[string][parameter(Mandatory=$true,Position=1)]$PropertyName,
[string]$width,
[switch]$trim
)
$propertyHash = @{
PropertyType=$PropertyType
PropertyName=$PropertyName
}
if ($width)
{
$propertyHash['PropertyTableWidth'] = $width
}
if ($trim)
{
$propertyHash['PropertyKeywords'] = "Trim"
}
New-Object -TypeName CType.Property -Property $propertyHash
}
function sqlproperty
{
<#
.SYNOPSIS
Define a SQL wrapper property for use in CType
#>
[CmdletBinding()]
[OutputType([CType.Property])]
param(
[string][parameter(Mandatory=$true,Position=0)]$PropertyType,
[string][parameter(Mandatory=$true,Position=1)]$PropertyName,
[string]$width,
[switch]$trim
)
$propertyHash = @{
PropertyType=$PropertyType
PropertyName=$PropertyName
}
if ($width)
{
$propertyHash['PropertyTableWidth'] = $width
}
if ($trim)
{
$propertyHash['PropertyKeywords'] = ('SQLWrapper','Trim')
}
else
{
$propertyHash['PropertyKeywords'] = 'SQLWrapper'
}
New-Object -TypeName CType.Property -Property $propertyHash
}
function parent
{
<#
.SYNOPSIS
Specify a parent class for use in CType
#>
[CmdletBinding()]
[OutputType([CType.Parent])]
param(
[string][parameter(Mandatory=$true,Position=0)]$ParentTypeName
)
New-Object -TypeName CType.ParentType -Property @{
ParentTypeName=$ParentTypeName
}
}
function use
{
<#
.SYNOPSIS
Specify a Using reference for use in CType
#>
[CmdletBinding()]
[OutputType([CType.Using])]
param(
[string][parameter(Mandatory=$true,Position=0)]$Namespace
)
New-Object -TypeName CType.Using -Property @{
Namespace=$Namespace
}
}
function ref
{
<#
.SYNOPSIS
Specify a Referenced Assembly for use in CType
#>
[CmdletBinding()]
[OutputType([CType.ReferencedAssembly])]
param(
[string][parameter(Mandatory=$true,Position=0)]$AssemblyName
)
New-Object -TypeName CType.ReferencedAssembly -Property @{
AssemblyName=$AssemblyName
}
}
function SqlWrapper
{
<#
.SYNOPSIS
Specify that a CType should be a SQL Wrapper
.LINK
ConvertTo-CTypeSqlWrapper
#>
[CmdletBinding()]
[OutputType([CType.SqlWrapper])]
param(
)
New-Object -TypeName CType.SqlWrapper
}
function SqlTemplateObject
{
<#
.SYNOPSIS
Specify a SQL template object for use in CType
#>
[CmdletBinding()]
[OutputType([CType.ReferencedAssembly])]
param(
[System.Data.DataRow][parameter(Mandatory=$true,Position=0)]$SqlTemplateObject
)
New-Object -TypeName CType.SqlTemplateObject -Property @{
DataRow=$SqlTemplateObject
}
}
function NoFormatData
{
<#
.SYNOPSIS
Specify that a CType should not generate formatting data
.DESCRIPTION
Specify that a CType should not generate formatting data.
For example, you might perfer to generate the formatting data using
Get-CTypeFormatPS1XML and enter this into the PS1XML for your module.
#>
[CmdletBinding()]
[OutputType([CType.NoFormatData])]
param(
)
New-Object -TypeName CType.NoFormatData
}
function ConvertTo-CTypeDefinition
{
<#
.SYNOPSIS
Generate a string representing a CType definition for the specified SQL object
.DESCRIPTION