33using System . Data ;
44using System . Data . Common ;
55using System . Diagnostics ;
6+ using System . Dynamic ;
7+ using System . Linq ;
8+ using System . Linq . Expressions ;
9+ using System . Reflection ;
10+ using System . Runtime . CompilerServices ;
611using log4net ;
12+ using Microsoft . CSharp . RuntimeBinder ;
713
814namespace ConnectionFactory
915{
@@ -33,7 +39,7 @@ public void Dispose()
3339 #region ExecuteNonQuery
3440
3541 //[System.Diagnostics.DebuggerStepThrough]
36- public int ExecuteNonQuery ( CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
42+ public int ExecuteNonQuery ( CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
3743 {
3844 Logger . Debug ( "Begin Method" ) ;
3945 try
@@ -72,7 +78,7 @@ public int ExecuteNonQuery(CommandType cmdType, string cmdText, IEnumerable<CfPa
7278 /// <param name="cmdParms">Command Parameters (@parameter)</param>
7379 /// <returns>IEnumerable IDataRecord</returns>
7480 [ System . Diagnostics . DebuggerStepThrough ]
75- public IEnumerable < IDataReader > LazyLoad ( CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
81+ public IEnumerable < IDataReader > LazyLoad ( CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
7682 {
7783 Logger . InfoFormat ( "LazyLoad: {0}" , cmdText ) ;
7884 Logger . Info ( cmdParms ) ;
@@ -103,7 +109,7 @@ public IEnumerable<IDataReader> LazyLoad(CommandType cmdType, string cmdText, IE
103109 /// <returns>list of entities</returns>
104110 [ System . Diagnostics . DebuggerStepThrough ]
105111 public IEnumerable < T > LazyLoadForObjects < T > (
106- CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null ) where T : new ( )
112+ CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null ) where T : new ( )
107113 {
108114 return LazyLoadForObjects < T > ( LazyLoad ( cmdType , cmdText , cmdParms ) ) ;
109115 }
@@ -162,7 +168,7 @@ public IEnumerable<T> LazyLoadForObjects<T>(
162168 }
163169
164170 public IEnumerable < dynamic > LazyLoadForObjects (
165- CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
171+ CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
166172 {
167173 return LazyLoadForObjects ( LazyLoad ( cmdType , cmdText , cmdParms ) ) ;
168174 }
@@ -195,7 +201,7 @@ public static IEnumerable<dynamic> LazyLoadForObjects(IEnumerable<IDataReader> d
195201 /// <param name="cmdParms">Command Parameters (@parameter)</param>
196202 /// <returns>Data Reader</returns>
197203 [ System . Diagnostics . DebuggerStepThrough ]
198- public IDataReader ExecuteReader ( CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
204+ public IDataReader ExecuteReader ( CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
199205 {
200206 try
201207 {
@@ -213,6 +219,55 @@ public IDataReader ExecuteReader(CommandType cmdType, string cmdText, IEnumerabl
213219 }
214220 }
215221
222+ public IDataReader ExecuteReader ( CfCommandType cmdType , string cmdText , object cmdParms )
223+ {
224+ try
225+ {
226+ IList < CfParameter > cfParams = null ;
227+ if ( cmdParms != null )
228+ {
229+ if ( ! ( cmdParms is IEnumerable < CfParameter > ) )
230+ {
231+
232+ var props = cmdParms as ExpandoObject as IDictionary < string , object > ;
233+ if ( props != null )
234+ {
235+ cfParams = new List < CfParameter > ( props . Count ( ) ) ;
236+ foreach ( var p in props )
237+ {
238+ cfParams . Add ( new CfParameter ( p . Key , p . Value ) ) ;
239+ }
240+ }
241+ else
242+ {
243+ var properties = cmdParms . GetType ( ) . GetProperties ( ) ;
244+ if ( properties . Any ( ) )
245+ {
246+ cfParams = new List < CfParameter > ( properties . Count ( ) ) ;
247+ foreach ( var property in properties )
248+ {
249+ cfParams . Add ( new CfParameter ( property . Name , property . GetValue ( cmdParms , null ) ) ) ;
250+ }
251+ }
252+ }
253+ }
254+ }
255+
256+ _conn . EstablishFactoryConnection ( ) ;
257+ PrepareCommand ( cmdType , cmdText , cfParams ) ;
258+ return _cmd . ExecuteReader ( CommandBehavior . CloseConnection ) ;
259+ }
260+ catch ( Exception ex )
261+ {
262+ Logger . Error ( ex ) ;
263+ throw new CfException ( "Unknown Error (Connection Factory: ExecuteReader) " + ex . Message , ex ) ;
264+ }
265+ }
266+
267+
268+
269+
270+
216271 #endregion
217272
218273 #region QueryForList
@@ -227,7 +282,7 @@ public IDataReader ExecuteReader(CommandType cmdType, string cmdText, IEnumerabl
227282 /// <returns>list of entities</returns>
228283 [ System . Diagnostics . DebuggerStepThrough ]
229284 public IList < T > QueryForList < T > (
230- CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null ) where T : new ( )
285+ CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null ) where T : new ( )
231286 {
232287 return QueryForList < T > ( ExecuteReader ( cmdType , cmdText , cmdParms ) ) ;
233288 }
@@ -300,7 +355,7 @@ public IList<T> QueryForList<T>(
300355 /// <param name="cmdParms">Command Parameters (@parameter)</param>
301356 /// <returns>list of entities (ExpandoObject)</returns>
302357 public IList < dynamic > QueryForList (
303- CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
358+ CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
304359 {
305360 return QueryForList ( ExecuteReader ( cmdType , cmdText , cmdParms ) ) ;
306361 }
@@ -339,7 +394,7 @@ public static IList<dynamic> QueryForList(IDataReader dr)
339394 /// <param name="cmdParms">Command Parameters (@parameter)</param>
340395 /// <returns>Entity</returns>
341396 [ System . Diagnostics . DebuggerStepThrough ]
342- public T QueryForObject < T > ( CommandType cmdType , string cmdText ,
397+ public T QueryForObject < T > ( CfCommandType cmdType , string cmdText ,
343398 IEnumerable < CfParameter > cmdParms = null ) where T : new ( )
344399 {
345400 return QueryForObject < T > ( ExecuteReader ( cmdType , cmdText , cmdParms ) ) ;
@@ -406,7 +461,7 @@ public T QueryForObject<T>(CommandType cmdType, string cmdText,
406461 }
407462
408463 public dynamic QueryForObject (
409- CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
464+ CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
410465 {
411466 return QueryForObject ( ExecuteReader ( cmdType , cmdText , cmdParms ) ) ;
412467 }
@@ -421,7 +476,7 @@ public static dynamic QueryForObject(IDataReader dr)
421476 #region ExecuteScalar
422477
423478 [ System . Diagnostics . DebuggerStepThrough ]
424- public T ExecuteScalar < T > ( CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
479+ public T ExecuteScalar < T > ( CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
425480 {
426481 try
427482 {
@@ -453,7 +508,7 @@ public T ExecuteScalar<T>(CommandType cmdType, string cmdText, IEnumerable<CfPar
453508 }
454509 }
455510
456- public dynamic ExecuteScalar ( CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
511+ public dynamic ExecuteScalar ( CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
457512 {
458513 _conn . EstablishFactoryConnection ( ) ;
459514 PrepareCommand ( cmdType , cmdText , cmdParms ) ;
@@ -480,7 +535,7 @@ public dynamic ExecuteScalar(CommandType cmdType, string cmdText, IEnumerable<Cf
480535 /// <param name="cmdParms">Command Parameters (@parameter)</param>
481536 /// <returns>DataSet</returns>
482537 [ System . Diagnostics . DebuggerStepThrough ]
483- public DataSet DataAdapter ( CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
538+ public DataSet DataAdapter ( CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
484539 {
485540 // we use a try/catch here because if the method throws an exception we want to
486541 // close the connection throw code, because no datareader will exist, hence the
@@ -511,7 +566,7 @@ public DataSet DataAdapter(CommandType cmdType, string cmdText, IEnumerable<CfPa
511566 #region PREPARE COMMAND AND CREATE PARAMETERS (Private Methods)
512567
513568 [ System . Diagnostics . DebuggerStepThrough ]
514- private void PrepareCommand ( CommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
569+ private void PrepareCommand ( CfCommandType cmdType , string cmdText , IEnumerable < CfParameter > cmdParms = null )
515570 {
516571 _conn . EstablishFactoryConnection ( ) ;
517572
@@ -521,7 +576,7 @@ private void PrepareCommand(CommandType cmdType, string cmdText, IEnumerable<CfP
521576 _cmd = _conn . CreateDbCommand ( ) ;
522577
523578 _cmd . CommandText = cmdText ;
524- _cmd . CommandType = cmdType ;
579+ _cmd . CommandType = ( CommandType ) cmdType ;
525580
526581 CreateDbParameters ( cmdParms ) ;
527582 }
@@ -554,4 +609,12 @@ private DbParameter CreateDbParameter(string name, object value, DbType dbType =
554609 #endregion
555610
556611 }
612+
613+ public enum CfCommandType
614+ {
615+ Text = 1 ,
616+ StoredProcedure = 4 ,
617+ TableDirect = 512 ,
618+ }
619+
557620}
0 commit comments