Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: use QueryRow if possible (part 4) (#1780)

p0nyc592e3e72ba8
diff --git a/store/inbox.go b/store/inbox.go
index 5ce205758c..f4dec3aaf8 100644
--- a/store/inbox.go
+++ b/store/inbox.go
@@ -101,55 +101,22 @@ func (s *Store) FindInboxSummary(ctx context.Context, principalID int) (*api.Inb
defer tx.PTx.Rollback()
query := `SELECT EXISTS (SELECT 1 FROM inbox WHERE receiver_id = $1 AND status = 'UNREAD')`
- row, err := tx.PTx.QueryContext(ctx, query, principalID)
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
var inboxSummary api.InboxSummary
- var found bool
- if row.Next() {
- if err := row.Scan(
- &inboxSummary.HasUnread,
- ); err != nil {
- return nil, FormatError(err)
+ if err := tx.PTx.QueryRowContext(ctx, query, principalID).Scan(&inboxSummary.HasUnread); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- found = true
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- if err := row.Close(); err != nil {
- return nil, err
- }
- if !found {
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
- }
if inboxSummary.HasUnread {
query2 := `SELECT EXISTS (SELECT 1 FROM inbox, activity WHERE inbox.receiver_id = $1 AND inbox.status = 'UNREAD' AND inbox.activity_id = activity.id AND activity.level = 'ERROR')`
- row2, err := tx.PTx.QueryContext(ctx, query2, principalID)
- if err != nil {
- return nil, FormatError(err)
- }
- defer row2.Close()
-
- var found2 bool
- if row2.Next() {
- if err := row2.Scan(
- &inboxSummary.HasUnreadError,
- ); err != nil {
- return nil, FormatError(err)
+ if err := tx.PTx.QueryRowContext(ctx, query2, principalID).Scan(&inboxSummary.HasUnreadError); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query2)
}
- found2 = true
- }
- if err := row2.Err(); err != nil {
return nil, FormatError(err)
}
- if !found2 {
- return nil, common.FormatDBErrorEmptyRowWithQuery(query2)
- }
} else {
inboxSummary.HasUnreadError = false
}
@@ -265,38 +232,28 @@ func (s *Store) createInboxImpl(ctx context.Context, tx *sql.Tx, create *api.Inb
VALUES ($1, $2, 'UNREAD')
RETURNING id, receiver_id, activity_id, status
`
- row, err := tx.QueryContext(ctx, query,
+ var inboxRaw inboxRaw
+ var activityID int
+ if err := tx.QueryRowContext(ctx, query,
create.ReceiverID,
create.ActivityID,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var inboxRaw inboxRaw
- var activityID int
- if err := row.Scan(
- &inboxRaw.ID,
- &inboxRaw.ReceiverID,
- &activityID,
- &inboxRaw.Status,
- ); err != nil {
- return nil, FormatError(err)
- }
- activityRaw, err := s.getActivityRawByID(ctx, activityID)
- if err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &inboxRaw.ID,
+ &inboxRaw.ReceiverID,
+ &activityID,
+ &inboxRaw.Status,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- inboxRaw.ActivityRaw = activityRaw
- return &inboxRaw, nil
+ return nil, FormatError(err)
}
- if err := row.Err(); err != nil {
+ activityRaw, err := s.getActivityRawByID(ctx, activityID)
+ if err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ inboxRaw.ActivityRaw = activityRaw
+ return &inboxRaw, nil
}
func findInboxImpl(ctx context.Context, tx *sql.Tx, find *api.InboxFind) ([]*inboxRaw, error) {
@@ -375,41 +332,31 @@ func (s *Store) patchInboxImpl(ctx context.Context, tx *sql.Tx, patch *api.Inbox
set, args := []string{"status = $1"}, []interface{}{patch.Status}
args = append(args, patch.ID)
+ var inboxRaw inboxRaw
+ var activityID int
// Execute update query with RETURNING.
- row, err := tx.QueryContext(ctx, `
+ if err := tx.QueryRowContext(ctx, `
UPDATE inbox
SET `+strings.Join(set, ", ")+`
WHERE id = $2
RETURNING id, receiver_id, activity_id, status
`,
args...,
- )
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var inboxRaw inboxRaw
- var activityID int
- if err := row.Scan(
- &inboxRaw.ID,
- &inboxRaw.ReceiverID,
- &activityID,
- &inboxRaw.Status,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &inboxRaw.ID,
+ &inboxRaw.ReceiverID,
+ &activityID,
+ &inboxRaw.Status,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("inbox ID not found: %d", patch.ID)}
}
- activityRaw, err := s.getActivityRawByID(ctx, activityID)
- if err != nil {
- return nil, FormatError(err)
- }
- inboxRaw.ActivityRaw = activityRaw
- return &inboxRaw, nil
+ return nil, FormatError(err)
}
- if err := row.Err(); err != nil {
+ activityRaw, err := s.getActivityRawByID(ctx, activityID)
+ if err != nil {
return nil, FormatError(err)
}
-
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("inbox ID not found: %d", patch.ID)}
+ inboxRaw.ActivityRaw = activityRaw
+ return &inboxRaw, nil
}
diff --git a/store/instance.go b/store/instance.go
index f6d94fda86..25621d8869 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -133,23 +133,14 @@ func (s *Store) CountInstance(ctx context.Context, find *api.InstanceFind) (int,
where, args := findInstanceQuery(find)
query := `SELECT COUNT(*) FROM instance WHERE ` + where
- row, err := tx.PTx.QueryContext(ctx, query, args...)
- if err != nil {
- return 0, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- count := 0
- if err := row.Scan(&count); err != nil {
- return 0, FormatError(err)
+ var count int
+ if err := tx.PTx.QueryRowContext(ctx, query, args...).Scan(&count); err != nil {
+ if err == sql.ErrNoRows {
+ return 0, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return count, nil
- }
- if err := row.Err(); err != nil {
return 0, FormatError(err)
}
- return 0, common.FormatDBErrorEmptyRowWithQuery(query)
+ return count, nil
}
// CountInstanceGroupByEngineAndEnvironmentID counts the number of instances and group by engine and environment_id.
@@ -431,7 +422,8 @@ func createInstanceImpl(ctx context.Context, tx *sql.Tx, create *api.InstanceCre
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, environment_id, name, engine, engine_version, external_link, host, port
`
- row, err := tx.QueryContext(ctx, query,
+ var instanceRaw instanceRaw
+ if err := tx.QueryRowContext(ctx, query,
create.CreatorID,
create.CreatorID,
create.EnvironmentID,
@@ -440,38 +432,27 @@ func createInstanceImpl(ctx context.Context, tx *sql.Tx, create *api.InstanceCre
create.ExternalLink,
create.Host,
create.Port,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var instanceRaw instanceRaw
- if err := row.Scan(
- &instanceRaw.ID,
- &instanceRaw.RowStatus,
- &instanceRaw.CreatorID,
- &instanceRaw.CreatedTs,
- &instanceRaw.UpdaterID,
- &instanceRaw.UpdatedTs,
- &instanceRaw.EnvironmentID,
- &instanceRaw.Name,
- &instanceRaw.Engine,
- &instanceRaw.EngineVersion,
- &instanceRaw.ExternalLink,
- &instanceRaw.Host,
- &instanceRaw.Port,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &instanceRaw.ID,
+ &instanceRaw.RowStatus,
+ &instanceRaw.CreatorID,
+ &instanceRaw.CreatedTs,
+ &instanceRaw.UpdaterID,
+ &instanceRaw.UpdatedTs,
+ &instanceRaw.EnvironmentID,
+ &instanceRaw.Name,
+ &instanceRaw.Engine,
+ &instanceRaw.EngineVersion,
+ &instanceRaw.ExternalLink,
+ &instanceRaw.Host,
+ &instanceRaw.Port,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &instanceRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &instanceRaw, nil
}
func findInstanceImpl(ctx context.Context, tx *sql.Tx, find *api.InstanceFind) ([]*instanceRaw, error) {
@@ -556,45 +537,36 @@ func patchInstanceImpl(ctx context.Context, tx *sql.Tx, patch *api.InstancePatch
args = append(args, patch.ID)
+ var instanceRaw instanceRaw
// Execute update query with RETURNING.
- row, err := tx.QueryContext(ctx, fmt.Sprintf(`
+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE instance
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, environment_id, name, engine, engine_version, external_link, host, port
`, len(args)),
args...,
- )
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var instanceRaw instanceRaw
- if err := row.Scan(
- &instanceRaw.ID,
- &instanceRaw.RowStatus,
- &instanceRaw.CreatorID,
- &instanceRaw.CreatedTs,
- &instanceRaw.UpdaterID,
- &instanceRaw.UpdatedTs,
- &instanceRaw.EnvironmentID,
- &instanceRaw.Name,
- &instanceRaw.Engine,
- &instanceRaw.EngineVersion,
- &instanceRaw.ExternalLink,
- &instanceRaw.Host,
- &instanceRaw.Port,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &instanceRaw.ID,
+ &instanceRaw.RowStatus,
+ &instanceRaw.CreatorID,
+ &instanceRaw.CreatedTs,
+ &instanceRaw.UpdaterID,
+ &instanceRaw.UpdatedTs,
+ &instanceRaw.EnvironmentID,
+ &instanceRaw.Name,
+ &instanceRaw.Engine,
+ &instanceRaw.EngineVersion,
+ &instanceRaw.ExternalLink,
+ &instanceRaw.Host,
+ &instanceRaw.Port,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("instance ID not found: %d", patch.ID)}
}
- return &instanceRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("instance ID not found: %d", patch.ID)}
+ return &instanceRaw, nil
}
func findInstanceQuery(find *api.InstanceFind) (string, []interface{}) {
diff --git a/store/instance_user.go b/store/instance_user.go
index ea72f78752..8d0bcfd4d5 100644
--- a/store/instance_user.go
+++ b/store/instance_user.go
@@ -85,35 +85,25 @@ func upsertInstanceUserImpl(ctx context.Context, tx *sql.Tx, upsert *api.Instanc
"grant" = excluded.grant
RETURNING id, instance_id, name, "grant"
`
- row, err := tx.QueryContext(ctx, query,
+ var instanceUser api.InstanceUser
+ if err := tx.QueryRowContext(ctx, query,
upsert.CreatorID,
upsert.CreatorID,
upsert.InstanceID,
upsert.Name,
upsert.Grant,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var instanceUser api.InstanceUser
- if err := row.Scan(
- &instanceUser.ID,
- &instanceUser.InstanceID,
- &instanceUser.Name,
- &instanceUser.Grant,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &instanceUser.ID,
+ &instanceUser.InstanceID,
+ &instanceUser.Name,
+ &instanceUser.Grant,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &instanceUser, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &instanceUser, nil
}
func findInstanceUserImpl(ctx context.Context, tx *sql.Tx, find *api.InstanceUserFind) ([]*api.InstanceUser, error) {
diff --git a/store/issue.go b/store/issue.go
index e514eb04f6..81779711f1 100644
--- a/store/issue.go
+++ b/store/issue.go
@@ -489,7 +489,8 @@ func (s *Store) createIssueImpl(ctx context.Context, tx *sql.Tx, create *api.Iss
VALUES ($1, $2, $3, $4, $5, 'OPEN', $6, $7, $8, $9)
RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, pipeline_id, name, status, type, description, assignee_id, payload
`
- row, err := tx.QueryContext(ctx, query,
+ var issueRaw issueRaw
+ if err := tx.QueryRowContext(ctx, query,
create.CreatorID,
create.CreatorID,
create.ProjectID,
@@ -499,38 +500,27 @@ func (s *Store) createIssueImpl(ctx context.Context, tx *sql.Tx, create *api.Iss
create.Description,
create.AssigneeID,
create.Payload,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var issueRaw issueRaw
- if err := row.Scan(
- &issueRaw.ID,
- &issueRaw.CreatorID,
- &issueRaw.CreatedTs,
- &issueRaw.UpdaterID,
- &issueRaw.UpdatedTs,
- &issueRaw.ProjectID,
- &issueRaw.PipelineID,
- &issueRaw.Name,
- &issueRaw.Status,
- &issueRaw.Type,
- &issueRaw.Description,
- &issueRaw.AssigneeID,
- &issueRaw.Payload,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &issueRaw.ID,
+ &issueRaw.CreatorID,
+ &issueRaw.CreatedTs,
+ &issueRaw.UpdaterID,
+ &issueRaw.UpdatedTs,
+ &issueRaw.ProjectID,
+ &issueRaw.PipelineID,
+ &issueRaw.Name,
+ &issueRaw.Status,
+ &issueRaw.Type,
+ &issueRaw.Description,
+ &issueRaw.AssigneeID,
+ &issueRaw.Payload,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &issueRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &issueRaw, nil
}
func (s *Store) findIssueImpl(ctx context.Context, tx *sql.Tx, find *api.IssueFind) ([]*issueRaw, error) {
@@ -644,44 +634,34 @@ func (s *Store) patchIssueImpl(ctx context.Context, tx *sql.Tx, patch *api.Issue
args = append(args, patch.ID)
+ var issueRaw issueRaw
// Execute update query with RETURNING.
- row, err := tx.QueryContext(ctx, fmt.Sprintf(`
+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE issue
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, pipeline_id, name, status, type, description, assignee_id, payload
`, len(args)),
args...,
- )
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var issueRaw issueRaw
- if err := row.Scan(
- &issueRaw.ID,
- &issueRaw.CreatorID,
- &issueRaw.CreatedTs,
- &issueRaw.UpdaterID,
- &issueRaw.UpdatedTs,
- &issueRaw.ProjectID,
- &issueRaw.PipelineID,
- &issueRaw.Name,
- &issueRaw.Status,
- &issueRaw.Type,
- &issueRaw.Description,
- &issueRaw.AssigneeID,
- &issueRaw.Payload,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &issueRaw.ID,
+ &issueRaw.CreatorID,
+ &issueRaw.CreatedTs,
+ &issueRaw.UpdaterID,
+ &issueRaw.UpdatedTs,
+ &issueRaw.ProjectID,
+ &issueRaw.PipelineID,
+ &issueRaw.Name,
+ &issueRaw.Status,
+ &issueRaw.Type,
+ &issueRaw.Description,
+ &issueRaw.AssigneeID,
+ &issueRaw.Payload,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("unable to find issue ID to update: %d", patch.ID)}
}
- return &issueRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
-
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("unable to find issue ID to update: %d", patch.ID)}
+ return &issueRaw, nil
}
diff --git a/store/issue_subscriber.go b/store/issue_subscriber.go
index 00e6311b28..233b3f346f 100644
--- a/store/issue_subscriber.go
+++ b/store/issue_subscriber.go
@@ -138,30 +138,20 @@ func createIssueSubscriberImpl(ctx context.Context, tx *sql.Tx, create *api.Issu
VALUES ($1, $2)
RETURNING issue_id, subscriber_id
`
- row, err := tx.QueryContext(ctx, query,
+ var issueSubscriberRaw issueSubscriberRaw
+ if err := tx.QueryRowContext(ctx, query,
create.IssueID,
create.SubscriberID,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var issueSubscriberRaw issueSubscriberRaw
- if err := row.Scan(
- &issueSubscriberRaw.IssueID,
- &issueSubscriberRaw.SubscriberID,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &issueSubscriberRaw.IssueID,
+ &issueSubscriberRaw.SubscriberID,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &issueSubscriberRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &issueSubscriberRaw, nil
}
func findIssueSubscriberImpl(ctx context.Context, tx *sql.Tx, find *api.IssueSubscriberFind) ([]*issueSubscriberRaw, error) {
diff --git a/store/label.go b/store/label.go
index 49c1a7b13a..b8c2766538 100644
--- a/store/label.go
+++ b/store/label.go
@@ -541,39 +541,29 @@ func (s *Store) upsertDatabaseLabelImpl(ctx context.Context, tx *sql.Tx, upsert
value = excluded.value
RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, database_id, key, value
`
- row, err := tx.QueryContext(ctx, query,
+ var dbLabelRaw databaseLabelRaw
+ if err := tx.QueryRowContext(ctx, query,
upsert.RowStatus,
upsert.UpdaterID,
upsert.UpdaterID,
upsert.DatabaseID,
upsert.Key,
upsert.Value,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var dbLabelRaw databaseLabelRaw
- if err := row.Scan(
- &dbLabelRaw.ID,
- &dbLabelRaw.RowStatus,
- &dbLabelRaw.CreatorID,
- &dbLabelRaw.CreatedTs,
- &dbLabelRaw.UpdaterID,
- &dbLabelRaw.UpdatedTs,
- &dbLabelRaw.DatabaseID,
- &dbLabelRaw.Key,
- &dbLabelRaw.Value,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &dbLabelRaw.ID,
+ &dbLabelRaw.RowStatus,
+ &dbLabelRaw.CreatorID,
+ &dbLabelRaw.CreatedTs,
+ &dbLabelRaw.UpdaterID,
+ &dbLabelRaw.UpdatedTs,
+ &dbLabelRaw.DatabaseID,
+ &dbLabelRaw.Key,
+ &dbLabelRaw.Value,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &dbLabelRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &dbLabelRaw, nil
}
diff --git a/store/member.go b/store/member.go
index e715bca1ec..f1b02ad1f5 100644
--- a/store/member.go
+++ b/store/member.go
@@ -309,40 +309,30 @@ func createMemberImpl(ctx context.Context, tx *sql.Tx, create *api.MemberCreate)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, status, role, principal_id
`
- row, err := tx.QueryContext(ctx, query,
+ var memberRaw memberRaw
+ if err := tx.QueryRowContext(ctx, query,
create.CreatorID,
create.CreatorID,
create.Status,
create.Role,
create.PrincipalID,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var memberRaw memberRaw
- if err := row.Scan(
- &memberRaw.ID,
- &memberRaw.RowStatus,
- &memberRaw.CreatorID,
- &memberRaw.CreatedTs,
- &memberRaw.UpdaterID,
- &memberRaw.UpdatedTs,
- &memberRaw.Status,
- &memberRaw.Role,
- &memberRaw.PrincipalID,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &memberRaw.ID,
+ &memberRaw.RowStatus,
+ &memberRaw.CreatorID,
+ &memberRaw.CreatedTs,
+ &memberRaw.UpdaterID,
+ &memberRaw.UpdatedTs,
+ &memberRaw.Status,
+ &memberRaw.Role,
+ &memberRaw.PrincipalID,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &memberRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &memberRaw, nil
}
func findMemberImpl(ctx context.Context, tx *sql.Tx, find *api.MemberFind) ([]*memberRaw, error) {
@@ -418,39 +408,30 @@ func patchMemberImpl(ctx context.Context, tx *sql.Tx, patch *api.MemberPatch) (*
args = append(args, patch.ID)
+ var memberRaw memberRaw
// Execute update query with RETURNING.
- row, err := tx.QueryContext(ctx, fmt.Sprintf(`
+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE member
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, status, role, principal_id
`, len(args)),
args...,
- )
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var memberRaw memberRaw
- if err := row.Scan(
- &memberRaw.ID,
- &memberRaw.RowStatus,
- &memberRaw.CreatorID,
- &memberRaw.CreatedTs,
- &memberRaw.UpdaterID,
- &memberRaw.UpdatedTs,
- &memberRaw.Status,
- &memberRaw.Role,
- &memberRaw.PrincipalID,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &memberRaw.ID,
+ &memberRaw.RowStatus,
+ &memberRaw.CreatorID,
+ &memberRaw.CreatedTs,
+ &memberRaw.UpdaterID,
+ &memberRaw.UpdatedTs,
+ &memberRaw.Status,
+ &memberRaw.Role,
+ &memberRaw.PrincipalID,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("member ID not found: %d", patch.ID)}
}
- return &memberRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("member ID not found: %d", patch.ID)}
+ return &memberRaw, nil
}
diff --git a/store/pipeline.go b/store/pipeline.go
index b2abd63f99..6efefd8fea 100644
--- a/store/pipeline.go
+++ b/store/pipeline.go
@@ -285,36 +285,26 @@ func (s *Store) createPipelineImpl(ctx context.Context, tx *sql.Tx, create *api.
VALUES ($1, $2, $3, 'OPEN')
RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, status
`
- row, err := tx.QueryContext(ctx, query,
+ var pipelineRaw pipelineRaw
+ if err := tx.QueryRowContext(ctx, query,
create.CreatorID,
create.CreatorID,
create.Name,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var pipelineRaw pipelineRaw
- if err := row.Scan(
- &pipelineRaw.ID,
- &pipelineRaw.CreatorID,
- &pipelineRaw.CreatedTs,
- &pipelineRaw.UpdaterID,
- &pipelineRaw.UpdatedTs,
- &pipelineRaw.Name,
- &pipelineRaw.Status,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &pipelineRaw.ID,
+ &pipelineRaw.CreatorID,
+ &pipelineRaw.CreatedTs,
+ &pipelineRaw.UpdaterID,
+ &pipelineRaw.UpdatedTs,
+ &pipelineRaw.Name,
+ &pipelineRaw.Status,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &pipelineRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &pipelineRaw, nil
}
func (s *Store) findPipelineImpl(ctx context.Context, tx *sql.Tx, find *api.PipelineFind) ([]*pipelineRaw, error) {
@@ -380,37 +370,28 @@ func (s *Store) patchPipelineImpl(ctx context.Context, tx *sql.Tx, patch *api.Pi
args = append(args, patch.ID)
+ var pipelineRaw pipelineRaw
// Execute update query with RETURNING.
- row, err := tx.QueryContext(ctx, fmt.Sprintf(`
+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE pipeline
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, status
`, len(args)),
args...,
- )
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var pipelineRaw pipelineRaw
- if err := row.Scan(
- &pipelineRaw.ID,
- &pipelineRaw.CreatorID,
- &pipelineRaw.CreatedTs,
- &pipelineRaw.UpdaterID,
- &pipelineRaw.UpdatedTs,
- &pipelineRaw.Name,
- &pipelineRaw.Status,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &pipelineRaw.ID,
+ &pipelineRaw.CreatorID,
+ &pipelineRaw.CreatedTs,
+ &pipelineRaw.UpdaterID,
+ &pipelineRaw.UpdatedTs,
+ &pipelineRaw.Name,
+ &pipelineRaw.Status,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("pipeline ID not found: %d", patch.ID)}
}
- return &pipelineRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("pipeline ID not found: %d", patch.ID)}
+ return &pipelineRaw, nil
}
diff --git a/store/policy.go b/store/policy.go
index 148fc3617e..0a5b42be5f 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -411,41 +411,31 @@ func upsertPolicyImpl(ctx context.Context, tx *sql.Tx, upsert *api.PolicyUpsert)
%s
RETURNING id, creator_id, created_ts, updater_id, updated_ts, row_status, environment_id, type, payload
`, strings.Join(set, ","))
- row, err := tx.QueryContext(ctx, query,
+ var policyRaw policyRaw
+ if err := tx.QueryRowContext(ctx, query,
upsert.UpdaterID,
upsert.UpdaterID,
upsert.EnvironmentID,
upsert.Type,
upsert.Payload,
upsert.RowStatus,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var policyRaw policyRaw
- if err := row.Scan(
- &policyRaw.ID,
- &policyRaw.CreatorID,
- &policyRaw.CreatedTs,
- &policyRaw.UpdaterID,
- &policyRaw.UpdatedTs,
- &policyRaw.RowStatus,
- &policyRaw.EnvironmentID,
- &policyRaw.Type,
- &policyRaw.Payload,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &policyRaw.ID,
+ &policyRaw.CreatorID,
+ &policyRaw.CreatedTs,
+ &policyRaw.UpdaterID,
+ &policyRaw.UpdatedTs,
+ &policyRaw.RowStatus,
+ &policyRaw.EnvironmentID,
+ &policyRaw.Type,
+ &policyRaw.Payload,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &policyRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &policyRaw, nil
}
// deletePolicyImpl deletes an existing ARCHIVED policy by id and type.
diff --git a/store/principal.go b/store/principal.go
index c9c7633d84..235e020d71 100644
--- a/store/principal.go
+++ b/store/principal.go
@@ -282,41 +282,31 @@ func createPrincipalImpl(ctx context.Context, tx *sql.Tx, create *api.PrincipalC
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, creator_id, created_ts, updater_id, updated_ts, type, name, email, password_hash
`
- row, err := tx.QueryContext(ctx, query,
+ var principalRaw principalRaw
+ if err := tx.QueryRowContext(ctx, query,
create.CreatorID,
create.CreatorID,
create.Type,
create.Name,
create.Email,
create.PasswordHash,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var principalRaw principalRaw
- if err := row.Scan(
- &principalRaw.ID,
- &principalRaw.CreatorID,
- &principalRaw.CreatedTs,
- &principalRaw.UpdaterID,
- &principalRaw.UpdatedTs,
- &principalRaw.Type,
- &principalRaw.Name,
- &principalRaw.Email,
- &principalRaw.PasswordHash,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &principalRaw.ID,
+ &principalRaw.CreatorID,
+ &principalRaw.CreatedTs,
+ &principalRaw.UpdaterID,
+ &principalRaw.UpdatedTs,
+ &principalRaw.Type,
+ &principalRaw.Name,
+ &principalRaw.Email,
+ &principalRaw.PasswordHash,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &principalRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &principalRaw, nil
}
func findPrincipalRawListImpl(ctx context.Context, tx *sql.Tx, find *api.PrincipalFind) ([]*principalRaw, error) {
@@ -388,39 +378,30 @@ func patchPrincipalImpl(ctx context.Context, tx *sql.Tx, patch *api.PrincipalPat
args = append(args, patch.ID)
+ var principalRaw principalRaw
// Execute update query with RETURNING.
- row, err := tx.QueryContext(ctx, fmt.Sprintf(`
+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE principal
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
RETURNING id, creator_id, created_ts, updater_id, updated_ts, type, name, email, password_hash
`, len(args)),
args...,
- )
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var principalRaw principalRaw
- if err := row.Scan(
- &principalRaw.ID,
- &principalRaw.CreatorID,
- &principalRaw.CreatedTs,
- &principalRaw.UpdaterID,
- &principalRaw.UpdatedTs,
- &principalRaw.Type,
- &principalRaw.Name,
- &principalRaw.Email,
- &principalRaw.PasswordHash,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &principalRaw.ID,
+ &principalRaw.CreatorID,
+ &principalRaw.CreatedTs,
+ &principalRaw.UpdaterID,
+ &principalRaw.UpdatedTs,
+ &principalRaw.Type,
+ &principalRaw.Name,
+ &principalRaw.Email,
+ &principalRaw.PasswordHash,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("principal ID not found: %d", patch.ID)}
}
- return &principalRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("principal ID not found: %d", patch.ID)}
+ return &principalRaw, nil
}
diff --git a/store/project_member.go b/store/project_member.go
index 577c47fa31..9568f0ce41 100644
--- a/store/project_member.go
+++ b/store/project_member.go
@@ -404,7 +404,8 @@ func createProjectMemberImpl(ctx context.Context, tx *sql.Tx, create *api.Projec
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, role, principal_id, role_provider, payload
`
- row, err := tx.QueryContext(ctx, query,
+ var projectMemberRaw projectMemberRaw
+ if err := tx.QueryRowContext(ctx, query,
create.CreatorID,
create.CreatorID,
create.ProjectID,
@@ -412,35 +413,24 @@ func createProjectMemberImpl(ctx context.Context, tx *sql.Tx, create *api.Projec
create.PrincipalID,
create.RoleProvider,
create.Payload,
- )
-
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var projectMemberRaw projectMemberRaw
- if err := row.Scan(
- &projectMemberRaw.ID,
- &projectMemberRaw.CreatorID,
- &projectMemberRaw.CreatedTs,
- &projectMemberRaw.UpdaterID,
- &projectMemberRaw.UpdatedTs,
- &projectMemberRaw.ProjectID,
- &projectMemberRaw.Role,
- &projectMemberRaw.PrincipalID,
- &projectMemberRaw.RoleProvider,
- &projectMemberRaw.Payload,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &projectMemberRaw.ID,
+ &projectMemberRaw.CreatorID,
+ &projectMemberRaw.CreatedTs,
+ &projectMemberRaw.UpdaterID,
+ &projectMemberRaw.UpdatedTs,
+ &projectMemberRaw.ProjectID,
+ &projectMemberRaw.Role,
+ &projectMemberRaw.PrincipalID,
+ &projectMemberRaw.RoleProvider,
+ &projectMemberRaw.Payload,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
- return &projectMemberRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ return &projectMemberRaw, nil
}
func findProjectMemberImpl(ctx context.Context, tx *sql.Tx, find *api.ProjectMemberFind) ([]*projectMemberRaw, error) {
@@ -525,42 +515,33 @@ func patchProjectMemberImpl(ctx context.Context, tx *sql.Tx, patch *api.ProjectM
args = append(args, patch.ID)
+ var projectMemberRaw projectMemberRaw
// Execute update query with RETURNING.
- row, err := tx.QueryContext(ctx, fmt.Sprintf(`
+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE project_member
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, role, principal_id, role_provider, payload
`, len(args)),
args...,
- )
- if err != nil {
- return nil, FormatError(err)
- }
- defer row.Close()
-
- if row.Next() {
- var projectMemberRaw projectMemberRaw
- if err := row.Scan(
- &projectMemberRaw.ID,
- &projectMemberRaw.CreatorID,
- &projectMemberRaw.CreatedTs,
- &projectMemberRaw.UpdaterID,
- &projectMemberRaw.UpdatedTs,
- &projectMemberRaw.ProjectID,
- &projectMemberRaw.Role,
- &projectMemberRaw.PrincipalID,
- &projectMemberRaw.RoleProvider,
- &projectMemberRaw.Payload,
- ); err != nil {
- return nil, FormatError(err)
+ ).Scan(
+ &projectMemberRaw.ID,
+ &projectMemberRaw.CreatorID,
+ &projectMemberRaw.CreatedTs,
+ &projectMemberRaw.UpdaterID,
+ &projectMemberRaw.UpdatedTs,
+ &projectMemberRaw.ProjectID,
+ &projectMemberRaw.Role,
+ &projectMemberRaw.PrincipalID,
+ &projectMemberRaw.RoleProvider,
+ &projectMemberRaw.Payload,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project member ID not found: %d", patch.ID)}
}
- return &projectMemberRaw, nil
- }
- if err := row.Err(); err != nil {
return nil, FormatError(err)
}
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project member ID not found: %d", patch.ID)}
+ return &projectMemberRaw, nil
}
// deleteProjectMemberImpl permanently deletes a projectMember by ID.