Skip to content

Support Hstore and Jsonb #242

Closed
Closed

Description

@namtih58

Add surport for hstore and jsonb please.

Activity

  1. franciscojunior commented on May 25, 2014

    @franciscojunior
    Member

    I already have a pull request which adds support for json type: #168 . Did you try that yet? I'm asking that because initial jsonb support would be almost the same code.

    If json support added by this pr is working ok, jsonb support would be easy to do.

    Please, give it a try and let me know if it works ok for you.

  2. namtih58 commented on May 27, 2014

    @namtih58
    Author

    No I havent tried your json implementation yet. I am currently using Json.net to serialize and deserialize to/from text and using linq to json for querying (migrating from another db). I currently in need of the indexing capabilities provided by the jsonb and hstore types to shift that work to the db.

    Ill try saving the serialized output as json instead of text

  3. self-assigned this
    on Jun 21, 2014
  4. roji commented on Jun 21, 2014

    @roji
    Member

    I've just merged @franciscojunior's json support into master, and added support for hstore and the new jsonb type (pg9.4beta1) as well.

    @namtih58, can you please grab the latest master build at build.npgsql.org (http://build.npgsql.org/viewLog.html?buildId=1348&buildTypeId=npgsql_all&tab=artifacts) and confirm that it works for you?

  5. kppullin commented on Jul 10, 2014

    @kppullin
    Contributor

    @roji, @franciscojunior - I gave the jsonb funtionality a test and it works great so far! I'm able to write jsonb data as well as query using the jsonb functions.

    As an aside, I prefer using Dapper for data access and had to write an implementation of the ICustomQueryParamter interface in order to set the correct NpgsqlParameter data type. Here's that code in case it's useful to others:

    public class JsonbString : Dapper.SqlMapper.ICustomQueryParameter
    {
        private string _json;
    
        public JsonbString(string json)
        {
            _json = json;
        }
    
        public void AddParameter(System.Data.IDbCommand command, string name)
        {
            var param = (NpgsqlParameter)command.CreateParameter();
            param.ParameterName = name;
            param.Value = _json;
            param.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Jsonb;
            command.Parameters.Add(param);
        }
    }
    
    ...
    
    class MyDto
    {
        JsonbString data { get; set; }
    }
    
    ...
    
    conn.Execute("INSERT INTO jsonb_table(data) VALUES(:data)", myDto);
    
  6. added this to the milestone on Jul 10, 2014
  7. roji commented on Jul 10, 2014

    @roji
    Member

    Great @kppullin, thanks for testing and the Dapper proposal, I'm also a pretty big fan of Dapper.

  8. changed the title [-]Surport Hstore and Jsonb[/-] [+]Support Hstore and Jsonb[/+] on Jul 10, 2014
  9. jmwright commented on Mar 27, 2015

    @jmwright

    Is there an example anywhere of how to use Hstore?

  10. Emill commented on Mar 27, 2015

    @Emill
    Contributor

    Hi. It is mapped as a Dictionary<string, string> so as long as you have configured the server for hstore support (see Postgresql docs), it should work out of the box work with the latest master branch.

  11. jmwright commented on Mar 27, 2015

    @jmwright

    Thanks for the reply.

    I keep getting an unexpected end of string error when I try the following.

    The following dictionary is filled with key/value pairs.

    Dictionary<string, string> tagObjects = new Dictionary<string, string>();
    cmd = PostgresDAL.CreateCommand(conn, "INSERT INTO process.cycles (created_at, equip_id, data) VALUES (now()," + equipID + ", :data) RETURNING id, created_at;");
    NpgsqlParameter dataEntries = new NpgsqlParameter(":data", NpgsqlDbType.Hstore);
    dataEntries.Value = tagObjects;
    cmd.Parameters.Add(dataEntries);
    rdr = cmd.ExecuteReader();
    if (rdr.Read())
    {
        cycleID = (int)rdr["id"];
        createdAt = (DateTime)rdr["created_at"];
    }

    PostgresDAL.CreateConnection is just the following.

    public static NpgsqlCommand CreateCommand(NpgsqlConnection pConnection, string pCommand)
     {
        NpgsqlCommand cmd = pConnection.CreateCommand();
        cmd.CommandText = pCommand;
        cmd.CommandTimeout = 30;
        cmd.CommandType = System.Data.CommandType.Text;
        return cmd;
    }

    Here's the exception I get.

    ERROR: XX000: Unexpected end of string;

    Any thoughts on what I'm doing wrong?

  12. roji commented on Mar 28, 2015

    @roji
    Member

    @jmwright, what version of Npgsql are you using?

  13. jmwright commented on Mar 28, 2015

    @jmwright

    @roji - 2.2.5 from NuGet

  14. roji commented on Mar 28, 2015

    @roji
    Member

    @jmwright in 2.2 we only support hstore as a text value, not as a dictionary of strings. So it's your responsibility to construct a valid hstore string from whatever data you have, and to parse the results.

    In v3.0 (our master branch), our next major version, hstore is supported as a dictionary of string key-value pairs, like @Emill mentioned above. However, that version isn't stable yet, so for now you'll have to make do...

  15. jmwright commented on Mar 28, 2015

    @jmwright

    OK, thanks. I'll switch to using a string.

  16. chaliy commented on Jan 8, 2016

    @chaliy

    There is easier way to insert json(and probably jsonb) values:

     await connection.ExecuteAsync("INSERT INTO json_table(data) VALUES(@Data::json)", new {
                    Data = "{\"foo\": \"bar\"}"
     });
    
  17. dukecheng commented on Oct 9, 2016

    @dukecheng

    @chaliy thanks, your code is worked.

  18. makcakaya commented on Oct 19, 2016

    @makcakaya

    @chaliy Thanks! It works with Dapper too (I don't know if there is or not any reason not to do so but still :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions