Exemplo n.º 1
0
Arquivo: sql.go Projeto: vozhyk-/gohan
//Delete delete resource from db
func (tx *Transaction) Delete(s *schema.Schema, resourceID interface{}) error {
	sql, args, err := sq.Delete(quote(s.GetDbTableName())).Where(sq.Eq{"id": resourceID}).ToSql()
	if err != nil {
		return err
	}
	return tx.Exec(sql, args...)
}
Exemplo n.º 2
0
func (ingest *Ingestion) clearRange(start int64, end int64, table string, idCol string) error {
	del := sq.Delete(table).Where(
		fmt.Sprintf("%s >= ? AND %s < ?", idCol, idCol),
		start,
		end,
	)
	_, err := ingest.DB.Exec(del)
	return err
}
Exemplo n.º 3
0
func RemoveM2M(table, col string, ids []int64, db *gorm.DB) error {
	sql, args, err := squirrel.Delete(table).Where(squirrel.Eq{col: ids}).ToSql()
	if err != nil {
		return err
	}
	if err := db.Exec(sql, args...).Error; err != nil {
		return err
	}
	return nil
}
Exemplo n.º 4
0
func (r *Repo) DeleteRange(
	start, end int64,
	table string,
	idCol string,
) error {
	del := sq.Delete(table).Where(
		fmt.Sprintf("%s >= ? AND %s < ?", idCol, idCol),
		start,
		end,
	)
	_, err := r.Exec(del)
	return err
}
Exemplo n.º 5
0
func (r *DaoResource) DeleteApplication(application *model.Application) error {
	transaction, err := r.Storage.Begin()
	if err != nil {
		return err
	}

	q := sq.Delete(model.TableEndpoints).Where("application_id = ?", application.Id)
	if _, err = transaction.ExecDelete(&q); err == nil {
		_, err = transaction.Delete(application)
	}

	if err != nil {
		transaction.Rollback()
	} else {
		transaction.Commit()
	}

	return err
}
Exemplo n.º 6
0
// Clear removes data from the ledger
func (ingest *Ingestion) Clear(start int64, end int64) error {

	if start <= 1 {
		del := sq.Delete("history_accounts").Where("id = 1")
		ingest.DB.Exec(del)
	}

	err := ingest.clearRange(start, end, "history_effects", "history_operation_id")
	if err != nil {
		return err
	}
	err = ingest.clearRange(start, end, "history_operation_participants", "history_operation_id")
	if err != nil {
		return err
	}
	err = ingest.clearRange(start, end, "history_operations", "id")
	if err != nil {
		return err
	}
	err = ingest.clearRange(start, end, "history_transaction_participants", "history_transaction_id")
	if err != nil {
		return err
	}
	err = ingest.clearRange(start, end, "history_transactions", "id")
	if err != nil {
		return err
	}
	err = ingest.clearRange(start, end, "history_accounts", "id")
	if err != nil {
		return err
	}
	err = ingest.clearRange(start, end, "history_ledgers", "id")
	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 7
0
func UpdateM2M(table, col1, col2 string, id int64, ids []int64, db *gorm.DB) error {
	if len(ids) == 0 {
		return nil
	}
	sql, args, err := squirrel.Delete(table).Where(squirrel.Eq{col1: id}).ToSql()
	if err != nil {
		return err
	}
	if err := db.Exec(sql, args...).Error; err != nil {
		return err
	}
	if ids[0] == 0 {
		return nil
	}
	query := squirrel.Insert(table).Columns(col1, col2)
	for _, id2 := range ids {
		query = query.Values(id, id2)
	}
	sql, args, err = query.ToSql()
	if err != nil {
		return err
	}
	return db.Exec(sql, args...).Error
}
Exemplo n.º 8
0
func main() {
	var conns []*websocket.Conn

	log.Println("Starting...")
	runtime.GOMAXPROCS(runtime.NumCPU())
	var err error

	// DB conn
	db, err := sql.Open("mysql", "hello_go@/hello_go")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	seedUsers(db)

	r := mux.NewRouter()

	r.HandleFunc("/socket", func(w http.ResponseWriter, req *http.Request) {
		conn, err := upgrader.Upgrade(w, req, nil)
		if err != nil {
			log.Println(err)
			return
		}

		conns = append(conns, conn)
	})

	r.Methods("GET", "HEAD").Path("/data").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		users := fetchUsers(db)
		w.Header()["content-type"] = []string{"application/json"}
		w.Header()["x-count"] = []string{strconv.FormatInt(int64(len(users)), 10)}
		if len(users) == 0 {
			w.Write([]byte("[]"))
		} else {
			fmt.Fprint(w, toJson(users))
		}
	})

	r.Methods("GET", "HEAD").Path("/data/{id}").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		vars := mux.Vars(req)
		id, _ := strconv.ParseInt(vars["id"], 10, 0)

		if id <= 0 {
			http.NotFound(w, req)
			return
		}

		row := squirrel.Select("name", "age").From("user").Where(squirrel.Eq{"id": id}).RunWith(db).QueryRow()

		var name string
		var age int
		if err = row.Scan(&name, &age); err != nil {
			log.Println(err)
			http.NotFound(w, req)
			return
		}

		w.Header()["content-type"] = []string{"application/json"}
		fmt.Fprint(w, toJson(User{int(id), name, age}))
	})

	r.Methods("DELETE").Path("/data").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		log.Println("Deleting...")
		_, err := squirrel.Delete("user").RunWith(db).Exec()
		if nil != err {
			log.Println(err)
		}
		pushUsers(db, conns)
		w.WriteHeader(http.StatusNoContent)
		fmt.Fprint(w, "[]")
	})

	r.Methods("POST").Path("/data").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		log.Println("Seeding...")
		seedUsers(db)
		pushUsers(db, conns)
		w.WriteHeader(http.StatusCreated)
	})

	n := negroni.New(
		negroni.NewRecovery(),
		negroni.NewLogger(),
		negroni.NewStatic(rice.MustFindBox("public").HTTPBox()),
	)
	n.UseHandler(r)
	listen := os.Getenv("LISTEN")
	if listen == "" {
		listen = ":3000"
	}
	n.Run(listen)
}