Exemplo n.º 1
0
        public static T WithTempTableExpression <T>(this System.Data.Entity.DbContext dbContexWithTempTable, IQueryable <ITempTable> expression, bool reuseExisting = false)
            where T : class
        {
            var contextWithTempTable    = (IDbContextWithTempTable)dbContexWithTempTable;
            var tableMetadataProvider   = new TableMetadataProvider();
            var tempTableType           = expression.ElementType.BaseType;
            var tempTableName           = tableMetadataProvider.GetTableNameFromBaseType(tempTableType);
            var hasAttachedDDLStatement = contextWithTempTable.TempTableContainer.TempSqlQueriesList.Any(x => x.Key == tempTableName);

            Validate(contextWithTempTable, tempTableName);

            var fieldsWithTypes               = tableMetadataProvider.GetFieldsWithTypes(tempTableType);
            var clusteredIndexesWithFields    = tableMetadataProvider.GetClusteredIndexColumns(tempTableType);
            var nonClusteredIndexesWithFields = tableMetadataProvider.GetNonClusteredIndexesWithColumns(tempTableType);

            var sqlSelectQuery      = expression.ToTraceQuery();
            var objectQuery         = expression.GetObjectQuery();
            var fieldsWithPositions = objectQuery.GetQueryPropertyPositions();

            var sqlAllCommandsQuery = "";

            if (hasAttachedDDLStatement)
            {
                sqlAllCommandsQuery = SqlInsertCommandBuilder.Continue(tempTableName)
                                      .AddInsertQuery(fieldsWithPositions, sqlSelectQuery)
                                      .Execute();
            }
            else
            {
                sqlAllCommandsQuery = SqlInsertCommandBuilder.Begin(tempTableName)
                                      .DropIfExists()
                                      .Create(fieldsWithTypes)
                                      .AddClusteredIndex(clusteredIndexesWithFields)
                                      .AddNonClusteredIndexes(nonClusteredIndexesWithFields)
                                      .AddInsertQuery(fieldsWithPositions, sqlSelectQuery)
                                      .Execute();
            }

            var tempTableDependencyManager = new TempTableDependencyManager(objectQuery, contextWithTempTable.TempTableContainer);

            tempTableDependencyManager.AddDependenciesForTable(tempTableName);

            contextWithTempTable.TempTableContainer
            .TempSqlQueriesList
            .Enqueue(new KeyValuePair <string, Query>(tempTableName, new Query
            {
                QueryString  = sqlAllCommandsQuery,
                IsDataAppend = hasAttachedDDLStatement
            }));

            return(dbContexWithTempTable as T);
        }
Exemplo n.º 2
0
        public static T WithTempTableExpression <T>(this System.Data.Entity.DbContext dbContexWithTempTable, IEnumerable <ITempTable> list, bool reuseExisting = false)
            where T : class
        {
            var tableMetadataProvider   = new TableMetadataProvider();
            var contextWithTempTable    = (IDbContextWithTempTable)dbContexWithTempTable;
            var tempTableType           = list.First().GetType();
            var tempTableName           = tableMetadataProvider.GetTableNameFromBaseType(tempTableType);
            var hasAttachedDDLStatement = contextWithTempTable.TempTableContainer.TempSqlQueriesList.Any(x => x.Key == tempTableName);

            Validate(contextWithTempTable, tempTableName);

            var fieldsWithTypes               = tableMetadataProvider.GetFieldsWithTypes(tempTableType);
            var clusteredIndexesWithFields    = tableMetadataProvider.GetClusteredIndexColumns(tempTableType);
            var nonClusteredIndexesWithFields = tableMetadataProvider.GetNonClusteredIndexesWithColumns(tempTableType);

            var sqlAllCommandsQuery = "";

            if (hasAttachedDDLStatement)
            {
                sqlAllCommandsQuery = SqlInsertCommandBuilder.Continue(tempTableName)
                                      .AddInsertQuery(list)
                                      .Execute();
            }
            else
            {
                sqlAllCommandsQuery = SqlInsertCommandBuilder.Begin(tempTableName)
                                      .DropIfExists()
                                      .Create(fieldsWithTypes)
                                      .AddClusteredIndex(clusteredIndexesWithFields)
                                      .AddNonClusteredIndexes(nonClusteredIndexesWithFields)
                                      .AddInsertQuery(list)
                                      .Execute();
            }

            contextWithTempTable.TempTableContainer
            .TempSqlQueriesList
            .Enqueue(new KeyValuePair <string, Query>(tempTableName, new Query
            {
                QueryString  = sqlAllCommandsQuery,
                IsDataAppend = hasAttachedDDLStatement
            }));

            return(dbContexWithTempTable as T);
        }