Skip to content

Commit

Permalink
api: support session settings
Browse files Browse the repository at this point in the history
Support session settings in a separate subpackage "settings" [1]. It
allows to create a specific requests to get or set session
settings. Settings are independent for each connection.

1. https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/_session_settings/

Closes #215
  • Loading branch information
DifferentialOrange committed Dec 14, 2022
1 parent 3a1e82b commit 404772c
Show file tree
Hide file tree
Showing 11 changed files with 1,138 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Support errors extended information (#209)
- Error type support in MessagePack (#209)
- Event subscription support (#119)
- Session settings support (#115)

### Changed

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ test-uuid:
go clean -testcache
go test -tags "$(TAGS)" ./uuid/ -v -p 1

.PHONY: test-settings
test-settings:
@echo "Running tests in settings package"
go clean -testcache
go test -tags "$(TAGS)" ./settings/ -v -p 1

.PHONY: test-main
test-main:
@echo "Running tests in main package"
Expand Down
15 changes: 15 additions & 0 deletions settings/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Do not set listen for now so connector won't be
-- able to send requests until everything is configured.
box.cfg{
work_dir = os.getenv("TEST_TNT_WORK_DIR"),
}

box.schema.user.create('test', { password = 'test' , if_not_exists = true })
box.schema.user.grant('test', 'execute', 'universe', nil, { if_not_exists = true })
box.schema.user.grant('test', 'create,read,write,drop,alter', 'space', nil, { if_not_exists = true })
box.schema.user.grant('test', 'create', 'sequence', nil, { if_not_exists = true })

-- Set listen only when every other thing is configured.
box.cfg{
listen = os.getenv("TEST_TNT_LISTEN"),
}
21 changes: 21 additions & 0 deletions settings/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package settings

const sessionSettingsSpace string = "_session_settings"

// In Go and IPROTO_UPDATE count starts with 0.
const sessionSettingValueField int = 1

const (
errorMarshalingEnabled string = "error_marshaling_enabled"
sqlDefaultEngine string = "sql_default_engine"
sqlDeferForeignKeys string = "sql_defer_foreign_keys"
sqlFullColumnNames string = "sql_full_column_names"
sqlFullMetadata string = "sql_full_metadata"
sqlParserDebug string = "sql_parser_debug"
sqlRecursiveTriggers string = "sql_recursive_triggers"
sqlReverseUnorderedSelects string = "sql_reverse_unordered_selects"
sqlSelectDebug string = "sql_select_debug"
sqlVDBEDebug string = "sql_vdbe_debug"
)

const selectAllLimit uint32 = 1000
78 changes: 78 additions & 0 deletions settings/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package settings_test

import (
"fmt"

"github.com/tarantool/go-tarantool"
"github.com/tarantool/go-tarantool/settings"
"github.com/tarantool/go-tarantool/test_helpers"
)

func example_connect(opts tarantool.Opts) *tarantool.Connection {
conn, err := tarantool.Connect(server, opts)
if err != nil {
panic("Connection is not established: " + err.Error())
}
return conn
}

func Example_sqlFullColumnNames() {
var resp *tarantool.Response
var err error
var isLess bool

conn := example_connect(opts)
defer conn.Close()

// Tarantool supports session settings since version 2.3.1
isLess, err = test_helpers.IsTarantoolVersionLess(2, 3, 1)
if err != nil || isLess {
return
}

// Create a space.
_, err = conn.Execute("CREATE TABLE example(id INT PRIMARY KEY, x INT);", []interface{}{})
if err != nil {
fmt.Printf("error in create table: %v\n", err)
return
}

// Insert some tuple into space.
_, err = conn.Execute("INSERT INTO example VALUES (1, 1);", []interface{}{})
if err != nil {
fmt.Printf("error on insert: %v\n", err)
return
}

// Enable showing full column names in SQL responses.
_, err = conn.Do(settings.NewSQLFullColumnNamesSetRequest(true)).Get()
if err != nil {
fmt.Printf("error on setting setup: %v\n", err)
return
}

// Get some data with SQL query.
resp, err = conn.Execute("SELECT x FROM example WHERE id = 1;", []interface{}{})
if err != nil {
fmt.Printf("error on select: %v\n", err)
return
}
// Show response metadata.
fmt.Printf("full column name: %v\n", resp.MetaData[0].FieldName)

// Disable showing full column names in SQL responses.
_, err = conn.Do(settings.NewSQLFullColumnNamesSetRequest(false)).Get()
if err != nil {
fmt.Printf("error on setting setup: %v\n", err)
return
}

// Get some data with SQL query.
resp, err = conn.Execute("SELECT x FROM example WHERE id = 1;", []interface{}{})
if err != nil {
fmt.Printf("error on select: %v\n", err)
return
}
// Show response metadata.
fmt.Printf("short column name: %v\n", resp.MetaData[0].FieldName)
}
10 changes: 10 additions & 0 deletions settings/msgpack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !go_tarantool_msgpack_v5
// +build !go_tarantool_msgpack_v5

package settings

import (
"gopkg.in/vmihailenco/msgpack.v2"
)

type encoder = msgpack.Encoder
13 changes: 13 additions & 0 deletions settings/msgpack_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !go_tarantool_msgpack_v5
// +build !go_tarantool_msgpack_v5

package settings_test

import (
"github.com/tarantool/go-tarantool"
)

func toBoxError(i interface{}) (v tarantool.BoxError, ok bool) {
v, ok = i.(tarantool.BoxError)
return
}
10 changes: 10 additions & 0 deletions settings/msgpack_v5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build go_tarantool_msgpack_v5
// +build go_tarantool_msgpack_v5

package settings

import (
"github.com/vmihailenco/msgpack/v5"
)

type encoder = msgpack.Encoder
16 changes: 16 additions & 0 deletions settings/msgpack_v5_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build go_tarantool_msgpack_v5
// +build go_tarantool_msgpack_v5

package settings_test

import (
"github.com/tarantool/go-tarantool"
)

func toBoxError(i interface{}) (v tarantool.BoxError, ok bool) {
var ptr *tarantool.BoxError
if ptr, ok = i.(*tarantool.BoxError); ok {
v = *ptr
}
return
}
Loading

0 comments on commit 404772c

Please sign in to comment.