66 using System . Linq ;
77 using QueryPolyfills ;
88
9- public partial class InMemoryAdapter : Adapter
9+ public partial class InMemoryAdapter : Adapter , IAdapterWithFunctions
1010 {
1111 private readonly Dictionary < string , string > _autoIncrementColumns = new Dictionary < string , string > ( ) ;
1212 private readonly Dictionary < string , string [ ] > _keyColumns = new Dictionary < string , string [ ] > ( ) ;
1313
1414 private readonly Dictionary < string , List < IDictionary < string , object > > > _tables =
1515 new Dictionary < string , List < IDictionary < string , object > > > ( ) ;
1616
17- private readonly ICollection < Join > _joins = new Collection < Join > ( ) ;
17+ private readonly Dictionary < string , Delegate > _functions = new Dictionary < string , Delegate > ( ) ;
18+
19+ private readonly ICollection < JoinInfo > _joins = new Collection < JoinInfo > ( ) ;
1820
1921 private List < IDictionary < string , object > > GetTable ( string tableName )
2022 {
@@ -208,12 +210,17 @@ public void SetKeyColumns(string tableName, params string[] columnNames)
208210 /// <param name="detailPropertyName">The name to give the collection property in the master object</param>
209211 public void ConfigureJoin ( string masterTableName , string masterKey , string masterPropertyName , string detailTableName , string detailKey , string detailPropertyName )
210212 {
211- var join = new Join ( masterTableName , masterKey , masterPropertyName , detailTableName , detailKey ,
213+ var join = new JoinInfo ( masterTableName , masterKey , masterPropertyName , detailTableName , detailKey ,
212214 detailPropertyName ) ;
213215 _joins . Add ( join ) ;
214216 }
215217
216- public class Join
218+ public JoinConfig Join
219+ {
220+ get { return new JoinConfig ( _joins ) ; }
221+ }
222+
223+ internal class JoinInfo
217224 {
218225 private readonly string _masterTableName ;
219226 private readonly string _masterKey ;
@@ -222,7 +229,7 @@ public class Join
222229 private readonly string _detailKey ;
223230 private readonly string _detailPropertyName ;
224231
225- public Join ( string masterTableName , string masterKey , string masterPropertyName , string detailTableName , string detailKey , string detailPropertyName )
232+ public JoinInfo ( string masterTableName , string masterKey , string masterPropertyName , string detailTableName , string detailKey , string detailPropertyName )
226233 {
227234 _masterTableName = masterTableName ;
228235 _masterKey = masterKey ;
@@ -282,5 +289,91 @@ public IDictionary<string, object> Get(string tableName, IAdapterTransaction tra
282289 {
283290 return Get ( tableName , parameterValues ) ;
284291 }
292+
293+ public class JoinConfig
294+ {
295+ private readonly ICollection < JoinInfo > _joins ;
296+ private JoinInfo _joinInfo ;
297+
298+ internal JoinConfig ( ICollection < JoinInfo > joins )
299+ {
300+ _joins = joins ;
301+ _joinInfo = new JoinInfo ( null , null , null , null , null , null ) ;
302+ }
303+
304+ public JoinConfig Master ( string tableName , string keyName , string propertyNameInDetailRecords = null )
305+ {
306+ if ( _joins . Contains ( _joinInfo ) ) _joins . Remove ( _joinInfo ) ;
307+ _joinInfo = new JoinInfo ( tableName , keyName , propertyNameInDetailRecords ?? tableName , _joinInfo . DetailTableName ,
308+ _joinInfo . DetailKey , _joinInfo . DetailPropertyName ) ;
309+ _joins . Add ( _joinInfo ) ;
310+ return this ;
311+ }
312+
313+
314+ public JoinConfig Detail ( string tableName , string keyName , string propertyNameInMasterRecords = null )
315+ {
316+ if ( _joins . Contains ( _joinInfo ) ) _joins . Remove ( _joinInfo ) ;
317+ _joinInfo = new JoinInfo ( _joinInfo . MasterTableName , _joinInfo . MasterKey , _joinInfo . MasterPropertyName ,
318+ tableName , keyName ,
319+ propertyNameInMasterRecords ?? tableName ) ;
320+ _joins . Add ( _joinInfo ) ;
321+ return this ;
322+ }
323+ }
324+
325+ public void AddFunction < TResult > ( string functionName , Func < TResult > function )
326+ {
327+ _functions . Add ( functionName , function ) ;
328+ }
329+
330+ public void AddFunction < T , TResult > ( string functionName , Func < T , TResult > function )
331+ {
332+ _functions . Add ( functionName , function ) ;
333+ }
334+
335+ public void AddFunction < T1 , T2 , TResult > ( string functionName , Func < T1 , T2 , TResult > function )
336+ {
337+ _functions . Add ( functionName , function ) ;
338+ }
339+
340+ public void AddFunction < T1 , T2 , T3 , TResult > ( string functionName , Func < T1 , T2 , T3 , TResult > function )
341+ {
342+ _functions . Add ( functionName , function ) ;
343+ }
344+
345+ public void AddFunction < T1 , T2 , T3 , T4 , TResult > ( string functionName , Func < T1 , T2 , T3 , T4 , TResult > function )
346+ {
347+ _functions . Add ( functionName , function ) ;
348+ }
349+
350+ public void AddDelegate ( string functionName , Delegate function )
351+ {
352+ _functions . Add ( functionName , function ) ;
353+ }
354+
355+ public bool IsValidFunction ( string functionName )
356+ {
357+ return _functions . ContainsKey ( functionName ) ;
358+ }
359+
360+ public IEnumerable < IEnumerable < IEnumerable < KeyValuePair < string , object > > > > Execute ( string functionName , IDictionary < string , object > parameters )
361+ {
362+ if ( ! _functions . ContainsKey ( functionName ) ) throw new InvalidOperationException ( "No function found with that name." ) ;
363+ var obj = _functions [ functionName ] . DynamicInvoke ( parameters . Values . ToArray ( ) ) ;
364+
365+ var dict = obj as IDictionary < string , object > ;
366+ if ( dict != null ) return new List < IEnumerable < IDictionary < string , object > > > { new List < IDictionary < string , object > > { dict } } ;
367+
368+ var list = obj as IEnumerable < IDictionary < string , object > > ;
369+ if ( list != null ) return new List < IEnumerable < IDictionary < string , object > > > { list } ;
370+
371+ return obj as IEnumerable < IEnumerable < IDictionary < string , object > > > ;
372+ }
373+
374+ public IEnumerable < IEnumerable < IEnumerable < KeyValuePair < string , object > > > > Execute ( string functionName , IDictionary < string , object > parameters , IAdapterTransaction transaction )
375+ {
376+ return Execute ( functionName , parameters ) ;
377+ }
285378 }
286379}
0 commit comments