This commit is contained in:
grzegorz.russek
2014-04-06 12:10:17 +00:00
parent 397a8da830
commit 1de570be42
13 changed files with 155 additions and 82 deletions

View File

@@ -53,22 +53,6 @@ namespace DynamORM.Builders
/// <returns>Result of a query.</returns>
object Scalar();
#region SubQuery
/// <summary>Adds to the 'From' clause of sub query the contents obtained by
/// parsing the dynamic lambda expressions given. The supported formats are:
/// <para>- Resolve to a string: 'x => "Table AS Alias', where the alias part is optional.</para>
/// <para>- Resolve to an expression: 'x => x.Table.As( x.Alias )', where the alias part is optional.</para>
/// <para>- Generic expression: 'x => x( expression ).As( x.Alias )', where the alias part is mandatory. In this
/// case the alias is not annotated.</para>
/// </summary>
/// <param name="subquery">First argument is parent query, second one is a subquery.</param>
/// <param name="func">The specification for subquery.</param>
/// <returns>This instance to permit chaining.</returns>
IDynamicSelectQueryBuilder SubQuery(Action<IDynamicSelectQueryBuilder, IDynamicSelectQueryBuilder> subquery, params Func<dynamic, object>[] func);
#endregion SubQuery
#region From/Join
/// <summary>

View File

@@ -360,7 +360,7 @@ namespace DynamORM.Builders.Implementation
return ParseConstant(node, pars, columnSchema);
}
protected virtual string ParseCommand(DynamicQueryBuilder node, IDictionary<string, IParameter> pars = null)
internal virtual string ParseCommand(DynamicQueryBuilder node, IDictionary<string, IParameter> pars = null)
{
// Getting the command's text...
string str = node.CommandText(); // Avoiding spurious "OUTPUT XXX" statements

View File

@@ -241,34 +241,6 @@ namespace DynamORM.Builders.Implementation
#endregion Execution
#region Subquery
/// <summary>
/// Adds to the 'From' clause of sub query the contents obtained by
/// parsing the dynamic lambda expressions given. The supported formats are:
/// <para>- Resolve to a string: 'x =&gt; "Table AS Alias', where the alias part is optional.</para>
/// <para>- Resolve to an expression: 'x =&gt; x.Table.As( x.Alias )', where the alias part is optional.</para>
/// <para>- Generic expression: 'x =&gt; x( expression ).As( x.Alias )', where the alias part is mandatory. In this
/// case the alias is not annotated.</para>
/// </summary>
/// <param name="subquery">First argument is parent query, second one is a subquery.</param>
/// <param name="func">The specification for subquery.</param>
/// <returns>
/// This instance to permit chaining.
/// </returns>
public virtual IDynamicSelectQueryBuilder SubQuery(Action<IDynamicSelectQueryBuilder, IDynamicSelectQueryBuilder> subquery, params Func<dynamic, object>[] func)
{
var sub = func == null || func.Length == 0 ? base.SubQuery() : base.SubQuery(func);
subquery(this, sub);
ParseCommand(sub as DynamicQueryBuilder, Parameters);
return this;
}
#endregion Subquery
#region From/Join
/// <summary>

View File

@@ -13,6 +13,11 @@
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<SignAssembly>False</SignAssembly>
<DelaySign>False</DelaySign>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -26,17 +31,23 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>PdbOnly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<DebugSymbols>true</DebugSymbols>
<DebugSymbols>false</DebugSymbols>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DocumentationFile>bin\Release\DynamORM.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />

View File

@@ -286,6 +286,61 @@ namespace DynamORM
{
return new DynamicInsertQueryBuilder(this).Table(typeof(T));
}
public virtual int Insert<T>(IEnumerable<T> e) where T : class
{
int affected = 0;
var mapper = DynamicMapperCache.GetMapper(typeof(T));
if (mapper != null)
using (var con = Open())
using (var tra = con.BeginTransaction())
using (var cmd = con.CreateCommand())
{
try
{
DynamicPropertyInvoker currentprop = null;
var temp = new Dictionary<string, DynamicPropertyInvoker>();
var parameters = new Dictionary<IDbDataParameter, DynamicPropertyInvoker>();
var ib = Insert<T>()
.SetVirtualMode(true)
.CreateTemporaryParameterAction(p => temp[p.Name] = currentprop)
.CreateParameterAction((p, cp) => parameters[cp] = temp[p.Name]);
foreach (var prop in mapper.PropertyMap)
if (!mapper.Ignored.Contains(prop.Key))
{
var col = mapper.PropertyMap.TryGetValue(prop.Key) ?? prop.Key;
currentprop = mapper.ColumnsMap.TryGetValue(col.ToLower());
if (currentprop.Get != null)
ib.Insert(col, null);
}
ib.FillCommand(cmd);
foreach (var o in e)
{
foreach (var m in parameters)
m.Key.Value = m.Value.Get(o);
affected += cmd.ExecuteNonQuery();
}
tra.Commit();
}
catch
{
if (tra != null)
tra.Rollback();
throw;
}
}
return affected;
}
/// <summary>
/// Adds to the <code>UPDATE</code> clause the contents obtained by parsing the dynamic lambda expressions given. The supported

View File

@@ -816,17 +816,6 @@ namespace DynamORM
return b;
}
/// <summary>Sets the virtual mode on builder.</summary>
/// <typeparam name="T">Class implementing <see cref="IDynamicQueryBuilder"/> interface.</typeparam>
/// <param name="b">The builder on which set delegate.</param>
/// <param name="virtualMode">Virtual mode.</param>
/// <returns>Returns instance of builder on which virtual mode is set.</returns>
public static T SetVirtualMode<T>(this T b, bool virtualMode) where T : IDynamicQueryBuilder
{
b.VirtualMode = virtualMode;
return b;
}
/// <summary>Sets the on create real parameter action.</summary>
/// <typeparam name="T">Class implementing <see cref="IDynamicQueryBuilder"/> interface.</typeparam>
/// <param name="b">The builder on which set delegate.</param>
@@ -838,6 +827,33 @@ namespace DynamORM
return b;
}
/// <summary>Sets the virtual mode on builder.</summary>
/// <typeparam name="T">Class implementing <see cref="IDynamicQueryBuilder"/> interface.</typeparam>
/// <param name="b">The builder on which set virtual mode.</param>
/// <param name="virtualMode">Virtual mode.</param>
/// <returns>Returns instance of builder on which virtual mode is set.</returns>
public static T SetVirtualMode<T>(this T b, bool virtualMode) where T : IDynamicQueryBuilder
{
b.VirtualMode = virtualMode;
return b;
}
/// <summary>Creates sub query that can be used inside of from/join/expresions.</summary>
/// <param name="b">The builder that will be parent of new sub query.</param>
/// <param name="subquery">First argument is parent query, second one is a subquery.</param>
/// <param name="func">The specification for subquery.</param>
/// <returns>This instance to permit chaining.</returns>
public static T SubQuery<T>(this T b, Action<T, IDynamicSelectQueryBuilder> subquery, params Func<dynamic, object>[] func) where T : IDynamicQueryBuilder
{
var sub = func == null || func.Length == 0 ? b.SubQuery() : b.SubQuery(func);
subquery(b, sub);
(b as DynamicQueryBuilder).ParseCommand(sub as DynamicQueryBuilder, b.Parameters);
return b;
}
#endregion Dynamic builders extensions
#region Dynamic extensions

View File

@@ -46,9 +46,11 @@ namespace DynamORM.Mapper
public Func<object> Creator { get; private set; }
/// <summary>Gets map of columns to properties.</summary>
/// <remarks>Key: Column name (lower), Value: <see cref="DynamicPropertyInvoker"/>.</remarks>
public Dictionary<string, DynamicPropertyInvoker> ColumnsMap { get; private set; }
/// <summary>Gets map of properties to column.</summary>
/// <remarks>Key: Property name, Value: Column name.</remarks>
public Dictionary<string, string> PropertyMap { get; private set; }
/// <summary>Gets list of ignored properties.</summary>
@@ -73,6 +75,7 @@ namespace DynamORM.Mapper
{
var columnMap = new Dictionary<string, DynamicPropertyInvoker>();
var propertyMap = new Dictionary<string, string>();
var ignored = new List<string>();
foreach (var pi in Type.GetProperties())
{
@@ -89,12 +92,15 @@ namespace DynamORM.Mapper
columnMap.Add(col.ToLower(), val);
propertyMap.Add(pi.Name, col);
if (val.Ignore)
ignored.Add(pi.Name);
}
ColumnsMap = columnMap;
PropertyMap = propertyMap;
Ignored = columnMap.Where(i => i.Value.Ignore).Select(i => i.Value.Name).ToList();
Ignored = ignored; ////columnMap.Where(i => i.Value.Ignore).Select(i => i.Value.Name).ToList();
}
private Func<object> CreateCreator()