Skip to content

Commit

Permalink
Drop precomputed FETCH_ALL constant
Browse files Browse the repository at this point in the history
In the PG headers FETCH_ALL is a synonym of LONG_MAX, and the latter
value depends on the actual architecture so cannot be precomputed and
hardcoded in the extracted enums.

Given that it's used in a single place, instead of doing fancy gymnic to
compute it lazily, take the straight path and directly use the LONG_MAX
symbol exposed in the parser module, that correspond to its homonym C
value.

This fixes issue #142.

While there, tweak the FetchStmt printer to emit slightly nicer
representation.
lelit committed Jan 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d8ae15b commit 6f4a6d8
Showing 6 changed files with 89 additions and 33 deletions.
6 changes: 1 addition & 5 deletions docs/parsenodes.rst
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
.. :Project: pglast -- DO NOT EDIT: generated automatically
.. :Author: Lele Gaifax <[email protected]>
.. :License: GNU General Public License version 3 or later
.. :Copyright: © 2017-2023 Lele Gaifax
.. :Copyright: © 2017-2024 Lele Gaifax
..
==============================================================================
@@ -985,7 +985,3 @@ __ https://github.com/pganalyze/libpg_query/blob/db39825/src/postgres/include/no
.. data:: CURSOR_OPT_PARALLEL_OK

See `here for details <https://github.com/pganalyze/libpg_query/blob/db39825/src/postgres/include/nodes/parsenodes.h#L2892>`__.

.. data:: FETCH_ALL

See `here for details <https://github.com/pganalyze/libpg_query/blob/db39825/src/postgres/include/nodes/parsenodes.h#L2927>`__.
4 changes: 1 addition & 3 deletions pglast/enums/parsenodes.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
# :Project: pglast -- DO NOT EDIT: automatically extracted from parsenodes.h @ 15-4.2.4-0-gdb39825
# :Author: Lele Gaifax <[email protected]>
# :License: GNU General Public License version 3 or later
# :Copyright: © 2017-2023 Lele Gaifax
# :Copyright: © 2017-2024 Lele Gaifax
#

from enum import Enum, IntEnum, IntFlag, auto
@@ -486,5 +486,3 @@ class WCOKind(IntEnum):
CURSOR_OPT_CUSTOM_PLAN = 0x0400

CURSOR_OPT_PARALLEL_OK = 0x0800

FETCH_ALL = 9223372036854775807
30 changes: 16 additions & 14 deletions pglast/printers/dml.py
Original file line number Diff line number Diff line change
@@ -3,10 +3,11 @@
# :Created: sab 05 ago 2017 16:34:08 CEST
# :Author: Lele Gaifax <[email protected]>
# :License: GNU General Public License version 3 or later
# :Copyright: © 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lele Gaifax
# :Copyright: © 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 Lele Gaifax
#

from .. import ast, enums
from ..parser import LONG_MAX
from . import IntEnumPrinter, get_string_value, node_printer


@@ -634,29 +635,29 @@ class FetchDirectionPrinter(IntEnumPrinter):
enum = enums.FetchDirection

def FETCH_FORWARD(self, node, output):
if node.howMany == enums.FETCH_ALL:
output.write('ALL ')
elif node.howMany != 1:
output.write(f'FORWARD {node.howMany} ')
if node.howMany == 1:
output.write('NEXT')
else:
output.write('FORWARD')
output.swrite('ALL' if node.howMany == LONG_MAX else str(node.howMany))

def FETCH_BACKWARD(self, node, output):
if node.howMany == enums.FETCH_ALL:
output.write('BACKWARD ALL ')
elif node.howMany != 1:
output.write(f'BACKWARD {node.howMany} ')
if node.howMany == 1:
output.write('PRIOR')
else:
output.write('PRIOR ')
output.write('BACKWARD')
output.swrite('ALL' if node.howMany == LONG_MAX else str(node.howMany))

def FETCH_ABSOLUTE(self, node, output):
if node.howMany == 1:
output.write('FIRST ')
output.write('FIRST')
elif node.howMany == -1:
output.write('LAST ')
output.write('LAST')
else:
output.write(f'ABSOLUTE {node.howMany} ')
output.write(f'ABSOLUTE {node.howMany}')

def FETCH_RELATIVE(self, node, output):
output.write(f'RELATIVE {node.howMany} ')
output.write(f'RELATIVE {node.howMany}')


fetch_direction_printer = FetchDirectionPrinter()
@@ -666,6 +667,7 @@ def FETCH_RELATIVE(self, node, output):
def fetch_stmt(node, output):
output.write('MOVE ' if node.ismove else 'FETCH ')
fetch_direction_printer(node.direction, node, output)
output.write(' IN ' if node.ismove else ' FROM ')
output.print_name(node.portalname)


35 changes: 35 additions & 0 deletions tests/test_printers_prettification/dml/fetch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fetch all foo
=
FETCH FORWARD ALL FROM foo

fetch next in foo
=
FETCH NEXT FROM foo

fetch forward 1 from foo
=
FETCH NEXT FROM foo

fetch 1 from foo
=
FETCH NEXT FROM foo

fetch backward 1 from foo
=
FETCH PRIOR FROM foo

fetch prior in foo
=
FETCH PRIOR FROM foo

fetch absolute 1 foo
=
FETCH FIRST FROM foo

fetch absolute -1 foo
=
FETCH LAST FROM foo

fetch absolute 42 foo
=
FETCH ABSOLUTE 42 FROM foo
35 changes: 35 additions & 0 deletions tests/test_printers_prettification/dml/move.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
move all foo
=
MOVE FORWARD ALL IN foo

move next in foo
=
MOVE NEXT IN foo

move forward 1 from foo
=
MOVE NEXT IN foo

move 1 from foo
=
MOVE NEXT IN foo

move backward 1 from foo
=
MOVE PRIOR IN foo

move prior in foo
=
MOVE PRIOR IN foo

move absolute 1 foo
=
MOVE FIRST IN foo

move absolute -1 foo
=
MOVE LAST IN foo

move absolute 42 foo
=
MOVE ABSOLUTE 42 IN foo
12 changes: 1 addition & 11 deletions tools/extract_enums.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
# :Created: gio 03 ago 2017 14:54:39 CEST
# :Author: Lele Gaifax <[email protected]>
# :License: GNU General Public License version 3 or later
# :Copyright: © 2017, 2018, 2019, 2020, 2021, 2022 Lele Gaifax
# :Copyright: © 2017, 2018, 2019, 2020, 2021, 2022, 2024 Lele Gaifax
#

from datetime import date
@@ -13,12 +13,6 @@

from pycparser import c_ast, c_parser

try:
from pglast.parser import LONG_MAX
except ModuleNotFoundError:
# bootstrap
from sys import maxsize as LONG_MAX


PY_HEADER = """\
# -*- coding: utf-8 -*-
@@ -144,10 +138,6 @@ def extract_defines(source):
line)
if m is not None:
yield m.group(1), m.group(2)
else:
m = match(r"#define\s+([a-zA-Z_]+)\s+LONG_MAX", line)
if m is not None:
yield m.group(1), LONG_MAX


def emit_constant(value):

0 comments on commit 6f4a6d8

Please sign in to comment.