Ejemplo n.º 1
0
 private void InitByStatement(AbstractRequestContext requestContext, SqlMap sqlMap)
 {
     requestContext.Statement = sqlMap.GetStatement(requestContext.FullSqlId);
     if (requestContext.Statement.SourceChoice.HasValue)
     {
         requestContext.DataSourceChoice = requestContext.Statement.SourceChoice.Value;
     }
     else
     {
         requestContext.DataSourceChoice = (StatementType.Write & requestContext.Statement.StatementType)
                                           != StatementType.Unknown ? DataSourceChoice.Write : DataSourceChoice.Read;
     }
     if (requestContext.Statement.CommandType.HasValue)
     {
         requestContext.CommandType = requestContext.Statement.CommandType.Value;
     }
     requestContext.Transaction         = requestContext.Transaction ?? requestContext.Statement.Transaction;
     requestContext.CommandTimeout      = requestContext.CommandTimeout ?? requestContext.Statement.CommandTimeout;
     requestContext.ReadDb              = requestContext.Statement.ReadDb;
     requestContext.CacheId             = requestContext.Statement.CacheId;
     requestContext.Cache               = requestContext.Statement.Cache;
     requestContext.ParameterMapId      = requestContext.Statement.ParameterMapId;
     requestContext.ParameterMap        = requestContext.Statement.ParameterMap;
     requestContext.ResultMapId         = requestContext.Statement.ResultMapId;
     requestContext.ResultMap           = requestContext.Statement.ResultMap;
     requestContext.MultipleResultMapId = requestContext.Statement.MultipleResultMapId;
     requestContext.MultipleResultMap   = requestContext.Statement.MultipleResultMap;
 }
Ejemplo n.º 2
0
 private void InitByStatement(AbstractRequestContext requestContext, SqlMap sqlMap)
 {
     requestContext.Statement = sqlMap.GetStatement(requestContext.FullSqlId);
     if (requestContext.Statement.SourceChoice.HasValue)
     {
         requestContext.DataSourceChoice = requestContext.Statement.SourceChoice.Value;
     }
     else
     {
         if ((StatementType.Write & requestContext.Statement.StatementType)
             == StatementType.Unknown)
         {
             requestContext.DataSourceChoice = DataSourceChoice.Write;
         }
     }
     if (requestContext.Statement.CommandType.HasValue)
     {
         requestContext.CommandType = requestContext.Statement.CommandType.Value;
     }
     if (String.IsNullOrEmpty(requestContext.ReadDb))
     {
         requestContext.ReadDb = requestContext.Statement.ReadDb;
     }
     requestContext.CacheId             = requestContext.Statement.CacheId;
     requestContext.Cache               = requestContext.Statement.Cache;
     requestContext.ParameterMapId      = requestContext.Statement.ParameterMapId;
     requestContext.ParameterMap        = requestContext.Statement.ParameterMap;
     requestContext.ResultMapId         = requestContext.Statement.ResultMapId;
     requestContext.ResultMap           = requestContext.Statement.ResultMap;
     requestContext.MultipleResultMapId = requestContext.Statement.MultipleResultMapId;
     requestContext.MultipleResultMap   = requestContext.Statement.MultipleResultMap;
 }
Ejemplo n.º 3
0
        private void InitByStatement(AbstractRequestContext requestContext, SqlMap sqlMap)
        {
            requestContext.Statement = sqlMap.GetStatement(requestContext.FullSqlId);
            if (requestContext.DataSourceChoice == DataSourceChoice.Unknow)
            {
                if (requestContext.Statement.SourceChoice.HasValue)
                {
                    requestContext.DataSourceChoice = requestContext.Statement.SourceChoice.Value;
                }
                else
                {
                    requestContext.DataSourceChoice = (StatementType.Write & requestContext.Statement.StatementType)
                                                      != StatementType.Unknown
                        ? DataSourceChoice.Write
                        : DataSourceChoice.Read;
                }
            }

            if (requestContext.Statement.CommandType.HasValue)
            {
                requestContext.CommandType = requestContext.Statement.CommandType.Value;
            }

            if (!requestContext.EnablePropertyChangedTrack.HasValue)
            {
                requestContext.EnablePropertyChangedTrack = requestContext.Statement.EnablePropertyChangedTrack;
            }

            requestContext.Transaction    = requestContext.Transaction ?? requestContext.Statement.Transaction;
            requestContext.CommandTimeout = requestContext.CommandTimeout ?? requestContext.Statement.CommandTimeout;
            if (String.IsNullOrEmpty(requestContext.ReadDb))
            {
                requestContext.ReadDb = requestContext.Statement.ReadDb;
            }

            if (String.IsNullOrEmpty(requestContext.CacheId))
            {
                requestContext.CacheId = requestContext.Statement.CacheId;
                requestContext.Cache   = requestContext.Statement.Cache;
            }
            else
            {
                SetCache(requestContext, sqlMap);
            }

            requestContext.ParameterMapId      = requestContext.Statement.ParameterMapId;
            requestContext.ParameterMap        = requestContext.Statement.ParameterMap;
            requestContext.ResultMapId         = requestContext.Statement.ResultMapId;
            requestContext.ResultMap           = requestContext.Statement.ResultMap;
            requestContext.MultipleResultMapId = requestContext.Statement.MultipleResultMapId;
            requestContext.MultipleResultMap   = requestContext.Statement.MultipleResultMap;
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        private void InitByStatement(AbstractRequestContext requestContext, SqlMap sqlMap)
        {
            requestContext.Statement = sqlMap.GetStatement(requestContext.FullSqlId);
            if (requestContext.DataSourceChoice == DataSourceChoice.Unknow)
            {
                if (requestContext.Statement.SourceChoice.HasValue)
                {
                    requestContext.DataSourceChoice = requestContext.Statement.SourceChoice.Value;
                }
                else
                {
                    requestContext.DataSourceChoice = (StatementType.Write & requestContext.Statement.StatementType)
                                                      != StatementType.Unknown
                        ? DataSourceChoice.Write
                        : DataSourceChoice.Read;
                }
            }

            if (requestContext.Statement.CommandType.HasValue)
            {
                requestContext.CommandType = requestContext.Statement.CommandType.Value;
            }

            if (!requestContext.EnablePropertyChangedTrack.HasValue)
            {
                requestContext.EnablePropertyChangedTrack = requestContext.Statement.EnablePropertyChangedTrack;
            }

            requestContext.Transaction    = requestContext.Transaction ?? requestContext.Statement.Transaction;
            requestContext.CommandTimeout = requestContext.CommandTimeout ?? requestContext.Statement.CommandTimeout;
            if (String.IsNullOrEmpty(requestContext.ReadDb))
            {
                requestContext.ReadDb = requestContext.Statement.ReadDb;
            }

            if (String.IsNullOrEmpty(requestContext.CacheId))
            {
                requestContext.CacheId = requestContext.Statement.CacheId;
                requestContext.Cache   = requestContext.Statement.Cache;
            }
            else
            {
                SetCache(requestContext, sqlMap);
            }

            if (!String.IsNullOrEmpty(requestContext.AutoConverterName))
            {
                if (!_smartSqlConfig.AutoConverters.TryGetValue(requestContext.AutoConverterName, out var autoConverter)
                    )
                {
                    throw new SmartSqlException(
                              $"The auto converter with name {requestContext.AutoConverterName} was not found");
                }

                requestContext.AutoConverter = autoConverter;
            }
            else if (requestContext.Statement.AutoConverter != null)
            {
                requestContext.AutoConverter = requestContext.Statement.AutoConverter;
            }
            else
            {
                requestContext.AutoConverter = sqlMap.AutoConverter;
            }

            requestContext.ParameterMapId      = requestContext.Statement.ParameterMapId;
            requestContext.ParameterMap        = requestContext.Statement.ParameterMap;
            requestContext.ResultMapId         = requestContext.Statement.ResultMapId;
            requestContext.ResultMap           = requestContext.Statement.ResultMap;
            requestContext.MultipleResultMapId = requestContext.Statement.MultipleResultMapId;
            requestContext.MultipleResultMap   = requestContext.Statement.MultipleResultMap;
        }