forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support session settings in a separate subpackage "settings" [1]. It allows to create a specific Select/Update 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/
- Loading branch information
1 parent
63ac35b
commit 9bf4566
Showing
9 changed files
with
1,014 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package settings | ||
|
||
const sessionSettingsSpace string = "_session_settings" | ||
|
||
// In Go and IPROTO_UPDATE count starts with 0. | ||
const sessionSettingValueField int = 1 | ||
|
||
const ( | ||
// ErrorMarshalingEnabled defines whether error objects | ||
// have a special structure. Added in Tarantool 2.4.1, dropped | ||
// in Tarantool 2.10.0 in favor of ErrorExtensionFeature protocol | ||
// feature. Default is `false`. | ||
ErrorMarshalingEnabled string = "error_marshaling_enabled" | ||
// SQLDefaultEngine defined default storage engine for | ||
// new SQL tables. Added in Tarantool 2.3.1. Default is `"memtx"`. | ||
SQLDefaultEngine string = "sql_default_engine" | ||
// SQLDeferForeignKeys defines whether foreign-key checks | ||
// can wait till commit. Added in Tarantool 2.3.1, dropped | ||
// in master commit 14618c4 (possible 2.10.5 or 2.11.0). Default is `false`. | ||
SQLDeferForeignKeys string = "sql_defer_foreign_keys" | ||
// SQLFullColumnNames defines whether full column names is displayed | ||
// in SQL result set metadata. Added in Tarantool 2.3.1. Default is `false`. | ||
SQLFullColumnNames string = "sql_full_column_names" | ||
// SQLFullMetadata defines whether SQL result set metadata will have | ||
// more than just name and type. Added in Tarantool 2.3.1. Default is `false`. | ||
SQLFullMetadata string = "sql_full_metadata" | ||
// SQLParserDebug defines whether to show parser steps for following | ||
// statements. Option has no effect unless Tarantool was built with | ||
// `-DCMAKE_BUILD_TYPE=Debug`. Added in Tarantool 2.3.1. Default is `false`. | ||
SQLParserDebug string = "sql_parser_debug" | ||
// SQLParserDebug defines whether a triggered statement can activate | ||
// a trigger. Added in Tarantool 2.3.1. Default is `true`. | ||
SQLRecursiveTriggers string = "sql_recursive_triggers" | ||
// SQLReverseUnorderedSelects defines whether result rows are usually | ||
// in reverse order if there is no ORDER BY clause. Added in Tarantool 2.3.1. | ||
// Default is `false`. | ||
SQLReverseUnorderedSelects string = "sql_reverse_unordered_selects" | ||
// SQLSelectDebug defines whether to show execution steps during SELECT. | ||
// Option has no effect unless Tarantool was built with `-DCMAKE_BUILD_TYPE=Debug`. | ||
// Added in Tarantool 2.3.1. Default is `false`. | ||
SQLSelectDebug string = "sql_select_debug" | ||
// SQLVDBEDebug defines whether VDBE debug mode is enabled. | ||
// Option has no effect unless Tarantool was built with `-DCMAKE_BUILD_TYPE=Debug`. | ||
// Added in Tarantool 2.3.1. Default is `false`. | ||
SQLVDBEDebug string = "sql_vdbe_debug" | ||
) | ||
|
||
const selectAllLimit uint32 = 1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ExampleSQLFullColumnNames() { | ||
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.SetSQLFullColumnNames(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.SetSQLFullColumnNames(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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package settings | ||
|
||
import ( | ||
"github.com/tarantool/go-tarantool" | ||
) | ||
|
||
func newSetRequest(setting string, value interface{}) *tarantool.UpdateRequest { | ||
return tarantool.NewUpdateRequest(sessionSettingsSpace). | ||
Key(tarantool.StringKey{S: setting}). | ||
Operations(tarantool.NewOperations().Assign(sessionSettingValueField, value)) | ||
} | ||
|
||
func newGetRequest(setting string) *tarantool.SelectRequest { | ||
return tarantool.NewSelectRequest(sessionSettingsSpace). | ||
Key(tarantool.StringKey{S: setting}). | ||
Limit(1) | ||
} | ||
|
||
// SetErrorMarshalingEnabled creates a request to | ||
// update current session ErrorMarshalingEnabled setting. | ||
// Added in 1.10.0. | ||
func SetErrorMarshalingEnabled(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(ErrorMarshalingEnabled, value) | ||
} | ||
|
||
// GetErrorMarshalingEnabled creates a request to get | ||
// current session ErrorMarshalingEnabled setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetErrorMarshalingEnabled() *tarantool.SelectRequest { | ||
return newGetRequest(ErrorMarshalingEnabled) | ||
} | ||
|
||
// SetSQLDefaultEngine creates a request to | ||
// update current session SQLDefaultEngine setting. | ||
// Added in 1.10.0. | ||
func SetSQLDefaultEngine(value string) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLDefaultEngine, value) | ||
} | ||
|
||
// GetSQLDefaultEngine creates a request to get | ||
// current session SQLDefaultEngine setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLDefaultEngine() *tarantool.SelectRequest { | ||
return newGetRequest(SQLDefaultEngine) | ||
} | ||
|
||
// SetSQLDeferForeignKeys creates a request to | ||
// update current session SQLDeferForeignKeys setting. | ||
// Added in 1.10.0. | ||
func SetSQLDeferForeignKeys(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLDeferForeignKeys, value) | ||
} | ||
|
||
// GetSQLDeferForeignKeys creates a request to get | ||
// current session SQLDeferForeignKeys setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLDeferForeignKeys() *tarantool.SelectRequest { | ||
return newGetRequest(SQLDeferForeignKeys) | ||
} | ||
|
||
// SetSQLFullColumnNames creates a request to | ||
// update current session SQLFullColumnNames setting. | ||
// Added in 1.10.0. | ||
func SetSQLFullColumnNames(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLFullColumnNames, value) | ||
} | ||
|
||
// GetSQLFullColumnNames creates a request to get | ||
// current session SQLFullColumnNames setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLFullColumnNames() *tarantool.SelectRequest { | ||
return newGetRequest(SQLFullColumnNames) | ||
} | ||
|
||
// SetSQLFullMetadata creates a request to | ||
// update current session SQLFullMetadata setting. | ||
// Added in 1.10.0. | ||
func SetSQLFullMetadata(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLFullMetadata, value) | ||
} | ||
|
||
// GetSQLFullMetadata creates a request to get | ||
// current session SQLFullMetadata setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLFullMetadata() *tarantool.SelectRequest { | ||
return newGetRequest(SQLFullMetadata) | ||
} | ||
|
||
// SetSQLParserDebug creates a request to | ||
// update current session SQLParserDebug setting. | ||
// Added in 1.10.0. | ||
func SetSQLParserDebug(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLParserDebug, value) | ||
} | ||
|
||
// GetSQLParserDebug creates a request to get | ||
// current session SQLParserDebug setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLParserDebug() *tarantool.SelectRequest { | ||
return newGetRequest(SQLParserDebug) | ||
} | ||
|
||
// SetSQLRecursiveTriggers creates a request to | ||
// update current session SQLRecursiveTriggers setting. | ||
// Added in 1.10.0. | ||
func SetSQLRecursiveTriggers(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLRecursiveTriggers, value) | ||
} | ||
|
||
// GetSQLRecursiveTriggers creates a request to get | ||
// current session SQLRecursiveTriggers setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLRecursiveTriggers() *tarantool.SelectRequest { | ||
return newGetRequest(SQLRecursiveTriggers) | ||
} | ||
|
||
// SetSQLReverseUnorderedSelects creates a request to | ||
// update current session SQLReverseUnorderedSelects setting. | ||
// Added in 1.10.0. | ||
func SetSQLReverseUnorderedSelects(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLReverseUnorderedSelects, value) | ||
} | ||
|
||
// GetSQLReverseUnorderedSelects creates a request to get | ||
// current session SQLReverseUnorderedSelects setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLReverseUnorderedSelects() *tarantool.SelectRequest { | ||
return newGetRequest(SQLReverseUnorderedSelects) | ||
} | ||
|
||
// SetSQLSelectDebug creates a request to | ||
// update current session SQLSelectDebug setting. | ||
// Added in 1.10.0. | ||
func SetSQLSelectDebug(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLSelectDebug, value) | ||
} | ||
|
||
// GetSQLSelectDebug creates a request to get | ||
// current session SQLSelectDebug setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLSelectDebug() *tarantool.SelectRequest { | ||
return newGetRequest(SQLSelectDebug) | ||
} | ||
|
||
// SetSQLVDBEDebug creates a request to | ||
// update current session SQLVDBEDebug setting. | ||
// Added in 1.10.0. | ||
func SetSQLVDBEDebug(value bool) *tarantool.UpdateRequest { | ||
return newSetRequest(SQLVDBEDebug, value) | ||
} | ||
|
||
// GetSQLVDBEDebug creates a request to get | ||
// current session SQLVDBEDebug setting in tuple format. | ||
// Added in 1.10.0. | ||
func GetSQLVDBEDebug() *tarantool.SelectRequest { | ||
return newGetRequest(SQLVDBEDebug) | ||
} | ||
|
||
// GetSession creates a request to get all | ||
// current session settings in tuple format. | ||
// Added in 1.10.0. | ||
func GetSession() *tarantool.SelectRequest { | ||
return tarantool.NewSelectRequest(sessionSettingsSpace). | ||
Limit(selectAllLimit) | ||
} |
Oops, something went wrong.