Example #1
0
        /// <summary>
        /// Compile a sqlBuilder into a query which can be executed multiple times
        /// </summary>
        /// <param name="sqlBuilder">The builder with all properties populated</param>
        /// <param name="parameters">Any constant parameters in the statement</param>
        /// <param name="queryParseType">Define the way results are to be parsed</param>
        public static CompiledQuery <TArgs, TResult> Compile <TArgs, TResult> (
            this ISqlString sqlBuilder,
            ISqlSelectStatement statement,
            IEnumerable <object> parameters,
            ISqlSyntax sqlSyntax,
            QueryParseType queryParseType,
            bool requiresSimpleValueUnwrap)
        {
            var sql = ToSql(sqlBuilder);

            var selectColumns = statement.SelectColumns.Select(Alias).ToArray();
            var resultType    = requiresSimpleValueUnwrap
                ? ReflectionUtils.CreatePropMapValue(typeof(TResult))
                : typeof(TResult);
            var propertyGraph = statement.BuildObjetPropertyGraph(resultType, queryParseType);

            return(new CompiledQuery <TArgs, TResult>(sql, parameters.ToArray(), selectColumns, propertyGraph, sqlSyntax, requiresSimpleValueUnwrap));
        }
Example #2
0
        /// <summary>
        /// Build an object property graph from a sql builder
        /// </summary>
        /// <param name="sqlBuilder">The builder with all properties populated</param>
        /// <param name="queryParseType">Define the way results are to be parsed</param>
        public static RootObjectPropertyGraph BuildObjetPropertyGraph(this ISqlSelectStatement sqlBuilder, Type objectType, QueryParseType queryParseType)
        {
            // row id's for each mapped table
            var mappedTableProperties = sqlBuilder.MappedPropertiesToPrimaryKeys
                                        .Select(GetMappedTable);

            // map each column to a chain of row id column numbers
            var columnWithPrimaryKeys = sqlBuilder
                                        .SelectColumns
                                        .Select(GetMappedColumn);

            return(ObjectPropertyGraphBuilder.Build(
                       objectType,
                       sqlBuilder.GetPrimaryTableKeyColumnIndexes().ToArray(),
                       mappedTableProperties,
                       columnWithPrimaryKeys,
                       queryParseType));

            (string name, int[] primaryKeyColumnMap) GetMappedTable((string columnGroupPrefix, ICompositeKey key) map) => (
                map.columnGroupPrefix,
                sqlBuilder.GetRowNumberColumnIndexes(map.key, map.key.Table).ToArray());

            (string name, int[] primaryKeyColumnMap, Type dataCellType, ConstructorInfo[] isConstructorArg) GetMappedColumn(ISelectColumn column) => (
                column.Alias,
                sqlBuilder
                .GetRowNumberColumnIndexes(
                    column.Alias,
                    TablePrecedenceOrderer.GetSingleMappingContext(column))
                .ToArray(),
                column.DataType,
                column.ArgConstructors);
        }