schemas

package
v0.0.0-...-5f82a4e Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TWOSIDES = iota + 1
	ONLYTODB
	ONLYFROMDB
)

enumerates all database mapping way

View Source
const (
	IndexType = iota + 1
	UniqueType
)

enumerate all index types

View Source
const (
	UNKNOW_TYPE = iota
	TEXT_TYPE
	BLOB_TYPE
	TIME_TYPE
	NUMERIC_TYPE
	ARRAY_TYPE
	BOOL_TYPE
)

enumerates all columns types

Variables

View Source
var (
	// AlwaysNoReserve always think it's not a reverse word
	AlwaysNoReserve = func(string) bool { return false }

	// AlwaysReserve always reverse the word
	AlwaysReserve = func(string) bool { return true }

	// CommanQuoteMark represnets the common quote mark
	CommanQuoteMark byte = '`'

	// CommonQuoter represetns a common quoter
	CommonQuoter = Quoter{CommanQuoteMark, CommanQuoteMark, AlwaysReserve}
)
View Source
var (
	Bit               = "BIT"
	UnsignedBit       = "UNSIGNED BIT"
	TinyInt           = "TINYINT"
	UnsignedTinyInt   = "UNSIGNED TINYINT"
	SmallInt          = "SMALLINT"
	UnsignedSmallInt  = "UNSIGNED SMALLINT"
	MediumInt         = "MEDIUMINT"
	UnsignedMediumInt = "UNSIGNED MEDIUMINT"
	Int               = "INT"
	UnsignedInt       = "UNSIGNED INT"
	Integer           = "INTEGER"
	BigInt            = "BIGINT"
	UnsignedBigInt    = "UNSIGNED BIGINT"
	Number            = "NUMBER"

	Enum = "ENUM"
	Set  = "SET"

	Char             = "CHAR"
	Varchar          = "VARCHAR"
	VARCHAR2         = "VARCHAR2"
	NChar            = "NCHAR"
	NVarchar         = "NVARCHAR"
	TinyText         = "TINYTEXT"
	Text             = "TEXT"
	NText            = "NTEXT"
	Clob             = "CLOB"
	MediumText       = "MEDIUMTEXT"
	LongText         = "LONGTEXT"
	Uuid             = "UUID"
	UniqueIdentifier = "UNIQUEIDENTIFIER"
	SysName          = "SYSNAME"

	Date          = "DATE"
	DateTime      = "DATETIME"
	SmallDateTime = "SMALLDATETIME"
	Time          = "TIME"
	TimeStamp     = "TIMESTAMP"
	TimeStampz    = "TIMESTAMPZ"
	Year          = "YEAR"

	Decimal    = "DECIMAL"
	Numeric    = "NUMERIC"
	Money      = "MONEY"
	SmallMoney = "SMALLMONEY"

	Real   = "REAL"
	Float  = "FLOAT"
	Double = "DOUBLE"

	Binary     = "BINARY"
	VarBinary  = "VARBINARY"
	TinyBlob   = "TINYBLOB"
	Blob       = "BLOB"
	MediumBlob = "MEDIUMBLOB"
	LongBlob   = "LONGBLOB"
	Bytea      = "BYTEA"

	Bool    = "BOOL"
	Boolean = "BOOLEAN"

	Serial    = "SERIAL"
	BigSerial = "BIGSERIAL"

	Json  = "JSON"
	Jsonb = "JSONB"

	XML   = "XML"
	Array = "ARRAY"

	SqlTypes = map[string]int{
		Bit:               NUMERIC_TYPE,
		UnsignedBit:       NUMERIC_TYPE,
		TinyInt:           NUMERIC_TYPE,
		UnsignedTinyInt:   NUMERIC_TYPE,
		SmallInt:          NUMERIC_TYPE,
		UnsignedSmallInt:  NUMERIC_TYPE,
		MediumInt:         NUMERIC_TYPE,
		UnsignedMediumInt: NUMERIC_TYPE,
		Int:               NUMERIC_TYPE,
		UnsignedInt:       NUMERIC_TYPE,
		Integer:           NUMERIC_TYPE,
		BigInt:            NUMERIC_TYPE,
		UnsignedBigInt:    NUMERIC_TYPE,
		Number:            NUMERIC_TYPE,

		Enum:  TEXT_TYPE,
		Set:   TEXT_TYPE,
		Json:  TEXT_TYPE,
		Jsonb: TEXT_TYPE,

		XML: TEXT_TYPE,

		Char:       TEXT_TYPE,
		NChar:      TEXT_TYPE,
		Varchar:    TEXT_TYPE,
		VARCHAR2:   TEXT_TYPE,
		NVarchar:   TEXT_TYPE,
		TinyText:   TEXT_TYPE,
		Text:       TEXT_TYPE,
		NText:      TEXT_TYPE,
		MediumText: TEXT_TYPE,
		LongText:   TEXT_TYPE,
		Uuid:       TEXT_TYPE,
		Clob:       TEXT_TYPE,
		SysName:    TEXT_TYPE,

		Date:          TIME_TYPE,
		DateTime:      TIME_TYPE,
		Time:          TIME_TYPE,
		TimeStamp:     TIME_TYPE,
		TimeStampz:    TIME_TYPE,
		SmallDateTime: TIME_TYPE,
		Year:          TIME_TYPE,

		Decimal:    NUMERIC_TYPE,
		Numeric:    NUMERIC_TYPE,
		Real:       NUMERIC_TYPE,
		Float:      NUMERIC_TYPE,
		Double:     NUMERIC_TYPE,
		Money:      NUMERIC_TYPE,
		SmallMoney: NUMERIC_TYPE,

		Binary:    BLOB_TYPE,
		VarBinary: BLOB_TYPE,

		TinyBlob:         BLOB_TYPE,
		Blob:             BLOB_TYPE,
		MediumBlob:       BLOB_TYPE,
		LongBlob:         BLOB_TYPE,
		Bytea:            BLOB_TYPE,
		UniqueIdentifier: BLOB_TYPE,

		Bool:    BOOL_TYPE,
		Boolean: BOOL_TYPE,

		Serial:    NUMERIC_TYPE,
		BigSerial: NUMERIC_TYPE,

		"INT8": NUMERIC_TYPE,

		Array: ARRAY_TYPE,
	}
)

enumerates all the database column types

View Source
var (
	IntType   = reflect.TypeOf((*int)(nil)).Elem()
	Int8Type  = reflect.TypeOf((*int8)(nil)).Elem()
	Int16Type = reflect.TypeOf((*int16)(nil)).Elem()
	Int32Type = reflect.TypeOf((*int32)(nil)).Elem()
	Int64Type = reflect.TypeOf((*int64)(nil)).Elem()

	UintType   = reflect.TypeOf((*uint)(nil)).Elem()
	Uint8Type  = reflect.TypeOf((*uint8)(nil)).Elem()
	Uint16Type = reflect.TypeOf((*uint16)(nil)).Elem()
	Uint32Type = reflect.TypeOf((*uint32)(nil)).Elem()
	Uint64Type = reflect.TypeOf((*uint64)(nil)).Elem()

	Float32Type = reflect.TypeOf((*float32)(nil)).Elem()
	Float64Type = reflect.TypeOf((*float64)(nil)).Elem()

	Complex64Type  = reflect.TypeOf((*complex64)(nil)).Elem()
	Complex128Type = reflect.TypeOf((*complex128)(nil)).Elem()

	StringType = reflect.TypeOf((*string)(nil)).Elem()
	BoolType   = reflect.TypeOf((*bool)(nil)).Elem()
	ByteType   = reflect.TypeOf((*byte)(nil)).Elem()
	BytesType  = reflect.SliceOf(ByteType)

	TimeType        = reflect.TypeOf((*time.Time)(nil)).Elem()
	BigFloatType    = reflect.TypeOf((*big.Float)(nil)).Elem()
	NullFloat64Type = reflect.TypeOf((*sql.NullFloat64)(nil)).Elem()
	NullStringType  = reflect.TypeOf((*sql.NullString)(nil)).Elem()
	NullInt32Type   = reflect.TypeOf((*sql.NullInt32)(nil)).Elem()
	NullInt64Type   = reflect.TypeOf((*sql.NullInt64)(nil)).Elem()
	NullBoolType    = reflect.TypeOf((*sql.NullBool)(nil)).Elem()
)

enumerates all types

Functions

func SQLType2Type

func SQLType2Type(st SQLType) reflect.Type

SQLType2Type convert default sql type change to go types

func SQLTypeName

func SQLTypeName(tp string) string

SQLTypeName returns sql type name

Types

type Column

type Column struct {
	Name            string
	TableName       string
	FieldName       string // Available only when parsed from a struct
	FieldIndex      []int  // Available only when parsed from a struct
	SQLType         SQLType
	IsJSON          bool
	Length          int64
	Length2         int64
	Nullable        bool
	Default         string
	Indexes         map[string]int
	IsPrimaryKey    bool
	IsAutoIncrement bool
	MapType         int
	IsCreated       bool
	IsUpdated       bool
	IsDeleted       bool
	IsCascade       bool
	IsVersion       bool
	DefaultIsEmpty  bool // false means column has no default set, but not default value is empty
	EnumOptions     map[string]int
	SetOptions      map[string]int
	DisableTimeZone bool
	TimeZone        *time.Location // column specified time zone
	Comment         string
}

Column defines database column

func NewColumn

func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int64, nullable bool) *Column

NewColumn creates a new column

func (*Column) ConvertID

func (col *Column) ConvertID(sid string) (interface{}, error)

ConvertID converts id content to suitable type according column type

func (*Column) ValueOf

func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error)

ValueOf returns column's filed of struct's value

func (*Column) ValueOfV

func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error)

ValueOfV returns column's filed of struct's value accept reflevt value

type DBType

type DBType string

DBType represents a database type

const (
	POSTGRES DBType = "postgres"
	SQLITE   DBType = "sqlite3"
	MYSQL    DBType = "mysql"
	MSSQL    DBType = "mssql"
	ORACLE   DBType = "oracle"
	DAMENG   DBType = "dameng"
)

enumerates all database types

type Index

type Index struct {
	IsRegular bool
	Name      string
	Type      int
	Cols      []string
}

Index represents a database index

func NewIndex

func NewIndex(name string, indexType int) *Index

NewIndex new an index object

func (*Index) AddColumn

func (index *Index) AddColumn(cols ...string)

AddColumn add columns which will be composite index

func (*Index) Equal

func (index *Index) Equal(dst *Index) bool

Equal return true if the two Index is equal

func (*Index) XName

func (index *Index) XName(tableName string) string

XName returns the special index name for the table

type PK

type PK []interface{}

PK represents primary key values

func NewPK

func NewPK(pks ...interface{}) *PK

NewPK creates primay keys

func (*PK) FromString

func (p *PK) FromString(content string) error

FromString reads content to load primary keys

func (*PK) IsZero

func (p *PK) IsZero() bool

IsZero return true if primay keys are zero

func (*PK) ToString

func (p *PK) ToString() (string, error)

ToString convert to SQL string

type Quoter

type Quoter struct {
	Prefix     byte
	Suffix     byte
	IsReserved func(string) bool
}

Quoter represents a quoter to the SQL table name and column name

func (Quoter) IsEmpty

func (q Quoter) IsEmpty() bool

IsEmpty return true if no prefix and suffix

func (Quoter) Join

func (q Quoter) Join(a []string, sep string) string

Join joins a slice with quoters

func (Quoter) JoinWrite

func (q Quoter) JoinWrite(b *strings.Builder, a []string, sep string) error

JoinWrite writes quoted content to a builder

func (Quoter) Quote

func (q Quoter) Quote(s string) string

Quote quote a string

func (Quoter) QuoteTo

func (q Quoter) QuoteTo(buf *strings.Builder, value string) error

QuoteTo quotes the table or column names. i.e. if the quotes are [ and ]

name -> [name]
`name` -> [name]
[name] -> [name]
schema.name -> [schema].[name]
`schema`.`name` -> [schema].[name]
`schema`.name -> [schema].[name]
schema.`name` -> [schema].[name]
[schema].name -> [schema].[name]
schema.[name] -> [schema].[name]
name AS a  ->  [name] AS a
schema.name AS a  ->  [schema].[name] AS a

func (Quoter) Replace

func (q Quoter) Replace(sql string) string

Replace replaces common quote(`) as the quotes on the sql

func (Quoter) Strings

func (q Quoter) Strings(s []string) []string

Strings quotes a slice of string

func (Quoter) Trim

func (q Quoter) Trim(s string) string

Trim removes quotes from s

type SQLType

type SQLType struct {
	Name           string
	DefaultLength  int64
	DefaultLength2 int64
}

SQLType represents SQL types

func Type2SQLType

func Type2SQLType(t reflect.Type) (st SQLType)

Type2SQLType generate SQLType acorrding Go's type

func (*SQLType) IsArray

func (s *SQLType) IsArray() bool

IsArray returns true if column is an array type

func (*SQLType) IsBlob

func (s *SQLType) IsBlob() bool

IsBlob returns true if column is a binary type

func (*SQLType) IsBool

func (s *SQLType) IsBool() bool

IsBool returns true if column is a boolean type

func (*SQLType) IsJson

func (s *SQLType) IsJson() bool

IsJson returns true if column is an array type

func (*SQLType) IsNumeric

func (s *SQLType) IsNumeric() bool

IsNumeric returns true if column is a numeric type

func (*SQLType) IsText

func (s *SQLType) IsText() bool

IsText returns true if column is a text type

func (*SQLType) IsTime

func (s *SQLType) IsTime() bool

IsTime returns true if column is a time type

func (*SQLType) IsType

func (s *SQLType) IsType(st int) bool

IsType reutrns ture if the column type is the same as the parameter

func (*SQLType) IsXML

func (s *SQLType) IsXML() bool

IsXML returns true if column is an xml type

type Table

type Table struct {
	Name string
	Type reflect.Type

	Indexes       map[string]*Index
	PrimaryKeys   []string
	AutoIncrement string
	Created       map[string]bool
	Updated       string
	Deleted       string
	Version       string
	StoreEngine   string
	Charset       string
	Comment       string
	// contains filtered or unexported fields
}

Table represents a database table

func NewEmptyTable

func NewEmptyTable() *Table

NewEmptyTable creates an empty table

func NewTable

func NewTable(name string, t reflect.Type) *Table

NewTable creates a new Table object

func (*Table) AddColumn

func (table *Table) AddColumn(col *Column)

AddColumn adds a column to table

func (*Table) AddIndex

func (table *Table) AddIndex(index *Index)

AddIndex adds an index or an unique to table

func (*Table) AutoIncrColumn

func (table *Table) AutoIncrColumn() *Column

AutoIncrColumn returns autoincrement column

func (*Table) ColumnType

func (table *Table) ColumnType(name string) reflect.Type

ColumnType returns a column's type

func (*Table) Columns

func (table *Table) Columns() []*Column

Columns returns table's columns

func (*Table) ColumnsSeq

func (table *Table) ColumnsSeq() []string

ColumnsSeq returns table's column names according sequence

func (*Table) DeletedColumn

func (table *Table) DeletedColumn() *Column

DeletedColumn returns deleted column's information

func (*Table) GetColumn

func (table *Table) GetColumn(name string) *Column

GetColumn returns column according column name, if column not found, return nil

func (*Table) GetColumnIdx

func (table *Table) GetColumnIdx(name string, idx int) *Column

GetColumnIdx returns column according name and idx

func (*Table) IDOfV

func (table *Table) IDOfV(rv reflect.Value) (PK, error)

IDOfV get id from one value of struct

func (*Table) PKColumns

func (table *Table) PKColumns() []*Column

PKColumns reprents all primary key columns

func (*Table) UpdatedColumn

func (table *Table) UpdatedColumn() *Column

UpdatedColumn returns updated column's information

func (*Table) VersionColumn

func (table *Table) VersionColumn() *Column

VersionColumn returns version column's information

type Version

type Version struct {
	Number  string // the version number which could be compared
	Level   string
	Edition string
}

Version represents a database version

Jump to

Keyboard shortcuts

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