Improve and simplify RPC interface methods

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-20 01:20:19 +00:00
parent e10debba6c
commit 529c1028a3

View file

@ -26,48 +26,73 @@ func (r *rpc) Sql(c *fibre.Context, sql string, vars map[string]interface{}) (in
return db.Execute(c, sql, vars)
}
func (r *rpc) List(c *fibre.Context, class string) (interface{}, error) {
return db.Execute(c, "SELECT * FROM $class", map[string]interface{}{
"class": sql.NewTable(class),
})
}
func (r *rpc) Create(c *fibre.Context, class string, data map[string]interface{}) (interface{}, error) {
return db.Execute(c, "CREATE $class CONTENT $data RETURN AFTER", map[string]interface{}{
"class": sql.NewTable(class),
"data": data,
})
}
func (r *rpc) Select(c *fibre.Context, class string, thing interface{}) (interface{}, error) {
return db.Execute(c, "SELECT * FROM $thing", map[string]interface{}{
"thing": sql.NewThing(class, thing),
})
switch thing.(type) {
case *fibre.RPCNull:
return db.Execute(c, "SELECT * FROM $class", map[string]interface{}{
"class": sql.NewTable(class),
})
default:
return db.Execute(c, "SELECT * FROM $thing", map[string]interface{}{
"thing": sql.NewThing(class, thing),
})
}
}
func (r *rpc) Insert(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
return db.Execute(c, "INSERT $thing CONTENT $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing),
"data": data,
})
func (r *rpc) Create(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
switch thing.(type) {
case *fibre.RPCNull:
return db.Execute(c, "CREATE $class CONTENT $data RETURN AFTER", map[string]interface{}{
"class": sql.NewTable(class),
"data": data,
})
default:
return db.Execute(c, "CREATE $thing CONTENT $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing),
"data": data,
})
}
}
func (r *rpc) Upsert(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
return db.Execute(c, "UPSERT $thing CONTENT $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing),
"data": data,
})
func (r *rpc) Update(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
switch thing.(type) {
case *fibre.RPCNull:
return db.Execute(c, "UPDATE $class CONTENT $data RETURN AFTER", map[string]interface{}{
"class": sql.NewTable(class),
"data": data,
})
default:
return db.Execute(c, "UPDATE $thing CONTENT $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing),
"data": data,
})
}
}
func (r *rpc) Modify(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
return db.Execute(c, "MODIFY $thing DIFF $data RETURN DIFF", map[string]interface{}{
"thing": sql.NewThing(class, thing),
"data": data,
})
switch thing.(type) {
case *fibre.RPCNull:
return db.Execute(c, "UPDATE $class DIFF $data RETURN AFTER", map[string]interface{}{
"class": sql.NewTable(class),
"data": data,
})
default:
return db.Execute(c, "UPDATE $thing DIFF $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing),
"data": data,
})
}
}
func (r *rpc) Delete(c *fibre.Context, class string, thing interface{}) (interface{}, error) {
return db.Execute(c, "DELETE $thing", map[string]interface{}{
"thing": sql.NewThing(class, thing),
})
switch thing.(type) {
case *fibre.RPCNull:
return db.Execute(c, "DELETE $class", map[string]interface{}{
"class": sql.NewTable(class),
})
default:
return db.Execute(c, "DELETE $thing", map[string]interface{}{
"thing": sql.NewThing(class, thing),
})
}
}