// GetDataClassListSumData 返回总条数
func
GetDataClassListSumData(db orm.Ormer, classify
int64, keyword
string) (count
int64, err
error) {
params :=
make([]
interface{},
0)
sql :=
`SELECT COUNT(id) FROM self_rpt_external_tableinfo `
sql +=
` WHERE 1=1 `
if classify >
0 {
sql +=
" AND type = ?"
params =
append(params, classify)
}
if keyword !=
"" {
sql +=
" AND table_name = ?"
params =
append(params, keyword)
}
err = db.
Raw(sql, params...).
QueryRow(&count)
return
}
// GetDataClassListData 表列
func
GetDataClassListData(db orm.Ormer, classify
int64, keyword
string, currentpage
int, pagesize
int) (cates []*models.QReportExternalTableInfo, err
error) {
//tableInfo *models.QReportExternalTableInfo
//var cates []*models.QReportExternalTableInfo
params :=
make([]
interface{},
0)
sql :=
`SELECT * FROM self_rpt_external_tableinfo WHERE 1=1 `
if classify >
0 {
sql +=
" AND type = ?"
params =
append(params, classify)
}
if keyword !=
"" {
sql +=
" AND table_name = ?"
params =
append(params, keyword)
}
//if pagesize != "" {
//sql += " LIMIT " + currentpage + "," + pagesize
//sql += " LIMIT " + currentpage + "," + pagesize
sql += fmt.
Sprintf(
" LIMIT %d,%d", currentpage, pagesize)
//sql += " LIMIT " + "2" + "," + "5"
//}
//sql += " LIMIT " + "0" + "," + "10"
//params = append(params, pagesize)
//params = append(params, currentpage)
/*
sql += pagesize
sql += ","
sql += currentpage
*/
_,
err = db.
Raw(sql, params...).
QueryRows(&cates)
return
/*
sql := `SELECT cate.* FROM self_rpt_external_tableinfo cate
JOIN self_rpt_user_collect coll ON cate.id = coll.menu_id
WHERE 1=1`
*/
/*
var tableInfolist []models.QReportExternalTableInfo
num, err := db.QueryTable("self_rpt_external_tableinfo").Filter("database_id", 37).All(&tableInfolist) //.Filter("database_id", 37)
if err == nil && num > 0 {
}
return tableInfolist, nil
*/
}
package main
import (
_ "crud_beego/routers" //自动注册路由
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
const (
DRIVER_NAME = "mysql"
DATA_SOURCE = "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&loc=Asia%2FShanghai"
MAX_IDLE_CONN = 5
MAX_OPEN_CONN = 30
)
type User struct {
Id int
Name string
Email string
Age int
isActive bool
}
func (u *User) TableName() string {
return "t_user"
}
func registerDB() {
orm.Debug = true
orm.RegisterDataBase("default", DRIVER_NAME, DATA_SOURCE, MAX_IDLE_CONN, MAX_OPEN_CONN)
orm.RegisterModel(new(User))
}
func (this User) ToString() string {
return fmt.Sprintf("Id:%d\tName:%s\tEmail:%s\tAge:%d\tisActive:%v", this.Id, this.Name, this.Email, this.Age, this.isActive)
}
func createUsers() {
users := []User{
User{Name: "adadmin1", Email: "[email protected]", Age: 12},
User{Name: "adadmin1", Email: "[email protected]", Age: 12},
User{Name: "adadmin1", Email: "[email protected]", Age: 12},
User{Name: "adadmin1", Email: "[email protected]", Age: 12},
User{Name: "adadmin1", Email: "[email protected]", Age: 12},
User{Name: "adadmin1", Email: "[email protected]", Age: 12},
User{Name: "adadmin1", Email: "[email protected]", Age: 12},
}
o := orm.NewOrm()
if successNums, err := o.InsertMulti(len(users), users); err != nil {
fmt.Println("insert fail.....")
} else {
fmt.Println("success inserted %d datas", successNums)
}
}
//find all users
func listUsers() {
var users []User
orm.NewOrm().QueryTable("t_user").All(&users)
for _, user := range users {
fmt.Println(user.ToString())
}
}
//get user count
func countUser() {
cnt, _ := orm.NewOrm().QueryTable("t_user").Count()
fmt.Println("All user count:", cnt)
}
//get the only one user
func getUser() {
var user User
err := orm.NewOrm().QueryTable("t_user").Filter("Id", 5).One(&user)
if err == nil {
fmt.Println(user.ToString())
}
}
//get the only one user
func getUsers() {
var users []User
_, err := orm.NewOrm().QueryTable("t_user").Filter("name__contains", "awd").All(&users)
if err == nil {
for _, user := range users {
fmt.Println(user.ToString())
}
}
}
//get limit user
func limitUser() {
var users []User
_, err := orm.NewOrm().QueryTable("t_user").Limit(6).OrderBy("-Name").All(&users)
if err == nil {
for _, user := range users {
fmt.Println(user.ToString())
}
}
}
//get user limit,offset
func limitoffsetUser() {
var users []User
_, err := orm.NewOrm().QueryTable("t_user").Limit(1, 4).OrderBy("Id").All(&users)
if err == nil {
for _, user := range users {
fmt.Println(user.ToString())
}
}
}
//del user
func delUser() {
num, err := orm.NewOrm().QueryTable("t_user").Filter("Id", "5").Delete()
fmt.Printf("Affected Num: %s, %s", num, err)
}
//update user
func updateUser() {
num, err := orm.NewOrm().QueryTable("t_user").Filter("name__contains", "awd").Update(orm.Params{
"name": "#########",
})
fmt.Printf("Affected Num: %s, %s", num, err)
}
func main() {
registerDB()
createUsers()
listUsers()
countUser()
getUser()
getUsers()
limitoffsetUser()
delUser()
updateUser()
beego.Run()
}