Example #1
0
        public void Range()
        {
            var cmd = new NpgsqlCommand("SELECT @p1, @p2, @p3, @p4", Conn);
            var p1  = new NpgsqlParameter("p1", NpgsqlDbType.Range | NpgsqlDbType.Integer)
            {
                Value = NpgsqlRange <int> .Empty()
            };
            var p2 = new NpgsqlParameter {
                ParameterName = "p2", Value = new NpgsqlRange <int>(1, 10)
            };
            var p3 = new NpgsqlParameter {
                ParameterName = "p3", Value = new NpgsqlRange <int>(1, false, 10, false)
            };
            var p4 = new NpgsqlParameter {
                ParameterName = "p4", Value = new NpgsqlRange <int>(0, false, true, 10, false, false)
            };

            Assert.That(p2.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Range | NpgsqlDbType.Integer));
            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);
            cmd.Parameters.Add(p3);
            cmd.Parameters.Add(p4);
            var reader = cmd.ExecuteReader();

            reader.Read();

            Assert.That(reader[0].ToString(), Is.EqualTo("empty"));
            Assert.That(reader[1].ToString(), Is.EqualTo("[1,11)"));
            Assert.That(reader[2].ToString(), Is.EqualTo("[2,10)"));
            Assert.That(reader[3].ToString(), Is.EqualTo("(,10)"));

            reader.Dispose();
            cmd.Dispose();
        }
Example #2
0
        public static NpgsqlRange <T> ParseNpgsqlRange <T>(string s)
        {
            if (string.IsNullOrEmpty(s) || s == "empty")
            {
                return(NpgsqlRange <T> .Empty());
            }
            string s1 = s.Trim('(', ')', '[', ']');

            string[] ss = s1.Split(new char[] { ',' }, 2);
            if (ss.Length != 2)
            {
                return(NpgsqlRange <T> .Empty());
            }
            T t1 = default(T);
            T t2 = default(T);

            if (!string.IsNullOrEmpty(ss[0]))
            {
                t1 = (T)Convert.ChangeType(ss[0], typeof(T));
            }
            if (!string.IsNullOrEmpty(ss[1]))
            {
                t2 = (T)Convert.ChangeType(ss[1], typeof(T));
            }
            return(new NpgsqlRange <T>(t1, s[0] == '[', s[0] == '(', t2, s[s.Length - 1] == ']', s[s.Length - 1] == ')'));
        }