func Test_userinfo(t *testing.T) { pool := db.NewDefaultDbPool() err := dbpool.InitPool("test", "mysql", "root:@tcp(192.168.16.58:3306)/test", 5) if err != nil { t.Fatal(err) } table := TableUserInfo{} table.Init() row1 := table.NewRow() row1.SetId(1) row1.SetUserName("yjx1") fmt.Println("add row1:", table.AddRow(row1)) fmt.Println("repeat add row1:", table.AddRow(row1)) fmt.Println("insert row1:", row1.InsertSql(nil)) row2 := table.NewRow() row2.SetId(2) row2.SetUserName("yjx2") fmt.Println("add row2:", table.AddRow(row2)) fmt.Println("insert row1:", row2.InsertSql(nil)) row3 := table.NewRow() row3.SetId(1) b, row4 := table.FindRow(row3) fmt.Println("b,", b, "row3:", row4) row3.SetUserName("yjx21") fmt.Println("update", table.UpdateRow(row3)) fmt.Println(table.OrmTable) fmt.Println("delete", table.DeleteRow(row2)) fmt.Println(table.OrmTable) }
func (this *OrmCmd) executecmd(cmd *cmdline) { switch cmd.word { case SET: if this.dbpool == nil { this.dbpool = db.NewDefaultDbPool() } strconn := this.dblink if this.dbtype == "mysql" { strconn += "/information_schema" } err := this.dbpool.InitPool(this.dbname, this.dbtype, strconn, 1) if err != nil { fmt.Println("数据库连接失败,请重新设置数据库信息") } else { fmt.Println("亲,数据库已经连上了") } case HELP: fmt.Println("Useage:set db = dbtype/dbname/dbuser:dbpwd@dbaddress:dbport") fmt.Println("Useage:orm -p packagename -table tablesneedtoorm [-path filestorepath]") fmt.Println("Useage:loadconf config.ini") fmt.Println("Useage:help") case ORM: if this.dblink == "" { fmt.Println("数据库尚未设置,无法做ORM操作,请先设置数据库") } else { this.packagename = cmd.kv["-p"] if cmd.kv["-path"] != "" { this.storepath = cmd.kv["-path"] } this.ormtables = cmd.kv["-table"] this.orm(this.ormtables) } case CONF: configfile := cmd.kv["conf"] conf := OrmConfig{} err := conf.LoadConfig(configfile) if err != nil { fmt.Println(err.Error()) } else { this.dbtype = conf.dbtype this.dbname = conf.dbname this.dblink = conf.dblink if this.dbpool == nil { this.dbpool = db.NewDefaultDbPool() } strconn := this.dblink if this.dbtype == "mysql" { strconn += "/information_schema" } err := this.dbpool.InitPool(this.dbname, this.dbtype, strconn, 1) if err != nil { fmt.Println("数据库连接失败,请重新设置数据库信息") } else { fmt.Println("亲,数据库已经连上了") } } } }