private void InitByMap(AbstractRequestContext requestContext, SqlMap sqlMap)
        {
            if (!string.IsNullOrEmpty(requestContext.CacheId))
            {
                var fullCacheId = $"{requestContext.Scope}.{requestContext.CacheId}";
                requestContext.Cache = sqlMap.GetCache(fullCacheId);
            }

            if (!String.IsNullOrEmpty(requestContext.ParameterMapId))
            {
                var fullParameterMapIdId = $"{requestContext.Scope}.{requestContext.ParameterMapId}";
                requestContext.ParameterMap = sqlMap.GetParameterMap(fullParameterMapIdId);
            }

            if (!String.IsNullOrEmpty(requestContext.ResultMapId))
            {
                var fullResultMapId = $"{requestContext.Scope}.{requestContext.ResultMapId}";
                requestContext.ResultMap = sqlMap.GetResultMap(fullResultMapId);
            }

            if (!String.IsNullOrEmpty(requestContext.MultipleResultMapId))
            {
                var fullMultipleResultMapId = $"{requestContext.Scope}.{requestContext.MultipleResultMapId}";
                requestContext.MultipleResultMap = sqlMap.GetMultipleResultMap(fullMultipleResultMapId);
            }
        }
        private static void SetCache(AbstractRequestContext requestContext, SqlMap sqlMap)
        {
            string fullCacheId;

            if (!requestContext.CacheId.Contains("."))
            {
                fullCacheId          = $"{requestContext.Scope}.{requestContext.CacheId}";
                requestContext.Cache = sqlMap.GetCache(fullCacheId);
            }
            else
            {
                fullCacheId = requestContext.CacheId;
                var fullCacheIdSplit = fullCacheId.Split('.');
                if (fullCacheIdSplit.Length != 2)
                {
                    throw new SmartSqlException($"Wrong CacheId:[{requestContext.CacheId}]");
                }

                var scope = fullCacheIdSplit[0];
                requestContext.Cache = sqlMap.Scope == scope
                    ? sqlMap.GetCache(fullCacheId)
                    : sqlMap.SmartSqlConfig.GetSqlMap(scope).GetCache(fullCacheId);
            }
        }
Esempio n. 3
0
        private Statement PreStatement(Type interfaceType, SqlMap sqlMap, MethodInfo methodInfo,
                                       Type returnType, bool isTaskReturnType, out ExecuteBehavior executeBehavior)
        {
            var statementAttr = methodInfo.GetCustomAttribute <StatementAttribute>();
            var methodName    = _sqlIdNamingConvert == null
                ? methodInfo.Name
                : _sqlIdNamingConvert.Invoke(interfaceType, methodInfo);

            if (isTaskReturnType && methodInfo.Name.EndsWith("Async") && _sqlIdNamingConvert == null)
            {
                methodName = methodName.Substring(0, methodName.Length - 5);
            }

            if (statementAttr != null)
            {
                statementAttr.Id = !String.IsNullOrEmpty(statementAttr.Id) ? statementAttr.Id : methodName;
            }
            else
            {
                statementAttr = new StatementAttribute
                {
                    Id = methodName
                };
            }

            var       fullSqlId = $"{sqlMap.Scope}.{statementAttr.Id}";
            Statement statement;

            if (String.IsNullOrEmpty(statementAttr.Sql))
            {
                statement = sqlMap.GetStatement(fullSqlId);
            }
            else
            {
                if (sqlMap.Statements.ContainsKey(fullSqlId))
                {
                    throw new SmartSqlException($"Statement.FullSqlId:[{fullSqlId}] already exists!");
                }

                var resultCacheAttr = methodInfo.GetCustomAttribute <ResultCacheAttribute>();
                statement = new Statement
                {
                    SqlMap        = sqlMap,
                    Id            = statementAttr.Id,
                    StatementType = _statementAnalyzer.Analyse(statementAttr.Sql),
                    SqlTags       = new List <ITag>
                    {
                        new SqlText(statementAttr.Sql, sqlMap.SmartSqlConfig.Database.DbProvider.ParameterPrefix)
                    },
                    CommandType = statementAttr.CommandType,
                    EnablePropertyChangedTrack = statementAttr.EnablePropertyChangedTrack,
                    ReadDb = statementAttr.ReadDb
                };
                if (statementAttr.CommandTimeout > 0)
                {
                    statement.CommandTimeout = statementAttr.CommandTimeout;
                }


                if (statementAttr.SourceChoice != DataSourceChoice.Unknow)
                {
                    statement.SourceChoice = statementAttr.SourceChoice;
                }

                if (resultCacheAttr != null)
                {
                    statement.CacheId = ParseCacheFullId(sqlMap.Scope, resultCacheAttr.CacheId);
                    statement.Cache   = sqlMap.GetCache(statement.CacheId);
                }

                sqlMap.Statements.Add(statement.FullSqlId, statement);
            }

            returnType = isTaskReturnType ? returnType.GetGenericArguments().FirstOrDefault() : returnType;
            if (returnType == typeof(DataTable))
            {
                statementAttr.Execute = ExecuteBehavior.GetDataTable;
            }

            if (returnType == typeof(DataSet))
            {
                statementAttr.Execute = ExecuteBehavior.GetDataSet;
            }

            if (statementAttr.Execute == ExecuteBehavior.Auto)
            {
                if (CommonType.IsValueTuple(returnType))
                {
                    statementAttr.Execute = ExecuteBehavior.QuerySingle;
                }
                else if (returnType == CommonType.Int32 || returnType == CommonType.Void || returnType == null)
                {
                    statementAttr.Execute = ExecuteBehavior.Execute;
                    if (returnType == CommonType.Int32)
                    {
                        if (statement.StatementType.HasFlag(Configuration.StatementType.Select))
                        {
                            statementAttr.Execute = ExecuteBehavior.ExecuteScalar;
                        }
                    }
                }
                else if (returnType.IsValueType || returnType == CommonType.String)
                {
                    statementAttr.Execute = ExecuteBehavior.ExecuteScalar;
                    if (!statement.StatementType.HasFlag(Configuration.StatementType.Select))
                    {
                        statementAttr.Execute = ExecuteBehavior.Execute;
                    }
                }
                else
                {
                    var isQueryEnumerable = typeof(IEnumerable).IsAssignableFrom(returnType);
                    statementAttr.Execute = isQueryEnumerable ? ExecuteBehavior.Query : ExecuteBehavior.QuerySingle;
                }
            }

            executeBehavior = statementAttr.Execute;
            return(statement);
        }