Re: hstore syntax

Lists: pgsql-general
From: Kaare Rasmussen <kaare(at)jasonic(dot)dk>
To: pgsql-general(at)postgresql(dot)org
Subject: hstore syntax
Date: 2013-12-08 10:22:41
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-general

Hi

Trying to write a sql function to return hstore generated from a select.
But I'm thinking there must be a better way.

SELECT 'key => "' || s.part || '"')::hstore

is neither pretty nor secure. At least I need to escape any '"' in
s.part. I'll do so if there's no better way to write this (?)

Also, it seems there's no length or size function for hstore? This is
what I've got working now array_length(akeys(s.hstore_field), 1). It
seems to be a rather long way to do it, but it works.


From: Magnus Hagander <magnus(at)hagander(dot)net>
To: Kaare Rasmussen <kaare(at)jasonic(dot)dk>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: hstore syntax
Date: 2013-12-08 10:45:04
Message-ID: CABUevExJkKdhHZXT5c-DNLLXTD408serTSGvvv7Go4q9k3drNA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-general

On Sun, Dec 8, 2013 at 11:22 AM, Kaare Rasmussen <kaare(at)jasonic(dot)dk> wrote:

> Hi
>
> Trying to write a sql function to return hstore generated from a select.
> But I'm thinking there must be a better way.
>
> SELECT 'key => "' || s.part || '"')::hstore
>
> is neither pretty nor secure. At least I need to escape any '"' in s.part.
> I'll do so if there's no better way to write this (?)
>

If it's just for a single value, you can just use

SELECT hstore('key', s.part)

It also takes array (either one big array, or one array of keys and one
array of values).

Also, it seems there's no length or size function for hstore? This is what
> I've got working now array_length(akeys(s.hstore_field), 1). It seems to
> be a rather long way to do it, but it works.

That I don't think there is one, no.

--
Magnus Hagander
Me: http://www.hagander.net/
Work: http://www.redpill-linpro.com/


From: Kaare Rasmussen <kaare(at)jasonic(dot)dk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: hstore syntax
Date: 2013-12-08 11:18:02
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-general

On 12/08/2013 11:45 AM, Magnus Hagander wrote:
> If it's just for a single value, you can just use
>
> SELECT hstore('key', s.part)
>
> It also takes array (either one big array, or one array of keys and
> one array of values).
>

Super! I knew I was missing the obvious.