mapserver/mapobjectdb/postgres/mapobjects.go

130 lines
2.4 KiB
Go
Raw Permalink Normal View History

2019-03-21 17:07:09 +03:00
package postgres
import (
"database/sql"
2019-03-21 17:07:09 +03:00
"mapserver/mapobjectdb"
2023-12-29 18:00:11 +03:00
"mapserver/types"
"unicode/utf8"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
2019-03-21 17:07:09 +03:00
)
func (a *PostgresAccessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectdb.MapObject, error) {
var rows *sql.Rows
var err error
if q.AttributeLike == nil {
//plain pos search
rows, err = a.db.Query(getMapDataPosQuery,
q.Type,
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
*q.Limit,
)
} else {
//attribute like search
rows, err = a.db.Query(getMapDataWithAttributeLikePosQuery,
q.Type,
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
q.AttributeLike.Key, q.AttributeLike.Value,
*q.Limit,
)
}
2019-03-21 17:07:09 +03:00
if err != nil {
return nil, err
}
defer rows.Close()
result := make([]*mapobjectdb.MapObject, 0)
var currentObj *mapobjectdb.MapObject
var currentUID string
2019-03-21 17:07:09 +03:00
for rows.Next() {
var id string
2019-03-21 17:07:09 +03:00
var Type string
var mtime int64
var x, y, z int
var posx, posy, posz int
var key, value string
err = rows.Scan(&id, &Type, &mtime,
&x, &y, &z, &posx, &posy, &posz,
&key, &value,
)
if err != nil {
return nil, err
}
if currentUID == "" || currentUID != id {
2023-12-29 18:00:11 +03:00
pos := types.NewMapBlockCoords(posx, posy, posz)
2019-03-21 17:07:09 +03:00
mo := &mapobjectdb.MapObject{
MBPos: pos,
Type: Type,
X: x,
Y: y,
Z: z,
Mtime: mtime,
Attributes: make(map[string]string),
}
currentObj = mo
currentUID = id
2019-03-21 17:07:09 +03:00
result = append(result, currentObj)
}
currentObj.Attributes[key] = value
}
return result, nil
}
2023-12-29 18:00:11 +03:00
func (a *PostgresAccessor) RemoveMapData(pos *types.MapBlockCoords) error {
_, err := a.db.Exec(removeMapDataQuery, pos.X, pos.Y, pos.Z)
2019-03-21 17:07:09 +03:00
return err
}
func (a *PostgresAccessor) AddMapData(data *mapobjectdb.MapObject) error {
for k, v := range data.Attributes {
if !utf8.Valid([]byte(v)) {
// invalid utf8, skip insert into db
fields := logrus.Fields{
"type": data.Type,
"value": v,
"key": k,
}
log.WithFields(fields).Info("Migration completed")
return nil
}
}
uid := uuid.NewString()
_, err := a.db.Exec(addMapDataQuery,
uid,
2019-03-21 17:07:09 +03:00
data.X, data.Y, data.Z,
data.MBPos.X, data.MBPos.Y, data.MBPos.Z,
data.Type, data.Mtime)
if err != nil {
return err
}
for k, v := range data.Attributes {
_, err := a.db.Exec(addMapDataAttributeQuery, uid, k, v)
2019-03-21 17:07:09 +03:00
if err != nil {
return err
}
}
return nil
}