Documentation ¶
Index ¶
- Variables
- type Column
- type ColumnType
- type Columns
- func (cs *Columns) Add(cols ...*Column)
- func (cs *Columns) AutoIncrement() *Column
- func (cs *Columns) Clone() *Columns
- func (cs *Columns) Col(name string) *Column
- func (cs *Columns) Cols(names ...string) (*Columns, error)
- func (cs *Columns) Ex(ctx context.Context) *builder.Ex
- func (cs *Columns) IsNil() bool
- func (cs *Columns) Len() int
- func (cs *Columns) MustCols(names ...string) *Columns
- func (cs *Columns) Range(f func(c *Column, idx int))
- func (cs *Columns) Reset()
- type Datatype
- func (v Datatype) ConstValues() []enum.IntStringerEnum
- func (v Datatype) Int() int
- func (v Datatype) Label() string
- func (v Datatype) MarshalText() ([]byte, error)
- func (v *Datatype) Scan(src interface{}) error
- func (v Datatype) String() string
- func (v Datatype) TypeName() string
- func (v *Datatype) UnmarshalText(data []byte) error
- func (v Datatype) Value() (driver.Value, error)
- type IndexDef
- type Key
- type Keys
- type Schema
- func (s *Schema) AddTable(t *Table)
- func (s *Schema) CreateSchema() builder.SqlExpr
- func (s *Schema) DBExecutor(d sqlx.DBExecutor) sqlx.DBExecutor
- func (Schema) DataType(drv string) string
- func (s *Schema) Init() error
- func (s *Schema) Scan(src interface{}) error
- func (s *Schema) T(name string) *Table
- func (s Schema) Value() (driver.Value, error)
- func (s *Schema) WithName(name string)
- type Table
- func (t *Table) AddCol(c *Column)
- func (t *Table) AddIndex(k *Key) builder.SqlExpr
- func (t *Table) AddKey(k *Key)
- func (t *Table) CreateIfNotExists() []builder.SqlExpr
- func (t *Table) Ex(ctx context.Context) *builder.Ex
- func (t *Table) Init() error
- func (t *Table) IsNil() bool
- func (t *Table) WithSchema(schema string)
- type TableDefinition
- type WithTableDefinition
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var InvalidDatatype = errors.New("invalid Datatype type")
Functions ¶
This section is empty.
Types ¶
type Column ¶ added in v1.0.0
type Column struct { Name string `json:"name"` Constrains *ColumnType `json:"constrains"` WithTableDefinition `json:"-"` // contains filtered or unexported fields }
type ColumnType ¶ added in v1.0.0
type ColumnType struct { Datatype Datatype `json:"datatype"` Length uint64 `json:"length,omitempty"` Decimal uint64 `json:"decimal,omitempty"` Default *string `json:"default,omitempty"` Null bool `json:"null,omitempty"` AutoIncrement bool `json:"autoincrement,omitempty"` Desc string `json:"desc,omitempty"` }
ColumnType defines universal column constrains
func (*ColumnType) AutoCompleteLengthDatatype ¶ added in v1.0.0
func (t *ColumnType) AutoCompleteLengthDatatype() string
func (*ColumnType) DatabaseDatatype ¶ added in v1.0.0
func (t *ColumnType) DatabaseDatatype() string
func (*ColumnType) IsNil ¶ added in v1.0.0
func (t *ColumnType) IsNil() bool
func (*ColumnType) NormalizeDefaultValue ¶ added in v1.0.0
func (t *ColumnType) NormalizeDefaultValue() string
func (*ColumnType) TypeModify ¶ added in v1.0.0
func (t *ColumnType) TypeModify() string
type Columns ¶ added in v1.0.0
func (*Columns) AutoIncrement ¶ added in v1.0.0
type Datatype ¶
type Datatype uint8
func ParseDatatypeFromLabel ¶
func ParseDatatypeFromString ¶
func (Datatype) ConstValues ¶
func (v Datatype) ConstValues() []enum.IntStringerEnum
func (Datatype) MarshalText ¶
func (*Datatype) UnmarshalText ¶
type Key ¶ added in v1.0.0
type Key struct { Name string `json:"name,omitempty"` Method string `json:"method,omitempty"` IsUnique bool `json:"isUnique,omitempty"` IndexDef WithTableDefinition `json:"-"` }
type Schema ¶
type Schema struct { Name string `json:"name,omitempty"` Tables []*Table `json:"tables"` *mapx.Map[string, *Table] `json:"-"` }
Example ¶
package main import ( "encoding/json" "fmt" "github.com/machinefi/w3bstream/pkg/depends/schema" ) func main() { s := schema.Schema{ Name: "demo", Tables: []*schema.Table{ { Name: "tbl", Desc: "test table", Cols: []*schema.Column{{ Name: "f_username", Constrains: &schema.ColumnType{ Datatype: schema.DATATYPE__TEXT, Length: 255, Desc: "user name", }, }, { Name: "f_gender", Constrains: &schema.ColumnType{ Datatype: schema.DATATYPE__UINT8, Length: 255, Desc: "user name", }, }}, Keys: []*schema.Key{{ Name: "ui_username", IsUnique: true, IndexDef: schema.IndexDef{ColumnNames: []string{"f_username"}}, }}, WithSoftDeletion: true, WithPrimaryKey: true, }, }, } data, err := json.MarshalIndent(s, "", " ") if err != nil { panic(err) } fmt.Println(string(data)) data, err = json.Marshal(s) if err != nil { panic(err) } fmt.Println(string(data)) }
Output: { "name": "demo", "tables": [ { "name": "tbl", "desc": "test table", "cols": [ { "name": "f_username", "constrains": { "datatype": "TEXT", "length": 255, "desc": "user name" } }, { "name": "f_gender", "constrains": { "datatype": "UINT8", "length": 255, "desc": "user name" } } ], "keys": [ { "name": "ui_username", "isUnique": true, "columnNames": [ "f_username" ] } ], "withSoftDeletion": true, "withPrimaryKey": true } ] } {"name":"demo","tables":[{"name":"tbl","desc":"test table","cols":[{"name":"f_username","constrains":{"datatype":"TEXT","length":255,"desc":"user name"}},{"name":"f_gender","constrains":{"datatype":"UINT8","length":255,"desc":"user name"}}],"keys":[{"name":"ui_username","isUnique":true,"columnNames":["f_username"]}],"withSoftDeletion":true,"withPrimaryKey":true}]}
func FromConfig ¶ added in v1.0.0
func (*Schema) CreateSchema ¶ added in v1.0.0
Example ¶
package main import ( "context" "fmt" "github.com/machinefi/w3bstream/pkg/depends/schema" ) var Schema *schema.Schema func main() { q := Schema.CreateSchema() fmt.Println(q.Ex(context.Background()).Query()) }
Output: CREATE SCHEMA IF NOT EXISTS wasm_project__demo;
func (*Schema) DBExecutor ¶ added in v1.0.0
func (s *Schema) DBExecutor(d sqlx.DBExecutor) sqlx.DBExecutor
type Table ¶
type Table struct { Name string `json:"name"` Desc string `json:"desc,omitempty"` Cols []*Column `json:"cols"` Keys []*Key `json:"keys"` WithSoftDeletion bool `json:"withSoftDeletion,omitempty"` WithPrimaryKey bool `json:"withPrimaryKey,omitempty"` // contains filtered or unexported fields }
func T ¶ added in v1.0.0
func T(name string, defs ...TableDefinition) *Table
T is used to contribute a table structure
func (*Table) CreateIfNotExists ¶ added in v1.0.0
Example ¶
t := Schema.T("tbl") es := t.CreateIfNotExists() for _, e := range es { fmt.Println(e.Ex(context.Background()).Query()) }
Output: CREATE TABLE IF NOT EXISTS wasm_project__demo.tbl ( f_id bigserial NOT NULL, f_username character varying(255) NOT NULL, f_gender integer NOT NULL DEFAULT '0'::integer, f_created_at bigint NOT NULL DEFAULT '0'::bigint, f_updated_at bigint NOT NULL DEFAULT '0'::bigint, f_deleted_at bigint NOT NULL DEFAULT '0'::bigint ); CREATE UNIQUE INDEX tbl_ui_username ON wasm_project__demo.tbl (f_username,f_deleted_at);
func (*Table) WithSchema ¶ added in v1.0.0
type TableDefinition ¶ added in v1.0.0
type WithTableDefinition ¶ added in v1.0.0
type WithTableDefinition struct {
// contains filtered or unexported fields
}
func (*WithTableDefinition) T ¶ added in v1.0.0
func (with *WithTableDefinition) T() *Table
func (*WithTableDefinition) WithTable ¶ added in v1.0.0
func (with *WithTableDefinition) WithTable(t *Table)
Click to show internal directories.
Click to hide internal directories.