コード例 #1
0
            public SqlCommandGenerateContext Build()
            {
                var parser = new DefaultCommandParser();

                this.context.QueryAttributes = this.context.Method.GetCustomAttributes <SqlAttribute>();

                this.context.HasAnyQueryAttribute = this.context.QueryAttributes != null && this.context.QueryAttributes.Any();

                if (!this.context.HasAnyQueryAttribute)
                {
                    this.context.CommandInfo = parser.Parse(this.context.Method.Name);
                }

                this.context.IDaoType = this.context.Method.DeclaringType;

                IEntityTypeProvider entityTypeProvider = NpiServicesCollection.GetService <IEntityTypeProvider>();

                this.context.EntityType = entityTypeProvider.Provide(this.context.Method);

                ITableNameProvider tableNameProvider = NpiServicesCollection.GetService <ITableNameProvider>();

                this.context.TableName = tableNameProvider.Provide(this.context.EntityType);

                this.context.Properties = this.context.EntityType.GetProperties();


                return(this.context);
            }
コード例 #2
0
        public void Intercept(IInvocation invocation)
        {
            string methodName = invocation.Method.Name;

            Type typeofIDao = typeof(INpiDao <>);
            Type entityType = invocation.Method.DeclaringType.GetInterface(typeofIDao.FullName).GetGenericArguments()[0];


            // todo :对其单例化
            ISqlCommandGenerator  g = NpiServicesCollection.GetService <ISqlServerCommandGenerator>();
            SqlCommandDescription d = g.Generate(invocation.Method, invocation.Arguments);

            DebugLogger.Debug(d.ToString());

            DapperParameters dp = new DapperParameters();

            foreach (var p in d.Parameters)
            {
                dp[p.Key] = p.Value.Value;
            }

            object dbReturnedValue = null;
            object handledValue    = null;

            DebugLogger.Debug("准备执行 Sql");

            switch (d.Type)
            {
            case SqlCommandTypes.Insert:
            case SqlCommandTypes.Update:
            case SqlCommandTypes.Delete:
            {
                var executor = ServicesCollection.GetService <ISqlCommandExecutor>();
                dbReturnedValue = executor.Execute(d.SqlCommand, dp, dbConnectionContext);
            }
            break;

            case SqlCommandTypes.Select:
            {
                var querier = ServicesCollection.GetService <ISqlCommandQuerier>();
                dbReturnedValue = querier.Select(entityType, d.SqlCommand, dp, dbConnectionContext);
            }
            break;

            default:
                break;
            }
            DebugLogger.Debug("Sql 执行完毕,开始处理结果集");
            foreach (var handler in dbReturnValueHandlers)
            {
                if (!handler.CanHandle(invocation.Method, entityType))
                {
                    continue;
                }
                handledValue = handler.Handle(invocation.Method, entityType, dbReturnedValue);
            }
            invocation.ReturnValue = handledValue;
            DebugLogger.Debug("结果集处理完毕");
        }
コード例 #3
0
        public override void Intercept(InterfaceInvocationInfo info)
        {
            this.AssertProviderIsValid();
            Type typeOfIDao = typeof(INpiDao <>);

            ISqlCommandGenerator  g = NpiServicesCollection.GetService <ISqlServerCommandGenerator>();
            SqlCommandDescription d = g.Generate(info.Method, info.Arguments);

            this.EventBus.Publish(new SqlCommandDescriptionGeneratedEvent(this, d));

            if (info.Method.ReturnType == typeof(void))
            {
                d.Mode = SqlCommandExecuteModes.Execute;
            }

            switch (d.Mode)
            {
            case SqlCommandExecuteModes.Execute:
                int i = this.Executor.Execute(this.Provider.Provide(), d);
                foreach (var handler in this.ExecuteResultHandlers)
                {
                    if (!handler.CanHandle(info.Method))
                    {
                        continue;
                    }
                    info.ReturnValue = handler.Handle(info.Method, i);
                }
                break;

            case SqlCommandExecuteModes.Query:
            {
                Type returnType = info.Method.ReturnType;
                Type itemType;
                if (TryGetIEnumerableItemType(returnType, out itemType))
                {
                    returnType = itemType;
                }

                IEnumerable <object> list = this.Executor.Select(this.Provider.Provide(), d, returnType);
                foreach (var handler in this.SelectResultHandlers)
                {
                    if (!handler.CanHandle(info.Method, returnType))
                    {
                        continue;
                    }
                    info.ReturnValue = handler.Handle(info.Method, returnType, list);
                }
            }
            break;

            default:
                break;
            }
        }
コード例 #4
0
        private ICommandInfo ParseFromCommand(Queue <string> wordQueue)
        {
            string action      = wordQueue.Dequeue();
            string realCommand = wordQueue.Join("", x => x);

            switch (action)
            {
            case ACTION_PAGING:
            {
                SelectInfo info = this.ParseFromCommand(wordQueue) as SelectInfo;
                if (info == null)
                {
                    throw new PagingMustBeFollowedBySelectException();
                }
                info.Paging = true;
                return(info);
            }

            case ACTION_GET:
            case ACTION_SELECT:
            case ACTION_FETCH:
            case ACTION_FIND:
                return(NpiServicesCollection.GetService <ISelectParser>().Parse(realCommand));

            case ACTION_UPDATE:
            case ACTION_MODIFY:
                return(NpiServicesCollection.GetService <IUpdateParser>().Parse(realCommand));

            case ACTION_REMOVE:
            case ACTION_DELETE:
                return(NpiServicesCollection.GetService <IDeleteParser>().Parse(realCommand));

            case ACTION_CREATE:
            case ACTION_NEW:
            case ACTION_INSERT:
                return(NpiServicesCollection.GetService <IInsertParser>().Parse(realCommand));

            case ACTION_COUNT:
                return(NpiServicesCollection.GetService <ICountParser>().Parse(realCommand));

            default:
                throw new NotSupportActionException(action);
            }
        }
コード例 #5
0
 public DefaultEntityTypeProvider()
 {
     this.cache = NpiServicesCollection.GetService <ICache>();
 }
コード例 #6
0
 public DefaultCommandParser()
 {
     this.cache = NpiServicesCollection.GetService <ICache>();
 }
コード例 #7
0
 public DefaultSqlServerCommandGenerator()
 {
     this.fieldNameProvider        = NpiServicesCollection.GetService <IFieldNameProvider>();
     this.conditionGenerateHandler = NpiServicesCollection.GetService <IConditionGenerateHandler>();
 }
コード例 #8
0
 public DefaultParameterLookupFactory()
 {
     this.lookups = NpiServicesCollection.GetServices <IParameterLookup>();
 }
コード例 #9
0
 public DefaultSqlParameterFinder()
 {
     cache = NpiServicesCollection.GetService <ICache>();
 }
コード例 #10
0
 static OperatorMappedConditionGenerator()
 {
     operatorMapper = NpiServicesCollection.GetService <IOperatorMapper>();
 }
コード例 #11
0
 public SqlCommandGeneratorBase()
 {
     this.parameterLookupFactory = NpiServicesCollection.GetService <IParameterLookupFactory>();
     this.cache = NpiServicesCollection.GetService <ICache>();
     this.sqlParameterFinder = NpiServicesCollection.GetService <ISqlParameterFinder>();
 }
コード例 #12
0
 static DefaultParseStateMachine()
 {
     stateMachineBuilderFactory = NpiServicesCollection.GetService <IStateMachineBuilderFactory>();
 }
コード例 #13
0
 public DefaultInsertParserTests()
 {
     this.parser = NpiServicesCollection.GetService <ICommandParser>();
 }
コード例 #14
0
 public DefaultConditionGenerateHandler()
 {
     this.conditionGenerators = NpiServicesCollection.GetServices <IConditionGenerator>();
 }
コード例 #15
0
 static DefaultOperatorMapper()
 {
     cache = NpiServicesCollection.GetService <ICache>();
 }
コード例 #16
0
 public DefaultOperatorMapper()
 {
     this.ResourceProvider = NpiServicesCollection.GetService <IResourceProvider>();
 }
コード例 #17
0
 public DefaultFieldNameProvider()
 {
     this.cache = NpiServicesCollection.GetService <ICache>();
 }
コード例 #18
0
 public ResourceNameProviderTests()
 {
     this.resourceNameProvider = NpiServicesCollection.GetService <IResourceNameProvider>();
 }
コード例 #19
0
 public DefaultStateMachineBuilderFactory()
 {
     this.cache                = NpiServicesCollection.GetService <ICache>();
     this.resourceProvider     = NpiServicesCollection.GetService <IResourceProvider>();
     this.resourceNameProvider = NpiServicesCollection.GetService <IResourceNameProvider>();
 }