format

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDatabaseConnection

func CreateDatabaseConnection(
	ctx context.Context,
	dsn string,
) (*sqlx.DB, error)

CreateDatabaseConnection creates a connection to the database. The connection is long lived and should only be created once per process.

func CreateTableMarkdown

func CreateTableMarkdown(tableName string, comment string, columns []ColumnDescription, indexes []LogicalIndex, foreignKeys ForeignDescriptions) string

CreateTableMarkdown takes the name of a table in a database and a list of ColumnDescription and returns a formatted markdown table with the corresponding data.

func WriteToFile

func WriteToFile(
	filename string,
	markdown string,
) error

WriteToFile takes a filename and a markdown string and writes the markdown to the file. If the file is annotated with markdown comments, the markdown will be inserted in between the comments. e.g.

fake markdown

<!-- sql-gen-doc BEGIN --> markdown will go here! <!-- sql-gen-doc END -->"

An error is returned if the file cannot be written.

Types

type ColumnDescription

type ColumnDescription struct {
	Field      string         `db:"Field"`
	Type       string         `db:"Type"`
	Null       string         `db:"Null"`
	Key        string         `db:"Key"`
	Default    sql.NullString `db:"Default"`
	Extra      string         `db:"Extra"`
	Comment    string         `db:"Comment"`
	Collation  sql.NullString `db:"Collation"`
	Privileges string         `db:"Privileges"`
}

ColumnDescription contains all the data rendered about a sql column by the DESCRIBE command.

func DescribeTable

func DescribeTable(
	ctx context.Context,
	db *sqlx.DB,
	tableName string,
) ([]ColumnDescription, error)

DescribeTable queries the database for information about the specified table. The result is scanned into a ColumnDescription struct.

type ForeignDescription added in v0.6.0

type ForeignDescription struct {
	TableName            string `db:"table_name"`
	ColumnName           string `db:"column_name"`
	ConstraintName       string `db:"constraint_name"`
	ReferencedTableName  string `db:"referenced_table_name"`
	ReferencedColumnName string `db:"referenced_column_name"`
}

ForeignDescription is generated from INFORMATION_SCHEMA.KEY_COLUMN_USAGE

type ForeignDescriptions added in v0.6.0

type ForeignDescriptions []ForeignDescription

ForeignDescriptions is a set of foreign key descriptions

func GetForeignKeyDescriptions added in v0.6.0

func GetForeignKeyDescriptions(
	ctx context.Context,
	db *sqlx.DB,
	tableName string,
) (ForeignDescriptions, error)

GetForeignKeyDescriptions queries INFORMATION_SCHEMA.KEY_COLUMN_USAGE table about references information

type GetTablesRow added in v0.3.0

type GetTablesRow struct {
	Name    string
	Comment string
}

GetTablesRow see GetTablesRow

func GetTables added in v0.3.0

func GetTables(ctx context.Context, db *sqlx.DB, dbName string) ([]GetTablesRow, error)

GetTables queries the database and returns a list of the tables that are present in the database.

type IndexDescription added in v0.2.0

type IndexDescription struct {
	Table      string         `db:"Table"`
	NonUnique  bool           `db:"Non_unique"`
	KeyName    string         `db:"Key_name"`
	SeqInIndex int            `db:"Seq_in_index"`
	ColumnName sql.NullString `db:"Column_name"`
	Comment    sql.NullString `db:"Comment"`
	Expression sql.NullString `db:"Expression"`

	// Not used (yet)
	Collation    sql.NullString `db:"Collation"`
	Cardinality  sql.NullString `db:"Cardinality"`
	SubPart      sql.NullString `db:"Sub_part"`
	Packed       sql.NullString `db:"Packed"`
	Null         sql.NullString `db:"Null"`
	IndexType    sql.NullString `db:"Index_type"`
	IndexComment sql.NullString `db:"Index_comment"`
	Visible      sql.NullString `db:"Visible"`
	Clustered    sql.NullString `db:"Clustered"`
}

IndexDescription contains all the data known about a specific index. Note that some indexes may be related (e.g. in cases of clustered indexes).

type IndexDescriptions added in v0.2.0

type IndexDescriptions []IndexDescription

IndexDescriptions is a set of index descriptions

func GetIndexDescriptions added in v0.2.0

func GetIndexDescriptions(
	ctx context.Context,
	db *sqlx.DB,
	tableName string,
) (IndexDescriptions, error)

GetIndexDescriptions queries the database for information about the specified table. The result is scanned into a IndexDescription struct.

func (IndexDescriptions) ConvertToLogicalIndexes added in v0.2.0

func (descs IndexDescriptions) ConvertToLogicalIndexes() ([]LogicalIndex, error)

ConvertToLogicalIndexes converts raw index descriptions into a aggregated type.

type LogicalIndex added in v0.2.0

type LogicalIndex struct {
	Table                     string
	NonUnique                 bool
	KeyName                   string
	IndexedColumnNamesOrdered []string
	Comment                   string
	Expression                string
}

LogicalIndex defines a "logical index -- e.g. what will make the most sense to humans"

Jump to

Keyboard shortcuts

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