Merge branch 'master' into fix/exec_for_update_on_master
This commit is contained in:
commit
3ecf64a7e8
|
@ -214,7 +214,7 @@ type UserDetail struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var users []UserDetail
|
var users []UserDetail
|
||||||
err := engine.Table("user").Select("user.*, detail.*")
|
err := engine.Table("user").Select("user.*, detail.*").
|
||||||
Join("INNER", "detail", "detail.user_id = user.id").
|
Join("INNER", "detail", "detail.user_id = user.id").
|
||||||
Where("user.name = ?", name).Limit(10, 0).
|
Where("user.name = ?", name).Limit(10, 0).
|
||||||
Find(&users)
|
Find(&users)
|
||||||
|
|
|
@ -206,7 +206,7 @@ func (db *Base) IsColumnExist(queryer core.Queryer, ctx context.Context, tableNa
|
||||||
// AddColumnSQL returns a SQL to add a column
|
// AddColumnSQL returns a SQL to add a column
|
||||||
func (db *Base) AddColumnSQL(tableName string, col *schemas.Column) string {
|
func (db *Base) AddColumnSQL(tableName string, col *schemas.Column) string {
|
||||||
s, _ := ColumnString(db.dialect, col, true)
|
s, _ := ColumnString(db.dialect, col, true)
|
||||||
return fmt.Sprintf("ALTER TABLE %v ADD %v", db.dialect.Quoter().Quote(tableName), s)
|
return fmt.Sprintf("ALTER TABLE %s ADD %s", db.dialect.Quoter().Quote(tableName), s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateIndexSQL returns a SQL to create index
|
// CreateIndexSQL returns a SQL to create index
|
||||||
|
|
|
@ -685,6 +685,12 @@ func (db *mysql) CreateTableSQL(ctx context.Context, queryer core.Queryer, table
|
||||||
b.WriteString(db.rowFormat)
|
b.WriteString(db.rowFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if table.Comment != "" {
|
||||||
|
b.WriteString(" COMMENT='")
|
||||||
|
b.WriteString(table.Comment)
|
||||||
|
b.WriteString("'")
|
||||||
|
}
|
||||||
|
|
||||||
return b.String(), true, nil
|
return b.String(), true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -991,13 +991,37 @@ func (db *postgres) IsTableExist(queryer core.Queryer, ctx context.Context, tabl
|
||||||
db.getSchema(), tableName)
|
db.getSchema(), tableName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) ModifyColumnSQL(tableName string, col *schemas.Column) string {
|
func (db *postgres) AddColumnSQL(tableName string, col *schemas.Column) string {
|
||||||
|
s, _ := ColumnString(db.dialect, col, true)
|
||||||
|
|
||||||
|
quoter := db.dialect.Quoter()
|
||||||
|
addColumnSQL := ""
|
||||||
|
commentSQL := "; "
|
||||||
if len(db.getSchema()) == 0 || strings.Contains(tableName, ".") {
|
if len(db.getSchema()) == 0 || strings.Contains(tableName, ".") {
|
||||||
return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s",
|
addColumnSQL = fmt.Sprintf("ALTER TABLE %s ADD %s", quoter.Quote(tableName), s)
|
||||||
db.quoter.Quote(tableName), db.quoter.Quote(col.Name), db.SQLType(col))
|
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s'", quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment)
|
||||||
|
return addColumnSQL + commentSQL
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("alter table %s.%s ALTER COLUMN %s TYPE %s",
|
|
||||||
db.quoter.Quote(db.getSchema()), db.quoter.Quote(tableName), db.quoter.Quote(col.Name), db.SQLType(col))
|
addColumnSQL = fmt.Sprintf("ALTER TABLE %s.%s ADD %s", quoter.Quote(db.getSchema()), quoter.Quote(tableName), s)
|
||||||
|
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s.%s IS '%s'", quoter.Quote(db.getSchema()), quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment)
|
||||||
|
return addColumnSQL + commentSQL
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *postgres) ModifyColumnSQL(tableName string, col *schemas.Column) string {
|
||||||
|
quoter := db.dialect.Quoter()
|
||||||
|
modifyColumnSQL := ""
|
||||||
|
commentSQL := "; "
|
||||||
|
|
||||||
|
if len(db.getSchema()) == 0 || strings.Contains(tableName, ".") {
|
||||||
|
modifyColumnSQL = fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s", quoter.Quote(tableName), quoter.Quote(col.Name), db.SQLType(col))
|
||||||
|
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s'", quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment)
|
||||||
|
return modifyColumnSQL + commentSQL
|
||||||
|
}
|
||||||
|
|
||||||
|
modifyColumnSQL = fmt.Sprintf("ALTER TABLE %s.%s ALTER COLUMN %s TYPE %s", quoter.Quote(db.getSchema()), quoter.Quote(tableName), quoter.Quote(col.Name), db.SQLType(col))
|
||||||
|
commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s.%s IS '%s'", quoter.Quote(db.getSchema()), quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment)
|
||||||
|
return modifyColumnSQL + commentSQL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) DropIndexSQL(tableName string, index *schemas.Index) string {
|
func (db *postgres) DropIndexSQL(tableName string, index *schemas.Index) string {
|
||||||
|
@ -1302,6 +1326,26 @@ func (db *postgres) GetIndexes(queryer core.Queryer, ctx context.Context, tableN
|
||||||
return indexes, nil
|
return indexes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *postgres) CreateTableSQL(ctx context.Context, queryer core.Queryer, table *schemas.Table, tableName string) (string, bool, error) {
|
||||||
|
quoter := db.dialect.Quoter()
|
||||||
|
if len(db.getSchema()) != 0 && !strings.Contains(tableName, ".") {
|
||||||
|
tableName = fmt.Sprintf("%s.%s", db.getSchema(), tableName)
|
||||||
|
}
|
||||||
|
|
||||||
|
createTableSQL, ok, err := db.Base.CreateTableSQL(ctx, queryer, table, tableName)
|
||||||
|
if err != nil {
|
||||||
|
return "", ok, err
|
||||||
|
}
|
||||||
|
|
||||||
|
commentSql := "; "
|
||||||
|
if table.Comment != "" {
|
||||||
|
// support schema.table -> "schema"."table"
|
||||||
|
commentSql += fmt.Sprintf("COMMENT ON TABLE %s IS '%s'", quoter.Quote(tableName), table.Comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
return createTableSQL + commentSql, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (db *postgres) Filters() []Filter {
|
func (db *postgres) Filters() []Filter {
|
||||||
return []Filter{&SeqFilter{Prefix: "$", Start: 1}}
|
return []Filter{&SeqFilter{Prefix: "$", Start: 1}}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,15 @@ type TableName interface {
|
||||||
TableName() string
|
TableName() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TableComment interface {
|
||||||
|
TableComment() string
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
|
tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
|
||||||
|
tpTableComment = reflect.TypeOf((*TableComment)(nil)).Elem()
|
||||||
tvCache sync.Map
|
tvCache sync.Map
|
||||||
|
tcCache sync.Map
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetTableName returns table name
|
// GetTableName returns table name
|
||||||
|
@ -55,3 +61,40 @@ func GetTableName(mapper Mapper, v reflect.Value) string {
|
||||||
|
|
||||||
return mapper.Obj2Table(v.Type().Name())
|
return mapper.Obj2Table(v.Type().Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTableComment returns table comment
|
||||||
|
func GetTableComment(v reflect.Value) string {
|
||||||
|
if v.Type().Implements(tpTableComment) {
|
||||||
|
return v.Interface().(TableComment).TableComment()
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
v = v.Elem()
|
||||||
|
if v.Type().Implements(tpTableComment) {
|
||||||
|
return v.Interface().(TableComment).TableComment()
|
||||||
|
}
|
||||||
|
} else if v.CanAddr() {
|
||||||
|
v1 := v.Addr()
|
||||||
|
if v1.Type().Implements(tpTableComment) {
|
||||||
|
return v1.Interface().(TableComment).TableComment()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
comment, ok := tcCache.Load(v.Type())
|
||||||
|
if ok {
|
||||||
|
if comment.(string) != "" {
|
||||||
|
return comment.(string)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v2 := reflect.New(v.Type())
|
||||||
|
if v2.Type().Implements(tpTableComment) {
|
||||||
|
tableComment := v2.Interface().(TableComment).TableComment()
|
||||||
|
tcCache.Store(v.Type(), tableComment)
|
||||||
|
return tableComment
|
||||||
|
}
|
||||||
|
|
||||||
|
tcCache.Store(v.Type(), "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
|
@ -332,6 +332,10 @@ func SQLType2Type(st SQLType) reflect.Type {
|
||||||
return IntType
|
return IntType
|
||||||
case BigInt, BigSerial:
|
case BigInt, BigSerial:
|
||||||
return Int64Type
|
return Int64Type
|
||||||
|
case UnsignedBit, UnsignedTinyInt, UnsignedSmallInt, UnsignedMediumInt, UnsignedInt:
|
||||||
|
return UintType
|
||||||
|
case UnsignedBigInt:
|
||||||
|
return Uint64Type
|
||||||
case Float, Real:
|
case Float, Real:
|
||||||
return Float32Type
|
return Float32Type
|
||||||
case Double:
|
case Double:
|
||||||
|
|
|
@ -394,6 +394,8 @@ func (session *Session) Sync(beans ...interface{}) error {
|
||||||
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if col.Comment != oriCol.Comment {
|
||||||
|
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.Default != oriCol.Default {
|
if col.Default != oriCol.Default {
|
||||||
|
|
|
@ -316,6 +316,7 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) {
|
||||||
table := schemas.NewEmptyTable()
|
table := schemas.NewEmptyTable()
|
||||||
table.Type = t
|
table.Type = t
|
||||||
table.Name = names.GetTableName(parser.tableMapper, v)
|
table.Name = names.GetTableName(parser.tableMapper, v)
|
||||||
|
table.Comment = names.GetTableComment(v)
|
||||||
|
|
||||||
for i := 0; i < t.NumField(); i++ {
|
for i := 0; i < t.NumField(); i++ {
|
||||||
col, err := parser.parseField(table, i, t.Field(i), v.Field(i))
|
col, err := parser.parseField(table, i, t.Field(i), v.Field(i))
|
||||||
|
|
|
@ -26,6 +26,20 @@ func (p ParseTableName2) TableName() string {
|
||||||
return "p_parseTableName"
|
return "p_parseTableName"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ParseTableComment struct{}
|
||||||
|
|
||||||
|
type ParseTableComment1 struct{}
|
||||||
|
|
||||||
|
type ParseTableComment2 struct{}
|
||||||
|
|
||||||
|
func (p ParseTableComment1) TableComment() string {
|
||||||
|
return "p_parseTableComment1"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ParseTableComment2) TableComment() string {
|
||||||
|
return "p_parseTableComment2"
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseTableName(t *testing.T) {
|
func TestParseTableName(t *testing.T) {
|
||||||
parser := NewParser(
|
parser := NewParser(
|
||||||
"xorm",
|
"xorm",
|
||||||
|
@ -47,6 +61,36 @@ func TestParseTableName(t *testing.T) {
|
||||||
assert.EqualValues(t, "p_parseTableName", table.Name)
|
assert.EqualValues(t, "p_parseTableName", table.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseTableComment(t *testing.T) {
|
||||||
|
parser := NewParser(
|
||||||
|
"xorm",
|
||||||
|
dialects.QueryDialect("mysql"),
|
||||||
|
names.SnakeMapper{},
|
||||||
|
names.SnakeMapper{},
|
||||||
|
caches.NewManager(),
|
||||||
|
)
|
||||||
|
|
||||||
|
table, err := parser.Parse(reflect.ValueOf(new(ParseTableComment)))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "", table.Comment)
|
||||||
|
|
||||||
|
table, err = parser.Parse(reflect.ValueOf(new(ParseTableComment1)))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "p_parseTableComment1", table.Comment)
|
||||||
|
|
||||||
|
table, err = parser.Parse(reflect.ValueOf(ParseTableComment1{}))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "p_parseTableComment1", table.Comment)
|
||||||
|
|
||||||
|
table, err = parser.Parse(reflect.ValueOf(new(ParseTableComment2)))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "p_parseTableComment2", table.Comment)
|
||||||
|
|
||||||
|
table, err = parser.Parse(reflect.ValueOf(ParseTableComment2{}))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "p_parseTableComment2", table.Comment)
|
||||||
|
}
|
||||||
|
|
||||||
func TestUnexportField(t *testing.T) {
|
func TestUnexportField(t *testing.T) {
|
||||||
parser := NewParser(
|
parser := NewParser(
|
||||||
"xorm",
|
"xorm",
|
||||||
|
|
Loading…
Reference in New Issue