Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add a column to a query #293

Open
kduraiswami opened this issue Jul 5, 2023 · 3 comments
Open

How to add a column to a query #293

kduraiswami opened this issue Jul 5, 2023 · 3 comments
Labels

Comments

@kduraiswami
Copy link

How is it possible to add a column to an existing query after we have converted it into a parsed tree

@kduraiswami
Copy link
Author

kduraiswami commented Jul 5, 2023

q = PgQuery.parse("SELECT a FROM tbl")

q.tree.stmts[0].stmt.select_stmt.target_list.push(
    PgQuery::Node.new(
        res_target: PgQuery::ResTarget.new(
            val: PgQuery::Node.new(
                column_ref: PgQuery::ColumnRef.new(
                    fields: [PgQuery::Node.new(string: PgQuery::String.new(sval: 'b'))]
                )
            )
        )
    )
)

q.deparse

=> "SELECT a, b FROM tbl"

@kduraiswami
Copy link
Author

Note that if you add two elements to the column_ref array it will qualify the name with a period:

column_ref: PgQuery::ColumnRef.new(
     fields: [PgQuery::Node.new(string: PgQuery::String.new(sval: 'tbl'))],
     fields: [PgQuery::Node.new(string: PgQuery::String.new(sval: 'b'))]
)
q.deparse

=> "SELECT a, tbl.b FROM tbl"

@lfittl
Copy link
Member

lfittl commented Jul 5, 2023

Thanks for opening the issue as discussed & sharing the solution I've shared via email!

@lfittl lfittl added the question label Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants