예제 #1
0
        public ValueTask <object> ExecuteScalar(string sql, SqlParameters sqlParameters, HttpContext httpContext)
        {
            CustomMetadataStore customMetadataStore = new CustomMetadataStore();

            return(_sqlExecutor.ExecuteScalar(sql, sqlParameters, new TableResolverData(
                                                  httpContext,
                                                  _serviceProvider,
                                                  customMetadataStore)));
        }
예제 #2
0
        public async ValueTask <Transport.QueryResult> Execute(string sql, SqlParameters sqlParameters, HttpContext httpContext)
        {
            _logger.LogInformation("Executing query: " + sql);
            foreach (var header in httpContext.Request.Headers)
            {
                if (header.Key.StartsWith("P_", StringComparison.OrdinalIgnoreCase))
                {
                    var parameterName = header.Key.Substring(2);

                    if (header.Value.Count > 1)
                    {
                        throw new SqlErrorException("Two parameters found with the same name in the http headers.");
                    }
                    var value = header.Value.First();
                    // URL decode
                    value = HttpUtility.UrlDecode(value, Encoding.UTF8);

                    sqlParameters.Add(SqlParameter.Create(parameterName, value));
                }
            }
            //Parse the sql
            var sqlTree = _sqlParser.Parse(sql, out var errors);

            //Check for parsing errors
            if (errors.Count > 0)
            {
                throw new SqlErrorException(errors.First().Message);
            }

            //Apply the row level security filter on the query
            await RowLevelSecurityHelper.ApplyRowLevelSecurity(sqlTree, httpContext, _metadataStore, _serviceProvider);

            //Only calculate the sql after the row level security if logging level is debug
            _logger.LogConditionally(LogLevel.Debug, logger => logger.LogDebug($"Sql after row level security: {sqlTree.Print()}"));

            CustomMetadataStore customMetadataStore = new CustomMetadataStore();
            var result = await _sqlExecutor.Execute(sqlTree, sqlParameters, new TableResolverData(
                                                        httpContext,
                                                        _serviceProvider,
                                                        customMetadataStore)).ConfigureAwait(false);

            var columnsBuilder = ImmutableList.CreateBuilder <Transport.Column>();

            foreach (var column in result.Columns)
            {
                if (!_metadataStore.TryGetTypeColumns(column.Type, out var columns))
                {
                    columns = new List <TableColumn>();
                }

                var childrenList = ImmutableList.CreateBuilder <Transport.Column>();
                foreach (var child in columns)
                {
                    childrenList.Add(GetTransportColumn(child));
                }

                var(columnType, nullable) = ColumnTypeHelper.GetKoraliumType(column.Type);
                columnsBuilder.Add(new Transport.Column(column.Name, column.Type, column.GetFunction, childrenList.ToImmutable(), columnType, nullable));
            }

            return(new Transport.QueryResult(
                       result.Result,
                       columnsBuilder.ToImmutable(),
                       customMetadataStore.GetMetadataValues().Select(x => new KeyValuePair <string, string>(x.Key, x.Value.ToString()))
                       ));
        }