server/pkg/modules/user/user-repository.go

48 lines
877 B
Go
Raw Normal View History

2019-11-18 14:28:56 +00:00
package user
import (
"context"
"log"
"reflect"
"git.loafle.net/loafer/annotation-go"
"git.loafle.net/loafer/di-go"
2019-11-19 13:54:01 +00:00
"github.com/jackc/pgx/v4/pgxpool"
2019-11-18 14:28:56 +00:00
)
2019-11-19 14:58:46 +00:00
var UserRepositoryType = reflect.TypeOf((*UserRepository)(nil))
2019-11-18 14:28:56 +00:00
func init() {
2019-11-19 14:58:46 +00:00
di.RegisterType(UserRepositoryType)
2019-11-18 14:28:56 +00:00
}
2019-11-19 14:58:46 +00:00
type UserRepository struct {
annotation.TypeAnnotation `annotation:"@web:Repository(\"name\": \"/userDao\")"`
2019-11-18 14:28:56 +00:00
2019-11-19 13:54:01 +00:00
Pool *pgxpool.Pool `annotation:"@Resource(\"name\": \"dbConnPool\")"`
2019-11-18 14:28:56 +00:00
}
2019-11-19 14:58:46 +00:00
func (ur *UserRepository) FindAll() {
2019-11-18 14:28:56 +00:00
log.Printf("UserDao FindAll")
2019-11-19 13:54:01 +00:00
2019-11-19 14:58:46 +00:00
conn, err := ur.Pool.Acquire(context.Background())
2019-11-19 13:54:01 +00:00
if nil != err {
log.Printf("%v", err)
return
}
defer conn.Release()
row, err := conn.Query(context.Background(), "select count(*) from users")
2019-11-18 14:28:56 +00:00
if nil != err {
log.Printf("%v", err)
return
}
if row.Next() {
2019-11-19 13:54:01 +00:00
var date int64
2019-11-18 14:28:56 +00:00
row.Scan(&date)
2019-11-19 13:54:01 +00:00
log.Printf("%d", date)
2019-11-18 14:28:56 +00:00
}
}