database

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	OrdinalPosition        int            `db:"ordinal_position"`
	Name                   string         `db:"column_name"`
	DataType               string         `db:"data_type"`
	DefaultValue           sql.NullString `db:"column_default"`
	IsNullable             string         `db:"is_nullable"`
	CharacterMaximumLength sql.NullInt64  `db:"character_maximum_length"`
	NumericPrecision       sql.NullInt64  `db:"numeric_precision"`
	ColumnKey              string         `db:"column_key"`      // mysql specific
	Extra                  string         `db:"extra"`           // mysql specific
	ConstraintName         sql.NullString `db:"constraint_name"` // pg specific
	ConstraintType         sql.NullString `db:"constraint_type"` // pg specific
}

Column stores information about a column.

type Database

type Database interface {
	DSN() string
	Connect() error
	Close() error

	GetTables(tables ...string) ([]*Table, error)
	PrepareGetColumnsOfTableStmt() error
	GetColumnsOfTable(table *Table) error

	IsPrimaryKey(column Column) bool
	IsAutoIncrement(column Column) bool
	IsNullable(column Column) bool

	GetStringDatatypes() []string
	IsString(column Column) bool

	GetTextDatatypes() []string
	IsText(column Column) bool

	GetIntegerDatatypes() []string
	IsInteger(column Column) bool

	GetFloatDatatypes() []string
	IsFloat(column Column) bool

	GetTemporalDatatypes() []string
	IsTemporal(column Column) bool
}

Database interface for the concrete databases.

func New

func New(s *settings.Settings) Database

New creates a new Database based on the given type in the settings.

type GeneralDatabase

type GeneralDatabase struct {
	GetColumnsOfTableStmt *sqlx.Stmt
	*sqlx.DB
	*settings.Settings
	// contains filtered or unexported fields
}

GeneralDatabase represents a base "class" database - for all other concrete databases it implements partly the Database interface.

func (*GeneralDatabase) Close

func (gdb *GeneralDatabase) Close() error

Close closes the database connection.

func (*GeneralDatabase) Connect

func (gdb *GeneralDatabase) Connect(dsn string) (err error)

Connect establishes a connection to the database with the given DSN. It pings the database to ensure it is reachable.

func (*GeneralDatabase) IsNullable

func (gdb *GeneralDatabase) IsNullable(column Column) bool

IsNullable returns true if the column is a nullable column.

type MySQL

type MySQL struct {
	*GeneralDatabase
	// contains filtered or unexported fields
}

MySQL implements the Database interface with help of GeneralDatabase.

func NewMySQL

func NewMySQL(s *settings.Settings) *MySQL

NewMySQL creates a new MySQL database.

func (*MySQL) Connect

func (mysql *MySQL) Connect() error

Connect connects to the database by the given data source name (dsn) of the concrete database.

func (*MySQL) DSN

func (mysql *MySQL) DSN() string

DSN creates the DSN String to connect to this database.

func (*MySQL) GetColumnsOfTable

func (mysql *MySQL) GetColumnsOfTable(table *Table) (err error)

GetColumnsOfTable executes the statement for retrieving the columns of a specific table for a given database.

func (*MySQL) GetFloatDatatypes

func (mysql *MySQL) GetFloatDatatypes() []string

GetFloatDatatypes returns the float datatypes for the MySQL database.

func (*MySQL) GetIntegerDatatypes

func (mysql *MySQL) GetIntegerDatatypes() []string

GetIntegerDatatypes returns the integer datatypes for the MySQL database.

func (*MySQL) GetStringDatatypes

func (mysql *MySQL) GetStringDatatypes() []string

GetStringDatatypes returns the string datatypes for the MySQL database.

func (*MySQL) GetTables

func (mysql *MySQL) GetTables(tables ...string) ([]*Table, error)

GetTables gets all tables for a given database by name.

func (*MySQL) GetTemporalDatatypes

func (mysql *MySQL) GetTemporalDatatypes() []string

GetTemporalDatatypes returns the temporal datatypes for the MySQL database.

func (*MySQL) GetTextDatatypes

func (mysql *MySQL) GetTextDatatypes() []string

GetTextDatatypes returns the text datatypes for the MySQL database.

func (*MySQL) IsAutoIncrement

func (mysql *MySQL) IsAutoIncrement(column Column) bool

IsAutoIncrement checks if the column is an auto_increment column.

func (*MySQL) IsFloat

func (mysql *MySQL) IsFloat(column Column) bool

IsFloat returns true if colum is of type float for the MySQL database.

func (*MySQL) IsInteger

func (mysql *MySQL) IsInteger(column Column) bool

IsInteger returns true if colum is of type integer for the MySQL database.

func (*MySQL) IsPrimaryKey

func (mysql *MySQL) IsPrimaryKey(column Column) bool

IsPrimaryKey checks if the column belongs to the primary key.

func (*MySQL) IsString

func (mysql *MySQL) IsString(column Column) bool

IsString returns true if the colum is of type string for the MySQL database.

func (*MySQL) IsTemporal

func (mysql *MySQL) IsTemporal(column Column) bool

IsTemporal returns true if colum is of type temporal for the MySQL database.

func (*MySQL) IsText

func (mysql *MySQL) IsText(column Column) bool

IsText returns true if colum is of type text for the MySQL database.

func (*MySQL) PrepareGetColumnsOfTableStmt

func (mysql *MySQL) PrepareGetColumnsOfTableStmt() (err error)

PrepareGetColumnsOfTableStmt prepares the statement for retrieving the columns of a specific table for a given database.

type Oracle

type Oracle struct {
	*GeneralDatabase
	// contains filtered or unexported fields
}

Oracle implements the Database interface with help of GeneralDatabase.

func NewOracle

func NewOracle(s *settings.Settings) *Oracle

NewOracle creates a new Oracle database handler.

func (*Oracle) Close

func (o *Oracle) Close() error

Close closes the database connection.

func (*Oracle) Connect

func (o *Oracle) Connect() error

Connect connects to the database using the DSN generated above.

func (*Oracle) DSN

func (o *Oracle) DSN() string

DSN creates the DSN string to connect to Oracle. Example format: user/password@host:port/servicename Adjust as needed for your driver or TNS usage.

func (*Oracle) GetColumnsOfTable

func (o *Oracle) GetColumnsOfTable(table *Table) error

GetColumnsOfTable executes the prepared statement to retrieve column metadata.

func (*Oracle) GetFloatDatatypes

func (o *Oracle) GetFloatDatatypes() []string

GetFloatDatatypes returns which datatypes Oracle generally treats as "floating".

func (*Oracle) GetIntegerDatatypes

func (o *Oracle) GetIntegerDatatypes() []string

GetIntegerDatatypes returns which datatypes Oracle generally treats as "integer".

func (*Oracle) GetStringDatatypes

func (o *Oracle) GetStringDatatypes() []string

GetStringDatatypes returns which datatypes Oracle generally treats as "string".

func (*Oracle) GetTables

func (o *Oracle) GetTables(tables ...string) ([]*Table, error)

GetTables retrieves all tables for the current (or specified) schema. If `tables...` is provided, it filters by those table names.

func (*Oracle) GetTemporalDatatypes

func (o *Oracle) GetTemporalDatatypes() []string

GetTemporalDatatypes returns which datatypes Oracle generally treats as "temporal".

func (*Oracle) GetTextDatatypes

func (o *Oracle) GetTextDatatypes() []string

GetTextDatatypes returns which datatypes Oracle generally treats as "text/clob".

func (*Oracle) IsAutoIncrement

func (o *Oracle) IsAutoIncrement(column Column) bool

IsAutoIncrement checks if a column is auto-increment. Oracle does not have the same concept; typically sequences & triggers are used, so this is false.

func (*Oracle) IsFloat

func (o *Oracle) IsFloat(column Column) bool

IsFloat checks if a column is treated as a floating-point type in Oracle.

func (*Oracle) IsInteger

func (o *Oracle) IsInteger(column Column) bool

IsInteger checks if a column is treated as an integer type in Oracle.

func (*Oracle) IsNullable

func (o *Oracle) IsNullable(column Column) bool

IsNullable returns whether the column is nullable ('Y' for Oracle).

func (*Oracle) IsPrimaryKey

func (o *Oracle) IsPrimaryKey(column Column) bool

IsPrimaryKey checks if a column belongs to the primary key.

func (*Oracle) IsString

func (o *Oracle) IsString(column Column) bool

IsString checks if a column is treated as a "string" type in Oracle.

func (*Oracle) IsTemporal

func (o *Oracle) IsTemporal(column Column) bool

IsTemporal checks if a column is treated as a temporal/date/time type in Oracle.

func (*Oracle) IsText

func (o *Oracle) IsText(column Column) bool

IsText checks if a column is treated as a "text/clob" type in Oracle.

func (*Oracle) PrepareGetColumnsOfTableStmt

func (o *Oracle) PrepareGetColumnsOfTableStmt() error

PrepareGetColumnsOfTableStmt prepares a statement to retrieve columns (including information about primary keys) for a specific table.

type Postgresql

type Postgresql struct {
	*GeneralDatabase
	// contains filtered or unexported fields
}

Postgresql implements the Database interface with help of GeneralDatabase.

func NewPostgresql

func NewPostgresql(s *settings.Settings) *Postgresql

NewPostgresql creates a new Postgresql database.

func (*Postgresql) Connect

func (pg *Postgresql) Connect() error

Connect connects to the database by the given data source name (dsn) of the concrete database.

func (*Postgresql) DSN

func (pg *Postgresql) DSN() string

DSN creates the DSN String to connect to this database.

func (*Postgresql) GetColumnsOfTable

func (pg *Postgresql) GetColumnsOfTable(table *Table) (err error)

GetColumnsOfTable executes the statement for retrieving the columns of a specific table in a given schema.

func (*Postgresql) GetFloatDatatypes

func (pg *Postgresql) GetFloatDatatypes() []string

GetFloatDatatypes returns the float datatypes for the Postgresql database.

func (*Postgresql) GetIntegerDatatypes

func (pg *Postgresql) GetIntegerDatatypes() []string

GetIntegerDatatypes returns the integer datatypes for the Postgresql database.

func (*Postgresql) GetStringDatatypes

func (pg *Postgresql) GetStringDatatypes() []string

GetStringDatatypes returns the string datatypes for the Postgresql database.

func (*Postgresql) GetTables

func (pg *Postgresql) GetTables(tables ...string) ([]*Table, error)

GetTables gets all tables for a given schema by name.

func (*Postgresql) GetTemporalDatatypes

func (pg *Postgresql) GetTemporalDatatypes() []string

GetTemporalDatatypes returns the temporal datatypes for the Postgresql database.

func (*Postgresql) GetTextDatatypes

func (pg *Postgresql) GetTextDatatypes() []string

GetTextDatatypes returns the text datatypes for the Postgresql database.

func (*Postgresql) IsAutoIncrement

func (pg *Postgresql) IsAutoIncrement(column Column) bool

IsAutoIncrement checks if the column is an auto_increment column.

func (*Postgresql) IsFloat

func (pg *Postgresql) IsFloat(column Column) bool

IsFloat returns true if colum is of type float for the Postgresql database.

func (*Postgresql) IsInteger

func (pg *Postgresql) IsInteger(column Column) bool

IsInteger returns true if colum is of type integer for the Postgresql database.

func (*Postgresql) IsPrimaryKey

func (pg *Postgresql) IsPrimaryKey(column Column) bool

IsPrimaryKey checks if the column belongs to the primary key.

func (*Postgresql) IsString

func (pg *Postgresql) IsString(column Column) bool

IsString returns true if colum is of type string for the Postgresql database.

func (*Postgresql) IsTemporal

func (pg *Postgresql) IsTemporal(column Column) bool

IsTemporal returns true if colum is of type temporal for the Postgresql database.

func (*Postgresql) IsText

func (pg *Postgresql) IsText(column Column) bool

IsText returns true if colum is of type text for the Postgresql database.

func (*Postgresql) PrepareGetColumnsOfTableStmt

func (pg *Postgresql) PrepareGetColumnsOfTableStmt() (err error)

PrepareGetColumnsOfTableStmt prepares the statement for retrieving the columns of a specific table for a given database.

type SQLite

type SQLite struct {
	*GeneralDatabase
}

SQLite implements the Database interface with help of GeneralDatabase.

func NewSQLite

func NewSQLite(s *settings.Settings) *SQLite

NewSQLite creates a new SQLite database.

func (*SQLite) Connect

func (s *SQLite) Connect() (err error)

Connect connects to the database by the given data source name (dsn) of the concrete database.

func (*SQLite) DSN

func (s *SQLite) DSN() string

DSN creates the DSN String to connect to this database.

func (*SQLite) GetColumnsOfTable

func (s *SQLite) GetColumnsOfTable(table *Table) (err error)

func (*SQLite) GetFloatDatatypes

func (s *SQLite) GetFloatDatatypes() []string

func (*SQLite) GetIntegerDatatypes

func (s *SQLite) GetIntegerDatatypes() []string

func (*SQLite) GetStringDatatypes

func (s *SQLite) GetStringDatatypes() []string

func (*SQLite) GetTables

func (s *SQLite) GetTables(tables ...string) ([]*Table, error)

func (*SQLite) GetTemporalDatatypes

func (s *SQLite) GetTemporalDatatypes() []string

func (*SQLite) GetTextDatatypes

func (s *SQLite) GetTextDatatypes() []string

func (*SQLite) IsAutoIncrement

func (s *SQLite) IsAutoIncrement(column Column) bool

func (*SQLite) IsFloat

func (s *SQLite) IsFloat(column Column) bool

func (*SQLite) IsInteger

func (s *SQLite) IsInteger(column Column) bool

func (*SQLite) IsPrimaryKey

func (s *SQLite) IsPrimaryKey(column Column) bool

func (*SQLite) IsString

func (s *SQLite) IsString(column Column) bool

func (*SQLite) IsTemporal

func (s *SQLite) IsTemporal(_ Column) bool

func (*SQLite) IsText

func (s *SQLite) IsText(column Column) bool

func (*SQLite) PrepareGetColumnsOfTableStmt

func (s *SQLite) PrepareGetColumnsOfTableStmt() (err error)

type Table

type Table struct {
	Name    string `db:"table_name"`
	Columns []Column
}

Table has a name and a set (slice) of columns.

Jump to

Keyboard shortcuts

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