Exemplo n.º 1
0
        private static void ReadObject(ILGenerator il, LocalBuilder bindingInstance, LocalBuilder sqlDataReader, int fieldIndex, FieldInfo targetField)
        {
            LocalBuilder localBuilder = SqlProcedureLinker.NewInstance(il, targetField.FieldType);

            SqlProcedureLinker.BindField(il, bindingInstance, targetField, localBuilder);
            SqlProcedureLinker.ReadFields(il, targetField.FieldType, localBuilder, sqlDataReader, fieldIndex);
        }
Exemplo n.º 2
0
        private static LocalBuilder ReadList(ILGenerator il, Type elementType, LocalBuilder sqlDataReader, int fieldIndex)
        {
            Label        label        = il.DefineLabel();
            Label        label2       = il.DefineLabel();
            LocalBuilder localBuilder = il.DeclareLocal(typeof(List <>).MakeGenericType(new Type[]
            {
                elementType
            }));

            il.Emit(OpCodes.Newobj, typeof(List <>).MakeGenericType(new Type[]
            {
                elementType
            }).GetConstructor(Type.EmptyTypes));
            il.Emit(OpCodes.Stloc, localBuilder);
            il.MarkLabel(label);
            il.Emit(OpCodes.Ldloc, sqlDataReader);
            il.Emit(OpCodes.Call, typeof(SqlDataReader).GetMethod("Read", Type.EmptyTypes));
            il.Emit(OpCodes.Brfalse, label2);
            LocalBuilder localBuilder2 = SqlProcedureLinker.NewInstance2(il, elementType);

            SqlProcedureLinker.ReadFields(il, elementType, localBuilder2, sqlDataReader, fieldIndex);
            il.Emit(OpCodes.Ldloc, localBuilder);
            il.Emit(OpCodes.Ldloc, localBuilder2);
            il.EmitCall(OpCodes.Call, typeof(List <>).MakeGenericType(new Type[]
            {
                elementType
            }).GetMethod("Add"), null);
            il.Emit(OpCodes.Br, label);
            il.MarkLabel(label2);
            return(localBuilder);
        }
Exemplo n.º 3
0
        public T Generate <T>(Type bindingType) where T : class
        {
            string      storedProcedureName;
            MethodInfo  methodInfo;
            CommandType commandType   = this.ParsingAttributes(bindingType, out storedProcedureName, out methodInfo);
            Type        reflectedType = methodInfo.ReflectedType;

            ParameterInfo[] parameters    = methodInfo.GetParameters();
            Type            parameterType = methodInfo.ReturnParameter.ParameterType;

            Type[] array = new Type[parameters.Length + 1];
            array[0] = typeof(string);
            for (int i = 0; i < parameters.Length; i++)
            {
                array[i + 1] = parameters[i].ParameterType;
            }
            DynamicMethod dynamicMethod          = new DynamicMethod(string.Format("{0}.InternalQueryExecute", bindingType.Namespace), parameterType, array, bindingType.Module, true);
            ILGenerator   ilgenerator            = dynamicMethod.GetILGenerator();
            LocalBuilder  localBuilder           = SqlProcedureLinker.NewSqlconnection(ilgenerator);
            LocalBuilder  localBuilder2          = SqlProcedureLinker.NewSqlCommand(ilgenerator, storedProcedureName, localBuilder);
            LocalBuilder  sqlParameterCollection = SqlProcedureLinker.InitSqlCommand(ilgenerator, localBuilder2);
            LocalBuilder  localBuilder3          = SqlProcedureLinker.NewInstance2(ilgenerator, bindingType);

            SqlProcedureLinker.BindingParameters(ilgenerator, bindingType, localBuilder3, parameters, sqlParameterCollection);
            if (parameterType.Equals(typeof(void)))
            {
                ilgenerator.Emit(OpCodes.Ldloc, localBuilder2);
                ilgenerator.Emit(OpCodes.Call, typeof(SqlCommand).GetMethod("ExecuteNonQuery", Type.EmptyTypes));
                ilgenerator.Emit(OpCodes.Pop);
            }
            else if (parameterType.Equals(bindingType))
            {
                LocalBuilder localBuilder4 = SqlProcedureLinker.NewSqlDataReader(ilgenerator, localBuilder2);
                SqlProcedureLinker.ExecuteReader(ilgenerator, bindingType, localBuilder3, localBuilder4, 0);
                ilgenerator.Emit(OpCodes.Ldloc, localBuilder4);
                ilgenerator.EmitCall(OpCodes.Call, typeof(SqlDataReader).GetMethod("Close", Type.EmptyTypes), null);
            }
            else
            {
                ilgenerator.Emit(OpCodes.Ldloc, localBuilder2);
                ilgenerator.Emit(OpCodes.Call, typeof(SqlCommand).GetMethod("ExecuteScalar", Type.EmptyTypes));
                ilgenerator.Emit(OpCodes.Unbox_Any, parameterType);
            }
            SqlProcedureLinker.GettingParameters(ilgenerator, parameters, sqlParameterCollection);
            SqlProcedureLinker.GettingReturnValue(ilgenerator, bindingType, localBuilder3, sqlParameterCollection);
            ilgenerator.Emit(OpCodes.Ldloc, localBuilder);
            ilgenerator.Emit(OpCodes.Call, typeof(SqlConnection).GetMethod("Close"));
            if (parameterType.Equals(bindingType))
            {
                ilgenerator.Emit(OpCodes.Ldloc, localBuilder3);
            }
            ilgenerator.Emit(OpCodes.Ret);
            T t = dynamicMethod.CreateDelegate(reflectedType, this.connectionString) as T;

            if (t == null)
            {
                throw new ArgumentException(string.Format("Generic delegator type-casting occurs error: {0}", typeof(T).Name));
            }
            return(t);
        }
Exemplo n.º 4
0
        private static void ExecuteReader(ILGenerator il, Type bindingType, LocalBuilder bindingInstance, LocalBuilder sqlDataReader, int fieldIndex)
        {
            il.Emit(OpCodes.Ldloc, sqlDataReader);
            il.Emit(OpCodes.Call, typeof(SqlDataReader).GetMethod("Read", Type.EmptyTypes));
            Label label = il.DefineLabel();

            il.Emit(OpCodes.Brfalse, label);
            SqlProcedureLinker.ReadFields(il, bindingType, bindingInstance, sqlDataReader, fieldIndex);
            il.MarkLabel(label);
        }
Exemplo n.º 5
0
 private void GetFieldInfos(Type type, ref List <string> fieldInfoList)
 {
     if (type.BaseType != null && type.BaseType != typeof(object) && !type.BaseType.IsValueType)
     {
         this.GetFieldInfos(type.BaseType, ref fieldInfoList);
     }
     foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
     {
         fieldInfoList.Add(string.Format("{0} {1} NOT NULL", fieldInfo.Name, SqlProcedureLinker.ConvertToString(fieldInfo)));
     }
 }
Exemplo n.º 6
0
 private static void ReadFields(ILGenerator il, Type bindingType, LocalBuilder bindingInstance, LocalBuilder sqlDataReader, int fieldIndex)
 {
     if (bindingType.BaseType != null && bindingType.BaseType != typeof(object) && !bindingType.IsValueType)
     {
         SqlProcedureLinker.ReadFields(il, bindingType.BaseType, bindingInstance, sqlDataReader, fieldIndex);
     }
     foreach (FieldInfo fieldInfo in bindingType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
     {
         Type fieldType = fieldInfo.FieldType;
         if ((fieldInfo.GetCustomAttributes(false).Length <= 0 || !(fieldInfo.GetCustomAttributes(false)[0].GetType() == typeof(SqlReturnAttribute))) && !fieldType.IsInterface && !fieldType.IsAbstract && !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral && !fieldType.IsByRef)
         {
             if (fieldType.IsArray)
             {
                 Label label = il.DefineLabel();
                 il.Emit(OpCodes.Ldloc, sqlDataReader);
                 il.Emit(OpCodes.Call, typeof(SqlDataReader).GetMethod("NextResult"));
                 il.Emit(OpCodes.Brfalse, label);
                 Type         elementType  = fieldType.GetElementType();
                 LocalBuilder local        = SqlProcedureLinker.ReadList(il, elementType, sqlDataReader, 0);
                 LocalBuilder localBuilder = il.DeclareLocal(elementType.MakeArrayType());
                 il.Emit(OpCodes.Ldloc, local);
                 il.Emit(OpCodes.Call, typeof(List <>).MakeGenericType(new Type[]
                 {
                     elementType
                 }).GetMethod("ToArray"));
                 il.Emit(OpCodes.Stloc, localBuilder);
                 SqlProcedureLinker.BindField(il, bindingInstance, fieldInfo, localBuilder);
                 il.MarkLabel(label);
             }
             else if (SqlProcedureLinker.IsGenericCollection(fieldType))
             {
                 Label label2 = il.DefineLabel();
                 il.Emit(OpCodes.Ldloc, sqlDataReader);
                 il.Emit(OpCodes.Call, typeof(SqlDataReader).GetMethod("NextResult"));
                 il.Emit(OpCodes.Brfalse, label2);
                 Type         elementType2 = fieldType.GetGenericArguments()[0];
                 LocalBuilder sourceField  = SqlProcedureLinker.ReadList(il, elementType2, sqlDataReader, 0);
                 SqlProcedureLinker.BindField(il, bindingInstance, fieldInfo, sourceField);
                 il.MarkLabel(label2);
             }
             else if (!fieldType.IsPrimitive && fieldType != typeof(string))
             {
                 SqlProcedureLinker.ReadObject(il, bindingInstance, sqlDataReader, fieldIndex, fieldInfo);
             }
             else
             {
                 SqlProcedureLinker.ReadField(il, bindingInstance, sqlDataReader, fieldIndex, fieldInfo);
                 fieldIndex++;
             }
         }
     }
 }
Exemplo n.º 7
0
        private static string ConvertToString(FieldInfo field)
        {
            SqlDbType sqlDbType = SqlProcedureLinker.ConvertPrimitiveToSqlDBType(field.FieldType);
            string    text      = sqlDbType.ToString();

            object[] customAttributes = field.GetCustomAttributes(false);
            if (customAttributes.Length == 1)
            {
                SqlColumnSizeAttribute sqlColumnSizeAttribute = customAttributes[0] as SqlColumnSizeAttribute;
                if (sqlColumnSizeAttribute == null)
                {
                    throw new ArgumentException(string.Format("SqlColumnSizeAttribute type casting error : {0}", field.Name));
                }
                if (sqlColumnSizeAttribute.Size == -1)
                {
                    text += "(max)";
                }
                else
                {
                    text += string.Format("({0})", sqlColumnSizeAttribute.Size.ToString());
                }
            }
            return(text);
        }
Exemplo n.º 8
0
 private static void DebugHelper(ILGenerator il)
 {
     SqlProcedureLinker.DebugHelper(typeof(object), il);
 }