Database/Database.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 |
if (!(Test-Path variable:Global:SQLiteConn )) { $Global:SQLiteConn = New-Object System.Collections.ArrayList } <# .Synopsis Creates a connection to a SQLite3 Database .DESCRIPTION Creates a connection to a SQLite3 Database file and stores the connection in to $Global:sqliteconn. .EXAMPLE Opens database main.db and creates a connection object for it. PS C:\> Connect-DBSQLite3 -DataBase .\main.db Connection Database Index ---------- -------- ----- System.Data.SQLite.SQLiteConnection .\main.db 0 #> function Connect-DBSQLite3 { [CmdletBinding()] param ( # Databse file to open. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [ValidateScript({Test-Path $_})] [string]$DataBase # Open Database ReadOnly also. #[switch]$ReadOnly, # Password for opening the database if it requieres it. #[SecureString]$Password ) Begin { $x86Assembly = "$($PSScriptRoot)\x86\System.Data.SQLite.dll" $x64Assembly = "$($PSScriptRoot)\x64\System.Data.SQLite.dll" # Load the appropiate DLL Depending on the Archiecture switch ([intptr]::size) { 4 {$sqlitedll = [System.Reflection.Assembly]::LoadFrom($x86Assembly)} 8 {$sqlitedll = [System.Reflection.Assembly]::LoadFrom($x64Assembly)} } $DataBaseFile = (Get-ItemProperty $DataBase).FullName } Process { $cn = New-Object -TypeName System.Data.SQLite.SQLiteConnection $cn.ConnectionString = "Data Source=$DataBaseFile" $cn.Open() $conn_obj = $cn if ($Global:sqliteconn -notcontains $conn_obj) { $SessionIndex = $Global:sqliteconn.Count $NewConnection = New-Object psobject -Property @{ Index = $SessionIndex.ToString() ; Connection = $conn_obj; Database = $DataBase } [void]$Global:sqliteconn.Add($NewConnection) # Return the connection object. $NewConnection } else { Write-Warning "A connection to $DataBase already exists." } } End { } } <# .Synopsis Removes a specific SQLite3 connection .DESCRIPTION Removes a specific SQLite3 connection given its Index .EXAMPLE Disconnect all SQLite3 connections PS C:\> Get-DBSQLite3Connection | Remove-SQLite3Connection Connection Database Index ---------- -------- ----- System.Data.SQLite.SQLiteConnection .\main.db 0 .EXAMPLE Remove a SQLite3 connection given its index PS C:\> Remove-DBSQLite3Connection -Index 0 Connection Database Index ---------- -------- ----- System.Data.SQLite.SQLiteConnection .\main.db 0 #> function Remove-DBSQLite3Connection { [CmdletBinding()] param( # Index for the database connection. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [Int32] $Index ) Begin{ $currentConnections = @() foreach($conn in $Global:sqliteconn) {$currentConnections += $conn} } Process { if ($Index -ge 0) { Write-Verbose "Removing connection with Index $Index" foreach($i in $Index) { foreach($Connection in $currentConnections) { if ($Connection.Index -eq $i) { Write-Verbose "Connection Found" $Connection.connection.close() $Global:sqliteconn.Remove($Connection) Write-Verbose "Connection removed." } } } } } End{} } <# .Synopsis Get SQLite3 Connections .DESCRIPTION Get all or a specified existing SQLite3 Connection. .EXAMPLE Gets all SQLIte3 Connections PS C:\> Get-DBSQLite3Connection Connection Database Index ---------- -------- ----- System.Data.SQLite.SQLiteConnection .\main.db 0 #> function Get-DBSQLite3Connection { [CmdletBinding()] param( [Parameter(Mandatory=$false)] [Int32] $Index ) Begin{} Process { if ($Index) { foreach($i in $Index) { foreach($Connection in $Global:sqliteconn) { if ($Connection.Index -eq $i) { $Connection } } } } else { # Return all database connections. $return_sessions = @() foreach($s in $Global:sqliteconn){$return_sessions += $s} $return_sessions } } End{} } <# .Synopsis Exsecutes SQL query against SQLite3 Connection .DESCRIPTION Exsecutes SQL query against SQLite3 Connection against an existing SQLite3 Connection .EXAMPLE Execute query to list all the tables in the database. PS C:\> Invoke-SQLite3Query -SQL "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;" -Index 0 #> function Invoke-DBSQLite3Query { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$SQL, [Parameter(Mandatory=$true, ParameterSetName = "Index")] [int32]$Index, [Parameter(Mandatory=$true, ParameterSetName = "Connection")] [PSobject]$Connection ) if ($Index -ge 0) { Write-Verbose "Executing Query $SQL" Write-Verbose "Executing against $Index" foreach($conn in $Global:sqliteconn) { if ($conn.index -in $Index) { $cmd = new-object System.Data.SQLite.SQLiteCommand($SQL,$conn.Connection) $ds = New-Object system.Data.DataSet $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd) $da.fill($ds) | Out-Null return $ds.tables[0] } } } elsif ($Connection -ne $null) { $cmd = new-object System.Data.SQLite.SQLiteCommand($SQL,$Connection.Connection) $ds = New-Object system.Data.DataSet $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd) $da.fill($ds) | Out-Null return $ds.tables[0] } } function New-DBSQLConnectionString { [CmdletBinding()] Param( [string]$ServerName, [string]$DatabaseName, [string]$UserName, [string]$Password, [Switch]$IntegratedAuth ) If($IntegratedAuth) { $ConnectionString = "server=$ServerName;database=$DatabaseName;trusted_connection=true;" } Else { $ConnectionString = "server=$ServerName;database=$DatabaseName;User Id=$UserName;Password=$Password;trusted_connection=False;" } Return $ConnectionString } |