-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8818907
commit feab9b0
Showing
8 changed files
with
337 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package player | ||
|
||
import ( | ||
"github.com/minetest-go/mtdb/types" | ||
) | ||
|
||
func getColumns(dbtype types.DatabaseType) []string { | ||
switch dbtype { | ||
case types.DATABASE_SQLITE: | ||
return []string{ | ||
"name", "pitch", "yaw", | ||
"posx", "posy", "posz", | ||
"hp", "breath", | ||
"strftime('%s', creation_date)", | ||
"strftime('%s', modification_date)", | ||
} | ||
case types.DATABASE_POSTGRES: | ||
return []string{ | ||
"name", "pitch", "yaw", | ||
"posx", "posy", "posz", | ||
"hp", "breath", | ||
"extract(epoch from creation_date)::int", | ||
"extract(epoch from modification_date)::int", | ||
} | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func scanPlayer(scan func(v ...any) error) (*Player, error) { | ||
p := &Player{} | ||
err := scan(&p.Name, &p.Pitch, &p.Yaw, &p.PosX, &p.PosY, &p.PosZ, &p.HP, &p.Breath, &p.CreationDate, &p.ModificationDate) | ||
return p, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package player | ||
|
||
import ( | ||
"archive/zip" | ||
"bufio" | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
func (r *PlayerRepository) Export(z *zip.Writer) error { | ||
w, err := z.Create("player.json") | ||
if err != nil { | ||
return err | ||
} | ||
enc := json.NewEncoder(w) | ||
|
||
rows, err := r.db.Query("select name from player") | ||
if err != nil { | ||
return err | ||
} | ||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
name := "" | ||
err = rows.Scan(&name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
player, err := r.GetPlayer(name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = enc.Encode(player) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *PlayerRepository) Import(z *zip.Reader) error { | ||
f, err := z.Open("player.json") | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
sc := bufio.NewScanner(f) | ||
for sc.Scan() { | ||
dc := json.NewDecoder(bytes.NewReader(sc.Bytes())) | ||
e := &Player{} | ||
err = dc.Decode(e) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = r.CreateOrUpdate(e) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package player | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func (repo *PlayerRepository) buildWhereClause(fields string, s *PlayerSearch) (string, []any) { | ||
q := `select ` + fields + ` from player where true ` | ||
args := make([]any, 0) | ||
i := 1 | ||
|
||
if s.Name != nil { | ||
q += fmt.Sprintf(" and name = $%d", i) | ||
args = append(args, *s.Name) | ||
i++ | ||
} | ||
|
||
if s.Namelike != nil { | ||
q += fmt.Sprintf(" and name like $%d", i) | ||
args = append(args, *s.Namelike) | ||
i++ | ||
} | ||
|
||
if s.OrderColumn != nil && orderColumns[*s.OrderColumn] { | ||
order := Ascending | ||
if s.OrderDirection != nil && orderDirections[*s.OrderDirection] { | ||
order = *s.OrderDirection | ||
} | ||
|
||
q += fmt.Sprintf(" order by %s %s", *s.OrderColumn, order) | ||
} | ||
|
||
// limit result length to 1000 per default | ||
limit := 1000 | ||
if s.Limit != nil { | ||
limit = *s.Limit | ||
} | ||
q += fmt.Sprintf(" limit %d", limit) | ||
|
||
return q, args | ||
} | ||
|
||
func (repo *PlayerRepository) Search(s *PlayerSearch) ([]*Player, error) { | ||
q, args := repo.buildWhereClause(strings.Join(getColumns(repo.dbtype), ","), s) | ||
rows, err := repo.db.Query(q, args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
list := make([]*Player, 0) | ||
for rows.Next() { | ||
p, err := scanPlayer(rows.Scan) | ||
if err != nil { | ||
return nil, err | ||
} | ||
list = append(list, p) | ||
} | ||
return list, nil | ||
} | ||
|
||
func (repo *PlayerRepository) Count(s *PlayerSearch) (int, error) { | ||
q, args := repo.buildWhereClause("count(*)", s) | ||
row := repo.db.QueryRow(q, args...) | ||
count := 0 | ||
err := row.Scan(&count) | ||
return count, err | ||
} |
Oops, something went wrong.