78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package model
|
|
|
|
import (
|
|
"background/db"
|
|
"background/logs"
|
|
"fmt"
|
|
"qiniupkg.com/x/log.v7"
|
|
"strings"
|
|
)
|
|
|
|
type Memo struct {
|
|
ID int64 `sql:"id" json:"id"`
|
|
Title string `sql:"title" json:"title"`
|
|
CreateTime string `sql:"create_time" json:"create_time"`
|
|
Content string `sql:"content" json:"content"`
|
|
}
|
|
|
|
func CreateMemo(memo Memo) error{
|
|
sql := fmt.Sprintf(`insert into memo(title,create_time,content) select '%s','%s','%s'
|
|
from dual where not exists (select * from memo where memo.title='%s') `,
|
|
memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1),memo.Title)
|
|
log.Print(sql)
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
if nil != e {
|
|
logs.Error(e.Error())
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UpdateMemo(memo Memo) error{
|
|
sql := fmt.Sprintf(`update memo set title = '%s',
|
|
create_time = '%s',
|
|
content = '%s' where memo.id = %d`,
|
|
memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1),memo.ID)
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
if nil != e {
|
|
logs.Error(e.Error())
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DeleteMemo(id int32) error{
|
|
sql := fmt.Sprintf(`delete from memo where memo.id = '%d'`,id)
|
|
log.Print(sql)
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
if nil != e {
|
|
logs.Error(e.Error())
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ReadMemo(id int32) ([]Memo,error){
|
|
memo := []Memo{}
|
|
sql := fmt.Sprintf(`select * from memo where memo.id = '%d'`,id)
|
|
e := db.GetMysqlClient().Query2(sql,&memo)
|
|
if nil != e {
|
|
logs.Error(e.Error())
|
|
return nil,e
|
|
}
|
|
return memo,nil
|
|
}
|
|
|
|
func GetMemos(title string,limit int,offset int) ([]Memo,error) {
|
|
memo := []Memo{}
|
|
sql := fmt.Sprintf(`select * from memo where memo.title like '%s%%' limit %d offset %d`,
|
|
title,limit,offset)
|
|
log.Print(sql)
|
|
e := db.GetMysqlClient().Query2(sql,&memo)
|
|
if nil != e {
|
|
logs.Error(e.Error())
|
|
return nil,e
|
|
}
|
|
return memo,nil
|
|
|
|
} |