owner ID not null

* Feature owner ID is never null.
* For 'extra geometry', system user ID=0 is used.
This commit is contained in:
zegkljan 2022-02-03 09:22:11 +01:00
parent 7471c89143
commit caeb5c9198
3 changed files with 6 additions and 17 deletions

View File

@ -21,7 +21,7 @@ type Feature struct {
// ID is an ID of the feature. // ID is an ID of the feature.
// When the feature is submitted by a client for creation (i.e. in Update.Create) it is considered a 'local' ID which must be unique across all submitted features. // When the feature is submitted by a client for creation (i.e. in Update.Create) it is considered a 'local' ID which must be unique across all submitted features.
ID FeatureID `json:"id"` ID FeatureID `json:"id"`
OwnerID *UserID `json:"owner_id"` OwnerID UserID `json:"owner_id"`
Name string `json:"name"` Name string `json:"name"`
Properties map[string]interface{} `json:"properties"` Properties map[string]interface{} `json:"properties"`
Geometry geojson.Geometry `json:"geometry"` Geometry geojson.Geometry `json:"geometry"`

View File

@ -8,7 +8,7 @@ INSERT INTO users(id, name) VALUES(0, 'system');
DROP TABLE IF EXISTS features; DROP TABLE IF EXISTS features;
CREATE TABLE IF NOT EXISTS features ( CREATE TABLE IF NOT EXISTS features (
id integer PRIMARY KEY AUTOINCREMENT, id integer PRIMARY KEY AUTOINCREMENT,
owner_id integer REFERENCES users(id) ON DELETE CASCADE, owner_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name text NOT NULL, name text NOT NULL,
properties text NOT NULL, properties text NOT NULL,
geom text NOT NULL geom text NOT NULL

View File

@ -283,10 +283,7 @@ func (s *Server) getFeatures(conn *sqlite.Conn) ([]models.Feature, error) {
id := stmt.ColumnInt64(0) id := stmt.ColumnInt64(0)
var ownerID *int64 ownerID := stmt.ColumnInt64(1)
if stmt.ColumnType(1) != sqlite.SQLITE_NULL {
ownerID = ptrInt64(stmt.ColumnInt64(1))
}
name := stmt.ColumnText(2) name := stmt.ColumnText(2)
@ -313,7 +310,7 @@ func (s *Server) getFeatures(conn *sqlite.Conn) ([]models.Feature, error) {
feature := models.Feature{ feature := models.Feature{
ID: models.FeatureID(id), ID: models.FeatureID(id),
OwnerID: (*models.UserID)(ownerID), OwnerID: models.UserID(ownerID),
Name: name, Name: name,
Properties: properties, Properties: properties,
Geometry: geom, Geometry: geom,
@ -350,11 +347,7 @@ func (s *Server) addFeatures(conn *sqlite.Conn, features []models.Feature) (map[
return nil, fmt.Errorf("failed to clear bindings of prepared statement: %w", err) return nil, fmt.Errorf("failed to clear bindings of prepared statement: %w", err)
} }
if feature.OwnerID == nil { stmt.BindInt64(1, int64(feature.OwnerID))
stmt.BindNull(1)
} else {
stmt.BindInt64(1, int64(*feature.OwnerID))
}
stmt.BindText(2, feature.Name) stmt.BindText(2, feature.Name)
@ -473,11 +466,7 @@ func (s *Server) updateFeatures(conn *sqlite.Conn, features []models.Feature) er
return fmt.Errorf("failed to clear bindings of prepared statement: %w", err) return fmt.Errorf("failed to clear bindings of prepared statement: %w", err)
} }
if feature.OwnerID == nil { stmt.BindInt64(1, int64(feature.OwnerID))
stmt.BindNull(1)
} else {
stmt.BindInt64(1, int64(*feature.OwnerID))
}
stmt.BindText(2, feature.Name) stmt.BindText(2, feature.Name)