postgres

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: May 24, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

contains the logic for the sqgen-postgres functions command

contains the logic for the sqgen-postgres tables command

Index

Constants

View Source
const (
	FieldTypeBoolean = "sq.BooleanField"
	FieldTypeJSON    = "sq.JSONField"
	FieldTypeNumber  = "sq.NumberField"
	FieldTypeString  = "sq.StringField"
	FieldTypeTime    = "sq.TimeField"
	FieldTypeEnum    = "sq.EnumField"
	FieldTypeArray   = "sq.ArrayField"
	FieldTypeBinary  = "sq.BinaryField"
	FieldTypeUUID    = "sq.UUIDField"

	FieldConstructorBoolean = "sq.NewBooleanField"
	FieldConstructorJSON    = "sq.NewJSONField"
	FieldConstructorNumber  = "sq.NewNumberField"
	FieldConstructorString  = "sq.NewStringField"
	FieldConstructorTime    = "sq.NewTimeField"
	FieldConstructorEnum    = "sq.NewEnumField"
	FieldConstructorArray   = "sq.NewArrayField"
	FieldConstructorBinary  = "sq.NewBinaryField"
	FieldConstructorUUID    = "sq.NewUUIDField"
)

sq Field Types

View Source
const (
	GoTypeInterface    = "interface{}"
	GoTypeBool         = "bool"
	GoTypeInt          = "int"
	GoTypeFloat64      = "float64"
	GoTypeString       = "string"
	GoTypeTime         = "time.Time"
	GoTypeBoolSlice    = "[]bool"
	GoTypeIntSlice     = "[]int"
	GoTypeFloat64Slice = "[]float64"
	GoTypeStringSlice  = "[]string"
	GoTypeTimeSlice    = "[]time.Time"
	GoTypeByteSlice    = "[]byte"
)

Go Types

Variables

View Source
var (
	// optionally matches a [] at the end of a type in a capturing group, includes EOL match
	ArrayPattern        = `(\[\])?$`
	FieldPatternBoolean = regexp.MustCompile(`boolean` + ArrayPattern)
	FieldPatternJSON    = regexp.MustCompile(`json` + `(?:b)?` + ArrayPattern)
	FieldPatternInt     = regexp.MustCompile(
		`(?:` +
			`smallint` +
			`|` + `oid` +
			`|` + `integer` +
			`|` + `bigint` +
			`|` + `smallserial` +
			`|` + `serial` +
			`|` + `bigserial` + `)` +
			ArrayPattern,
	)
	FieldPatternFloat = regexp.MustCompile(
		`(?:` + `decimal` +
			`|` + `numeric` +
			`|` + `real` +
			`|` + `double precision` + `)` +
			ArrayPattern,
	)
	FieldPatternString = regexp.MustCompile(
		`(?:` + `text` +
			`|` + `name` +
			`|` + `char` + `(?:\(\d+\))?` +
			`|` + `character` + `(?:\(\d+\))?` +
			`|` + `varchar` + `(?:\(\d+\))?` +
			`|` + `character varying` + `(?:\(\d+\))?` + `)` +
			ArrayPattern,
	)
	FieldPatternTime = regexp.MustCompile(
		`(?:` + `date` +
			`|` + "timestamp" +
			`|` + "time" + ")" +
			`(?: \(\d+\))?` +
			`(?: without time zone| with time zone)?` +
			ArrayPattern,
	)
	FieldPatternBinary = regexp.MustCompile(
		`bytea` +
			ArrayPattern,
	)
)

patterns used to match the types of arguments/return types of a function

Functions

func BuildFunctions

func BuildFunctions(config Config, writer io.Writer) (int, error)

func BuildTables

func BuildTables(config Config, writer io.Writer) (int, error)

Types

type Config

type Config struct {
	// (required) DB URL
	DB *sql.DB
	// Package name of the file to be generated
	Package string
	// Slice of database schemas that you want to generate tables for
	Schemas []string
	// Slice of case-insensitive table names or functions to exclude from generation
	Exclude []string
	// Used to log any skipped/unsupported column types
	Logger sqgen.Logger
}

type Function

type Function struct {
	Schema       string
	Name         string
	RawResults   string
	RawArguments string
	StructName   string
	Constructor  string
	Results      []FunctionField
	Arguments    []FunctionField
}

Function contains metadata for a plpgsql function.

func (Function) Populate

func (function Function) Populate(isDuplicate bool, overloadCount int) (*Function, error)

isDuplicate refers to if the function name is duplicated in an other schema if we have multiple function overloads within the same schema suffix the generated function with an index (starting at 1) that increments with each function overload the current overload index that we're on is the overloadCount if it is 0, means that the function is not overloaded, and we can skip adding the suffix

type FunctionField

type FunctionField struct {
	RawField    string
	Name        string
	FieldType   string
	GoType      string
	Constructor string
}

FunctionField represents a Function that is also a Field.

type FunctionsTemplateData

type FunctionsTemplateData struct {
	PackageName string
	Imports     []string
	Functions   []Function
}

type Table

type Table struct {
	Schema      string
	Name        string
	StructName  string
	RawType     string
	Constructor string
	Fields      []TableField
}

func (Table) Populate

func (table Table) Populate(config *Config, isDuplicate bool) Table

Adds constructor and struct names to table, populates Fields isDuplicate parameter indicates if there is a table in another schema with the same name

type TableField

type TableField struct {
	Name        string
	RawType     string
	Type        string
	Constructor string
}

func (TableField) Populate

func (field TableField) Populate() TableField

populate will fill in the .Type and .Constructor for a field based on the field's .RawType. For list of possible RawTypes that can appear, consult this link (Table 8.1): https://www.postgresql.org/docs/current/datatype.html.

type TablesTemplateData

type TablesTemplateData struct {
	PackageName string
	Imports     []string
	Tables      []Table
}

Jump to

Keyboard shortcuts

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