@@ -34,9 +34,23 @@ public static Selection Parse(ParserContext context, uint depth)
3434 }
3535
3636 List < Expression > expressions = [ ] ;
37+ List < BooleanExpression > excluders = [ ] ;
3738
38- IEnumerable < object ? [ ] > ApplyClauses ( IEnumerable < object ? [ ] > records )
39+ IEnumerable < object ? [ ] > ApplyClauses ( IEnumerable < object ? [ ] > records , Func < object ? [ ] , List < string > , object ? > getColumnValueFromRow )
3940 {
41+ records = records . Where ( row =>
42+ {
43+ foreach ( var excluder in excluders )
44+ {
45+ if ( ! excluder . Run ( columnName => getColumnValueFromRow ( row , columnName ) ) )
46+ return false ;
47+ }
48+
49+ return true ;
50+ } ) . Select < object ? [ ] , object ? [ ] > ( row =>
51+ [ .. expressions . Select ( x => x . Run ( columnName => getColumnValueFromRow ( row , columnName ) ) ) ]
52+ ) ;
53+
4054 if ( topCount is not null )
4155 records = records . Take ( topCount . GetValueOrDefault ( ) ) ;
4256
@@ -105,11 +119,11 @@ public static Selection Parse(ParserContext context, uint depth)
105119
106120 return new ( new (
107121 expressions ,
108- ApplyClauses ( table . Rows . Select < object ? [ ] , object ? [ ] > ( row => [ .. expressions . Select ( x => x . Run ( columnName =>
122+ ApplyClauses ( table . Rows , ( row , columnName ) =>
109123 {
110124 var columnIndex = table . Columns . FindIndex ( column => Collation . Default . Equals ( column . Name , columnName . Last ( ) ) ) ;
111125 return columnIndex == - 1 ? throw SimulatedSqlException . InvalidColumnName ( columnName ) : row [ columnIndex ] ;
112- } ) ) ] ) )
126+ } )
113127 ) ) ;
114128
115129 case Operator { Character : '(' } :
@@ -131,16 +145,21 @@ public static Selection Parse(ParserContext context, uint depth)
131145
132146 return new ( new (
133147 expressions ,
134- ApplyClauses ( derived . Select < object ? [ ] , object ? [ ] > ( row => [ .. expressions . Select ( x => x . Run ( columnName =>
148+ ApplyClauses ( derived . records , ( row , columnName ) =>
135149 {
136150 var columnIndex = Array . FindIndex ( derived . columnNames , name => Collation . Default . Equals ( name , columnName . Last ( ) ) ) ;
137151 return columnIndex == - 1 ? throw SimulatedSqlException . InvalidColumnName ( columnName ) : row [ columnIndex ] ;
138- } ) ) ] ) )
152+ } )
139153 ) ) ;
140154 }
141155 }
142156
143157 throw SimulatedSqlException . SyntaxErrorNear ( context ) ;
158+
159+ case ReservedKeyword { Keyword : Keyword . Where } :
160+ context . MoveNextRequired ( ) ;
161+ excluders . Add ( BooleanExpression . Parse ( context ) ) ;
162+ continue ;
144163 }
145164
146165 throw SimulatedSqlException . SyntaxErrorNear ( context ) ;
@@ -149,7 +168,7 @@ public static Selection Parse(ParserContext context, uint depth)
149168
150169 return new ( new (
151170 expressions ,
152- ApplyClauses ( [ [ .. expressions . Select ( x => x . Run ( column => throw SimulatedSqlException . InvalidColumnName ( column ) ) ) ] ] )
171+ ApplyClauses ( [ [ .. expressions . Select ( x => x . Run ( column => throw SimulatedSqlException . InvalidColumnName ( column ) ) ) ] ] , ( row , columnName ) => throw new NotSupportedException ( ) )
153172 ) ) ;
154173 }
155174}
0 commit comments