| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import csv |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
from harlequin import HarlequinCompletion |
| 7 |
from psycopg import Connection |
| 8 |
|
| 9 |
|
| 10 |
def _get_completions(conn: Connection) -> list[HarlequinCompletion]: |
| 11 |
completions: list[HarlequinCompletion] = [] |
| 12 |
|
| 13 |
# source: https://www.postgresql.org/docs/current/sql-keywords-appendix.html |
| 14 |
keyword_path = Path(__file__).parent / "keywords.tsv" |
| 15 |
with keyword_path.open("r") as f: |
| 16 |
keyword_reader = csv.reader( |
| 17 |
f, |
| 18 |
delimiter="\t", |
| 19 |
) |
| 20 |
_header = next(keyword_reader) |
| 21 |
for keyword, kind, _, _, _ in keyword_reader: |
| 22 |
completions.append( |
| 23 |
HarlequinCompletion( |
| 24 |
label=keyword.lower(), |
| 25 |
type_label="kw", |
| 26 |
value=keyword.lower(), |
| 27 |
priority=100 if kind.startswith("reserved") else 1000, |
| 28 |
context=None, |
| 29 |
) |
| 30 |
) |
| 31 |
|
| 32 |
with conn.cursor() as cur: |
| 33 |
cur.execute( |
| 34 |
r""" |
| 35 |
select distinct |
| 36 |
routine_name as label, |
| 37 |
case when routine_type is null then 'agg' else 'fn' end as type_label, |
| 38 |
case |
| 39 |
when routine_schema = 'pg_catalog' -- |
| 40 |
then null |
| 41 |
else routine_schema |
| 42 |
end as context |
| 43 |
from information_schema.routines |
| 44 |
where |
| 45 |
length(routine_name) < 37 |
| 46 |
and routine_name not ilike '\_%' |
| 47 |
and routine_name not ilike 'pg\_%' |
| 48 |
and routine_name not ilike 'binary\_upgrade\_%' |
| 49 |
|
| 50 |
;""" |
| 51 |
) |
| 52 |
results = cur.fetchall() |
| 53 |
for label, type_label, context in results: |
| 54 |
completions.append( |
| 55 |
HarlequinCompletion( |
| 56 |
label=label, |
| 57 |
type_label=type_label, |
| 58 |
value=label, |
| 59 |
priority=1000, |
| 60 |
context=context, |
| 61 |
) |
| 62 |
) |
| 63 |
|
| 64 |
with conn.cursor() as cur: |
| 65 |
cur.execute("""select distinct name as label from pg_settings""") |
| 66 |
results = cur.fetchall() |
| 67 |
for (label,) in results: |
| 68 |
completions.append( |
| 69 |
HarlequinCompletion( |
| 70 |
label=label, type_label="set", value=label, priority=2000, context=None |
| 71 |
) |
| 72 |
) |
| 73 |
|
| 74 |
return sorted(completions) |