2019-02-05 15:25:01 +03:00
|
|
|
package sqlite
|
2019-01-21 17:13:33 +03:00
|
|
|
|
|
|
|
import (
|
2019-04-14 22:13:59 +03:00
|
|
|
"database/sql"
|
2019-02-05 15:25:01 +03:00
|
|
|
"mapserver/mapobjectdb"
|
2023-12-29 18:00:11 +03:00
|
|
|
"mapserver/types"
|
2020-06-23 08:44:04 +03:00
|
|
|
"unicode/utf8"
|
2022-01-17 11:06:07 +03:00
|
|
|
|
2023-12-27 12:35:24 +03:00
|
|
|
"github.com/google/uuid"
|
2022-01-17 11:06:07 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-21 17:13:33 +03:00
|
|
|
)
|
|
|
|
|
2019-02-22 20:30:38 +03:00
|
|
|
func (db *Sqlite3Accessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectdb.MapObject, error) {
|
2019-04-14 22:13:59 +03:00
|
|
|
|
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if q.AttributeLike == nil {
|
|
|
|
//plain pos search
|
|
|
|
rows, err = db.db.Query(getMapDataPosQuery,
|
|
|
|
q.Type,
|
|
|
|
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
|
|
|
|
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
|
2022-01-17 11:06:07 +03:00
|
|
|
*q.Limit,
|
2019-04-14 22:13:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
} else {
|
2022-01-17 11:06:07 +03:00
|
|
|
//attribute like search
|
|
|
|
rows, err = db.db.Query(getMapDataWithAttributeLikePosQuery,
|
|
|
|
q.AttributeLike.Key, q.AttributeLike.Value,
|
|
|
|
q.Type,
|
|
|
|
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
|
|
|
|
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
|
|
|
|
*q.Limit,
|
|
|
|
)
|
2019-04-14 22:13:59 +03:00
|
|
|
}
|
2019-01-25 12:39:17 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
2019-02-05 15:25:01 +03:00
|
|
|
result := make([]*mapobjectdb.MapObject, 0)
|
|
|
|
var currentObj *mapobjectdb.MapObject
|
2023-12-27 12:35:24 +03:00
|
|
|
var currentUID string
|
2019-01-25 12:39:17 +03:00
|
|
|
|
|
|
|
for rows.Next() {
|
2023-12-27 12:35:24 +03:00
|
|
|
var uid string
|
2019-01-25 12:39:17 +03:00
|
|
|
var Type string
|
|
|
|
var mtime int64
|
|
|
|
var x, y, z int
|
|
|
|
var posx, posy, posz int
|
|
|
|
var key, value string
|
|
|
|
|
2023-12-27 12:35:24 +03:00
|
|
|
err = rows.Scan(&uid, &Type, &mtime,
|
2019-01-25 12:39:17 +03:00
|
|
|
&x, &y, &z, &posx, &posy, &posz,
|
|
|
|
&key, &value,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-27 12:35:24 +03:00
|
|
|
if currentUID == "" || currentUID != uid {
|
2023-12-29 18:00:11 +03:00
|
|
|
pos := types.NewMapBlockCoords(posx, posy, posz)
|
2019-02-06 20:28:24 +03:00
|
|
|
mo := &mapobjectdb.MapObject{
|
|
|
|
MBPos: pos,
|
|
|
|
Type: Type,
|
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
Z: z,
|
|
|
|
Mtime: mtime,
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2019-01-25 13:02:36 +03:00
|
|
|
|
|
|
|
currentObj = mo
|
2023-12-27 12:35:24 +03:00
|
|
|
currentUID = uid
|
2019-01-25 13:02:36 +03:00
|
|
|
|
|
|
|
result = append(result, currentObj)
|
2019-01-25 12:39:17 +03:00
|
|
|
}
|
|
|
|
|
2019-01-25 13:02:36 +03:00
|
|
|
currentObj.Attributes[key] = value
|
2019-01-25 12:39:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2019-01-21 17:13:33 +03:00
|
|
|
}
|
|
|
|
|
2023-12-29 18:00:11 +03:00
|
|
|
func (a *Sqlite3Accessor) RemoveMapData(pos *types.MapBlockCoords) error {
|
2023-12-27 12:35:24 +03:00
|
|
|
_, err := a.db.Exec(removeMapDataAttributesQuery, pos.X, pos.Y, pos.Z)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = a.db.Exec(removeMapDataQuery, pos.X, pos.Y, pos.Z)
|
2019-01-23 16:05:17 +03:00
|
|
|
return err
|
2019-01-21 17:13:33 +03:00
|
|
|
}
|
|
|
|
|
2019-02-05 15:25:01 +03:00
|
|
|
func (db *Sqlite3Accessor) AddMapData(data *mapobjectdb.MapObject) error {
|
2020-06-23 08:44:04 +03:00
|
|
|
|
|
|
|
for k, v := range data.Attributes {
|
|
|
|
if !utf8.Valid([]byte(v)) {
|
|
|
|
// invalid utf8, skip insert into db
|
|
|
|
fields := logrus.Fields{
|
2022-01-17 11:06:07 +03:00
|
|
|
"type": data.Type,
|
2020-06-23 08:44:04 +03:00
|
|
|
"value": v,
|
2022-01-17 11:06:07 +03:00
|
|
|
"key": k,
|
2020-06-23 08:44:04 +03:00
|
|
|
}
|
|
|
|
log.WithFields(fields).Info("Migration completed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 12:35:24 +03:00
|
|
|
uid := uuid.NewString()
|
|
|
|
_, err := db.db.Exec(addMapDataQuery,
|
|
|
|
uid,
|
2019-01-23 18:06:56 +03:00
|
|
|
data.X, data.Y, data.Z,
|
|
|
|
data.MBPos.X, data.MBPos.Y, data.MBPos.Z,
|
|
|
|
data.Type, data.Mtime)
|
2019-01-23 16:05:17 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range data.Attributes {
|
2023-12-27 12:35:24 +03:00
|
|
|
_, err := db.db.Exec(addMapDataAttributeQuery, uid, k, v)
|
2019-01-23 18:06:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-23 16:05:17 +03:00
|
|
|
}
|
2019-01-23 16:23:24 +03:00
|
|
|
|
2019-01-21 17:13:33 +03:00
|
|
|
return nil
|
|
|
|
}
|