This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -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>
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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<> interface.</summary>
|
/// <summary>Check if type implements IEnumerable<> 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>
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user