func delTag(reponame, itemname, tagname string) (int, error) { log.Println("TODO delete tag from db") sqlrpdmid := fmt.Sprintf(`SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY='%s' AND DATAITEM='%s' AND STATUS='A';`, reponame, itemname) var rpdmId int row, err := g_ds.QueryRow(sqlrpdmid) if err != nil { l := log.Error("select rpdmid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return 0, err } row.Scan(&rpdmId) sql := fmt.Sprintf(`SELECT TAGID FROM DH_RPDM_TAG_MAP WHERE STATUS='A' AND TAGNAME='%s' AND RPDMID=%d`, tagname, rpdmId) var tagid int row, err = g_ds.QueryRow(sql) if err != nil { l := log.Error("select tagid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return 0, err } row.Scan(&tagid) sql = fmt.Sprintf(`UPDATE DH_RPDM_TAG_MAP SET STATUS='N' WHERE TAGNAME='%s' AND RPDMID=%d`, tagname, rpdmId) _, err = g_ds.Update(sql) if err != nil { l := log.Error("delete tag from DH_RPDM_TAG_MAP error:", err) logq.LogPutqueue(l) return 0, err } return tagid, nil }
func saveDaemonID(id string) { log.Println("TODO save daemonid to db when srv returns code 0.") count := `SELECT COUNT(*) FROM DH_DAEMON;` row, err := g_ds.QueryRow(count) if err != nil { l := log.Error(count, "error.", err) logq.LogPutqueue(l) } var c int row.Scan(&c) if c > 0 { Update := fmt.Sprintf(`UPDATE DH_DAEMON SET DAEMONID='%s';`, id) log.Debug(Update) if _, e := g_ds.Update(Update); e != nil { l := log.Error(Update, "error.", e) logq.LogPutqueue(l) } } else { Insert := fmt.Sprintf(`INSERT INTO DH_DAEMON (DAEMONID) VALUES ('%s');`, id) log.Debug(c, Insert) if _, e := g_ds.Insert(Insert); e != nil { l := log.Error(Insert, "error.", e) logq.LogPutqueue(l) } } }
func delTagsForDelItem(reponame, itemname string) error { log.Println("Begin to remove tags for remove item from db") sqlrpdmid := fmt.Sprintf(`SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY='%s' AND DATAITEM='%s' AND STATUS='A';`, reponame, itemname) row, err := g_ds.QueryRow(sqlrpdmid) if err != nil { l := log.Error("select rpdmid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return err } var rpdmId int row.Scan(&rpdmId) if rpdmId == 0 { log.Debug(reponame, itemname, "not exist.") return nil } sqldeltag := fmt.Sprintf(`UPDATE DH_RPDM_TAG_MAP SET STATUS='N' WHERE RPDMID=%d`, rpdmId) _, err = g_ds.Update(sqldeltag) log.Info("sqldeltag", sqldeltag) if err != nil { l := log.Error("delete tag error:", err) logq.LogPutqueue(l) return err } return nil }
func getBatchDelTagsName(reponame, itemname, tagname string) ([]string, error) { log.Println("Batch delete tags from db") sqlrpdmid := fmt.Sprintf(`SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY='%s' AND DATAITEM='%s' AND STATUS='A';`, reponame, itemname) var rpdmId int row, err := g_ds.QueryRow(sqlrpdmid) if err != nil { l := log.Error("select rpdmid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return nil, err } row.Scan(&rpdmId) tagname = strings.Replace(tagname, "*", "%", -1) log.Println(tagname) sql := fmt.Sprintf(`SELECT TAGNAME FROM DH_RPDM_TAG_MAP WHERE TAGNAME LIKE '%s' AND RPDMID=%d AND STATUS='A';`, tagname, rpdmId) //var tagnames []string tagsname := make([]string, 0) rows, err := g_ds.QueryRows(sql) if err != nil { l := log.Error("batch delete tag from DH_RPDM_TAG_MAP error:", err) logq.LogPutqueue(l) return nil, err } for rows.Next() { rows.Scan(&tagname) tagsname = append(tagsname, tagname) } log.Println(tagsname) /*if len(tagsname) == 0 { return nil, errors.New("没有匹配的tag") }*/ return tagsname, nil }
func saveEntryPoint(ep string) { log.Println("TODO save ep to db") count := `SELECT COUNT(*) FROM DH_DAEMON;` row, err := g_ds.QueryRow(count) if err != nil { l := log.Error(count, "error.", err) logq.LogPutqueue(l) } var c int row.Scan(&c) if c > 0 { Update := fmt.Sprintf(`UPDATE DH_DAEMON SET ENTRYPOINT='%s';`, ep) log.Debug(Update) if _, e := g_ds.Update(Update); e != nil { l := log.Error(Update, "error.", e) logq.LogPutqueue(l) } } else { Insert := fmt.Sprintf(`INSERT INTO DH_DAEMON (ENTRYPOINT) VALUES ('%s');`, ep) log.Debug(c, Insert) if _, e := g_ds.Insert(Insert); e != nil { l := log.Error(Insert, "error.", e) logq.LogPutqueue(l) } } }
func GetRepoInfo(dpName, status string, offset int64, limit int) ([]ds.RepoInfo, error) { if status == "published" { status = "Y" } else { status = "N" } sql := fmt.Sprintf(`SELECT DPID FROM DH_DP WHERE DPNAME = '%s' AND STATUS = 'A';`, dpName) row, err := g_ds.QueryRow(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return nil, err } var dpid int row.Scan(&dpid) if dpid == 0 { return nil, errors.New(fmt.Sprintf("No datapool named %s.", dpName)) } sql = fmt.Sprintf(`SELECT DISTINCT REPOSITORY FROM DH_DP_RPDM_MAP WHERE DPID = %d AND PUBLISH = '%s' AND STATUS = 'A' ORDER BY RPDMID LIMIT %v OFFSET %v;`, dpid, status, limit, offset) rows, err := g_ds.QueryRows(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return nil, err } var repository string var itemCount int repoinfo := ds.RepoInfo{} repoInfos := make([]ds.RepoInfo, 0) for rows.Next() { rows.Scan(&repository) repoinfo.RepositoryName = repository sql = fmt.Sprintf(`SELECT COUNT(*) FROM DH_DP_RPDM_MAP WHERE REPOSITORY = '%s' AND PUBLISH = '%s' AND STATUS = 'A';`, repository, status) row, err := g_ds.QueryRow(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return nil, err } row.Scan(&itemCount) repoinfo.ItemCount = itemCount repoInfos = append(repoInfos, repoinfo) } return repoInfos, err }
func GetPulledTagsOfItemInfo(dpname, repo, item string, offset int64, limit int) ([]ds.PulledTagsOfItem, error) { pulledTagOfItem := ds.PulledTagsOfItem{} pulledTagsOfItem := make([]ds.PulledTagsOfItem, 0) sql := fmt.Sprintf(`SELECT DPID FROM DH_DP WHERE DPNAME = '%s' AND STATUS = 'A';`, dpname) row, err := g_ds.QueryRow(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return nil, err } var dpid int row.Scan(&dpid) if dpid == 0 { return pulledTagsOfItem, errors.New(fmt.Sprintf("No datapool named %s.", dpname)) } sql = fmt.Sprintf(`SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY = '%s' AND DATAITEM = '%s' AND DPID = %d AND PUBLISH = 'N' AND STATUS = 'A';`, repo, item, dpid) row, err = g_ds.QueryRow(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return nil, err } var rpdmid int row.Scan(&rpdmid) sql = fmt.Sprintf(`SELECT TAGNAME, CREATE_TIME, COMMENT FROM DH_RPDM_TAG_MAP WHERE RPDMID = %d AND STATUS = 'A' LIMIT %v OFFSET %v;`, rpdmid, limit, offset) rows, err := g_ds.QueryRows(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return nil, err } for rows.Next() { rows.Scan(&pulledTagOfItem.TagName, &pulledTagOfItem.DownloadTime, &pulledTagOfItem.Content) pulledTagsOfItem = append(pulledTagsOfItem, pulledTagOfItem) } return pulledTagsOfItem, err }
func dbinit() { DB_TYPE := os.Getenv("DB_TYPE") if strings.ToUpper(DB_TYPE) == "MYSQL" { for i := 0; i < 3; i++ { connectMysql() if g_ds.Db == nil { select { case <-time.After(time.Second * 5): continue } } else { break } } if g_ds.Db == nil { return } } else { log.Println("connect to db sqlite3") db, err := sql.Open("sqlite3", g_dbfile) //defer db.Close() chk(err) g_ds.Db = db g_ds.DbType = "sqlite" } var RetDhRpdmTagMap string row, err := g_ds.QueryRow(ds.SQLIsExistRpdmTagMap) if err != nil { l := log.Error("Get Dh_Rpdm_Tag_Map error!") logq.LogPutqueue(l) return } row.Scan(&RetDhRpdmTagMap) if len(RetDhRpdmTagMap) > 1 { if false == strings.Contains(RetDhRpdmTagMap, "COMMENT") { // UpdateSql04To05() UpdateSql16To17() } } //if err := UpgradeSql07To08(); err != nil { // panic(err) //} if err := CreateTable(); err != nil { l := log.Error("Get CreateTable error!", err) logq.LogPutqueue(l) panic(err) } }
func GetLocalfilePath() (localfilepath []string) { sql := `SELECT DISTINCT DPCONN, ITEMDESC FROM DH_DP A, DH_DP_RPDM_MAP B WHERE A.DPID=B.DPID AND A.DPTYPE='file' AND A.STATUS='A' AND B.PUBLISH='Y' AND B.STATUS='A';` //dpci = make(map[string] string) var conn string var desc string localfilepath = make([]string, 0) rows, err := g_ds.QueryRows(sql) if err != nil { l := log.Error("QueryRow error:", err) logq.LogPutqueue(l) return } else { for rows.Next() { rows.Scan(&conn, &desc) path := conn + "/" + desc localfilepath = append(localfilepath, path) } return } }
func getPulledTagCount(datapool, repo, item string) (int64, error) { sql := fmt.Sprintf(`SELECT COUNT(*) FROM DH_RPDM_TAG_MAP WHERE RPDMID = (SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY = '%s' AND DATAITEM = '%s' AND PUBLISH = 'N' AND STATUS = 'A' AND DPID = (SELECT DPID FROM DH_DP WHERE DPNAME = '%s' AND STATUS='A')) AND STATUS = 'A';`, repo, item, datapool) row, err := g_ds.QueryRow(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return 0, err } var count int64 row.Scan(&count) log.Debug("Published repository count:", count) return count, err }
func getRepoCountByDp(datapool, status string) int64 { if status == "published" { status = "Y" } else { status = "N" } sql := fmt.Sprintf(`SELECT COUNT(DISTINCT REPOSITORY) FROM DH_DP_RPDM_MAP WHERE DPID IN (SELECT DPID FROM DH_DP WHERE DPNAME = '%s' AND STATUS='A') AND PUBLISH= '%s' AND STATUS = 'A';`, datapool, status) row, err := g_ds.QueryRow(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return 0 } var count int64 row.Scan(&count) log.Debug("Published repository count:", count) return count }
func commToServer(method, path string, buffer []byte, w http.ResponseWriter) (body []byte, err error) { //Trace() s := log.Info("daemon: connecting to", DefaultServer+path) logq.LogPutqueue(s) req, err := http.NewRequest(strings.ToUpper(method), DefaultServer+path, bytes.NewBuffer(buffer)) if len(loginAuthStr) > 0 { req.Header.Set("Authorization", loginAuthStr) } //req.Header.Set("User", "admin") resp, err := http.DefaultClient.Do(req) if err != nil { log.Error(err) d := ds.Result{Code: cmd.ErrorServiceUnavailable, Msg: err.Error()} body, e := json.Marshal(d) if e != nil { log.Error(e) return body, e } w.WriteHeader(http.StatusServiceUnavailable) w.Write(body) return body, err } defer resp.Body.Close() w.WriteHeader(resp.StatusCode) body, err = ioutil.ReadAll(resp.Body) w.Write(body) log.Info(resp.StatusCode, string(body)) return }
func ErrLogAndResp(c chan int, w http.ResponseWriter, httpcode, errorcode int, err error) (int64, error) { l := log.Error(err) logq.LogPutqueue(l) c <- -1 HttpNoData(w, http.StatusBadRequest, cmd.ErrorNoRecord, err.Error()) return 0, err }
func delEntryPoint() { log.Println("TODO remove ep from db.") d := `UPDATE DH_DAEMON SET ENTRYPOINT = '';` if _, e := g_ds.Update(d); e != nil { l := log.Error(d, "error.", e) logq.LogPutqueue(l) } }
func MoveFromTmp(src, dest string) (err error) { err = os.Rename(src, dest) if err != nil { l := log.Errorf("Rename %v to %v error. %v", src, dest, err) logq.LogPutqueue(l) } return err }
/*AlterDhJob() Temporarily not use*/ func AlterDhJob() (err error) { sqltoken := `ALTER TABLE DH_JOB ADD ACCESSTOKEN VARCHAR(20);` _, err = g_ds.Exec(sqltoken) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return err } sqlep := `ALTER TABLE DH_JOB ADD ENTRYPOINT VARCHAR(128);` _, err = g_ds.Exec(sqlep) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return err } return nil }
func rollbackInsertPubItem(w http.ResponseWriter, repo, item string) { err := rollbackInsertPubItemToDb(repo, item) if err != nil { l := log.Error("rollbackInsertPubTagToDb error :", err) logq.LogPutqueue(l) JsonResult(w, http.StatusBadRequest, cmd.InternalError, "Internal Error.", nil) return } return }
func removeJobDB(job *ds.JobInfo) (e error) { log.Debug("TODO remove jobid from db") sRmJob := fmt.Sprintf(`DELETE FROM DH_JOB WHERE JOBID=%d;`, job.ID) _, e = g_ds.Delete(sRmJob) if e != nil { l := log.Error(e) logq.LogPutqueue(l) } return }
func removeAllJobDB() (e error) { log.Debug("TODO remove all jobs from db") sRmJobs := `DELETE FROM DH_JOB;` _, e = g_ds.Delete(sRmJobs) if e != nil { l := log.Error(e) logq.LogPutqueue(l) } return }
func UpdateSql16To17() (err error) { sqlm := `ALTER TABLE DH_RPDM_TAG_MAP ADD COMMENT VARCHAR(256);` _, err = g_ds.Exec(sqlm) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return err } return nil }
func Test_datapoolMonitor(t *testing.T) { var err error watcher, err = fsnotify.NewWatcher() if err != nil { l := log.Error(err) logq.LogPutqueue(l) } defer watcher.Close() AddtoMonitor("/var/lib/datahub/datahubUtest.db", "repounittest/itemunittest:tagunittestdatahubUtest.db") }
func updateJobStatus(job *ds.JobInfo) (e error) { log.Debug("TODO updata job stat to db.") sUpdateJob := fmt.Sprintf(`UPDATE DH_JOB SET STATUS='%s', STAT_TIME=datetime('now'), DOWNSIZE=%d WHERE JOBID='%s';`, job.Stat, job.Dlsize, job.ID) _, e = g_ds.Update(sUpdateJob) if e != nil { l := log.Error(e) logq.LogPutqueue(l) } return }
func AlterTagStatus(d []string) (err error) { sqlTag := fmt.Sprintf(`UPDATE DH_RPDM_TAG_MAP SET STATUS = 'N' WHERE STATUS='A' AND TAGNAME=%v`, d) _, e := g_ds.Update(sqlTag) if e != nil { l := log.Error(err) logq.LogPutqueue(l) return } return }
func AlterItemStatus(c []string) (err error) { sqlItem := fmt.Sprintf(`UPDATE DH_DP_RPDM_MAP SET STATUS = 'N' WHERE STATUS='A' AND DATAITEM=%v`, c) _, e := g_ds.Update(sqlItem) if e != nil { l := log.Error(err) logq.LogPutqueue(l) return } return }
func rollbackDelItem(reponame, itemname string) error { log.Println("TODO rollback delete item from db") sql := fmt.Sprintf(`UPDATE DH_DP_RPDM_MAP SET STATUS='A' WHERE REPOSITORY='%s' AND DATAITEM='%s';`, reponame, itemname) if _, err := g_ds.Update(sql); err != nil { l := log.Error("rollback delete item error:", err) logq.LogPutqueue(l) return err } return nil }
func GetDpconnDpnameDptypeByDpid(dpid int) (dpconn, dpname, dptype string) { sSqlGetDpconn := fmt.Sprintf(`SELECT DPCONN, DPNAME, DPTYPE FROM DH_DP WHERE DPID=%d AND STATUS='A';`, dpid) row, err := g_ds.QueryRow(sSqlGetDpconn) if err != nil { l := log.Error("QueryRow error:", err) logq.LogPutqueue(l) return } row.Scan(&dpconn, &dpname, &dptype) return }
func delItem(reponame, itemname string) (err error) { log.Println("Begin to remove item from db") sql := fmt.Sprintf(`UPDATE DH_DP_RPDM_MAP SET STATUS = 'N' WHERE REPOSITORY='%s' AND DATAITEM='%s';`, reponame, itemname) if _, err := g_ds.Update(sql); err != nil { l := log.Error("delete item error:", err) logq.LogPutqueue(l) return err } return nil }
func rollbackDelTag(tagid int) error { log.Println("TODO rollback delete tag from db") log.Println(tagid) sql := fmt.Sprintf(`UPDATE DH_RPDM_TAG_MAP SET STATUS='A' WHERE TAGID=%d`, tagid) _, err := g_ds.Update(sql) if err != nil { l := log.Error("rollback delete tag error:", err) logq.LogPutqueue(l) return err } return nil }
func rollbackDelTags(reponame, itemname string) error { log.Println("TODO rollback delete tags for item from db") sqlrpdmid := fmt.Sprintf(`SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY='%s' AND DATAITEM='%s';`, reponame, itemname) var rpdmId int row, err := g_ds.QueryRow(sqlrpdmid) if err != nil { l := log.Error("select rpdmid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return err } row.Scan(&rpdmId) sqlrollback := fmt.Sprintf(`UPDATE DH_RPDM_TAG_MAP SET STATUS='A' WHERE RPDMID=%d`, rpdmId) _, err = g_ds.Update(sqlrollback) if err != nil { l := log.Error("rollback delete tags error:", err) logq.LogPutqueue(l) return err } return nil }
func getEntryPoint() (ep string) { log.Println("TODO get ep from db") s := `SELECT ENTRYPOINT FROM DH_DAEMON;` r, e := g_ds.QueryRow(s) if e != nil { l := log.Error(s, "error.", e) logq.LogPutqueue(l) return } r.Scan(&ep) return ep }