2019-01-21 17:13:33 +03:00
|
|
|
package mapobjectdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mapserver/coords"
|
|
|
|
)
|
|
|
|
|
2019-01-25 12:39:17 +03:00
|
|
|
const getMapDataPosQuery = `
|
|
|
|
select o.id, o.type, o.mtime,
|
|
|
|
o.x, o.y, o.z,
|
|
|
|
o.posx, o.posy, o.posz,
|
|
|
|
oa.key, oa.value
|
|
|
|
from objects o
|
|
|
|
left join object_attributes oa on o.id = oa.objectid
|
|
|
|
where o.type = ?
|
|
|
|
and o.posx >= ? and o.posy >= ? and o.posz >= ?
|
|
|
|
and o.posx <= ? and o.posy <= ? and o.posz <= ?
|
|
|
|
order by o.id
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
func (db *Sqlite3Accessor) GetMapData(q SearchQuery) ([]*MapObject, error) {
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
result := make([]*MapObject, 0)
|
|
|
|
var currentObj *MapObject
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var id int64
|
|
|
|
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 currentObj == nil {
|
|
|
|
//TODO
|
|
|
|
} else {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2019-01-21 17:13:33 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:05:17 +03:00
|
|
|
const removeMapDataQuery = `
|
|
|
|
delete from objects where posx = ? and posy = ? and posz = ?
|
|
|
|
`
|
|
|
|
|
2019-01-24 10:26:28 +03:00
|
|
|
func (db *Sqlite3Accessor) RemoveMapData(pos *coords.MapBlockCoords) error {
|
2019-01-23 16:05:17 +03:00
|
|
|
_, err := db.db.Exec(removeMapDataQuery, pos.X, pos.Y, pos.Z)
|
|
|
|
return err
|
2019-01-21 17:13:33 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:05:17 +03:00
|
|
|
const addMapDataQuery = `
|
|
|
|
insert into
|
|
|
|
objects(x,y,z,posx,posy,posz,type,mtime)
|
|
|
|
values(?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
`
|
|
|
|
|
|
|
|
const addMapDataAttributeQuery = `
|
|
|
|
insert into
|
|
|
|
object_attributes(objectid, key, value)
|
|
|
|
values(?, ?, ?)
|
|
|
|
`
|
|
|
|
|
2019-01-24 10:26:28 +03:00
|
|
|
func (db *Sqlite3Accessor) AddMapData(data *MapObject) error {
|
2019-01-23 16:15:22 +03:00
|
|
|
res, err := db.db.Exec(addMapDataQuery,
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:15:22 +03:00
|
|
|
id, err := res.LastInsertId()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-23 16:05:17 +03:00
|
|
|
|
|
|
|
for k, v := range data.Attributes {
|
2019-01-23 16:15:22 +03:00
|
|
|
//TODO: batch insert
|
2019-01-23 16:05:17 +03:00
|
|
|
_, err := db.db.Exec(addMapDataAttributeQuery, id, 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
|
|
|
|
}
|