Example #1
0
        public virtual IStatement ReadStatementAttribute(Type commandType)
        { 
            S statement = Activator.CreateInstance<S>();
            StatementAttribute statementAttribute = (StatementAttribute)Attribute.GetCustomAttribute(commandType, typeof(StatementAttribute), true);
            SA oracleStatementAttribute = (SA)Attribute.GetCustomAttribute(commandType, typeof(SA), true);
            if (statementAttribute == null)
            {
                if (oracleStatementAttribute != null)
                {
                    throw AttributeException.StatementAttributeNotFoundException(commandType, typeof(SA));
                }
                else
                {
                    return null;
                }
            }

            SetStatementValue(statement,commandType, statementAttribute, oracleStatementAttribute);
            return statement;
        }
Example #2
0
        protected void SetStatementValue(S statement, Type entityType, StatementAttribute statementAttribute, StatementAttributeBase statementAttributeBase)
        {
            statement.CommandBehavior = statementAttribute.CommandBehavior;
            switch (statementAttribute.CommandType)
            {
                case AttributeCommandType.Null:
                case AttributeCommandType.StoredProcedure:
                    statement.CommandType = CommandType.StoredProcedure;
                    break;
                case AttributeCommandType.TableDirect:
                    statement.CommandType = CommandType.TableDirect;
                    break;
                case AttributeCommandType.Text:
                    statement.CommandType = CommandType.Text;
                    break;
            }
            statement.CommandText = string.IsNullOrWhiteSpace(statementAttribute.CommandText) ? entityType.Name : statementAttribute.CommandText;
            statement.Parameters = new Dictionary<string, IParameter>(StringComparer.OrdinalIgnoreCase);


            PropertyInfo[] propertyInfos = entityType.GetProperties();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                IParameter parameter = ReadParameterAttribute(propertyInfo);
                if (parameter != null)
                {
                    statement.Parameters.Add(propertyInfo.Name, parameter);
                }

            }
            FieldInfo[] fieldInfos = entityType.GetFields();
            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                IParameter parameter = ReadParameterAttribute(fieldInfo);
                if (parameter != null)
                {
                    statement.Parameters.Add(fieldInfo.Name, parameter);
                }
            }

            #region 覆盖
            if (statementAttributeBase != null)
            {
                if (statementAttributeBase.CommandType != AttributeCommandType.Null)
                {
                    switch (statementAttributeBase.CommandType)
                    {
                        case AttributeCommandType.StoredProcedure:
                            statement.CommandType = CommandType.StoredProcedure;
                            break;
                        case AttributeCommandType.TableDirect:
                            statement.CommandType = CommandType.TableDirect;
                            break;
                        case AttributeCommandType.Text:
                            statement.CommandType = CommandType.Text;
                            break;
                    }
                }
                if (!string.IsNullOrWhiteSpace(statementAttributeBase.CommandText))
                {
                    statement.CommandText = statementAttributeBase.CommandText;//覆盖
                }
            }
            #endregion
        }