2015-10-22 01:14:02 +02:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2015-10-22 01:39:43 +02:00
|
|
|
"fmt"
|
2015-10-22 01:14:02 +02:00
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
2017-05-03 23:25:33 +02:00
|
|
|
"github.com/drone/drone/store/datastore/sql"
|
2015-10-22 01:14:02 +02:00
|
|
|
"github.com/russross/meddler"
|
|
|
|
)
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) GetUser(id int64) (*model.User, error) {
|
2015-10-22 01:14:02 +02:00
|
|
|
var usr = new(model.User)
|
|
|
|
var err = meddler.Load(db, userTable, usr, id)
|
|
|
|
return usr, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) GetUserLogin(login string) (*model.User, error) {
|
2015-10-22 01:14:02 +02:00
|
|
|
var usr = new(model.User)
|
|
|
|
var err = meddler.QueryRow(db, usr, rebind(userLoginQuery), login)
|
|
|
|
return usr, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) GetUserList() ([]*model.User, error) {
|
2015-10-22 01:14:02 +02:00
|
|
|
var users = []*model.User{}
|
|
|
|
var err = meddler.QueryAll(db, &users, rebind(userListQuery))
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) GetUserFeed(listof []*model.RepoLite) ([]*model.Feed, error) {
|
2015-10-22 01:39:43 +02:00
|
|
|
var (
|
|
|
|
args []interface{}
|
|
|
|
stmt string
|
2016-07-06 15:02:36 +02:00
|
|
|
err error
|
2016-07-10 21:52:56 +02:00
|
|
|
|
|
|
|
feed = []*model.Feed{}
|
2015-10-22 01:39:43 +02:00
|
|
|
)
|
|
|
|
switch meddler.Default {
|
|
|
|
case meddler.PostgreSQL:
|
2016-09-02 13:32:17 +02:00
|
|
|
stmt, args = toListPostgres(listof)
|
2015-10-22 01:39:43 +02:00
|
|
|
default:
|
|
|
|
stmt, args = toList(listof)
|
|
|
|
}
|
2016-07-06 15:02:36 +02:00
|
|
|
if len(args) > 0 {
|
|
|
|
err = meddler.QueryAll(db, &feed, fmt.Sprintf(userFeedQuery, stmt), args...)
|
|
|
|
}
|
2015-10-22 01:39:43 +02:00
|
|
|
return feed, err
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 02:34:47 +02:00
|
|
|
func (db *datastore) GetUserFeedLatest(listof []*model.RepoLite) ([]*model.Feed, error) {
|
|
|
|
var (
|
|
|
|
args []interface{}
|
|
|
|
stmt string
|
2016-07-06 15:02:36 +02:00
|
|
|
err error
|
2016-07-10 21:52:56 +02:00
|
|
|
|
|
|
|
feed = []*model.Feed{}
|
2016-06-15 02:34:47 +02:00
|
|
|
)
|
|
|
|
switch meddler.Default {
|
|
|
|
case meddler.PostgreSQL:
|
2016-09-02 13:32:17 +02:00
|
|
|
stmt, args = toListPostgres(listof)
|
2016-06-15 02:34:47 +02:00
|
|
|
default:
|
|
|
|
stmt, args = toList(listof)
|
|
|
|
}
|
2016-07-06 15:02:36 +02:00
|
|
|
if len(args) > 0 {
|
|
|
|
err = meddler.QueryAll(db, &feed, fmt.Sprintf(userFeedLatest, stmt), args...)
|
|
|
|
}
|
2016-06-15 02:34:47 +02:00
|
|
|
return feed, err
|
|
|
|
}
|
|
|
|
|
2017-05-03 23:25:33 +02:00
|
|
|
func (db *datastore) GetUserCount() (count int, err error) {
|
|
|
|
stmt := sql.Lookup(db.driver, "count-users")
|
|
|
|
err = meddler.QueryAll(db, &count, stmt)
|
|
|
|
return
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) CreateUser(user *model.User) error {
|
2015-10-22 01:14:02 +02:00
|
|
|
return meddler.Insert(db, userTable, user)
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) UpdateUser(user *model.User) error {
|
2015-10-22 01:14:02 +02:00
|
|
|
return meddler.Update(db, userTable, user)
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) DeleteUser(user *model.User) error {
|
2015-10-22 01:14:02 +02:00
|
|
|
var _, err = db.Exec(rebind(userDeleteStmt), user.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const userTable = "users"
|
|
|
|
|
|
|
|
const userLoginQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM users
|
|
|
|
WHERE user_login=?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
const userListQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM users
|
|
|
|
ORDER BY user_login ASC
|
|
|
|
`
|
|
|
|
|
|
|
|
const userCountQuery = `
|
|
|
|
SELECT count(1)
|
|
|
|
FROM users
|
|
|
|
`
|
|
|
|
|
|
|
|
const userDeleteStmt = `
|
|
|
|
DELETE FROM users
|
|
|
|
WHERE user_id=?
|
|
|
|
`
|
|
|
|
|
|
|
|
const userFeedQuery = `
|
|
|
|
SELECT
|
|
|
|
repo_owner
|
|
|
|
,repo_name
|
|
|
|
,repo_full_name
|
|
|
|
,build_number
|
|
|
|
,build_event
|
|
|
|
,build_status
|
|
|
|
,build_created
|
|
|
|
,build_started
|
|
|
|
,build_finished
|
|
|
|
,build_commit
|
|
|
|
,build_branch
|
|
|
|
,build_ref
|
|
|
|
,build_refspec
|
|
|
|
,build_remote
|
|
|
|
,build_title
|
|
|
|
,build_message
|
|
|
|
,build_author
|
|
|
|
,build_email
|
|
|
|
,build_avatar
|
|
|
|
FROM
|
|
|
|
builds b
|
|
|
|
,repos r
|
|
|
|
WHERE b.build_repo_id = r.repo_id
|
2015-10-22 01:39:43 +02:00
|
|
|
AND r.repo_full_name IN (%s)
|
2015-10-22 01:14:02 +02:00
|
|
|
ORDER BY b.build_id DESC
|
2016-03-23 19:05:27 +02:00
|
|
|
LIMIT 50
|
2015-10-22 01:14:02 +02:00
|
|
|
`
|
2016-06-15 02:34:47 +02:00
|
|
|
|
|
|
|
// thanks to this article for helping me find a sane sql query
|
|
|
|
// https://www.periscopedata.com/blog/4-ways-to-join-only-the-first-row-in-sql.html
|
|
|
|
|
|
|
|
const userFeedLatest = `
|
|
|
|
SELECT
|
|
|
|
repo_owner
|
|
|
|
,repo_name
|
|
|
|
,repo_full_name
|
|
|
|
,build_number
|
|
|
|
,build_event
|
|
|
|
,build_status
|
|
|
|
,build_created
|
|
|
|
,build_started
|
|
|
|
,build_finished
|
|
|
|
,build_commit
|
|
|
|
,build_branch
|
|
|
|
,build_ref
|
|
|
|
,build_refspec
|
|
|
|
,build_remote
|
|
|
|
,build_title
|
|
|
|
,build_message
|
|
|
|
,build_author
|
|
|
|
,build_email
|
|
|
|
,build_avatar
|
|
|
|
FROM repos LEFT OUTER JOIN builds ON build_id = (
|
|
|
|
SELECT build_id FROM builds
|
|
|
|
WHERE builds.build_repo_id = repos.repo_id
|
|
|
|
ORDER BY build_id DESC
|
|
|
|
LIMIT 1
|
|
|
|
)
|
|
|
|
WHERE repo_full_name IN (%s)
|
|
|
|
`
|