Commit d34b6bbf authored by Marc Gravell's avatar Marc Gravell

Merge pull request #106 from aggieben/master

add ComputedAttribute to allow support for computed columns on Insert
parents efd44601 67026a0d
...@@ -23,6 +23,7 @@ public interface IProxy ...@@ -23,6 +23,7 @@ public interface IProxy
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> KeyProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>(); private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> KeyProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> TypeProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>(); private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> TypeProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> ComputedProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> GetQueries = new ConcurrentDictionary<RuntimeTypeHandle, string>(); private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> GetQueries = new ConcurrentDictionary<RuntimeTypeHandle, string>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> TypeTableName = new ConcurrentDictionary<RuntimeTypeHandle, string>(); private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> TypeTableName = new ConcurrentDictionary<RuntimeTypeHandle, string>();
...@@ -30,7 +31,19 @@ public interface IProxy ...@@ -30,7 +31,19 @@ public interface IProxy
{"sqlconnection", new SqlServerAdapter()}, {"sqlconnection", new SqlServerAdapter()},
{"npgsqlconnection", new PostgresAdapter()} {"npgsqlconnection", new PostgresAdapter()}
}; };
private static IEnumerable<PropertyInfo> ComputedPropertiesCache(Type type)
{
IEnumerable<PropertyInfo> pi;
if (ComputedProperties.TryGetValue(type.TypeHandle, out pi))
{
return pi;
}
var computedProperties = TypePropertiesCache(type).Where(p => p.GetCustomAttributes(true).Any(a => a is ComputedAttribute)).ToList();
ComputedProperties[type.TypeHandle] = computedProperties;
return computedProperties;
}
private static IEnumerable<PropertyInfo> KeyPropertiesCache(Type type) private static IEnumerable<PropertyInfo> KeyPropertiesCache(Type type)
{ {
...@@ -175,22 +188,23 @@ private static string GetTableName(Type type) ...@@ -175,22 +188,23 @@ private static string GetTableName(Type type)
var allProperties = TypePropertiesCache(type); var allProperties = TypePropertiesCache(type);
var keyProperties = KeyPropertiesCache(type); var keyProperties = KeyPropertiesCache(type);
var allPropertiesExceptKey = allProperties.Except(keyProperties); var computedProperties = ComputedPropertiesCache(type);
var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties));
for (var i = 0; i < allPropertiesExceptKey.Count(); i++) for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++)
{ {
var property = allPropertiesExceptKey.ElementAt(i); var property = allPropertiesExceptKeyAndComputed.ElementAt(i);
sbColumnList.AppendFormat("[{0}]", property.Name); sbColumnList.AppendFormat("[{0}]", property.Name);
if (i < allPropertiesExceptKey.Count() - 1) if (i < allPropertiesExceptKeyAndComputed.Count() - 1)
sbColumnList.Append(", "); sbColumnList.Append(", ");
} }
var sbParameterList = new StringBuilder(null); var sbParameterList = new StringBuilder(null);
for (var i = 0; i < allPropertiesExceptKey.Count(); i++) for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++)
{ {
var property = allPropertiesExceptKey.ElementAt(i); var property = allPropertiesExceptKeyAndComputed.ElementAt(i);
sbParameterList.AppendFormat("@{0}", property.Name); sbParameterList.AppendFormat("@{0}", property.Name);
if (i < allPropertiesExceptKey.Count() - 1) if (i < allPropertiesExceptKeyAndComputed.Count() - 1)
sbParameterList.Append(", "); sbParameterList.Append(", ");
} }
ISqlAdapter adapter = GetFormatter(connection); ISqlAdapter adapter = GetFormatter(connection);
...@@ -472,6 +486,11 @@ public WriteAttribute(bool write) ...@@ -472,6 +486,11 @@ public WriteAttribute(bool write)
} }
public bool Write { get; private set; } public bool Write { get; private set; }
} }
[AttributeUsage(AttributeTargets.Property)]
public class ComputedAttribute : Attribute
{
}
} }
public interface ISqlAdapter public interface ISqlAdapter
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment