Skip to content

Commit c642a48

Browse files
authored
Merge pull request #877 from dolthub/fulghum/set_config
Function: `set_config()`
2 parents bcd2766 + a68aa0f commit c642a48

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

server/functions/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func Init() {
106106
initRpad()
107107
initRtrim()
108108
initScale()
109+
initSetConfig()
109110
initSetVal()
110111
initShobjDescription()
111112
initSign()

server/functions/set_config.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package functions
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
21+
"github.com/dolthub/go-mysql-server/sql"
22+
23+
"github.com/dolthub/doltgresql/server/functions/framework"
24+
pgtypes "github.com/dolthub/doltgresql/server/types"
25+
)
26+
27+
func initSetConfig() {
28+
framework.RegisterFunction(set_config_text_text_boolean)
29+
}
30+
31+
// set_config_text_text_boolean implements the set_config() function
32+
// https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-SET
33+
var set_config_text_text_boolean = framework.Function3{
34+
Name: "set_config",
35+
Return: pgtypes.Text,
36+
Parameters: [3]pgtypes.DoltgresType{pgtypes.Text, pgtypes.Text, pgtypes.Bool},
37+
Callable: func(ctx *sql.Context, _ [4]pgtypes.DoltgresType, settingName any, newValue any, isLocal any) (any, error) {
38+
if settingName == nil {
39+
return nil, fmt.Errorf("NULL value not allowed for configuration setting name")
40+
}
41+
42+
// NULL is not supported for configuration values, and gets turned into the empty string
43+
if newValue == nil {
44+
newValue = ""
45+
}
46+
47+
if isLocal == true {
48+
// TODO: If isLocal is true, then the config setting should only persist for the current transaction
49+
return nil, fmt.Errorf("setting configuration values for the current transaction is not supported yet")
50+
}
51+
52+
// set_config can set system configuration or user configuration. System configuration settings are in top
53+
// level settings, while user configuration settings are namespaced.
54+
isUserConfig := strings.Contains(settingName.(string), ".")
55+
if isUserConfig {
56+
if err := ctx.SetUserVariable(ctx, settingName.(string), newValue.(string), pgtypes.Text); err != nil {
57+
return nil, err
58+
}
59+
} else {
60+
if err := ctx.SetSessionVariable(ctx, settingName.(string), newValue.(string)); err != nil {
61+
return nil, err
62+
}
63+
}
64+
65+
return newValue.(string), nil
66+
},
67+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package output
16+
17+
import (
18+
"testing"
19+
20+
"github.com/dolthub/go-mysql-server/sql"
21+
)
22+
23+
func Test_SetConfig(t *testing.T) {
24+
RunScripts(t, []ScriptTest{
25+
{
26+
Name: "set_config",
27+
Assertions: []ScriptTestAssertion{
28+
{
29+
// non-namespaced, non-system settings result in an error: "unrecognized configuration parameter"
30+
Query: "SELECT set_config('doesnotexist', '42', false);",
31+
ExpectedErr: true,
32+
},
33+
{
34+
Query: "SELECT set_config('', 'bar', false);",
35+
ExpectedErr: true,
36+
},
37+
{
38+
Query: "SELECT set_config(NULL, 'bar', false);",
39+
ExpectedErr: true,
40+
},
41+
{
42+
// Set a system config setting
43+
Query: "SELECT set_config('search_path', '123', false);",
44+
Expected: []sql.Row{{"123"}},
45+
},
46+
{
47+
Query: "SELECT current_setting('search_path');",
48+
Expected: []sql.Row{{"123"}},
49+
},
50+
{
51+
// Set a user config setting in a custom namespace
52+
Query: "SELECT set_config('mynamespace.var', 'bar', false);",
53+
Expected: []sql.Row{{"bar"}},
54+
},
55+
{
56+
Query: "SELECT current_setting('mynamespace.var');",
57+
Expected: []sql.Row{{"bar"}},
58+
},
59+
{
60+
// Only text values are supported
61+
Query: "SELECT set_config('myvars.boo', 3.14159, false);",
62+
ExpectedErr: true,
63+
},
64+
{
65+
// All settings must be text
66+
Query: "SELECT set_config('myvars.boo', 3.14159::text, false);",
67+
Expected: []sql.Row{{"3.14159"}},
68+
},
69+
{
70+
Query: "SELECT current_setting('myvars.boo');",
71+
Expected: []sql.Row{{"3.14159"}},
72+
},
73+
{
74+
// A NULL value is turned into the empty string
75+
Query: "SELECT set_config('myvars.nullval', NULL, false);",
76+
Expected: []sql.Row{{""}},
77+
},
78+
{
79+
Query: "SELECT current_setting('myvars.nullval');",
80+
Expected: []sql.Row{{""}},
81+
},
82+
},
83+
},
84+
})
85+
}

0 commit comments

Comments
 (0)