Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the new 9.4 jsonb type support

  • Loading branch information...
commit bfa1e6d3377a5ad8fd01e738fc4dbde7b9099148 1 parent 22e4fdb
@roji roji authored
View
3  Npgsql/NpgsqlTypes/NpgsqlDbType.cs
@@ -74,7 +74,8 @@ public enum NpgsqlDbType
Abstime,
MacAddr,
Json,
- Hstore
+ Jsonb,
+ Hstore,
}
}
View
8 Npgsql/NpgsqlTypes/NpgsqlTypesHelper.cs
@@ -453,6 +453,10 @@ private static NpgsqlNativeTypeMapping PrepareDefaultTypesMap()
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
+ nativeTypeMapping.AddType("jsonb", NpgsqlDbType.Jsonb, DbType.Object, false,
+ BasicNativeToBackendTypeConverter.StringToTextText,
+ BasicNativeToBackendTypeConverter.StringToTextBinary);
+
nativeTypeMapping.AddType("hstore", NpgsqlDbType.Hstore, DbType.Object, false,
BasicNativeToBackendTypeConverter.StringToTextText,
BasicNativeToBackendTypeConverter.StringToTextBinary);
@@ -582,6 +586,10 @@ yield return
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
+ yield return new NpgsqlBackendTypeInfo(0, "jsonb", NpgsqlDbType.Jsonb, DbType.Object, typeof(String),
+ null,
+ BasicBackendToNativeTypeConverter.TextBinaryToString);
+
yield return new NpgsqlBackendTypeInfo(0, "hstore", NpgsqlDbType.Hstore, DbType.Object, typeof(String),
null,
BasicBackendToNativeTypeConverter.TextBinaryToString);
View
30 tests/CommandTests.cs
@@ -3888,29 +3888,33 @@ public void TestIEnumerableAsArray()
[Test]
public void InsertJsonValueDataType()
{
- if (Conn.PostgreSqlVersion < new Version(9, 2, 0))
+ if (Conn.PostgreSqlVersion < new Version(9, 2))
Assert.Ignore("json data type only introduced in 9.2");
-
- var jsonValue = @"
- {
- ""DisplayFieldName"" : ""ObjectName""
- }
- ";
- using (var cmd = new NpgsqlCommand("insert into data (field_json) values (:paramJson)", Conn))
+ using (var cmd = new NpgsqlCommand("INSERT INTO data (field_json) VALUES (:param)", Conn))
{
- cmd.Parameters.AddWithValue("paramJson", jsonValue);
+ cmd.Parameters.AddWithValue("param", @"{ ""Key"" : ""Value"" }");
cmd.Parameters[0].NpgsqlDbType = NpgsqlDbType.Json;
+ Assert.That(cmd.ExecuteNonQuery(), Is.EqualTo(1));
+ }
+ }
- var result = cmd.ExecuteNonQuery();
-
- Assert.AreEqual(1, result);
+ [Test]
+ public void InsertJsonbValueDataType()
+ {
+ if (Conn.PostgreSqlVersion < new Version(9, 4))
+ Assert.Ignore("json data type only introduced in 9.4 (we're on {0})", Conn.PostgreSqlVersion);
+ using (var cmd = new NpgsqlCommand("INSERT INTO data (field_jsonb) VALUES (:param)", Conn))
+ {
+ cmd.Parameters.AddWithValue("param", @"{ ""Key"" : ""Value"" }");
+ cmd.Parameters[0].NpgsqlDbType = NpgsqlDbType.Jsonb;
+ Assert.That(cmd.ExecuteNonQuery(), Is.EqualTo(1));
}
}
[Test]
public void InsertHstoreValueDataType()
{
- if (Conn.PostgreSqlVersion < new Version(9, 1, 0))
+ if (Conn.PostgreSqlVersion < new Version(9, 1))
Assert.Ignore("Loading the hstore extension in pre-9.1 is too complicated");
using (var cmd = new NpgsqlCommand("INSERT INTO data (field_hstore) VALUES (:param)", Conn))
{
View
9 tests/TestBase.cs
@@ -236,17 +236,22 @@ private void CreateSchema()
field_circle CIRCLE
) WITH OIDS");
- if (Conn.PostgreSqlVersion >= new Version(9, 1, 0))
+ if (Conn.PostgreSqlVersion >= new Version(9, 1))
{
ExecuteNonQuery(@"CREATE EXTENSION IF NOT EXISTS hstore");
ExecuteNonQuery(@"ALTER TABLE data ADD COLUMN field_hstore HSTORE");
}
- if (Conn.PostgreSqlVersion >= new Version(9, 2, 0))
+ if (Conn.PostgreSqlVersion >= new Version(9, 2))
{
ExecuteNonQuery(@"ALTER TABLE data ADD COLUMN field_json JSON");
}
+ if (Conn.PostgreSqlVersion >= new Version(9, 4))
+ {
+ ExecuteNonQuery(@"ALTER TABLE data ADD COLUMN field_jsonb JSONB");
+ }
+
_schemaCreated = true;
}
Please sign in to comment.
Something went wrong with that request. Please try again.