package project import ( "Web/src/main/utils" "database/sql" "encoding/json" "fmt" _ "github.com/lib/pq" "sync" ) var db *sql.DB var err error var wg sync.WaitGroup var mt sync.Mutex func OpenDB() { //conf, err := ini.Load("conf.ini") //if err != nil { // fmt.Printf("%s", err) //} // //for _, v := range conf.Sections() { // fmt.Println(v.KeyStrings()) //} utils.IniInit() url := utils.GIniParser.GetString("Server", "url") dbType := utils.GIniParser.GetString("Server", "type") //dbType := "postgres" //dbType := "postgres://postgres:postgres@10.8.30.156:5432/Anxinyun0808?sslmode=disable" //url := "postgres://FashionAdmin:123456@10.8.30.36:5432/test" if db, err = sql.Open(dbType, url); err != nil { fmt.Printf("%s", err) return } else { err := db.Ping() if err != nil { fmt.Printf("%s", err) } println("Open db success") } } func deleteDb(str []byte) error { var body delete if err = json.Unmarshal(str, &body); err != nil { fmt.Printf("%s", err) return err } else { tableName := body.Table_name var k1 string var k2 string switch tableName { case "test_people": k1 = "name_people" k2 = "post_people" case "test_prpject": k1 = "name_project" k2 = "post_progress" case "test_waiting": k1 = "name_project" k2 = "progress" } v1 := body.Delete v2 := body.Delete_state deleteSQL := fmt.Sprintf("DELETE FROM \"%v\" WHERE %v = '%v' AND %v = '%v'", tableName, k1, v1, k2, v2) _, err = db.Exec(deleteSQL) if err != nil { fmt.Printf("%s", err) return err } return nil } } func ProjectInDB(str []byte) error { var bodys []Project if err = json.Unmarshal(str, &bodys); err != nil { return err } else { var Err chan error for _, body := range bodys { wg.Add(1) go func(body Project) { mt.Lock() defer mt.Unlock() defer wg.Done() var bytePeople []byte bytePeople, err = json.Marshal(body.Part_people) if err != nil { fmt.Printf("%s", err) } peopleSQL := fmt.Sprintf( "DO $$ BEGIN\n"+ "IF NOT EXISTS ( SELECT * FROM test_project WHERE name_project = '%v' ) THEN\n"+ "INSERT INTO test_project (name_project, build_time, publish_time, part_people, progress )VALUES( '%v', '%v', '%v', '%v', '%v' );\n"+ "ELSE\n"+ "UPDATE test_project SET build_time = '%v',publish_time = '%v',part_people = '%v',progress = '%v' WHERE name_project = '%v';\n"+ "END IF;\nEND $$", body.Name_project, body.Name_project, body.Build_time, body.Publish_time, string(bytePeople), body.Progress, body.Build_time, body.Publish_time, string(bytePeople), body.Progress, body.Name_project) //var result sql.Result _, err = db.Exec(peopleSQL) if err != nil { fmt.Printf("%s", err) } if err != nil { Err <- err } }(body) } wg.Wait() if len(Err) != 0 { return <-Err } return nil } } func PeopleInDB(str []byte) error { var bodys []People if err = json.Unmarshal(str, &bodys); err != nil { return err } else { var Err chan error for _, body := range bodys { wg.Add(1) go func(body People) { mt.Lock() defer mt.Unlock() defer wg.Done() var byteWork []byte byteWork, err = json.Marshal(body.Work) if err != nil { fmt.Printf("%s", err) } peopleSQL := fmt.Sprintf( "DO\n$$\nBEGIN \n"+ "IF NOT EXISTS (select * FROM test_people WHERE name_people = '%v')\nTHEN\n"+ "INSERT INTO test_people (name_people,post_people,work)VALUES('%v','%v','%v');\n"+ "ELSE\n"+ "UPDATE test_people SET post_people ='%v' ,work= '%v' WHERE name_people = '%v';\n"+ "end if;\nEND\n$$", body.Name_people, body.Name_people, body.Post_people, string(byteWork), body.Post_people, string(byteWork), body.Name_people) //var result sql.Result _, err = db.Exec(peopleSQL) if err != nil { fmt.Printf("%s", err) } if err != nil { Err <- err } }(body) } wg.Wait() if len(Err) != 0 { return <-Err } return nil } } func WaitInDB(str []byte) error { var bodys []Wait if err = json.Unmarshal(str, &bodys); err != nil { return err } else { var Err chan error for _, body := range bodys { wg.Add(1) go func(body Wait) { mt.Lock() defer mt.Unlock() defer wg.Done() peopleSQL := fmt.Sprintf(""+ "DO $$ BEGIN\n"+ "IF NOT EXISTS ( SELECT * FROM test_waiting WHERE name_project = '%v' ) THEN\n"+ "INSERT INTO test_waiting (name_project, from_project, contacts, progress)VALUES( '%v', '%v', '%v', '%v' );\n"+ "ELSE\n"+ "UPDATE test_waiting SET from_project = '%v',contacts = '%v',progress = '%v' WHERE name_project = '%v';\n"+ "END IF;\nEND $$", body.Name_project, body.Name_project, body.From_project, body.Contacts, body.Progress, body.From_project, body.Contacts, body.Progress, body.Name_project) //var result sql.Result _, err = db.Exec(peopleSQL) if err != nil { fmt.Printf("%s", err) } if err != nil { Err <- err } }(body) } wg.Wait() if len(Err) != 0 { return <-Err } return nil } } type delete struct { Table_name string `json:"table_name"` Delete string `json:"delete"` Delete_state string `json:"delete_state"` } type Project struct { Name_project string `json:"name_project"` Build_time string `json:"build_time"` Publish_time string `json:"publish_time"` Part_people []pPeople `json:"part_people"` Progress string `json:"progress"` } type pPeople struct { Name_people string `json:"name_people"` } type People struct { Name_people string `json:"name_people"` Post_people string `json:"post_people"` Work []Work `json:"work"` } type Work struct { Time string `json:"time"` Project string `json:"project"` } type Wait struct { Name_project string `json:"name_project"` From_project string `json:"from_project"` Contacts string `json:"contacts"` Progress string `json:"progress"` } const SearchProject = `SELECT * FROM "test_project"` const SearchPeople = `SELECT * FROM "test_people"` const SearchWait = `SELECT * FROM "test_waiting"`