// Summary: // Adds a System.Data.Common.DbParameter item with the specified value to the // System.Data.Common.DbParameterCollection. // // Parameters: // value: // The System.Data.Common.DbParameter.Value of the System.Data.Common.DbParameter // to add to the collection. // // Returns: // The index of the System.Data.Common.DbParameter object in the collection. public override int Add(object value) { #if CODECONTRACTS Contract.Ensures(Contract.Result <int>() >= -1); #endif PqsqlParameter p = value as PqsqlParameter; if (p == null) { return(-1); } int i; string s = p.ParameterName; #if CODECONTRACTS Contract.Assume(s != null); #endif if (!mLookup.TryGetValue(s, out i)) { mParamList.Add(p); i = mParamList.Count - 1; mLookup.Add(s, i); } return(i < -1 ? -1 : i); }
// // Summary: // Removes the System.Data.Common.DbParameter object with the specified name // from the collection. // // Parameters: // parameterName: // The name of the System.Data.Common.DbParameter object to remove. public override void RemoveAt(string parameterName) { #if CODECONTRACTS Contract.Assume(parameterName != null); #endif if (!string.IsNullOrEmpty(parameterName)) { int ret; string canonical = PqsqlParameter.CanonicalParameterName(parameterName); if (mLookup.TryGetValue(canonical, out ret) && ret != -1) { mParamList.RemoveAt(ret); mLookup.Remove(canonical); // update lookup index foreach (KeyValuePair <string, int> kv in mLookup.Where(kv => kv.Value > ret).ToArray()) { mLookup[kv.Key] = kv.Value - 1; } return; } } throw new KeyNotFoundException("Could not find parameter name " + parameterName); }
// // Summary: // Specifies whether all column values in an update statement are included or // only changed ones. // // Returns: // true if the UPDATE statement generated by the System.Data.Common.DbCommandBuilder // includes all columns; false if it includes only changed columns. //[DefaultValue(false)] //public bool SetAllValues //{ // get; // set; //} // Summary: // Allows the provider implementation of the System.Data.Common.DbCommandBuilder // class to handle additional parameter properties. // // Parameters: // parameter: // A System.Data.Common.DbParameter to which the additional modifications are // applied. // // row: // The System.Data.DataRow from the schema table provided by System.Data.Common.DbDataReader.GetSchemaTable(). // // statementType: // The type of command being generated; INSERT, UPDATE or DELETE. // // whereClause: // true if the parameter is part of the update or delete WHERE clause, false // if it is part of the insert or update values. protected override void ApplyParameterInfo(DbParameter p, DataRow row, StatementType statementType, bool whereClause) { PqsqlParameter pqp = p as PqsqlParameter; if (pqp == null) { throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, "{0} is not a {1}", nameof(p), nameof(PqsqlParameter))); } if (row == null) { throw new ArgumentNullException(nameof(row)); } if (pqp.SourceColumnNullMapping) { pqp.SourceColumn = ""; } else { object o = row[PqsqlSchemaTableColumn.TypeOid]; #if CODECONTRACTS Contract.Assume(o != null); #endif pqp.PqsqlDbType = (PqsqlDbType)o; } }
// // Summary: // Returns the index of the System.Data.Common.DbParameter object with the specified // name. // // Parameters: // parameterName: // The name of the System.Data.Common.DbParameter object in the collection. // // Returns: // The index of the System.Data.Common.DbParameter object with the specified // name. public override int IndexOf(string parameterName) { #if CODECONTRACTS Contract.Ensures(Contract.Result <int>() >= -1); Contract.Ensures(Contract.Result <int>() < Count); #endif int ret; if (!string.IsNullOrEmpty(parameterName) && mLookup.TryGetValue(PqsqlParameter.CanonicalParameterName(parameterName), out ret)) { return(ret < -1 ? -1 : ret); } return(-1); }
// // Summary: // Inserts the specified index of the System.Data.Common.DbParameter object // with the specified name into the collection at the specified index. // // Parameters: // index: // The index at which to insert the System.Data.Common.DbParameter object. // // value: // The System.Data.Common.DbParameter object to insert into the collection. public override void Insert(int index, object value) { #if CODECONTRACTS Contract.Requires <ArgumentNullException>(value != null); #else if (value == null) { throw new ArgumentNullException(nameof(value)); } #endif #if CODECONTRACTS Contract.Assert(index >= 0); Contract.Assume(index < mParamList.Count); #endif PqsqlParameter val = value as PqsqlParameter; if (val == null) { throw new InvalidCastException(nameof(value) + " is not a PqsqlParameter"); } string newParamName = val.ParameterName; if (newParamName == null) { throw new ArgumentNullException(nameof(value), "ParameterName is null"); } if (mLookup.ContainsKey(newParamName)) { throw new DuplicateNameException("A key with name " + newParamName + " already exists in the collection"); } mParamList.Insert(index, val); // update lookup index foreach (KeyValuePair <string, int> kv in mLookup.Where(kv => kv.Value >= index).ToArray()) { mLookup[kv.Key] = kv.Value + 1; } mLookup.Add(newParamName, index); }
// add PqsqlParameter with value to PqsqlParameterCollection without known DbType public PqsqlParameter AddWithValue(string parameterName, object value) { if (string.IsNullOrEmpty(parameterName)) { throw new ArgumentOutOfRangeException(nameof(parameterName), "parameter name is empty or null"); } int i = IndexOf(parameterName); PqsqlParameter p; if (i >= 0) { // re-use PqsqlParameter p = this[i]; // reset all properties p.ResetDbType(); p.SourceColumn = string.Empty; p.Size = 0; p.Direction = ParameterDirection.Input; p.SourceVersion = DataRowVersion.Current; p.IsNullable = false; // set name and value p.ParameterName = parameterName; p.Value = value; } else { // fresh PqsqlParameter with name and value p = new PqsqlParameter { ParameterName = parameterName, Value = value }; mParamList.Add(p); i = mParamList.Count - 1; mLookup.Add(p.ParameterName, i); } return(p); }
// // Summary: // Removes the System.Data.Common.DbParameter object at the specified from the // collection. // // Parameters: // index: // The index where the System.Data.Common.DbParameter object is located. public override void RemoveAt(int index) { #if CODECONTRACTS Contract.Assert(index >= 0); Contract.Assume(index < mParamList.Count); #endif PqsqlParameter old = mParamList[index]; if (old != null) { mParamList.RemoveAt(index); mLookup.Remove(old.ParameterName); // update lookup index foreach (KeyValuePair <string, int> kv in mLookup.Where(kv => kv.Value > index).ToArray()) { mLookup[kv.Key] = kv.Value - 1; } } }
// Summary: // Gets and sets the System.Data.Common.DbParameter at the specified index. // // Parameters: // index: // The zero-based index of the parameter. // // Returns: // The System.Data.Common.DbParameter at the specified index. // // Exceptions: // System.IndexOutOfRangeException: // The specified index does not exist. public new PqsqlParameter this[int index] { get { #if CODECONTRACTS Contract.Requires <IndexOutOfRangeException>(index >= 0); #else if (index < 0) { throw new ArgumentOutOfRangeException(nameof(index)); } #endif #if CODECONTRACTS Contract.Assume(index < mParamList.Count); #endif return(mParamList[index]); } set { #if CODECONTRACTS Contract.Requires <IndexOutOfRangeException>(index >= 0); #else if (index < 0) { throw new ArgumentOutOfRangeException(nameof(index)); } #endif #if CODECONTRACTS Contract.Assume(index < mParamList.Count); #endif if (value == null) { throw new ArgumentNullException(nameof(value)); } PqsqlParameter old = mParamList[index]; if (old == value) { return; } if (string.IsNullOrEmpty(value.ParameterName)) { throw new ArgumentOutOfRangeException(nameof(value)); } mParamList[index] = value; mLookup.Remove(old.ParameterName); #if CODECONTRACTS Contract.Assert(value.ParameterName != null); #endif mLookup.Add(value.ParameterName, index); } }
/// <summary> /// append parameter to parameter buffer. /// we convert and infer the right datatype in case the user supplied inconsistent type information. /// </summary> public void AddParameter(PqsqlParameter parameter) { if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } ParameterDirection direction = parameter.Direction; // skip output parameters and return values if (direction == ParameterDirection.Output || direction == ParameterDirection.ReturnValue) { return; } PqsqlDbType oid = parameter.PqsqlDbType; object v = parameter.Value; bool vNotNull = v != null && v != DBNull.Value; TypeCode vtc = Convert.GetTypeCode(v); // no PqsqlDbType set by the user, try to infer datatype from Value and set new oid // if v is null or DBNull.Value, we can work with PqsqlDbType.Unknown if (oid == PqsqlDbType.Unknown && vNotNull) { if (vtc != TypeCode.Object) { oid = InferValueType(vtc); } else if (v is DateTimeOffset) { oid = PqsqlDbType.TimestampTZ; } else if (v is byte[]) { oid = PqsqlDbType.Bytea; } else if (v is Guid) { oid = PqsqlDbType.Uuid; } else if (v is TimeSpan) { oid = PqsqlDbType.Interval; } if (oid == PqsqlDbType.Unknown) // cannot resolve oid for non-null v { throw new PqsqlException(string.Format(CultureInfo.InvariantCulture, "Could not infer datatype for PqsqlParameter {0} (TypeCode={1})", parameter.ParameterName, vtc)); } } // get SetValue / SetArrayItem delegates for oid PqsqlTypeRegistry.PqsqlTypeParameter tp = PqsqlTypeRegistry.Get(oid & ~PqsqlDbType.Array); if (tp == null) { // do not try to fetch datatype specs with PqsqlTypeRegistry.FetchType() here, just bail out throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Datatype {0} is not supported", oid & ~PqsqlDbType.Array)); } // try to convert to the proper datatype in case the user supplied a wrong PqsqlDbType // if v is null or DBNull.Value, we can work with PqsqlDbType.Unknown if (vNotNull && (oid & PqsqlDbType.Array) != PqsqlDbType.Array) { TypeCode tc = tp.TypeCode; if (vtc != TypeCode.Empty && vtc != tc) { v = ConvertParameterValue(v, vtc, tc, oid); } } // add parameter to the parameter buffer AddParameterValue(tp, oid, v); }