resolved #6: add created & updated
This commit is contained in:
parent
5d523cc3b2
commit
695b89c35f
31
base_test.go
31
base_test.go
|
@ -704,6 +704,36 @@ func testCustomType(engine *Engine, t *testing.T) {
|
||||||
func testTrans(engine *Engine, t *testing.T) {
|
func testTrans(engine *Engine, t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserCU struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
Created time.Time `xorm:"created"`
|
||||||
|
Updated time.Time `xorm:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCreatedAndUpdated(engine *Engine, t *testing.T) {
|
||||||
|
u := new(UserCU)
|
||||||
|
err := engine.CreateTables(u)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
u.Name = "sss"
|
||||||
|
_, err = engine.Insert(u)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
u.Name = "xxx"
|
||||||
|
_, err = engine.Id(u.Id).Update(u)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testAll(engine *Engine, t *testing.T) {
|
func testAll(engine *Engine, t *testing.T) {
|
||||||
directCreateTable(engine, t)
|
directCreateTable(engine, t)
|
||||||
mapper(engine, t)
|
mapper(engine, t)
|
||||||
|
@ -737,4 +767,5 @@ func testAll(engine *Engine, t *testing.T) {
|
||||||
testExtends(engine, t)
|
testExtends(engine, t)
|
||||||
testColTypes(engine, t)
|
testColTypes(engine, t)
|
||||||
testCustomType(engine, t)
|
testCustomType(engine, t)
|
||||||
|
testCreatedAndUpdated(engine, t)
|
||||||
}
|
}
|
||||||
|
|
26
engine.go
26
engine.go
|
@ -277,13 +277,16 @@ func (engine *Engine) MapType(t reflect.Type) *Table {
|
||||||
case k == "PK":
|
case k == "PK":
|
||||||
col.IsPrimaryKey = true
|
col.IsPrimaryKey = true
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
table.PrimaryKey = col.Name
|
|
||||||
case k == "NULL":
|
case k == "NULL":
|
||||||
col.Nullable = (strings.ToUpper(tags[j-1]) != "NOT")
|
col.Nullable = (strings.ToUpper(tags[j-1]) != "NOT")
|
||||||
case k == "AUTOINCR":
|
case k == "AUTOINCR":
|
||||||
col.IsAutoIncrement = true
|
col.IsAutoIncrement = true
|
||||||
case k == "DEFAULT":
|
case k == "DEFAULT":
|
||||||
col.Default = tags[j+1]
|
col.Default = tags[j+1]
|
||||||
|
case k == "CREATED":
|
||||||
|
col.IsCreated = true
|
||||||
|
case k == "UPDATED":
|
||||||
|
col.IsUpdated = true
|
||||||
case strings.HasPrefix(k, "INDEX"):
|
case strings.HasPrefix(k, "INDEX"):
|
||||||
if k == "INDEX" {
|
if k == "INDEX" {
|
||||||
col.IndexName = ""
|
col.IndexName = ""
|
||||||
|
@ -302,7 +305,11 @@ func (engine *Engine) MapType(t reflect.Type) *Table {
|
||||||
}
|
}
|
||||||
case k == "NOT":
|
case k == "NOT":
|
||||||
default:
|
default:
|
||||||
if strings.Contains(k, "(") && strings.HasSuffix(k, ")") {
|
if strings.HasPrefix(k, "'") && strings.HasSuffix(k, "'") {
|
||||||
|
if key != col.Default {
|
||||||
|
col.Name = key[1 : len(key)-1]
|
||||||
|
}
|
||||||
|
} else if strings.Contains(k, "(") && strings.HasSuffix(k, ")") {
|
||||||
fs := strings.Split(k, "(")
|
fs := strings.Split(k, "(")
|
||||||
if _, ok := sqlTypes[fs[0]]; !ok {
|
if _, ok := sqlTypes[fs[0]]; !ok {
|
||||||
continue
|
continue
|
||||||
|
@ -318,7 +325,7 @@ func (engine *Engine) MapType(t reflect.Type) *Table {
|
||||||
} else {
|
} else {
|
||||||
if _, ok := sqlTypes[k]; ok {
|
if _, ok := sqlTypes[k]; ok {
|
||||||
col.SQLType = SQLType{k, 0, 0}
|
col.SQLType = SQLType{k, 0, 0}
|
||||||
} else if k != col.Default {
|
} else if key != col.Default {
|
||||||
col.Name = key
|
col.Name = key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -339,6 +346,15 @@ func (engine *Engine) MapType(t reflect.Type) *Table {
|
||||||
if col.Name == "" {
|
if col.Name == "" {
|
||||||
col.Name = engine.Mapper.Obj2Table(t.Field(i).Name)
|
col.Name = engine.Mapper.Obj2Table(t.Field(i).Name)
|
||||||
}
|
}
|
||||||
|
if col.IsPrimaryKey {
|
||||||
|
table.PrimaryKey = col.Name
|
||||||
|
}
|
||||||
|
if col.IsCreated {
|
||||||
|
table.Created = col.Name
|
||||||
|
}
|
||||||
|
if col.IsUpdated {
|
||||||
|
table.Updated = col.Name
|
||||||
|
}
|
||||||
if col.IndexType == SINGLEINDEX {
|
if col.IndexType == SINGLEINDEX {
|
||||||
col.IndexName = col.Name
|
col.IndexName = col.Name
|
||||||
table.Indexes[col.IndexName] = []string{col.Name}
|
table.Indexes[col.IndexName] = []string{col.Name}
|
||||||
|
@ -364,8 +380,8 @@ func (engine *Engine) MapType(t reflect.Type) *Table {
|
||||||
} else {
|
} else {
|
||||||
sqlType := Type2SQLType(fieldType)
|
sqlType := Type2SQLType(fieldType)
|
||||||
col = &Column{engine.Mapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
|
col = &Column{engine.Mapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
|
||||||
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", NONEUNIQUE, "", NONEINDEX, "", false, false, TWOSIDES}
|
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", NONEUNIQUE, "",
|
||||||
|
NONEINDEX, "", false, false, TWOSIDES, false, false}
|
||||||
}
|
}
|
||||||
if col.IsAutoIncrement {
|
if col.IsAutoIncrement {
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
|
|
59
session.go
59
session.go
|
@ -243,13 +243,7 @@ func (session *Session) scanMapIntoStruct(obj interface{}, objMap map[string][]b
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
v = (string(data) == "1")
|
v = (string(data) == "1")
|
||||||
structField.Set(reflect.ValueOf(v))
|
structField.Set(reflect.ValueOf(v))
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
x, err := strconv.Atoi(string(data))
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("arg " + key + " as int: " + err.Error())
|
|
||||||
}
|
|
||||||
structField.SetInt(int64(x))
|
|
||||||
case reflect.Int64:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
x, err := strconv.ParseInt(string(data), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("arg " + key + " as int: " + err.Error())
|
return errors.New("arg " + key + " as int: " + err.Error())
|
||||||
|
@ -814,12 +808,21 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
||||||
if col.MapType == ONLYFROMDB {
|
if col.MapType == ONLYFROMDB {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
arg, err := session.value2Interface(fieldValue)
|
if session.Statement.ColumnStr != "" {
|
||||||
if err != nil {
|
if _, ok := session.Statement.columnMap[col.Name]; !ok {
|
||||||
return 0, err
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if col.IsCreated || col.IsUpdated {
|
||||||
|
args = append(args, time.Now())
|
||||||
|
} else {
|
||||||
|
arg, err := session.value2Interface(fieldValue)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
args = append(args, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, arg)
|
|
||||||
colNames = append(colNames, col.Name)
|
colNames = append(colNames, col.Name)
|
||||||
cols = append(cols, col)
|
cols = append(cols, col)
|
||||||
colPlaces = append(colPlaces, "?")
|
colPlaces = append(colPlaces, "?")
|
||||||
|
@ -838,12 +841,16 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
arg, err := session.value2Interface(fieldValue)
|
if col.IsCreated || col.IsUpdated {
|
||||||
if err != nil {
|
args = append(args, time.Now())
|
||||||
return 0, err
|
} else {
|
||||||
|
arg, err := session.value2Interface(fieldValue)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
args = append(args, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, arg)
|
|
||||||
colPlaces = append(colPlaces, "?")
|
colPlaces = append(colPlaces, "?")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -951,12 +958,16 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
arg, err := session.value2Interface(fieldValue)
|
if col.IsCreated || col.IsUpdated {
|
||||||
if err != nil {
|
args = append(args, time.Now())
|
||||||
return 0, err
|
} else {
|
||||||
|
arg, err := session.value2Interface(fieldValue)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
args = append(args, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, arg)
|
|
||||||
colNames = append(colNames, col.Name)
|
colNames = append(colNames, col.Name)
|
||||||
colPlaces = append(colPlaces, "?")
|
colPlaces = append(colPlaces, "?")
|
||||||
}
|
}
|
||||||
|
@ -1033,10 +1044,15 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
table := session.Engine.AutoMap(bean)
|
table := session.Engine.AutoMap(bean)
|
||||||
session.Statement.RefTable = table
|
session.Statement.RefTable = table
|
||||||
colNames, args = BuildConditions(session.Engine, table, bean)
|
colNames, args = BuildConditions(session.Engine, table, bean)
|
||||||
|
if table.Updated != "" {
|
||||||
|
colNames = append(colNames, session.Engine.Quote(table.Updated)+" = ?")
|
||||||
|
args = append(args, time.Now())
|
||||||
|
}
|
||||||
} else if t.Kind() == reflect.Map {
|
} else if t.Kind() == reflect.Map {
|
||||||
if session.Statement.RefTable == nil {
|
if session.Statement.RefTable == nil {
|
||||||
return -1, TableNotFoundError
|
return -1, TableNotFoundError
|
||||||
}
|
}
|
||||||
|
table := session.Statement.RefTable
|
||||||
colNames = make([]string, 0)
|
colNames = make([]string, 0)
|
||||||
args = make([]interface{}, 0)
|
args = make([]interface{}, 0)
|
||||||
bValue := reflect.ValueOf(bean)
|
bValue := reflect.ValueOf(bean)
|
||||||
|
@ -1045,7 +1061,10 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
colNames = append(colNames, session.Engine.Quote(v.String())+" = ?")
|
colNames = append(colNames, session.Engine.Quote(v.String())+" = ?")
|
||||||
args = append(args, bValue.MapIndex(v).Interface())
|
args = append(args, bValue.MapIndex(v).Interface())
|
||||||
}
|
}
|
||||||
|
if table.Updated != "" {
|
||||||
|
colNames = append(colNames, session.Engine.Quote(table.Updated)+" = ?")
|
||||||
|
args = append(args, time.Now())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return -1, ParamsTypeError
|
return -1, ParamsTypeError
|
||||||
}
|
}
|
||||||
|
|
8
table.go
8
table.go
|
@ -124,9 +124,11 @@ func Type2SQLType(t reflect.Type) (st SQLType) {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if t == reflect.TypeOf(tm) {
|
if t == reflect.TypeOf(tm) {
|
||||||
st = SQLType{DateTime, 0, 0}
|
st = SQLType{DateTime, 0, 0}
|
||||||
|
} else {
|
||||||
|
st = SQLType{Text, 0, 0}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
st = SQLType{Varchar, 255, 0}
|
st = SQLType{Text, 0, 0}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -164,6 +166,8 @@ type Column struct {
|
||||||
IsPrimaryKey bool
|
IsPrimaryKey bool
|
||||||
IsAutoIncrement bool
|
IsAutoIncrement bool
|
||||||
MapType int
|
MapType int
|
||||||
|
IsCreated bool
|
||||||
|
IsUpdated bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (col *Column) String(engine *Engine) string {
|
func (col *Column) String(engine *Engine) string {
|
||||||
|
@ -215,6 +219,8 @@ type Table struct {
|
||||||
Indexes map[string][]string
|
Indexes map[string][]string
|
||||||
Uniques map[string][]string
|
Uniques map[string][]string
|
||||||
PrimaryKey string
|
PrimaryKey string
|
||||||
|
Created string
|
||||||
|
Updated string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *Table) PKColumn() *Column {
|
func (table *Table) PKColumn() *Column {
|
||||||
|
|
Loading…
Reference in New Issue