postgres

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 6 Imported by: 2

Documentation

Overview

Package postgres provides a PostgreSQL dialect parser using pg_query_go.

Package postgres provides a PostgreSQL dialect parser using pg_query_go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseViewSQL

func ParseViewSQL(sql []byte, defaultSchema string) (parser.ViewSQL, error)

ParseViewSQL parses a CREATE VIEW or CREATE MATERIALIZED VIEW statement using pg_query and extracts the view name, schema, SELECT column metadata, and FROM clause table aliases. CREATE OR REPLACE MATERIALIZED VIEW is not valid PostgreSQL and fails at pg_query.Parse.

An unqualified view name resolves to defaultSchema, the schema New gives an unqualified CREATE TABLE (input.schema, PRD §5.5); an empty defaultSchema means "public". Every PostgreSQL view therefore carries a schema, as every table does.

func QualifyFilterIdentifiers

func QualifyFilterIdentifiers(expr, alias string) (string, error)

QualifyFilterIdentifiers rewrites every bare column reference inside expr to be qualified with alias. Already-qualified refs (table.column) are left untouched. The expression is parsed as a PostgreSQL WHERE clause and re-emitted via pg_query's deparser, so quoted identifiers, JSONB operators, CAST/CASE/function-call expressions, and string-literal contents are all handled by the upstream parser rather than custom tokenization.

It performs the codegen-time qualification of sub-categorized o2o `filter:` predicates so the JOIN ON clause binds bare identifiers to the relationship target instead of the parent table.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser implements the parser.Parser interface for PostgreSQL DDL. It uses pg_query_go (which wraps libpg_query via CGO) to parse SQL using PostgreSQL's own parser.

func New

func New(defaultSchema string) *Parser

New creates a new PostgreSQL parser. If defaultSchema is empty, "public" is used.

func (*Parser) Parse

func (p *Parser) Parse(filename string, sql []byte) error

Parse parses PostgreSQL DDL from sql. It applies the statements one at a time, in file order, as PostgreSQL does: a statement sees the schema the statements before it left, so a table moved or renamed mid-file no longer answers to its old name, and the old name is free for a later CREATE. It can be called multiple times with different files; results accumulate.

func (*Parser) Schema

func (p *Parser) Schema() *parser.Schema

Schema returns the accumulated schema model.

func (*Parser) Seed

func (p *Parser) Seed(base *parser.Schema)

Seed pre-populates the parser with an existing schema. This enables "both" mode where introspected tables are loaded before file-based DDL is applied. CREATE TABLE on a seeded table produces a duplicate error; ALTER TABLE on a seeded table modifies it in place.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL