-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccessFunctions.psm1
438 lines (369 loc) · 24.7 KB
/
AccessFunctions.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
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
#Requires -version 2.0
##
## Author Richard Siddaway
## Version 0.2
## Added Reset-Autonum function
## Added Autonum option to Add-AccessColumn
## Added fields parameter to Add-AccessRecord
## Added external Help file
## Added New-AccessIndex, Remove-AccessIndex functions
## Added Get-AccessTableDefinition
## Added New-StoredProcedure
## Added Invoke-AccessStoredProcedure
##
## Version 0.1 - Inital Release December 2009
##
## Connection functions
##
function Open-AccessDatabase {
param (
[string]$name,
[string]$path
)
$file = Join-Path -Path $path -ChildPath $name
if (!(Test-Path $file)){Throw "File Does Not Exists"}
$connection = New-Object System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=$file")
$connection.Open()
$connection
}
function Close-AccessDatabase {
param (
[System.Data.OleDb.OleDbConnection]$connection
)
$connection.Close()
}
function Test-AccessConnection {
param (
[System.Data.OleDb.OleDbConnection]$connection
)
if ($connection.State -eq "Open"){$open = $true}
else {$open = $false}
$open
}
##
## data definition functions
##
function New-AccessDatabase {
param (
[string]$name,
[string]$path,
[switch]$acc3
)
if (!(Test-Path $path)){Throw "Invaild Folder"}
$file = Join-Path -Path $path -ChildPath $name
if (Test-Path $file){Throw "File Already Exists"}
$cat = New-Object -ComObject 'ADOX.Catalog'
if ($acc3) {$cat.Create("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$file")}
else {$cat.Create("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=$file")}
$cat.ActiveConnection.Close()
}
##
## Tables
##
$datatype = DATA {
ConvertFrom-StringData -StringData @'
3 = Integer
7 = Date
130 = String
'@
}
function Get-AccessTableDefinition {
param (
[string]$name,
[string]$path,
[string]$table = ""
)
$file = Join-Path -Path $path -ChildPath $name
if (!(Test-Path $file)){Throw "File Does Not Exists"}
$conn = New-Object -ComObject ADODB.Connection
$conn.Open("Provider = Microsoft.JET.OLEDB.4.0; Data Source = $file")
$cat = New-Object -ComObject ADOX.Catalog
$cat.ActiveConnection = $conn
## view tables
## note user tables are of type TABLE
if ($table) {
$actable = $cat.Tables | where {$_.Name -eq $table}
$actable.Columns | Format-Table Name, DefinedSize,
@{Name="Data Type"; Expression={$datatype["$($_.Type)"]}} -AutoSize
}
else {$cat.tables | select Name, DateCreated, DateModified}
$conn.Close()
}
function New-AccessTable {
## assumes database is open
## add code to check if table exists
param (
[string]$table,
[System.Data.OleDb.OleDbConnection]$connection
)
$sql = " CREATE TABLE $table"
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$cmd.ExecuteNonQuery()
}
function Remove-AccessTable {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[string]$table,
[System.Data.OleDb.OleDbConnection]$connection
)
$sql = "DROP TABLE $table "
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
if ($psCmdlet.ShouldProcess("$($connection.DataSource)", "$sql")){$cmd.ExecuteNonQuery()}
}
##
## Columns
##
function New-AccessColumn {
[CmdletBinding()]
param (
[System.Data.OleDb.OleDbConnection]$connection,
[string]$table,
[switch]$notnull,
[parameter(ParameterSetName="datetime")]
[string]$dtname,
[parameter(ParameterSetName="unique")]
[string]$uniquename,
[parameter(ParameterSetName="binary")]
[string]$binname,
[parameter(ParameterSetName="bit")]
[string]$bitname,
[parameter(ParameterSetName="tinyinteger")]
[string]$tnyintname,
[parameter(ParameterSetName="smallinteger")]
[string]$smlintname,
[parameter(ParameterSetName="integer")]
[string]$intname,
[parameter(ParameterSetName="double")]
[string]$dblname,
[parameter(ParameterSetName="real")]
[string]$realname,
[parameter(ParameterSetName="float")]
[string]$floatname,
[parameter(ParameterSetName="decimal")]
[string]$decname,
[parameter(ParameterSetName="money")]
[string]$mnyname,
[parameter(ParameterSetName="autonum")]
[string]$autoname,
[parameter(ParameterSetName="char")]
[string]$charname,
[parameter(ParameterSetName="text")]
[string]$textname,
[parameter(ParameterSetName="image")]
[string]$imgname,
[parameter(ParameterSetName="char")]
[parameter(ParameterSetName="text")]
[int]$size = 10
)
switch ($psCmdlet.ParameterSetName){
datetime {$sql = "ALTER TABLE $table ADD COLUMN $dtname DATETIME" }
autonum {$sql = "ALTER TABLE $table ADD COLUMN $autoname COUNTER(1,1)"}
binary {$sql = "ALTER TABLE $table ADD COLUMN $binname BINARY" }
bit {$sql = "ALTER TABLE $table ADD COLUMN $bitname BIT" }
unique {$sql = "ALTER TABLE $table ADD COLUMN $uniquename UNIQUEIDENTIFIER" }
tinyinteger {$sql = "ALTER TABLE $table ADD COLUMN $tnyintname TINYINT" }
smallinteger {$sql = "ALTER TABLE $table ADD COLUMN $smlintname SMALLINT" }
integer {$sql = "ALTER TABLE $table ADD COLUMN $intname INTEGER" }
double {$sql = "ALTER TABLE $table ADD COLUMN $dblname DOUBLE" }
float {$sql = "ALTER TABLE $table ADD COLUMN $floatname FLOAT" }
real {$sql = "ALTER TABLE $table ADD COLUMN $realname REAL" }
decimal {$sql = "ALTER TABLE $table ADD COLUMN $decname DECIMAL" }
money {$sql = "ALTER TABLE $table ADD COLUMN $mnyname MONEY" }
char {$sql = "ALTER TABLE $table ADD COLUMN $charname CHARACTER($size)" }
text {$sql = "ALTER TABLE $table ADD COLUMN $textname TEXT($size)" }
image {$sql = "ALTER TABLE $table ADD COLUMN $imgname IMAGE" }
}
if ($notnull) {$sql = $sql + " NOT NULL"}
Write-Debug $sql
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$cmd.ExecuteNonQuery()
}
function Remove-AccessColumn {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[string]$table,
[string]$column,
[System.Data.OleDb.OleDbConnection]$connection
)
$sql = "ALTER TABLE $table DROP COLUMN $column"
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
if ($psCmdlet.ShouldProcess("$($connection.DataSource)", "$sql")){$cmd.ExecuteNonQuery()}
}
##
## Data manipulation functions
##
function Add-AccessRecord {
# .ExternalHelp Maml-AccessFunctions.XML
[CmdletBinding()]
param (
[parameter(ParameterSetName="sql")]
[string]$sql,
[System.Data.OleDb.OleDbConnection]$connection,
[parameter(ParameterSetName="field")]
[parameter(ParameterSetName="value")]
[string]$table,
[parameter(ParameterSetName="field")]
[string]$fields,
[parameter(ParameterSetName="field")]
[parameter(ParameterSetName="value")]
[string]$values
)
switch ($psCmdlet.ParameterSetName){
value {$sql = "INSERT INTO $table VALUES ($values)" }
field {$sql = "INSERT INTO $table ($fields) VALUES ($values)" }
}
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$cmd.ExecuteNonQuery()
}
function Get-AccessData {
param (
[string]$sql,
[System.Data.OleDb.OleDbConnection]$connection,
[switch]$grid
)
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$reader = $cmd.ExecuteReader()
$dt = New-Object System.Data.DataTable
$dt.Load($reader)
if ($grid) {$dt | Out-GridView -Title "$sql" }
else {$dt}
}
function Remove-AccessData {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[string]$table,
[string]$filter,
[System.Data.OleDb.OleDbConnection]$connection
)
$sql = "DELETE FROM $table WHERE $filter"
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
if ($psCmdlet.ShouldProcess("$($connection.DataSource)", "$sql")){$cmd.ExecuteNonQuery()}
}
function Set-AccessData {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[string]$table,
[string]$filter,
[string]$value,
[System.Data.OleDb.OleDbConnection]$connection
)
$sql = "UPDATE $table SET $value WHERE $filter"
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
if ($psCmdlet.ShouldProcess("$($connection.DataSource)", "$sql")){$cmd.ExecuteNonQuery()}
}
## reset the Autonumber function to a different
## start value
function Reset-Autonum {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[string]$table,
[string]$column,
[int]$newstart,
[System.Data.OleDb.OleDbConnection]$connection
)
$sql = "INSERT INTO $table ($column) VALUES ($newstart)"
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
if ($psCmdlet.ShouldProcess("$($connection.DataSource)", "$sql")){$cmd.ExecuteNonQuery()}
}
##
## Index functions
##
function New-AccessIndex {
# .ExternalHelp Maml-AccessFunctions.XML
[CmdletBinding()]
param (
[string]$table,
[string]$index,
[string]$field,
[System.Data.OleDb.OleDbConnection]$connection,
[switch]$unique,
[switch]$descend
)
if ($unique) {$sql = " CREATE UNIQUE INDEX $index ON $table"}
else {$sql = " CREATE INDEX $index ON $table"}
if ($descend){$sql += " ($field DESC)"}
else {$sql += " ($field)"}
Write-Debug $sql
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$cmd.ExecuteNonQuery()
}
function Remove-AccessIndex {
# .ExternalHelp Maml-AccessFunctions.XML
[CmdletBinding()]
param (
[string]$table,
[string]$index,
[System.Data.OleDb.OleDbConnection]$connection
)
$sql = "DROP INDEX $index ON $table"
Write-Debug $sql
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$cmd.ExecuteNonQuery()
}
##
## Stored procedure functions
##
function Get-AccessStoredProcedure {
param (
[string]$name,
[string]$path,
[string]$proc = ""
)
$file = Join-Path -Path $path -ChildPath $name
if (!(Test-Path $file)){Throw "File Does Not Exists"}
$conn = New-Object -ComObject ADODB.Connection
$conn.Open("Provider = Microsoft.JET.OLEDB.4.0; Data Source = $file")
$cat = New-Object -ComObject ADOX.Catalog
$cat.ActiveConnection = $conn
## view procedures
<#
if ($table) {
$actable = $cat.Tables | where {$_.Name -eq $table}
$actable.Columns | Format-Table Name, DefinedSize,
@{Name="Data Type"; Expression={$datatype["$($_.Type)"]}} -AutoSize
}
else {$cat.tables | select Name, DateCreated, DateModified}
#>
$conn.Close()
}
function New-AccessStoredProcedure {
# .ExternalHelp Maml-AccessFunctions.XML
[CmdletBinding()]
param (
[System.Data.OleDb.OleDbConnection]$connection,
[string]$name,
[string]$proc
)
$sql = "CREATE PROCEDURE $name AS $proc"
Write-Debug $sql
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$cmd.ExecuteNonQuery()
}
function Invoke-AccessStoredProcedure {
# .ExternalHelp Maml-AccessFunctions.XML
[CmdletBinding()]
param (
[System.Data.OleDb.OleDbConnection]$connection,
[string]$name,
[switch]$grid
)
$sql = "EXECUTE $name "
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$reader = $cmd.ExecuteReader()
$dt = New-Object System.Data.DataTable
$dt.Load($reader)
if ($grid) {$dt | Out-GridView -Title "$sql" }
else {$dt}
}
function Remove-AccessStoredProcedure {
# .ExternalHelp Maml-AccessFunctions.XML
[CmdletBinding()]
param (
[System.Data.OleDb.OleDbConnection]$connection,
[string]$name
)
$sql = "DROP PROCEDURE $name"
Write-Debug $sql
$cmd = New-Object System.Data.OleDb.OleDbCommand($sql, $connection)
$cmd.ExecuteNonQuery()
}