Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FilterItemModel"/> class.
        /// </summary>
        /// <param name="column"></param>
        /// <param name="type"></param>
        /// <param name="filter"></param>
        /// <param name="value"></param>
        public FilterItemModel(string column, ColumnType type, FilterType filter, object value)
            : this(column)
        {
            if (!ColumnTypeHelper.GetValidFilterTypes(type).Contains(filter))
            {
                throw new ArgumentOutOfRangeException(nameof(filter), $"{filter} is invalid for {type}.");
            }

            Column = column;
            Type   = type;
            Filter = filter;
            Value  = value;
        }
Exemplo n.º 2
0
        private static (bool, string) CheckCircularDependency(Type type, HashSet <Type> visitedTypes)
        {
            var(columnType, _) = ColumnTypeHelper.GetKoraliumType(type);

            if (columnType == Transport.ColumnType.Object)
            {
                return(CheckObjectCircularDependency(type, visitedTypes));
            }
            else if (columnType == Transport.ColumnType.List)
            {
                return(CheckListCircularDependency(type, visitedTypes));
            }
            return(false, string.Empty);
        }
Exemplo n.º 3
0
        private Transport.Column GetTransportColumn(TableColumn tableColumn)
        {
            var childrenList = ImmutableList.CreateBuilder <Transport.Column>();

            if (tableColumn.Children != null)
            {
                foreach (var child in tableColumn.Children)
                {
                    childrenList.Add(GetTransportColumn(child));
                }
            }
            var(columnType, nullable) = ColumnTypeHelper.GetKoraliumType(tableColumn.ColumnType);
            return(new Transport.Column(tableColumn.Name, tableColumn.ColumnType, tableColumn.PropertyAccessor, childrenList.ToImmutable(), columnType, nullable));
        }
Exemplo n.º 4
0
        private IImmutableList <Transport.Column> ConvertColumns(IImmutableList <ColumnMetadata> columnMetadatas)
        {
            var columnsBuilder = ImmutableList.CreateBuilder <Transport.Column>();

            foreach (var column in columnMetadatas)
            {
                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(columnsBuilder.ToImmutable());
        }
Exemplo n.º 5
0
        private static bool TryCreateEx(IExceptionContext ectx, ColInfo info, DataKind kind, KeyCount range,
                                        out PrimitiveDataViewType itemType, out ColInfoEx ex)
        {
            ectx.AssertValue(info);
            ectx.Assert(Enum.IsDefined(typeof(DataKind), kind));

            ex = null;

            var typeSrc = info.TypeSrc;

            if (range != null)
            {
                itemType = TypeParsingUtils.ConstructKeyType(SchemaHelper.DataKind2InternalDataKind(kind), range);
            }
            else if (!typeSrc.ItemType().IsKey())
            {
                itemType = ColumnTypeHelper.PrimitiveFromKind(kind);
            }
            else if (!ColumnTypeHelper.IsValidDataKind(kind))
            {
                itemType = ColumnTypeHelper.PrimitiveFromKind(kind);
                return(false);
            }
            else
            {
                var key = typeSrc.ItemType().AsKey();
                ectx.Assert(ColumnTypeHelper.IsValidDataKind(key.RawKind()));
                ulong count = key.Count;
                // Technically, it's an error for the counts not to match, but we'll let the Conversions
                // code return false below. There's a possibility we'll change the standard conversions to
                // map out of bounds values to zero, in which case, this is the right thing to do.
                ulong max = (ulong)kind;
                if ((ulong)count > max)
                {
                    count = max;
                }
                itemType = new KeyDataViewType(SchemaHelper.DataKind2ColumnType(kind).RawType, count);
            }

            // Ensure that the conversion is legal. We don't actually cache the delegate here. It will get
            // re-fetched by the utils code when needed.
            bool     identity;
            Delegate del;

            if (!Conversions.DefaultInstance.TryGetStandardConversion(typeSrc.ItemType(), itemType, out del, out identity))
            {
                if (typeSrc.ItemType().RawKind() == itemType.RawKind())
                {
                    switch (typeSrc.ItemType().RawKind())
                    {
                    case DataKind.UInt32:
                        // Key starts at 1.
                        // Multiclass future issue
                        uint plus = (itemType.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                        identity = false;
                        ValueMapper <uint, uint> map_ = (in uint src, ref uint dst) => { dst = src + plus; };
                        del = (Delegate)map_;
                        if (del == null)
                        {
                            throw Contracts.ExceptNotSupp("Issue with casting");
                        }
                        break;

                    default:
                        throw Contracts.Except("Not suppoted type {0}", typeSrc.ItemType().RawKind());
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Int64 && kind == DataKind.UInt64)
                {
                    ulong plus = (itemType.IsKey() ? (ulong)1 : (ulong)0) - (typeSrc.IsKey() ? (ulong)1 : (ulong)0);
                    identity = false;
                    ValueMapper <long, ulong> map_ = (in long src, ref ulong dst) =>
                    {
                        CheckRange(src, dst, ectx); dst = (ulong)src + plus;
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Single && kind == DataKind.UInt64)
                {
                    ulong plus = (itemType.IsKey() ? (ulong)1 : (ulong)0) - (typeSrc.IsKey() ? (ulong)1 : (ulong)0);
                    identity = false;
                    ValueMapper <float, ulong> map_ = (in float src, ref ulong dst) =>
                    {
                        CheckRange(src, dst, ectx); dst = (ulong)src + plus;
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Int64 && kind == DataKind.UInt32)
                {
                    // Multiclass future issue
                    uint plus = (itemType.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                    identity = false;
                    ValueMapper <long, uint> map_ = (in long src, ref uint dst) =>
                    {
                        CheckRange(src, dst, ectx); dst = (uint)src + plus;
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Single && kind == DataKind.UInt32)
                {
                    // Multiclass future issue
                    uint plus = (itemType.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                    identity = false;
                    ValueMapper <float, uint> map_ = (in float src, ref uint dst) =>
                    {
                        CheckRange(src, dst, ectx); dst = (uint)src + plus;
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Single && kind == DataKind.String)
                {
                    // Multiclass future issue
                    identity = false;
                    ValueMapper <float, DvText> map_ = (in float src, ref DvText dst) =>
                    {
                        dst = new DvText(string.Format("{0}", (int)src));
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else
                {
                    return(false);
                }
            }

            DataViewType typeDst = itemType;

            if (typeSrc.IsVector())
            {
                typeDst = new VectorDataViewType(itemType, typeSrc.AsVector().Dimensions.ToArray());
            }

            // An output column is transposable iff the input column was transposable.
            VectorDataViewType slotType = null;

            if (info.SlotTypeSrc != null)
            {
                slotType = new VectorDataViewType(itemType, info.SlotTypeSrc.Dimensions.ToArray());
            }

            ex = new ColInfoEx(kind, range != null, typeDst, slotType);
            return(true);
        }
Exemplo n.º 6
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()))
                       ));
        }