Add batch support to RPC endpoint queries

This commit is contained in:
Tobie Morgan Hitchcock 2017-11-16 20:46:02 +00:00
parent d7469c30d5
commit d5a947bf53

View file

@ -35,11 +35,15 @@ func (r *rpc) Query(c *fibre.Context, sql string, vars map[string]interface{}) (
} }
func (r *rpc) Select(c *fibre.Context, class string, thing interface{}) (interface{}, error) { func (r *rpc) Select(c *fibre.Context, class string, thing interface{}) (interface{}, error) {
switch thing.(type) { switch thing := thing.(type) {
case *fibre.RPCNull: case *fibre.RPCNull:
return db.Execute(c, "SELECT * FROM $class", map[string]interface{}{ return db.Execute(c, "SELECT * FROM $class", map[string]interface{}{
"class": sql.NewTable(class), "class": sql.NewTable(class),
}) })
case []interface{}:
return db.Execute(c, "SELECT * FROM $batch", map[string]interface{}{
"batch": sql.NewBatch(class, thing),
})
default: default:
return db.Execute(c, "SELECT * FROM $thing", map[string]interface{}{ return db.Execute(c, "SELECT * FROM $thing", map[string]interface{}{
"thing": sql.NewThing(class, thing), "thing": sql.NewThing(class, thing),
@ -48,12 +52,17 @@ func (r *rpc) Select(c *fibre.Context, class string, thing interface{}) (interfa
} }
func (r *rpc) Create(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) { func (r *rpc) Create(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
switch thing.(type) { switch thing := thing.(type) {
case *fibre.RPCNull: case *fibre.RPCNull:
return db.Execute(c, "CREATE $class CONTENT $data RETURN AFTER", map[string]interface{}{ return db.Execute(c, "CREATE $class CONTENT $data RETURN AFTER", map[string]interface{}{
"class": sql.NewTable(class), "class": sql.NewTable(class),
"data": data, "data": data,
}) })
case []interface{}:
return db.Execute(c, "CREATE $batch CONTENT $data RETURN AFTER", map[string]interface{}{
"batch": sql.NewBatch(class, thing),
"data": data,
})
default: default:
return db.Execute(c, "CREATE $thing CONTENT $data RETURN AFTER", map[string]interface{}{ return db.Execute(c, "CREATE $thing CONTENT $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing), "thing": sql.NewThing(class, thing),
@ -63,12 +72,17 @@ func (r *rpc) Create(c *fibre.Context, class string, thing interface{}, data map
} }
func (r *rpc) Update(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) { func (r *rpc) Update(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
switch thing.(type) { switch thing := thing.(type) {
case *fibre.RPCNull: case *fibre.RPCNull:
return db.Execute(c, "UPDATE $class CONTENT $data RETURN AFTER", map[string]interface{}{ return db.Execute(c, "UPDATE $class CONTENT $data RETURN AFTER", map[string]interface{}{
"class": sql.NewTable(class), "class": sql.NewTable(class),
"data": data, "data": data,
}) })
case []interface{}:
return db.Execute(c, "UPDATE $batch CONTENT $data RETURN AFTER", map[string]interface{}{
"batch": sql.NewBatch(class, thing),
"data": data,
})
default: default:
return db.Execute(c, "UPDATE $thing CONTENT $data RETURN AFTER", map[string]interface{}{ return db.Execute(c, "UPDATE $thing CONTENT $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing), "thing": sql.NewThing(class, thing),
@ -78,12 +92,17 @@ func (r *rpc) Update(c *fibre.Context, class string, thing interface{}, data map
} }
func (r *rpc) Change(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) { func (r *rpc) Change(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
switch thing.(type) { switch thing := thing.(type) {
case *fibre.RPCNull: case *fibre.RPCNull:
return db.Execute(c, "UPDATE $class MERGE $data RETURN AFTER", map[string]interface{}{ return db.Execute(c, "UPDATE $class MERGE $data RETURN AFTER", map[string]interface{}{
"class": sql.NewTable(class), "class": sql.NewTable(class),
"data": data, "data": data,
}) })
case []interface{}:
return db.Execute(c, "UPDATE $batch MERGE $data RETURN AFTER", map[string]interface{}{
"batch": sql.NewBatch(class, thing),
"data": data,
})
default: default:
return db.Execute(c, "UPDATE $thing MERGE $data RETURN AFTER", map[string]interface{}{ return db.Execute(c, "UPDATE $thing MERGE $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing), "thing": sql.NewThing(class, thing),
@ -93,12 +112,17 @@ func (r *rpc) Change(c *fibre.Context, class string, thing interface{}, data map
} }
func (r *rpc) Modify(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) { func (r *rpc) Modify(c *fibre.Context, class string, thing interface{}, data map[string]interface{}) (interface{}, error) {
switch thing.(type) { switch thing := thing.(type) {
case *fibre.RPCNull: case *fibre.RPCNull:
return db.Execute(c, "UPDATE $class DIFF $data RETURN AFTER", map[string]interface{}{ return db.Execute(c, "UPDATE $class DIFF $data RETURN AFTER", map[string]interface{}{
"class": sql.NewTable(class), "class": sql.NewTable(class),
"data": data, "data": data,
}) })
case []interface{}:
return db.Execute(c, "UPDATE $batch DIFF $data RETURN AFTER", map[string]interface{}{
"batch": sql.NewBatch(class, thing),
"data": data,
})
default: default:
return db.Execute(c, "UPDATE $thing DIFF $data RETURN AFTER", map[string]interface{}{ return db.Execute(c, "UPDATE $thing DIFF $data RETURN AFTER", map[string]interface{}{
"thing": sql.NewThing(class, thing), "thing": sql.NewThing(class, thing),
@ -108,11 +132,15 @@ func (r *rpc) Modify(c *fibre.Context, class string, thing interface{}, data map
} }
func (r *rpc) Delete(c *fibre.Context, class string, thing interface{}) (interface{}, error) { func (r *rpc) Delete(c *fibre.Context, class string, thing interface{}) (interface{}, error) {
switch thing.(type) { switch thing := thing.(type) {
case *fibre.RPCNull: case *fibre.RPCNull:
return db.Execute(c, "DELETE $class", map[string]interface{}{ return db.Execute(c, "DELETE $class", map[string]interface{}{
"class": sql.NewTable(class), "class": sql.NewTable(class),
}) })
case []interface{}:
return db.Execute(c, "DELETE $batch", map[string]interface{}{
"batch": sql.NewBatch(class, thing),
})
default: default:
return db.Execute(c, "DELETE $thing", map[string]interface{}{ return db.Execute(c, "DELETE $thing", map[string]interface{}{
"thing": sql.NewThing(class, thing), "thing": sql.NewThing(class, thing),