fixed field mapping length bug
This commit is contained in:
parent
a2ebf21969
commit
0517c1ddcf
42
engine.go
42
engine.go
|
@ -106,7 +106,7 @@ func (e *Engine) genColumnStr(col *Column) string {
|
||||||
} else {
|
} else {
|
||||||
sql += col.SQLType.Name
|
sql += col.SQLType.Name
|
||||||
}
|
}
|
||||||
if e.DriverName != SQLITE {
|
if e.DriverName != SQLITE && col.SQLType != Text {
|
||||||
if col.SQLType != Decimal {
|
if col.SQLType != Decimal {
|
||||||
sql += "(" + strconv.Itoa(col.Length) + ")"
|
sql += "(" + strconv.Itoa(col.Length) + ")"
|
||||||
} else {
|
} else {
|
||||||
|
@ -170,7 +170,7 @@ func (engine *Engine) MapType(t reflect.Type) Table {
|
||||||
fieldType := t.Field(i).Type
|
fieldType := t.Field(i).Type
|
||||||
|
|
||||||
if ormTagStr != "" {
|
if ormTagStr != "" {
|
||||||
col = Column{FieldName: t.Field(i).Name}
|
col = Column{FieldName: t.Field(i).Name, Nullable: true}
|
||||||
ormTagStr = strings.ToLower(ormTagStr)
|
ormTagStr = strings.ToLower(ormTagStr)
|
||||||
tags := strings.Split(ormTagStr, " ")
|
tags := strings.Split(ormTagStr, " ")
|
||||||
// TODO:
|
// TODO:
|
||||||
|
@ -179,26 +179,50 @@ func (engine *Engine) MapType(t reflect.Type) Table {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for j, key := range tags {
|
for j, key := range tags {
|
||||||
switch k := strings.ToLower(key); k {
|
k := strings.ToLower(key)
|
||||||
case "pk":
|
switch {
|
||||||
|
case k == "pk":
|
||||||
col.IsPrimaryKey = true
|
col.IsPrimaryKey = true
|
||||||
pkCol = &col
|
pkCol = &col
|
||||||
case "null":
|
case k == "null":
|
||||||
col.Nullable = (tags[j-1] != "not")
|
col.Nullable = (tags[j-1] != "not")
|
||||||
case "autoincr":
|
case k == "autoincr":
|
||||||
col.IsAutoIncrement = true
|
col.IsAutoIncrement = true
|
||||||
case "default":
|
case k == "default":
|
||||||
col.Default = tags[j+1]
|
col.Default = tags[j+1]
|
||||||
case "int":
|
case k == "text":
|
||||||
|
col.SQLType = Text
|
||||||
|
case strings.HasPrefix(k, "int"):
|
||||||
col.SQLType = Int
|
col.SQLType = Int
|
||||||
case "not":
|
lens := k[len("int")+1 : len(k)-1]
|
||||||
|
col.Length, _ = strconv.Atoi(lens)
|
||||||
|
case strings.HasPrefix(k, "varchar"):
|
||||||
|
col.SQLType = Varchar
|
||||||
|
lens := k[len("decimal")+1 : len(k)-1]
|
||||||
|
col.Length, _ = strconv.Atoi(lens)
|
||||||
|
case strings.HasPrefix(k, "decimal"):
|
||||||
|
col.SQLType = Decimal
|
||||||
|
lens := k[len("decimal")+1 : len(k)-1]
|
||||||
|
twolen := strings.Split(lens, ",")
|
||||||
|
col.Length, _ = strconv.Atoi(twolen[0])
|
||||||
|
col.Length2, _ = strconv.Atoi(twolen[1])
|
||||||
|
case k == "date":
|
||||||
|
col.SQLType = Date
|
||||||
|
case k == "not":
|
||||||
default:
|
default:
|
||||||
|
if k != col.Default {
|
||||||
col.Name = k
|
col.Name = k
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if col.SQLType.Name == "" {
|
if col.SQLType.Name == "" {
|
||||||
col.SQLType = Type2SQLType(fieldType)
|
col.SQLType = Type2SQLType(fieldType)
|
||||||
|
}
|
||||||
|
|
||||||
|
if col.Length == 0 {
|
||||||
col.Length = col.SQLType.DefaultLength
|
col.Length = col.SQLType.DefaultLength
|
||||||
|
}
|
||||||
|
if col.Length2 == 0 {
|
||||||
col.Length2 = col.SQLType.DefaultLength2
|
col.Length2 = col.SQLType.DefaultLength2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
16
session.go
16
session.go
|
@ -436,11 +436,13 @@ func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
colMultiPlaces := make([]string, 0)
|
colMultiPlaces := make([]string, 0)
|
||||||
var args = make([]interface{}, 0)
|
var args = make([]interface{}, 0)
|
||||||
|
cols := make([]Column, 0)
|
||||||
|
|
||||||
for i := 0; i < size; i++ {
|
for i := 0; i < size; i++ {
|
||||||
elemValue := sliceValue.Index(i).Interface()
|
elemValue := sliceValue.Index(i).Interface()
|
||||||
colPlaces := make([]string, 0)
|
colPlaces := make([]string, 0)
|
||||||
|
|
||||||
|
if i == 0 {
|
||||||
for _, col := range table.Columns {
|
for _, col := range table.Columns {
|
||||||
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
||||||
val := fieldValue.Interface()
|
val := fieldValue.Interface()
|
||||||
|
@ -448,11 +450,21 @@ func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
args = append(args, val)
|
args = append(args, val)
|
||||||
if i == 0 {
|
|
||||||
colNames = append(colNames, col.Name)
|
colNames = append(colNames, col.Name)
|
||||||
}
|
cols = append(cols, col)
|
||||||
colPlaces = append(colPlaces, "?")
|
colPlaces = append(colPlaces, "?")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
for _, col := range cols {
|
||||||
|
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
||||||
|
val := fieldValue.Interface()
|
||||||
|
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
args = append(args, val)
|
||||||
|
colPlaces = append(colPlaces, "?")
|
||||||
|
}
|
||||||
|
}
|
||||||
colMultiPlaces = append(colMultiPlaces, strings.Join(colPlaces, ", "))
|
colMultiPlaces = append(colMultiPlaces, strings.Join(colPlaces, ", "))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
table.go
9
table.go
|
@ -18,6 +18,7 @@ var (
|
||||||
Char = SQLType{"char", 1, 0}
|
Char = SQLType{"char", 1, 0}
|
||||||
Bool = SQLType{"int", 1, 0}
|
Bool = SQLType{"int", 1, 0}
|
||||||
Varchar = SQLType{"varchar", 50, 0}
|
Varchar = SQLType{"varchar", 50, 0}
|
||||||
|
Text = SQLType{"text", 16, 0}
|
||||||
Date = SQLType{"date", 24, 0}
|
Date = SQLType{"date", 24, 0}
|
||||||
Decimal = SQLType{"decimal", 26, 2}
|
Decimal = SQLType{"decimal", 26, 2}
|
||||||
Float = SQLType{"float", 31, 0}
|
Float = SQLType{"float", 31, 0}
|
||||||
|
@ -77,14 +78,6 @@ func (table *Table) ColumnStr() string {
|
||||||
return strings.Join(colNames, ", ")
|
return strings.Join(colNames, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
/*func (table *Table) PlaceHolders() string {
|
|
||||||
colNames := make([]string, 0)
|
|
||||||
for _, col := range table.Columns {
|
|
||||||
colNames = append(colNames, "?")
|
|
||||||
}
|
|
||||||
return strings.Join(colNames, ", ")
|
|
||||||
}*/
|
|
||||||
|
|
||||||
func (table *Table) PKColumn() Column {
|
func (table *Table) PKColumn() Column {
|
||||||
return table.Columns[table.PrimaryKey]
|
return table.Columns[table.PrimaryKey]
|
||||||
}
|
}
|
||||||
|
|
13
xorm_test.go
13
xorm_test.go
|
@ -35,8 +35,8 @@ type Userinfo struct {
|
||||||
|
|
||||||
type Userdetail struct {
|
type Userdetail struct {
|
||||||
Uid int `xorm:"id pk not null"`
|
Uid int `xorm:"id pk not null"`
|
||||||
Intro string
|
Intro string `xorm:"text"`
|
||||||
Profile string
|
Profile string `xorm:"varchar(2000)"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var engine xorm.Engine
|
var engine xorm.Engine
|
||||||
|
@ -106,9 +106,12 @@ func insertAutoIncr(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertMulti(t *testing.T) {
|
func insertMulti(t *testing.T) {
|
||||||
users := []*Userinfo{
|
engine.InsertMany = true
|
||||||
|
users := []Userinfo{
|
||||||
{Username: "xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
{Username: "xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||||
{Username: "xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
{Username: "xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||||
|
{Username: "xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||||
|
{Username: "xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||||
}
|
}
|
||||||
_, err := engine.Insert(&users)
|
_, err := engine.Insert(&users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -117,9 +120,11 @@ func insertMulti(t *testing.T) {
|
||||||
|
|
||||||
engine.InsertMany = false
|
engine.InsertMany = false
|
||||||
|
|
||||||
users = []*Userinfo{
|
users = []Userinfo{
|
||||||
{Username: "xlw9", Departname: "dev", Alias: "lunny9", Created: time.Now()},
|
{Username: "xlw9", Departname: "dev", Alias: "lunny9", Created: time.Now()},
|
||||||
{Username: "xlw10", Departname: "dev", Alias: "lunny10", Created: time.Now()},
|
{Username: "xlw10", Departname: "dev", Alias: "lunny10", Created: time.Now()},
|
||||||
|
{Username: "xlw99", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||||
|
{Username: "xlw1010", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||||
}
|
}
|
||||||
_, err = engine.Insert(&users)
|
_, err = engine.Insert(&users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue