protected AStoredProc( AConnection oDB, ASafeLog oLog = null, CommandSpecies nSpecies = CommandSpecies.StoredProcedure ) : base(oDB, oLog, nSpecies) { } // constructor
protected virtual DbCommand BuildCommand(string spName, CommandSpecies nSpecies, params QueryParameter[] aryParams) { DbCommand command = CreateCommand(spName); switch (nSpecies) { case CommandSpecies.Auto: command.CommandType = aryParams.Length == 0 ? CommandType.Text : CommandType.StoredProcedure; break; case CommandSpecies.StoredProcedure: command.CommandType = CommandType.StoredProcedure; break; case CommandSpecies.Text: command.CommandType = CommandType.Text; break; case CommandSpecies.TableDirect: command.CommandType = CommandType.TableDirect; break; default: throw new ArgumentOutOfRangeException("nSpecies"); } // switch command.CommandTimeout = CommandTimeout; foreach (var prm in aryParams) { AppendParameter(command, prm); } return(command); } // BuildCommand
} // ForEachRow public virtual void ForEachRow(Action <DbDataReader> oAction, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { if (ReferenceEquals(oAction, null)) { throw new DbException("Callback action not specified in 'ForEachRow' call."); } ForEachRow(null, oAction, sQuery, nSpecies, aryParams); } // ForEachRow
} // ForEachResult public virtual void ForEachResult <T>(Action <T> oAction, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) where T : IResultRow, new() { if (ReferenceEquals(oAction, null)) { throw new DbException("Callback action not specified in 'ForEachResult' call."); } ForEachResult(null, oAction, sQuery, nSpecies, aryParams); } // ForEachResult
} // PublishRunningTime protected virtual object Run( ConnectionWrapper cw, ExecMode nMode, CommandSpecies nSpecies, string spName, params QueryParameter[] aryParams ) { return(Run(cw, null, nMode, nSpecies, spName, aryParams)); } // Run
} // ToString protected AStoredProcedure( AConnection oDB, ASafeLog oLog = null, CommandSpecies nSpecies = CommandSpecies.StoredProcedure ) { m_aryArgs = null; Log = oLog.Safe(); Species = nSpecies; CheckDirection(); DB = oDB; } // constructor
} // Fill public List <T> Fill <T>(ConnectionWrapper oConnectionToUse, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) where T : new() { var oResult = new List <T>(); ForEachRowSafe( oConnectionToUse, (sr, bRowsetStart) => { oResult.Add(sr.Fill <T>()); return(ActionResult.Continue); }, sQuery, nSpecies, aryParams ); return(oResult); } // Fill
} // ForEachResult public virtual void ForEachResult <T>(ConnectionWrapper oConnectionToUse, Action <T> oAction, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) where T : IResultRow, new() { if (ReferenceEquals(oAction, null)) { throw new DbException("Callback action not specified in 'ForEachResult' call."); } Func <T, ActionResult> oFunc = r => { oAction(r); return(ActionResult.Continue); }; ForEachResult(oConnectionToUse, oFunc, sQuery, nSpecies, aryParams); } // ForEachResult
} // ExecuteScalar public virtual T ExecuteScalar <T>(string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { return(ExecuteScalar <T>(null, sQuery, nSpecies, aryParams)); } // ExecuteScalar
} // ForEachRow public virtual void ForEachRow(ConnectionWrapper oConnectionToUse, Func <DbDataReader, bool, ActionResult> oAction, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { if (ReferenceEquals(oAction, null)) { throw new DbException("Callback action not specified in 'ForEachRow' call."); } Run(oConnectionToUse, oAction, ExecMode.ForEachRow, nSpecies, sQuery, aryParams); } // ForEachRow
} // ForEachRow public virtual void ForEachRow(ConnectionWrapper oConnectionToUse, Action <DbDataReader> oAction, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { if (ReferenceEquals(oAction, null)) { throw new DbException("Callback action not specified in 'ForEachRow' call."); } Func <DbDataReader, bool, ActionResult> oFunc = (r, bRowsetStart) => { oAction(r); return(ActionResult.Continue); }; ForEachRow(oConnectionToUse, oFunc, sQuery, nSpecies, aryParams); } // ForEachRow
} // ExecuteScalar public virtual T ExecuteScalar <T>(ConnectionWrapper oConnectionToUse, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { object oRes = Run(oConnectionToUse, ExecMode.Scalar, nSpecies, sQuery, aryParams); if ((oRes == null) || (oRes is DBNull)) { return(default(T)); } return((T)oRes); } // ExecuteScalar
} // ExecuteEnumerable public virtual IEnumerable <SafeReader> ExecuteEnumerable(ConnectionWrapper oConnectionToUse, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { return((IEnumerable <SafeReader>)Run(oConnectionToUse, null, ExecMode.Enumerable, nSpecies, sQuery, aryParams)); } // ExecuteEnumerable
} // FillFirst public virtual T FillFirst <T>(ConnectionWrapper oConnectionToUse, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) where T : new() { var oResult = new T(); ForEachRowSafe( oConnectionToUse, (sr, bRowsetStart) => { sr.Fill(oResult); return(ActionResult.SkipAll); }, sQuery, nSpecies, aryParams ); return(oResult); } // FillFirst
} // Run protected virtual object Run( ConnectionWrapper cw, Func <DbDataReader, bool, ActionResult> oAction, ExecMode nMode, CommandSpecies nSpecies, string spName, params QueryParameter[] aryParams ) { if ((nMode == ExecMode.ForEachRow) && ReferenceEquals(oAction, null)) { throw new DbException("Callback action not specified in 'ForEachRow' call."); } var oArgsForLog = new StringBuilder(); LogVerbosityLevel nLogVerbosityLevel = LogVerbosityLevel; foreach (QueryParameter prm in aryParams) { oArgsForLog.Append(oArgsForLog.Length > 0 ? ", " : string.Empty).Append(prm); } string sArgsForLog = "(" + oArgsForLog + ")"; Guid guid = Guid.NewGuid(); if (nLogVerbosityLevel == LogVerbosityLevel.Verbose) { Log.Debug("Starting to run query:\n\tid = {0}\n\t{1}{2}", guid, spName, sArgsForLog); } SqlRetryer oRetryer = CreateRetryer(); oRetryer.LogVerbosityLevel = this.LogVerbosityLevel; DbCommand oCmdToDispose = null; try { DbCommand cmd = BuildCommand(spName, nSpecies, aryParams); oCmdToDispose = cmd; object oResult = null; oRetryer.Retry(() => oResult = RunOnce(cw, oAction, nMode, cmd, nLogVerbosityLevel, spName, sArgsForLog, guid) ); // Retry return(oResult); } catch (Exception e) { if (nLogVerbosityLevel == LogVerbosityLevel.Verbose) { Log.Error(e, "Error while executing query {0}", guid); } else { Log.Error(e, "Error while executing query:\n\tid = {0}\n\t{1}{2}", guid, spName, sArgsForLog); } throw; } finally { if (oCmdToDispose != null) { oCmdToDispose.Dispose(); if (nLogVerbosityLevel == LogVerbosityLevel.Verbose) { Log.Debug("Command has been disposed."); } } // if } // try } // Run
} // ExecuteNonQuery public virtual int ExecuteNonQuery(ConnectionWrapper oConnectionToUse, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { return((int)Run(oConnectionToUse, ExecMode.NonQuery, nSpecies, sQuery, aryParams)); } // ExecuteNonQuery
} // GetFirst public virtual SafeReader GetFirst(ConnectionWrapper oConnectionToUse, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { SafeReader oResult = null; ForEachRowSafe( oConnectionToUse, (sr, bRowsetStart) => { oResult = sr.ToCache(); return(ActionResult.SkipAll); }, sQuery, nSpecies, aryParams ); return(oResult ?? SafeReader.CreateEmpty()); } // GetFirst
} // GetFirst public virtual SafeReader GetFirst(string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { return(GetFirst(null, sQuery, nSpecies, aryParams)); } // GetFirst
} // FillFirst public virtual void FillFirst <T>(ConnectionWrapper oConnectionToUse, T oInstance, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { if (!typeof(T).IsValueType) { // Plain comparison "oInstance == null" fires warning "possible compare of value type with null". // Assignment to temp variable is a workaround to suppress the warning. // And if we are already here then T is not a value type so it can be null. object obj = oInstance; if (obj == null) { throw new NullReferenceException("Cannot FillFirst of type " + typeof(T) + ": no instance specified."); } } // if ForEachRowSafe( oConnectionToUse, (sr, bRowsetStart) => { sr.Fill(oInstance); return(ActionResult.SkipAll); }, sQuery, nSpecies, aryParams ); } // FillFirst
} // FillFirst public virtual void FillFirst <T>(T oInstance, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { FillFirst <T>(null, oInstance, sQuery, nSpecies, aryParams); } // FillFirst
} // ForEachResult public virtual void ForEachResult <T>(ConnectionWrapper oConnectionToUse, Func <T, ActionResult> oAction, string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) where T : IResultRow, new() { if (ReferenceEquals(oAction, null)) { throw new DbException("Callback action not specified in 'ForEachResult' call."); } ForEachRowSafe( oConnectionToUse, (sr, bRowsetStart) => { var oResult = new T(); oResult.SetIsFirst(bRowsetStart); sr.Fill(oResult); return(oAction(oResult)); }, sQuery, nSpecies, aryParams ); } // ForEachResult
} // ExecuteEnumerable public virtual IEnumerable <SafeReader> ExecuteEnumerable(string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { return(ExecuteEnumerable(null, sQuery, nSpecies, aryParams)); } // ExecuteEnumerable
} // FillFirst public virtual T FillFirst <T>(string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) where T : new() { return(FillFirst <T>(null, sQuery, nSpecies, aryParams)); } // FillFirst
public void SetUp() { DatabaseContext = SetupInMemoryDatabase(); DatabaseContext.Database.EnsureCreated(); _commands = new CommandSpecies(DatabaseContext, Mapper); }
} // ExecuteNonQuery public virtual int ExecuteNonQuery(string sQuery, CommandSpecies nSpecies, params QueryParameter[] aryParams) { return(ExecuteNonQuery(null, sQuery, nSpecies, aryParams)); } // ExecuteNonQuery