Skip to content

Commit

Permalink
add Count() function on player, blocks and mod_storage
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Aug 28, 2023
1 parent 579df82 commit 1382812
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type BlockRepository interface {
Update(block *Block) error
Delete(x, y, z int) error
Vacuum() error
Count() (int64, error)
}

func NewBlockRepository(db *sql.DB, dbtype types.DatabaseType) BlockRepository {
Expand Down
7 changes: 7 additions & 0 deletions block/block_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func (repo *postgresBlockRepository) Vacuum() error {
return err
}

func (repo *postgresBlockRepository) Count() (int64, error) {
row := repo.db.QueryRow("select count(*) from blocks")
count := int64(0)
err := row.Scan(&count)
return count, err
}

func (r *postgresBlockRepository) Export(z *zip.Writer) error {
w, err := z.Create("blocks.json")
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions block/block_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func (repo *sqliteBlockRepository) Vacuum() error {
return err
}

func (repo *sqliteBlockRepository) Count() (int64, error) {
row := repo.db.QueryRow("select count(*) from blocks")
count := int64(0)
err := row.Scan(&count)
return count, err
}

func (r *sqliteBlockRepository) Export(z *zip.Writer) error {
w, err := z.Create("blocks.json")
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func testBlocksRepository(t *testing.T, block_repo block.BlockRepository) {
}
assert.NoError(t, block_repo.Update(b))

// count
blocks, err := block_repo.Count()
assert.NoError(t, err)
assert.Equal(t, int64(1), blocks)

// get
b, err = block_repo.GetByPos(0, 0, 0)
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions mod_storage/mod_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ModStorageRepository interface {
Create(entry *ModStorageEntry) error
Update(entry *ModStorageEntry) error
Delete(modname string, key []byte) error
Count() (int64, error)
Export(z *zip.Writer) error
Import(z *zip.Reader) error
}
Expand Down
7 changes: 7 additions & 0 deletions mod_storage/mod_storage_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func (repo *modStorageSqliteRepository) Delete(modname string, key []byte) error
return err
}

func (repo *modStorageSqliteRepository) Count() (int64, error) {
row := repo.db.QueryRow("select count(*) from entries")
count := int64(0)
err := row.Scan(&count)
return count, err
}

func (repo *modStorageSqliteRepository) Export(z *zip.Writer) error {
w, err := z.Create("mod_storage.json")
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions mod_storage/mod_storage_sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func TestModStorageSQliteRepo(t *testing.T) {
}
assert.NoError(t, repo.Create(entry))

// count
entry_count, err := repo.Count()
assert.NoError(t, err)
assert.Equal(t, int64(3), entry_count)

// export
buf := bytes.NewBuffer([]byte{})
w := zip.NewWriter(buf)
Expand Down
7 changes: 7 additions & 0 deletions player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ func (r *PlayerRepository) RemovePlayer(name string) error {
return err
}

func (r *PlayerRepository) Count() (int64, error) {
row := r.db.QueryRow("select count(*) from player")
count := int64(0)
err := row.Scan(&count)
return count, err
}

func (r *PlayerRepository) Export(z *zip.Writer) error {
w, err := z.Create("player.json")
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions player/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func TestSqlitePlayerRepo(t *testing.T) {
repo := player.NewPlayerRepository(db, types.DATABASE_SQLITE)
assert.NotNil(t, repo)

// count
player_count, err := repo.Count()
assert.NoError(t, err)
assert.Equal(t, int64(1), player_count)

// existing entry
p, err := repo.GetPlayer("singleplayer")
assert.NoError(t, err)
Expand Down

0 comments on commit 1382812

Please sign in to comment.