Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

How to select into array? #368

Open
DJDaveMark opened this issue Sep 21, 2018 · 1 comment
Open

How to select into array? #368

DJDaveMark opened this issue Sep 21, 2018 · 1 comment

Comments

@DJDaveMark
Copy link

DJDaveMark commented Sep 21, 2018

I need to use the following style of query:

select
    array(select col1 from table where id = ?) as col1,
    array(select col2 from table where id = ?) as col2;

Which produces something like:

col1 col2
{1,2,3} {4,5,6}

How can I turn it into a squel query while still using squel.toParam()?

@DJDaveMark
Copy link
Author

DJDaveMark commented Sep 21, 2018

Here's a workaround I hacked together:

var query = squel.select()
    .field(
        Squel.select()
            .field('col1')
            .from('table')
            .where('id = ?', id),
        'col1' // subquery alias
    )
    .from('DELETE_FROM')
    .toParam();

// SELECT (SELECT col1 FROM table WHERE (parent_id = 1)) AS col1 FROM DELETE_FROM

query.text = modifiedQuery.text.split('(SELECT').join('ARRAY(SELECT'); // replace all
query.text = modifiedQuery.text.replace(' FROM DELETE_FROM', '');

// SELECT ARRAY(SELECT col1 FROM table WHERE (id = 1)) AS col1

Things I didn't expect:

  • If I didn't use .from() then the field parts weren't added.
  • By using subqueries, the arguments are no longer parameterized via query.values

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant