完成 Commit 查询的方法

This commit is contained in:
巴拉迪维 2022-04-18 15:29:42 +08:00
parent fc8b09f3b3
commit 089a033748
6 changed files with 167 additions and 1 deletions

View File

@ -9,6 +9,7 @@
package gitee
import (
"errors"
"reflect"
"time"
)
@ -28,3 +29,17 @@ type User struct {
func (u User) isNilOrEmpty() bool {
return reflect.DeepEqual(u, User{})
}
func (r *User) Scan(src interface{}) error {
var userId int
switch src.(type) {
case int64:
userId = int(src.(int64))
case int32:
userId = int(src.(int32))
default:
return errors.New("can not find any valid user")
}
*r = User{ID: userId}
return nil
}

View File

@ -34,7 +34,10 @@ func InitDatabaseService() (*DatabaseService, error) {
conn.SetMaxOpenConns(utils.DatabaseConifg.MaxOpenConns)
conn.SetMaxIdleConns(utils.DatabaseConifg.MaxIdleConn)
conn.SetConnMaxLifetime(0) //always REUSE
dbService.Connection = conn
// Unsafe returns a version of DB which will silently succeed to scan when
// columns in the SQL result have no fields in the destination struct.
dbService.Connection = conn.Unsafe()
return dbService, nil
}

View File

@ -13,6 +13,12 @@ import (
"repostats/storage"
)
var selectQueryPrefix = `SELECT c.author_name AS "author.name",c.author_email AS "author.email",
c.author_name AS "commit.author.name", c.author_email AS "commit.author.email", c.author_date AS "commit.author.date",
c.committer_name AS "committer.name",c.committer_email AS "committer.email",
c.committer_name AS "commit.committer.name", c.committer_email AS "commit.committer.email", c.committer_date AS "commit.committer.date",
c.detail_message AS "commit.message", c.tree AS "commit.tree.sha",c.* FROM gitee.commits c `
func BulkSaveCommits(commits []gitee_model.Commit) error {
query := `INSERT INTO gitee.commits (repo_id, sha, html_url, author_name, author_email, author_date, committer_name,
committer_email, committer_date, detail_message,tree)
@ -23,3 +29,24 @@ func BulkSaveCommits(commits []gitee_model.Commit) error {
committer_email=EXCLUDED.committer_email,committer_date=EXCLUDED.committer_date,detail_message=EXCLUDED.detail_message,tree=EXCLUDED.tree`
return storage.DbNamedExec(query, commits)
}
func FindCommits() ([]gitee_model.Commit, error) {
found := []gitee_model.Commit{}
query := selectQueryPrefix + ` ORDER BY c.author_date DESC`
err := storage.DbSelect(query, &found)
return found, err
}
func FindCommitBySha(sha string) (gitee_model.Commit, error) {
found := gitee_model.Commit{}
query := selectQueryPrefix + ` WHERE c.sha = $1 ORDER BY c.author_date DESC`
err := storage.DbGet(query, &found, sha)
return found, err
}
func FindCommitsByRepoID(repoID int) ([]gitee_model.Commit, error) {
found := []gitee_model.Commit{}
query := selectQueryPrefix + ` WHERE c.repo_id = $1 ORDER BY c.author_date DESC`
err := storage.DbSelect(query, &found, repoID)
return found, err
}

View File

@ -9,6 +9,7 @@
package gitee
import (
"reflect"
gitee_model "repostats/model/gitee"
"repostats/network"
"repostats/storage"
@ -64,3 +65,89 @@ func TestBulkSaveCommits(t *testing.T) {
})
}
}
func TestFindCommits(t *testing.T) {
testSetup(t)
defer testTeardown(t)
tests := []struct {
name string
want int
wantErr bool
}{
{name: "TestCase1", want: 597, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FindCommits()
if (err != nil) != tt.wantErr {
t.Errorf("FindCommits() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(len(got), tt.want) {
t.Errorf("FindCommits() = %v, want %v", got, tt.want)
}
})
}
}
func TestFindCommitsByRepoID(t *testing.T) {
testSetup(t)
defer testTeardown(t)
type args struct {
repoID int
}
tests := []struct {
name string
args args
want int
wantErr bool
}{
{name: "TestCase 1", args: args{repoID: 111}, want: 0, wantErr: false},
{name: "TestCase 2", args: args{repoID: 10918992}, want: 597, wantErr: false}, // 597
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FindCommitsByRepoID(tt.args.repoID)
if (err != nil) != tt.wantErr {
t.Errorf("FindCommitsByRepoID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(len(got), tt.want) {
t.Errorf("FindCommitsByRepoID() = %v, want %v", got, tt.want)
}
})
}
}
func TestFindCommitBySha(t *testing.T) {
testSetup(t)
defer testTeardown(t)
type args struct {
sha string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{name: "TestCase 1", args: args{sha: "adsfas"}, want: "", wantErr: false},
{name: "TestCase 2", args: args{sha: "dfb6fb1514ceb065ecd18c71a4c38ecb4b497b1c"}, want: "dfb6fb1514ceb065ecd18c71a4c38ecb4b497b1c", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FindCommitBySha(tt.args.sha)
if (err != nil) != tt.wantErr {
t.Errorf("FindCommitBySha() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.Sha, tt.want) {
t.Errorf("FindCommitBySha() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -25,3 +25,10 @@ func BulkSaveRepos(repos []gitee_model.Repository) error {
license=EXCLUDED.license,pushed_at=EXCLUDED.pushed_at,created_at=EXCLUDED.created_at,updated_at=EXCLUDED.updated_at`
return storage.DbNamedExec(query, repos)
}
func FindRepos() ([]gitee_model.Repository, error) {
repos := []gitee_model.Repository{}
query := `SELECT r.owner_id AS "owner", r.assigner_id AS "assigner",r.* FROM gitee.repos r ORDER BY r.id DESC`
err := storage.DbSelect(query, &repos)
return repos, err
}

View File

@ -9,6 +9,7 @@
package gitee
import (
"reflect"
gitee_model "repostats/model/gitee"
"repostats/network"
"repostats/utils"
@ -45,3 +46,29 @@ func TestBulkSaveRepos(t *testing.T) {
})
}
}
func TestFindRepos(t *testing.T) {
testSetup(t)
defer testTeardown(t)
tests := []struct {
name string
want int
wantErr bool
}{
{name: "TestCase FindRepos()", want: 399, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FindRepos()
if (err != nil) != tt.wantErr {
t.Errorf("FindRepos() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(len(got), tt.want) {
t.Errorf("FindRepos() = %v, want %v", got, tt.want)
}
})
}
}