dbutils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

DropPostgresFunctionsSQL drops all popgres helper SQL functions.

View Source
const DropPostgresJSONFunctionsSQL = `` /* 337-byte string literal not displayed */
View Source
const DropPostgresStrftimeFunctionsSQL = `` /* 530-byte string literal not displayed */

PostgresFunctionsSQL creates all popgres helper SQL functions.

View Source
const PostgresJSONFunctionsSQL = `` /* 2392-byte string literal not displayed */

PostgresJSONFunctionsSQL creates the json helper functions used by the tools/dbutils json expressions (in the main and in the logs schema).

View Source
const PostgresStrftimeFunctionsSQL = `` /* 7425-byte string literal not displayed */

PostgresStrftimeFunctionsSQL creates the popgres_strftime SQL functions used by the strftime() filter function (an emulation of the SQLite strftime with its time values, modifiers and format substitutions).

The functions run with the UTC time zone (the same as SQLite) and return NULL for invalid time values, modifiers or format substitutions.

Variables

This section is empty.

Functions

func AliasOrIdentifier

func AliasOrIdentifier(columnOrTableIdentifier string) string

AliasOrIdentifier returns the alias from a column or table identifier. Returns the identifier unmodified if no alias was found.

func HasSingleColumnUniqueIndex deprecated

func HasSingleColumnUniqueIndex(column string, indexes []string) bool

Deprecated: Use `_, ok := FindSingleColumnUniqueIndex(indexes, column)` instead.

HasColumnUniqueIndex loosely checks whether the specified column has a single column unique index (WHERE statements are ignored).

func JSONArrayLength

func JSONArrayLength(column string) string

JSONArrayLength returns a SQL expression with the number of elements of a json array column.

It works with both json and non-json column values.

Returns 0 for empty string or NULL column values.

func JSONEach

func JSONEach(column string) string

JSONEach returns a set returning SQL expression with a single "value" text column for each element of a json array column.

Non-array and non-json column values are treated as a single element array.

It is intended to be used as a FROM/JOIN item, eg.:

"LEFT JOIN " + JSONEach("tbl.col") + " je ON TRUE"

func JSONExtract

func JSONExtract(column string, path string) string

JSONExtract returns a SQL expression that extracts the jsonb value at the specified path of a json column (or the whole normalized jsonb value when path is empty).

The path uses the PocketBase notation (eg. "a.b[0].c").

NULL is returned for missing paths and for the json null value (the same as the SQLite JSON_EXTRACT).

func NormalizeIdentifierQuotes

func NormalizeIdentifierQuotes(expr string) string

NormalizeIdentifierQuotes replaces the backtick quoted identifiers in the provided SQL expression with double quoted ones.

Single quoted string literals (and the double quoted identifiers) are left untouched.

Types

type Index

type Index struct {
	SchemaName string        `json:"schemaName"`
	IndexName  string        `json:"indexName"`
	TableName  string        `json:"tableName"`
	Where      string        `json:"where"`
	Columns    []IndexColumn `json:"columns"`
	Unique     bool          `json:"unique"`
	Optional   bool          `json:"optional"`
}

Index represents a single parsed SQL CREATE INDEX expression.

func FindSingleColumnUniqueIndex

func FindSingleColumnUniqueIndex(indexes []string, column string) (Index, bool)

FindSingleColumnUniqueIndex returns the first matching single column unique index.

func ParseIndex

func ParseIndex(createIndexExpr string) Index

ParseIndex parses the provided "CREATE INDEX" SQL string into Index struct.

func (Index) Build

func (idx Index) Build() string

Build returns a "CREATE INDEX" SQL string from the current index parts.

It is the PocketBase (SQLite) format used for the stored collection index definitions; use Index.BuildPostgres for the SQL that is executed.

Returns empty string if idx.IsValid() is false.

func (Index) BuildPostgres

func (idx Index) BuildPostgres() string

BuildPostgres returns a PostgreSQL "CREATE INDEX" SQL string from the current index parts (the SQL that is executed, while Index.Build returns the stored definition format).

The identifiers are double quoted and the backtick quoted identifiers inside the column expressions and the WHERE clause (the PocketBase/SQLite index format) are converted to double quoted ones.

The SQLite collations are translated as follows:

  • NOCASE is replaced with a lower(col) expression;
  • BINARY is omitted since it is the Postgres default behavior;
  • any other collation name is used as it is (eg. "C").

Returns empty string if idx.IsValid() is false.

func (Index) IsValid

func (idx Index) IsValid() bool

IsValid checks if the current Index contains the minimum required fields to be considered valid.

type IndexColumn

type IndexColumn struct {
	Name    string `json:"name"` // identifier or expression
	Collate string `json:"collate"`
	Sort    string `json:"sort"`
}

IndexColumn represents a single parsed SQL index column.

Jump to

Keyboard shortcuts

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