示例#1
0
func deleteMain(mainID string) {
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	stmt, err := db.Prepare("delete from main where id=?")
	general.CheckErr(err)
	_, err = stmt.Exec(mainID)
	general.CheckErr(err)
}
示例#2
0
文件: reply.go 项目: k-lusine/forum
func deleteAllRepliesByThread(thr_id string) {
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	stmt, err := db.Prepare("delete from reply where thread_id=?")
	general.CheckErr(err)
	_, err = stmt.Exec(thr_id)
	general.CheckErr(err)
}
示例#3
0
文件: reply.go 项目: k-lusine/forum
func (R *Reply) Delete() (err error) {
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	stmt, err := db.Prepare("delete from reply where id=?")
	general.CheckErr(err)
	_, err = stmt.Exec(R.Id)
	general.CheckErr(err)
	return
}
示例#4
0
文件: reply.go 项目: k-lusine/forum
func (R *Reply) Update() {
	//Updates edited reply to database
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	stmt, err := db.Prepare("UPDATE reply SET title=?,content=? WHERE id=?")
	general.CheckErr(err)
	_, err = stmt.Exec(R.Name, R.Content, R.Id)
	general.CheckErr(err)
}
示例#5
0
文件: reply.go 项目: k-lusine/forum
func (R *Reply) Create() (id int64) {
	//Saves new reply to database
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	stmt, err := db.Prepare("INSERT reply SET title=?,content=?, thread_id=?, user_id=?")
	general.CheckErr(err)
	res, err := stmt.Exec(R.Name, R.Content, R.Thread_id, R.UserID)
	id, err = res.LastInsertId()
	//fmt.Println(Id, "sgsdg")
	general.CheckErr(err)
	return id
}
示例#6
0
文件: reply.go 项目: k-lusine/forum
func (R *Reply) GetSingleReply() {
	//opens and closes connection
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	rows, err := db.Query(`SELECT reply.title, reply.content, reply.user_id, user.username
	FROM reply
	INNER JOIN user
	ON reply.user_id=user.Id
	WHERE reply.id=?`, R.Id)
	general.CheckErr(err)
	for rows.Next() {
		err = rows.Scan(&R.Name, &R.Content, &R.UserID, &R.Username)
		general.CheckErr(err)
	}
}
示例#7
0
文件: reply.go 项目: k-lusine/forum
func (tl *Thread) GetReplies() {
	//opens and closes connection
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	rows, err := db.Query(`SELECT reply.id, reply.title, reply.content,reply.thread_id, user.username, reply.user_id, reply.updated
	FROM reply
	INNER JOIN user
	ON reply.user_id=user.Id
	WHERE reply.thread_id=?`, tl.Id)
	general.CheckErr(err)
	for rows.Next() {
		var r Reply
		err = rows.Scan(&r.Id, &r.Name, &r.Content, &r.Thread_id, &r.Username, &r.UserID, &r.Updated)
		general.CheckErr(err)
		tl.Reply = append(tl.Reply, r)
	}
}
示例#8
0
func (M *Main) Save() {
	//Saves new main post and creates new corresponding thread in the database
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	//gets the category Id based on the category name
	cat_result, err := db.Query("SELECT id FROM category WHERE `name`='" + M.Category + "'")
	var cat_id string
	for cat_result.Next() {
		err = cat_result.Scan(&cat_id)
		general.CheckErr(err)
	}
	general.CheckErr(err)
	//checks to see if create or update request is needed
	if M.ID == "" {
		M.Create(cat_id)
	} else {
		M.Update(cat_id)
	}
}
示例#9
0
文件: reply.go 项目: k-lusine/forum
func updateThreadTimestamp(id string) {
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	rows, err := db.Query(`SELECT reply.updated, reply.thread_id, reply.user_id
	FROM reply
	WHERE id=?`, id)
	general.CheckErr(err)
	var upd, thrID, userID string
	fmt.Println(upd, thrID, "sgsdg")
	for rows.Next() {
		err = rows.Scan(&upd, &thrID, &userID)
		general.CheckErr(err)
	}
	fmt.Println(upd, thrID, userID, "fgfgfgf")
	stmt, err := db.Prepare("UPDATE thread SET updated=?, user_id=? WHERE id=?")
	general.CheckErr(err)
	_, err = stmt.Exec(upd, userID, thrID)
	general.CheckErr(err)
}
示例#10
0
func (m *Main) GetMainData() {
	//opens and closes connection
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	//query to get data from db
	rows, err := db.Query(`SELECT category.name, main.title, main.content, main.id, main.user_id, user.username, main.created, main.updated
	FROM thread
	INNER JOIN category
	ON thread.category_id=category.id
	INNER JOIN main
	ON thread.main_id=main.id
    INNER JOIN user
    ON main.user_id=user.Id
	WHERE thread.id=?`, m.Thr_id)
	general.CheckErr(err)
	//saves values to thread
	//tl.Id=id
	for rows.Next() {
		err = rows.Scan(&m.Category, &m.Name, &m.Content, &m.ID, &m.UserID, &m.Username, &m.Created, &m.Updated)
		general.CheckErr(err)
	}
}
示例#11
0
func (M *Main) Update(cat_id string) {
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	stmt, err := db.Prepare("UPDATE main SET title=?,content=? WHERE id=?")
	general.CheckErr(err)
	res, err := stmt.Exec(M.Name, M.Content, M.ID)
	general.CheckErr(err)
	_, err = res.RowsAffected()
	general.CheckErr(err)
	stmt, err = db.Prepare("UPDATE thread SET title=?, category_id=? WHERE main_id=?")
	general.CheckErr(err)
	res, err = stmt.Exec(M.Name, cat_id, M.ID)
	general.CheckErr(err)
}
示例#12
0
func (M *Main) Create(cat_id string) {
	db, err := connectDB()
	general.CheckErr(err)
	defer db.Close()
	//insert new entry into the table main
	stmt, err := db.Prepare("INSERT main SET title=?,content=?, user_id=?")
	general.CheckErr(err)
	res, err := stmt.Exec(M.Name, M.Content, M.UserID)
	general.CheckErr(err)
	main_id, err := res.LastInsertId()
	general.CheckErr(err)
	//insert new entry into the table thread
	stmt, err = db.Prepare("INSERT thread SET title=?, category_id=?, main_id=?, user_id=?")
	general.CheckErr(err)
	res, err = stmt.Exec(M.Name, cat_id, main_id, M.UserID)
	general.CheckErr(err)
}