Exemplo n.º 1
0
        public override ITable ReadTableAttribute(Type entityType)
        {
            TableBase               table                   = new TableBase();
            TableAttribute          tableAttribute          = (TableAttribute)Attribute.GetCustomAttribute(entityType, typeof(TableAttribute), true);
            SqlServerTableAttribute sqlServerTableAttribute = (SqlServerTableAttribute)Attribute.GetCustomAttribute(entityType, typeof(SqlServerTableAttribute), true);

            if (tableAttribute == null)
            {
                if (sqlServerTableAttribute != null)
                {
                    throw AttributeException.TableAttributeNotFoundException(entityType, typeof(SqlServerTableAttribute));
                }
                else
                {
                    throw AttributeException.AttributeNotFoundException(entityType, typeof(TableAttribute));
                }
            }

            SetTableValue(table, entityType, tableAttribute, sqlServerTableAttribute);

            if (string.IsNullOrWhiteSpace(tableAttribute.TableName) &&
                (
                    sqlServerTableAttribute == null ||
                    (sqlServerTableAttribute != null && string.IsNullOrWhiteSpace(sqlServerTableAttribute.TableName))
                )
                )
            {
                //针对sqlServer,如果某个属性的ColumnAttribute.ColumnName为空,则自动在字段名加[]
                table.TableName = "[" + entityType.Name + "]";
            }

            return(table);
        }
Exemplo n.º 2
0
        public override IParameter ReadParameterAttribute(MemberInfo memberInfo)
        {
            ParameterBase parameter = new ParameterBase();

            ParameterAttribute       statementParameterAttribute = (ParameterAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(ParameterAttribute), true);
            SQLiteParameterAttribute sqliteParameterAttribute    = (SQLiteParameterAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(SQLiteParameterAttribute), true);

            if (statementParameterAttribute == null)
            {
                if (sqliteParameterAttribute != null)
                {
                    throw AttributeException.ColumnAttributeNotFoundException(memberInfo, typeof(SQLiteParameterAttribute));
                }
                else
                {
                    return(null);
                }
            }


            SetParameterValue(parameter, memberInfo, statementParameterAttribute, sqliteParameterAttribute);


            if (string.IsNullOrWhiteSpace(statementParameterAttribute.ParameterName))
            {
                parameter.ParameterName = "@" + memberInfo.Name;
            }
            if (sqliteParameterAttribute != null && string.IsNullOrWhiteSpace(sqliteParameterAttribute.ParameterName))
            {
                parameter.ParameterName = "@" + memberInfo.Name;
            }

            return(null);
        }
Exemplo n.º 3
0
        public override IColumn ReadColumnAttribute(MemberInfo memberInfo)
        {
            ColumnWithIdentity column = new ColumnWithIdentity();

            ColumnAttribute       columnAttribute       = (ColumnAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(ColumnAttribute), true);
            SQLiteColumnAttribute sqliteColumnAttribute = (SQLiteColumnAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(SQLiteColumnAttribute), true);

            if (columnAttribute == null)
            {
                if (sqliteColumnAttribute != null)
                {
                    throw AttributeException.ColumnAttributeNotFoundException(memberInfo, typeof(SQLiteColumnAttribute));
                }
                else
                {
                    return(null);
                }
            }


            SetColumnValue(column, memberInfo, columnAttribute, sqliteColumnAttribute);



            if (string.IsNullOrWhiteSpace(columnAttribute.ColumnName) &&
                (
                    sqliteColumnAttribute == null ||
                    (sqliteColumnAttribute != null && string.IsNullOrWhiteSpace(sqliteColumnAttribute.ColumnName))
                )
                )
            {
                //针对sqlServer,如果某个属性的ColumnAttribute.ColumnName为空,则自动在字段名加[]
                column.ColumnName = "[" + memberInfo.Name + "]";
            }
            //去除转义
            if (column.OutputAlias.StartsWith("["))
            {
                column.OutputAlias = column.OutputAlias.Substring(1, column.OutputAlias.Length - 1);
            }
            if (column.OutputAlias.EndsWith("]"))
            {
                column.OutputAlias = column.OutputAlias.Substring(0, column.OutputAlias.Length - 1);
            }


            if (sqliteColumnAttribute != null)
            {
                column.IsIdentity = sqliteColumnAttribute.IsIdentity;
                if (column.IsIdentity)
                {
                    column.InsertBehavior.Generate = false;                   //如果是IsIdentity则强制不要在Insert语句中包含此列
                }
                if (columnAttribute.ReturnAfterInsert == AttributeBoolean.Null && column.IsIdentity)
                {
                    column.ReturnAfterInsert = true;//如果ReturnAfterInsert=null则默认会取回自动增长的值
                }
            }
            return(column);
        }
Exemplo n.º 4
0
        //DataReader로 부터 값들을 읽어 인스턴스를 만든다
        private ConstructInstance CreateConstructInstance(IDataReader reader)
        {
            ParameterException.Check(reader != null, ParameterError.NotExistReader);
            var   type          = typeof(T);
            var   dynamicMethod = new DynamicMethod("ConstructInstance", type, new[] { typeof(IDataReader) }, type);
            var   il            = dynamicMethod.GetILGenerator();
            Label startLoop     = il.DefineLabel();
            Label endLoop       = il.DefineLabel();

            ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);

            il.Emit(OpCodes.Newobj, constructor); //[instance]
            il.MarkLabel(startLoop);

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo property in properties)
            {
                Label startValueTypeNullLoop = il.DefineLabel();
                Label endValueTypeNullLoop   = il.DefineLabel();
                Label jumpToEnd = il.DefineLabel();

                var columnAttribute = property.GetCustomAttribute <ColumnAttribute>();
                AttributeException.Check(columnAttribute != null, AttributeError.NotExistColumnAttribute);
                var columnName = columnAttribute.Name ?? property.Name;
                //SetValue
                il.Emit(OpCodes.Dup);               //[instance][instance]
                il.Emit(OpCodes.Ldarg_0);           //[instance][instance][IDataReader]
                il.Emit(OpCodes.Ldstr, columnName); //[instance][instance][IDataReader][Name]
                il.Emit(OpCodes.Call,
                        typeof(IDataReaderHelper).GetMethod(nameof(IDataReaderHelper
                                                                   .GetValue))); //[instance][instance][value]
                il.Emit(OpCodes.Dup);                                            //[instance][instance][value][value]
                il.Emit(OpCodes.Ldsfld,
                        typeof(DBNull).GetField(nameof(DBNull.Value)));          //[instance][instance][value][value][DBNullValue]
                il.Emit(OpCodes.Ceq);                                            // [instance][instance][value][result]
                il.Emit(OpCodes.Brtrue, startValueTypeNullLoop);                 //[instance][instance]
                if (property.PropertyType.IsValueType)
                {
                    il.Emit(OpCodes.Unbox_Any, property.PropertyType); //[instance][instance][unbox_value]
                }
                il.MarkLabel(endValueTypeNullLoop);
                il.Emit(OpCodes.Call, property.GetSetMethod()); //[instance]
                il.Emit(OpCodes.Br, jumpToEnd);

                il.MarkLabel(startValueTypeNullLoop);
                if (property.PropertyType.IsValueType)
                {
                    il.Emit(OpCodes.Pop);      //[instance][instance]
                    il.Emit(OpCodes.Ldc_I4_0); //[instance][instance][0]
                }

                il.Emit(OpCodes.Br, endValueTypeNullLoop);
                il.MarkLabel(jumpToEnd);
            }

            il.MarkLabel(endLoop);
            il.Emit(OpCodes.Ret);
            return((ConstructInstance)dynamicMethod.CreateDelegate(typeof(ConstructInstance)));
        }
Exemplo n.º 5
0
        public void TestAttributeInnerExceptionTest()
        {
            Exception          innerException = new Exception("Inner exception");
            AttributeException exception      = new AttributeException("Attribute Exception Messsage", innerException);

            Assert.AreEqual(exception.Message, "Attribute Exception Messsage");
            Assert.AreEqual(exception.InnerException, innerException);
        }
Exemplo n.º 6
0
        public override IParameter ReadParameterAttribute(MemberInfo memberInfo)
        {
            OracleParameter parameter = new OracleParameter();
            //oracle有个特殊情况,如果有独到OracleCursorAttribute则立刻返回true 无需StatementParameterAttribute标记也可
            OracleCursorAttribute cursorAttribute = (OracleCursorAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(OracleCursorAttribute), true);

            if (cursorAttribute != null)
            {
                parameter.ParameterName = cursorAttribute.ParameterName;
                parameter.Direction     = ParameterDirection.Output;
                parameter.ParameterName = string.IsNullOrWhiteSpace(cursorAttribute.ParameterName) ? memberInfo.Name : parameter.ParameterName = cursorAttribute.ParameterName;
                //parameter.PropertyAdapter = new PropertyAdapter(memberInfo);
                if (memberInfo is PropertyInfo)
                {
                    PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
                    parameter.PropertyType = propertyInfo.PropertyType;
                }
                else
                {
                    FieldInfo fieldInfo = (FieldInfo)memberInfo;
                    parameter.PropertyType = fieldInfo.FieldType;
                }
                parameter.IsCursor = true;
                return(parameter);
            }



            ParameterAttribute       statementParameterAttribute = (ParameterAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(ParameterAttribute), true);
            OracleParameterAttribute oracleParameterAttribute    = (OracleParameterAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(OracleParameterAttribute), true);

            if (statementParameterAttribute == null)
            {
                if (oracleParameterAttribute != null)
                {
                    throw AttributeException.ColumnAttributeNotFoundException(memberInfo, typeof(OracleParameterAttribute));
                }
                else
                {
                    return(null);
                }
            }


            SetParameterValue(parameter, memberInfo, statementParameterAttribute, oracleParameterAttribute);


            return(parameter);
        }
Exemplo n.º 7
0
        public override IColumn ReadColumnAttribute(MemberInfo memberInfo)
        {
            ColumnWithIdentity column = new ColumnWithIdentity();

            ColumnAttribute      columnAttribute      = (ColumnAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(ColumnAttribute), true);
            MySqlColumnAttribute mySqlColumnAttribute = (MySqlColumnAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(MySqlColumnAttribute), true);

            if (columnAttribute == null)
            {
                if (mySqlColumnAttribute != null)
                {
                    throw AttributeException.ColumnAttributeNotFoundException(memberInfo, typeof(MySqlColumnAttribute));
                }
                else
                {
                    return(null);
                }
            }


            SetColumnValue(column, memberInfo, columnAttribute, mySqlColumnAttribute);


            if (column.OutputAlias.StartsWith("`"))
            {
                column.OutputAlias = column.OutputAlias.Substring(1, column.OutputAlias.Length - 1);
            }
            if (column.OutputAlias.EndsWith("`"))
            {
                column.OutputAlias = column.OutputAlias.Substring(0, column.OutputAlias.Length - 1);
            }

            if (mySqlColumnAttribute != null)
            {
                column.IsIdentity = mySqlColumnAttribute.IsIdentity;
                if (column.IsIdentity)
                {
                    column.InsertBehavior.Generate = false;                   //如果是IsIdentity则强制不要在Insert语句中包含此列
                }
                if (columnAttribute.ReturnAfterInsert == AttributeBoolean.Null && column.IsIdentity)
                {
                    column.ReturnAfterInsert = true;//如果ReturnAfterInsert=null则默认会取回自动增长的值
                }
            }

            return(column);
        }
Exemplo n.º 8
0
        public override IColumn ReadColumnAttribute(MemberInfo memberInfo)
        {
            OracleColumn column = new OracleColumn();

            ColumnAttribute       columnAttribute       = (ColumnAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(ColumnAttribute), true);
            OracleColumnAttribute oracleColumnAttribute = (OracleColumnAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(OracleColumnAttribute), true);

            if (columnAttribute == null)
            {
                if (oracleColumnAttribute != null)
                {
                    throw AttributeException.ColumnAttributeNotFoundException(memberInfo, typeof(OracleColumnAttribute));
                }
                else
                {
                    return(null);
                }
            }


            SetColumnValue(column, memberInfo, columnAttribute, oracleColumnAttribute);


            if (column.OutputAlias.StartsWith("\""))
            {
                column.OutputAlias = column.OutputAlias.Substring(1, column.OutputAlias.Length - 1);
            }
            if (column.OutputAlias.EndsWith("\""))
            {
                column.OutputAlias = column.OutputAlias.Substring(0, column.OutputAlias.Length - 1);
            }

            if (oracleColumnAttribute != null)
            {
                column.Sequence = oracleColumnAttribute.Sequence;
                if (column.HasSequence)
                {
                    column.InsertBehavior.ValueBehavior   = ValueBehavior.UseValueExpression;
                    column.InsertBehavior.ValueExpression = column.Sequence + ".nextval";
                    if (columnAttribute.ReturnAfterInsert == AttributeBoolean.Null)
                    {
                        column.ReturnAfterInsert = true;//如果ReturnAfterInsert=null则默认会取回自动增长的值
                    }
                }
            }
            return(column);
        }
Exemplo n.º 9
0
        public void TestAttributeMessageTest()
        {
            AttributeException exception = new AttributeException("Attribute Exception Messsage");

            Assert.AreEqual(exception.Message, "Attribute Exception Messsage");
        }
Exemplo n.º 10
0
        public void TestAttributeDefaultTest()
        {
            AttributeException exception = new AttributeException();

            Assert.AreEqual(exception.Message, "Exception of type 'WebTestSuite.Exceptions.AttributeException' was thrown.");
        }