44// Created by: Denis Krjuchkov
55// Created: 2009.11.13
66
7- using System ;
8- using System . Collections . Concurrent ;
9- using System . Collections . Generic ;
10- using System . Linq ;
11- using System . Linq . Expressions ;
12- using Xtensive . Core ;
13- using Xtensive . Linq ;
14- using Xtensive . Orm . Internals ;
157using Xtensive . Orm . Model ;
168using Xtensive . Orm . Rse . Providers ;
179using Xtensive . Reflection ;
@@ -24,22 +16,7 @@ namespace Xtensive.Orm.Providers
2416{
2517 public partial class SqlCompiler
2618 {
27- protected readonly struct QueryAndBindings
28- {
29- public SqlSelect Query { get ; }
30- public List < QueryParameterBinding > Bindings { get ; }
31-
32- public QueryAndBindings ( SqlSelect initialQuery )
33- {
34- Query = initialQuery ;
35- Bindings = new List < QueryParameterBinding > ( ) ;
36- }
37- public QueryAndBindings ( SqlSelect initialQuery , List < QueryParameterBinding > bindings )
38- {
39- Query = initialQuery ;
40- Bindings = bindings ;
41- }
42- }
19+ protected record struct QueryAndBindings ( SqlSelect Query , List < QueryParameterBinding > Bindings ) ;
4320
4421 private TypeMapping int32TypeMapping ;
4522
@@ -54,17 +31,13 @@ internal protected override SqlProvider VisitIndex(IndexProvider provider)
5431 protected QueryAndBindings BuildProviderQuery ( IndexInfo index )
5532 {
5633 if ( index . IsVirtual ) {
57- if ( ( index . Attributes & IndexAttributes . Union ) > 0 )
58- return BuildUnionQuery ( index ) ;
59- if ( ( index . Attributes & IndexAttributes . Join ) > 0 )
60- return BuildJoinQuery ( index ) ;
61- if ( ( index . Attributes & IndexAttributes . Filtered ) > 0 )
62- return BuildFilteredQuery ( index ) ;
63- if ( ( index . Attributes & IndexAttributes . View ) > 0 )
64- return BuildViewQuery ( index ) ;
65- if ( ( index . Attributes & IndexAttributes . Typed ) > 0 )
66- return BuildTypedQuery ( index ) ;
67- throw new NotSupportedException ( String . Format ( Strings . ExUnsupportedIndex , index . Name , index . Attributes ) ) ;
34+ var attrs = index . Attributes ;
35+ return ( attrs & IndexAttributes . Union ) > 0 ? BuildUnionQuery ( index )
36+ : ( attrs & IndexAttributes . Join ) > 0 ? BuildJoinQuery ( index )
37+ : ( attrs & IndexAttributes . Filtered ) > 0 ? BuildFilteredQuery ( index )
38+ : ( attrs & IndexAttributes . View ) > 0 ? BuildViewQuery ( index )
39+ : ( attrs & IndexAttributes . Typed ) > 0 ? BuildTypedQuery ( index )
40+ : throw new NotSupportedException ( String . Format ( Strings . ExUnsupportedIndex , index . Name , index . Attributes ) ) ;
6841 }
6942 return BuildTableQuery ( index ) ;
7043 }
@@ -98,7 +71,7 @@ private QueryAndBindings BuildTableQuery(IndexInfo index)
9871 queryColumns . Add ( tableRef [ lookup [ c . Field ] ] ) ;
9972 }
10073 }
101- return new QueryAndBindings ( query ) ;
74+ return new QueryAndBindings ( query , [ ] ) ;
10275 }
10376
10477 private QueryAndBindings BuildUnionQuery ( IndexInfo index )
@@ -285,10 +258,9 @@ private QueryAndBindings BuildTypedQuery(IndexInfo index)
285258 var baseQueryAndBindings = BuildProviderQuery ( underlyingIndex ) ;
286259 var baseQuery = baseQueryAndBindings . Query ;
287260 var bindings = baseQueryAndBindings . Bindings ;
288- var query = SqlDml . Select ( baseQuery . From ) ;
261+ var query = SqlDml . Select ( baseQuery . From , baseQuery . Columns . Count + 1 ) ;
289262 query . Where = baseQuery . Where ;
290263
291- var baseColumns = baseQuery . Columns . ToList ( ) ;
292264 var typeIdColumnIndex = index . Columns
293265 . Select ( ( c , i ) => ( c . Field , i ) )
294266 . Single ( p => p . Field . IsTypeId && p . Field . IsSystem ) . i ;
@@ -332,8 +304,19 @@ private QueryAndBindings BuildTypedQuery(IndexInfo index)
332304 }
333305
334306 var typeIdColumnRef = SqlDml . ColumnRef ( typeIdColumn , WellKnown . TypeIdFieldName ) ;
335- baseColumns . Insert ( typeIdColumnIndex , typeIdColumnRef ) ;
336- query . Columns . AddRange ( baseColumns ) ;
307+ var queryColumns = query . Columns ;
308+ bool inserted = false ;
309+ int i = 0 ;
310+ foreach ( var column in baseQuery . Columns ) {
311+ if ( i ++ == typeIdColumnIndex ) {
312+ queryColumns . Add ( typeIdColumnRef ) ;
313+ inserted = true ;
314+ }
315+ queryColumns . Add ( column ) ;
316+ }
317+ if ( ! inserted ) {
318+ queryColumns . Add ( typeIdColumnRef ) ;
319+ }
337320
338321 return new QueryAndBindings ( query , bindings ) ;
339322 }
0 commit comments