Skip to content

Commit 2bb5325

Browse files
committed
api: add CRUD module support
This patch provides crud [1] methods as request objects to support CRUD API. The following methods are supported: * `insert` * `insert_object` * `insert_many` * `insert_object_many` * `get` * `update` * `delete` * `replace` * `replace_object` * `replace_many` * `replace_object_many` * `upsert` * `upsert_object` * `upsert_many` * `upsert_object_many` * `select` * `min` * `max` * `truncate` * `len` * `storage_info` * `count` * `stats` * `unflatten_rows` 1. https://github.com/tarantool/crud Closes #108
1 parent a22527a commit 2bb5325

28 files changed

+2393
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1212

1313
- Support pagination (#246)
1414
- A Makefile target to test with race detector (#218)
15+
- Support CRUD API (#108)
1516

1617
### Changed
1718

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ endif
2121

2222
.PHONY: clean
2323
clean:
24-
( cd ./queue; rm -rf .rocks )
24+
( rm -rf queue/.rocks crud/.rocks )
2525
rm -f $(COVERAGE_FILE)
2626

2727
.PHONY: deps
2828
deps: clean
2929
( cd ./queue/testdata; $(TTCTL) rocks install queue 1.2.1 )
30+
( cd ./crud; $(TTCTL) rocks install crud 0.14.1 )
3031

3132
.PHONY: datetime-timezones
3233
datetime-timezones:
@@ -99,6 +100,13 @@ test-settings:
99100
go clean -testcache
100101
go test -tags "$(TAGS)" ./settings/ -v -p 1
101102

103+
.PHONY: test-crud
104+
test-crud:
105+
@echo "Running tests in crud package"
106+
cd ./crud/ && tarantool -e "require('crud')"
107+
go clean -testcache
108+
go test -tags "$(TAGS)" ./crud/ -v -p 1
109+
102110
.PHONY: test-main
103111
test-main:
104112
@echo "Running tests in main package"

crud/common.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
type baseRequest struct {
10+
impl *tarantool.CallRequest
11+
}
12+
13+
func (req *baseRequest) initImpl(methodName string) {
14+
req.impl = tarantool.NewCall17Request(methodName)
15+
}
16+
17+
// Code returns IPROTO code for CRUD request.
18+
func (req *baseRequest) Code() int32 {
19+
return req.impl.Code()
20+
}
21+
22+
// Ctx returns a context of CRUD request.
23+
func (req *baseRequest) Ctx() context.Context {
24+
return req.impl.Ctx()
25+
}
26+
27+
// Async returns is CRUD request expects a response.
28+
func (req *baseRequest) Async() bool {
29+
return req.impl.Async()
30+
}
31+
32+
type spaceRequest struct {
33+
baseRequest
34+
space interface{}
35+
}
36+
37+
func (req *spaceRequest) setSpace(space interface{}) {
38+
req.space = space
39+
}

crud/count.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
// CountRequest helps you to create request
10+
// object to call `crud.count` for execution
11+
// by a Connection.
12+
type CountRequest struct {
13+
spaceRequest
14+
conditions interface{}
15+
opts interface{}
16+
}
17+
18+
// NewCountRequest returns a new empty CountRequest.
19+
func NewCountRequest(space interface{}) *SelectRequest {
20+
req := new(SelectRequest)
21+
req.initImpl("crud.count")
22+
req.setSpace(space)
23+
req.conditions = []interface{}{}
24+
req.opts = map[string]interface{}{}
25+
return req
26+
}
27+
28+
// Conditions sets the conditions for the CountRequest request.
29+
// Note: default value is nil.
30+
func (req *CountRequest) Conditions(conditions interface{}) *CountRequest {
31+
req.conditions = conditions
32+
return req
33+
}
34+
35+
// Opts sets the options for the CountRequest request.
36+
// Note: default value is nil.
37+
func (req *CountRequest) Opts(opts interface{}) *CountRequest {
38+
req.opts = opts
39+
return req
40+
}
41+
42+
// Body fills an encoder with the call request body.
43+
func (req *CountRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
44+
req.impl.Args([]interface{}{req.space, req.conditions, req.opts})
45+
return req.impl.Body(res, enc)
46+
}
47+
48+
// Context sets a passed context to CRUD request.
49+
func (req *CountRequest) Context(ctx context.Context) *CountRequest {
50+
req.impl = req.impl.Context(ctx)
51+
52+
return req
53+
}

crud/delete.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
// DeleteRequest helps you to create request
10+
// object to call `crud.delete` for execution
11+
// by a Connection.
12+
type DeleteRequest struct {
13+
spaceRequest
14+
key interface{}
15+
opts interface{}
16+
}
17+
18+
// NewDeleteRequest returns a new empty DeleteRequest.
19+
func NewDeleteRequest(space interface{}) *DeleteRequest {
20+
req := new(DeleteRequest)
21+
req.initImpl("crud.delete")
22+
req.setSpace(space)
23+
req.key = []interface{}{}
24+
req.opts = map[string]interface{}{}
25+
return req
26+
}
27+
28+
// Key sets the key for the DeleteRequest request.
29+
// Note: default value is nil.
30+
func (req *DeleteRequest) Key(key interface{}) *DeleteRequest {
31+
req.key = key
32+
return req
33+
}
34+
35+
// Opts sets the options for the DeleteRequest request.
36+
// Note: default value is nil.
37+
func (req *DeleteRequest) Opts(opts interface{}) *DeleteRequest {
38+
req.opts = opts
39+
return req
40+
}
41+
42+
// Body fills an encoder with the call request body.
43+
func (req *DeleteRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
44+
req.impl.Args([]interface{}{req.space, req.key, req.opts})
45+
return req.impl.Body(res, enc)
46+
}
47+
48+
// Context sets a passed context to CRUD request.
49+
func (req *DeleteRequest) Context(ctx context.Context) *DeleteRequest {
50+
req.impl = req.impl.Context(ctx)
51+
52+
return req
53+
}

crud/get.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
// GetRequest helps you to create request
10+
// object to call `crud.get` for execution
11+
// by a Connection.
12+
type GetRequest struct {
13+
spaceRequest
14+
key interface{}
15+
opts interface{}
16+
}
17+
18+
// NewGetRequest returns a new empty GetRequest.
19+
func NewGetRequest(space interface{}) *GetRequest {
20+
req := new(GetRequest)
21+
req.initImpl("crud.get")
22+
req.setSpace(space)
23+
req.key = []interface{}{}
24+
req.opts = map[string]interface{}{}
25+
return req
26+
}
27+
28+
// Key sets the key for the GetRequest request.
29+
// Note: default value is nil.
30+
func (req *GetRequest) Key(key interface{}) *GetRequest {
31+
req.key = key
32+
return req
33+
}
34+
35+
// Opts sets the options for the GetRequest request.
36+
// Note: default value is nil.
37+
func (req *GetRequest) Opts(opts interface{}) *GetRequest {
38+
req.opts = opts
39+
return req
40+
}
41+
42+
// Body fills an encoder with the call request body.
43+
func (req *GetRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
44+
req.impl.Args([]interface{}{req.space, req.key, req.opts})
45+
return req.impl.Body(res, enc)
46+
}
47+
48+
// Context sets a passed context to CRUD request.
49+
func (req *GetRequest) Context(ctx context.Context) *GetRequest {
50+
req.impl = req.impl.Context(ctx)
51+
52+
return req
53+
}

crud/insert.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
// InsertRequest helps you to create request
10+
// object to call `crud.insert` for execution
11+
// by a Connection.
12+
type InsertRequest struct {
13+
spaceRequest
14+
tuple interface{}
15+
opts interface{}
16+
}
17+
18+
// NewInsertRequest returns a new empty InsertRequest.
19+
func NewInsertRequest(space interface{}) *InsertRequest {
20+
req := new(InsertRequest)
21+
req.initImpl("crud.insert")
22+
req.setSpace(space)
23+
req.tuple = []interface{}{}
24+
req.opts = map[string]interface{}{}
25+
return req
26+
}
27+
28+
// Tuple sets the tuple for the InsertRequest request.
29+
// Note: default value is nil.
30+
func (req *InsertRequest) Tuple(tuple interface{}) *InsertRequest {
31+
req.tuple = tuple
32+
return req
33+
}
34+
35+
// Opts sets the options for the insert request.
36+
// Note: default value is nil.
37+
func (req *InsertRequest) Opts(opts interface{}) *InsertRequest {
38+
req.opts = opts
39+
return req
40+
}
41+
42+
// Body fills an encoder with the call request body.
43+
func (req *InsertRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
44+
req.impl.Args([]interface{}{req.space, req.tuple, req.opts})
45+
return req.impl.Body(res, enc)
46+
}
47+
48+
// Context sets a passed context to CRUD request.
49+
func (req *InsertRequest) Context(ctx context.Context) *InsertRequest {
50+
req.impl = req.impl.Context(ctx)
51+
52+
return req
53+
}
54+
55+
// InsertObjectRequest helps you to create request
56+
// object to call `crud.insert_object` for execution
57+
// by a Connection.
58+
type InsertObjectRequest struct {
59+
spaceRequest
60+
object interface{}
61+
opts interface{}
62+
}
63+
64+
// NewInsertObjectRequest returns a new empty InsertObjectRequest.
65+
func NewInsertObjectRequest(space interface{}) *InsertObjectRequest {
66+
req := new(InsertObjectRequest)
67+
req.initImpl("crud.insert_object")
68+
req.setSpace(space)
69+
req.object = map[string]interface{}{}
70+
req.opts = map[string]interface{}{}
71+
return req
72+
}
73+
74+
// Object sets the tuple for the InsertObjectRequest request.
75+
// Note: default value is nil.
76+
func (req *InsertObjectRequest) Object(object interface{}) *InsertObjectRequest {
77+
req.object = object
78+
return req
79+
}
80+
81+
// Opts sets the options for the InsertObjectRequest request.
82+
// Note: default value is nil.
83+
func (req *InsertObjectRequest) Opts(opts interface{}) *InsertObjectRequest {
84+
req.opts = opts
85+
return req
86+
}
87+
88+
// Body fills an encoder with the call request body.
89+
func (req *InsertObjectRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
90+
req.impl.Args([]interface{}{req.space, req.object, req.opts})
91+
return req.impl.Body(res, enc)
92+
}
93+
94+
// Context sets a passed context to CRUD request.
95+
func (req *InsertObjectRequest) Context(ctx context.Context) *InsertObjectRequest {
96+
req.impl = req.impl.Context(ctx)
97+
98+
return req
99+
}

0 commit comments

Comments
 (0)