Skip to content

Commit

Permalink
(BIDS-2548) Refactor query call placement
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisei24 committed Oct 17, 2023
1 parent b651bc5 commit 94f0698
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,30 @@ func GetEth1DepositsJoinEth2Deposits(query string, length, start uint64, orderBy
// The query does not fulfill any of the requirements for a search
shouldSearch = false
}
}
} else {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, ""))
if err != nil {
return nil, 0, err
}

// The deposits count query only has one parameter for the search
countSearchQuery := strings.ReplaceAll(searchQuery, "$3", "$1")
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, "", orderBy, orderDir), length, start)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}

return deposits, totalCount, nil
}

// Get the deposits and the total count
if shouldSearch {
if param != nil {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery), param)
} else {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery))
}
// The deposits count query only has one parameter for the search
countSearchQuery := strings.ReplaceAll(searchQuery, "$3", "$1")

err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery), param)
if err != nil {
return nil, 0, err
}

if param != nil {
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
} else {
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start)
}
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}
Expand Down Expand Up @@ -406,27 +409,30 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
// The query does not fulfill any of the requirements for a search
shouldSearch = false
}
}
} else {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, ""))
if err != nil {
return nil, 0, err
}

// The deposits count query only has one parameter for the search
countSearchQuery := strings.ReplaceAll(searchQuery, "$3", "$1")
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, "", orderBy, orderDir), length, start)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}

return deposits, totalCount, nil
}

// Get the deposits and the total count
if shouldSearch {
if param != nil {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery), param)
} else {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery))
}
// The deposits count query only has one parameter for the search
countSearchQuery := strings.ReplaceAll(searchQuery, "$3", "$1")

err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery), param)
if err != nil {
return nil, 0, err
}

if param != nil {
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
} else {
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start)
}
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}
Expand Down

0 comments on commit 94f0698

Please sign in to comment.