diff Source/Data/Linq/SqlFunctionAttribute.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Source/Data/Linq/SqlFunctionAttribute.cs	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,79 @@
+using System;
+using System.Linq;
+using System.Reflection;
+
+namespace BLToolkit.Data.Linq
+{
+	using Data.Sql;
+	using Reflection;
+
+	[SerializableAttribute]
+	[AttributeUsageAttribute(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
+	public class SqlFunctionAttribute : Attribute
+	{
+		public SqlFunctionAttribute()
+		{
+		}
+
+		public SqlFunctionAttribute(string name)
+		{
+			Name = name;
+		}
+
+		public SqlFunctionAttribute(string name, params int[] argIndices)
+		{
+			Name        = name;
+			ArgIndices  = argIndices;
+		}
+
+		public SqlFunctionAttribute(string sqlProvider, string name)
+		{
+			SqlProvider = sqlProvider;
+			Name        = name;
+		}
+
+		public SqlFunctionAttribute(string sqlProvider, string name, params int[] argIndices)
+		{
+			SqlProvider = sqlProvider;
+			Name        = name;
+			ArgIndices  = argIndices;
+		}
+
+		public string SqlProvider      { get; set; }
+		public string Name             { get; set; }
+		public bool   ServerSideOnly   { get; set; }
+		public bool   PreferServerSide { get; set; }
+		public int[]  ArgIndices       { get; set; }
+
+		protected ISqlExpression[] ConvertArgs(MemberInfo member, ISqlExpression[] args)
+		{
+			if (member is MethodInfo)
+			{
+				var method = (MethodInfo)member;
+
+				if (method.DeclaringType.IsGenericType)
+					args = args.Concat(method.DeclaringType.GetGenericArguments().Select(t => (ISqlExpression)SqlDataType.GetDataType(t))).ToArray();
+
+				if (method.IsGenericMethod)
+					args = args.Concat(method.GetGenericArguments().Select(t => (ISqlExpression)SqlDataType.GetDataType(t))).ToArray();
+			}
+
+			if (ArgIndices != null)
+			{
+				var idxs = new ISqlExpression[ArgIndices.Length];
+
+				for (var i = 0; i < ArgIndices.Length; i++)
+					idxs[i] = args[ArgIndices[i]];
+
+				return idxs;
+			}
+
+			return args;
+		}
+
+		public virtual ISqlExpression GetExpression(MemberInfo member, params ISqlExpression[] args)
+		{
+			return new SqlFunction(TypeHelper.GetMemberType(member), Name ?? member.Name, ConvertArgs(member, args));
+		}
+	}
+}