This commit is contained in:
grzegorz.russek
2014-07-17 15:43:23 +00:00
parent 41a2103bdc
commit baaa6e9452
7 changed files with 1300 additions and 1019 deletions

View File

@@ -33,30 +33,30 @@
* * DYNAMORM_OMMIT_TRYPARSE - Remove TryParse helpers (also applies DYNAMORM_OMMIT_GENERICEXECUTION) * * DYNAMORM_OMMIT_TRYPARSE - Remove TryParse helpers (also applies DYNAMORM_OMMIT_GENERICEXECUTION)
*/ */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.Common;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using DynamORM.Builders;
using DynamORM.Builders.Extensions; using DynamORM.Builders.Extensions;
using DynamORM.Builders.Implementation; using DynamORM.Builders.Implementation;
using DynamORM.Helpers; using DynamORM.Builders;
using DynamORM.Helpers.Dynamics; using DynamORM.Helpers.Dynamics;
using DynamORM.Helpers;
using DynamORM.Mapper; using DynamORM.Mapper;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;
using System.Data.Common;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Linq.Expressions;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
using System;
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "This is a generated file which generates all the necessary support classes.")] [module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "This is a generated file which generates all the necessary support classes.")]
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1403:FileMayOnlyContainASingleNamespace", Justification = "This is a generated file which generates all the necessary support classes.")] [module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1403:FileMayOnlyContainASingleNamespace", Justification = "This is a generated file which generates all the necessary support classes.")]
namespace DynamORM namespace DynamORM
{ {
/// <summary>Small utility class to manage single columns.</summary> /// <summary>Small utility class to manage single columns.</summary>
@@ -1756,8 +1756,8 @@ namespace DynamORM
} }
/// <summary>Builds query cache if necessary and returns it.</summary> /// <summary>Builds query cache if necessary and returns it.</summary>
/// <param name="sql">Sql query from which read schema.</param> /// <param name="sql">SQL query from which read schema.</param>
/// <param name="args">Sql query argumants.</param> /// <param name="args">SQL query arguments.</param>
/// <returns>Query schema.</returns> /// <returns>Query schema.</returns>
public Dictionary<string, DynamicSchemaColumn> GetQuerySchema(string sql, params object[] args) public Dictionary<string, DynamicSchemaColumn> GetQuerySchema(string sql, params object[] args)
{ {
@@ -1829,7 +1829,7 @@ namespace DynamORM
.SetCommand(string.Format("SELECT * FROM {0}{1} WHERE 1 = 0", .SetCommand(string.Format("SELECT * FROM {0}{1} WHERE 1 = 0",
!string.IsNullOrEmpty(owner) ? string.Format("{0}.", DecorateName(owner)) : string.Empty, !string.IsNullOrEmpty(owner) ? string.Format("{0}.", DecorateName(owner)) : string.Empty,
DecorateName(table)))) DecorateName(table))))
return ReadSchema(cmd); return ReadSchema(cmd).ToList();
} }
/// <summary>Get schema describing objects from reader.</summary> /// <summary>Get schema describing objects from reader.</summary>
@@ -3327,8 +3327,7 @@ namespace DynamORM
/// <returns>Returns <c>true</c> if it does.</returns> /// <returns>Returns <c>true</c> if it does.</returns>
public static bool IsGenericEnumerable(this Type type) public static bool IsGenericEnumerable(this Type type)
{ {
return type.IsGenericType && return type.IsGenericType && type.GetInterfaces().Any(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
typeof(IEnumerable<>).IsAssignableFrom(type.GetGenericTypeDefinition());
} }
/// <summary>Check if type implements IEnumerable&lt;&gt; interface.</summary> /// <summary>Check if type implements IEnumerable&lt;&gt; interface.</summary>
@@ -3364,7 +3363,7 @@ namespace DynamORM
return type.GetElementType().IsValueType; return type.GetElementType().IsValueType;
else else
{ {
if (type.IsGenericType) if (type.IsGenericType && type.GetInterfaces().Any(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{ {
Type[] gt = type.GetGenericArguments(); Type[] gt = type.GetGenericArguments();
@@ -3375,6 +3374,30 @@ namespace DynamORM
return false; return false;
} }
/// <summary>Gets <see cref="System.Data.DbType"/> corresponding to the
/// provided <see cref="System.Type"/>.</summary>
/// <param name="t">The type to be converted.</param>
/// <returns>Returns <see cref="System.Data.DbType"/> corresponding to the
/// provided <see cref="System.Type"/>.</returns>
public static DbType ToDbType(this Type t)
{
return TypeMap.TryGetNullable(t) ?? DbType.Object;
}
/// <summary>Gets <see cref="System.Type"/> corresponding to the
/// provided <see cref="System.Data.DbType"/>.</summary>
/// <param name="dbt">The type to be converted.</param>
/// <returns>Returns <see cref="System.Type"/> corresponding to the
/// provided <see cref="System.Data.DbType"/>.</returns>
public static Type ToType(this DbType dbt)
{
foreach (var tdbt in TypeMap)
if (tdbt.Value == dbt)
return tdbt.Key;
return typeof(object);
}
#endregion Type extensions #endregion Type extensions
#region IDictionary extensions #region IDictionary extensions
@@ -3426,6 +3449,23 @@ namespace DynamORM
#endregion IDictionary extensions #endregion IDictionary extensions
#region IDataReader extensions
/// <summary>Gets the <see cref="System.Data.DbType"/> information corresponding
/// to the type of <see cref="System.Object"/> that would be returned from
/// <see cref="System.Data.IDataRecord.GetValue(System.Int32)"/>.</summary>
/// <param name="r">The data reader.</param>
/// <param name="i">The index of the field to find.</param>
/// <returns>The <see cref="System.Data.DbType"/> information corresponding to the
/// type of <see cref="System.Object"/> that would be returned from
/// <see cref="System.Data.IDataRecord.GetValue(System.Int32)"/>.</returns>
public static DbType GetFieldDbType(this IDataReader r, int i)
{
return TypeMap.TryGetNullable(r.GetFieldType(i)) ?? DbType.String;
}
#endregion IDataReader extensions
#region Mapper extensions #region Mapper extensions
/// <summary>MapEnumerable object enumerator into specified type.</summary> /// <summary>MapEnumerable object enumerator into specified type.</summary>
@@ -4898,6 +4938,7 @@ namespace DynamORM
IDynamicDeleteQueryBuilder Where(object conditions, bool schema = false); IDynamicDeleteQueryBuilder Where(object conditions, bool schema = false);
} }
/// <summary>Dynamic insert query builder interface.</summary> /// <summary>Dynamic insert query builder interface.</summary>
/// <remarks>This interface it publically available. Implementation should be hidden.</remarks> /// <remarks>This interface it publically available. Implementation should be hidden.</remarks>
public interface IDynamicInsertQueryBuilder : IDynamicQueryBuilder public interface IDynamicInsertQueryBuilder : IDynamicQueryBuilder
@@ -4929,6 +4970,7 @@ namespace DynamORM
IDynamicInsertQueryBuilder Insert(object o); IDynamicInsertQueryBuilder Insert(object o);
} }
/// <summary>Dynamic query builder base interface.</summary> /// <summary>Dynamic query builder base interface.</summary>
/// <remarks>This interface it publically available. Implementation should be hidden.</remarks> /// <remarks>This interface it publically available. Implementation should be hidden.</remarks>
public interface IDynamicQueryBuilder public interface IDynamicQueryBuilder
@@ -4969,6 +5011,7 @@ namespace DynamORM
Action<IParameter, IDbDataParameter> OnCreateParameter { get; set; } Action<IParameter, IDbDataParameter> OnCreateParameter { get; set; }
} }
/// <summary>Dynamic select query builder interface.</summary> /// <summary>Dynamic select query builder interface.</summary>
/// <remarks>This interface it publically available. Implementation should be hidden.</remarks> /// <remarks>This interface it publically available. Implementation should be hidden.</remarks>
public interface IDynamicSelectQueryBuilder : IDynamicQueryBuilder ////, IEnumerable<object> public interface IDynamicSelectQueryBuilder : IDynamicQueryBuilder ////, IEnumerable<object>
@@ -5172,6 +5215,7 @@ namespace DynamORM
#endregion Top/Limit/Offset/Distinct #endregion Top/Limit/Offset/Distinct
} }
/// <summary>Dynamic update query builder interface.</summary> /// <summary>Dynamic update query builder interface.</summary>
/// <remarks>This interface it publically available. Implementation should be hidden.</remarks> /// <remarks>This interface it publically available. Implementation should be hidden.</remarks>
public interface IDynamicUpdateQueryBuilder : IDynamicQueryBuilder public interface IDynamicUpdateQueryBuilder : IDynamicQueryBuilder
@@ -5262,6 +5306,7 @@ namespace DynamORM
#endregion Where #endregion Where
} }
/// <summary>Interface describing parameter info.</summary> /// <summary>Interface describing parameter info.</summary>
public interface IParameter public interface IParameter
{ {
@@ -5285,6 +5330,7 @@ namespace DynamORM
DynamicSchemaColumn? Schema { get; set; } DynamicSchemaColumn? Schema { get; set; }
} }
/// <summary>Interface describing table information.</summary> /// <summary>Interface describing table information.</summary>
public interface ITableInfo public interface ITableInfo
{ {
@@ -5301,6 +5347,7 @@ namespace DynamORM
Dictionary<string, DynamicSchemaColumn> Schema { get; } Dictionary<string, DynamicSchemaColumn> Schema { get; }
} }
namespace Extensions namespace Extensions
{ {
internal static class DynamicModifyBuilderExtensions internal static class DynamicModifyBuilderExtensions
@@ -5424,6 +5471,7 @@ namespace DynamORM
} }
} }
internal static class DynamicWhereQueryExtensions internal static class DynamicWhereQueryExtensions
{ {
#region Where #region Where
@@ -5616,6 +5664,8 @@ namespace DynamORM
#endregion Where #endregion Where
} }
} }
namespace Implementation namespace Implementation
@@ -5712,6 +5762,7 @@ namespace DynamORM
#endregion Where #endregion Where
} }
/// <summary>Implementation of dynamic insert query builder.</summary> /// <summary>Implementation of dynamic insert query builder.</summary>
internal class DynamicInsertQueryBuilder : DynamicModifyBuilder, IDynamicInsertQueryBuilder internal class DynamicInsertQueryBuilder : DynamicModifyBuilder, IDynamicInsertQueryBuilder
{ {
@@ -5884,6 +5935,7 @@ namespace DynamORM
#endregion Insert #endregion Insert
} }
/// <summary>Base query builder for insert/update/delete statements.</summary> /// <summary>Base query builder for insert/update/delete statements.</summary>
internal abstract class DynamicModifyBuilder : DynamicQueryBuilder internal abstract class DynamicModifyBuilder : DynamicQueryBuilder
{ {
@@ -5923,6 +5975,7 @@ namespace DynamORM
} }
} }
/// <summary>Implementation of dynamic query builder base interface.</summary> /// <summary>Implementation of dynamic query builder base interface.</summary>
internal abstract class DynamicQueryBuilder : IDynamicQueryBuilder internal abstract class DynamicQueryBuilder : IDynamicQueryBuilder
{ {
@@ -6671,6 +6724,7 @@ namespace DynamORM
#endregion Helpers #endregion Helpers
} }
/// <summary>Implementation of dynamic select query builder.</summary> /// <summary>Implementation of dynamic select query builder.</summary>
internal class DynamicSelectQueryBuilder : DynamicQueryBuilder, IDynamicSelectQueryBuilder, DynamicQueryBuilder.IQueryWithWhere internal class DynamicSelectQueryBuilder : DynamicQueryBuilder, IDynamicSelectQueryBuilder, DynamicQueryBuilder.IQueryWithWhere
{ {
@@ -7056,6 +7110,7 @@ namespace DynamORM
_from = string.IsNullOrEmpty(_from) ? sb.ToString() : string.Format("{0}, {1}", _from, sb.ToString()); _from = string.IsNullOrEmpty(_from) ? sb.ToString() : string.Format("{0}, {1}", _from, sb.ToString());
} }
return index; return index;
} }
@@ -7505,6 +7560,7 @@ namespace DynamORM
if (!anon) if (!anon)
ParseSelectAddColumn(main, alias, all); ParseSelectAddColumn(main, alias, all);
} }
return index; return index;
} }
@@ -7577,6 +7633,7 @@ namespace DynamORM
else else
_groupby = string.Format("{0}, {1}", _groupby, main); _groupby = string.Format("{0}, {1}", _groupby, main);
} }
return index; return index;
} }
@@ -7705,6 +7762,7 @@ namespace DynamORM
else else
_orderby = string.Format("{0}, {1}", _orderby, main); _orderby = string.Format("{0}, {1}", _orderby, main);
} }
return index; return index;
} }
@@ -7926,6 +7984,7 @@ namespace DynamORM
#endregion Helpers #endregion Helpers
} }
/// <summary>Update query builder.</summary> /// <summary>Update query builder.</summary>
internal class DynamicUpdateQueryBuilder : DynamicModifyBuilder, IDynamicUpdateQueryBuilder, DynamicQueryBuilder.IQueryWithWhere internal class DynamicUpdateQueryBuilder : DynamicModifyBuilder, IDynamicUpdateQueryBuilder, DynamicQueryBuilder.IQueryWithWhere
{ {
@@ -8208,7 +8267,10 @@ namespace DynamORM
#endregion Where #endregion Where
} }
} }
} }
namespace Helpers namespace Helpers
@@ -8306,6 +8368,7 @@ namespace DynamORM
} }
} }
/// <summary>Framework detection and specific implementations.</summary> /// <summary>Framework detection and specific implementations.</summary>
public static class FrameworkTools public static class FrameworkTools
{ {
@@ -8426,6 +8489,7 @@ namespace DynamORM
#endregion GetGenericTypeArguments #endregion GetGenericTypeArguments
} }
/// <summary>Extends <see cref="IDisposable"/> interface.</summary> /// <summary>Extends <see cref="IDisposable"/> interface.</summary>
public interface IExtendedDisposable : IDisposable public interface IExtendedDisposable : IDisposable
{ {
@@ -8438,6 +8502,7 @@ namespace DynamORM
bool IsDisposed { get; } bool IsDisposed { get; }
} }
/// <summary>Class containing useful string extensions.</summary> /// <summary>Class containing useful string extensions.</summary>
internal static class StringExtensions internal static class StringExtensions
{ {
@@ -8735,6 +8800,7 @@ namespace DynamORM
} }
} }
/// <summary>Class contains unclassified extensions.</summary> /// <summary>Class contains unclassified extensions.</summary>
internal static class UnclassifiedExtensions internal static class UnclassifiedExtensions
{ {
@@ -8778,6 +8844,7 @@ namespace DynamORM
} }
} }
namespace Dynamics namespace Dynamics
{ {
/// <summary> /// <summary>
@@ -9874,6 +9941,7 @@ namespace DynamORM
#endregion Implementation of IExtendedDisposable #endregion Implementation of IExtendedDisposable
} }
/// <summary>Class that allows to use interfaces as dynamic objects.</summary> /// <summary>Class that allows to use interfaces as dynamic objects.</summary>
/// <typeparam name="T">Type of class to proxy.</typeparam> /// <typeparam name="T">Type of class to proxy.</typeparam>
/// <remarks>This is temporary solution. Which allows to use builders as a dynamic type.</remarks> /// <remarks>This is temporary solution. Which allows to use builders as a dynamic type.</remarks>
@@ -9897,12 +9965,18 @@ namespace DynamORM
_proxy = proxiedObject; _proxy = proxiedObject;
_type = typeof(T); _type = typeof(T);
_properties = _type.GetProperties() var members = GetAllMembers(_type);
_properties = members
.Where(x => x is PropertyInfo)
.ToDictionary( .ToDictionary(
k => k.Name, k => k.Name,
v => new DynamicPropertyInvoker(v, null)); v => new DynamicPropertyInvoker((PropertyInfo)v, null));
_methods = _type.GetMethods() _methods = members
.Where(x => x is MethodInfo)
.Cast<MethodInfo>()
.Where(m => !((m.Name.StartsWith("set_") && m.ReturnType == typeof(void)) || m.Name.StartsWith("get_")))
.Where(m => !m.IsStatic && !m.IsGenericMethod) .Where(m => !m.IsStatic && !m.IsGenericMethod)
.ToDictionary( .ToDictionary(
k => k, k => k,
@@ -10104,8 +10178,51 @@ namespace DynamORM
{ {
return arguments.Concat(parameters.Skip(arguments.Length).Select(p => p.DefaultValue)).ToArray(); return arguments.Concat(parameters.Skip(arguments.Length).Select(p => p.DefaultValue)).ToArray();
} }
private IEnumerable<MemberInfo> GetAllMembers(Type type)
{
if (type.IsInterface)
{
var members = new List<MemberInfo>();
var considered = new List<Type>();
var queue = new Queue<Type>();
considered.Add(type);
queue.Enqueue(type);
while (queue.Count > 0)
{
var subType = queue.Dequeue();
foreach (var subInterface in subType.GetInterfaces())
{
if (considered.Contains(subInterface)) continue;
considered.Add(subInterface);
queue.Enqueue(subInterface);
}
var typeProperties = subType.GetMembers(
BindingFlags.FlattenHierarchy
| BindingFlags.Public
| BindingFlags.Instance);
var newPropertyInfos = typeProperties
.Where(x => !members.Contains(x));
members.InsertRange(0, newPropertyInfos);
}
return members;
}
return type.GetMembers(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance);
}
} }
} }
} }
namespace Mapper namespace Mapper
@@ -10140,12 +10257,12 @@ namespace DynamORM
/// <remarks>Used when overriding schema.</remarks> /// <remarks>Used when overriding schema.</remarks>
public byte? Scale { get; set; } public byte? Scale { get; set; }
/// <summary>Gets or sets a value indicating whether this kolumn is no allowed to be inserted.</summary> /// <summary>Gets or sets a value indicating whether this column is no allowed to be inserted.</summary>
/// <remarks>This is only a suggesstion to automated mapping.</remarks> /// <remarks>This is only a suggestion to automated mapping.</remarks>
public bool IsNoInsert { get; set; } public bool IsNoInsert { get; set; }
/// <summary>Gets or sets a value indicating whether this kolumn is no allowed to be updated.</summary> /// <summary>Gets or sets a value indicating whether this column is no allowed to be updated.</summary>
/// <remarks>This is only a suggesstion to automated mapping.</remarks> /// <remarks>This is only a suggestion to automated mapping.</remarks>
public bool IsNoUpdate { get; set; } public bool IsNoUpdate { get; set; }
#region Constructors #region Constructors
@@ -10251,6 +10368,7 @@ namespace DynamORM
#endregion Constructors #endregion Constructors
} }
/// <summary>Class with mapper cache.</summary> /// <summary>Class with mapper cache.</summary>
public static class DynamicMapperCache public static class DynamicMapperCache
{ {
@@ -10292,6 +10410,7 @@ namespace DynamORM
} }
} }
/// <summary>Dynamic property invoker.</summary> /// <summary>Dynamic property invoker.</summary>
public class DynamicPropertyInvoker public class DynamicPropertyInvoker
{ {
@@ -10386,6 +10505,7 @@ namespace DynamORM
#endregion Type command cache #endregion Type command cache
} }
/// <summary>Represents type columnMap.</summary> /// <summary>Represents type columnMap.</summary>
public class DynamicTypeMap public class DynamicTypeMap
{ {
@@ -10501,6 +10621,7 @@ namespace DynamORM
#endregion Type command cache #endregion Type command cache
} }
/// <summary>Allows to add ignore action to property.</summary> /// <summary>Allows to add ignore action to property.</summary>
/// <remarks>Property still get's mapped from output.</remarks> /// <remarks>Property still get's mapped from output.</remarks>
[AttributeUsage(AttributeTargets.Property)] [AttributeUsage(AttributeTargets.Property)]
@@ -10508,6 +10629,7 @@ namespace DynamORM
{ {
} }
/// <summary>Allows to add table name to class.</summary> /// <summary>Allows to add table name to class.</summary>
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class TableAttribute : Attribute public class TableAttribute : Attribute
@@ -10524,5 +10646,9 @@ namespace DynamORM
/// set this to true to get schema from type.</remarks> /// set this to true to get schema from type.</remarks>
public bool Override { get; set; } public bool Override { get; set; }
} }
} }
} }

View File

@@ -597,6 +597,34 @@ namespace DynamORM.Tests.Select
Assert.AreEqual("SELECT Sum(\"dbo\".\"Users\".\"UserName\") FROM \"dbo\".\"Users\"", cmd.CommandText()); Assert.AreEqual("SELECT Sum(\"dbo\".\"Users\".\"UserName\") FROM \"dbo\".\"Users\"", cmd.CommandText());
} }
/// <summary>
/// Tests select aggregate field with alias (Sum).
/// </summary>
[Test]
public void TestSelectAggregateField4()
{
IDynamicSelectQueryBuilder cmd = new DynamicSelectQueryBuilder(Database);
cmd.From(u => u.dbo.Users.As(u.c))
.Select(u => u.Sum(u("\"UserName\"")).As(u.Name));
Assert.AreEqual("SELECT Sum(\"UserName\") AS \"Name\" FROM \"dbo\".\"Users\" AS c", cmd.CommandText());
}
/// <summary>
/// Tests select aggregate field with alias (Sum).
/// </summary>
[Test]
public void TestSelectAggregateField5()
{
IDynamicSelectQueryBuilder cmd = new DynamicSelectQueryBuilder(Database);
cmd.From(u => u.dbo.Users.As(u.c))
.Select(u => u(u.Sum(u("\"UserName\"")), " + 1").As(u.Name));
Assert.AreEqual("SELECT Sum(\"UserName\") + 1 AS \"Name\" FROM \"dbo\".\"Users\" AS c", cmd.CommandText());
}
/// <summary> /// <summary>
/// Tests select from anonymous type. /// Tests select from anonymous type.
/// </summary> /// </summary>

View File

@@ -426,6 +426,7 @@ namespace DynamORM.Builders.Implementation
_from = string.IsNullOrEmpty(_from) ? sb.ToString() : string.Format("{0}, {1}", _from, sb.ToString()); _from = string.IsNullOrEmpty(_from) ? sb.ToString() : string.Format("{0}, {1}", _from, sb.ToString());
} }
return index; return index;
} }
@@ -875,6 +876,7 @@ namespace DynamORM.Builders.Implementation
if (!anon) if (!anon)
ParseSelectAddColumn(main, alias, all); ParseSelectAddColumn(main, alias, all);
} }
return index; return index;
} }
@@ -947,6 +949,7 @@ namespace DynamORM.Builders.Implementation
else else
_groupby = string.Format("{0}, {1}", _groupby, main); _groupby = string.Format("{0}, {1}", _groupby, main);
} }
return index; return index;
} }
@@ -1075,6 +1078,7 @@ namespace DynamORM.Builders.Implementation
else else
_orderby = string.Format("{0}, {1}", _orderby, main); _orderby = string.Format("{0}, {1}", _orderby, main);
} }
return index; return index;
} }

View File

@@ -873,8 +873,8 @@ namespace DynamORM
} }
/// <summary>Builds query cache if necessary and returns it.</summary> /// <summary>Builds query cache if necessary and returns it.</summary>
/// <param name="sql">Sql query from which read schema.</param> /// <param name="sql">SQL query from which read schema.</param>
/// <param name="args">Sql query argumants.</param> /// <param name="args">SQL query arguments.</param>
/// <returns>Query schema.</returns> /// <returns>Query schema.</returns>
public Dictionary<string, DynamicSchemaColumn> GetQuerySchema(string sql, params object[] args) public Dictionary<string, DynamicSchemaColumn> GetQuerySchema(string sql, params object[] args)
{ {
@@ -934,6 +934,43 @@ namespace DynamORM
return schema; return schema;
} }
/// <summary>Clears the schema from cache.</summary>
/// <remarks>Use this method to refresh table information.</remarks>
/// <param name="table">Name of table for which clear schema.</param>
/// <param name="owner">Owner of table for which clear schema.</param>
public void ClearSchema(string table = null, string owner = null)
{
lock (SyncLock)
if (Schema.ContainsKey(table.ToLower()))
Schema.Remove(table.ToLower());
}
/// <summary>Clears the schema from cache.</summary>
/// <remarks>Use this method to refresh table information.</remarks>
/// <typeparam name="T">Type of table for which clear schema.</typeparam>
public void ClearSchema<T>()
{
ClearSchema(typeof(T));
}
/// <summary>Clears the schema from cache.</summary>
/// <remarks>Use this method to refresh table information.</remarks>
/// <param name="table">Type of table for which clear schema.</param>
public void ClearSchema(Type table)
{
lock (SyncLock)
if (Schema.ContainsKey(table.FullName))
Schema.Remove(table.FullName);
}
/// <summary>Clears the all schemas from cache.</summary>
/// <remarks>Use this method to refresh all table information.</remarks>
public void ClearSchema()
{
lock (SyncLock)
Schema.Clear();
}
/// <summary>Get schema describing objects from reader.</summary> /// <summary>Get schema describing objects from reader.</summary>
/// <param name="table">Table from which extract column info.</param> /// <param name="table">Table from which extract column info.</param>
/// <param name="owner">Owner of table from which extract column info.</param> /// <param name="owner">Owner of table from which extract column info.</param>
@@ -946,7 +983,7 @@ namespace DynamORM
.SetCommand(string.Format("SELECT * FROM {0}{1} WHERE 1 = 0", .SetCommand(string.Format("SELECT * FROM {0}{1} WHERE 1 = 0",
!string.IsNullOrEmpty(owner) ? string.Format("{0}.", DecorateName(owner)) : string.Empty, !string.IsNullOrEmpty(owner) ? string.Format("{0}.", DecorateName(owner)) : string.Empty,
DecorateName(table)))) DecorateName(table))))
return ReadSchema(cmd); return ReadSchema(cmd).ToList();
} }
/// <summary>Get schema describing objects from reader.</summary> /// <summary>Get schema describing objects from reader.</summary>

View File

@@ -1078,8 +1078,7 @@ namespace DynamORM
/// <returns>Returns <c>true</c> if it does.</returns> /// <returns>Returns <c>true</c> if it does.</returns>
public static bool IsGenericEnumerable(this Type type) public static bool IsGenericEnumerable(this Type type)
{ {
return type.IsGenericType && return type.IsGenericType && type.GetInterfaces().Any(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
typeof(IEnumerable<>).IsAssignableFrom(type.GetGenericTypeDefinition());
} }
/// <summary>Check if type implements IEnumerable&lt;&gt; interface.</summary> /// <summary>Check if type implements IEnumerable&lt;&gt; interface.</summary>
@@ -1115,7 +1114,7 @@ namespace DynamORM
return type.GetElementType().IsValueType; return type.GetElementType().IsValueType;
else else
{ {
if (type.IsGenericType) if (type.IsGenericType && type.GetInterfaces().Any(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{ {
Type[] gt = type.GetGenericArguments(); Type[] gt = type.GetGenericArguments();
@@ -1126,6 +1125,30 @@ namespace DynamORM
return false; return false;
} }
/// <summary>Gets <see cref="System.Data.DbType"/> corresponding to the
/// provided <see cref="System.Type"/>.</summary>
/// <param name="t">The type to be converted.</param>
/// <returns>Returns <see cref="System.Data.DbType"/> corresponding to the
/// provided <see cref="System.Type"/>.</returns>
public static DbType ToDbType(this Type t)
{
return TypeMap.TryGetNullable(t) ?? DbType.Object;
}
/// <summary>Gets <see cref="System.Type"/> corresponding to the
/// provided <see cref="System.Data.DbType"/>.</summary>
/// <param name="dbt">The type to be converted.</param>
/// <returns>Returns <see cref="System.Type"/> corresponding to the
/// provided <see cref="System.Data.DbType"/>.</returns>
public static Type ToType(this DbType dbt)
{
foreach (var tdbt in TypeMap)
if (tdbt.Value == dbt)
return tdbt.Key;
return typeof(object);
}
#endregion Type extensions #endregion Type extensions
#region IDictionary extensions #region IDictionary extensions
@@ -1177,6 +1200,23 @@ namespace DynamORM
#endregion IDictionary extensions #endregion IDictionary extensions
#region IDataReader extensions
/// <summary>Gets the <see cref="System.Data.DbType"/> information corresponding
/// to the type of <see cref="System.Object"/> that would be returned from
/// <see cref="System.Data.IDataRecord.GetValue(System.Int32)"/>.</summary>
/// <param name="r">The data reader.</param>
/// <param name="i">The index of the field to find.</param>
/// <returns>The <see cref="System.Data.DbType"/> information corresponding to the
/// type of <see cref="System.Object"/> that would be returned from
/// <see cref="System.Data.IDataRecord.GetValue(System.Int32)"/>.</returns>
public static DbType GetFieldDbType(this IDataReader r, int i)
{
return TypeMap.TryGetNullable(r.GetFieldType(i)) ?? DbType.String;
}
#endregion IDataReader extensions
#region Mapper extensions #region Mapper extensions
/// <summary>MapEnumerable object enumerator into specified type.</summary> /// <summary>MapEnumerable object enumerator into specified type.</summary>

View File

@@ -59,12 +59,18 @@ namespace DynamORM.Helpers.Dynamics
_proxy = proxiedObject; _proxy = proxiedObject;
_type = typeof(T); _type = typeof(T);
_properties = _type.GetProperties() var members = GetAllMembers(_type);
_properties = members
.Where(x => x is PropertyInfo)
.ToDictionary( .ToDictionary(
k => k.Name, k => k.Name,
v => new DynamicPropertyInvoker(v, null)); v => new DynamicPropertyInvoker((PropertyInfo)v, null));
_methods = _type.GetMethods() _methods = members
.Where(x => x is MethodInfo)
.Cast<MethodInfo>()
.Where(m => !((m.Name.StartsWith("set_") && m.ReturnType == typeof(void)) || m.Name.StartsWith("get_")))
.Where(m => !m.IsStatic && !m.IsGenericMethod) .Where(m => !m.IsStatic && !m.IsGenericMethod)
.ToDictionary( .ToDictionary(
k => k, k => k,
@@ -266,5 +272,45 @@ namespace DynamORM.Helpers.Dynamics
{ {
return arguments.Concat(parameters.Skip(arguments.Length).Select(p => p.DefaultValue)).ToArray(); return arguments.Concat(parameters.Skip(arguments.Length).Select(p => p.DefaultValue)).ToArray();
} }
private IEnumerable<MemberInfo> GetAllMembers(Type type)
{
if (type.IsInterface)
{
var members = new List<MemberInfo>();
var considered = new List<Type>();
var queue = new Queue<Type>();
considered.Add(type);
queue.Enqueue(type);
while (queue.Count > 0)
{
var subType = queue.Dequeue();
foreach (var subInterface in subType.GetInterfaces())
{
if (considered.Contains(subInterface)) continue;
considered.Add(subInterface);
queue.Enqueue(subInterface);
}
var typeProperties = subType.GetMembers(
BindingFlags.FlattenHierarchy
| BindingFlags.Public
| BindingFlags.Instance);
var newPropertyInfos = typeProperties
.Where(x => !members.Contains(x));
members.InsertRange(0, newPropertyInfos);
}
return members;
}
return type.GetMembers(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance);
}
} }
} }

View File

@@ -61,12 +61,12 @@ namespace DynamORM.Mapper
/// <remarks>Used when overriding schema.</remarks> /// <remarks>Used when overriding schema.</remarks>
public byte? Scale { get; set; } public byte? Scale { get; set; }
/// <summary>Gets or sets a value indicating whether this kolumn is no allowed to be inserted.</summary> /// <summary>Gets or sets a value indicating whether this column is no allowed to be inserted.</summary>
/// <remarks>This is only a suggesstion to automated mapping.</remarks> /// <remarks>This is only a suggestion to automated mapping.</remarks>
public bool IsNoInsert { get; set; } public bool IsNoInsert { get; set; }
/// <summary>Gets or sets a value indicating whether this kolumn is no allowed to be updated.</summary> /// <summary>Gets or sets a value indicating whether this column is no allowed to be updated.</summary>
/// <remarks>This is only a suggesstion to automated mapping.</remarks> /// <remarks>This is only a suggestion to automated mapping.</remarks>
public bool IsNoUpdate { get; set; } public bool IsNoUpdate { get; set; }
#region Constructors #region Constructors