mirror of
https://github.com/tangpanqing/aorm.git
synced 2025-10-23 07:49:32 +08:00
修改部分类型转换
This commit is contained in:
2
go.mod
2
go.mod
@@ -2,6 +2,4 @@ module github.com/tangpanqing/aorm
|
||||
|
||||
go 1.18
|
||||
|
||||
require gopkg.in/guregu/null.v4 v4.0.0
|
||||
|
||||
require github.com/go-sql-driver/mysql v1.7.0
|
||||
|
27
migrate.go
27
migrate.go
@@ -163,7 +163,12 @@ func (db *Executor) getColumnsFromDb(dbName string, tableName string) []Column {
|
||||
dataColumn, _ := db.Query(sqlColumn)
|
||||
|
||||
for j := 0; j < len(dataColumn); j++ {
|
||||
dataType := dataColumn[j]["DATA_TYPE"].(string)
|
||||
maxLength, _ := strconv.Atoi(fmt.Sprintf("%v", dataColumn[j]["CHARACTER_MAXIMUM_LENGTH"]))
|
||||
if dataType == "text" && maxLength == 65535 {
|
||||
maxLength = 0
|
||||
}
|
||||
|
||||
defaultVal := ""
|
||||
if dataColumn[j]["COLUMN_DEFAULT"] != nil {
|
||||
defaultVal = dataColumn[j]["COLUMN_DEFAULT"].(string)
|
||||
@@ -171,7 +176,7 @@ func (db *Executor) getColumnsFromDb(dbName string, tableName string) []Column {
|
||||
|
||||
columnsFromDb = append(columnsFromDb, Column{
|
||||
ColumnName: dataColumn[j]["COLUMN_NAME"].(string),
|
||||
DataType: dataColumn[j]["DATA_TYPE"].(string),
|
||||
DataType: dataType,
|
||||
IsNullable: dataColumn[j]["IS_NULLABLE"].(string),
|
||||
MaxLength: maxLength,
|
||||
ColumnType: dataColumn[j]["COLUMN_TYPE"].(string),
|
||||
@@ -203,13 +208,6 @@ func (db *Executor) getIndexsFromDb(tableName string) []Index {
|
||||
|
||||
// 修改表
|
||||
func (db *Executor) modifyTable(tableFromCode Table, columnsFromCode []Column, indexsFromCode []Index, tableFromDb Table, columnsFromDb []Column, indexsFromDb []Index) {
|
||||
//fmt.Println("正在修改表" + tableFromCode.TableName)
|
||||
//fmt.Println(columnsFromCode)
|
||||
//fmt.Println(columnsFromDb)
|
||||
|
||||
//fmt.Println(indexsFromCode)
|
||||
//fmt.Println(indexsFromDb)
|
||||
|
||||
if tableFromCode.Engine != tableFromDb.Engine {
|
||||
sql := "ALTER TABLE " + tableFromCode.TableName + " Engine " + tableFromCode.Engine
|
||||
_, err := db.Exec(sql)
|
||||
@@ -239,7 +237,7 @@ func (db *Executor) modifyTable(tableFromCode Table, columnsFromCode []Column, i
|
||||
if columnCode.ColumnName == columnDb.ColumnName {
|
||||
isFind = 1
|
||||
|
||||
if columnCode.ColumnType != columnDb.ColumnType || columnCode.ColumnComment != columnDb.ColumnComment || columnCode.Extra != columnDb.Extra || columnCode.DefaultVal != columnDb.DefaultVal {
|
||||
if columnCode.DataType != columnDb.DataType || columnCode.MaxLength != columnDb.MaxLength || columnCode.ColumnComment != columnDb.ColumnComment || columnCode.Extra != columnDb.Extra || columnCode.DefaultVal != columnDb.DefaultVal {
|
||||
sql := "ALTER TABLE " + tableFromCode.TableName + " MODIFY " + getColumnStr(columnCode)
|
||||
_, err := db.Exec(sql)
|
||||
if err != nil {
|
||||
@@ -494,11 +492,16 @@ func getMaxLength(DataType string, fieldMap map[string]string) int {
|
||||
func getNullAble(fieldMap map[string]string) string {
|
||||
var IsNullable string
|
||||
|
||||
_, ok := fieldMap["not null"]
|
||||
if ok {
|
||||
_, primaryOk := fieldMap["primary"]
|
||||
if primaryOk {
|
||||
IsNullable = "NO"
|
||||
} else {
|
||||
IsNullable = "YES"
|
||||
_, ok := fieldMap["not null"]
|
||||
if ok {
|
||||
IsNullable = "NO"
|
||||
} else {
|
||||
IsNullable = "YES"
|
||||
}
|
||||
}
|
||||
|
||||
return IsNullable
|
||||
|
@@ -25,7 +25,7 @@ type ArticleVO struct {
|
||||
}
|
||||
|
||||
type Person struct {
|
||||
Id aorm.Int `aorm:"primary;auto_increment;type:bigint" json:"id"`
|
||||
Id aorm.Int `aorm:"primary;auto_increment" json:"id"`
|
||||
Name aorm.String `aorm:"size:100;not null;comment:名字" json:"name"`
|
||||
Sex aorm.Bool `aorm:"index;comment:性别" json:"sex"`
|
||||
Age aorm.Int `aorm:"index;comment:年龄" json:"age"`
|
||||
@@ -41,17 +41,15 @@ type PersonAge struct {
|
||||
}
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
|
||||
db := testConnect()
|
||||
defer db.Close()
|
||||
|
||||
testMigrate(db)
|
||||
|
||||
testShowCreateTable(db)
|
||||
|
||||
id := testInsert(db)
|
||||
|
||||
fmt.Println(id)
|
||||
return
|
||||
testGetOne(db, id)
|
||||
testGetMany(db)
|
||||
testUpdate(db, id)
|
||||
@@ -119,8 +117,8 @@ func testMigrate(db *sql.DB) {
|
||||
func testShowCreateTable(db *sql.DB) {
|
||||
fmt.Println("--- testShowCreateTable ---")
|
||||
|
||||
//showCreate := aorm.Use(db).ShowCreateTable("person")
|
||||
//fmt.Println(showCreate)
|
||||
showCreate := aorm.Use(db).ShowCreateTable("person")
|
||||
fmt.Println(showCreate)
|
||||
}
|
||||
|
||||
func testInsert(db *sql.DB) int64 {
|
||||
@@ -213,7 +211,7 @@ func testWhere(db *sql.DB) {
|
||||
where1 = append(where1, aorm.WhereItem{Field: "money", Opt: aorm.Eq, Val: 100.15})
|
||||
where1 = append(where1, aorm.WhereItem{Field: "name", Opt: aorm.Like, Val: []string{"%", "li", "%"}})
|
||||
|
||||
aorm.Use(db).Debug(true).Table("person").Where(where1).GetMany(&listByWhere)
|
||||
aorm.Use(db).Debug(true).Table("person").WhereArr(where1).GetMany(&listByWhere)
|
||||
for i := 0; i < len(listByWhere); i++ {
|
||||
fmt.Println(listByWhere[i])
|
||||
}
|
||||
@@ -387,11 +385,11 @@ func testValueFloat32(db *sql.DB, id int64) {
|
||||
func testValueFloat64(db *sql.DB, id int64) {
|
||||
fmt.Println("--- testValueFloat64 ---")
|
||||
|
||||
money, err := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).ValueFloat64("test")
|
||||
test, err := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).ValueFloat64("test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(money)
|
||||
fmt.Println(test)
|
||||
}
|
||||
|
||||
func testCount(db *sql.DB) {
|
||||
@@ -470,7 +468,7 @@ func testTransaction(db *sql.DB) {
|
||||
|
||||
tx, _ := db.Begin()
|
||||
|
||||
id, errInsert := aorm.Use(tx).Insert(&Person{
|
||||
id, errInsert := aorm.Use(tx).Debug(true).Insert(&Person{
|
||||
Name: aorm.StringFrom("Alice"),
|
||||
})
|
||||
|
||||
@@ -480,7 +478,7 @@ func testTransaction(db *sql.DB) {
|
||||
return
|
||||
}
|
||||
|
||||
countUpdate, errUpdate := aorm.Use(tx).Where(&Person{
|
||||
countUpdate, errUpdate := aorm.Use(tx).Debug(true).Where(&Person{
|
||||
Id: aorm.IntFrom(id),
|
||||
}).Update(&Person{
|
||||
Name: aorm.StringFrom("Bob"),
|
||||
@@ -499,7 +497,7 @@ func testTransaction(db *sql.DB) {
|
||||
func testTruncate(db *sql.DB) {
|
||||
fmt.Println("--- testTruncate ---")
|
||||
|
||||
count, err := aorm.Use(db).Table("person").Truncate()
|
||||
count, err := aorm.Use(db).Debug(true).Table("person").Truncate()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user