db

package
v0.0.0-...-326c6b9 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinMysqlVersion = "5.7.6"
	DriverName      = "mysql"
)
View Source
const CurrentDB = "tidb_5_2"
View Source
const TypeFK = "FOREIGN KEY"

Variables

This section is empty.

Functions

func InitLogger

func InitLogger() (*zap.SugaredLogger, error)

func ParseReferencedTables

func ParseReferencedTables(src string) []string

ParseReferencedTables parse DDL of view table and list tables referenced by view table.

Types

type Column

type Column struct {
	Name            string      `json:"name"`
	Type            string      `json:"type"`
	Nullable        bool        `json:"nullable"`
	Default         *string     `json:"default"`
	Comment         *string     `json:"comment"`
	ExtraDef        *string     `json:"extra_def,omitempty" yaml:"extraDef,omitempty"`
	ParentRelations []*Relation `json:"-"`
	ChildRelations  []*Relation `json:"-"`
}

Column is the struct for table column

type Constraint

type Constraint struct {
	Name              string   `json:"name"`
	Type              string   `json:"type"`
	Def               string   `json:"def"`
	Table             *string  `json:"table"`
	ReferencedTable   *string  `json:"referenced_table" yaml:"referencedTable"`
	Columns           []string `json:"columns"`
	ReferencedColumns []string `json:"referenced_columns" yaml:"referencedColumns"`
	Comment           string   `json:"comment"`
}

Constraint is the struct for database constraint

type DB

type DB struct {
	// Name for database driver, such as mysql
	Name    string `json:"name"`
	Version string `json:"version"`
	URL     string `json:"url"`

	Schema *Schema `json:"schema"`
	// contains filtered or unexported fields
}

DB stands for a connection to database, including current schema The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.

func Open

func Open(ds *Datasource) (*DB, error)

Open takes a dataSourceName like "root:mypass@tcp(127.0.0.1:33308)/testdb?parseTime=true"

func (*DB) CheckSchema

func (db *DB) CheckSchema() error

CheckSchema set schema

func (*DB) CheckVersion

func (db *DB) CheckVersion() error

CheckVersion set version and supportGeneratedColumn

func (*DB) Close

func (db *DB) Close() error

func (*DB) Columns

func (db *DB) Columns(tableName string) ([]*Column, error)

func (*DB) Constraints

func (db *DB) Constraints(tableName string) ([]*Constraint, error)

func (*DB) FindTableDDL

func (db *DB) FindTableDDL(tableName string, tableType TableType) (string, error)

func (*DB) Indexes

func (db *DB) Indexes(tableName string) ([]*Index, error)

Indexes get a table's indexes

func (*DB) Query

func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*DB) QueryRowx

func (db *DB) QueryRowx(query string, args ...interface{}) (*sqlx.Row, error)

func (*DB) Queryx

func (db *DB) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)

func (*DB) ReferencedTables

func (db *DB) ReferencedTables(tb *Table) error

ReferencedTables get referenced tables for view

func (*DB) Relation

func (db *DB) Relation(tb *Table, c *Constraint) (*Relation, error)

Relation get one relation from one constraint

func (*DB) Table

func (db *DB) Table(name string) (*Table, error)

func (*DB) TableRelations

func (db *DB) TableRelations(tb *Table) ([]*Relation, error)

TableRelations get table's relations from its constraints

func (*DB) Tables

func (db *DB) Tables(pattern string) ([]*TableInfo, error)

Tables get table infos using query like "%pattern%" in table name or table comment

func (*DB) Triggers

func (db *DB) Triggers(tableName string) ([]*Trigger, error)

type Datasource

type Datasource struct {
	UserName string
	Passwd   string
	Host     string
	Port     int
	DBName   string
	Comment  string
}

func PredefinedDatasource

func PredefinedDatasource(comment string) (*Datasource, error)

func (*Datasource) ConnectString

func (ds *Datasource) ConnectString() string

ConnectString returns a connection string based on the parameters it's given This would normally also contain the password, however we're not using one

type Index

type Index struct {
	Name    string   `json:"name"`
	Def     string   `json:"def"`
	Table   *string  `json:"table"`
	Columns []string `json:"columns"`
	Comment string   `json:"comment"`
}

Index is the struct for database index

type Relation

type Relation struct {
	Table         *Table    `json:"table"`
	Columns       []*Column `json:"columns"`
	ParentTable   *Table    `json:"parent_table" yaml:"parentTable"`
	ParentColumns []*Column `json:"parent_columns" yaml:"parentColumns"`
	Def           string    `json:"def"`
	Virtual       bool      `json:"virtual"`
}

Relation is the struct for table relation

type Schema

type Schema struct {
	Name string `json:"name"`
}

Schema is a named collection of tables. A schema can also contain views, indexes, sequences, data types, operators, and functions. Schemas are analogous to directories at the operating system level, except schemas cannot be nested. For mysql, schema is equal to database. For postgres, a database could have multi schemas.

type Table

type Table struct {
	TableInfo
	Columns     []*Column     `json:"columns"`
	Indexes     []*Index      `json:"indexes"`
	Constraints []*Constraint `json:"constraints"`
	Triggers    []*Trigger    `json:"triggers"`
	// only used for view
	ReferencedTables []*Table `json:"referenced_tables,omitempty" yaml:"referencedTables,omitempty"`
}

Table is the struct for database table

func (*Table) CollectTablesAndRelations

func (t *Table) CollectTablesAndRelations(distance int, root bool) ([]*Table, []*Relation, error)

func (*Table) FindColumnByName

func (t *Table) FindColumnByName(name string) (*Column, error)

FindColumnByName find column by column name

func (*Table) FindConstrainsByColumnName

func (t *Table) FindConstrainsByColumnName(name string) []*Constraint

FindConstrainsByColumnName find constraint by column name

func (*Table) FindConstraintByName

func (t *Table) FindConstraintByName(name string) (*Constraint, error)

FindConstraintByName find constraint by constraint name

func (*Table) FindIndexByName

func (t *Table) FindIndexByName(name string) (*Index, error)

FindIndexByName find index by index name

func (*Table) FindTriggerByName

func (t *Table) FindTriggerByName(name string) (*Trigger, error)

FindTriggerByName find trigger by trigger name

func (*Table) HasColumnWithExtraDef

func (t *Table) HasColumnWithExtraDef() bool

type TableInfo

type TableInfo struct {
	Name      string     `json:"name"`
	Type      TableType  `json:"type"`
	Comment   *string    `json:"comment"`
	Def       string     `json:"def"`
	CreatedAt *time.Time `json:"createdAt"`
	External  bool       `json:"-"` // Table external to the schema
}

type TableType

type TableType = string
const (
	BaseTable TableType = "BASE TABLE"
	View      TableType = "VIEW"
)

type Trigger

type Trigger struct {
	Name    string `json:"name"`
	Def     string `json:"def"`
	Comment string `json:"comment"`
}

Trigger is the struct for database trigger

Jump to

Keyboard shortcuts

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