Exemplo n.º 1
0
        private void BuildDbParameters(AbstractRequestContext reqConetxt)
        {
            if (reqConetxt.CommandType == CommandType.StoredProcedure)
            {
                foreach (var sqlParameter in reqConetxt.Parameters.Values)
                {
                    if (sqlParameter.SourceParameter != null)
                    {
                        sqlParameter.OnSetSourceParameter.Invoke(sqlParameter);
                        continue;
                    }

                    var sourceParam = _dbProviderFactory.CreateParameter();
                    InitSourceDbParameter(sourceParam, sqlParameter);
                    sourceParam.ParameterName = sqlParameter.Name;
                    sourceParam.Value         = sqlParameter.Value;
                    sqlParameter.TypeHandler?.SetParameter(sourceParam, sqlParameter.Value);
                    sqlParameter.SourceParameter = sourceParam;
                }
            }
            else
            {
                reqConetxt.RealSql = _sqlParamAnalyzer.Replace(reqConetxt.RealSql, (paramName, nameWithPrefix) =>
                {
                    if (!reqConetxt.Parameters.TryGetValue(paramName, out var sqlParameter))
                    {
                        return(nameWithPrefix);
                    }

                    ITypeHandler typeHandler =
                        (reqConetxt.ParameterMap?.GetParameter(paramName)?.Handler ?? sqlParameter.TypeHandler) ??
                        _typeHandlerFactory.GetTypeHandler(sqlParameter.ParameterType);

                    var sourceParam = _dbProviderFactory.CreateParameter();
                    InitSourceDbParameter(sourceParam, sqlParameter);
                    sourceParam.ParameterName = sqlParameter.Name;
                    typeHandler.SetParameter(sourceParam, sqlParameter.Value);
                    sqlParameter.SourceParameter = sourceParam;
                    if (sqlParameter.Name != paramName)
                    {
                        return
                        ($"{reqConetxt.ExecutionContext.SmartSqlConfig.Database.DbProvider.ParameterPrefix}{sqlParameter.Name}");
                    }

                    return(nameWithPrefix);
                });
            }
        }
        private void InitParameters(ExecutionContext executionContext)
        {
            BuildSql(executionContext.Request);
            BuildDbParameters(executionContext.Request);
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                var    sourceParameters = executionContext.Request.Parameters.DbParameters.Values;
                var    cmdText          = executionContext.Request.RealSql;
                string dbParameterStr   = string.Join(",", sourceParameters.Select(p => $"{p.ParameterName}={p.Value}"));
                string realSql          = _sqlParamAnalyzer.Replace(cmdText, (paramName, nameWithPrefix) =>
                {
                    var paramNameCompare = executionContext.SmartSqlConfig.Settings.IgnoreParameterCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture;
                    var dbParam          = sourceParameters.FirstOrDefault(m => String.Equals(m.ParameterName, paramName, paramNameCompare));
                    if (dbParam == null)
                    {
                        return(nameWithPrefix);
                    }
                    if (dbParam.Value == DBNull.Value)
                    {
                        return("NULL");
                    }
                    switch (dbParam.DbType)
                    {
                    case DbType.AnsiString:
                    case DbType.AnsiStringFixedLength:
                    case DbType.DateTime:
                    case DbType.DateTime2:
                    case DbType.DateTimeOffset:
                    case DbType.Guid:
                    case DbType.String:
                    case DbType.StringFixedLength:
                    case DbType.Time:
                    case DbType.Xml:
                        { return($"'{dbParam.Value}'"); }

                    case DbType.Boolean:
                        {
                            return(((bool)dbParam.Value) ? "1" : "0");
                        }
                    }
                    return(dbParam.Value.ToString());
                });
                _logger.LogDebug($"Statement.Id:[{executionContext.Request.FullSqlId}],Sql:{Environment.NewLine}{cmdText}{Environment.NewLine}Parameters:[{dbParameterStr}]{Environment.NewLine}Sql with parameter value: {Environment.NewLine}{realSql}");
            }
        }