Esempio n. 1
0
 /// <summary>
 /// Method needed to re-create parameters on each new execution of
 /// the query cause
 /// </summary>
 private void ResetParameters()
 {
     foreach (var entryKey in ParametersMap.Keys)
     {
         QueryParameterEntry entry = (QueryParameterEntry)ParametersMap[entryKey];
         // create a new SQL parameter
         SqlParameter newDbParameter = CreateParameterFromEntry(entry);
         // set the new parameter
         entry.Parameter = newDbParameter;
     }
 }
Esempio n. 2
0
        private void InsertParametersIntoDbCommand(SqlCommand dbCommand)
        {
            // clear any previous parameters
            dbCommand.Parameters.Clear();

            // take all parameters and insert them into DB command
            foreach (var parameterKey in ParametersMap.Keys)
            {
                QueryParameterEntry entry = (QueryParameterEntry)ParametersMap[parameterKey];
                dbCommand.Parameters.Add(entry.Parameter);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Check if the parameter is set in a query
        /// </summary>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public bool IsParameterSet(string paramName)
        {
            if (string.IsNullOrEmpty(paramName))
            {
                throw new ArgumentException("Parameter name is not defined!", "paramName");
            }

            QueryParameterEntry entry = (QueryParameterEntry)ParametersMap[paramName];

            return((entry != null) &&
                   (entry.Parameter != null) &&
                   string.Equals(entry.Parameter.ParameterName, paramName));
        }
Esempio n. 4
0
        private SqlParameter CreateParameterFromEntry(QueryParameterEntry entry)
        {
            SqlParameter newDbParameter = DbContext.CreateParameter();

            newDbParameter.ParameterName = entry.Config.ParameterName;
            newDbParameter.SqlDbType     = entry.Config.DbType;
            newDbParameter.IsNullable    = entry.Config.IsNullable;
            newDbParameter.Size          = entry.Config.Size;
            newDbParameter.Direction     = entry.Config.Direction;
            newDbParameter.Value         = entry.Parameter.Value;

            return(newDbParameter);
        }
Esempio n. 5
0
        protected bool RemoveParameter(string paramName)
        {
            if (string.IsNullOrEmpty(paramName))
            {
                throw new ArgumentException("Parameter name is not defined!", "paramName");
            }
            QueryParameterEntry entry = (QueryParameterEntry)ParametersMap[paramName];

            if (entry == null)
            {
                return(false);
            }

            ParametersMap.Remove(paramName);
            return(true);
        }
Esempio n. 6
0
        private string UnParameterizeSqlQuery(string sql)
        {
            foreach (var parameterKey in ParametersMap.Keys)
            {
                // take all entries
                QueryParameterEntry entry = (QueryParameterEntry)ParametersMap[parameterKey];

                // in case the parameter is set with NoQuotes
                bool noQuotes = (entry.Parameter.Value is string) && entry.Config.NoQuotes;

                //
                sql = sql.Replace(entry.Parameter.ParameterName, ValueToString(entry.Parameter.Value, noQuotes));
            }

            return(sql);
        }
Esempio n. 7
0
        protected virtual TParamType GetParameter <TParamType>(string paramName)
        {
            if (string.IsNullOrEmpty(paramName))
            {
                throw new ArgumentException("Parameter name is not defined!", "paramName");
            }

            QueryParameterEntry entry = (QueryParameterEntry)ParametersMap[paramName];

            if (entry == null)
            {
                // parameter entry was not found by parameter name
                string errorMsg = string.Format("Parameter [{0}] was not found within query parameters!", paramName);
                throw new InvalidOperationException(errorMsg);
            }

            object parameterValue = entry.Parameter.Value;

            return(((parameterValue != null) && !DBNull.Value.Equals(parameterValue))
                ? (TParamType)parameterValue
                : default(TParamType));
        }
Esempio n. 8
0
        protected virtual void SetParameter(string paramName, object paramValue, QueryParameterConfig config)
        {
            if (string.IsNullOrEmpty(paramName))
            {
                throw new ArgumentNullException("paramName");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config", "Parameter configuration is not defined!");
            }



            // get a parameter from the parameters hashmap
            QueryParameterEntry entry = (QueryParameterEntry)ParametersMap[paramName];

            if ((entry != null) && (entry.Config != null) && entry.Config.Equals(config))
            {
                // entry is found for parameter
                // just create a new DB parameter
                // leave the parameter config untouched if it did not change
                if (ExecutedTimes > 0)
                {
                    SqlParameter newDbParameter = config.IsStoredProcedureParameter
                        ? DbContext.CreateStoredProcParameter(
                        paramName: paramName, paramValue: paramValue,
                        direction: config.Direction, sqlDbType: config.DbType,
                        isNullable: config.IsNullable, size: config.Size)
                        : DbContext.CreateParameter(paramName, paramValue);

                    // reset parameter with a one with updated values
                    entry.Parameter = newDbParameter;
                    return;
                }

                // just reset the parameter value
                // without re-creating parameter
                entry.Parameter.Value = paramValue ?? DBNull.Value;
            }
            else
            {
                SqlParameter newDbParameter = config.IsStoredProcedureParameter
                    ? DbContext.CreateStoredProcParameter(
                    paramName: paramName,
                    paramValue: paramValue,
                    direction: config.Direction,
                    sqlDbType: config.DbType,
                    isNullable: config.IsNullable,
                    size: config.Size)
                    : DbContext.CreateParameter(paramName, paramValue);

                // create a new parameter entry, contating the parameter itself and its configuration
                QueryParameterEntry newEntry = new QueryParameterEntry
                {
                    Parameter = newDbParameter,
                    Config    = config
                };

                // save entry in the parameters map
                ParametersMap[paramName] = newEntry;
            }
        }