diff --git a/go.mod b/go.mod index 722de73..7560625 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 - github.com/kwilteam/kuneiform-grammar-go v0.4.1-0.20230807191258-143bb3c13545 + github.com/kwilteam/kuneiform-grammar-go v0.4.1-0.20230807195420-c5c1c840c665 github.com/kwilteam/kwil-db v0.4.1-0.20230721231103-a25e503b79a8 github.com/spf13/cobra v1.7.0 github.com/stretchr/testify v1.8.3 diff --git a/go.sum b/go.sum index 8f49d4a..d510623 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/kwilteam/action-grammar-go v0.0.0-20230626155541-72265621f427 h1:MYJuuqYXqJN7m4CwPrYPdsOCy48mtMTH4u8zXRySKtg= github.com/kwilteam/action-grammar-go v0.0.0-20230626155541-72265621f427/go.mod h1:2KYz1ittMk7BbKRJSnSnRSysrgDUUtNB1Ha47ZMdc6M= -github.com/kwilteam/kuneiform-grammar-go v0.4.1-0.20230807191258-143bb3c13545 h1:5oumiJQJY1Mhc/H2E97wNjqYj4aUdZo4Y2AoOdwhH2M= -github.com/kwilteam/kuneiform-grammar-go v0.4.1-0.20230807191258-143bb3c13545/go.mod h1:uki0OHLCXttIQqy/DsJ5X9WaNtRf3HB/OmxCCd+XdZU= +github.com/kwilteam/kuneiform-grammar-go v0.4.1-0.20230807195420-c5c1c840c665 h1:Y4s8p3u5S6gicNoHtOqFM+GI+UNRiSkN2rWdyGvZ6nw= +github.com/kwilteam/kuneiform-grammar-go v0.4.1-0.20230807195420-c5c1c840c665/go.mod h1:uki0OHLCXttIQqy/DsJ5X9WaNtRf3HB/OmxCCd+XdZU= github.com/kwilteam/kwil-db v0.4.1-0.20230721231103-a25e503b79a8 h1:M39ma+5BQ8eH5vQW7IlpGiaj26M4XUE1FhUsg25uors= github.com/kwilteam/kwil-db v0.4.1-0.20230721231103-a25e503b79a8/go.mod h1:UHBvRl97FIeqO2wqrU2rIDU/oiIvMBfEHRnUhsOejRQ= github.com/kwilteam/sql-grammar-go v0.0.2 h1:Tg8xo/Pzd72QsO19wwNvFrpw2muuBaERP7XTv21U4Iw= diff --git a/kfparser/parser_test.go b/kfparser/parser_test.go index e8c27de..3e1f31a 100644 --- a/kfparser/parser_test.go +++ b/kfparser/parser_test.go @@ -419,6 +419,7 @@ func TestParse_valid_syntax(t *testing.T) { }, }}, // action + // action attributes {"action with view", `database td1; table tt1 { tc1 int, tc2 text } action act1() public view { insert into tt1 (tc1, tc2) values (1, "2"); }`, genOneTableTwoColWithActions(schema.ColInt, schema.ColText, @@ -444,6 +445,33 @@ func TestParse_valid_syntax(t *testing.T) { }, }...), }, + {"action with owner", `database td1; table tt1 { tc1 int, tc2 text } + action act1() public owner { insert into tt1 (tc1, tc2) values (1, "2"); }`, + genOneTableTwoColWithActions(schema.ColInt, schema.ColText, + []schema.Action{ + { + Name: "act1", + Public: true, + Mutability: schema.MutabilityUpdate, + Auxiliaries: []schema.AuxiliaryType{schema.AuxiliaryTypeOwner}, + Statements: []string{`insert into tt1 (tc1, tc2) values (1, "2");`}, + }, + }...), + }, + {"action with all auxiliaries", `database td1; table tt1 { tc1 int, tc2 text } + action act1() public owner mustsign { insert into tt1 (tc1, tc2) values (1, "2"); }`, + genOneTableTwoColWithActions(schema.ColInt, schema.ColText, + []schema.Action{ + { + Name: "act1", + Public: true, + Mutability: schema.MutabilityUpdate, + Auxiliaries: []schema.AuxiliaryType{schema.AuxiliaryTypeOwner, schema.AuxiliaryTypeMustSign}, + Statements: []string{`insert into tt1 (tc1, tc2) values (1, "2");`}, + }, + }...), + }, + // action statements {"action with sql insert", `database td1; table tt1 { tc1 int, tc2 text } action act1() public { insert into tt1 (tc1, tc2) values (1, "2"); }`, genOneTableTwoColWithActions(schema.ColInt, schema.ColText, @@ -821,6 +849,9 @@ func TestParse_invalid_semantic(t *testing.T) { {"action auxiliary already set", `database td1; action act1() mustsign view public mustsign { select *; }`, schema.ErrActionAuxiliaryAlreadySet}, + {"action auxiliary already set 2", + `database td1; action act1() mustsign owner public owner { select *; }`, + schema.ErrActionAuxiliaryAlreadySet}, } mode := Default diff --git a/kfparser/visitor.go b/kfparser/visitor.go index 01d9da4..304ac30 100644 --- a/kfparser/visitor.go +++ b/kfparser/visitor.go @@ -366,7 +366,12 @@ func (v *KFVisitor) VisitAction_attr_list(ctx *kfgrammar.Action_attr_listContext panic(fmt.Errorf("%w: %s", schema.ErrActionAuxiliaryAlreadySet, aux.GetText())) } seenAuxs[aux.GetText()] = true - aa.auxs[i] = schema.AuxiliaryType(aux.GetText()) + switch { + case aux.GetText() == schema.AuxiliaryTypeMustSign.String(): + aa.auxs[i] = schema.AuxiliaryTypeMustSign + case aux.GetText() == schema.AuxiliaryTypeOwner.String(): + aa.auxs[i] = schema.AuxiliaryTypeOwner + } } } diff --git a/schema/types.go b/schema/types.go index b7e2f7a..9722bdf 100644 --- a/schema/types.go +++ b/schema/types.go @@ -168,4 +168,6 @@ func (t AuxiliaryType) String() string { const ( // AuxiliaryTypeMustSign is used to specify that an action need signature, it is used for `view` action. AuxiliaryTypeMustSign AuxiliaryType = "mustsign" + // AuxiliaryTypeOwner is used to specify that an action caller must be the owner of the database. + AuxiliaryTypeOwner AuxiliaryType = "owner" )