diff --git a/migrations/20210411_add_imprint_content.go b/migrations/20210411_add_imprint_content.go index d29345d..d275d8e 100644 --- a/migrations/20210411_add_imprint_content.go +++ b/migrations/20210411_add_imprint_content.go @@ -1,8 +1,6 @@ package migrations import ( - "fmt" - "github.com/muety/wakapi/config" "github.com/muety/wakapi/models" "github.com/muety/wakapi/utils" @@ -19,7 +17,7 @@ func init() { return nil } - condition := fmt.Sprintf("%s = ?", utils.QuoteDbIdentifier(db, "key")) + condition := utils.QuoteSql(db, "%s = ?", "key") imprintKv := &models.KeyStringValue{Key: "imprint", Value: "no content here"} if err := db. diff --git a/migrations/shared.go b/migrations/shared.go index 95e71db..ef1b00a 100644 --- a/migrations/shared.go +++ b/migrations/shared.go @@ -1,8 +1,6 @@ package migrations import ( - "fmt" - "github.com/emvi/logbuch" "github.com/muety/wakapi/models" "github.com/muety/wakapi/utils" @@ -10,7 +8,7 @@ import ( ) func hasRun(name string, db *gorm.DB) bool { - condition := fmt.Sprintf("%s = ?", utils.QuoteDbIdentifier(db, "key")) + condition := utils.QuoteSql(db, "%s = ?", "key") lookupResult := db.Where(condition, name).First(&models.KeyStringValue{}) if lookupResult.Error == nil && lookupResult.RowsAffected > 0 { diff --git a/repositories/heartbeat.go b/repositories/heartbeat.go index 5176db7..70de35c 100644 --- a/repositories/heartbeat.go +++ b/repositories/heartbeat.go @@ -234,10 +234,17 @@ func (r *HeartbeatRepository) GetUserProjectStats(user *models.User, from, to ti // multi-line string with backticks yields an error with the github.com/glebarez/sqlite driver - limitClause := "limit ?" + args := []interface{}{ + user.ID, from.Format(time.RFC3339), to.Format(time.RFC3339), + } + + limitOffsetClause := "limit ? offset ?" if r.config.Db.IsMssql() { - limitClause = "ROWS fetch next ? rows only" + limitOffsetClause = "offset ? ROWS fetch next ? rows only" + args = append(args, offset, limit) + } else { + args = append(args, limit, offset) } if err := r.db. @@ -249,13 +256,13 @@ func (r *HeartbeatRepository) GetUserProjectStats(user *models.User, from, to ti "and language is not null and language != '' and project != '' "+ "group by project, user_id "+ "order by last desc "+ - "offset ? "+limitClause+ + limitOffsetClause+ ") "+ "select distinct project, min(first) as first, min(last) as last, min(cnt) as count, first_value(language) over (partition by project order by count(*) desc) as top_language "+ "from heartbeats "+ "inner join projects on heartbeats.project = projects.p and heartbeats.user_id = projects.user_id "+ "group by project, language "+ - "order by last desc", user.ID, from.Format(time.RFC3339), to.Format(time.RFC3339), offset, limit). + "order by last desc", args...). Scan(&projectStats).Error; err != nil { return nil, err } diff --git a/repositories/user.go b/repositories/user.go index 53d220c..2c4bc0f 100644 --- a/repositories/user.go +++ b/repositories/user.go @@ -5,6 +5,7 @@ import ( "time" "github.com/muety/wakapi/models" + "github.com/muety/wakapi/utils" "gorm.io/gorm" ) @@ -86,12 +87,12 @@ func (r *UserRepository) GetByLoggedInAfter(t time.Time) ([]*models.User, error) // NOTE: Only ID field will be populated func (r *UserRepository) GetByLastActiveAfter(t time.Time) ([]*models.User, error) { subQuery1 := r.db.Model(&models.Heartbeat{}). - Select("user_id , max(time) as time"). + Select(utils.QuoteSql(r.db, "user_id as %s, max(time) as time", "user")). Group("user_id") var userIds []string if err := r.db. - Select("user"). + Select(utils.QuoteSql(r.db, "user as %s", "id")). Table("(?) as q", subQuery1). Where("time >= ?", t.Local()). Scan(&userIds).Error; err != nil {