-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ODBC connection to MS Access (#815)
* feat: ODBC connection to MS Access * docs: update automatically generated documentation --------- Co-authored-by: AppVeyor bot <[email protected]>
- Loading branch information
Showing
13 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
DubUrl.Core/Locating/OdbcDriver/Implementation/MsAccessDriverLocator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using DubUrl.Locating.RegexUtils; | ||
using DubUrl.Mapping.Database; | ||
using DubUrl.Mapping.Implementation; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace DubUrl.Locating.OdbcDriver; | ||
|
||
[Driver<MsAccessDriverRegex, OdbcDbqMapper, MsAccessDatabase>()] | ||
public class MsAccessDriverLocator : BaseDriverLocator | ||
{ | ||
internal class MsAccessDriverRegex : BaseDriverRegex | ||
{ | ||
public MsAccessDriverRegex() | ||
: base( | ||
[ | ||
new WordMatch("Microsoft Access Driver"), | ||
new SpaceMatch(), | ||
new LiteralMatch("(*.mdb, *.accdb)"), | ||
]) | ||
{ } | ||
} | ||
private List<string> Candidates { get; } = []; | ||
|
||
public MsAccessDriverLocator() | ||
: base(GetRegexPattern<MsAccessDriverLocator>()) { } | ||
internal MsAccessDriverLocator(DriverLister driverLister) | ||
: base(GetRegexPattern<MsAccessDriverLocator>(), driverLister) { } | ||
|
||
|
||
protected override void AddCandidate(string driver, string[] matches) | ||
=> Candidates.Add(driver); | ||
|
||
protected override List<string> RankCandidates() | ||
=> Candidates; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using DubUrl.Querying.Dialects; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Common; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DubUrl.Mapping.Database; | ||
|
||
[Database<MsAccessDialect>( | ||
"Microsoft Access" | ||
, ["accdb", "access", "msaccess"] | ||
, DatabaseCategory.FileBased | ||
)] | ||
[Brand("microsoftaccess", "#217346")] | ||
public class MsAccessDatabase : IDatabase | ||
{ } |
15 changes: 15 additions & 0 deletions
15
DubUrl.Core/Querying/Dialects/Formatters/DateCrossSurroundingFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DubUrl.Querying.Dialects.Formatters; | ||
|
||
internal class DateCrossSurroundingFormatter : IValueFormatter<DateOnly> | ||
{ | ||
public string Format(DateOnly value) | ||
=> $"#{value:MM/dd/yyyy}#"; | ||
public string Format(object obj) | ||
=> obj is DateOnly value ? Format(value) : throw new Exception(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using DubUrl.Querying.Dialects.Casters; | ||
using DubUrl.Querying.Dialects.Renderers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DubUrl.Querying.Dialects; | ||
|
||
[Renderer<MsAccessRenderer>()] | ||
[ParentLanguage<SqlLanguage>] | ||
public class MsAccessDialect : BaseDialect | ||
{ | ||
internal MsAccessDialect(ILanguage language, string[] aliases, IRenderer renderer, ICaster[] casters) | ||
: base(language, aliases, renderer, casters) { } | ||
} |
20 changes: 20 additions & 0 deletions
20
DubUrl.Core/Querying/Dialects/Renderers/MsAccessRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using DubUrl.Querying.Dialects.Formatters; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DubUrl.Querying.Dialects.Renderers; | ||
|
||
internal class MsAccessRenderer : AnsiRenderer | ||
{ | ||
public MsAccessRenderer() | ||
: base(new ValueFormatter() | ||
.With(new DateCrossSurroundingFormatter()) | ||
, new NullFormatter() | ||
, new IdentifierSquareBracketFormatter()) { } | ||
|
||
protected MsAccessRenderer(BaseValueFormatter value) | ||
: base(value, new NullFormatter(), new IdentifierBacktickFormatter()) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using NUnit.Framework; | ||
using System.Diagnostics; | ||
using System.Data; | ||
using System.Data.Common; | ||
using DubUrl.Registering; | ||
using System.Data.Odbc; | ||
using System.Drawing; | ||
|
||
namespace DubUrl.QA.MsAccess; | ||
|
||
[Category("MsAccess")] | ||
[FixtureLifeCycle(LifeCycle.SingleInstance)] | ||
public class OdbcDriverMsAccess : BaseOdbcDriver | ||
{ | ||
protected string CurrentDirectory | ||
=> Path.GetDirectoryName(GetType().Assembly.Location) + "\\"; | ||
|
||
public override string ConnectionString | ||
=> $"odbc+accdb:///MsAccess/DubUrl.accdb?Defaultdir={CurrentDirectory}"; | ||
|
||
[Test] | ||
public override void QueryCustomer() | ||
=> QueryCustomer("select [FullName] from [Customer] where [CustomerId]=1"); | ||
|
||
[Test] | ||
public override void QueryCustomerWithParams() | ||
=> QueryCustomerWithParams("select [FullName] from [Customer] where [CustomerId]=?"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Param( | ||
[switch] $force=$false | ||
, [string] $config = "Release" | ||
, [string[]] $frameworks = @("net6.0", "net7.0") | ||
) | ||
. $PSScriptRoot\..\Run-TestSuite.ps1 | ||
|
||
if ($force) { | ||
Write-Host "Enforcing QA testing for Microsoft Access" | ||
} | ||
|
||
$filesChanged = & git diff --name-only HEAD HEAD~1 | ||
if ($force -or ($filesChanged -like "*access*")) { | ||
Write-Host "Deploying Microsoft Access testing environment" | ||
|
||
# Installing ODBC driver | ||
Write-host "`tDeploying Microsoft Access ODBC drivers" | ||
$drivers = Get-OdbcDriver -Name "*accdb*" -Platform "64-bit" | ||
|
||
If ($drivers.Length -eq 0) { | ||
Write-Host "`t`tDownloading Microsoft Access ODBC driver ..." | ||
Invoke-WebRequest "https://download.microsoft.com/download/3/5/C/35C84C36-661A-44E6-9324-8786B8DBE231/accessdatabaseengine_X64.exe" -OutFile "$env:temp\accessdatabaseengine_X64.exe" | ||
Write-Host "`t`tInstalling Microsoft Access ODBC driver ..." | ||
& cmd /c start /wait "$env:temp\accessdatabaseengine_X64.exe" /quiet | ||
Write-Host "`t`tChecking installation ..." | ||
Get-OdbcDriver -Name "*accdb*" | ||
Write-Host "`tDeployment of Microsoft Access ODBC driver finalized." | ||
} else { | ||
Write-Host "`t`tDrivers already installed:" | ||
Get-OdbcDriver -Name "*accdb*" -Platform "64-bit" | ||
Write-Host "`t`tSkipping installation of new drivers" | ||
} | ||
|
||
# Running QA tests | ||
Write-Host "Running QA tests related to Microsoft Access" | ||
$testSuccessful = Run-TestSuite @("MsAccess") -config $config -frameworks $frameworks | ||
|
||
# Raise failing tests | ||
exit $testSuccessful | ||
} else { | ||
return -1 | ||
} |
58 changes: 58 additions & 0 deletions
58
DubUrl.Testing/Locating/OdbcDriver/Implementation/MsAccessDriverLocatorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using DubUrl.Locating.OdbcDriver; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DubUrl.Testing.Locating.OdbcDriver.Implementation; | ||
|
||
public class MsAccessDriverLocatorTest | ||
{ | ||
private class FakeDriverLister : DriverLister | ||
{ | ||
private string[] Drivers { get; } | ||
|
||
public FakeDriverLister(string[] drivers) | ||
=> Drivers = drivers; | ||
|
||
public override string[] List() => Drivers; | ||
} | ||
|
||
[Test] | ||
public void Locate_SingleElementMatching_ElementReturned() | ||
{ | ||
var driverLister = new FakeDriverLister(["Microsoft Access Driver (*.mdb, *.accdb)"]); | ||
var driverLocator = new MsAccessDriverLocator(driverLister); | ||
var driver = driverLocator.Locate(); | ||
Assert.That(driver, Is.EqualTo("Microsoft Access Driver (*.mdb, *.accdb)")); | ||
} | ||
|
||
[Test] | ||
public void Locate_MultipleIdenticalElementMatching_BestElementReturned() | ||
{ | ||
var driverLister = new FakeDriverLister(["Microsoft Access Driver (*.mdb, *.accdb)", "Microsoft Access Driver (*.mdb, *.accdb)"]); | ||
var driverLocator = new MsAccessDriverLocator(driverLister); | ||
var driver = driverLocator.Locate(); | ||
Assert.That(driver, Is.EqualTo("Microsoft Access Driver (*.mdb, *.accdb)")); | ||
} | ||
|
||
[Test] | ||
public void Locate_ElementNonMatching_ElementNotReturned() | ||
{ | ||
var driverLister = new FakeDriverLister(["ODBC Driver 13 for SQL Server", "Microsoft Access Driver (*.mdb, *.accdb)"]); | ||
var driverLocator = new MsAccessDriverLocator(driverLister); | ||
var driver = driverLocator.Locate(); | ||
Assert.That(driver, Is.EqualTo("Microsoft Access Driver (*.mdb, *.accdb)")); | ||
} | ||
|
||
[Test] | ||
public void Locate_NoMatching_EmptyString() | ||
{ | ||
var driverLister = new FakeDriverLister(["ODBC Driver 17 for Other Database"]); | ||
var driverLocator = new MsAccessDriverLocator(driverLister); | ||
var driver = driverLocator.Locate(); | ||
Assert.That(driver, Is.Null.Or.Empty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters