public static NpgsqlParameter AddWithNullableValue(
     this NpgsqlParameterCollection collection,
     string parameterName,
     object?value)
 {
     if (value == null)
     {
         return(collection.AddWithValue(parameterName, DBNull.Value));
     }
     else
     {
         return(collection.AddWithValue(parameterName, value));
     }
 }
 public static NpgsqlParameterCollection AddSlidingExpirationInSeconds(
     this NpgsqlParameterCollection parameters,
     TimeSpan?value)
 {
     if (value.HasValue)
     {
         parameters.AddWithValue(
             Columns.Names.SlidingExpirationInSeconds, NpgsqlDbType.Double, value.Value.TotalSeconds);
     }
     else
     {
         parameters.AddWithValue(Columns.Names.SlidingExpirationInSeconds, NpgsqlDbType.Double, DBNull.Value);
     }
     return(parameters);
 }
 public static NpgsqlParameter AddSafeWithValue(
     this NpgsqlParameterCollection collection,
     string parameterName,
     object value)
 {
     return(collection.AddWithValue(parameterName, value ?? DBNull.Value));
 }
 public static NpgsqlParameterCollection AddExpiresAtTime(
     this NpgsqlParameterCollection parameters,
     DateTimeOffset?utcTime)
 {
     if (utcTime.HasValue)
     {
         parameters.AddWithValue(
             Columns.Names.ExpiresAtTime, NpgsqlDbType.Timestamp, utcTime.Value);
     }
     else
     {
         parameters.AddWithValue(
             Columns.Names.ExpiresAtTime, NpgsqlDbType.Timestamp, DBNull.Value);
     }
     return(parameters);
 }
Example #5
0
 public DbParameters PutNull(string name)
 {
     if (name == null)
     {
         name = Defaults[position++];
     }
     coll.AddWithValue(name, DBNull.Value);
     return(this);
 }
        public static NpgsqlParameterCollection AddCacheItemValue(this NpgsqlParameterCollection parameters, byte[] value)
        {
            if (value != null && value.Length < DefaultValueColumnWidth)
            {
                parameters.AddWithValue(
                    Columns.Names.CacheItemValue,
                    NpgsqlDbType.Bytea,
                    DefaultValueColumnWidth,
                    value);
            }
            else
            {
                // do not mention the size
                parameters.AddWithValue(Columns.Names.CacheItemValue, NpgsqlDbType.Bytea, value);
            }

            return(parameters);
        }
Example #7
0
 public static void AddWithJsonOrNull <T>(this NpgsqlParameterCollection parameters, string name, T value)
 {
     if (value == null)
     {
         parameters.AddWithValue(name, DBNull.Value);
     }
     else
     {
         parameters.Add(new NpgsqlParameter(name, NpgsqlDbType.Jsonb)
         {
             Value = value
         });
     }
 }
Example #8
0
        public static void AddWithValueOrNull <T>(this NpgsqlParameterCollection parameters, string name, T value)
        {
            var bullshitValue = ((object)value) ?? DBNull.Value;

            parameters.AddWithValue(name, bullshitValue);
        }
        public static void AddParameter(this NpgsqlParameterCollection sqlparam, string key, object value)
        {
            var t = value?.GetType();

            if (t == typeof(Vector3))
            {
                var v = (Vector3)value;
                sqlparam.AddWithValue(key + "X", v.X);
                sqlparam.AddWithValue(key + "Y", v.Y);
                sqlparam.AddWithValue(key + "Z", v.Z);
            }
            else if (t == typeof(GridVector))
            {
                var v = (GridVector)value;
                sqlparam.AddWithValue(key + "X", (int)v.X);
                sqlparam.AddWithValue(key + "Y", (int)v.Y);
            }
            else if (t == typeof(Quaternion))
            {
                var v = (Quaternion)value;
                sqlparam.AddWithValue(key + "X", v.X);
                sqlparam.AddWithValue(key + "Y", v.Y);
                sqlparam.AddWithValue(key + "Z", v.Z);
                sqlparam.AddWithValue(key + "W", v.W);
            }
            else if (t == typeof(Color))
            {
                var v = (Color)value;
                sqlparam.AddWithValue(key + "Red", v.R);
                sqlparam.AddWithValue(key + "Green", v.G);
                sqlparam.AddWithValue(key + "Blue", v.B);
            }
            else if (t == typeof(ColorAlpha))
            {
                var v = (ColorAlpha)value;
                sqlparam.AddWithValue(key + "Red", v.R);
                sqlparam.AddWithValue(key + "Green", v.G);
                sqlparam.AddWithValue(key + "Blue", v.B);
                sqlparam.AddWithValue(key + "Alpha", v.A);
            }
            else if (t == typeof(EnvironmentController.WLVector2))
            {
                var vec = (EnvironmentController.WLVector2)value;
                sqlparam.AddWithValue(key + "X", vec.X);
                sqlparam.AddWithValue(key + "Y", vec.Y);
            }
            else if (t == typeof(EnvironmentController.WLVector4))
            {
                var vec = (EnvironmentController.WLVector4)value;
                sqlparam.AddWithValue(key + "Red", vec.X);
                sqlparam.AddWithValue(key + "Green", vec.Y);
                sqlparam.AddWithValue(key + "Blue", vec.Z);
                sqlparam.AddWithValue(key + "Value", vec.W);
            }
            else if (t == typeof(bool))
            {
                sqlparam.AddWithValue(key, (bool)value);
            }
            else if (t == typeof(UUID))
            {
                sqlparam.AddWithValue(key, (Guid)(UUID)value);
            }
            else if (t == typeof(UGUI) || t == typeof(UGUIWithName) || t == typeof(UGI) || t == typeof(Uri) || t == typeof(UEI))
            {
                sqlparam.AddWithValue(key, value.ToString());
            }
            else if (t == typeof(ParcelID))
            {
                ParcelID parcelid = (ParcelID)value;
                UUID     id       = new UUID(parcelid.GetBytes(), 0);
                sqlparam.AddWithValue(key, (Guid)id);
            }
            else if (t == typeof(AnArray))
            {
                using (var stream = new MemoryStream())
                {
                    LlsdBinary.Serialize((AnArray)value, stream);
                    sqlparam.AddWithValue(key, stream.ToArray());
                }
            }
            else if (t == typeof(Date))
            {
                sqlparam.AddWithValue(key, ((Date)value).AsLong);
            }
            else if (t == typeof(ulong))
            {
                sqlparam.AddWithValue(key, (long)(ulong)value);
            }
            else if (t == typeof(uint))
            {
                sqlparam.AddWithValue(key, (int)(uint)value);
            }
            else if (t == typeof(ushort))
            {
                sqlparam.AddWithValue(key, (short)(ushort)value);
            }
            else if (t == typeof(byte))
            {
                sqlparam.AddWithValue(key, (short)(byte)value);
            }
            else if (t == typeof(sbyte))
            {
                sqlparam.AddWithValue(key, (short)(sbyte)value);
            }
            else if (t.IsEnum)
            {
                Type utype = t.GetEnumUnderlyingType();
                if (utype == typeof(byte) || utype == typeof(sbyte) || utype == typeof(ushort))
                {
                    utype = typeof(short);
                }
                else if (utype == typeof(uint))
                {
                    utype = typeof(int);
                }
                else if (utype == typeof(ulong))
                {
                    utype = typeof(long);
                }

                sqlparam.AddWithValue(key, Convert.ChangeType(value, utype));
            }
            else
            {
                sqlparam.AddWithValue(key, value);
            }
        }
 public static NpgsqlParameterCollection AddCacheItemId(this NpgsqlParameterCollection parameters, string value)
 {
     parameters.AddWithValue(Columns.Names.CacheItemId, NpgsqlDbType.Text, CacheItemIdColumnWidth, value);
     return(parameters);
 }