harness-drone/store/user/scan.go
2019-02-19 15:56:41 -08:00

75 lines
1.8 KiB
Go

// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
package user
import (
"database/sql"
"github.com/drone/drone/store/shared/db"
"github.com/drone/drone/core"
)
// helper function converts the User structure to a set
// of named query parameters.
func toParams(u *core.User) map[string]interface{} {
return map[string]interface{}{
"user_id": u.ID,
"user_login": u.Login,
"user_email": u.Email,
"user_admin": u.Admin,
"user_machine": u.Machine,
"user_active": u.Active,
"user_avatar": u.Avatar,
"user_syncing": u.Syncing,
"user_synced": u.Synced,
"user_created": u.Created,
"user_updated": u.Updated,
"user_last_login": u.LastLogin,
"user_oauth_token": u.Token,
"user_oauth_refresh": u.Refresh,
"user_oauth_expiry": u.Expiry,
"user_hash": u.Hash,
}
}
// helper function scans the sql.Row and copies the column
// values to the destination object.
func scanRow(scanner db.Scanner, dest *core.User) error {
return scanner.Scan(
&dest.ID,
&dest.Login,
&dest.Email,
&dest.Admin,
&dest.Machine,
&dest.Active,
&dest.Avatar,
&dest.Syncing,
&dest.Synced,
&dest.Created,
&dest.Updated,
&dest.LastLogin,
&dest.Token,
&dest.Refresh,
&dest.Expiry,
&dest.Hash,
)
}
// helper function scans the sql.Row and copies the column
// values to the destination object.
func scanRows(rows *sql.Rows) ([]*core.User, error) {
defer rows.Close()
users := []*core.User{}
for rows.Next() {
user := new(core.User)
err := scanRow(rows, user)
if err != nil {
return nil, err
}
users = append(users, user)
}
return users, nil
}