parent
acac33c217
commit
7b17c3e36c
@ -1,22 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<Dictionary>
|
||||
<Words>
|
||||
<Recognized>
|
||||
<Word>Ack</Word>
|
||||
<Word>Minifier</Word>
|
||||
<Word>Jsonp</Word>
|
||||
<Word>Linktionary</Word>
|
||||
<Word>Scaleout</Word>
|
||||
<Word>Redis</Word>
|
||||
<Word>Owin</Word>
|
||||
<Word>Stringify</Word>
|
||||
<Word>Unminify</Word>
|
||||
<Word>Unminified</Word>
|
||||
<Word>Stateful</Word>
|
||||
<Word>SignalR</Word>
|
||||
<Word>Hubservable</Word>
|
||||
<Word>Sse</Word>
|
||||
<Word>GitHub</Word>
|
||||
</Recognized>
|
||||
</Words>
|
||||
</Dictionary>
|
@ -1,5 +0,0 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System.Reflection;
|
||||
|
||||
[assembly: AssemblyVersion("10.0.0.*")]
|
@ -1,227 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="11.0">
|
||||
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
|
||||
<Rule Id="CA1000" Action="Error" />
|
||||
<Rule Id="CA1001" Action="Error" />
|
||||
<Rule Id="CA1002" Action="Error" />
|
||||
<Rule Id="CA1003" Action="Error" />
|
||||
<Rule Id="CA1004" Action="Error" />
|
||||
<Rule Id="CA1005" Action="Error" />
|
||||
<Rule Id="CA1007" Action="Error" />
|
||||
<Rule Id="CA1008" Action="Error" />
|
||||
<Rule Id="CA1010" Action="Error" />
|
||||
<Rule Id="CA1011" Action="Error" />
|
||||
<Rule Id="CA1012" Action="Error" />
|
||||
<Rule Id="CA1013" Action="Error" />
|
||||
<Rule Id="CA1014" Action="Error" />
|
||||
<Rule Id="CA1016" Action="Error" />
|
||||
<Rule Id="CA1017" Action="Error" />
|
||||
<Rule Id="CA1018" Action="Error" />
|
||||
<Rule Id="CA1019" Action="Error" />
|
||||
<Rule Id="CA1021" Action="Error" />
|
||||
<Rule Id="CA1023" Action="Error" />
|
||||
<Rule Id="CA1024" Action="Error" />
|
||||
<Rule Id="CA1025" Action="Error" />
|
||||
<Rule Id="CA1026" Action="Error" />
|
||||
<Rule Id="CA1027" Action="Error" />
|
||||
<Rule Id="CA1028" Action="Error" />
|
||||
<Rule Id="CA1030" Action="Error" />
|
||||
<Rule Id="CA1031" Action="Error" />
|
||||
<Rule Id="CA1032" Action="Error" />
|
||||
<Rule Id="CA1034" Action="Error" />
|
||||
<Rule Id="CA1035" Action="Error" />
|
||||
<Rule Id="CA1036" Action="Error" />
|
||||
<Rule Id="CA1038" Action="Error" />
|
||||
<Rule Id="CA1039" Action="Error" />
|
||||
<Rule Id="CA1040" Action="Error" />
|
||||
<Rule Id="CA1041" Action="Error" />
|
||||
<Rule Id="CA1043" Action="Error" />
|
||||
<Rule Id="CA1044" Action="Error" />
|
||||
<Rule Id="CA1045" Action="Error" />
|
||||
<Rule Id="CA1046" Action="Error" />
|
||||
<Rule Id="CA1047" Action="Error" />
|
||||
<Rule Id="CA1048" Action="Error" />
|
||||
<Rule Id="CA1049" Action="Error" />
|
||||
<Rule Id="CA1050" Action="Error" />
|
||||
<Rule Id="CA1051" Action="Error" />
|
||||
<Rule Id="CA1052" Action="Error" />
|
||||
<Rule Id="CA1053" Action="Error" />
|
||||
<Rule Id="CA1057" Action="Error" />
|
||||
<Rule Id="CA1058" Action="Error" />
|
||||
<Rule Id="CA1059" Action="Error" />
|
||||
<Rule Id="CA1060" Action="Error" />
|
||||
<Rule Id="CA1061" Action="Error" />
|
||||
<Rule Id="CA1062" Action="Error" />
|
||||
<Rule Id="CA1063" Action="Error" />
|
||||
<Rule Id="CA1064" Action="Error" />
|
||||
<Rule Id="CA1065" Action="Error" />
|
||||
<Rule Id="CA1300" Action="Error" />
|
||||
<Rule Id="CA1301" Action="Error" />
|
||||
<Rule Id="CA1302" Action="Error" />
|
||||
<Rule Id="CA1303" Action="Error" />
|
||||
<Rule Id="CA1304" Action="Error" />
|
||||
<Rule Id="CA1305" Action="Error" />
|
||||
<Rule Id="CA1306" Action="Error" />
|
||||
<Rule Id="CA1307" Action="Error" />
|
||||
<Rule Id="CA1308" Action="Error" />
|
||||
<Rule Id="CA1309" Action="Error" />
|
||||
<Rule Id="CA1400" Action="Error" />
|
||||
<Rule Id="CA1401" Action="Error" />
|
||||
<Rule Id="CA1402" Action="Error" />
|
||||
<Rule Id="CA1403" Action="Error" />
|
||||
<Rule Id="CA1404" Action="Error" />
|
||||
<Rule Id="CA1405" Action="Error" />
|
||||
<Rule Id="CA1406" Action="Error" />
|
||||
<Rule Id="CA1407" Action="Error" />
|
||||
<Rule Id="CA1408" Action="Error" />
|
||||
<Rule Id="CA1409" Action="Error" />
|
||||
<Rule Id="CA1410" Action="Error" />
|
||||
<Rule Id="CA1411" Action="Error" />
|
||||
<Rule Id="CA1412" Action="Error" />
|
||||
<Rule Id="CA1413" Action="Error" />
|
||||
<Rule Id="CA1414" Action="Error" />
|
||||
<Rule Id="CA1415" Action="Error" />
|
||||
<Rule Id="CA1500" Action="Error" />
|
||||
<Rule Id="CA1501" Action="Error" />
|
||||
<Rule Id="CA1502" Action="Error" />
|
||||
<Rule Id="CA1504" Action="Error" />
|
||||
<Rule Id="CA1505" Action="Error" />
|
||||
<Rule Id="CA1506" Action="Error" />
|
||||
<Rule Id="CA1600" Action="Error" />
|
||||
<Rule Id="CA1601" Action="Error" />
|
||||
<Rule Id="CA1700" Action="Error" />
|
||||
<Rule Id="CA1701" Action="Error" />
|
||||
<Rule Id="CA1702" Action="Error" />
|
||||
<Rule Id="CA1703" Action="Error" />
|
||||
<Rule Id="CA1704" Action="Error" />
|
||||
<Rule Id="CA1707" Action="Error" />
|
||||
<Rule Id="CA1708" Action="Error" />
|
||||
<Rule Id="CA1709" Action="Error" />
|
||||
<Rule Id="CA1712" Action="Error" />
|
||||
<Rule Id="CA1713" Action="Error" />
|
||||
<Rule Id="CA1714" Action="Error" />
|
||||
<Rule Id="CA1715" Action="Error" />
|
||||
<Rule Id="CA1716" Action="Error" />
|
||||
<Rule Id="CA1717" Action="Error" />
|
||||
<Rule Id="CA1719" Action="Error" />
|
||||
<Rule Id="CA1720" Action="Error" />
|
||||
<Rule Id="CA1721" Action="Error" />
|
||||
<Rule Id="CA1722" Action="Error" />
|
||||
<Rule Id="CA1724" Action="Error" />
|
||||
<Rule Id="CA1725" Action="Error" />
|
||||
<Rule Id="CA1726" Action="Error" />
|
||||
<Rule Id="CA1800" Action="Error" />
|
||||
<Rule Id="CA1801" Action="Error" />
|
||||
<Rule Id="CA1802" Action="Error" />
|
||||
<Rule Id="CA1804" Action="Error" />
|
||||
<Rule Id="CA1806" Action="Error" />
|
||||
<Rule Id="CA1809" Action="Error" />
|
||||
<Rule Id="CA1810" Action="Error" />
|
||||
<Rule Id="CA1811" Action="Error" />
|
||||
<Rule Id="CA1812" Action="Error" />
|
||||
<Rule Id="CA1813" Action="Error" />
|
||||
<Rule Id="CA1814" Action="Error" />
|
||||
<Rule Id="CA1815" Action="Error" />
|
||||
<Rule Id="CA1819" Action="Error" />
|
||||
<Rule Id="CA1820" Action="Error" />
|
||||
<Rule Id="CA1821" Action="Error" />
|
||||
<Rule Id="CA1822" Action="Error" />
|
||||
<Rule Id="CA1823" Action="Error" />
|
||||
<Rule Id="CA1824" Action="Error" />
|
||||
<Rule Id="CA1900" Action="Error" />
|
||||
<Rule Id="CA1901" Action="Error" />
|
||||
<Rule Id="CA1903" Action="Error" />
|
||||
<Rule Id="CA2000" Action="Error" />
|
||||
<Rule Id="CA2001" Action="Error" />
|
||||
<Rule Id="CA2002" Action="Error" />
|
||||
<Rule Id="CA2003" Action="Error" />
|
||||
<Rule Id="CA2004" Action="Error" />
|
||||
<Rule Id="CA2006" Action="Error" />
|
||||
<Rule Id="CA2100" Action="Error" />
|
||||
<Rule Id="CA2101" Action="Error" />
|
||||
<Rule Id="CA2102" Action="Error" />
|
||||
<Rule Id="CA2103" Action="Error" />
|
||||
<Rule Id="CA2104" Action="Error" />
|
||||
<Rule Id="CA2105" Action="Error" />
|
||||
<Rule Id="CA2106" Action="Error" />
|
||||
<Rule Id="CA2107" Action="Error" />
|
||||
<Rule Id="CA2108" Action="Error" />
|
||||
<Rule Id="CA2109" Action="Error" />
|
||||
<Rule Id="CA2111" Action="Error" />
|
||||
<Rule Id="CA2112" Action="Error" />
|
||||
<Rule Id="CA2114" Action="Error" />
|
||||
<Rule Id="CA2115" Action="Error" />
|
||||
<Rule Id="CA2116" Action="Error" />
|
||||
<Rule Id="CA2117" Action="Error" />
|
||||
<Rule Id="CA2118" Action="Error" />
|
||||
<Rule Id="CA2119" Action="Error" />
|
||||
<Rule Id="CA2120" Action="Error" />
|
||||
<Rule Id="CA2121" Action="Error" />
|
||||
<Rule Id="CA2122" Action="Error" />
|
||||
<Rule Id="CA2123" Action="Error" />
|
||||
<Rule Id="CA2124" Action="Error" />
|
||||
<Rule Id="CA2126" Action="Error" />
|
||||
<Rule Id="CA2130" Action="Error" />
|
||||
<Rule Id="CA2131" Action="Error" />
|
||||
<Rule Id="CA2132" Action="Error" />
|
||||
<Rule Id="CA2133" Action="Error" />
|
||||
<Rule Id="CA2134" Action="Error" />
|
||||
<Rule Id="CA2135" Action="Error" />
|
||||
<Rule Id="CA2136" Action="Error" />
|
||||
<Rule Id="CA2137" Action="Error" />
|
||||
<Rule Id="CA2138" Action="Error" />
|
||||
<Rule Id="CA2139" Action="Error" />
|
||||
<Rule Id="CA2140" Action="Error" />
|
||||
<Rule Id="CA2141" Action="Error" />
|
||||
<Rule Id="CA2142" Action="Error" />
|
||||
<Rule Id="CA2143" Action="Error" />
|
||||
<Rule Id="CA2144" Action="Error" />
|
||||
<Rule Id="CA2145" Action="Error" />
|
||||
<Rule Id="CA2146" Action="Error" />
|
||||
<Rule Id="CA2147" Action="Error" />
|
||||
<Rule Id="CA2149" Action="Error" />
|
||||
<Rule Id="CA2151" Action="Error" />
|
||||
<Rule Id="CA2200" Action="Error" />
|
||||
<Rule Id="CA2201" Action="Error" />
|
||||
<Rule Id="CA2202" Action="Error" />
|
||||
<Rule Id="CA2204" Action="Error" />
|
||||
<Rule Id="CA2205" Action="Error" />
|
||||
<Rule Id="CA2207" Action="Error" />
|
||||
<Rule Id="CA2208" Action="Error" />
|
||||
<Rule Id="CA2210" Action="Error" />
|
||||
<Rule Id="CA2211" Action="Error" />
|
||||
<Rule Id="CA2212" Action="Error" />
|
||||
<Rule Id="CA2213" Action="Error" />
|
||||
<Rule Id="CA2214" Action="Error" />
|
||||
<Rule Id="CA2215" Action="Error" />
|
||||
<Rule Id="CA2216" Action="Error" />
|
||||
<Rule Id="CA2217" Action="Error" />
|
||||
<Rule Id="CA2218" Action="Error" />
|
||||
<Rule Id="CA2219" Action="Error" />
|
||||
<Rule Id="CA2220" Action="Error" />
|
||||
<Rule Id="CA2221" Action="Error" />
|
||||
<Rule Id="CA2222" Action="Error" />
|
||||
<Rule Id="CA2223" Action="Error" />
|
||||
<Rule Id="CA2224" Action="Error" />
|
||||
<Rule Id="CA2225" Action="Error" />
|
||||
<Rule Id="CA2226" Action="Error" />
|
||||
<Rule Id="CA2227" Action="Error" />
|
||||
<Rule Id="CA2228" Action="Error" />
|
||||
<Rule Id="CA2229" Action="Error" />
|
||||
<Rule Id="CA2230" Action="Error" />
|
||||
<Rule Id="CA2231" Action="Error" />
|
||||
<Rule Id="CA2232" Action="Error" />
|
||||
<Rule Id="CA2233" Action="Error" />
|
||||
<Rule Id="CA2234" Action="Error" />
|
||||
<Rule Id="CA2235" Action="Error" />
|
||||
<Rule Id="CA2236" Action="Error" />
|
||||
<Rule Id="CA2237" Action="Error" />
|
||||
<Rule Id="CA2238" Action="Error" />
|
||||
<Rule Id="CA2239" Action="Error" />
|
||||
<Rule Id="CA2240" Action="Error" />
|
||||
<Rule Id="CA2241" Action="Error" />
|
||||
<Rule Id="CA2242" Action="Error" />
|
||||
<Rule Id="CA2243" Action="Error" />
|
||||
<Rule Id="CA5122" Action="Error" />
|
||||
</Rules>
|
||||
</RuleSet>
|
@ -1,40 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Condition="'$(ArtifactsDir)' != ''">
|
||||
<OutputPath Condition="$(UseBinPath) == ''">$(ArtifactsDir)\$(MSBuildProjectName)</OutputPath>
|
||||
<OutputPath Condition="$(UseBinPath) == 'true'">$(ArtifactsDir)\$(MSBuildProjectName)\bin</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)Microsoft.AspNet.SignalR.ruleset</CodeAnalysisRuleSet>
|
||||
<RunCodeAnalysis Condition="'$(RunCodeAnalysis)' == ''">false</RunCodeAnalysis>
|
||||
<NoWarn>1591</NoWarn>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(RunCodeAnalysis)' == 'true'">
|
||||
<DefineConstants>$(DefineConstants);CODE_ANALYSIS</DefineConstants>
|
||||
<VisualStudioVersion>11.0</VisualStudioVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(OS)' != 'Windows_NT'">
|
||||
<DefineConstants>$(DefineConstants);MONO</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(KeyFile)' != '' And '$(DisableSigning)' != 'true'">
|
||||
<DefineConstants>$(DefineConstants);SIGNED</DefineConstants>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<DelaySign>true</DelaySign>
|
||||
<AssemblyOriginatorKeyFile>$(KeyFile)</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="Exists('$(MSBuildThisFileDirectory)GlobalSuppressions.cs')">
|
||||
<Compile Include="$(MSBuildThisFileDirectory)GlobalSuppressions.cs">
|
||||
<Link>GlobalSuppressions.cs</Link>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<CodeAnalysisDictionary Include="$(MSBuildThisFileDirectory)CodeAnalysisDictionary.xml" />
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -1,958 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Reflection;
|
||||
using System.Collections;
|
||||
using Marr.Data.Mapping;
|
||||
using Marr.Data.Parameters;
|
||||
using Marr.Data.QGen;
|
||||
using System.Linq.Expressions;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Marr.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is the main access point for making database related calls.
|
||||
/// </summary>
|
||||
public class DataMapper : IDataMapper
|
||||
{
|
||||
|
||||
#region - Contructor, Members -
|
||||
|
||||
private DbCommand _command;
|
||||
|
||||
/// <summary>
|
||||
/// A database provider agnostic initialization.
|
||||
/// </summary>
|
||||
/// <param name="connectionString">The database connection string.</param>
|
||||
public DataMapper(DbProviderFactory dbProviderFactory, string connectionString)
|
||||
{
|
||||
SqlMode = SqlModes.StoredProcedure;
|
||||
if (dbProviderFactory == null)
|
||||
throw new ArgumentNullException("dbProviderFactory");
|
||||
|
||||
if (string.IsNullOrEmpty(connectionString))
|
||||
throw new ArgumentNullException("connectionString");
|
||||
|
||||
ProviderFactory = dbProviderFactory;
|
||||
|
||||
ConnectionString = connectionString;
|
||||
}
|
||||
|
||||
public string ConnectionString { get; private set; }
|
||||
|
||||
public DbProviderFactory ProviderFactory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command utilizing the connection string.
|
||||
/// </summary>
|
||||
private DbCommand CreateNewCommand()
|
||||
{
|
||||
DbConnection conn = ProviderFactory.CreateConnection();
|
||||
conn.ConnectionString = ConnectionString;
|
||||
DbCommand cmd = conn.CreateCommand();
|
||||
SetSqlMode(cmd);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command utilizing the connection string with a given SQL command.
|
||||
/// </summary>
|
||||
private DbCommand CreateNewCommand(string sql)
|
||||
{
|
||||
DbCommand cmd = CreateNewCommand();
|
||||
cmd.CommandText = sql;
|
||||
return cmd;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates a DbCommand object.
|
||||
/// </summary>
|
||||
public DbCommand Command
|
||||
{
|
||||
get
|
||||
{
|
||||
// Lazy load
|
||||
if (_command == null)
|
||||
_command = CreateNewCommand();
|
||||
else
|
||||
SetSqlMode(_command); // Set SqlMode every time.
|
||||
|
||||
return _command;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Parameters -
|
||||
|
||||
public DbParameterCollection Parameters
|
||||
{
|
||||
get
|
||||
{
|
||||
return Command.Parameters;
|
||||
}
|
||||
}
|
||||
|
||||
public ParameterChainMethods AddParameter(string name, object value)
|
||||
{
|
||||
return new ParameterChainMethods(Command, name, value);
|
||||
}
|
||||
|
||||
public IDbDataParameter AddParameter(IDbDataParameter parameter)
|
||||
{
|
||||
// Convert null values to DBNull.Value
|
||||
if (parameter.Value == null)
|
||||
parameter.Value = DBNull.Value;
|
||||
|
||||
Parameters.Add(parameter);
|
||||
return parameter;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - SP / SQL Mode -
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value that determines whether the DataMapper will
|
||||
/// use a stored procedure or a sql text command to access
|
||||
/// the database. The default is stored procedure.
|
||||
/// </summary>
|
||||
public SqlModes SqlMode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets the DbCommand objects CommandType to the current SqlMode.
|
||||
/// </summary>
|
||||
/// <param name="command">The DbCommand object we are modifying.</param>
|
||||
/// <returns>Returns the same DbCommand that was passed in.</returns>
|
||||
private DbCommand SetSqlMode(DbCommand command)
|
||||
{
|
||||
if (SqlMode == SqlModes.StoredProcedure)
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
else
|
||||
command.CommandType = CommandType.Text;
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - ExecuteScalar, ExecuteNonQuery, ExecuteReader -
|
||||
|
||||
/// <summary>
|
||||
/// Executes a stored procedure that returns a scalar value.
|
||||
/// </summary>
|
||||
/// <param name="sql">The SQL command to execute.</param>
|
||||
/// <returns>A scalar value</returns>
|
||||
public object ExecuteScalar(string sql)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
Command.CommandText = sql;
|
||||
|
||||
try
|
||||
{
|
||||
OpenConnection();
|
||||
return Command.ExecuteScalar();
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a non query that returns an integer.
|
||||
/// </summary>
|
||||
/// <param name="sql">The SQL command to execute.</param>
|
||||
/// <returns>An integer value</returns>
|
||||
public int ExecuteNonQuery(string sql)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
Command.CommandText = sql;
|
||||
|
||||
try
|
||||
{
|
||||
OpenConnection();
|
||||
return Command.ExecuteNonQuery();
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a DataReader that can be controlled using a Func delegate.
|
||||
/// (Note that reader.Read() will be called automatically).
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type that will be return in the result set.</typeparam>
|
||||
/// <param name="sql">The sql statement that will be executed.</param>
|
||||
/// <param name="func">The function that will build the the TResult set.</param>
|
||||
/// <returns>An IEnumerable of TResult.</returns>
|
||||
public IEnumerable<TResult> ExecuteReader<TResult>(string sql, Func<DbDataReader, TResult> func)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
Command.CommandText = sql;
|
||||
|
||||
try
|
||||
{
|
||||
OpenConnection();
|
||||
|
||||
var list = new List<TResult>();
|
||||
DbDataReader reader = null;
|
||||
try
|
||||
{
|
||||
reader = Command.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
list.Add(func(reader));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (reader != null) reader.Close();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a DataReader that can be controlled using an Action delegate.
|
||||
/// </summary>
|
||||
/// <param name="sql">The sql statement that will be executed.</param>
|
||||
/// <param name="action">The delegate that will work with the result set.</param>
|
||||
public void ExecuteReader(string sql, Action<DbDataReader> action)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
|
||||
Command.CommandText = sql;
|
||||
|
||||
try
|
||||
{
|
||||
OpenConnection();
|
||||
|
||||
DbDataReader reader = null;
|
||||
try
|
||||
{
|
||||
reader = Command.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
action(reader);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (reader != null) reader.Close();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - DataSets -
|
||||
|
||||
public DataSet GetDataSet(string sql)
|
||||
{
|
||||
return GetDataSet(sql, new DataSet(), null);
|
||||
}
|
||||
|
||||
public DataSet GetDataSet(string sql, DataSet ds, string tableName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
|
||||
try
|
||||
{
|
||||
using (DbDataAdapter adapter = ProviderFactory.CreateDataAdapter())
|
||||
{
|
||||
Command.CommandText = sql;
|
||||
adapter.SelectCommand = Command;
|
||||
|
||||
if (ds == null)
|
||||
ds = new DataSet();
|
||||
|
||||
OpenConnection();
|
||||
|
||||
if (string.IsNullOrEmpty(tableName))
|
||||
adapter.Fill(ds);
|
||||
else
|
||||
adapter.Fill(ds, tableName);
|
||||
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection(); // Clears parameters
|
||||
}
|
||||
}
|
||||
|
||||
public DataTable GetDataTable(string sql)
|
||||
{
|
||||
return GetDataTable(sql, null, null);
|
||||
}
|
||||
|
||||
public DataTable GetDataTable(string sql, DataTable dt, string tableName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
|
||||
try
|
||||
{
|
||||
using (DbDataAdapter adapter = ProviderFactory.CreateDataAdapter())
|
||||
{
|
||||
Command.CommandText = sql;
|
||||
adapter.SelectCommand = Command;
|
||||
|
||||
if (dt == null)
|
||||
dt = new DataTable();
|
||||
|
||||
adapter.Fill(dt);
|
||||
|
||||
if (!string.IsNullOrEmpty(tableName))
|
||||
dt.TableName = tableName;
|
||||
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection(); // Clears parameters
|
||||
}
|
||||
}
|
||||
|
||||
public int UpdateDataSet(DataSet ds, string sql)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
|
||||
if (ds == null)
|
||||
throw new ArgumentNullException("ds", "DataSet cannot be null.");
|
||||
|
||||
DbDataAdapter adapter = null;
|
||||
|
||||
try
|
||||
{
|
||||
adapter = ProviderFactory.CreateDataAdapter();
|
||||
|
||||
adapter.UpdateCommand = Command;
|
||||
adapter.UpdateCommand.CommandText = sql;
|
||||
|
||||
return adapter.Update(ds);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (adapter.UpdateCommand != null)
|
||||
adapter.UpdateCommand.Dispose();
|
||||
|
||||
adapter.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public int InsertDataTable(DataTable table, string insertSP)
|
||||
{
|
||||
return InsertDataTable(table, insertSP, UpdateRowSource.None);
|
||||
}
|
||||
|
||||
public int InsertDataTable(DataTable dt, string sql, UpdateRowSource updateRowSource)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
|
||||
if (dt == null)
|
||||
throw new ArgumentNullException("dt", "DataTable cannot be null.");
|
||||
|
||||
DbDataAdapter adapter = null;
|
||||
|
||||
try
|
||||
{
|
||||
adapter = ProviderFactory.CreateDataAdapter();
|
||||
|
||||
adapter.InsertCommand = Command;
|
||||
adapter.InsertCommand.CommandText = sql;
|
||||
|
||||
adapter.InsertCommand.UpdatedRowSource = updateRowSource;
|
||||
|
||||
return adapter.Update(dt);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (adapter.InsertCommand != null)
|
||||
adapter.InsertCommand.Dispose();
|
||||
|
||||
adapter.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public int DeleteDataTable(DataTable dt, string sql)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required");
|
||||
|
||||
if (dt == null)
|
||||
throw new ArgumentNullException("dt", "DataSet cannot be null.");
|
||||
|
||||
DbDataAdapter adapter = null;
|
||||
|
||||
try
|
||||
{
|
||||
adapter = ProviderFactory.CreateDataAdapter();
|
||||
|
||||
adapter.DeleteCommand = Command;
|
||||
adapter.DeleteCommand.CommandText = sql;
|
||||
|
||||
return adapter.Update(dt);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (adapter.DeleteCommand != null)
|
||||
adapter.DeleteCommand.Dispose();
|
||||
|
||||
adapter.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Find -
|
||||
|
||||
public T Find<T>(string sql)
|
||||
{
|
||||
return Find<T>(sql, default(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an entity of type T.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of entity that is to be instantiated and loaded with values.</typeparam>
|
||||
/// <param name="sql">The SQL command to execute.</param>
|
||||
/// <returns>An instantiated and loaded entity of type T.</returns>
|
||||
public T Find<T>(string sql, T ent)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A stored procedure name has not been specified for 'Find'.");
|
||||
|
||||
Type entityType = typeof(T);
|
||||
Command.CommandText = sql;
|
||||
|
||||
MapRepository repository = MapRepository.Instance;
|
||||
ColumnMapCollection mappings = repository.GetColumns(entityType);
|
||||
|
||||
bool isSimpleType = DataHelper.IsSimpleType(typeof(T));
|
||||
|
||||
try
|
||||
{
|
||||
OpenConnection();
|
||||
var mappingHelper = new MappingHelper(this);
|
||||
|
||||
using (DbDataReader reader = Command.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
if (isSimpleType)
|
||||
{
|
||||
return mappingHelper.LoadSimpleValueFromFirstColumn<T>(reader);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ent == null)
|
||||
ent = (T)mappingHelper.CreateAndLoadEntity<T>(mappings, reader, false);
|
||||
else
|
||||
mappingHelper.LoadExistingEntity(mappings, reader, ent, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
|
||||
return ent;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Query -
|
||||
|
||||
/// <summary>
|
||||
/// Creates a QueryBuilder that allows you to build a query.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object that will be queried.</typeparam>
|
||||
/// <returns>Returns a QueryBuilder of T.</returns>
|
||||
public QueryBuilder<T> Query<T>()
|
||||
{
|
||||
var dialect = QueryFactory.CreateDialect(this);
|
||||
return new QueryBuilder<T>(this, dialect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the results of a query.
|
||||
/// Uses a List of type T to return the data.
|
||||
/// </summary>
|
||||
/// <returns>Returns a list of the specified type.</returns>
|
||||
public List<T> Query<T>(string sql)
|
||||
{
|
||||
return (List<T>)Query<T>(sql, new List<T>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the results of a SP query.
|
||||
/// </summary>
|
||||
/// <returns>Returns a list of the specified type.</returns>
|
||||
public ICollection<T> Query<T>(string sql, ICollection<T> entityList)
|
||||
{
|
||||
return Query<T>(sql, entityList, false);
|
||||
}
|
||||
|
||||
internal ICollection<T> Query<T>(string sql, ICollection<T> entityList, bool useAltName)
|
||||
{
|
||||
if (entityList == null)
|
||||
throw new ArgumentNullException("entityList", "ICollection instance cannot be null.");
|
||||
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "A query or stored procedure has not been specified for 'Query'.");
|
||||
|
||||
var mappingHelper = new MappingHelper(this);
|
||||
Type entityType = typeof(T);
|
||||
Command.CommandText = sql;
|
||||
ColumnMapCollection mappings = MapRepository.Instance.GetColumns(entityType);
|
||||
|
||||
bool isSimpleType = DataHelper.IsSimpleType(typeof(T));
|
||||
|
||||
try
|
||||
{
|
||||
OpenConnection();
|
||||
using (DbDataReader reader = Command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
if (isSimpleType)
|
||||
{
|
||||
entityList.Add(mappingHelper.LoadSimpleValueFromFirstColumn<T>(reader));
|
||||
}
|
||||
else
|
||||
{
|
||||
entityList.Add((T)mappingHelper.CreateAndLoadEntity<T>(mappings, reader, useAltName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
|
||||
return entityList;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Query to Graph -
|
||||
|
||||
public List<T> QueryToGraph<T>(string sql)
|
||||
{
|
||||
return (List<T>)QueryToGraph<T>(sql, new List<T>());
|
||||
}
|
||||
|
||||
public ICollection<T> QueryToGraph<T>(string sql, ICollection<T> entityList)
|
||||
{
|
||||
EntityGraph graph = new EntityGraph(typeof(T), (IList)entityList);
|
||||
return QueryToGraph<T>(sql, graph, new List<MemberInfo>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries a view that joins multiple tables and returns an object graph.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="entityList"></param>
|
||||
/// <param name="entityGraph">Coordinates loading all objects in the graph..</param>
|
||||
/// <returns></returns>
|
||||
internal ICollection<T> QueryToGraph<T>(string sql, EntityGraph graph, List<MemberInfo> childrenToLoad)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException("sql", "sql");
|
||||
|
||||
var mappingHelper = new MappingHelper(this);
|
||||
Type parentType = typeof(T);
|
||||
Command.CommandText = sql;
|
||||
|
||||
try
|
||||
{
|
||||
OpenConnection();
|
||||
using (DbDataReader reader = Command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
// The entire EntityGraph is traversed for each record,
|
||||
// and multiple entities are created from each view record.
|
||||
foreach (EntityGraph lvl in graph)
|
||||
{
|
||||
if (lvl.IsParentReference)
|
||||
{
|
||||
// A child specified a circular reference to its previously loaded parent
|
||||
lvl.AddParentReference();
|
||||
}
|
||||
else if (childrenToLoad.Count > 0 && !lvl.IsRoot && !childrenToLoad.ContainsMember(lvl.Member))
|
||||
{
|
||||
// A list of relationships-to-load was specified and this relationship was not included
|
||||
continue;
|
||||
}
|
||||
else if (lvl.IsNewGroup(reader))
|
||||
{
|
||||
// Create a new entity with the data reader
|
||||
var newEntity = mappingHelper.CreateAndLoadEntity(lvl.EntityType, lvl.Columns, reader, true);
|
||||
|
||||
// Add entity to the appropriate place in the object graph
|
||||
lvl.AddEntity(newEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
|
||||
return (ICollection<T>)graph.RootList;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Update -
|
||||
|
||||
public UpdateQueryBuilder<T> Update<T>()
|
||||
{
|
||||
return new UpdateQueryBuilder<T>(this);
|
||||
}
|
||||
|
||||
public int Update<T>(T entity, Expression<Func<T, bool>> filter)
|
||||
{
|
||||
return Update<T>()
|
||||
.Entity(entity)
|
||||
.Where(filter)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
public int Update<T>(string tableName, T entity, Expression<Func<T, bool>> filter)
|
||||
{
|
||||
return Update<T>()
|
||||
.TableName(tableName)
|
||||
.Entity(entity)
|
||||
.Where(filter)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
public int Update<T>(T entity, string sql)
|
||||
{
|
||||
return Update<T>()
|
||||
.Entity(entity)
|
||||
.QueryText(sql)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Insert -
|
||||
|
||||
/// <summary>
|
||||
/// Creates an InsertQueryBuilder that allows you to build an insert statement.
|
||||
/// This method gives you the flexibility to manually configure all options of your insert statement.
|
||||
/// Note: You must manually call the Execute() chaining method to run the query.
|
||||
/// </summary>
|
||||
public InsertQueryBuilder<T> Insert<T>()
|
||||
{
|
||||
return new InsertQueryBuilder<T>(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates and executes an insert query for the given entity.
|
||||
/// This overload will automatically run an identity query if you have mapped an auto-incrementing column,
|
||||
/// and if an identity query has been implemented for your current database dialect.
|
||||
/// </summary>
|
||||
public object Insert<T>(T entity)
|
||||
{
|
||||
var columns = MapRepository.Instance.GetColumns(typeof(T));
|
||||
var dialect = QueryFactory.CreateDialect(this);
|
||||
var builder = Insert<T>().Entity(entity);
|
||||
|
||||
// If an auto-increment column exists and this dialect has an identity query...
|
||||
if (columns.Exists(c => c.ColumnInfo.IsAutoIncrement) && dialect.HasIdentityQuery)
|
||||
{
|
||||
builder.GetIdentity();
|
||||
}
|
||||
|
||||
return builder.Execute();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates and executes an insert query for the given entity.
|
||||
/// This overload will automatically run an identity query if you have mapped an auto-incrementing column,
|
||||
/// and if an identity query has been implemented for your current database dialect.
|
||||
/// </summary>
|
||||
public object Insert<T>(string tableName, T entity)
|
||||
{
|
||||
var columns = MapRepository.Instance.GetColumns(typeof(T));
|
||||
var dialect = QueryFactory.CreateDialect(this);
|
||||
var builder = Insert<T>().Entity(entity).TableName(tableName);
|
||||
|
||||
// If an auto-increment column exists and this dialect has an identity query...
|
||||
if (columns.Exists(c => c.ColumnInfo.IsAutoIncrement) && dialect.HasIdentityQuery)
|
||||
{
|
||||
builder.GetIdentity();
|
||||
}
|
||||
|
||||
return builder.Execute();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes an insert query for the given entity using the given sql insert statement.
|
||||
/// This overload will automatically run an identity query if you have mapped an auto-incrementing column,
|
||||
/// and if an identity query has been implemented for your current database dialect.
|
||||
/// </summary>
|
||||
public object Insert<T>(T entity, string sql)
|
||||
{
|
||||
var columns = MapRepository.Instance.GetColumns(typeof(T));
|
||||
var dialect = QueryFactory.CreateDialect(this);
|
||||
var builder = Insert<T>().Entity(entity).QueryText(sql);
|
||||
|
||||
// If an auto-increment column exists and this dialect has an identity query...
|
||||
if (columns.Exists(c => c.ColumnInfo.IsAutoIncrement) && dialect.HasIdentityQuery)
|
||||
{
|
||||
builder.GetIdentity();
|
||||
}
|
||||
|
||||
return builder.Execute();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Delete -
|
||||
|
||||
public int Delete<T>(Expression<Func<T, bool>> filter)
|
||||
{
|
||||
return Delete<T>(null, filter);
|
||||
}
|
||||
|
||||
public int Delete<T>(string tableName, Expression<Func<T, bool>> filter)
|
||||
{
|
||||
// Remember sql mode
|
||||
var previousSqlMode = SqlMode;
|
||||
SqlMode = SqlModes.Text;
|
||||
|
||||
var mappingHelper = new MappingHelper(this);
|
||||
if (tableName == null)
|
||||
{
|
||||
tableName = MapRepository.Instance.GetTableName(typeof(T));
|
||||
}
|
||||
var dialect = QueryFactory.CreateDialect(this);
|
||||
TableCollection tables = new TableCollection();
|
||||
tables.Add(new Table(typeof(T)));
|
||||
var where = new WhereBuilder<T>(Command, dialect, filter, tables, false, false);
|
||||
IQuery query = QueryFactory.CreateDeleteQuery(dialect, tables[0], where.ToString());
|
||||
Command.CommandText = query.Generate();
|
||||
|
||||
int rowsAffected = 0;
|
||||
|
||||
try
|
||||
{
|
||||
OpenConnection();
|
||||
rowsAffected = Command.ExecuteNonQuery();
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
|
||||
// Return to previous sql mode
|
||||
SqlMode = previousSqlMode;
|
||||
|
||||
return rowsAffected;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Events -
|
||||
|
||||
public event EventHandler OpeningConnection;
|
||||
|
||||
public event EventHandler ClosingConnection;
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Connections / Transactions -
|
||||
|
||||
protected virtual void OnOpeningConnection()
|
||||
{
|
||||
if (OpeningConnection != null)
|
||||
OpeningConnection(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
protected virtual void OnClosingConnection()
|
||||
{
|
||||
WriteToTraceLog();
|
||||
|
||||
if (ClosingConnection != null)
|
||||
ClosingConnection(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
protected internal void OpenConnection()
|
||||
{
|
||||
OnOpeningConnection();
|
||||
|
||||
if (Command.Connection.State != ConnectionState.Open)
|
||||
Command.Connection.Open();
|
||||
}
|
||||
|
||||
protected internal void CloseConnection()
|
||||
{
|
||||
OnClosingConnection();
|
||||
|
||||
Command.Parameters.Clear();
|
||||
Command.CommandText = string.Empty;
|
||||
|
||||
if (Command.Transaction == null)
|
||||
Command.Connection.Close(); // Only close if no transaction is present
|
||||
|
||||
UnbindEvents();
|
||||
}
|
||||
|
||||
private void WriteToTraceLog()
|
||||
{
|
||||
if (MapRepository.Instance.EnableTraceLogging)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("==== Begin Query Trace ====");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("QUERY TYPE:");
|
||||
sb.AppendLine(Command.CommandType.ToString());
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("QUERY TEXT:");
|
||||
sb.AppendLine(Command.CommandText);
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("PARAMETERS:");
|
||||
foreach (IDbDataParameter p in Parameters)
|
||||
{
|
||||
object val = (p.Value != null && p.Value is string) ? string.Format("\"{0}\"", p.Value) : p.Value;
|
||||
sb.AppendFormat("{0} = [{1}]", p.ParameterName, val ?? "NULL").AppendLine();
|
||||
}
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("==== End Query Trace ====");
|
||||
sb.AppendLine();
|
||||
|
||||
Trace.Write(sb.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private void UnbindEvents()
|
||||
{
|
||||
OpeningConnection = null;
|
||||
ClosingConnection = null;
|
||||
}
|
||||
|
||||
public void BeginTransaction(IsolationLevel isolationLevel)
|
||||
{
|
||||
OpenConnection();
|
||||
DbTransaction trans = Command.Connection.BeginTransaction(isolationLevel);
|
||||
Command.Transaction = trans;
|
||||
}
|
||||
|
||||
public void RollBack()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Command.Transaction != null)
|
||||
Command.Transaction.Rollback();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Command.Connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Command.Transaction != null)
|
||||
Command.Transaction.Commit();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Command.Connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - IDisposable Members -
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this); // In case a derived class implements a finalizer
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
if (_command != null)
|
||||
{
|
||||
if (_command.Transaction != null)
|
||||
{
|
||||
_command.Transaction.Dispose();
|
||||
_command.Transaction = null;
|
||||
}
|
||||
|
||||
if (_command.Connection != null)
|
||||
{
|
||||
_command.Connection.Dispose();
|
||||
_command.Connection = null;
|
||||
}
|
||||
|
||||
_command.Dispose();
|
||||
_command = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@ -1,419 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using Marr.Data.Mapping;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Marr.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Holds metadata about an object graph that is being queried and eagerly loaded.
|
||||
/// Contains all metadata needed to instantiate the object and fill it with data from a DataReader.
|
||||
/// Does not iterate through lazy loaded child relationships.
|
||||
/// </summary>
|
||||
internal class EntityGraph : IEnumerable<EntityGraph>
|
||||
{
|
||||
private MapRepository _repos;
|
||||
private EntityGraph _parent;
|
||||
private Type _entityType;
|
||||
private Relationship _relationship;
|
||||
private ColumnMapCollection _columns;
|
||||
private RelationshipCollection _relationships;
|
||||
private List<EntityGraph> _children;
|
||||
private object _entity;
|
||||
private GroupingKeyCollection _groupingKeyColumns;
|
||||
private Dictionary<string, EntityReference> _entityReferences;
|
||||
|
||||
internal IList RootList { get; private set; }
|
||||
internal bool IsParentReference { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Recursively builds an entity graph of the given parent type.
|
||||
/// </summary>
|
||||
/// <param name="entityType"></param>
|
||||
public EntityGraph(Type entityType, IList rootList)
|
||||
: this(entityType, null, null) // Recursively constructs hierarchy
|
||||
{
|
||||
RootList = rootList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursively builds entity graph hierarchy.
|
||||
/// </summary>
|
||||
/// <param name="entityType"></param>
|
||||
/// <param name="parent"></param>
|
||||
/// <param name="relationship"></param>
|
||||
private EntityGraph(Type entityType, EntityGraph parent, Relationship relationship)
|
||||
{
|
||||
_repos = MapRepository.Instance;
|
||||
|
||||
_entityType = entityType;
|
||||
_parent = parent;
|
||||
_relationship = relationship;
|
||||
IsParentReference = !IsRoot && AnyParentsAreOfType(entityType);
|
||||
if (!IsParentReference)
|
||||
{
|
||||
_columns = _repos.GetColumns(entityType);
|
||||
}
|
||||
|
||||
_relationships = _repos.GetRelationships(entityType);
|
||||
_children = new List<EntityGraph>();
|
||||
Member = relationship != null ? relationship.Member : null;
|
||||
_entityReferences = new Dictionary<string, EntityReference>();
|
||||
|
||||
if (IsParentReference)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a new EntityGraph for each child relationship that is not lazy loaded
|
||||
foreach (Relationship childRelationship in Relationships)
|
||||
{
|
||||
if (!childRelationship.IsLazyLoaded)
|
||||
{
|
||||
_children.Add(new EntityGraph(childRelationship.RelationshipInfo.EntityType, this, childRelationship));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MemberInfo Member { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parent of this EntityGraph.
|
||||
/// </summary>
|
||||
public EntityGraph Parent
|
||||
{
|
||||
get
|
||||
{
|
||||
return _parent;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Type of this EntityGraph.
|
||||
/// </summary>
|
||||
public Type EntityType
|
||||
{
|
||||
get { return _entityType; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a boolean than indicates whether this entity is the root node in the graph.
|
||||
/// </summary>
|
||||
public bool IsRoot
|
||||
{
|
||||
get
|
||||
{
|
||||
return _parent == null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a boolean that indicates whether this entity is a child.
|
||||
/// </summary>
|
||||
public bool IsChild
|
||||
{
|
||||
get
|
||||
{
|
||||
return _parent != null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the columns mapped to this entity.
|
||||
/// </summary>
|
||||
public ColumnMapCollection Columns
|
||||
{
|
||||
get { return _columns; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the relationships mapped to this entity.
|
||||
/// </summary>
|
||||
public RelationshipCollection Relationships
|
||||
{
|
||||
get { return _relationships; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A list of EntityGraph objects that hold metadata about the child entities that will be loaded.
|
||||
/// </summary>
|
||||
public List<EntityGraph> Children
|
||||
{
|
||||
get { return _children; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an Child in the graph for LazyLoaded property.
|
||||
/// </summary>
|
||||
public void AddLazyRelationship(Relationship childRelationship)
|
||||
{
|
||||
_children.Add(new EntityGraph(childRelationship.RelationshipInfo.EntityType.GetGenericArguments()[0], this, childRelationship));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an entity to the appropriate place in the object graph.
|
||||
/// </summary>
|
||||
/// <param name="entityInstance"></param>
|
||||
public void AddEntity(object entityInstance)
|
||||
{
|
||||
_entity = entityInstance;
|
||||
|
||||
// Add newly created entityInstance to list (Many) or set it to field (One)
|
||||
if (IsRoot)
|
||||
{
|
||||
RootList.Add(entityInstance);
|
||||
}
|
||||
else if (_relationship.RelationshipInfo.RelationType == RelationshipTypes.Many)
|
||||
{
|
||||
var list = _parent._entityReferences[_parent.GroupingKeyColumns.GroupingKey]
|
||||
.ChildLists[_relationship.Member.Name];
|
||||
|
||||
list.Add(entityInstance);
|
||||
}
|
||||
else // RelationTypes.One
|
||||
{
|
||||
if (_relationship.IsLazyLoaded)
|
||||
_relationship.Setter(_parent._entity, Activator.CreateInstance(_relationship.MemberType, entityInstance));
|
||||
else
|
||||
_relationship.Setter(_parent._entity, entityInstance);
|
||||
}
|
||||
|
||||
EntityReference entityRef = new EntityReference(entityInstance);
|
||||
_entityReferences.Add(GroupingKeyColumns.GroupingKey, entityRef);
|
||||
|
||||
InitOneToManyChildLists(entityRef);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches for a previously loaded parent entity and then sets that reference to the mapped Relationship property.
|
||||
/// </summary>
|
||||
public void AddParentReference()
|
||||
{
|
||||
var parentReference = FindParentReference();
|
||||
_relationship.Setter(_parent._entity, parentReference);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concatenates the values of the GroupingKeys property and compares them
|
||||
/// against the LastKeyGroup property. Returns true if the values are different,
|
||||
/// or false if the values are the same.
|
||||
/// The currently concatenated keys are saved in the LastKeyGroup property.
|
||||
/// </summary>
|
||||
/// <param name="reader"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsNewGroup(DbDataReader reader)
|
||||
{
|
||||
bool isNewGroup = false;
|
||||
|
||||
// Get primary keys from parent entity and any one-to-one child entites
|
||||
GroupingKeyCollection groupingKeyColumns = GroupingKeyColumns;
|
||||
|
||||
// Concatenate column values
|
||||
KeyGroupInfo keyGroupInfo = groupingKeyColumns.CreateGroupingKey(reader);
|
||||
|
||||
if (!keyGroupInfo.HasNullKey && !_entityReferences.ContainsKey(keyGroupInfo.GroupingKey))
|
||||
{
|
||||
isNewGroup = true;
|
||||
}
|
||||
|
||||
return isNewGroup;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the GroupingKeys for this entity.
|
||||
/// GroupingKeys determine when to create and add a new entity to the graph.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A simple entity with no relationships will return only its PrimaryKey columns.
|
||||
/// A parent entity with one-to-one child relationships will include its own PrimaryKeys,
|
||||
/// and it will recursively traverse all Children with one-to-one relationships and add their PrimaryKeys.
|
||||
/// A child entity that has a one-to-one relationship with its parent will use the same
|
||||
/// GroupingKeys already defined by its parent.
|
||||
/// </remarks>
|
||||
public GroupingKeyCollection GroupingKeyColumns
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_groupingKeyColumns == null)
|
||||
_groupingKeyColumns = GetGroupingKeyColumns();
|
||||
|
||||
return _groupingKeyColumns;
|
||||
}
|
||||
}
|
||||
|
||||
private bool AnyParentsAreOfType(Type type)
|
||||
{
|
||||
EntityGraph parent = _parent;
|
||||
while (parent != null)
|
||||
{
|
||||
if (parent._entityType == type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
parent = parent._parent;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private object FindParentReference()
|
||||
{
|
||||
var parent = Parent.Parent;
|
||||
while (parent != null)
|
||||
{
|
||||
if (parent._entityType == _relationship.MemberType)
|
||||
{
|
||||
return parent._entity;
|
||||
}
|
||||
|
||||
parent = parent.Parent;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the owning lists on many-to-many Children.
|
||||
/// </summary>
|
||||
/// <param name="entityInstance"></param>
|
||||
private void InitOneToManyChildLists(EntityReference entityRef)
|
||||
{
|
||||
// Get a reference to the parent's the childrens' OwningLists to the parent entity
|
||||
for (int i = 0; i < Relationships.Count; i++)
|
||||
{
|
||||
Relationship relationship = Relationships[i];
|
||||
if (relationship.RelationshipInfo.RelationType == RelationshipTypes.Many)
|
||||
{
|
||||
try
|
||||
{
|
||||
IList list = (IList)_repos.ReflectionStrategy.CreateInstance(relationship.MemberType);
|
||||
relationship.Setter(entityRef.Entity, list);
|
||||
|
||||
// Save a reference to each 1-M list
|
||||
entityRef.AddChildList(relationship.Member.Name, list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new DataMappingException(
|
||||
string.Format("{0}.{1} is a \"Many\" relationship type so it must derive from IList.",
|
||||
entityRef.Entity.GetType().Name, relationship.Member.Name),
|
||||
ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of keys to group by.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// When converting an unnormalized set of data from a database view,
|
||||
/// a new entity is only created when the grouping keys have changed.
|
||||
/// NOTE: This behavior works on the assumption that the view result set
|
||||
/// has been sorted by the root entity primary key(s), followed by the
|
||||
/// child entity primary keys.
|
||||
/// </remarks>
|
||||
/// <returns></returns>
|
||||
private GroupingKeyCollection GetGroupingKeyColumns()
|
||||
{
|
||||
// Get primary keys for this parent entity
|
||||
GroupingKeyCollection groupingKeyColumns = new GroupingKeyCollection();
|
||||
groupingKeyColumns.PrimaryKeys.AddRange(Columns.PrimaryKeys);
|
||||
|
||||
// The following conditions should fail with an exception:
|
||||
// 1) Any parent entity (entity with children) must have at least one PK specified or an exception will be thrown
|
||||
// 2) All 1-M relationship entities must have at least one PK specified
|
||||
// * Only 1-1 entities with no children are allowed to have 0 PKs specified.
|
||||
if ((groupingKeyColumns.PrimaryKeys.Count == 0 && _children.Count > 0) ||
|
||||
(groupingKeyColumns.PrimaryKeys.Count == 0 && !IsRoot && _relationship.RelationshipInfo.RelationType == RelationshipTypes.Many))
|
||||
throw new MissingPrimaryKeyException(string.Format("There are no primary key mappings defined for the following entity: '{0}'.", EntityType.Name));
|
||||
|
||||
// Add parent's keys
|
||||
if (IsChild)
|
||||
groupingKeyColumns.ParentPrimaryKeys.AddRange(Parent.GroupingKeyColumns);
|
||||
|
||||
return groupingKeyColumns;
|
||||
}
|
||||
|
||||
#region IEnumerable<EntityGraph> Members
|
||||
|
||||
public IEnumerator<EntityGraph> GetEnumerator()
|
||||
{
|
||||
return TraverseGraph(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursively traverses through every entity in the EntityGraph.
|
||||
/// </summary>
|
||||
/// <param name="entityGraph"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerator<EntityGraph> TraverseGraph(EntityGraph entityGraph)
|
||||
{
|
||||
Stack<EntityGraph> stack = new Stack<EntityGraph>();
|
||||
stack.Push(entityGraph);
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
EntityGraph node = stack.Pop();
|
||||
yield return node;
|
||||
|
||||
foreach (EntityGraph childGraph in node.Children)
|
||||
{
|
||||
stack.Push(childGraph);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public struct KeyGroupInfo
|
||||
{
|
||||
private string _groupingKey;
|
||||
private bool _hasNullKey;
|
||||
|
||||
public KeyGroupInfo(string groupingKey, bool hasNullKey)
|
||||
{
|
||||
_groupingKey = groupingKey;
|
||||
_hasNullKey = hasNullKey;
|
||||
}
|
||||
|
||||
public string GroupingKey
|
||||
{
|
||||
get { return _groupingKey; }
|
||||
}
|
||||
|
||||
public bool HasNullKey
|
||||
{
|
||||
get { return _hasNullKey; }
|
||||
}
|
||||
}
|
@ -1,250 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Marr.Data.Converters;
|
||||
using Marr.Data.Parameters;
|
||||
using Marr.Data.Mapping;
|
||||
using Marr.Data.Mapping.Strategies;
|
||||
using Marr.Data.Reflection;
|
||||
|
||||
namespace Marr.Data
|
||||
{
|
||||
public class MapRepository
|
||||
{
|
||||
private static readonly object _tablesLock = new object();
|
||||
private static readonly object _columnsLock = new object();
|
||||
private static readonly object _relationshipsLock = new object();
|
||||
|
||||
private IDbTypeBuilder _dbTypeBuilder;
|
||||
private Dictionary<Type, IMapStrategy> _columnMapStrategies;
|
||||
|
||||
internal Dictionary<Type, string> Tables { get; set; }
|
||||
internal Dictionary<Type, ColumnMapCollection> Columns { get; set; }
|
||||
internal Dictionary<Type, RelationshipCollection> Relationships { get; set; }
|
||||
public Dictionary<Type, IConverter> TypeConverters { get; private set; }
|
||||
|
||||
// Explicit static constructor to tell C# compiler
|
||||
// not to mark type as beforefieldinit
|
||||
static MapRepository()
|
||||
{ }
|
||||
|
||||
private MapRepository()
|
||||
{
|
||||
Tables = new Dictionary<Type, string>();
|
||||
Columns = new Dictionary<Type, ColumnMapCollection>();
|
||||
Relationships = new Dictionary<Type, RelationshipCollection>();
|
||||
TypeConverters = new Dictionary<Type, IConverter>();
|
||||
|
||||
// Register a default IReflectionStrategy
|
||||
ReflectionStrategy = new SimpleReflectionStrategy();
|
||||
|
||||
// Register a default type converter for Enums
|
||||
TypeConverters.Add(typeof(Enum), new EnumStringConverter());
|
||||
|
||||
// Register a default IDbTypeBuilder
|
||||
_dbTypeBuilder = new DbTypeBuilder();
|
||||
|
||||
_columnMapStrategies = new Dictionary<Type, IMapStrategy>();
|
||||
RegisterDefaultMapStrategy(new AttributeMapStrategy());
|
||||
|
||||
EnableTraceLogging = false;
|
||||
}
|
||||
|
||||
private readonly static MapRepository _instance = new MapRepository();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the singleton MapRepository.
|
||||
/// </summary>
|
||||
public static MapRepository Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a boolean that determines whether debug information should be written to the trace log.
|
||||
/// The default is false.
|
||||
/// </summary>
|
||||
public bool EnableTraceLogging { get; set; }
|
||||
|
||||
#region - Column Map Strategies -
|
||||
|
||||
public void RegisterDefaultMapStrategy(IMapStrategy strategy)
|
||||
{
|
||||
RegisterMapStrategy(typeof(object), strategy);
|
||||
}
|
||||
|
||||
public void RegisterMapStrategy(Type entityType, IMapStrategy strategy)
|
||||
{
|
||||
if (_columnMapStrategies.ContainsKey(entityType))
|
||||
_columnMapStrategies[entityType] = strategy;
|
||||
else
|
||||
_columnMapStrategies.Add(entityType, strategy);
|
||||
}
|
||||
|
||||
private IMapStrategy GetMapStrategy(Type entityType)
|
||||
{
|
||||
if (_columnMapStrategies.ContainsKey(entityType))
|
||||
{
|
||||
// Return entity specific column map strategy
|
||||
return _columnMapStrategies[entityType];
|
||||
}
|
||||
// Return the default column map strategy
|
||||
return _columnMapStrategies[typeof(object)];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Table repository -
|
||||
|
||||
internal string GetTableName(Type entityType)
|
||||
{
|
||||
if (!Tables.ContainsKey(entityType))
|
||||
{
|
||||
lock (_tablesLock)
|
||||
{
|
||||
if (!Tables.ContainsKey(entityType))
|
||||
{
|
||||
string tableName = GetMapStrategy(entityType).MapTable(entityType);
|
||||
Tables.Add(entityType, tableName);
|
||||
return tableName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Tables[entityType];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Columns repository -
|
||||
|
||||
public ColumnMapCollection GetColumns(Type entityType)
|
||||
{
|
||||
if (!Columns.ContainsKey(entityType))
|
||||
{
|
||||
lock (_columnsLock)
|
||||
{
|
||||
if (!Columns.ContainsKey(entityType))
|
||||
{
|
||||
ColumnMapCollection columnMaps = GetMapStrategy(entityType).MapColumns(entityType);
|
||||
Columns.Add(entityType, columnMaps);
|
||||
return columnMaps;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Columns[entityType];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Relationships repository -
|
||||
|
||||
public RelationshipCollection GetRelationships(Type type)
|
||||
{
|
||||
if (!Relationships.ContainsKey(type))
|
||||
{
|
||||
lock (_relationshipsLock)
|
||||
{
|
||||
if (!Relationships.ContainsKey(type))
|
||||
{
|
||||
RelationshipCollection relationships = GetMapStrategy(type).MapRelationships(type);
|
||||
Relationships.Add(type, relationships);
|
||||
return relationships;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Relationships[type];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Reflection Strategy -
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the reflection strategy that the DataMapper will use to load entities.
|
||||
/// By default the CachedReflector will be used, which provides a performance increase over the SimpleReflector.
|
||||
/// However, the SimpleReflector can be used in Medium Trust enviroments.
|
||||
/// </summary>
|
||||
///
|
||||
public IReflectionStrategy ReflectionStrategy { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Type Converters -
|
||||
|
||||
/// <summary>
|
||||
/// Registers a converter for a given type.
|
||||
/// </summary>
|
||||
/// <param name="type">The CLR data type that will be converted.</param>
|
||||
/// <param name="converter">An IConverter object that will handle the data conversion.</param>
|
||||
public void RegisterTypeConverter(Type type, IConverter converter)
|
||||
{
|
||||
TypeConverters[type] = converter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks for a type converter (if one exists).
|
||||
/// 1) Checks for a converter registered for the current columns data type.
|
||||
/// 2) Checks to see if a converter is registered for all enums (type of Enum) if the current column is an enum.
|
||||
/// 3) Checks to see if a converter is registered for all objects (type of Object).
|
||||
/// </summary>
|
||||
/// <param name="dataMap">The current data map.</param>
|
||||
/// <returns>Returns an IConverter object or null if one does not exist.</returns>
|
||||
internal IConverter GetConverter(Type dataType)
|
||||
{
|
||||
if (TypeConverters.ContainsKey(dataType))
|
||||
{
|
||||
// User registered type converter
|
||||
return TypeConverters[dataType];
|
||||
}
|
||||
if (TypeConverters.ContainsKey(typeof(Enum)) && dataType.IsEnum)
|
||||
{
|
||||
// A converter is registered to handled enums
|
||||
return TypeConverters[typeof(Enum)];
|
||||
}
|
||||
if (TypeConverters.ContainsKey(typeof(object)))
|
||||
{
|
||||
// User registered default converter
|
||||
return TypeConverters[typeof(object)];
|
||||
}
|
||||
// No conversion
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - DbTypeBuilder -
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the IDBTypeBuilder that is responsible for converting parameter DbTypes based on the parameter value.
|
||||
/// Defaults to use the DbTypeBuilder.
|
||||
/// You can replace this with a more specific builder if you want more control over the way the parameter types are set.
|
||||
/// </summary>
|
||||
public IDbTypeBuilder DbTypeBuilder
|
||||
{
|
||||
get { return _dbTypeBuilder; }
|
||||
set { _dbTypeBuilder = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Data;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
|
||||
public class ColumnAttribute : Attribute, IColumnInfo
|
||||
{
|
||||
private string _name;
|
||||
private string _altName;
|
||||
private int _size = 0;
|
||||
private bool _isPrimaryKey;
|
||||
private bool _isAutoIncrement;
|
||||
private bool _returnValue;
|
||||
private ParameterDirection _paramDirection = ParameterDirection.Input;
|
||||
|
||||
public ColumnAttribute()
|
||||
{
|
||||
}
|
||||
|
||||
public ColumnAttribute(string name)
|
||||
{
|
||||
_name = name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the column name.
|
||||
/// </summary>
|
||||
public string Name
|
||||
{
|
||||
get { return _name; }
|
||||
set { _name = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets an alternate name that is used to define this column in views.
|
||||
/// If an AltName is present, it is used in the QueryViewToObjectGraph method.
|
||||
/// If an AltName is not present, it will return the Name property value.
|
||||
/// </summary>
|
||||
public string AltName
|
||||
{
|
||||
get { return _altName; }
|
||||
set { _altName = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the column size.
|
||||
/// </summary>
|
||||
public int Size
|
||||
{
|
||||
get { return _size; }
|
||||
set { _size = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value that determines whether the column is the Primary Key.
|
||||
/// </summary>
|
||||
public bool IsPrimaryKey
|
||||
{
|
||||
get { return _isPrimaryKey; }
|
||||
set { _isPrimaryKey = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value that determines whether the column is an auto-incrementing seed column.
|
||||
/// </summary>
|
||||
public bool IsAutoIncrement
|
||||
{
|
||||
get { return _isAutoIncrement; }
|
||||
set { _isAutoIncrement = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value that determines whether the column has a return value.
|
||||
/// </summary>
|
||||
public bool ReturnValue
|
||||
{
|
||||
get { return _returnValue; }
|
||||
set { _returnValue = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ParameterDirection.
|
||||
/// </summary>
|
||||
public ParameterDirection ParamDirection
|
||||
{
|
||||
get { return _paramDirection; }
|
||||
set { _paramDirection = value; }
|
||||
}
|
||||
|
||||
public string TryGetAltName()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(AltName) && AltName != Name)
|
||||
{
|
||||
return AltName;
|
||||
}
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Marr.Data.Converters;
|
||||
using Marr.Data.Reflection;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains information about the class fields and their associated stored proc parameters
|
||||
/// </summary>
|
||||
public class ColumnMap
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Creates a column map with an empty ColumnInfo object.
|
||||
/// </summary>
|
||||
/// <param name="member">The .net member that is being mapped.</param>
|
||||
public ColumnMap(MemberInfo member)
|
||||
: this(member, new ColumnInfo())
|
||||
{ }
|
||||
|
||||
public ColumnMap(MemberInfo member, IColumnInfo columnInfo)
|
||||
{
|
||||
FieldName = member.Name;
|
||||
ColumnInfo = columnInfo;
|
||||
|
||||
// If the column name is not specified, the field name will be used.
|
||||
if (string.IsNullOrEmpty(columnInfo.Name))
|
||||
columnInfo.Name = member.Name;
|
||||
|
||||
FieldType = ReflectionHelper.GetMemberType(member);
|
||||
Type paramNetType = FieldType;
|
||||
|
||||
Converter = MapRepository.Instance.GetConverter(FieldType);
|
||||
if (Converter != null)
|
||||
{
|
||||
paramNetType = Converter.DbType;
|
||||
}
|
||||
|
||||
DBType = MapRepository.Instance.DbTypeBuilder.GetDbType(paramNetType);
|
||||
|
||||
Getter = MapRepository.Instance.ReflectionStrategy.BuildGetter(member.DeclaringType, FieldName);
|
||||
Setter = MapRepository.Instance.ReflectionStrategy.BuildSetter(member.DeclaringType, FieldName);
|
||||
}
|
||||
|
||||
public string FieldName { get; set; }
|
||||
public Type FieldType { get; set; }
|
||||
public Enum DBType { get; set; }
|
||||
public IColumnInfo ColumnInfo { get; set; }
|
||||
|
||||
public GetterDelegate Getter { get; private set; }
|
||||
public SetterDelegate Setter { get; private set; }
|
||||
public IConverter Converter { get; private set; }
|
||||
}
|
||||
}
|
@ -1,172 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Data.Common;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
/// <summary>
|
||||
/// This class contains a list of column mappings.
|
||||
/// It also provides various methods to filter the collection.
|
||||
/// </summary>
|
||||
public class ColumnMapCollection : List<ColumnMap>
|
||||
{
|
||||
#region - Filters -
|
||||
|
||||
public ColumnMap GetByColumnName(string columnName)
|
||||
{
|
||||
return Find(m => m.ColumnInfo.Name == columnName);
|
||||
}
|
||||
|
||||
public ColumnMap GetByFieldName(string fieldName)
|
||||
{
|
||||
return Find(m => m.FieldName == fieldName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Iterates through all fields marked as return values.
|
||||
/// </summary>
|
||||
public IEnumerable<ColumnMap> ReturnValues
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (ColumnMap map in this)
|
||||
if (map.ColumnInfo.ReturnValue)
|
||||
yield return map;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Iterates through all fields that are not return values.
|
||||
/// </summary>
|
||||
public ColumnMapCollection NonReturnValues
|
||||
{
|
||||
get
|
||||
{
|
||||
ColumnMapCollection collection = new ColumnMapCollection();
|
||||
|
||||
foreach (ColumnMap map in this)
|
||||
if (!map.ColumnInfo.ReturnValue)
|
||||
collection.Add(map);
|
||||
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Iterates through all fields marked as Output parameters or InputOutput.
|
||||
/// </summary>
|
||||
public IEnumerable<ColumnMap> OutputFields
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (ColumnMap map in this)
|
||||
if (map.ColumnInfo.ParamDirection == ParameterDirection.InputOutput ||
|
||||
map.ColumnInfo.ParamDirection == ParameterDirection.Output)
|
||||
yield return map;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Iterates through all fields marked as primary keys.
|
||||
/// </summary>
|
||||
public ColumnMapCollection PrimaryKeys
|
||||
{
|
||||
get
|
||||
{
|
||||
ColumnMapCollection keys = new ColumnMapCollection();
|
||||
foreach (ColumnMap map in this)
|
||||
if (map.ColumnInfo.IsPrimaryKey)
|
||||
keys.Add(map);
|
||||
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses and orders the parameters from the query text.
|
||||
/// Filters the list of mapped columns to match the parameters found in the sql query.
|
||||
/// All parameters starting with the '@' or ':' symbol are matched and returned.
|
||||
/// </summary>
|
||||
/// <param name="command">The command and parameters that are being parsed.</param>
|
||||
/// <returns>A list of mapped columns that are present in the sql statement as parameters.</returns>
|
||||
public ColumnMapCollection OrderParameters(DbCommand command)
|
||||
{
|
||||
if (command.CommandType == CommandType.Text && Count > 0)
|
||||
{
|
||||
string commandTypeString = command.GetType().ToString();
|
||||
if (commandTypeString.Contains("Oracle") || commandTypeString.Contains("OleDb"))
|
||||
{
|
||||
ColumnMapCollection columns = new ColumnMapCollection();
|
||||
|
||||
// Find all @Parameters contained in the sql statement
|
||||
string paramPrefix = commandTypeString.Contains("Oracle") ? ":" : "@";
|
||||
string regexString = string.Format(@"{0}[\w-]+", paramPrefix);
|
||||
Regex regex = new Regex(regexString);
|
||||
foreach (Match m in regex.Matches(command.CommandText))
|
||||
{
|
||||
ColumnMap matchingColumn = Find(c => string.Concat(paramPrefix, c.ColumnInfo.Name.ToLower()) == m.Value.ToLower());
|
||||
if (matchingColumn != null)
|
||||
columns.Add(matchingColumn);
|
||||
}
|
||||
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Actions -
|
||||
|
||||
/// <summary>
|
||||
/// Set's each column's altname as the given prefix + the column name.
|
||||
/// Ex:
|
||||
/// Original column name: "ID"
|
||||
/// Passed in prefix: "PRODUCT_"
|
||||
/// Generated AltName: "PRODUCT_ID"
|
||||
/// </summary>
|
||||
/// <param name="prefix">The given prefix.</param>
|
||||
/// <returns></returns>
|
||||
public ColumnMapCollection PrefixAltNames(string prefix)
|
||||
{
|
||||
ForEach(c => c.ColumnInfo.AltName = c.ColumnInfo.Name.Insert(0, prefix));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set's each column's altname as the column name + the given prefix.
|
||||
/// Ex:
|
||||
/// Original column name: "ID"
|
||||
/// Passed in suffix: "_PRODUCT"
|
||||
/// Generated AltName: "ID_PRODUCT"
|
||||
/// </summary>
|
||||
/// <param name="suffix"></param>
|
||||
/// <returns></returns>
|
||||
public ColumnMapCollection SuffixAltNames(string suffix)
|
||||
{
|
||||
ForEach(c => c.ColumnInfo.AltName = c.ColumnInfo.Name + suffix);
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System.Data;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
public interface IColumnInfo
|
||||
{
|
||||
string Name { get; set; }
|
||||
string AltName { get; set; }
|
||||
int Size { get; set; }
|
||||
bool IsPrimaryKey { get; set; }
|
||||
bool IsAutoIncrement { get; set; }
|
||||
bool ReturnValue { get; set; }
|
||||
ParameterDirection ParamDirection { get; set; }
|
||||
string TryGetAltName();
|
||||
}
|
||||
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
public interface IRelationshipInfo
|
||||
{
|
||||
RelationshipTypes RelationType { get; set; }
|
||||
Type EntityType { get; set; }
|
||||
}
|
||||
|
||||
public enum RelationshipTypes
|
||||
{
|
||||
AutoDetect,
|
||||
One,
|
||||
Many
|
||||
}
|
||||
}
|
@ -1,194 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Data.Common;
|
||||
using Marr.Data.Converters;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
internal class MappingHelper
|
||||
{
|
||||
private MapRepository _repos;
|
||||
private IDataMapper _db;
|
||||
|
||||
public MappingHelper(IDataMapper db)
|
||||
{
|
||||
_repos = MapRepository.Instance;
|
||||
_db = db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates an entity and loads its mapped fields with the data from the reader.
|
||||
/// </summary>
|
||||
public object CreateAndLoadEntity<T>(ColumnMapCollection mappings, DbDataReader reader, bool useAltName)
|
||||
{
|
||||
return CreateAndLoadEntity(typeof(T), mappings, reader, useAltName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates an entity and loads its mapped fields with the data from the reader.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The entity being created and loaded.</param>
|
||||
/// <param name="mappings">The field mappings for the passed in entity.</param>
|
||||
/// <param name="reader">The open data reader.</param>
|
||||
/// <param name="useAltNames">Determines if the column AltName should be used.</param>
|
||||
/// <returns>Returns an entity loaded with data.</returns>
|
||||
public object CreateAndLoadEntity(Type entityType, ColumnMapCollection mappings, DbDataReader reader, bool useAltName)
|
||||
{
|
||||
// Create new entity
|
||||
object ent = _repos.ReflectionStrategy.CreateInstance(entityType);
|
||||
return LoadExistingEntity(mappings, reader, ent, useAltName);
|
||||
}
|
||||
|
||||
public object LoadExistingEntity(ColumnMapCollection mappings, DbDataReader reader, object ent, bool useAltName)
|
||||
{
|
||||
// Populate entity fields from data reader
|
||||
foreach (ColumnMap dataMap in mappings)
|
||||
{
|
||||
object dbValue = null;
|
||||
try
|
||||
{
|
||||
string colName = dataMap.ColumnInfo.GetColumName(useAltName);
|
||||
int ordinal = reader.GetOrdinal(colName);
|
||||
dbValue = reader.GetValue(ordinal);
|
||||
|
||||
// Handle conversions
|
||||
if (dataMap.Converter != null)
|
||||
{
|
||||
var convertContext = new ConverterContext
|
||||
{
|
||||
DbValue = dbValue,
|
||||
ColumnMap = dataMap,
|
||||
MapCollection = mappings,
|
||||
DataRecord = reader
|
||||
};
|
||||
|
||||
dbValue = dataMap.Converter.FromDB(convertContext);
|
||||
}
|
||||
|
||||
if (dbValue != DBNull.Value && dbValue != null)
|
||||
{
|
||||
dataMap.Setter(ent, dbValue);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string msg = string.Format("The DataMapper was unable to load the following field: '{0}' value: '{1}'. {2}",
|
||||
dataMap.ColumnInfo.Name, dbValue, ex.Message);
|
||||
|
||||
throw new DataMappingException(msg, ex);
|
||||
}
|
||||
}
|
||||
|
||||
PrepareLazyLoadedProperties(ent);
|
||||
|
||||
return ent;
|
||||
}
|
||||
|
||||
private void PrepareLazyLoadedProperties(object ent)
|
||||
{
|
||||
// Handle lazy loaded properties
|
||||
Type entType = ent.GetType();
|
||||
if (_repos.Relationships.ContainsKey(entType))
|
||||
{
|
||||
var provider = _db.ProviderFactory;
|
||||
var connectionString = _db.ConnectionString;
|
||||
Func<IDataMapper> dbCreate = () =>
|
||||
{
|
||||
var db = new DataMapper(provider, connectionString);
|
||||
db.SqlMode = SqlModes.Text;
|
||||
return db;
|
||||
};
|
||||
|
||||
var relationships = _repos.Relationships[entType];
|
||||
foreach (var rel in relationships.Where(r => r.IsLazyLoaded))
|
||||
{
|
||||
var lazyLoaded = (ILazyLoaded)rel.LazyLoaded.Clone();
|
||||
lazyLoaded.Prepare(dbCreate, ent);
|
||||
rel.Setter(ent, lazyLoaded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public T LoadSimpleValueFromFirstColumn<T>(DbDataReader reader)
|
||||
{
|
||||
try
|
||||
{
|
||||
return (T)reader.GetValue(0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string firstColumnName = reader.GetName(0);
|
||||
string msg = string.Format("The DataMapper was unable to create a value of type '{0}' from the first column '{1}'.",
|
||||
typeof(T).Name, firstColumnName);
|
||||
|
||||
throw new DataMappingException(msg, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates all parameters for a SP based on the mappings of the entity,
|
||||
/// and assigns them values based on the field values of the entity.
|
||||
/// </summary>
|
||||
public void CreateParameters<T>(T entity, ColumnMapCollection columnMapCollection, bool isAutoQuery)
|
||||
{
|
||||
ColumnMapCollection mappings = columnMapCollection;
|
||||
|
||||
if (!isAutoQuery)
|
||||
{
|
||||
// Order columns (applies to Oracle and OleDb only)
|
||||
mappings = columnMapCollection.OrderParameters(_db.Command);
|
||||
}
|
||||
|
||||
foreach (ColumnMap columnMap in mappings)
|
||||
{
|
||||
if (columnMap.ColumnInfo.IsAutoIncrement)
|
||||
continue;
|
||||
|
||||
var param = _db.Command.CreateParameter();
|
||||
param.ParameterName = columnMap.ColumnInfo.Name;
|
||||
param.Size = columnMap.ColumnInfo.Size;
|
||||
param.Direction = columnMap.ColumnInfo.ParamDirection;
|
||||
|
||||
object val = columnMap.Getter(entity);
|
||||
|
||||
param.Value = val ?? DBNull.Value; // Convert nulls to DBNulls
|
||||
|
||||
if (columnMap.Converter != null)
|
||||
{
|
||||
param.Value = columnMap.Converter.ToDB(param.Value);
|
||||
}
|
||||
|
||||
// Set the appropriate DbType property depending on the parameter type
|
||||
// Note: the columnMap.DBType property was set when the ColumnMap was created
|
||||
MapRepository.Instance.DbTypeBuilder.SetDbType(param, columnMap.DBType);
|
||||
|
||||
_db.Command.Parameters.Add(param);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Assigns the SP result columns to the passed in 'mappings' fields.
|
||||
/// </summary>
|
||||
public void SetOutputValues<T>(T entity, IEnumerable<ColumnMap> mappings)
|
||||
{
|
||||
foreach (ColumnMap dataMap in mappings)
|
||||
{
|
||||
object output = _db.Command.Parameters[dataMap.ColumnInfo.Name].Value;
|
||||
dataMap.Setter(entity, output);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Assigns the passed in 'value' to the passed in 'mappings' fields.
|
||||
/// </summary>
|
||||
public void SetOutputValues<T>(T entity, IEnumerable<ColumnMap> mappings, object value)
|
||||
{
|
||||
foreach (ColumnMap dataMap in mappings)
|
||||
{
|
||||
dataMap.Setter(entity, Convert.ChangeType(value, dataMap.FieldType));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using Marr.Data.Reflection;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
public class Relationship
|
||||
{
|
||||
|
||||
public Relationship(MemberInfo member)
|
||||
: this(member, new RelationshipInfo())
|
||||
{ }
|
||||
|
||||
public Relationship(MemberInfo member, IRelationshipInfo relationshipInfo)
|
||||
{
|
||||
Member = member;
|
||||
|
||||
MemberType = ReflectionHelper.GetMemberType(member);
|
||||
|
||||
// Try to determine the RelationshipType
|
||||
if (relationshipInfo.RelationType == RelationshipTypes.AutoDetect)
|
||||
{
|
||||
if (typeof(ICollection).IsAssignableFrom(MemberType))
|
||||
{
|
||||
relationshipInfo.RelationType = RelationshipTypes.Many;
|
||||
}
|
||||
else
|
||||
{
|
||||
relationshipInfo.RelationType = RelationshipTypes.One;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to determine the EntityType
|
||||
if (relationshipInfo.EntityType == null)
|
||||
{
|
||||
if (relationshipInfo.RelationType == RelationshipTypes.Many)
|
||||
{
|
||||
if (MemberType.IsGenericType)
|
||||
{
|
||||
// Assume a Collection<T> or List<T> and return T
|
||||
relationshipInfo.EntityType = MemberType.GetGenericArguments()[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException(string.Format(
|
||||
"The DataMapper could not determine the RelationshipAttribute EntityType for {0}.",
|
||||
MemberType.Name));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
relationshipInfo.EntityType = MemberType;
|
||||
}
|
||||
}
|
||||
|
||||
RelationshipInfo = relationshipInfo;
|
||||
|
||||
|
||||
|
||||
Setter = MapRepository.Instance.ReflectionStrategy.BuildSetter(member.DeclaringType, member.Name);
|
||||
}
|
||||
|
||||
public IRelationshipInfo RelationshipInfo { get; private set; }
|
||||
|
||||
public MemberInfo Member { get; private set; }
|
||||
|
||||
public Type MemberType { get; private set; }
|
||||
|
||||
public bool IsLazyLoaded
|
||||
{
|
||||
get
|
||||
{
|
||||
return LazyLoaded != null;
|
||||
}
|
||||
}
|
||||
|
||||
public ILazyLoaded LazyLoaded { get; set; }
|
||||
|
||||
public GetterDelegate Getter { get; set; }
|
||||
public SetterDelegate Setter { get; set; }
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a field as a related entity that needs to be created at filled with data.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
|
||||
public class RelationshipAttribute : Attribute, IRelationshipInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a data relationship.
|
||||
/// </summary>
|
||||
public RelationshipAttribute()
|
||||
: this(RelationshipTypes.AutoDetect)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Defines a data relationship.
|
||||
/// </summary>
|
||||
/// <param name="relationType"></param>
|
||||
public RelationshipAttribute(RelationshipTypes relationType)
|
||||
{
|
||||
RelationType = relationType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a One-ToMany data relationship for a given type.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The type of the child entity.</param>
|
||||
public RelationshipAttribute(Type entityType)
|
||||
: this(entityType, RelationshipTypes.AutoDetect)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Defines a data relationship.
|
||||
/// </summary>
|
||||
/// <param name="entityType">The type of the child entity.</param>
|
||||
/// <param name="relationType">The relationship type can be "One" or "Many".</param>
|
||||
public RelationshipAttribute(Type entityType, RelationshipTypes relationType)
|
||||
{
|
||||
EntityType = entityType;
|
||||
RelationType = relationType;
|
||||
}
|
||||
|
||||
#region IRelationshipInfo Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the relationship type can be "One" or "Many".
|
||||
/// </summary>
|
||||
public RelationshipTypes RelationType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the type of the child entity.
|
||||
/// </summary>
|
||||
public Type EntityType { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
public class RelationshipCollection : List<Relationship>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a ColumnMap by its field name.
|
||||
/// </summary>
|
||||
/// <param name="fieldName"></param>
|
||||
/// <returns></returns>
|
||||
public Relationship this[string fieldName]
|
||||
{
|
||||
get
|
||||
{
|
||||
return Find(m => m.Member.Name == fieldName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
namespace Marr.Data
|
||||
{
|
||||
public enum SqlModes
|
||||
{
|
||||
StoredProcedure,
|
||||
Text
|
||||
}
|
||||
}
|
Loading…
Reference in new issue