dbsql2go

package module
v0.0.0-...-822da2f Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2017 License: Apache-2.0 Imports: 6 Imported by: 0

README

dbsql2go

GoDocBuild Status A package for generating Go code for DB tables.

Work In Progress

The API will change as support for additional RDBMSs are added. At this point, using this package directly is not recommended.

CLI

Use the dbsql2go CLI to generate Go code from a database.

Supported RDBMSs

MySQL is currently the only supported RDBMS.

Documentation

Overview

package dbsql2go enables the gathering of schema data about a database from RDBMSs system catalogs and the generation of Go code from this data.

Index

Constants

View Source
const (
	LowerExclusive = "exclusive"
	TitleExclusive = "Exclusive"
	LowerInclusive = "inclusive"
	TitleInclusive = "Inclusive"
	LF             = 0x0A // Line Feed (NL) byte value
)

Variables

View Source
var (
	SelectSQL *template.Template // Template to SELECT from a single table with only ANDs
	// SelectAndOrSQL allows for between and not between type statements on a single
	// table using the supplied conditional operators that correspond with the column
	// of the same index. BETWEEN is not used so that exclusive and inclusive can be
	// supported using: <, >, =, <=, >=. Since this template will generate SQL
	// that may return more than one row of data, these should be used in funcs that
	// return a slice of results; not as a method on a table.
	SelectAndOrSQL *template.Template
	DeleteSQL      *template.Template // Template to DELETE from a single table with only ANDs
	InsertSQL      *template.Template // Template to INSERT into a single table with only ANDs
	UpdateSQL      *template.Template // Template to UPDATE a row in a single table with only ANDs

	// Comment fragments
	SelectAndOrWhereComment *template.Template // The WHERE clause comment fragment for AndOR SQL funcs.
)

Templates

Functions

func StringToComments

func StringToComments(s string, l int) (comment string, err error)

StringToComments creates line comments of length l out of a string. The resulting comment block is returned. If s is an empty string, an empty string is returned. If an error occurs, the error is returned, along with an empty string.

Types

type Constraint

type Constraint struct {
	Type       ConstraintType // The key or constraint type
	Name       string         // Name of key
	Table      string         // the table to which this key belongs.
	Columns    []string       // the columns that this key/constraint are on, in order.
	Fields     []string       // the Go struct field names corresponding to the table's column names.
	RefTable   string         // Referred to table for Foreign Keys
	RefColumns []string       // Referred to columns, in order, for Foreign Keys
	RefFields  []string       // the Go struct field names corresponding to the table's column names.
}

Constraint holds information about a table's constraints, e.g. Primary Key.

type ConstraintType

type ConstraintType int

ConstarintType is the type of the table constraint.

const (
	UnknownConstraint ConstraintType = iota
	PK
	FK
	Unique
)

func ParseConstraintType

func ParseConstraintType(s string) (ConstraintType, error)

func (ConstraintType) String

func (i ConstraintType) String() string

type DBType

type DBType int
const (
	Unsupported DBType = iota
	MySQL
)

func ParseDBType

func ParseDBType(s string) (DBType, error)

func (DBType) String

func (i DBType) String() string

type DBer

type DBer interface {
	Get() error
	GetTables() error
	Tables() []Tabler
	GetIndexes() error
	GetConstraints() error
	GetViews() error
	Views() []Viewer
	UpdateTableConstraints() error
	UpdateTableIndexes()
}

type Index

type Index struct {
	Type    string   // type of Index
	Primary bool     // if the Index is a primary key
	Name    string   // Name of Index
	Table   string   // Index's table
	Columns []string // Index Columns, in order.
}

Index holds information about a given index.

type Indexer

type Indexer interface {
	Name() string // Just so that there's semething to fulfill until this gets fleshed out further.
}

Indexer

type TableSQL

type TableSQL struct {
	Table              string   // the table from which to SELECT
	Columns            []string // the columns that will be SELECTed
	WhereColumns       []string // the where column names
	WhereComparisonOps []string // the comparison operator for the corresponding column index
	WhereConditions    []string // The conditional operator for Column pairs.
}

TableSQL is used to describe basic components of a sql statement for a single table. Everything specified for the WHERE clause is assumed to be an AND. This is mainly meant for basic INSERT, UPDATE, SELECT, DELETE statements on a table.

type Tabler

type Tabler interface {
	//Columns() []Column
	Name() string
	Schema() string
	Collation() string
	Definition(io.Writer) error
	Go(io.Writer) error
	GoFmt(io.Writer) error
	ColumnNames() []string
	NonPKColumnNames() []string
	Indexes() []Index
	Constraints() []Constraint
	IsView() bool // If this is actually a view
	PK() *Constraint
	StructName() string
}

Tabler

type UnknownConstraintErr

type UnknownConstraintErr struct {
	Value string
}

func (UnknownConstraintErr) Error

func (u UnknownConstraintErr) Error() string

type UnsupportedDBErr

type UnsupportedDBErr struct {
	Value string
}

func (UnsupportedDBErr) Error

func (u UnsupportedDBErr) Error() string

type Viewer

type Viewer interface {
	Name() string // Just so that there's semething to fulfill until this gets fleshed out further.
}

Viewer

Directories

Path Synopsis
cmd
dbsql2go
dbsql2go is a CLI tool to generate Go struct definitions from tables in a database.
dbsql2go is a CLI tool to generate Go struct definitions from tables in a database.
package mysql gathers data about a database from MySQL's information schema and generates Go code using this data.
package mysql gathers data about a database from MySQL's information schema and generates Go code using this data.

Jump to

Keyboard shortcuts

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