Skip to content

Commit 871faba

Browse files
committed
Refactored AdoAdapter into its own project/assembly
1 parent bc94b55 commit 871faba

File tree

61 files changed

+2223
-48
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2223
-48
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Simple.Data.Ado.Test")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("Simple.Data.Ado.Test")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("0c1c9ef1-f025-4216-92f4-23eb9ff648a7")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{96C623FD-869E-4DCE-98A1-17075EE384F9}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Simple.Data.Ado.Test</RootNamespace>
12+
<AssemblyName>Simple.Data.Ado.Test</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="nunit.framework, Version=2.5.7.10213, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
35+
<SpecificVersion>False</SpecificVersion>
36+
<HintPath>..\packages\NUnit.2.5.7.10213\lib\nunit.framework.dll</HintPath>
37+
</Reference>
38+
<Reference Include="nunit.mocks, Version=2.5.7.10213, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
39+
<SpecificVersion>False</SpecificVersion>
40+
<HintPath>..\packages\NUnit.2.5.7.10213\lib\nunit.mocks.dll</HintPath>
41+
</Reference>
42+
<Reference Include="pnunit.framework">
43+
<HintPath>..\packages\NUnit.2.5.7.10213\lib\pnunit.framework.dll</HintPath>
44+
</Reference>
45+
<Reference Include="System" />
46+
<Reference Include="System.Core" />
47+
<Reference Include="System.Xml.Linq" />
48+
<Reference Include="System.Data.DataSetExtensions" />
49+
<Reference Include="Microsoft.CSharp" />
50+
<Reference Include="System.Data" />
51+
<Reference Include="System.Xml" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
<Compile Include="TableCollectionTest.cs" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<None Include="packages.config" />
59+
</ItemGroup>
60+
<ItemGroup>
61+
<ProjectReference Include="..\Simple.Data.Ado\Simple.Data.Ado.csproj">
62+
<Project>{ECC2D7DB-EC7F-44B6-B09F-5B471C629685}</Project>
63+
<Name>Simple.Data.Ado</Name>
64+
</ProjectReference>
65+
<ProjectReference Include="..\Simple.Data\Simple.Data.csproj">
66+
<Project>{148CEE80-2E84-4ABD-B5AB-20415B2BBD21}</Project>
67+
<Name>Simple.Data</Name>
68+
</ProjectReference>
69+
</ItemGroup>
70+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
71+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
72+
Other similar extension points exist, see Microsoft.Common.targets.
73+
<Target Name="BeforeBuild">
74+
</Target>
75+
<Target Name="AfterBuild">
76+
</Target>
77+
-->
78+
</Project>

Simple.Data.UnitTest/TableCollectionTest.cs renamed to Simple.Data.Ado.Test/TableCollectionTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Simple.Data.UnitTest
66
{
7-
8-
7+
8+
99
/// <summary>
1010
///This is a test class for TableCollection and is intended
1111
///to contain all TableCollection Unit Tests
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="NUnit" version="2.5.7.10213" />
4+
</packages>

Simple.Data.Ado/AdoAdapter.cs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.Composition;
4+
using System.Data;
5+
using System.Data.Common;
6+
using System.Dynamic;
7+
using System.Linq;
8+
using System.Text;
9+
using Simple.Data.Ado.Schema;
10+
11+
namespace Simple.Data.Ado
12+
{
13+
[Export("Ado", typeof(Adapter))]
14+
internal class AdoAdapter : Adapter, IAdapterWithRelation
15+
{
16+
private IConnectionProvider _connectionProvider;
17+
private DatabaseSchema _schema;
18+
private Lazy<AdoAdapterRelatedFinder> _relatedFinder;
19+
20+
public AdoAdapter()
21+
{
22+
23+
}
24+
25+
internal AdoAdapter(IConnectionProvider connectionProvider)
26+
{
27+
_connectionProvider = connectionProvider;
28+
_schema = DatabaseSchema.Get(_connectionProvider);
29+
_relatedFinder = new Lazy<AdoAdapterRelatedFinder>(CreateRelatedFinder);
30+
}
31+
32+
protected override void OnSetup()
33+
{
34+
var settingsKeys = ((IDictionary<string, object>) Settings).Keys;
35+
if (settingsKeys.Contains("ConnectionString"))
36+
{
37+
_connectionProvider = ProviderHelper.GetProviderByConnectionString(Settings.ConnectionString);
38+
}
39+
else if (settingsKeys.Contains("Filename"))
40+
{
41+
_connectionProvider = ProviderHelper.GetProviderByFilename(Settings.Filename);
42+
}
43+
_schema = DatabaseSchema.Get(_connectionProvider);
44+
_relatedFinder = new Lazy<AdoAdapterRelatedFinder>(CreateRelatedFinder);
45+
}
46+
47+
private AdoAdapterRelatedFinder CreateRelatedFinder()
48+
{
49+
return new AdoAdapterRelatedFinder(this);
50+
}
51+
52+
public override IEnumerable<IDictionary<string, object>> Find(string tableName, SimpleExpression criteria)
53+
{
54+
return new AdoAdapterFinder(this).Find(tableName, criteria);
55+
}
56+
57+
public override IDictionary<string, object> Insert(string tableName, IDictionary<string, object> data)
58+
{
59+
return new AdoAdapterInserter(this).Insert(tableName, data);
60+
}
61+
62+
public override int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria)
63+
{
64+
var commandBuilder = new UpdateHelper(_schema).GetUpdateCommand(tableName, data, criteria);
65+
return Execute(commandBuilder);
66+
}
67+
68+
/// <summary>
69+
/// Deletes from the specified table.
70+
/// </summary>
71+
/// <param name="tableName">Name of the table.</param>
72+
/// <param name="criteria">The expression to use as criteria for the delete operation.</param>
73+
/// <returns>The number of records which were deleted.</returns>
74+
public override int Delete(string tableName, SimpleExpression criteria)
75+
{
76+
var commandBuilder = new DeleteHelper(_schema).GetDeleteCommand(tableName, criteria);
77+
return Execute(commandBuilder);
78+
}
79+
80+
/// <summary>
81+
/// Gets the names of the fields which comprise the unique identifier for the specified table.
82+
/// </summary>
83+
/// <param name="tableName">Name of the table.</param>
84+
/// <returns>A list of field names; an empty list if no key is defined.</returns>
85+
public override IEnumerable<string> GetKeyFieldNames(string tableName)
86+
{
87+
return _schema.FindTable(tableName).PrimaryKey.AsEnumerable();
88+
}
89+
90+
private int Execute(ICommandBuilder commandBuilder)
91+
{
92+
using (var connection = CreateConnection())
93+
{
94+
using (var command = commandBuilder.GetCommand(connection))
95+
{
96+
return TryExecute(connection, command);
97+
}
98+
}
99+
}
100+
101+
private static int TryExecute(DbConnection connection, IDbCommand command)
102+
{
103+
try
104+
{
105+
connection.Open();
106+
return command.ExecuteNonQuery();
107+
}
108+
catch (DbException ex)
109+
{
110+
throw new AdoAdapterException(ex.Message, command);
111+
}
112+
}
113+
114+
internal DbConnection CreateConnection()
115+
{
116+
return _connectionProvider.CreateConnection();
117+
}
118+
119+
internal DatabaseSchema GetSchema()
120+
{
121+
return DatabaseSchema.Get(_connectionProvider);
122+
}
123+
124+
/// <summary>
125+
/// Determines whether a relation is valid.
126+
/// </summary>
127+
/// <param name="tableName">Name of the known table.</param>
128+
/// <param name="relatedTableName">Name of the table to test.</param>
129+
/// <returns>
130+
/// <c>true</c> if there is a valid relation; otherwise, <c>false</c>.
131+
/// </returns>
132+
public bool IsValidRelation(string tableName, string relatedTableName)
133+
{
134+
return _relatedFinder.Value.IsValidRelation(tableName, relatedTableName);
135+
}
136+
137+
/// <summary>
138+
/// Finds data from a "table" related to the specified "table".
139+
/// </summary>
140+
/// <param name="tableName">Name of the table.</param>
141+
/// <param name="row"></param>
142+
/// <param name="relatedTableName"></param>
143+
/// <returns>The list of records matching the criteria. If no records are found, return an empty list.</returns>
144+
/// <remarks>When implementing the <see cref="Adapter"/> interface, if relationships are not possible, throw a <see cref="NotSupportedException"/>.</remarks>
145+
public IEnumerable<IDictionary<string, object>> FindRelated(string tableName, IDictionary<string, object> row, string relatedTableName)
146+
{
147+
return _relatedFinder.Value.FindRelated(tableName, row, relatedTableName);
148+
}
149+
}
150+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
5+
using System.Runtime.Serialization;
6+
using System.Text;
7+
using Simple.Data.Extensions;
8+
9+
namespace Simple.Data.Ado
10+
{
11+
public class AdoAdapterException : AdapterException
12+
{
13+
private readonly string _commandText;
14+
private readonly IDictionary<string,object> _parameters;
15+
16+
public AdoAdapterException() : base(typeof(AdoAdapter))
17+
{
18+
}
19+
20+
public AdoAdapterException(string message, IDbCommand command) : base(message, typeof(AdoAdapter))
21+
{
22+
_commandText = command.CommandText;
23+
_parameters = command.Parameters.Cast<IDbDataParameter>()
24+
.ToDictionary(p => p.ParameterName, p => p.Value);
25+
}
26+
27+
public AdoAdapterException(string commandText, IEnumerable<KeyValuePair<string,object>> parameters)
28+
:base(typeof(AdoAdapter))
29+
{
30+
_commandText = commandText;
31+
_parameters = parameters.ToDictionary();
32+
}
33+
34+
35+
public AdoAdapterException(string message) : base(message, typeof(AdoAdapter))
36+
{
37+
}
38+
39+
public AdoAdapterException(string message, string commandText, IEnumerable<KeyValuePair<string,object>> parameters)
40+
:base(message, typeof(AdoAdapter))
41+
{
42+
_commandText = commandText;
43+
_parameters = parameters.ToDictionary();
44+
}
45+
46+
public AdoAdapterException(string message, Exception inner) : base(message, inner, typeof(AdoAdapter))
47+
{
48+
}
49+
50+
public AdoAdapterException(SerializationInfo info, StreamingContext context) : base(info, context)
51+
{
52+
}
53+
54+
public IDictionary<string, object> Parameters
55+
{
56+
get { return _parameters; }
57+
}
58+
59+
public string CommandText
60+
{
61+
get { return _commandText; }
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)