Documentation ¶
Index ¶
- func AddColumn(db *sqlx.DB, col Column) error
- func ChangeColumn(db *sqlx.DB, col Column) error
- func CheckAutoSet(defaultVal *string) bool
- func CheckAutoincrement(extra string) bool
- func CheckNull(null nullenum.Null) bool
- func CheckPk(key keyenum.Key) bool
- func CheckUnsigned(dbColType string) bool
- func CreateTable(db *sqlx.DB, t Table) error
- func NewFieldFromColumn(col Column) astutils.FieldMeta
- type Column
- type DbColumn
- type DbIndex
- type Index
- type IndexItem
- type IndexItems
- type Table
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckAutoSet ¶
func CheckAutoincrement ¶
func CheckUnsigned ¶
func CreateTable ¶
Example ¶
terminator, db, err := setup() if err != nil { panic(err) } defer terminator() defer db.Close() expectjson := `{"Name":"user_createtable","Columns":[{"Table":"user","Name":"id","Type":"INT","Default":null,"Pk":true,"Nullable":false,"Unsigned":false,"Autoincrement":true,"Extra":"","Meta":{"Name":"ID","Type":"int","Tag":"dd:\"pk;auto\"","Comments":null},"AutoSet":false,"Indexes":null},{"Table":"user","Name":"name","Type":"VARCHAR(255)","Default":"'jack'","Pk":false,"Nullable":false,"Unsigned":false,"Autoincrement":false,"Extra":"","Meta":{"Name":"Name","Type":"string","Tag":"dd:\"index:name_phone_idx,2;default:'jack'\"","Comments":null},"AutoSet":false,"Indexes":null},{"Table":"user","Name":"phone","Type":"VARCHAR(255)","Default":"'13552053960'","Pk":false,"Nullable":false,"Unsigned":false,"Autoincrement":false,"Extra":"comment '手机号'","Meta":{"Name":"Phone","Type":"string","Tag":"dd:\"index:name_phone_idx,1;default:'13552053960';extra:comment '手机号'\"","Comments":null},"AutoSet":false,"Indexes":null},{"Table":"user","Name":"age","Type":"INT","Default":null,"Pk":false,"Nullable":false,"Unsigned":false,"Autoincrement":false,"Extra":"","Meta":{"Name":"Age","Type":"int","Tag":"dd:\"index\"","Comments":null},"AutoSet":false,"Indexes":null},{"Table":"user","Name":"no","Type":"INT","Default":null,"Pk":false,"Nullable":false,"Unsigned":false,"Autoincrement":false,"Extra":"","Meta":{"Name":"No","Type":"int","Tag":"dd:\"unique\"","Comments":null},"AutoSet":false,"Indexes":null},{"Table":"user","Name":"school","Type":"VARCHAR(255)","Default":"'harvard'","Pk":false,"Nullable":true,"Unsigned":false,"Autoincrement":false,"Extra":"comment '学校'","Meta":{"Name":"School","Type":"string","Tag":"dd:\"null;default:'harvard';extra:comment '学校'\"","Comments":null},"AutoSet":false,"Indexes":null},{"Table":"user","Name":"is_student","Type":"TINYINT","Default":null,"Pk":false,"Nullable":false,"Unsigned":false,"Autoincrement":false,"Extra":"","Meta":{"Name":"IsStudent","Type":"bool","Tag":"","Comments":null},"AutoSet":false,"Indexes":null},{"Table":"user","Name":"delete_at","Type":"DATETIME","Default":null,"Pk":false,"Nullable":true,"Unsigned":false,"Autoincrement":false,"Extra":"","Meta":{"Name":"DeleteAt","Type":"*time.Time","Tag":"","Comments":null},"AutoSet":false,"Indexes":null},{"Table":"user","Name":"create_at","Type":"DATETIME","Default":"CURRENT_TIMESTAMP","Pk":false,"Nullable":true,"Unsigned":false,"Autoincrement":false,"Extra":"","Meta":{"Name":"CreateAt","Type":"*time.Time","Tag":"dd:\"default:CURRENT_TIMESTAMP\"","Comments":null},"AutoSet":true,"Indexes":null},{"Table":"user","Name":"update_at","Type":"DATETIME","Default":"CURRENT_TIMESTAMP","Pk":false,"Nullable":true,"Unsigned":false,"Autoincrement":false,"Extra":"ON UPDATE CURRENT_TIMESTAMP","Meta":{"Name":"UpdateAt","Type":"*time.Time","Tag":"dd:\"default:CURRENT_TIMESTAMP;extra:ON UPDATE CURRENT_TIMESTAMP\"","Comments":null},"AutoSet":true,"Indexes":null}],"Pk":"id","Indexes":[{"Unique":false,"Name":"name_phone_idx","Items":[{"Unique":false,"Name":"","Column":"phone","Order":1,"Sort":"asc"},{"Unique":false,"Name":"","Column":"name","Order":2,"Sort":"asc"}]},{"Unique":false,"Name":"age_idx","Items":[{"Unique":false,"Name":"","Column":"age","Order":1,"Sort":"asc"}]},{"Unique":true,"Name":"no_idx","Items":[{"Unique":false,"Name":"","Column":"no","Order":1,"Sort":"asc"}]}],"Meta":{"Name":"User","Fields":[{"Name":"ID","Type":"int","Tag":"dd:\"pk;auto\"","Comments":null},{"Name":"Name","Type":"string","Tag":"dd:\"index:name_phone_idx,2;default:'jack'\"","Comments":null},{"Name":"Phone","Type":"string","Tag":"dd:\"index:name_phone_idx,1;default:'13552053960';extra:comment '手机号'\"","Comments":null},{"Name":"Age","Type":"int","Tag":"dd:\"index\"","Comments":null},{"Name":"No","Type":"int","Tag":"dd:\"unique\"","Comments":null},{"Name":"School","Type":"string","Tag":"dd:\"null;default:'harvard';extra:comment '学校'\"","Comments":null},{"Name":"IsStudent","Type":"bool","Tag":"","Comments":null},{"Name":"DeleteAt","Type":"*time.Time","Tag":"","Comments":null},{"Name":"CreateAt","Type":"*time.Time","Tag":"dd:\"default:CURRENT_TIMESTAMP\"","Comments":null},{"Name":"UpdateAt","Type":"*time.Time","Tag":"dd:\"default:CURRENT_TIMESTAMP;extra:ON UPDATE CURRENT_TIMESTAMP\"","Comments":null}],"Comments":["dd:table"],"Methods":null}}` var table Table if err = json.Unmarshal([]byte(expectjson), &table); err != nil { panic(err) } if err := CreateTable(db, table); (err != nil) != false { panic(fmt.Sprintf("CreateTable() error = %v, wantErr %v", err, false)) }
Output:
func NewFieldFromColumn ¶
Types ¶
type Column ¶
type Column struct { Table string Name string Type columnenum.ColumnType Default interface{} Pk bool Nullable bool Unsigned bool Autoincrement bool Extra extraenum.Extra Meta astutils.FieldMeta AutoSet bool Indexes []IndexItem }
func (*Column) AddColumnSql ¶
func (*Column) ChangeColumnSql ¶
type DbIndex ¶
type DbIndex struct { Table string `db:"Table"` // The name of the table Non_unique bool `db:"Non_unique"` // 1 if the index can contain duplicates, 0 if it cannot. Key_name string `db:"Key_name"` // The name of the index. The primary key index always has the name of PRIMARY. Seq_in_index int `db:"Seq_in_index"` // The column sequence number in the index. The first column sequence number starts from 1. Column_name string `db:"Column_name"` // The column name Collation string `db:"Collation"` // Collation represents how the column is sorted in the index. A means ascending, B means descending, or NULL means not sorted. }
https://www.mysqltutorial.org/mysql-index/mysql-show-indexes/
type IndexItems ¶
type IndexItems []IndexItem
func (IndexItems) Len ¶
func (it IndexItems) Len() int
func (IndexItems) Less ¶
func (it IndexItems) Less(i, j int) bool
func (IndexItems) Swap ¶
func (it IndexItems) Swap(i, j int)
Click to show internal directories.
Click to hide internal directories.