├── tools
├── Publish.ps1
└── Publish-Local.ps1
├── .gitignore
├── source
├── Horker.Data.Tests
│ ├── UnitTest1.cs
│ └── Horker.Data.Tests.csproj
├── Horker.Data
│ ├── Horker.Data.csproj
│ ├── Classes
│ │ ├── ConnectionOpener.cs
│ │ ├── Helpers.cs
│ │ └── ConnectionSetting.cs
│ ├── Cmdlets
│ │ ├── Schema.cs
│ │ ├── Connection.cs
│ │ ├── Configuration.cs
│ │ ├── DbProviderFactory.cs
│ │ ├── CopyDataRow.cs
│ │ ├── ConnectionString.cs
│ │ ├── InvokeDataQuery.cs
│ │ └── ExportDataTable.cs
│ └── Horker.Data.xml
└── dataquery.sln
├── docs
├── Get-DataQueryResult.md
├── Get-DataConnectionString.md
├── Close-DataConnection.md
├── Get-DataConnectionHistory.md
├── Unregister-DataConnectionString.md
├── Unregister-DbProviderFactory.md
├── Get-DbProviderFactory.md
├── Register-DataConfiguration.md
├── Register-DataConnectionString.md
├── New-DataConnection.md
├── New-DataConnectionString.md
├── Get-DataSchema.md
├── Register-DbProviderFactory.md
├── Export-DataTable.md
├── Copy-DataRow.md
└── Invoke-DataQuery.md
├── LICENSE.txt
├── scripts
├── HorkerDataQuery.psm1
└── HorkerDataQuery.psd1
├── tests
├── Copy-DataRow.Tests.ps1
└── Invoke-DataQuery.Tests.ps1
├── .build.ps1
└── README.md
/tools/Publish.ps1:
--------------------------------------------------------------------------------
1 | Publish-Module -Path $PSScriptRoot\..\module\release\HorkerDataQuery -NugetApiKey (cat private\NugetApiKey.txt)
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | HorkerDataQuery/
2 | debug/
3 |
4 | .vs/
5 | bin/
6 | obj/
7 | packages/
8 | *.user
9 |
10 | private/
11 | vendor/
12 |
13 | *.sqlite
14 | *.accdb
15 | *.db
16 |
--------------------------------------------------------------------------------
/source/Horker.Data.Tests/UnitTest1.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Xunit;
3 |
4 | namespace Horker.Data.Tests
5 | {
6 | public class UnitTest1
7 | {
8 | [Fact]
9 | public void Test1()
10 | {
11 |
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/tools/Publish-Local.ps1:
--------------------------------------------------------------------------------
1 | if (Test-Path ~\localpsrepo\HorkerDataQuery.*.nupkg) {
2 | rm ~\localpsrepo\HorkerDataQuery.*.nupkg
3 | }
4 |
5 | Publish-Module -path $PSScriptRoot\..\module\release\HorkerDataQuery -Repository LocalPSrepo -NuGetApiKey any -vb
6 | Install-Module HorkerDataQuery -force -Repository LocalPSRepo
7 |
--------------------------------------------------------------------------------
/source/Horker.Data.Tests/Horker.Data.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/docs/Get-DataQueryResult.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Get-DataQueryResult
9 |
10 | ## SYNOPSIS
11 |
12 | Gets a result of the last SQL statement.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Get-DataQueryResult []
18 | ```
19 |
20 | ## DESCRIPTION
21 |
22 | Gets the number of records affected by the last statement by the Invoke-DataQuery cmdlet. If the previous statement is SELECT, the cmdlet will return -1.
23 |
24 | ## EXAMPLES
25 |
26 | ## PARAMETERS
27 |
28 | ### CommonParameters
29 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
30 |
31 | ## INPUTS
32 |
33 | ### None
34 |
35 | ## OUTPUTS
36 |
37 | ### System.Int32
38 |
39 | ## NOTES
40 |
41 | ## RELATED LINKS
42 |
--------------------------------------------------------------------------------
/docs/Get-DataConnectionString.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Get-DataConnectionString
9 |
10 | ## SYNOPSIS
11 |
12 | Gets connection strings defined in the ConfigurationManager.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Get-DataConnectionString []
18 | ```
19 |
20 | ## DESCRIPTION
21 |
22 | The Get-DataConnectionString cmdlet gets all connection strings defined in the ConfigurationManager.
23 |
24 | ## EXAMPLES
25 |
26 | ## PARAMETERS
27 |
28 | ### CommonParameters
29 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
30 |
31 | ## INPUTS
32 |
33 | ### None
34 |
35 | ## OUTPUTS
36 |
37 | ### System.Configuration.ConnectionStringSettings
38 |
39 | ## NOTES
40 |
41 | ## RELATED LINKS
42 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2018 horker
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of
4 | this software and associated documentation files (the "Software"), to deal in
5 | the Software without restriction, including without limitation the rights to
6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 | of the Software, and to permit persons to whom the Software is furnished to do
8 | so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/docs/Close-DataConnection.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Close-DataConnection
9 |
10 | ## SYNOPSIS
11 |
12 | Closes a database connection.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Close-DataConnection [-Connection] []
18 | ```
19 |
20 | ## DESCRIPTION
21 |
22 | The Close-DataConnection cmdlet closes a database connection.
23 |
24 | ## EXAMPLES
25 |
26 | ## PARAMETERS
27 |
28 | ### -Connection
29 |
30 | A database connection.
31 |
32 | ```yaml
33 | Type: DbConnection
34 | Parameter Sets: (All)
35 | Aliases:
36 |
37 | Required: True
38 | Position: 0
39 | Default value: None
40 | Accept pipeline input: False
41 | Accept wildcard characters: False
42 | ```
43 |
44 | ### CommonParameters
45 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
46 |
47 | ## INPUTS
48 |
49 | ### None
50 |
51 | ## OUTPUTS
52 |
53 | ### System.Void
54 |
55 | ## NOTES
56 |
57 | ## RELATED LINKS
58 |
--------------------------------------------------------------------------------
/docs/Get-DataConnectionHistory.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Get-DataConnectionHistory
9 |
10 | ## SYNOPSIS
11 |
12 | Gets open database connections in the current session.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Get-DataConnectionHistory []
18 | ```
19 |
20 | ## DESCRIPTION
21 |
22 | The Get-DataConnectionHistory cmdlet gets open database connections in the current session that you have opened explicitly by the New-DataConnection cmdlet, or implicitly by the other cmdlets you invoked.
23 |
24 | This cmdlet is useful when you need to investigate untracked open connections causing a trouble (acquiring a file lock, for example).
25 |
26 | ## EXAMPLES
27 |
28 | ## PARAMETERS
29 |
30 | ### CommonParameters
31 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
32 |
33 | ## INPUTS
34 |
35 | ### None
36 |
37 | ## OUTPUTS
38 |
39 | ### System.Data.Common.DbConnection
40 |
41 | ## NOTES
42 |
43 | ## RELATED LINKS
44 |
--------------------------------------------------------------------------------
/docs/Unregister-DataConnectionString.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Unregister-DataConnectionString
9 |
10 | ## SYNOPSIS
11 |
12 | Removes a connection string definition from the ConfigurationManager.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Unregister-DataConnectionString [-Name] []
18 | ```
19 |
20 | ## DESCRIPTION
21 |
22 | The Unregister-DataConnectionString cmdlet removes a connection string definition from the ConfigurationManager.
23 |
24 | ## EXAMPLES
25 |
26 | ## PARAMETERS
27 |
28 | ### -Name
29 |
30 | A connection string name.
31 |
32 | ```yaml
33 | Type: String
34 | Parameter Sets: (All)
35 | Aliases:
36 |
37 | Required: True
38 | Position: 0
39 | Default value: None
40 | Accept pipeline input: False
41 | Accept wildcard characters: False
42 | ```
43 |
44 | ### CommonParameters
45 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
46 |
47 | ## INPUTS
48 |
49 | ### None
50 |
51 | ## OUTPUTS
52 |
53 | ### System.Void
54 |
55 | ## NOTES
56 |
57 | ## RELATED LINKS
58 |
--------------------------------------------------------------------------------
/docs/Unregister-DbProviderFactory.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Unregister-DbProviderFactory
9 |
10 | ## SYNOPSIS
11 |
12 | Removes a database provider factory from the ConfigurationManager.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Unregister-DbProviderFactory [-ProviderName] []
18 | ```
19 |
20 | ## DESCRIPTION
21 |
22 | The Unregister-DbProviderFactory cmdlet removes a database provider factory from the ConfigurationManager.
23 |
24 | ## EXAMPLES
25 |
26 | ## PARAMETERS
27 |
28 | ### -ProviderName
29 |
30 | A database provider name.
31 |
32 | ```yaml
33 | Type: String
34 | Parameter Sets: (All)
35 | Aliases:
36 |
37 | Required: True
38 | Position: 0
39 | Default value: None
40 | Accept pipeline input: False
41 | Accept wildcard characters: False
42 | ```
43 |
44 | ### CommonParameters
45 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
46 |
47 | ## INPUTS
48 |
49 | ### None
50 |
51 | ## OUTPUTS
52 |
53 | ### System.Void
54 |
55 | ## NOTES
56 |
57 | ## RELATED LINKS
58 |
--------------------------------------------------------------------------------
/source/Horker.Data/Horker.Data.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.1
5 | Horker.Data
6 | Horker.Data
7 | AnyCPU;x64
8 |
9 |
10 |
11 | $(TargetDir)Horker.Data.xml
12 |
13 |
14 |
15 |
16 | $(TargetDir)Horker.Data.xml
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/docs/Get-DbProviderFactory.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Get-DbProviderFactory
9 |
10 | ## SYNOPSIS
11 |
12 | Gets database provider factories defined in the ConfigurationManager.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Get-DbProviderFactory [[-ProviderName] ] []
18 | ```
19 |
20 | ## DESCRIPTION
21 |
22 | The Get-DbProviderFactory cmdlet gets database provider factories defined in the ConfigurationManager.
23 |
24 | If the -ProviderName parameter is not specified, it returns all database provider factories.
25 |
26 | ## EXAMPLES
27 |
28 | ## PARAMETERS
29 |
30 | ### -ProviderName
31 |
32 | A database provider name.
33 |
34 | ```yaml
35 | Type: String
36 | Parameter Sets: (All)
37 | Aliases:
38 |
39 | Required: False
40 | Position: 0
41 | Default value: None
42 | Accept pipeline input: False
43 | Accept wildcard characters: False
44 | ```
45 |
46 | ### CommonParameters
47 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
48 |
49 | ## INPUTS
50 |
51 | ### None
52 |
53 | ## OUTPUTS
54 |
55 | ### System.Data.DataTable
56 |
57 | ## NOTES
58 |
59 | ## RELATED LINKS
60 |
--------------------------------------------------------------------------------
/scripts/HorkerDataQuery.psm1:
--------------------------------------------------------------------------------
1 | $script:DefaultDbProviderFactories = @(
2 | @{
3 | Name = "Odbc Data Provider"
4 | InvariantName = "System.Data.Odbc"
5 | Description = ".Net Framework Data Provider for Odbc"
6 | Type = [System.Data.Odbc.OdbcFactory].AssemblyQualifiedName
7 | }
8 | @{
9 | Name = "OleDb Data Provider"
10 | InvariantName = "System.Data.OleDb"
11 | Description = ".Net Framework Data Provider for OleDb"
12 | Type = [System.Data.OleDb.OleDbFactory].AssemblyQualifiedName
13 | }
14 | @{
15 | Name = "SqlClient Data Provider"
16 | InvariantName = "System.Data.SqlClient"
17 | Description = ".Net Framework Data Provider for SqlServer"
18 | Type = [System.Data.SqlClient.SqlClientFactory].AssemblyQualifiedName
19 | }
20 | @{
21 | Name = "SQLite Data Provider"
22 | InvariantName = "System.Data.SQLite"
23 | Description = ".NET Framework Data Provider for SQLite"
24 | Type = [System.Data.SQLite.SQLiteFactory].AssemblyQualifiedName
25 | }
26 | @{
27 | Name = "Npgsql Data Provider"
28 | InvariantName = "Npgsql"
29 | Description = ".Net Data Provider for PostgreSQL"
30 | Type = [Npgsql.NpgsqlFactory].AssemblyQualifiedName
31 | }
32 | )
33 |
34 | foreach ($f in $DefaultDbProviderFactories) {
35 | Register-DbProviderFactory $f["Name"] $f["InvariantName"] $f["Description"] $f["Type"]
36 | }
37 |
38 | Register-DataConnectionString "memory" "System.Data.SQLite" "Data Source=':memory:'"
39 |
--------------------------------------------------------------------------------
/docs/Register-DataConfiguration.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Register-DataConfiguration
9 |
10 | ## SYNOPSIS
11 |
12 | Registers connection strings and database provider factories.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Register-DataConfiguration [-ConfigurationFile] []
18 | ```
19 |
20 | ## DESCRIPTION
21 |
22 | The Register-DataConfiguration cmdlet reads a configuration file (app.config or web.config in most cases), finds the /configuration/connectionStrings and /configuration/system.data/DbProviderFactories sections and, according to their contents, registers connection strings and database provider factories to the ConfigurationManager.
23 |
24 | ## EXAMPLES
25 |
26 | ## PARAMETERS
27 |
28 | ### -ConfigurationFile
29 |
30 | A configuration file name.
31 |
32 | ```yaml
33 | Type: String
34 | Parameter Sets: (All)
35 | Aliases:
36 |
37 | Required: True
38 | Position: 0
39 | Default value: None
40 | Accept pipeline input: False
41 | Accept wildcard characters: False
42 | ```
43 |
44 | ### CommonParameters
45 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
46 |
47 | ## INPUTS
48 |
49 | ### None
50 |
51 | ## OUTPUTS
52 |
53 | ### System.Void
54 |
55 | ## NOTES
56 |
57 | ## RELATED LINKS
58 |
--------------------------------------------------------------------------------
/source/Horker.Data/Classes/ConnectionOpener.cs:
--------------------------------------------------------------------------------
1 | using System.Data.Common;
2 |
3 | #pragma warning disable CS1591
4 |
5 | namespace Horker.Data
6 | {
7 | public class ConnectionSpecifier
8 | {
9 | public DbConnection Connection { get; private set; }
10 | public bool ConnectionOpened { get; private set; }
11 |
12 | public ConnectionSpecifier(string fileOrName, DbConnection connection, string providerName, string connectionString)
13 | {
14 | if (fileOrName != null) {
15 | Connection = new FileConnectionSetting(fileOrName).GetConnection();
16 | ConnectionOpened = true;
17 | }
18 | else if (connection != null) {
19 | Connection = connection;
20 | ConnectionOpened = false;
21 | }
22 | else {
23 | Connection = new ProviderConnectionSetting(providerName, connectionString).GetConnection();
24 | ConnectionOpened = true;
25 | }
26 |
27 | if (ConnectionOpened) {
28 | GetDataConnectionHistory.AddToHistory(Connection);
29 | }
30 | }
31 |
32 | public void Close()
33 | {
34 | if (ConnectionOpened && Connection != null)
35 | Connection.Close();
36 | ConnectionOpened = false;
37 | }
38 |
39 | public static implicit operator ConnectionSpecifier(string fileOrName)
40 | {
41 | return new ConnectionSpecifier(fileOrName, null, null, null);
42 | }
43 |
44 | public static implicit operator ConnectionSpecifier(DbConnection connection)
45 | {
46 | return new ConnectionSpecifier(null, connection, null, null);
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/docs/Register-DataConnectionString.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Register-DataConnectionString
9 |
10 | ## SYNOPSIS
11 |
12 | Registers a connection string to the ConfigurationManager.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Register-DataConnectionString [-Name] [-ProviderName] [-ConnectionString]
18 | []
19 | ```
20 |
21 | ## DESCRIPTION
22 |
23 | The Register-DataConnectionString cmdlet registers a connection string to the ConfigurationManager.
24 |
25 | ## EXAMPLES
26 |
27 | ## PARAMETERS
28 |
29 | ### -ConnectionString
30 |
31 | A connection string.
32 |
33 | ```yaml
34 | Type: String
35 | Parameter Sets: (All)
36 | Aliases:
37 |
38 | Required: True
39 | Position: 2
40 | Default value: None
41 | Accept pipeline input: False
42 | Accept wildcard characters: False
43 | ```
44 |
45 | ### -Name
46 |
47 | A connection string name.
48 |
49 | ```yaml
50 | Type: String
51 | Parameter Sets: (All)
52 | Aliases:
53 |
54 | Required: True
55 | Position: 0
56 | Default value: None
57 | Accept pipeline input: False
58 | Accept wildcard characters: False
59 | ```
60 |
61 | ### -ProviderName
62 |
63 | A database provider name.
64 |
65 | ```yaml
66 | Type: String
67 | Parameter Sets: (All)
68 | Aliases:
69 |
70 | Required: True
71 | Position: 1
72 | Default value: None
73 | Accept pipeline input: False
74 | Accept wildcard characters: False
75 | ```
76 |
77 | ### CommonParameters
78 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
79 |
80 | ## INPUTS
81 |
82 | ### None
83 |
84 | ## OUTPUTS
85 |
86 | ### System.Void
87 |
88 | ## NOTES
89 |
90 | ## RELATED LINKS
91 |
--------------------------------------------------------------------------------
/docs/New-DataConnection.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # New-DataConnection
9 |
10 | ## SYNOPSIS
11 |
12 | Opens a database connection.
13 |
14 | ## SYNTAX
15 |
16 | ### FileOrName (Default)
17 | ```
18 | New-DataConnection [-FileOrName] []
19 | ```
20 |
21 | ### ConnectionString
22 | ```
23 | New-DataConnection [-ProviderName] [-ConnectionString] []
24 | ```
25 |
26 | ## DESCRIPTION
27 |
28 | The New-DataConnection cmdlet opens a database connection.
29 |
30 | ## EXAMPLES
31 |
32 | ## PARAMETERS
33 |
34 | ### -ConnectionString
35 |
36 | A database provider name.
37 |
38 | ```yaml
39 | Type: String
40 | Parameter Sets: ConnectionString
41 | Aliases:
42 |
43 | Required: True
44 | Position: 1
45 | Default value: None
46 | Accept pipeline input: False
47 | Accept wildcard characters: False
48 | ```
49 |
50 | ### -FileOrName
51 |
52 | A database file name or a connection string name.
53 |
54 | ```yaml
55 | Type: String
56 | Parameter Sets: FileOrName
57 | Aliases:
58 |
59 | Required: True
60 | Position: 0
61 | Default value: None
62 | Accept pipeline input: False
63 | Accept wildcard characters: False
64 | ```
65 |
66 | ### -ProviderName
67 |
68 | A database provider name.
69 |
70 | ```yaml
71 | Type: String
72 | Parameter Sets: ConnectionString
73 | Aliases:
74 |
75 | Required: True
76 | Position: 0
77 | Default value: None
78 | Accept pipeline input: False
79 | Accept wildcard characters: False
80 | ```
81 |
82 | ### CommonParameters
83 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
84 |
85 | ## INPUTS
86 |
87 | ### None
88 |
89 | ## OUTPUTS
90 |
91 | ### System.Data.Common.DbConnection
92 |
93 | ## NOTES
94 |
95 | ## RELATED LINKS
96 |
--------------------------------------------------------------------------------
/tests/Copy-DataRow.Tests.ps1:
--------------------------------------------------------------------------------
1 | Set-StrictMode -Version 4
2 |
3 | $db1 = "$PSScriptRoot\testdb1.db"
4 | $db2 = "$PSScriptRoot\testdb2.db"
5 |
6 | Describe "Copy-DataRow" {
7 |
8 | BeforeEach {
9 | New-Item -Force $db1
10 | New-Item -Force $db2
11 | $conn1 = New-DataConnection $db1
12 | $conn2 = New-DataConnection $db2
13 | }
14 |
15 | AfterEach {
16 | Close-DataConnection $conn1
17 | Close-DataConnection $conn2
18 | Remove-Item $db1
19 | Remove-Item $db2
20 | }
21 |
22 | It "can copy data to another database by the specified SQL" {
23 | Invoke-DataQuery $conn1 "create table T (a, b, c)"
24 | Invoke-DataQuery $conn1 "insert into T (a, b, c) values (1, 'foo', 'bar')"
25 | Invoke-DataQuery $conn1 "insert into T (a, b, c) values (2, 'xxx', 'yyy')"
26 | Invoke-DataQuery $conn1 "insert into T (a, b, c) values (3, 'nnn', 'mmm')"
27 |
28 | Invoke-DataQuery $conn2 "create table U (x, y)"
29 |
30 | Copy-DataRow $conn1 "select a, c from T where a >= 2" $conn2 -TargetSql "insert into U values (@a, @c)"
31 |
32 | $result = Invoke-DataQuery $conn2 "select * from U"
33 |
34 | $result[0].x | Should -Be 2
35 | $result[0].y | Should -Be "yyy"
36 | $result[1].x | Should -Be 3
37 | $result[1].y | Should -Be "mmm"
38 | }
39 |
40 | It "can copy data to the specified table of another database" {
41 | Invoke-DataQuery $conn1 "create table T (a, b, c)"
42 | Invoke-DataQuery $conn1 "insert into T (a, b, c) values (1, 'foo', 'bar')"
43 | Invoke-DataQuery $conn1 "insert into T (a, b, c) values (2, 'xxx', 'yyy')"
44 | Invoke-DataQuery $conn1 "insert into T (a, b, c) values (3, 'nnn', 'mmm')"
45 |
46 | Invoke-DataQuery $conn2 "create table U (x, y)"
47 |
48 | Copy-DataRow $conn1 "select a as y, c as x from T where a >= 2" $conn2 "U"
49 |
50 | $result = Invoke-DataQuery $conn2 "select * from U"
51 |
52 | $result[0].x | Should -Be "yyy"
53 | $result[0].y | Should -Be 2
54 | $result[1].x | Should -Be "mmm"
55 | $result[1].y | Should -Be 3
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/tests/Invoke-DataQuery.Tests.ps1:
--------------------------------------------------------------------------------
1 | Set-StrictMode -Version 4
2 |
3 | $ACCESS = "$PSScriptRoot\test.accdb"
4 |
5 | Describe "Invoke-DataQuery" {
6 |
7 | BeforeEach {
8 | $mem = New-DataConnection memory
9 | Invoke-DataQuery $mem "create table Test (a, b, c)"
10 | }
11 |
12 | AfterEach {
13 | Close-DataConnection $mem
14 | }
15 |
16 | It "can make a query to SQLite" {
17 |
18 | $result = Invoke-DataQuery $mem "insert into Test (a, b, c) values (10, 'abc', null)"
19 | $result | Should -BeNull
20 | Get-DataQueryResult | Should -Be 1
21 |
22 | $result = Invoke-DataQuery $mem "select * from Test"
23 |
24 | $result | Should -BeOfType [PSObject]
25 | $result[0].a | Should -Be 10
26 | $result[0].b | Should -Be 'abc'
27 | $result[0].c | Should -Be $null
28 | }
29 |
30 | It "can take parameters as hashtable" {
31 |
32 | $result = Invoke-DataQuery $mem "insert into Test (a, b, c) values (@a, @b, @c)" @{ a = 10; b = "xxx"; c = $null }
33 | $result | Should -BeNull
34 | Get-DataQueryResult | Should -Be 1
35 |
36 | $result = Invoke-DataQuery $mem "select * from Test"
37 |
38 | $result | Should -BeOfType [PSObject]
39 | $result[0].a | Should -Be 10
40 | $result[0].b | Should -Be 'xxx'
41 | $result[0].c | Should -Be $null
42 | }
43 |
44 | It "can take parameters as array" {
45 |
46 | $result = Invoke-DataQuery $mem "insert into Test (a, b, c) values (?, ?, ?)" @(10, "xxx", $null)
47 | $result | Should -BeNull
48 | Get-DataQueryResult | Should -Be 1
49 |
50 | $result = Invoke-DataQuery $mem "select * from Test"
51 |
52 | $result | Should -BeOfType [PSObject]
53 | $result[0].a | Should -Be 10
54 | $result[0].b | Should -Be 'xxx'
55 | $result[0].c | Should -Be $null
56 | }
57 |
58 | It "can take parameters as single object" {
59 |
60 | $result = Invoke-DataQuery $mem "insert into Test (a, b, c) values (?, 'xxx', 3)" 999
61 | $result | Should -BeNull
62 | Get-DataQueryResult | Should -Be 1
63 |
64 | $result = Invoke-DataQuery $mem "select * from Test"
65 |
66 | $result | Should -BeOfType [PSObject]
67 | $result[0].a | Should -Be 999
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/docs/New-DataConnectionString.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # New-DataConnectionString
9 |
10 | ## SYNOPSIS
11 |
12 | Creates a connection string based on the given parameters.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | New-DataConnectionString [-ProviderName] [-Parameters] [-TestConnection]
18 | []
19 | ```
20 |
21 | ## DESCRIPTION
22 |
23 | The New-DataConnectionString cmdlet creates a connection string based on parameters for a specific database provider.
24 |
25 | When the -TestConnection parameter is specified, the cmdlet tries to connect to a database with a newly created connection string.
26 |
27 | ## EXAMPLES
28 |
29 | ## PARAMETERS
30 |
31 | ### -Parameters
32 |
33 | A set of parameters that should be included in a connection string.
34 |
35 | ```yaml
36 | Type: Hashtable
37 | Parameter Sets: (All)
38 | Aliases:
39 |
40 | Required: True
41 | Position: 1
42 | Default value: None
43 | Accept pipeline input: False
44 | Accept wildcard characters: False
45 | ```
46 |
47 | ### -ProviderName
48 |
49 | A database provider name.
50 |
51 | ```yaml
52 | Type: String
53 | Parameter Sets: (All)
54 | Aliases:
55 |
56 | Required: True
57 | Position: 0
58 | Default value: None
59 | Accept pipeline input: False
60 | Accept wildcard characters: False
61 | ```
62 |
63 | ### -TestConnection
64 |
65 | Makes the cmdlet to test connectivity of a generated connection string.
66 |
67 | ```yaml
68 | Type: SwitchParameter
69 | Parameter Sets: (All)
70 | Aliases:
71 |
72 | Required: False
73 | Position: 2
74 | Default value: None
75 | Accept pipeline input: False
76 | Accept wildcard characters: False
77 | ```
78 |
79 | ### CommonParameters
80 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
81 |
82 | ## INPUTS
83 |
84 | ### None
85 |
86 | ## OUTPUTS
87 |
88 | ### System.String
89 |
90 | ## NOTES
91 |
92 | ## RELATED LINKS
93 |
--------------------------------------------------------------------------------
/docs/Get-DataSchema.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Get-DataSchema
9 |
10 | ## SYNOPSIS
11 |
12 | Gets database schema information.
13 |
14 | ## SYNTAX
15 |
16 | ### FileOrName
17 | ```
18 | Get-DataSchema [-FileOrName] [[-CollectionName] ] []
19 | ```
20 |
21 | ### Connection
22 | ```
23 | Get-DataSchema [-Connection] [[-CollectionName] ] []
24 | ```
25 |
26 | ## DESCRIPTION
27 |
28 | The Get-DataSchema cmdlet gets database schema information that the database engine provides. If a schema collection name (-CollectionName) is not specified, it returns the information of all available schemas. The provided information varies among database products.
29 |
30 | ## EXAMPLES
31 |
32 | ## PARAMETERS
33 |
34 | ### -CollectionName
35 |
36 | A schama collection name.
37 |
38 | ```yaml
39 | Type: String
40 | Parameter Sets: (All)
41 | Aliases:
42 |
43 | Required: False
44 | Position: 1
45 | Default value: None
46 | Accept pipeline input: False
47 | Accept wildcard characters: False
48 | ```
49 |
50 | ### -Connection
51 |
52 | A database connection.
53 |
54 | ```yaml
55 | Type: DbConnection
56 | Parameter Sets: Connection
57 | Aliases:
58 |
59 | Required: True
60 | Position: 0
61 | Default value: None
62 | Accept pipeline input: False
63 | Accept wildcard characters: False
64 | ```
65 |
66 | ### -FileOrName
67 |
68 | A database file name or a connection string name.
69 |
70 | ```yaml
71 | Type: String
72 | Parameter Sets: FileOrName
73 | Aliases:
74 |
75 | Required: True
76 | Position: 0
77 | Default value: None
78 | Accept pipeline input: False
79 | Accept wildcard characters: False
80 | ```
81 |
82 | ### CommonParameters
83 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
84 |
85 | ## INPUTS
86 |
87 | ### None
88 |
89 | ## OUTPUTS
90 |
91 | ### System.Data.DataRow
92 |
93 | ## NOTES
94 |
95 | ## RELATED LINKS
96 |
--------------------------------------------------------------------------------
/docs/Register-DbProviderFactory.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Register-DbProviderFactory
9 |
10 | ## SYNOPSIS
11 |
12 | Registers a database provider factory to the ConfigurationManager.
13 |
14 | ## SYNTAX
15 |
16 | ```
17 | Register-DbProviderFactory [-Name] [-Invariant] [-Description] [-Type]
18 | []
19 | ```
20 |
21 | ## DESCRIPTION
22 |
23 | The Register-DbProviderFactory cmdlet registers a database provider factory to the ConfigurationManager.
24 |
25 | ## EXAMPLES
26 |
27 | ## PARAMETERS
28 |
29 | ### -Description
30 |
31 | A human readable description.
32 |
33 | ```yaml
34 | Type: String
35 | Parameter Sets: (All)
36 | Aliases:
37 |
38 | Required: True
39 | Position: 2
40 | Default value: None
41 | Accept pipeline input: False
42 | Accept wildcard characters: False
43 | ```
44 |
45 | ### -Invariant
46 |
47 | An invariant name.
48 |
49 | ```yaml
50 | Type: String
51 | Parameter Sets: (All)
52 | Aliases:
53 |
54 | Required: True
55 | Position: 1
56 | Default value: None
57 | Accept pipeline input: False
58 | Accept wildcard characters: False
59 | ```
60 |
61 | ### -Name
62 |
63 | A database provider name.
64 |
65 | ```yaml
66 | Type: String
67 | Parameter Sets: (All)
68 | Aliases:
69 |
70 | Required: True
71 | Position: 0
72 | Default value: None
73 | Accept pipeline input: False
74 | Accept wildcard characters: False
75 | ```
76 |
77 | ### -Type
78 |
79 | An assembly qualified name of the provider's factory class.
80 |
81 | ```yaml
82 | Type: String
83 | Parameter Sets: (All)
84 | Aliases:
85 |
86 | Required: True
87 | Position: 3
88 | Default value: None
89 | Accept pipeline input: False
90 | Accept wildcard characters: False
91 | ```
92 |
93 | ### CommonParameters
94 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
95 |
96 | ## INPUTS
97 |
98 | ### None
99 |
100 | ## OUTPUTS
101 |
102 | ### System.Void
103 |
104 | ## NOTES
105 |
106 | ## RELATED LINKS
107 |
--------------------------------------------------------------------------------
/source/dataquery.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29709.97
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Horker.Data.Tests", "Horker.Data.Tests\Horker.Data.Tests.csproj", "{55113E39-5E2F-4707-AC7C-46301DB5EFA2}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Horker.Data", "Horker.Data\Horker.Data.csproj", "{68260625-65C8-47CA-ADCD-2764A55F7796}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Debug|x64 = Debug|x64
14 | Release|Any CPU = Release|Any CPU
15 | Release|x64 = Release|x64
16 | EndGlobalSection
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
18 | {55113E39-5E2F-4707-AC7C-46301DB5EFA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {55113E39-5E2F-4707-AC7C-46301DB5EFA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {55113E39-5E2F-4707-AC7C-46301DB5EFA2}.Debug|x64.ActiveCfg = Debug|Any CPU
21 | {55113E39-5E2F-4707-AC7C-46301DB5EFA2}.Debug|x64.Build.0 = Debug|Any CPU
22 | {55113E39-5E2F-4707-AC7C-46301DB5EFA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {55113E39-5E2F-4707-AC7C-46301DB5EFA2}.Release|Any CPU.Build.0 = Release|Any CPU
24 | {55113E39-5E2F-4707-AC7C-46301DB5EFA2}.Release|x64.ActiveCfg = Release|Any CPU
25 | {55113E39-5E2F-4707-AC7C-46301DB5EFA2}.Release|x64.Build.0 = Release|Any CPU
26 | {68260625-65C8-47CA-ADCD-2764A55F7796}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {68260625-65C8-47CA-ADCD-2764A55F7796}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {68260625-65C8-47CA-ADCD-2764A55F7796}.Debug|x64.ActiveCfg = Debug|x64
29 | {68260625-65C8-47CA-ADCD-2764A55F7796}.Debug|x64.Build.0 = Debug|x64
30 | {68260625-65C8-47CA-ADCD-2764A55F7796}.Release|Any CPU.ActiveCfg = Release|Any CPU
31 | {68260625-65C8-47CA-ADCD-2764A55F7796}.Release|Any CPU.Build.0 = Release|Any CPU
32 | {68260625-65C8-47CA-ADCD-2764A55F7796}.Release|x64.ActiveCfg = Release|x64
33 | {68260625-65C8-47CA-ADCD-2764A55F7796}.Release|x64.Build.0 = Release|x64
34 | EndGlobalSection
35 | GlobalSection(SolutionProperties) = preSolution
36 | HideSolutionNode = FALSE
37 | EndGlobalSection
38 | GlobalSection(ExtensibilityGlobals) = postSolution
39 | SolutionGuid = {C9C90415-35AA-4028-8C28-5AE5BDFF44E1}
40 | EndGlobalSection
41 | EndGlobal
42 |
--------------------------------------------------------------------------------
/source/Horker.Data/Cmdlets/Schema.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Data;
3 | using System.Data.Common;
4 | using System.Management.Automation;
5 |
6 | #pragma warning disable CS1591
7 |
8 | namespace Horker.Data
9 | {
10 | ///
11 | /// Gets database schema information.
12 | /// The Get-DataSchema cmdlet gets database schema information that the database engine provides. If a schema collection name (-CollectionName) is not specified, it returns the information of all available schemas. The provided information varies among database products.
13 | ///
14 | [Cmdlet("Get", "DataSchema")]
15 | [OutputType(typeof(DataRow))]
16 | public class GetDataSchema : PSCmdlet
17 | {
18 | ///
19 | /// A database file name or a connection string name.
20 | ///
21 | [Parameter(Position = 0, ParameterSetName = "FileOrName", Mandatory = true)]
22 | public string FileOrName { get; set; }
23 |
24 | ///
25 | /// A database connection.
26 | ///
27 | [Parameter(Position = 0, ParameterSetName = "Connection", Mandatory = true)]
28 | public DbConnection Connection { get; set; }
29 |
30 | ///
31 | /// A schama collection name.
32 | ///
33 | [Parameter(Position = 1, Mandatory = false)]
34 | public string CollectionName { get; set; }
35 |
36 | protected override void EndProcessing()
37 | {
38 | var opener = new ConnectionSpecifier(FileOrName, Connection, null, null);
39 | var connection = opener.Connection;
40 | var connectionOpen = opener.ConnectionOpened;
41 |
42 | try
43 | {
44 | DataTable schema;
45 | if (CollectionName != null && CollectionName != "")
46 | schema = connection.GetSchema(CollectionName);
47 | else
48 | schema = connection.GetSchema();
49 |
50 | try
51 | {
52 | foreach (var row in schema.Rows)
53 | WriteObject(row);
54 | }
55 | finally
56 | {
57 | schema.Dispose();
58 | }
59 | }
60 | catch (Exception ex)
61 | {
62 | WriteError(new ErrorRecord(ex, "", ErrorCategory.NotSpecified, null));
63 | }
64 | finally
65 | {
66 | if (connectionOpen)
67 | {
68 | connection.Close();
69 | connection.Dispose();
70 | }
71 | }
72 | }
73 | }
74 | }
--------------------------------------------------------------------------------
/source/Horker.Data/Classes/Helpers.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using System.Data.Common;
5 | using System.Linq;
6 | using System.Management.Automation;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace Horker.Data.Classes
11 | {
12 | internal static class Helpers
13 | {
14 | public static void SetParameters(DbCommand cmd, object parameters)
15 | {
16 | if (parameters == null)
17 | return;
18 |
19 | if (parameters is IDictionary dictParam)
20 | {
21 | foreach (DictionaryEntry entry in dictParam)
22 | {
23 | object value;
24 | if (entry.Value is PSObject psobj)
25 | value = psobj.BaseObject;
26 | else
27 | value = entry.Value;
28 |
29 | var param = cmd.CreateParameter();
30 | param.ParameterName = (string)entry.Key;
31 | param.Value = value;
32 | cmd.Parameters.Add(param);
33 | }
34 | }
35 | else if (parameters is IEnumerable enumParam)
36 | {
37 | foreach (var v in enumParam)
38 | {
39 | object value;
40 | if (v is PSObject psobj)
41 | value = psobj.BaseObject;
42 | else
43 | value = v;
44 |
45 | var param = cmd.CreateParameter();
46 | param.Value = value;
47 | cmd.Parameters.Add(param);
48 | }
49 | }
50 | else
51 | throw new ArgumentException("Query parameters must be a IDictionary or an IEnumerable");
52 | }
53 |
54 | public static DbProviderFactory GetDbProviderFactory(DbConnection connection)
55 | {
56 | DbProviderFactory factory = null;
57 |
58 | // ODBC, OLEDB and SQLite connections fail to obtain the corresponding factories.
59 | if (connection is System.Data.Odbc.OdbcConnection)
60 | {
61 | factory = DbProviderFactories.GetFactory("System.Data.Odbc");
62 | }
63 | else if (connection is System.Data.OleDb.OleDbConnection)
64 | {
65 | factory = DbProviderFactories.GetFactory("System.Data.OleDb");
66 | }
67 | else if (connection is System.Data.SQLite.SQLiteConnection)
68 | {
69 | factory = DbProviderFactories.GetFactory("System.Data.SQLite");
70 | }
71 | else
72 | {
73 | factory = DbProviderFactories.GetFactory(connection);
74 | }
75 |
76 | if (factory == null)
77 | throw new RuntimeException("Failed to obtain a DbProviderFactory object");
78 |
79 | return factory;
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/source/Horker.Data/Classes/ConnectionSetting.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Configuration;
3 | using System.Data.Common;
4 | using System.IO;
5 | using System.Management.Automation;
6 |
7 | #pragma warning disable CS1591
8 |
9 | namespace Horker.Data
10 | {
11 | public abstract class ConnectionSetting
12 | {
13 | public abstract DbConnection GetConnection();
14 | }
15 |
16 | public class ProviderConnectionSetting : ConnectionSetting
17 | {
18 | public string ProviderName { get; set; }
19 | public string ConnectionString { get; set; }
20 |
21 | public ProviderConnectionSetting(string providerName, string connectionString)
22 | {
23 | ProviderName = providerName;
24 | ConnectionString = connectionString;
25 | }
26 |
27 | public override DbConnection GetConnection()
28 | {
29 | DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName);
30 |
31 | var conn = factory.CreateConnection();
32 | conn.ConnectionString = ConnectionString;
33 | conn.Open();
34 |
35 | return conn;
36 | }
37 | }
38 |
39 | public class FileConnectionSetting : ConnectionSetting
40 | {
41 | public string FileOrName { get; private set; }
42 |
43 | public FileConnectionSetting(string fileOrName)
44 | {
45 | FileOrName = fileOrName;
46 | }
47 |
48 | public override DbConnection GetConnection()
49 | {
50 | // Look up configuration
51 | var cs = ConfigurationManager.ConnectionStrings[FileOrName];
52 | if (cs != null)
53 | return new ProviderConnectionSetting(cs.ProviderName, cs.ConnectionString).GetConnection();
54 |
55 | // Access file?
56 | if (FileOrName.EndsWith(".accdb") || FileOrName.EndsWith(".mdb"))
57 | {
58 | var builder = new System.Data.Odbc.OdbcConnectionStringBuilder();
59 | builder.Add("Driver", "{Microsoft Access Driver (*.mdb, *.accdb)}");
60 | builder.Add("Dbq", FileOrName);
61 |
62 | var conn = new System.Data.Odbc.OdbcConnection();
63 | conn.ConnectionString = builder.ConnectionString;
64 | conn.Open();
65 |
66 | return conn;
67 | }
68 |
69 | // Assume a SQLite file
70 | // Restricts to an exsiting file to prevent mistyping from creating a new database
71 | if (File.Exists(FileOrName))
72 | {
73 | var builder = new System.Data.SQLite.SQLiteConnectionStringBuilder();
74 | builder.Add("Data Source", FileOrName);
75 |
76 | var conn = new System.Data.SQLite.SQLiteConnection();
77 | conn.ConnectionString = builder.ConnectionString;
78 | conn.Open();
79 |
80 | return conn;
81 | }
82 |
83 | throw new RuntimeException(String.Format("'{0}' is not a database file name nor connection string name", FileOrName));
84 | }
85 | }
86 | }
--------------------------------------------------------------------------------
/.build.ps1:
--------------------------------------------------------------------------------
1 | task . Compile, Build, ImportDebug, Test
2 |
3 | Set-StrictMode -Version 4
4 |
5 | ############################################################
6 |
7 | $SOURCE_PATH = "$PSScriptRoot\source"
8 | $SCRIPT_PATH = "$PSScriptRoot\scripts"
9 |
10 | $DLL_PATH = "$SOURCE_PATH\Horker.Data.Tests\bin\{0}\netcoreapp3.1"
11 |
12 | $MODULE_PATH = "$PSScriptRoot\module\{0}\HorkerDataQuery"
13 |
14 | $HELP_INPUT = "$SOURCE_PATH\Horker.Data.Tests\bin\Release\netcoreapp3.1\Horker.Data.dll"
15 | $HELP_INTERM = "$SOURCE_PATH\Horker.Data.Tests\bin\Release\netcoreapp3.1\Horker.Data.dll-Help.xml"
16 | $HELP_OUTPUT = "$MODULE_PATH\Horker.Data.dll-Help.xml" -f "Release"
17 |
18 | $HELPGEN = "$PSScriptRoot\vendor\XmlDoc2CmdletDoc.0.2.13\tools\XmlDoc2CmdletDoc.exe"
19 |
20 | $OBJECT_FILES = @(
21 | "Horker.Data.dll"
22 | "Horker.Data.pdb"
23 | "System.Data.Odbc.dll"
24 | "System.Data.OleDb.dll"
25 | "System.Data.SqlClient.dll"
26 | "System.Data.SQLite.dll"
27 | "Npgsql.dll"
28 | )
29 |
30 | $NATIVE_PATH = "$PSScriptRoot\vendor\System.Data.SQLite.Core.1.0.112.0\runtimes\win-x64\native\netstandard2.0"
31 |
32 | ############################################################
33 |
34 | function New-FolderSkip {
35 | param(
36 | [string]$Path
37 | )
38 |
39 | try {
40 | $null = New-Item -Type Directory $Path -EA Stop
41 | Write-Host -ForegroundColor DarkCyan $Path
42 | }
43 | catch {
44 | Write-Host -ForegroundColor DarkYellow $_
45 | }
46 | }
47 |
48 | function Copy-ItemSkip {
49 | param(
50 | [string]$Source,
51 | [string]$Dest
52 | )
53 |
54 | try {
55 | Copy-Item $Source $Dest
56 | Write-Host -ForegroundColor DarkCyan "$Source => $Dest"
57 | }
58 | catch {
59 | Write-Host -ForegroundColor DarkYellow $_
60 | }
61 | }
62 |
63 | ############################################################
64 |
65 | task Compile {
66 | dotnet build "$SOURCE_PATH\dataquery.sln" -c Debug -nologo -v minimal
67 | dotnet build "$SOURCE_PATH\dataquery.sln" -c Release -nologo -v minimal
68 | }
69 |
70 | task Build {
71 |
72 | . {
73 | $ErrorActionPreference = "Continue"
74 |
75 | "Release", "Debug" | foreach {
76 | $modulePath = $MODULE_PATH -f $_
77 | $dllPath = $DLL_PATH -f "Release"
78 |
79 | New-FolderSkip "$modulePath\x64"
80 |
81 | Copy-ItemSkip "$SCRIPT_PATH\*" $modulePath
82 |
83 | foreach ($f in $OBJECT_FILES) {
84 | Copy-ItemSkip "$dllPath\$f" $modulePath
85 | }
86 |
87 | Copy-ItemSkip "$NATIVE_PATH\*" "$modulePath\x64\"
88 | }
89 | }
90 | }
91 |
92 | task UpdateHelp {
93 | Update-MarkdownHelp HorkerDataQuery $PSScriptRoot\docs
94 | }
95 | task BuildHelp {
96 | $outputFolder = "$PSScriptRoot\module\release\HorkerDataQuery\en-US"
97 | New-ExternalHelp -Path $PSScriptRoot\docs -OutputPath $outputFolder
98 | Copy-Item $outputFolder\* $PSScriptRoot\module\debug\HorkerDataQuery\en-US\
99 | }
100 |
101 | task Test {
102 | Invoke-Pester "$PSScriptRoot\tests"
103 | }
104 |
105 | task ImportDebug {
106 | Import-Module ($MODULE_PATH -f "Debug") -Force
107 | }
108 |
109 | task Clean {
110 | "Release", "Debug" | foreach {
111 | $modulePath = $MODULE_PATH -f $_
112 | Remove-Item "$modulePath\*" -Force -Recurse -EA Continue
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/docs/Export-DataTable.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: Horker.Data.dll-Help.xml
3 | Module Name: HorkerDataQuery
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Export-DataTable
9 |
10 | ## SYNOPSIS
11 |
12 | Inserts objects as data rows into a database table.
13 |
14 | ## SYNTAX
15 |
16 | ### FileOrName
17 | ```
18 | Export-DataTable [-InputObject