@@ -82,6 +82,28 @@ public Q AddComponent(string component, AbstractClause clause, string engineCode
8282 return ( Q ) this ;
8383 }
8484
85+ /// <summary>
86+ /// If the query already contains a clause for the given component
87+ /// and engine, replace it with the specified clause. Otherwise, just
88+ /// add the clause.
89+ /// </summary>
90+ /// <param name="component"></param>
91+ /// <param name="clause"></param>
92+ /// <param name="engineCode"></param>
93+ /// <returns></returns>
94+ public Q AddOrReplaceComponent ( string component , AbstractClause clause , string engineCode = null )
95+ {
96+ engineCode = engineCode ?? EngineScope ;
97+
98+ var current = GetComponents ( component ) . SingleOrDefault ( c => c . Engine == engineCode ) ;
99+ if ( current != null )
100+ Clauses . Remove ( current ) ;
101+
102+ return AddComponent ( component , clause , engineCode ) ;
103+ }
104+
105+
106+
85107 /// <summary>
86108 /// Get the list of clauses for a component.
87109 /// </summary>
@@ -123,13 +145,10 @@ public List<AbstractClause> GetComponents(string component, string engineCode =
123145 /// <returns></returns>
124146 public C GetOneComponent < C > ( string component , string engineCode = null ) where C : AbstractClause
125147 {
126- if ( engineCode == null )
127- {
128- engineCode = EngineScope ;
129- }
148+ engineCode = engineCode ?? EngineScope ;
130149
131- return GetComponents < C > ( component , engineCode )
132- . FirstOrDefault ( ) ;
150+ var all = GetComponents < C > ( component , engineCode ) ;
151+ return all . FirstOrDefault ( c => c . Engine == engineCode ) ?? all . FirstOrDefault ( c => c . Engine == null ) ;
133152 }
134153
135154 /// <summary>
@@ -149,7 +168,7 @@ public AbstractClause GetOneComponent(string component, string engineCode = null
149168 }
150169
151170 /// <summary>
152- /// Return wether the query has clauses for a component.
171+ /// Return whether the query has clauses for a component.
153172 /// </summary>
154173 /// <param name="component"></param>
155174 /// <param name="engineCode"></param>
@@ -247,9 +266,9 @@ protected bool GetNot()
247266 /// <returns></returns>
248267 public Q From ( string table )
249268 {
250- return ClearComponent ( "from" ) . AddComponent ( "from" , new FromClause
269+ return AddOrReplaceComponent ( "from" , new FromClause
251270 {
252- Table = table
271+ Table = table ,
253272 } ) ;
254273 }
255274
@@ -263,15 +282,15 @@ public Q From(Query query, string alias = null)
263282 query . As ( alias ) ;
264283 } ;
265284
266- return ClearComponent ( "from" ) . AddComponent ( "from" , new QueryFromClause
285+ return AddOrReplaceComponent ( "from" , new QueryFromClause
267286 {
268287 Query = query
269288 } ) ;
270289 }
271290
272291 public Q FromRaw ( string sql , params object [ ] bindings )
273292 {
274- return ClearComponent ( "from" ) . AddComponent ( "from" , new RawFromClause
293+ return AddOrReplaceComponent ( "from" , new RawFromClause
275294 {
276295 Expression = sql ,
277296 Bindings = bindings ,
0 commit comments