information

package
v0.0.0-...-ab31a86 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package information contains structs and functions to query the information_schema.

See https://en.wikipedia.org/wiki/Information_schema and https://www.postgresql.org/docs/13/information-schema.html

Index

Constants

View Source
const StyleAllMonospace = `<style>* { font-family: "Lucida Console", Monaco, monospace; }</style>`
View Source
const StyleDefaultTable = `` /* 909-byte string literal not displayed */

Variables

View Source
var RenderUUIDPrimaryKeyRefsHTML = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
	var (
		title       string
		mainContent any
		style       = []string{StyleAllMonospace, StyleDefaultTable, `<style>h1 {color:red}</style>`}
	)
	pk, err := uu.IDFromString(request.URL.Query().Get("pk"))
	if err != nil {
		title = "Primary Key UUID"
		mainContent = `
			<form onsubmit="event.preventDefault();location='.?pk='+encodeURIComponent(document.getElementById('uuid').value.trim())">
				<input type="text" size="40" id="uuid"/>
				<input type="submit" value="Look up"/>
			</form>`
	} else {
		title = pk.String()
		ctx := request.Context()
		cols, err := GetPrimaryKeyColumnsOfType(ctx, "uuid")
		if err != nil {
			http.Error(writer, err.Error(), http.StatusInternalServerError)
			return
		}
		tableRows, err := GetTableRowsWithPrimaryKey(ctx, cols, pk)
		if err != nil {
			http.Error(writer, err.Error(), http.StatusInternalServerError)
			return
		}
		sort.SliceStable(tableRows, func(i, j int) bool {
			return !tableRows[i].ForeignKey && tableRows[j].ForeignKey
		})
		var b strings.Builder
		fmt.Fprintf(&b, "<h2><button onclick='navigator.clipboard.writeText(%q)'>Copy UUID</button></h2>", pk)
		for _, tableRow := range tableRows {
			fmt.Fprintf(&b, "<h3>%s</h3>", html.EscapeString(tableRow.Table))
			fmt.Fprintf(&b, "<table>")
			for col, title := range tableRow.Header {
				val := tableRow.Row[col]
				id, err := uu.IDFromString(val)
				if err == nil {
					if id == pk {
						var fk string
						if tableRow.ForeignKey {
							fk = " (foreign key)"
						}
						fmt.Fprintf(&b, "<tr><td>%s</td><td><b style='color:red'>%s</b>%s</td></tr>", html.EscapeString(title), id, fk)
					} else {
						fmt.Fprintf(&b, "<tr><td>%[1]s</td><td><a href='.?pk=%[2]s'>%[2]s</a></td></tr>", html.EscapeString(title), id)
					}
				} else {
					fmt.Fprintf(&b, "<tr><td>%s</td><td>%s</td></tr>", html.EscapeString(title), html.EscapeString(val))
				}
			}
			fmt.Fprintf(&b, "</table>")
		}
		mainContent = b.String()
	}

	tpl, err := template.New("").Parse(htmlTemplate)
	if err != nil {
		http.Error(writer, err.Error(), http.StatusInternalServerError)
		return
	}
	var buf bytes.Buffer
	err = tpl.Execute(&buf, map[string]any{
		"title":       title,
		"style":       style,
		"headerTitle": true,
		"mainContent": mainContent,
	})
	if err != nil {
		http.Error(writer, err.Error(), http.StatusInternalServerError)
		return
	}
	writer.Header().Add("Content-Type", "text/html; charset=utf-8")
	writer.Write(buf.Bytes())
})

Functions

func ColumnExists

func ColumnExists(ctx context.Context, table, column string) (exists bool, err error)

Types

type CheckConstraints

type CheckConstraints struct {
	ConstraintCatalog String `db:"constraint_catalog"`
	ConstraintSchema  String `db:"constraint_schema"`
	ConstraintName    String `db:"constraint_name"`
	CheckClause       String `db:"check_clause"`
}

type Column

type Column struct {
	TableCatalog           String `db:"table_catalog"`
	TableSchema            String `db:"table_schema"`
	TableName              String `db:"table_name"`
	ColumnName             String `db:"column_name"`
	OrdinalPosition        int    `db:"ordinal_position"`
	ColumnDefault          String `db:"column_default"`
	IsNullable             YesNo  `db:"is_nullable"`
	DataType               String `db:"data_type"`
	CharacterMaximumLength *int   `db:"character_maximum_length"`
	CharacterOctetLength   *int   `db:"character_octet_length"`
	NumericPrecision       *int   `db:"numeric_precision"`
	NumericPrecisionRadix  *int   `db:"numeric_precision_radix"`
	NumericScale           *int   `db:"numeric_scale"`
	DatetimePrecision      *int   `db:"datetime_precision"`
	IntervalType           String `db:"interval_type"`
	IntervalPrecision      *int   `db:"interval_precision"`
	CharacterSetCatalog    String `db:"character_set_catalog"`
	CharacterSetSchema     String `db:"character_set_schema"`
	CharacterSetName       String `db:"character_set_name"`
	CollationCatalog       String `db:"collation_catalog"`
	CollationSchema        String `db:"collation_schema"`
	CollationName          String `db:"collation_name"`
	DomainCatalog          String `db:"domain_catalog"`
	DomainSchema           String `db:"domain_schema"`
	DomainName             String `db:"domain_name"`
	UDTCatalog             String `db:"udt_catalog"`
	UDTSchema              String `db:"udt_schema"`
	UDTName                String `db:"udt_name"`
	ScopeCatalog           String `db:"scope_catalog"`
	ScopeSchema            String `db:"scope_schema"`
	ScopeName              String `db:"scope_name"`
	MaximumCardinality     *int   `db:"maximum_cardinality"`
	DTDIdentifier          String `db:"dtd_identifier"`
	IsSelfReferencing      YesNo  `db:"is_self_referencing"`
	IsIdentity             YesNo  `db:"is_identity"`
	IdentityGeneration     String `db:"identity_generation"`
	IdentityStart          String `db:"identity_start"`
	IdentityIncrement      String `db:"identity_increment"`
	IdentityMaximum        String `db:"identity_maximum"`
	IdentityMinimum        String `db:"identity_minimum"`
	IdentityCycle          YesNo  `db:"identity_cycle"`
	IsGenerated            String `db:"is_generated"`
	GenerationExpression   String `db:"generation_expression"`
	IsUpdatable            YesNo  `db:"is_updatable"`
}

type Domain

type Domain struct {
	DomainCatalog          String `db:"domain_catalog"`
	DomainSchema           String `db:"domain_schema"`
	DomainName             String `db:"domain_name"`
	DataType               String `db:"data_type"`
	CharacterMaximumLength *int   `db:"character_maximum_length"`
	CharacterOctetLength   *int   `db:"character_octet_length"`
	CharacterSetCatalog    String `db:"character_set_catalog"`
	CharacterSetSchema     String `db:"character_set_schema"`
	CharacterSetName       String `db:"character_set_name"`
	CollationCatalog       String `db:"collation_catalog"`
	CollationSchema        String `db:"collation_schema"`
	CollationName          String `db:"collation_name"`
	NumericPrecision       *int   `db:"numeric_precision"`
	NumericPrecisionRadix  *int   `db:"numeric_precision_radix"`
	NumericScale           *int   `db:"numeric_scale"`
	DatetimePrecision      *int   `db:"datetime_precision"`
	IntervalType           String `db:"interval_type"`
	IntervalPrecision      *int   `db:"interval_precision"`
	DomainDefault          String `db:"domain_default"`
	UDTCatalog             String `db:"udt_catalog"`
	UDTSchema              String `db:"udt_schema"`
	UDTName                String `db:"udt_name"`
	ScopeCatalog           String `db:"scope_catalog"`
	ScopeSchema            String `db:"scope_schema"`
	ScopeName              String `db:"scope_name"`
	MaximumCardinality     *int   `db:"maximum_cardinality"`
	DTDIdentifier          String `db:"dtd_identifier"`
}

type KeyColumnUsage

type KeyColumnUsage struct {
	ConstraintCatalog          String `db:"constraint_catalog"`
	ConstraintSchema           String `db:"constraint_schema"`
	ConstraintName             String `db:"constraint_name"`
	TableCatalog               String `db:"table_catalog"`
	TableSchema                String `db:"table_schema"`
	TableName                  String `db:"table_name"`
	ColumnName                 String `db:"column_name"`
	OrdinalPosition            int    `db:"ordinal_position"`
	PositionInUniqueConstraint *int   `db:"position_in_unique_constraint"`
}

type PrimaryKeyColumn

type PrimaryKeyColumn struct {
	Table      string `db:"table"`
	Column     string `db:"column"`
	Type       string `db:"type"`
	ForeignKey bool   `db:"foreign_key"`
}

func GetPrimaryKeyColumns

func GetPrimaryKeyColumns(ctx context.Context) (cols []PrimaryKeyColumn, err error)

func GetPrimaryKeyColumnsOfType

func GetPrimaryKeyColumnsOfType(ctx context.Context, pkType string) (cols []PrimaryKeyColumn, err error)

type Schema

type Schema struct {
	CatalogName                String `db:"catalog_name"`
	SchemaName                 String `db:"schema_name"`
	SchemaOwner                String `db:"schema_owner"`
	DefaultCharacterSetCatalog String `db:"default_character_set_catalog"`
	DefaultCharacterSetSchema  String `db:"default_character_set_schema"`
	DefaultCharacterSetName    String `db:"default_character_set_name"`
	SqlPath                    String `db:"sql_path"`
}

type String

type String string

String is a string that implements the sql.Scanner interface to scan NULL as an empty string.

func (*String) Scan

func (y *String) Scan(value any) error

type Table

type Table struct {
	TableCatalog              String `db:"table_catalog"`
	TableSchema               String `db:"table_schema"`
	TableName                 String `db:"table_name"`
	TableType                 String `db:"table_type"`
	SelfReferencingColumnName String `db:"self_referencing_column_name"`
	ReferenceGeneration       String `db:"reference_generation"`
	UserDefinedTypeCatalog    String `db:"user_defined_type_catalog"`
	UserDefinedTypeSchema     String `db:"user_defined_type_schema"`
	UserDefinedTypeName       String `db:"user_defined_type_name"`
	IsInsertableInto          YesNo  `db:"is_insertable_into"`
	IsTyped                   YesNo  `db:"is_typed"`
	CommitAction              String `db:"commit_action"`
}

func GetAllTables

func GetAllTables(ctx context.Context) (tables []*Table, err error)

func GetTable

func GetTable(ctx context.Context, catalog, schema, name string) (table *Table, err error)

type TableRowWithPrimaryKey

type TableRowWithPrimaryKey struct {
	PrimaryKeyColumn
	Header []string
	Row    []string
}

func GetTableRowsWithPrimaryKey

func GetTableRowsWithPrimaryKey(ctx context.Context, pkCols []PrimaryKeyColumn, pk any) (tableRows []TableRowWithPrimaryKey, err error)

type View

type View struct {
	CatalogName                String `db:"catalog_name"`
	SchemaName                 String `db:"schema_name"`
	SchemaOwner                String `db:"schema_owner"`
	DefaultCharacterSetCatalog String `db:"default_character_set_catalog"`
	DefaultCharacterSetSchema  String `db:"default_character_set_schema"`
	DefaultCharacterSetName    String `db:"default_character_set_name"`
	SqlPath                    String `db:"sql_path"`
}

type YesNo

type YesNo bool

YesNo is a bool type that implements the sql.Scanner interface for the information_schema.yes_or_no type.

func (*YesNo) Scan

func (y *YesNo) Scan(value any) error

Jump to

Keyboard shortcuts

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