/// <summary> /// Get a list of pk columns for the primary table /// <summary> public static IEnumerable <int> GetPrimaryTableKeyColumnIndexes(this ISqlSelectStatement sqlStatement) { foreach (var k in sqlStatement.PrimaryKey) { yield return(sqlStatement.SelectColumns.IndexOf(k)); } }
/// <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); }
/// <summary> /// Initialize a new instance of SqlServerSource class from a SQL subquery. /// </summary> /// <param name="statement">A SQL SELECT statement.</param> /// <param name="name">Subquery name.</param> /// <exception cref="System.ArgumentException"><paramref name="statement"/> is null.</exception> /// <exception cref="System.ArgumentException"><paramref name="name"/> is null or empty.</exception> public SqlServerSource(ISqlSelectStatement statement, string name) : base(new SqlObject(statement, name, new SqlServerObjectFullnameProvider(), new SqlServerObjectNamedSqlStringProvider(true))) { if (statement == null) { throw new ArgumentException("SQL SELECT statement is null.", "statement"); } }
public MappedSqlStatementBuilder( BuildMapState state, IEnumerable <QueryElementBasedMappedProperty> selectProperties, ISqlSelectStatement statement, ISqlString innerSqlString, ISqlSyntax sqlSyntax) { State = state ?? throw new ArgumentNullException(nameof(state)); SelectProperties = selectProperties?.ToDictionary(x => x.To) ?? throw new ArgumentNullException(nameof(selectProperties)); Statement = statement ?? throw new ArgumentNullException(nameof(statement)); InnerSqlString = innerSqlString ?? throw new ArgumentNullException(nameof(innerSqlString)); SqlSyntax = sqlSyntax ?? throw new ArgumentNullException(nameof(innerSqlString)); // this paradigm can be extended if there are more than 1 // alias needed per query InnerQueryAlias = SqlStatementConstants.InnerQueryAlias; }
/// <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)); }
static IEnumerable <int> GetRowNumberColumnIndexes(this ISqlSelectStatement sqlStatement, IEnumerable <ICompositeKey> keys) { keys = keys.Enumerate(); return(RemoveTrailingMinusOnes( keys.SelectMany(GetIndexes), keys .SelectMany(k => k .Select(c => c.Alias)) .JoinString(", "))); IEnumerable <int> GetIndexes(ICompositeKey key) { foreach (var k in key) { yield return(sqlStatement.SelectColumns.IndexOf(k)); } } }
/// <summary> /// Get a list of pk columns which is a chain from the "forKey" value to the "root", containing the "context" /// <summary> public static IEnumerable <ICompositeKey> SupplimentPrimaryKeyColumns(this ISqlSelectStatement sqlStatement, ICompositeKey forKey, IQueryTable context) { // if the piece is a parameter, Table will be null if (forKey == null) { return(Enumerable.Empty <ICompositeKey>()); } var path = forKey.Table .GetPrimaryKeyColumns(context) .ToArray(); if (path.Contains(context.PrimaryKey)) { return(path); } // in this case a column is being used in the // context of one of its child properties return(context.GetPrimaryKeyColumns(forKey.Table)); }
/// <inheritdoc /> public ISqlObject CreateSource(ISqlSelectStatement statement, string name) { return(new MySqlSource(statement, name)); }
/// <summary> /// Initialize a instance of DatabaseExpirilityProvider class. /// </summary> /// <param name="statement"></param> /// <param name="connectionSelector"></param> public DatabaseExpirilityProvider(ISqlSelectStatement statement, Func <IDbConnection> connectionSelector) : this(statement != null ? (obj) => statement : (Func <T, ISqlSelectStatement>)null, connectionSelector) { }
public static IEnumerable <int> GetRowNumberColumnIndexes(this ISqlSelectStatement sqlStatement, ICompositeKey key, IQueryTable context) { return(sqlStatement.GetRowNumberColumnIndexes(sqlStatement.SupplimentPrimaryKeyColumns(key, context))); }
public static IEnumerable <int> GetRowNumberColumnIndexes(this ISqlSelectStatement sqlStatement, string columnAlias, IQueryTable context) { return(sqlStatement.GetRowNumberColumnIndexes(sqlStatement.GetPrimaryKeyColumns(columnAlias, context))); }
/// <summary> /// Get a list of pk columns for the "columnAlias" containing the "context" /// <summary> public static IEnumerable <ICompositeKey> GetPrimaryKeyColumns(this ISqlSelectStatement sqlStatement, string columnAlias, IQueryTable context) { return(sqlStatement.SupplimentPrimaryKeyColumns( sqlStatement.SelectColumns[columnAlias].PrimaryKey, context)); }