sqlmodel

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package core.sqlmodel provides functionality for modeling SQL constructs.

Index

Constants

View Source
const (
	// StmtSelect is executed using sql.DB.Query.
	StmtSelect = "select"

	// StmtOther is executed using sql.DB.Exec.
	StmtOther = "other"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ColDef

type ColDef struct {
	Name  string    `json:"name"`
	Table *TableDef `json:"-"`
	Kind  kind.Kind `json:"kind"`

	NotNull    bool `json:"not_null"`
	HasDefault bool `json:"has_default"`

	// Size typically applies to text fields, e.g. VARCHAR(255).
	Size int `json:"size"`

	Unique     bool          `json:"unique"`
	ForeignKey *FKConstraint `json:"foreign_key,omitempty"`
}

ColDef models a table column definition.

type FKConstraint

type FKConstraint struct {
	// RefTable is the name of the referenced parent table.
	RefTable string `json:"ref_table"`
	// RefCol is the name of the referenced col in the parent table.
	RefCol string `json:"ref_col"`
	// OnDelete is one of CASCADE or SET_NULL, defaults to CASCADE.
	OnDelete string `json:"on_delete"`
	// OnUpdate is one of CASCADE or SET_NULL, defaults to CASCADE.
	OnUpdate string `json:"on_update"`
}

FKConstraint models a foreign key constraint.

type StmtType

type StmtType string

StmtType is the type of SQL statement such as "select".

func SplitSQL

func SplitSQL(input io.Reader, delim string, moreDelims ...string) (stmts []string, types []StmtType, err error)

SplitSQL splits SQL text into multiple statements, demarcated by delim (typically a semicolon) or additional delim values such as "GO" or "GO;" For example, this is useful for splitting up a .sql file containing multiple statements. Empty lines and comment lines are not returned, nor are the separator elements themselves.

This is a very rudimentary implementation. It currently only works if the delimiters are at the end of the line. Also, its ability to detect the correct statement type is limited.

type TableDef

type TableDef struct {
	// Name is the table name.
	Name string `json:"name"`

	// PKColName is the name of the primary key column, or empty.
	//
	// REVISIT: this construct does not allow for composite PK.
	PKColName string `json:"primary_key,omitempty"`

	// AutoIncrement, if true, indicates that a PK column
	// should autoincrement.
	//
	// REVISIT: this construct does not allow for composite PK.
	AutoIncrement bool `json:"auto_increment"`

	// Cols is the table's column definitions.
	Cols []*ColDef `json:"cols"`
}

TableDef models a database table definition.

func NewTableDef

func NewTableDef(tblName string, colNames []string, colKinds []kind.Kind) *TableDef

NewTableDef is a convenience constructor for creating a simple table definition.

func (*TableDef) ColKinds

func (t *TableDef) ColKinds() []kind.Kind

ColKinds returns a new slice containing the kinds of t's columns.

func (*TableDef) ColNames

func (t *TableDef) ColNames() []string

ColNames returns a new slice containing the names of t's columns.

func (*TableDef) ColsByName

func (t *TableDef) ColsByName(cols []string) ([]*ColDef, error)

ColsByName returns the ColDefs for each named column, or an error if any column is not matched.

func (*TableDef) FindCol

func (t *TableDef) FindCol(name string) (*ColDef, error)

FindCol returns the named ColDef or nil if not found.

func (*TableDef) String

func (t *TableDef) String() string

Jump to

Keyboard shortcuts

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