sqlmodel

package
v1.0.0-rc9 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package sqlmodel provides primitives for generating structs from database schema

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codegen

type Codegen struct {
	// Meta information
	Meta map[string]interface{}
	// Format the code
	Format bool
}

Codegen generates Golang structs from database schema

func (*Codegen) Generate

func (g *Codegen) Generate(ctx *GeneratorContext) error

Generate generates the golang structs from database schema

type Column

type Column struct {
	// Name is the name of this column
	Name string
	// Type is the database type of this column
	Type ColumnType
	// ScanType is the database type of this column
	ScanType string
	// Model representation of this column
	Model ColumnModel
}

Column represents a metadata for database column

type ColumnModel

type ColumnModel struct {
	// HasDocumentation return true if the column has documentation
	HasDocumentation bool
	// Name is the name of this column
	Name string
	// Type is the database type of this column
	Type string
	// Tage is the field tag
	Tag string
}

ColumnModel represents the field definition for given column

type ColumnType

type ColumnType struct {
	// Name of the column type
	Name string
	// Underlying is the name of the column data type (the underlying type of the domain, if applicable)
	Underlying string
	// IsPrimaryKey returns true if the column is in primary key
	IsPrimaryKey bool
	// IsNullable determines whether the column allow null values
	IsNullable bool
	// IsUnsigned returns true if the numeric type is unassigned
	IsUnsigned bool
	// CharMaxLength determines the maximum length for character types
	CharMaxLength int
	// Precision for numeric type
	Precision int
	// PrecisionScale for numeric type
	PrecisionScale int
}

ColumnType is the type of the column

func (ColumnType) DBType

func (t ColumnType) DBType() string

DBType returns the db type as string

func (ColumnType) String

func (t ColumnType) String() string

String represents the ColumnType as string

type CompositeTagBuilder

type CompositeTagBuilder []TagBuilder

CompositeTagBuilder composes multiple builders

func (CompositeTagBuilder) Build

func (composition CompositeTagBuilder) Build(column *Column) string

Build builds tags for given column

type Executor

type Executor struct {
	// Generator is the generator
	Generator Generator
	// Provider provides information the database schema
	Provider SchemaProvider
}

Executor executes the schema generation

func (*Executor) Create

func (e *Executor) Create(spec *Spec) (string, error)

Create creates a package with the generated schema sqlmodels

func (*Executor) Write

func (e *Executor) Write(w io.Writer, spec *Spec) error

Write writes the generated schema sqlmodels to a writer

type FileSystem

type FileSystem = fs.FS

FileSystem provides with primitives to work with the underlying file system

type GORMTagBuilder

type GORMTagBuilder struct{}

GORMTagBuilder builds tags for GORM mapper

func (GORMTagBuilder) Build

func (builder GORMTagBuilder) Build(column *Column) string

Build builds tags for given column

type Generator

type Generator interface {
	// Generate generates a model or script
	Generate(ctx *GeneratorContext) error
}

Generator generates the sqlmodels

type GeneratorContext

type GeneratorContext struct {
	// Template name
	Template string
	// Writer where the output will be written
	Writer io.Writer
	// Schema definition
	Schema *Schema
}

GeneratorContext is the generator's context

type JSONTagBuilder

type JSONTagBuilder struct{}

JSONTagBuilder builds JSON tags

func (JSONTagBuilder) Build

func (builder JSONTagBuilder) Build(column *Column) string

Build builds tags for given column

type ModelProvider

type ModelProvider struct {
	// Config for this provider
	Config *ModelProviderConfig
	// Provider represents the actual provider
	Provider SchemaProvider
	// TagBuilder builds struct tags from column type
	TagBuilder TagBuilder
}

ModelProvider represents the model provider

func (*ModelProvider) Close

func (m *ModelProvider) Close() error

Close closes connection to the db

func (*ModelProvider) Schema

func (m *ModelProvider) Schema(name string, tables ...string) (*Schema, error)

Schema returns the schema definition

func (*ModelProvider) Tables

func (m *ModelProvider) Tables(schema string) ([]string, error)

Tables returns all tables for this schema

type ModelProviderConfig

type ModelProviderConfig struct {
	// Package name
	Package string
	// UseNamedParams determines whether to use named params
	UseNamedParams bool
	// InlcudeDoc determines whether to include documentation
	InlcudeDoc bool
}

ModelProviderConfig is the ModelProvider's config

type MySQLProvider

type MySQLProvider struct {
	// DB is a connection to MySQL database
	DB Querier
}

MySQLProvider represents a metadata provider for MySQL

func (*MySQLProvider) Close

func (m *MySQLProvider) Close() error

Close closes connection to the db

func (*MySQLProvider) Schema

func (m *MySQLProvider) Schema(schema string, names ...string) (*Schema, error)

Schema returns the schema definition

func (*MySQLProvider) Tables

func (m *MySQLProvider) Tables(schema string) ([]string, error)

Tables returns all tables for this schema

type NoopTagBuilder

type NoopTagBuilder struct{}

NoopTagBuilder composes multiple builders

func (NoopTagBuilder) Build

func (composition NoopTagBuilder) Build(column *Column) string

Build builds tags for given column

type PostgreSQLProvider

type PostgreSQLProvider struct {
	// DB is a connection to PostgreSQL database
	DB Querier
}

PostgreSQLProvider represents a metadata provider for PostgreSQL

func (*PostgreSQLProvider) Close

func (m *PostgreSQLProvider) Close() error

Close closes connection to the db

func (*PostgreSQLProvider) Schema

func (m *PostgreSQLProvider) Schema(schema string, names ...string) (*Schema, error)

Schema returns the schema definition

func (*PostgreSQLProvider) Tables

func (m *PostgreSQLProvider) Tables(schema string) ([]string, error)

Tables returns all tables for this schema

type Querier

type Querier interface {
	// Query performs a query and returns a set of rows
	Query(query string, args ...interface{}) (*sql.Rows, error)
	// QueryRow performs a query and returns a row
	QueryRow(query string, args ...interface{}) *sql.Row
	// Close closes the connection
	Close() error
}

Querier executes queries

type SQLXTagBuilder

type SQLXTagBuilder struct{}

SQLXTagBuilder builds tags for SQLX mapper

func (SQLXTagBuilder) Build

func (builder SQLXTagBuilder) Build(column *Column) string

Build builds tags for given column

type SQLiteProvider

type SQLiteProvider struct {
	// DB is a connection to PostgreSQL database
	DB Querier
}

SQLiteProvider represents a metadata provider for SQLite

func (*SQLiteProvider) Close

func (m *SQLiteProvider) Close() error

Close closes connection to the db

func (*SQLiteProvider) Schema

func (m *SQLiteProvider) Schema(schema string, names ...string) (*Schema, error)

Schema returns the schema definition

func (*SQLiteProvider) Tables

func (m *SQLiteProvider) Tables(schema string) ([]string, error)

Tables returns all tables for this schema

type Schema

type Schema struct {
	// Name of the schema
	Name string
	// Driver name
	Driver string
	// Tables are the associated tables
	Tables []Table
	// IsDefault returns if this schema is default
	IsDefault bool
	// Model for this schema
	Model SchemaModel
}

Schema represents a database schema

type SchemaModel

type SchemaModel struct {
	// Package name
	Package string
	// HasDocumentation return true if the schema has documentation
	HasDocumentation bool
}

SchemaModel represents the schema's model

type SchemaProvider

type SchemaProvider interface {
	// Tables returns all tables for this schema
	Tables(schema string) ([]string, error)
	// Schema returns the schema definition
	Schema(schema string, tables ...string) (*Schema, error)
	// Close closes connection to the db
	Close() error
}

SchemaProvider provides a metadata for database schema

type Spec

type Spec struct {
	// Filename of the spec
	Filename string
	// Template name
	Template string
	// FileSystem is the underlying file system
	FileSystem WriteFileSystem
	// Schema is the database schema name
	Schema string
	// Tables is the list of the desired tables from the database schema
	Tables []string
	// IgnoreTables ecludes the those tables from generation
	IgnoreTables []string
}

Spec specifies the generation options

type Table

type Table struct {
	// Name of this table
	Name string
	// Driver name
	Driver string
	// Model representation of this table
	Model TableModel
	// Columns of this table
	Columns []Column
}

Table represents a table name and its schema

type TableModel

type TableModel struct {
	// HasDocumentation return true if the table has documentation
	HasDocumentation bool
	// Type of this model
	Type string
	// Package name
	Package string
	// InsertRoutine is the insert routine name
	InsertRoutine string
	// InsertColumns are the columns
	InsertColumns string
	// InsertValues are the values to be inserted
	InsertValues string
	// SelectByPKRoutine is the select by primary key routine
	SelectByPKRoutine string
	// SelectAllRoutine is the select-all's routine
	SelectAllRoutine string
	// DeleteByPkRoutine is the delete by primary key routine
	DeleteByPKRoutine string
	// UpdateByPKRoutine is the update by primary key routine
	UpdateByPKRoutine string
	// UpdateByPKColumns is the columns for update condition
	UpdateByPKColumns string
	// PrimaryKeyCondition is a where clause condition
	PrimaryKeyCondition string
	// PrimaryKeyParams is the primary key args
	PrimaryKeyParams string
	// PrimaryKeyEntityParams is the primary key args
	PrimaryKeyEntityParams string
	// PrimaryKeyArgs is the primary key args
	PrimaryKeyArgs string
	// PrimaryKey is the map of primary key args
	PrimaryKey map[string]string
}

TableModel represents the model definition

type TagBuilder

type TagBuilder interface {
	// Build returns a struct tag from column type
	Build(column *Column) string
}

TagBuilder builds tags from column type

type TypeDef

type TypeDef struct {
	// Type name
	Type string
	// NullableType name
	NullableType string
}

TypeDef represents a type definition

func (*TypeDef) As

func (t *TypeDef) As(nullable bool) string

As returns the type name if nullable is true, otherwise the nullable type

type ValidateTagBuilder

type ValidateTagBuilder struct{}

ValidateTagBuilder builds JSON tags

func (ValidateTagBuilder) Build

func (builder ValidateTagBuilder) Build(column *Column) string

Build builds tags for given column

type WriteFileSystem

type WriteFileSystem interface {
	FileSystem

	// OpenFile opens a new file
	OpenFile(string, int, fs.FileMode) (fs.File, error)
}

WriteFileSystem represents a wriable file system

type XMLTagBuilder

type XMLTagBuilder struct{}

XMLTagBuilder builds XML tags

func (XMLTagBuilder) Build

func (builder XMLTagBuilder) Build(column *Column) string

Build builds tags for given column

Jump to

Keyboard shortcuts

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