github.com/team-ide/go-dialect@v1.9.20/test/dialect_test.go (about) 1 package test 2 3 import ( 4 "database/sql" 5 "encoding/json" 6 "fmt" 7 "github.com/team-ide/go-dialect/dialect" 8 "github.com/team-ide/go-dialect/worker" 9 ) 10 11 func getTable() (table *dialect.TableModel) { 12 table = &dialect.TableModel{ 13 TableName: "USER_INFO", 14 TableComment: "用户信息", 15 ColumnList: []*dialect.ColumnModel{ 16 {ColumnName: "userId", ColumnDataType: "bigint", ColumnLength: 20, PrimaryKey: true}, 17 {ColumnName: "name", ColumnDataType: "varchar", ColumnLength: 200}, 18 {ColumnName: "account", ColumnDataType: "varchar", ColumnLength: 50}, 19 {ColumnName: "status", ColumnDataType: "int", ColumnLength: 3}, 20 {ColumnName: "deleted", ColumnDataType: "bit", ColumnLength: 1}, 21 {ColumnName: "detail", ColumnDataType: "text", ColumnLength: 500}, 22 {ColumnName: "detail2", ColumnDataType: "longtext", ColumnLength: 500}, 23 {ColumnName: "detail3", ColumnDataType: "blob", ColumnLength: 500}, 24 {ColumnName: "detail4", ColumnDataType: "longblob", ColumnLength: 500}, 25 {ColumnName: "createDate", ColumnDataType: "date", ColumnLength: 20}, 26 {ColumnName: "createDate1", ColumnDataType: "datetime", ColumnLength: 20}, 27 }, 28 IndexList: []*dialect.IndexModel{ 29 {IndexName: "account", IndexType: "UNIQUE", ColumnNames: []string{"account"}}, 30 }, 31 } 32 return 33 } 34 35 func testDLL(db *sql.DB, dia dialect.Dialect, ownerName string) { 36 //initKingBase() 37 table := getTable() 38 testTableCreate(db, dia, ownerName, getTable()) 39 40 testColumnUpdate(db, dia, ownerName, table.TableName, 41 &dialect.ColumnModel{ 42 ColumnName: "name", 43 ColumnDataType: "varchar", 44 ColumnLength: 500, 45 ColumnComment: "name1注释", 46 }, &dialect.ColumnModel{ 47 ColumnName: "name1", 48 ColumnDataType: "varchar", 49 ColumnLength: 600, 50 ColumnComment: "name1注释", 51 }, 52 ) 53 testColumnDelete(db, dia, ownerName, table.TableName, "detail3") 54 testColumnAdd(db, dia, ownerName, table.TableName, &dialect.ColumnModel{ 55 ColumnName: "name2", 56 ColumnDataType: "varchar", 57 ColumnLength: 500, 58 ColumnComment: "name2注释", 59 }) 60 tableDetail(db, dia, ownerName, table.TableName) 61 testTableDelete(db, dia, ownerName, table.TableName) 62 } 63 64 func testSql(db *sql.DB, dia dialect.Dialect, ownerName, sqlInfo string) { 65 sqlList := dia.SqlSplit(sqlInfo) 66 exec(db, sqlList) 67 tables(db, dia, ownerName) 68 } 69 70 func testOwnerCreate(db *sql.DB, dia dialect.Dialect, owner *dialect.OwnerModel) { 71 sqlList, err := dia.OwnerCreateSql(nil, owner) 72 if err != nil { 73 panic(err) 74 } 75 fmt.Println("--------owner [" + owner.OwnerName + "] create--------") 76 exec(db, sqlList) 77 fmt.Println() 78 fmt.Println() 79 } 80 81 func testOwnerDelete(db *sql.DB, dia dialect.Dialect, ownerName string) { 82 sqlList, err := dia.OwnerDeleteSql(nil, ownerName) 83 if err != nil { 84 panic(err) 85 } 86 fmt.Println("--------owner [" + ownerName + "] delete--------") 87 exec(db, sqlList) 88 fmt.Println() 89 fmt.Println() 90 } 91 func testTableCreate(db *sql.DB, dia dialect.Dialect, ownerName string, table *dialect.TableModel) { 92 sqlList, err := dia.TableCreateSql(nil, ownerName, table) 93 if err != nil { 94 panic(err) 95 } 96 fmt.Println("--------owner [" + ownerName + "] table [" + table.TableName + "] create--------") 97 exec(db, sqlList) 98 fmt.Println() 99 fmt.Println() 100 101 } 102 func testTableDelete(db *sql.DB, dia dialect.Dialect, ownerName string, tableName string) { 103 sqlList, err := dia.TableDeleteSql(nil, ownerName, tableName) 104 if err != nil { 105 panic(err) 106 } 107 fmt.Println("--------owner [" + ownerName + "] table [" + tableName + "] delete--------") 108 exec(db, sqlList) 109 fmt.Println() 110 fmt.Println() 111 112 } 113 func testColumnAdd(db *sql.DB, dia dialect.Dialect, ownerName string, tableName string, column *dialect.ColumnModel) { 114 sqlList, err := dia.ColumnAddSql(nil, ownerName, tableName, column) 115 if err != nil { 116 panic(err) 117 } 118 fmt.Println("--------owner [" + ownerName + "] table [" + tableName + "] column [" + column.ColumnName + "] add--------") 119 exec(db, sqlList) 120 fmt.Println() 121 fmt.Println() 122 123 } 124 func testColumnUpdate(db *sql.DB, dia dialect.Dialect, ownerName string, tableName string, oldColumn *dialect.ColumnModel, newColumn *dialect.ColumnModel) { 125 sqlList, err := dia.ColumnUpdateSql(nil, ownerName, tableName, oldColumn, newColumn) 126 if err != nil { 127 panic(err) 128 } 129 fmt.Println("--------owner [" + ownerName + "] table [" + tableName + "] column [" + oldColumn.ColumnName + "] update--------") 130 exec(db, sqlList) 131 fmt.Println() 132 fmt.Println() 133 134 } 135 func testColumnDelete(db *sql.DB, dia dialect.Dialect, ownerName string, tableName string, columnName string) { 136 sqlList, err := dia.ColumnDeleteSql(nil, ownerName, tableName, columnName) 137 if err != nil { 138 panic(err) 139 } 140 fmt.Println("--------owner [" + ownerName + "] table [" + tableName + "] column [" + columnName + "] delete--------") 141 exec(db, sqlList) 142 fmt.Println() 143 fmt.Println() 144 145 } 146 147 func exec(db *sql.DB, sqlList []string) { 148 if len(sqlList) == 0 { 149 return 150 } 151 for _, one := range sqlList { 152 if one == "" { 153 continue 154 } 155 fmt.Printf("%s\n", one) 156 _, err := db.Exec(one) 157 if err != nil { 158 fmt.Println("error sql:" + one) 159 panic(err) 160 return 161 } 162 163 } 164 165 } 166 167 func owners(db *sql.DB, dia dialect.Dialect) { 168 fmt.Println("--------owners--------") 169 list, err := worker.OwnersSelect(db, dia, nil) 170 if err != nil { 171 panic(err) 172 } 173 for _, one := range list { 174 if one.Error != "" { 175 println("owner error:" + one.Error) 176 continue 177 } 178 179 bs, _ := json.Marshal(one) 180 fmt.Printf("%s\n", bs) 181 tables(db, dia, one.OwnerName) 182 183 } 184 185 } 186 187 func tables(db *sql.DB, dia dialect.Dialect, ownerName string) { 188 fmt.Println("--------owner [" + ownerName + "] tables--------") 189 list, err := worker.TablesSelect(db, dia, nil, ownerName) 190 if err != nil { 191 panic(err) 192 } 193 for _, one := range list { 194 if one.Error != "" { 195 println("table error:" + one.Error) 196 continue 197 } 198 199 bs, _ := json.Marshal(one) 200 fmt.Printf("%s\n", bs) 201 tableDetail(db, dia, ownerName, one.OwnerName) 202 } 203 204 } 205 206 func tableDetail(db *sql.DB, dia dialect.Dialect, ownerName string, tableName string) { 207 fmt.Println("--------owner [" + ownerName + "] table [" + tableName + "] detail--------") 208 table, err := worker.TableDetail(db, dia, nil, ownerName, tableName, false) 209 if err != nil { 210 panic(err) 211 } 212 213 bs, _ := json.MarshalIndent(table, "", " ") 214 fmt.Printf("%s\n", bs) 215 216 }