ddl

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: MIT Imports: 7 Imported by: 0

README

DDL-Parser

DDL-Parser is a simple go module to parse all table columns from a running database and transform them into a generic go struct.

The following SQL databases are supported and tested:

  • MariaDB 10.6
  • Oracle 21

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {

	// Unique column name within a table
	Name string

	// Generic data type definition
	Type DataType

	// Internal name of the data type (with lenght)
	InternalType string

	// Weather this column is a primary key of the table
	PrimaryKey bool

	// Weather this column references another table
	ForeignKey bool
	// Table to which this column has a reference to
	ForeignKeyColumn ForeignColumn

	// Weather this column can be null
	CanBeNull bool

	// A default value of the column
	DefaultValue sql.NullString

	// Comment of this column
	Comment string

	// Extras
	Extras Columner
}

Column of a table. Every database system has a own column struct that embeds this type and extends it with database specific informations

type Columner

type Columner interface {

	// GetExtraInfos returns a string with additional properties that is passed
	// to the struct tag "Db" when using the struct generator
	GetExtraInfos() string

	// GetSpecificInfos returns the underlaying
	GetSpecificInfos() any
}

Columner returns additonal informations to a column that are specific for a SQL system

type DataType

type DataType string

DataType is a generic data type of a db type

const (
	StringType  DataType = "String"
	IntType     DataType = "Integer"
	DoubleType  DataType = "Double"
	DateType    DataType = "Date"
	GeoType     DataType = "Geo"
	UnknownType DataType = "Unknown"
)

type DbSystem

type DbSystem interface {

	// GetTable returns a single table identified by the schema and name
	GetTable(schema, name string) (*Table, error)

	// GeTables returns a list of tables that are present in the provided schema
	// or database
	GetTables(schema string) ([]*Table, error)
}

DbSystem has to be implemented by every single database system that is supported by the module to fetch a list of tables and columns

func NewMariaDb

func NewMariaDb(db *sql.DB) DbSystem

NewMariaDb initializes a new database parser for a MariaDB database

type ForeignColumn

type ForeignColumn struct {

	// Name of the referenced table
	Name string

	// Schema or database the table belongs to
	Schema string

	// Name of the column that is referenced
	Column string
}

ForeignColumn contains information to which the column points to with an foreign key

type Location

type Location struct {
	Longitude float64
	Latitude  float64
}

Location is a database type that stores a geographic point on the earth with a longitude and latitude. It can currently only used for Mariadb!

func (*Location) Scan

func (g *Location) Scan(src interface{}) error

Scan handles the scanning of the custom location type for a MariaDb datbase

func (Location) Value

func (g Location) Value() (driver.Value, error)

Value transforms the latitude and longitude into the "WKB" format the database understands. See https://dev.mysql.com/doc/refman/8.0/en/gis-data-formats.html

type Mariadb

type Mariadb struct {
	// contains filtered or unexported fields
}

Mariadb implements "DbSystem" for a MariaDB database

func (*Mariadb) GetDataType

func (s *Mariadb) GetDataType(internalType string) DataType

func (*Mariadb) GetTable

func (s *Mariadb) GetTable(schema, name string) (*Table, error)

func (*Mariadb) GetTables

func (s *Mariadb) GetTables(schema string) ([]*Table, error)

type MariadbColumn

type MariadbColumn struct {
	*Column

	// Weather this column has the auto_increment flag
	AutoIncrement bool

	// The character lenght or numeric precision
	DataTypeLenght int

	// The internal column key like 'UNI' or 'PRI'
	KeyType MariadbKeyType
}

func (*MariadbColumn) GetExtraInfos

func (c *MariadbColumn) GetExtraInfos() string

func (*MariadbColumn) GetSpecificInfos

func (c *MariadbColumn) GetSpecificInfos() any

type MariadbKeyType

type MariadbKeyType string
const (
	// Primary key
	MariadbKeyPrimary MariadbKeyType = "PRI"
	// Unique index
	MariadbKeyUnique MariadbKeyType = "UNI"
	// Nonunique index
	MariadbKeyMultipleIndex MariadbKeyType = "MUL"
)

type OracleColumn

type OracleColumn struct {
	*Column

	// Weather this column has the auto_increment flag
	AutoIncrement bool

	// Character lenght or numeric precision on the LEFT side
	// of the dot
	DataTypeLenght int

	// Decimal precision on the RIGHT side of the dot
	Scale int
}

func (*OracleColumn) GetExtraInfos

func (c *OracleColumn) GetExtraInfos() string

func (*OracleColumn) GetSpecificInfos

func (c *OracleColumn) GetSpecificInfos() any

type OracleDb

type OracleDb struct {
	// contains filtered or unexported fields
}

OracleDb implements "DbSystem" for an oracle database

func NewOracleDb

func NewOracleDb(db *sql.DB) *OracleDb

NewMariaDb initializes a new database parser for an oracle database

func (*OracleDb) GetDataType

func (s *OracleDb) GetDataType(internalType string, col *OracleColumn) DataType

func (*OracleDb) GetTable

func (s *OracleDb) GetTable(schema, name string) (*Table, error)

func (*OracleDb) GetTables

func (s *OracleDb) GetTables(schema string) ([]*Table, error)

func (*OracleDb) GetTablesByType

func (s *OracleDb) GetTablesByType(schema string, typ OracleTableType) ([]*Table, error)

type OracleTableType

type OracleTableType string
const (
	OracleTable OracleTableType = "TABLE"
	OracleView  OracleTableType = "VIEW"
)

type Table

type Table struct {

	// Name of the table
	Name string

	// Schema or database the table belongs to
	Schema string

	// List of columns the table has
	Columns []*Column
}

Table represents a logical table on the database

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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