Array for Insertion

From: "Bee(dot)Lists" <bee(dot)lists(at)gmail(dot)com>
To: Submit Postgresql Novice <pgsql-novice(at)postgresql(dot)org>
Subject: Array for Insertion
Date: 2019-02-13 13:27:54
Message-ID: 24DDEB78-BD41-4E38-BE4E-492EF1961327@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Can anybody shed some light on this?

I have an array in Ruby, which I need to use inside various statements in PostgreSQL.

a = [“Alex”, “Bert”, “Charlie”]

Formatting that for insertion is oddly not represented in any methods or functionality anywhere.

-> PostgreSQL is on its own, so it doesn’t have to respect Ruby by any means. However PostgreSQL doesn’t even have a function to format any of its own arrays into friendly string format:

(‘Alex’, ‘Bert’, ‘Charlie’)

a.join(‘,‘) # => Alex’,‘Bert’,‘Charlie

…which is not usable. Yes I know this isn’t a PostgreSQL function. Getting to a PostgreSQL array type is the first challenge, then using array_to_string(a, ‘,’) still results in this issue.

-> PG Gem formats arrays to PostgreSQL arrays:

puts PG::TextEncoder::Array.new.encode(a) # => {Alex,Bert,Charlie}

…which brings us back to the PostgreSQL challenge above.

-> Ruby, from my experience, can’t take an array and wrap its elements in a fully encapsulating join, such as this suggestion:

a = [“Alex”, “Bert”, “Charlie”]
puts a.fulljoin(“‘“, “, " # => ‘Alex’, ‘Bert’, ‘Charlie’

Yes, indeed, I can write my own, and extend the array Class. However I find that in the language, a useful go-to gem with loads of PostgreSQL functions, and the database itself, all are void of any functionality in this space.

Jumping from language type formats to database formats should be second nature, so I’m confused as to why.

I guess this is more of a theoretical question, unless I’m completely blind to some methods. Any insight out there?

Cheers, Bee

Browse pgsql-novice by date

  From Date Subject
Next Message Peter Neave 2019-02-19 00:09:15 Exclusive Locks on Insert into large Logging tables
Previous Message Stephen Frost 2019-02-12 23:39:56 Re: WAL on zfs Settings