Optional filters

On this page

On SQLite:

  • SQLite needs no cast in ?1 IS NULL, unlike Postgres
  • Numbered ?N placeholders let each filter use its parameter twice

A search form has fields the user may leave blank: a destination, a top price, whether dogs are welcome. You want one query that applies the filters it is given and ignores the rest, not a query for every combination, and not SQL assembled from strings at run time.

The SQL does the switching. Write each filter so that a NULL turns it off, (param IS NULL OR column = param), and mark the parameter optional: true. Seagull makes its field a pointer, and a nil pointer is sent as NULL. The SQL never changes, only the values bound to it.

This example searches a harbour's boat trips four ways with one query.

The query

db/queries/trips.sql

-- sgl.query(SearchTrips, many)
-- ?1 as sgl.param(destination, optional: true)
-- ?2 as sgl.param(max_price, optional: true)
-- ?3 as sgl.param(dogs_welcome, optional: true)
SELECT id, name, destination, price_pence, dogs_welcome
FROM trips
WHERE (?1 IS NULL OR destination = ?1)
  AND (?2 IS NULL OR price_pence <= ?2)
  AND (?3 IS NULL OR dogs_welcome = ?3)
ORDER BY price_pence, id;

Each filter uses its parameter twice, once in the IS NULL test and once in the comparison, so the placeholders are numbered: ?1 in both places is the same value. The binding lines name each parameter and mark it optional.

What sgl generates

Every optional parameter is a pointer field, so the zero value of the struct, with every field nil, is a search with no filters at all:

internal/store

type SearchTripsParams struct {
	Destination *string
	MaxPrice    *int64
	DogsWelcome *bool
}

type SearchTripsRow struct {
	ID          int64  `json:"id"`
	Name        string `json:"name"`
	Destination string `json:"destination"`
	PricePence  int64  `json:"price_pence"`
	DogsWelcome bool   `json:"dogs_welcome"`
}

func (queries *Queries) SearchTrips(ctx context.Context, params SearchTripsParams) ([]SearchTripsRow, error) {
	rows, err := queries.reader.QueryContext(ctx, searchTripsSQL, params.Destination, params.MaxPrice, params.DogsWelcome)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var results []SearchTripsRow
	if rows.Next() {
		var row SearchTripsRow
		scanArgs := []any{&row.ID, &row.Name, &row.Destination, &row.PricePence, &row.DogsWelcome}
		for more := true; more; more = rows.Next() {
			row = SearchTripsRow{}
			if err := rows.Scan(scanArgs...); err != nil {
				return nil, err
			}
			results = append(results, row)
		}
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return results, nil
}

Using it

The program adds four trips, then runs the same search four times: with no filters, by destination, by price, and by destination and dogs together. new("Gull Rock") makes a pointer to a value in one step, and any field left out stays nil.

example.go

package main

import (
	"context"
	"database/sql"
	"fmt"
	"io"
	"strings"

	"seagull.sh/seagull/examples/sqlite/011_optional_filters/internal/store"
)

// run searches boat trips four ways with one query. Each filter in SearchTrips is written
// as (?1 IS NULL OR destination = ?1), and its binding says optional: true, which makes
// the Params field a pointer. A nil pointer is sent as NULL, so ?1 IS NULL is true and
// that filter matches every row; a set pointer applies the filter. The SQL never changes,
// only the values bound to it.
//
// Takes database (*sql.DB) which is a migrated database.
// Takes output (io.Writer) which receives each search's trips.
//
// Returns error when a query fails.
func run(ctx context.Context, database *sql.DB, output io.Writer) error {
	queries := store.New(database)

	trips := []store.AddTripParams{
		{Name: "Seal spotting", Destination: "Gull Rock", PricePence: 1500, DogsWelcome: true},
		{Name: "Puffin cruise", Destination: "Puffin Island", PricePence: 2500, DogsWelcome: false},
		{Name: "Mackerel fishing", Destination: "Gull Rock", PricePence: 2000, DogsWelcome: false},
		{Name: "Sunset sail", Destination: "the bay", PricePence: 1200, DogsWelcome: true},
	}
	for _, trip := range trips {
		if err := queries.AddTrip(ctx, trip); err != nil {
			return fmt.Errorf("adding %s: %w", trip.Name, err)
		}
	}

	searches := []struct {
		label      string
		parameters store.SearchTripsParams
	}{
		{label: "no filters", parameters: store.SearchTripsParams{}},
		{label: "to Gull Rock", parameters: store.SearchTripsParams{Destination: new("Gull Rock")}},
		{label: "£15 or less", parameters: store.SearchTripsParams{MaxPrice: new(int64(1500))}},
		{label: "to Gull Rock with a dog", parameters: store.SearchTripsParams{Destination: new("Gull Rock"), DogsWelcome: new(true)}},
	}
	for _, search := range searches {
		found, err := queries.SearchTrips(ctx, search.parameters)
		if err != nil {
			return fmt.Errorf("searching %s: %w", search.label, err)
		}
		names := make([]string, 0, len(found))
		for _, trip := range found {
			names = append(names, trip.Name)
		}
		fmt.Fprintf(output, "%-24s %s\n", search.label+":", strings.Join(names, ", "))
	}
	return nil
}

What it prints

Each line is the same query with different fields set. Results come back cheapest first, as the query orders them.

no filters:              Sunset sail, Seal spotting, Mackerel fishing, Puffin cruise
to Gull Rock:            Seal spotting, Mackerel fishing
£15 or less:             Sunset sail, Seal spotting
to Gull Rock with a dog: Seal spotting

Across engines

Only these Go types of SearchTrips differ between engines; the rest are the same everywhere.

SQLitePostgreSQLMySQLDuckDBClickHouse
max_price parameter*int64*int64*int32*int32*uint32
id columnint64int64int64int64uint64
price_pence columnint64int64int32int32uint32

CockroachDB and TimescaleDB run the PostgreSQL version unchanged.

MariaDB runs the MySQL version unchanged.

Compare the whole example side by side with PostgreSQL, MySQL, DuckDB and ClickHouse.

Run it locally

From the directory you cloned the Seagull repository into:

cd seagull/examples/sqlite && go run ./011_optional_filters

Its test runs it too, and checks what it prints against output.txt:

cd seagull/examples/sqlite && go test ./011_optional_filters

The files

Every file of the example, as it is in the Seagull repository.

sgl.yaml

# Copyright 2026 PolitePixels Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This project stands against fascism, authoritarianism, and all forms of
# oppression. We built this to empower people, not to enable those who would
# strip others of their rights and dignity.

version: 1

databases:
  - name: trips
    dialect: sqlite
    migrations: db/migrations
    queries: db/queries
    go:
      package: store
      out: internal/store

db/migrations/0001_trips.up.sql

CREATE TABLE trips (
    id           INTEGER PRIMARY KEY,
    name         TEXT    NOT NULL,
    destination  TEXT    NOT NULL,
    price_pence  INTEGER NOT NULL,
    dogs_welcome BOOLEAN NOT NULL
);

db/migrations/0001_trips.down.sql

DROP TABLE trips;

db/queries/trips.sql

-- sgl.query(AddTrip, exec)
INSERT INTO trips (name, destination, price_pence, dogs_welcome) VALUES (?, ?, ?, ?);

-- sgl.query(SearchTrips, many)
-- ?1 as sgl.param(destination, optional: true)
-- ?2 as sgl.param(max_price, optional: true)
-- ?3 as sgl.param(dogs_welcome, optional: true)
SELECT id, name, destination, price_pence, dogs_welcome
FROM trips
WHERE (?1 IS NULL OR destination = ?1)
  AND (?2 IS NULL OR price_pence <= ?2)
  AND (?3 IS NULL OR dogs_welcome = ?3)
ORDER BY price_pence, id;

example.go

package main

import (
	"context"
	"database/sql"
	"fmt"
	"io"
	"strings"

	"seagull.sh/seagull/examples/sqlite/011_optional_filters/internal/store"
)

// run searches boat trips four ways with one query. Each filter in SearchTrips is written
// as (?1 IS NULL OR destination = ?1), and its binding says optional: true, which makes
// the Params field a pointer. A nil pointer is sent as NULL, so ?1 IS NULL is true and
// that filter matches every row; a set pointer applies the filter. The SQL never changes,
// only the values bound to it.
//
// Takes database (*sql.DB) which is a migrated database.
// Takes output (io.Writer) which receives each search's trips.
//
// Returns error when a query fails.
func run(ctx context.Context, database *sql.DB, output io.Writer) error {
	queries := store.New(database)

	trips := []store.AddTripParams{
		{Name: "Seal spotting", Destination: "Gull Rock", PricePence: 1500, DogsWelcome: true},
		{Name: "Puffin cruise", Destination: "Puffin Island", PricePence: 2500, DogsWelcome: false},
		{Name: "Mackerel fishing", Destination: "Gull Rock", PricePence: 2000, DogsWelcome: false},
		{Name: "Sunset sail", Destination: "the bay", PricePence: 1200, DogsWelcome: true},
	}
	for _, trip := range trips {
		if err := queries.AddTrip(ctx, trip); err != nil {
			return fmt.Errorf("adding %s: %w", trip.Name, err)
		}
	}

	searches := []struct {
		label      string
		parameters store.SearchTripsParams
	}{
		{label: "no filters", parameters: store.SearchTripsParams{}},
		{label: "to Gull Rock", parameters: store.SearchTripsParams{Destination: new("Gull Rock")}},
		{label: "£15 or less", parameters: store.SearchTripsParams{MaxPrice: new(int64(1500))}},
		{label: "to Gull Rock with a dog", parameters: store.SearchTripsParams{Destination: new("Gull Rock"), DogsWelcome: new(true)}},
	}
	for _, search := range searches {
		found, err := queries.SearchTrips(ctx, search.parameters)
		if err != nil {
			return fmt.Errorf("searching %s: %w", search.label, err)
		}
		names := make([]string, 0, len(found))
		for _, trip := range found {
			names = append(names, trip.Name)
		}
		fmt.Fprintf(output, "%-24s %s\n", search.label+":", strings.Join(names, ", "))
	}
	return nil
}

main.go

// Command 011_optional_filters applies its migrations to a SQLite database, then runs the
// example in example.go: boat trips are searched with any mix of three optional filters.
//
// Run it with `go run .`, or `go run . -db trips.db` to keep the database.
package main

import (
	"context"
	"database/sql"
	"embed"
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"

	_ "modernc.org/sqlite"

	"seagull.sh/seagull/sdk/migrate"
	"seagull.sh/seagull/sdk/migrate/migrate_backend_sqlite"
)

// migrations holds the migration files, compiled into the binary.
//
//go:embed db/migrations
var migrations embed.FS

func main() {
	path := flag.String("db", "", "SQLite database file; a temporary one when empty")
	flag.Parse()
	if err := execute(context.Background(), *path); err != nil {
		log.Fatal(err)
	}
}

// execute opens and migrates the database at path, or a temporary one when path is empty,
// and runs the example against it.
//
// Takes path (string) which is the database file, or empty.
//
// Returns error when the database cannot be prepared or the example fails.
func execute(ctx context.Context, path string) error {
	if path == "" {
		directory, err := os.MkdirTemp("", "seagull-example-")
		if err != nil {
			return err
		}
		defer func() { _ = os.RemoveAll(directory) }()
		path = filepath.Join(directory, "example.db")
	}
	database, err := sql.Open("sqlite", "file:"+path)
	if err != nil {
		return err
	}
	defer func() { _ = database.Close() }()

	migrator, err := migrate.NewMigrator(database, migrate_backend_sqlite.New(), migrations, "db/migrations")
	if err != nil {
		return err
	}
	if _, err := migrator.Up(ctx); err != nil {
		return fmt.Errorf("migrating: %w", err)
	}
	return run(ctx, database, os.Stdout)
}

internal/store/models.go

// Code generated by sgl. DO NOT EDIT.

package store

type Trips struct {
	ID          int64  `json:"id"`
	Name        string `json:"name"`
	Destination string `json:"destination"`
	PricePence  int64  `json:"price_pence"`
	DogsWelcome bool   `json:"dogs_welcome"`
}

internal/store/querier.go

// Code generated by sgl. DO NOT EDIT.

package store

import (
	"context"
	"database/sql"
)

type DBTX interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

type dbtxWrapper interface {
	WrapDBTX(inner any) any
}

type Queries struct {
	reader  DBTX
	writer  DBTX
	wrapper dbtxWrapper
}

func New(db DBTX) *Queries {
	wrapper, _ := db.(dbtxWrapper)
	return &Queries{reader: db, writer: db, wrapper: wrapper}
}

func NewWithReplica(writer DBTX, reader DBTX) *Queries {
	wrapper, _ := writer.(dbtxWrapper)
	return &Queries{reader: reader, writer: writer, wrapper: wrapper}
}

func (queries *Queries) WithTx(transaction *sql.Tx) *Queries {
	if queries == nil {
		return &Queries{reader: transaction, writer: transaction, wrapper: nil}
	}
	db := queries.wrapDBTX(transaction)
	return &Queries{reader: db, writer: db, wrapper: queries.wrapper}
}

func (queries *Queries) wrapDBTX(db DBTX) DBTX {
	if queries.wrapper == nil {
		return db
	}
	wrapped, ok := queries.wrapper.WrapDBTX(db).(DBTX)
	if !ok {
		return db
	}
	return wrapped
}

func (queries *Queries) RunInTx(ctx context.Context, db *sql.DB, fn func(*Queries) error) error {
	transaction, err := db.BeginTx(ctx, nil)
	if err != nil {
		return err
	}
	defer transaction.Rollback()
	if err = fn(queries.WithTx(transaction)); err != nil {
		return err
	}
	return transaction.Commit()
}

internal/store/trips.sql.go

// Code generated by sgl. DO NOT EDIT.

package store

import "context"

const addTripSQL = `INSERT INTO trips (name, destination, price_pence, dogs_welcome) VALUES (?1, ?2, ?3, ?4);`

type AddTripParams struct {
	Name        string
	Destination string
	PricePence  int64
	DogsWelcome bool
}

func (queries *Queries) AddTrip(ctx context.Context, params AddTripParams) error {
	_, err := queries.writer.ExecContext(ctx, addTripSQL, params.Name, params.Destination, params.PricePence, params.DogsWelcome)
	return err
}

const searchTripsSQL = `SELECT id, name, destination, price_pence, dogs_welcome
FROM trips
WHERE (?1 IS NULL OR destination = ?1)
  AND (?2 IS NULL OR price_pence <= ?2)
  AND (?3 IS NULL OR dogs_welcome = ?3)
ORDER BY price_pence, id;`

type SearchTripsParams struct {
	Destination *string
	MaxPrice    *int64
	DogsWelcome *bool
}

type SearchTripsRow struct {
	ID          int64  `json:"id"`
	Name        string `json:"name"`
	Destination string `json:"destination"`
	PricePence  int64  `json:"price_pence"`
	DogsWelcome bool   `json:"dogs_welcome"`
}

func (queries *Queries) SearchTrips(ctx context.Context, params SearchTripsParams) ([]SearchTripsRow, error) {
	rows, err := queries.reader.QueryContext(ctx, searchTripsSQL, params.Destination, params.MaxPrice, params.DogsWelcome)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var results []SearchTripsRow
	if rows.Next() {
		var row SearchTripsRow
		scanArgs := []any{&row.ID, &row.Name, &row.Destination, &row.PricePence, &row.DogsWelcome}
		for more := true; more; more = rows.Next() {
			row = SearchTripsRow{}
			if err := rows.Scan(scanArgs...); err != nil {
				return nil, err
			}
			results = append(results, row)
		}
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return results, nil
}

output.txt

no filters:              Sunset sail, Seal spotting, Mackerel fishing, Puffin cruise
to Gull Rock:            Seal spotting, Mackerel fishing
£15 or less:             Sunset sail, Seal spotting
to Gull Rock with a dog: Seal spotting

Also on other engines