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

Support for VALUES List #569

Open
bmyzk opened this issue Mar 10, 2021 · 5 comments
Open

Support for VALUES List #569

bmyzk opened this issue Mar 10, 2021 · 5 comments

Comments

@bmyzk
Copy link

bmyzk commented Mar 10, 2021

Does pypika support for VALUES List like below?
If not, it would be great to add a class for it.

https://www.postgresql.org/docs/9.5/queries-values.html
SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);

@bmyzk
Copy link
Author

bmyzk commented Mar 10, 2021

Here is my workaround.

class ValueList(Selectable):
    def __init__(self, value_list, table_name, alias=None):
        super().__init__(alias=alias)
        self.value_list = value_list
        self.table_name = table_name

    def get_table_name(self):
        return self.table_name

    def list_to_sql(self, value_list):
        return ','.join([f'({",".join([str(v) for v in _v])})' for _v in value_list])

    def get_sql(self, quote_char=None, **kwargs):
        return format_alias_sql('(VALUES ' + self.list_to_sql(self.value_list) + ')', self.alias, quote_char)

values = [[fn.Date('2020-01-01'), fn.Date('2020-01-02')], [fn.Date('2020-01-03'), fn.Date('2020-01-04')]]

t = ValueList(values, 'tmp')

q = Query()
q = q.from_(t.as_(f'{t.table_name}(a,b)'))
q = q.select('a', 'b')

q.get_sql(quote_char=None)

# SELECT tmp.a,tmp.b FROM (VALUES (DATE('2020-01-01'),DATE('2020-01-02')),(DATE('2020-01-03'),DATE('2020-01-04'))) tmp(a,b)

@danifus
Copy link

danifus commented Mar 19, 2021

See also:

@anhqle
Copy link
Contributor

anhqle commented Aug 7, 2023

This would be great PR to add! Are we waiting for the library owner to respond?

@blazing-gig
Copy link

Commenting to know more about the latest status of this ticket and the linked PR. Thanks!

@AzisK
Copy link

AzisK commented Sep 25, 2023

I am one of the maintainers of the repo now but I need to familiarize with the code myself a bit to provide a better insight. Apart from that, feel free to create such a pull request. Does this pull request #517 cover the requested functionality in this issue? If not, it would be great to have 1 pull request that covers a most of the popular databases

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

No branches or pull requests

5 participants