Skip to content

Commit

Permalink
Merge pull request #111 from isucon/rename-row_number
Browse files Browse the repository at this point in the history
row_numberはMySQL8では予約語(関数)なのでrow_numにする
  • Loading branch information
fujiwara authored Jun 28, 2022
2 parents 219293a + 28bf6da commit 11bcdaa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
initial_data.tar.gz
go/
6 changes: 3 additions & 3 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ func storeTenant(tenant *isuports.TenantRow, players []*isuports.PlayerRow, comp
fmt.Fprint(os.Stderr, ".")
tx = mustTx()
if _, err := tx.NamedExec(
`INSERT INTO player_score (tenant_id, id, player_id, competition_id, score, row_number, created_at, updated_at)
VALUES(:tenant_id, :id, :player_id, :competition_id, :score, :row_number, :created_at, :updated_at)`,
`INSERT INTO player_score (tenant_id, id, player_id, competition_id, score, row_num, created_at, updated_at)
VALUES(:tenant_id, :id, :player_id, :competition_id, :score, :row_num, :created_at, :updated_at)`,
pss[from:i],
); err != nil {
tx.Rollback()
Expand Down Expand Up @@ -438,7 +438,7 @@ func CreatePlayerData(
return competitionScores[i].CreatedAt < competitionScores[j].CreatedAt
})
for i := range competitionScores {
competitionScores[i].RowNumber = int64(i + 1)
competitionScores[i].RowNum = int64(i + 1)
}
scores = append(scores, competitionScores...)
}
Expand Down
24 changes: 12 additions & 12 deletions webapp/go/isuports.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ type PlayerScoreRow struct {
PlayerID string `db:"player_id"`
CompetitionID string `db:"competition_id"`
Score int64 `db:"score"`
RowNumber int64 `db:"row_number"`
RowNum int64 `db:"row_num"`
CreatedAt int64 `db:"created_at"`
UpdatedAt int64 `db:"updated_at"`
}
Expand Down Expand Up @@ -1045,10 +1045,10 @@ func competitionResultHandler(c echo.Context) error {
return fmt.Errorf("error flockByTenantID: %w", err)
}
defer fl.Close()
var rowNumber int64
var rowNum int64
playerScoreRows := []PlayerScoreRow{}
for {
rowNumber++
rowNum++
row, err := r.Read()
if err != nil {
if err == io.EOF {
Expand Down Expand Up @@ -1088,7 +1088,7 @@ func competitionResultHandler(c echo.Context) error {
PlayerID: playerID,
CompetitionID: competitionID,
Score: score,
RowNumber: rowNumber,
RowNum: rowNum,
CreatedAt: now,
UpdatedAt: now,
})
Expand All @@ -1105,12 +1105,12 @@ func competitionResultHandler(c echo.Context) error {
for _, ps := range playerScoreRows {
if _, err := tenantDB.NamedExecContext(
ctx,
"INSERT INTO player_score (id, tenant_id, player_id, competition_id, score, row_number, created_at, updated_at) VALUES (:id, :tenant_id, :player_id, :competition_id, :score, :row_number, :created_at, :updated_at)",
"INSERT INTO player_score (id, tenant_id, player_id, competition_id, score, row_num, created_at, updated_at) VALUES (:id, :tenant_id, :player_id, :competition_id, :score, :row_num, :created_at, :updated_at)",
ps,
); err != nil {
return fmt.Errorf(
"error Insert player_score: id=%s, tenant_id=%d, playerID=%s, competitionID=%s, score=%d, rowNumber=%d, createdAt=%d, updatedAt=%d, %w",
ps.ID, ps.TenantID, ps.PlayerID, ps.CompetitionID, ps.Score, ps.RowNumber, ps.CreatedAt, ps.UpdatedAt, err,
"error Insert player_score: id=%s, tenant_id=%d, playerID=%s, competitionID=%s, score=%d, rowNum=%d, createdAt=%d, updatedAt=%d, %w",
ps.ID, ps.TenantID, ps.PlayerID, ps.CompetitionID, ps.Score, ps.RowNum, ps.CreatedAt, ps.UpdatedAt, err,
)

}
Expand Down Expand Up @@ -1231,8 +1231,8 @@ func playerHandler(c echo.Context) error {
if err := tenantDB.GetContext(
ctx,
&ps,
// 最後にCSVに登場したスコアを採用する = row_numberが一番行もの
"SELECT * FROM player_score WHERE tenant_id = ? AND competition_id = ? AND player_id = ? ORDER BY row_number DESC LIMIT 1",
// 最後にCSVに登場したスコアを採用する = row_numが一番大きいもの
"SELECT * FROM player_score WHERE tenant_id = ? AND competition_id = ? AND player_id = ? ORDER BY row_num DESC LIMIT 1",
v.tenantID,
c.ID,
p.ID,
Expand Down Expand Up @@ -1353,7 +1353,7 @@ func competitionRankingHandler(c echo.Context) error {
if err := tenantDB.SelectContext(
ctx,
&pss,
"SELECT * FROM player_score WHERE tenant_id = ? AND competition_id = ? ORDER BY row_number DESC",
"SELECT * FROM player_score WHERE tenant_id = ? AND competition_id = ? ORDER BY row_num DESC",
tenant.ID,
competitionID,
); err != nil {
Expand All @@ -1362,8 +1362,8 @@ func competitionRankingHandler(c echo.Context) error {
ranks := make([]CompetitionRank, 0, len(pss))
scoredPlayerSet := make(map[string]struct{}, len(pss))
for _, ps := range pss {
// player_scoreが同一player_id内ではrow_numberの降順でソートされているので
// 現れたのが2回目以降のplayer_idはより大きいrow_numberでスコアが出ているとみなせる
// player_scoreが同一player_id内ではrow_numの降順でソートされているので
// 現れたのが2回目以降のplayer_idはより大きいrow_numでスコアが出ているとみなせる
if _, ok := scoredPlayerSet[ps.PlayerID]; ok {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion webapp/sql/tenant/10_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CREATE TABLE player_score (
player_id VARCHAR(255) NOT NULL,
competition_id VARCHAR(255) NOT NULL,
score BIGINT NOT NULL,
row_number BIGINT NOT NULL,
row_num BIGINT NOT NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL
);

0 comments on commit 11bcdaa

Please sign in to comment.