schema

package
v1.0.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

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
}

func Col

func Col(name string) *Column

func (*Column) Ex added in v1.0.0

func (c *Column) Ex(ctx context.Context) *builder.Ex

func (*Column) IsNil added in v1.0.0

func (c *Column) IsNil() bool

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) Ex added in v1.0.0

func (t *ColumnType) Ex(ctx context.Context) *builder.Ex

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

type Columns struct {
	*mapx.Map[string, *Column]
	// contains filtered or unexported fields
}

func Cols added in v1.0.0

func Cols(names ...string) *Columns

func (*Columns) Add added in v1.0.0

func (cs *Columns) Add(cols ...*Column)

func (*Columns) AutoIncrement added in v1.0.0

func (cs *Columns) AutoIncrement() *Column

func (*Columns) Clone added in v1.0.0

func (cs *Columns) Clone() *Columns

func (*Columns) Col added in v1.0.0

func (cs *Columns) Col(name string) *Column

func (*Columns) Cols added in v1.0.0

func (cs *Columns) Cols(names ...string) (*Columns, error)

func (*Columns) Ex added in v1.0.0

func (cs *Columns) Ex(ctx context.Context) *builder.Ex

func (*Columns) IsNil added in v1.0.0

func (cs *Columns) IsNil() bool

func (*Columns) Len added in v1.0.0

func (cs *Columns) Len() int

func (*Columns) MustCols added in v1.0.0

func (cs *Columns) MustCols(names ...string) *Columns

func (*Columns) Range added in v1.0.0

func (cs *Columns) Range(f func(c *Column, idx int))

func (*Columns) Reset added in v1.0.0

func (cs *Columns) Reset()

type Datatype

type Datatype uint8
const (
	DATATYPE_UNKNOWN Datatype = iota
	DATATYPE__INT
	DATATYPE__INT8
	DATATYPE__INT16
	DATATYPE__INT32
	DATATYPE__INT64
	DATATYPE__UINT
	DATATYPE__UINT8
	DATATYPE__UINT16
	DATATYPE__UINT32
	DATATYPE__UINT64
	DATATYPE__FLOAT32
	DATATYPE__FLOAT64
	DATATYPE__TEXT
	DATATYPE__BOOL
	DATATYPE__TIMESTAMP
)

func ParseDatatypeFromLabel

func ParseDatatypeFromLabel(s string) (Datatype, error)

func ParseDatatypeFromString

func ParseDatatypeFromString(s string) (Datatype, error)

func (Datatype) ConstValues

func (v Datatype) ConstValues() []enum.IntStringerEnum

func (Datatype) Int

func (v Datatype) Int() int

func (Datatype) Label

func (v Datatype) Label() string

func (Datatype) MarshalText

func (v Datatype) MarshalText() ([]byte, error)

func (*Datatype) Scan

func (v *Datatype) Scan(src interface{}) error

func (Datatype) String

func (v Datatype) String() string

func (Datatype) TypeName

func (v Datatype) TypeName() string

func (*Datatype) UnmarshalText

func (v *Datatype) UnmarshalText(data []byte) error

func (Datatype) Value

func (v Datatype) Value() (driver.Value, error)

type IndexDef added in v1.0.0

type IndexDef struct {
	ColumnNames []string `json:"columnNames"`
	Expr        string   `json:"expr,omitempty"`
}

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:"-"`
}

func (*Key) IsPrimary added in v1.0.0

func (k *Key) IsPrimary() bool

type Keys added in v1.0.0

type Keys struct {
	*mapx.Map[string, *Key]
	// contains filtered or unexported fields
}

func (*Keys) Add added in v1.0.0

func (ks *Keys) Add(keys ...*Key)

func (*Keys) Range added in v1.0.0

func (ks *Keys) Range(f func(k *Key, idx int))

func (*Keys) Reset added in v1.0.0

func (ks *Keys) Reset()

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 FromConfig(data []byte) (*Schema, error)

func NewSchema added in v1.0.0

func NewSchema(name string) *Schema

func (*Schema) AddTable added in v1.0.0

func (s *Schema) AddTable(t *Table)

func (*Schema) CreateSchema added in v1.0.0

func (s *Schema) CreateSchema() builder.SqlExpr
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

func (Schema) DataType added in v1.0.0

func (Schema) DataType(drv string) string

func (*Schema) Init added in v1.0.0

func (s *Schema) Init() error

func (*Schema) Scan added in v1.0.0

func (s *Schema) Scan(src interface{}) error

func (*Schema) T added in v1.0.0

func (s *Schema) T(name string) *Table

func (Schema) Value added in v1.0.0

func (s Schema) Value() (driver.Value, error)

func (*Schema) WithName added in v1.0.0

func (s *Schema) WithName(name string)

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) AddCol added in v1.0.0

func (t *Table) AddCol(c *Column)

func (*Table) AddIndex added in v1.0.0

func (t *Table) AddIndex(k *Key) builder.SqlExpr

func (*Table) AddKey added in v1.0.0

func (t *Table) AddKey(k *Key)

func (*Table) CreateIfNotExists added in v1.0.0

func (t *Table) CreateIfNotExists() []builder.SqlExpr
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) Ex added in v1.0.0

func (t *Table) Ex(ctx context.Context) *builder.Ex

func (*Table) Init added in v1.0.0

func (t *Table) Init() error

func (*Table) IsNil added in v1.0.0

func (t *Table) IsNil() bool

func (*Table) WithSchema added in v1.0.0

func (t *Table) WithSchema(schema string)

type TableDefinition added in v1.0.0

type TableDefinition interface {
	WithTable(*Table)
	T() *Table
}

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)

Jump to

Keyboard shortcuts

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