This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMySql.psm1
143 lines (129 loc) · 3.97 KB
/
MySql.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
Function LoadAssembly
{
# We need to know the default installation location for 32-bit or 64-bit
$query = Get-WmiObject -query "Select AddressWidth from Win32_Processor" | Select-Object -First 1
Try
{
if($query.AddressWidth -eq 64)
{
$rootFolder = 'C:\Program Files (x86)\MySQL'
}
else
{
$rootFolder = 'C:\Program Files\MySQL\'
}
$library = $rootFolder | Get-ChildItem -Filter 'MySql.Data.dll' -Recurse | Select-Object -First 1
[void][system.reflection.Assembly]::LoadFrom($($library.FullName))
}
Catch [Exception]
{
Throw "Could not load the MySQL connector for DotNet. Please make sure it is installed in the default location. You can download the installer from http://dev.mysql.com/downloads/connector/net/"
}
}
Function Invoke-NonQuery
{
Param(
[Parameter(Mandatory=$TRUE)][string]$serverName,
[Parameter(Mandatory=$TRUE)][string]$userName,
[Parameter(Mandatory=$TRUE)][string]$password,
[Parameter(Mandatory=$TRUE)][string]$dbName,
[Parameter(Mandatory=$TRUE)][string]$query
)
LoadAssembly
$dbconnect = New-Object MySql.Data.MySqlClient.MySqlConnection
$dbconnect.ConnectionString = "server=$serverName;user id=$userName;password=$password;database=$dbName;pooling=false"
Try
{
$dbconnect.Open()
$sql = New-Object MySql.Data.MySqlClient.MySqlCommand
$sql.Connection = $dbconnect
$sql.CommandText = $query
return $sql.ExecuteNonQuery()
}
Catch
{
Throw
}
Finally
{
if($dbconnect -ne $null)
{
$dbconnect.Close()
}
}
}
Function Invoke-Query
{
Param(
[Parameter(Mandatory=$TRUE)][string]$serverName,
[Parameter(Mandatory=$TRUE)][string]$userName,
[Parameter(Mandatory=$TRUE)][string]$password,
[Parameter(Mandatory=$TRUE)][string]$dbName,
[Parameter(Mandatory=$TRUE)][string]$query,
[Parameter(Mandatory=$FALSE)][string]$queryTimeoutSeconds=60
)
LoadAssembly
$dbconnect = New-Object MySql.Data.MySqlClient.MySqlConnection
$dbconnect.ConnectionString = "server=$serverName;user id=$userName;password=$password;database=$dbName;pooling=false"
Try
{
$dbconnect.Open()
$sql = New-Object MySql.Data.MySqlClient.MySqlCommand
$sql.Connection = $dbconnect
$sql.CommandText = $query
$sql.CommandTimeout = $queryTimeoutSeconds
return $sql.ExecuteReader()
}
Catch
{
Throw
}
Finally
{
if($dbconnect -ne $null)
{
$dbconnect.Close()
}
}
}
Function Get-PSObjectFromDataRecords
{
Param(
[Parameter(Mandatory=$TRUE)]$records
)
if($records.GetType().BaseType.Name -eq "DbDataRecord" -or $records.count -gt 0)
{
# property bag for column names
$properties = @{}
# add properties to the bag
if($records.GetType().BaseType.Name -eq "DbDataRecord")
{
# single record
for($i=0;$i -lt $records.FieldCount;$i++)
{
$properties.Add($records.GetName($i),$null)
}
}
else
{
# multiple records
for($i=0;$i -lt $records[0].FieldCount;$i++)
{
$properties.Add($records[0].GetName($i),$null)
}
}
# iterate results, set up the customobject, pop to the pipeline
foreach($result in $records)
{
$resultObject = New-Object -TypeName PSObject -Property $properties
for($i=0;$i -lt $result.FieldCount;$i++)
{
$resultObject.($result.GetName($i)) = $result.GetValue($i)
}
$resultObject
}
}
}
Export-ModuleMember -Function Invoke-NonQuery
Export-ModuleMember -Function Invoke-Query
Export-ModuleMember -Function Get-PSObjectFromDataRecords