-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolsOracleModule.ps1
More file actions
130 lines (95 loc) · 2.68 KB
/
ToolsOracleModule.ps1
File metadata and controls
130 lines (95 loc) · 2.68 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
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\DatabaseModule.ps1"
. "$here\OracleModule.ps1"
function Invoke-OracleSqlPlus($userName, $password, $server, $sql) {
$script = "WHENEVER SQLERROR EXIT SQL.SQLCODE`n" + ((Get-Content $sql) -join "`n")
$script | &sqlplus "$userName/$password@$server"
if(!$?) {
throw "SqlPlus failed!"
}
}
#region SqlLoader
function Invoke-OracleSqlLoader
{
param(
$userName, $password, $server,
[string]$ctlFile
)
$logFile = $ctlFile + '.log'
&sqlldr "userid=$userName/$password@$server control=$ctlFile log=$logFile"
}
function Export-OracleControlFile
{
#http://psoug.org/reference/sqlloader.html
[CmdletBinding()]
param([string[]]$csvFiles,
[char]$delimiter = ',',
[string]$outputPath = 'control.ctl',
[switch]$PassThru,
[string]$intoTableName= '',
[string]$dateTimeFormat = 'YYYYMMDD',
[string]$recordNumberField = 'recno',
[string]$sysdateField = 'rundate',
[hashtable]$constants = @{},
[validateSet(
'APPEND',
'INSERT',
'REPLACE',
'TRUNCATE'
)]
[string]$modes = 'APPEND'
)
$firstCsvFile = $csvFiles[0]
$csvFeed = Import-Csv -Delimiter $delimiter -Path $firstCsvFile
$csvHeaders = $csvFeed | select -first 1 | get-member -type properties | select name
$infileBody = $csvFiles | %{"INFILE `'$_`' BADFILE `'$($_).bad`' DISCARDFILE `'$($_).discard`'"} | out-string
$headerBody = $csvHeaders | %{$_.name +','} | out-string
Write-Verbose "Headers: $headerBody"
#region body
#region additional Fields
$recordNumberBody = ''
if($recordNumberField)
{
$recordNumberBody = $recordNumberField + " RECNUM, `r`n"
}
$sysdateBody = ''
if($sysdateField)
{
$sysdateBody = $sysdateField + " SYSDATE, `r`n"
}
$constantsBody = ''
if($constants)
{
$constants.Keys | %{
$key = $_
$value = $constants[$key]
$constantsBody += "$key CONSTANT `"$value`", `r`n"
}
}
#endregion
$fieldBody = $headerBody + $recordNumberBody + $sysdateBody + $constantsBody
$fieldBody = ($fieldBody.Trim()) -replace ".$"
[string]$ctlBody ="
-- Control file created on $(get-date)
LOAD DATA
$infileBody
$modes
INTO TABLE $intoTableName
TRAILING NULLCOLS
FIELDS TERMINATED BY `'$delimiter`'
DATE FORMAT $dateTimeFormat
(
$fieldBody
)
"
#endregion
#region file operation
Remove-Item -Force -Path $outputPath | Out-Null
$ctlBody | out-file -FilePath $outputPath -Encoding ascii -Force -NoClobber
#endregion
if($PassThru)
{
return $ctlBody
}
}
#endregion