Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add procedures to sdkv2 #2202

Merged
merged 12 commits into from
Dec 8, 2023
2 changes: 2 additions & 0 deletions pkg/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Client struct {
Parameters Parameters
PasswordPolicies PasswordPolicies
Pipes Pipes
Procedures Procedures
ResourceMonitors ResourceMonitors
Roles Roles
Schemas Schemas
Expand Down Expand Up @@ -173,6 +174,7 @@ func (c *Client) initialize() {
c.Parameters = &parameters{client: c}
c.PasswordPolicies = &passwordPolicies{client: c}
c.Pipes = &pipes{client: c}
c.Procedures = &procedures{client: c}
c.ReplicationFunctions = &replicationFunctions{client: c}
c.ResourceMonitors = &resourceMonitors{client: c}
c.Roles = &roles{client: c}
Expand Down
28 changes: 28 additions & 0 deletions pkg/sdk/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,31 @@ func (row *propertyRow) toBoolProperty() *BoolProperty {
Description: row.Description,
}
}

type ExecuteAs string

func ExecuteAsPointer(v ExecuteAs) *ExecuteAs {
return &v
}

const (
ExecuteAsCaller ExecuteAs = "EXECUTE AS CALLER"
ExecuteAsOwner ExecuteAs = "EXECUTE AS OWNER"
)

type NullInputBehavior string

func NullInputBehaviorPointer(v NullInputBehavior) *NullInputBehavior {
return &v
}

const (
NullInputBehaviorCalledOnNullInput NullInputBehavior = "CALLED ON NULL INPUT"
NullInputBehaviorReturnNullInput NullInputBehavior = "RETURN NULL ON NULL INPUT"
NullInputBehaviorStrict NullInputBehavior = "STRICT"
)

type Secret struct {
VariableName string `ddl:"keyword,single_quotes"`
Name string `ddl:"parameter,no_quotes"`
}
4 changes: 2 additions & 2 deletions pkg/sdk/poc/generator/keyword_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (v *QueryStruct) SetTags() *QueryStruct {
}

func (v *QueryStruct) OptionalSetTags() *QueryStruct {
return v.setTags(nil)
return v.setTags(KeywordOptions().SQL("SET TAG"))
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
}

func (v *QueryStruct) setTags(transformer *KeywordTransformer) *QueryStruct {
Expand All @@ -91,7 +91,7 @@ func (v *QueryStruct) UnsetTags() *QueryStruct {
}

func (v *QueryStruct) OptionalUnsetTags() *QueryStruct {
return v.unsetTags(nil)
return v.unsetTags(KeywordOptions().SQL("UNSET TAG"))
}

func (v *QueryStruct) unsetTags(transformer *KeywordTransformer) *QueryStruct {
Expand Down
1 change: 1 addition & 0 deletions pkg/sdk/poc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var definitionMapping = map[string]*generator.Interface{
"application_roles_def.go": sdk.ApplicationRolesDef,
"views_def.go": sdk.ViewsDef,
"stages_def.go": sdk.StagesDef,
"procedures_def.go": sdk.ProceduresDef,
"event_tables_def.go": sdk.EventTablesDef,
}

Expand Down
317 changes: 317 additions & 0 deletions pkg/sdk/procedures_def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
package sdk

import g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator"

//go:generate go run ./poc/main.go

/*
todo: add custom operation for:
- call and call-with for scripting https://docs.snowflake.com/en/sql-reference/sql/call
*/

var procedureArgument = g.NewQueryStruct("ProcedureArgument").
Text("ArgName", g.KeywordOptions().NoQuotes().Required()).
PredefinedQueryStructField("ArgDataType", "DataType", g.KeywordOptions().NoQuotes().Required()).
PredefinedQueryStructField("DefaultValue", "*string", g.ParameterOptions().NoEquals().SQL("DEFAULT"))

var procedureColumn = g.NewQueryStruct("ProcedureColumn").
sfc-gh-jcieslak marked this conversation as resolved.
Show resolved Hide resolved
Text("ColumnName", g.KeywordOptions().NoQuotes().Required()).
PredefinedQueryStructField("ColumnDataType", "DataType", g.KeywordOptions().NoQuotes().Required())

var procedureReturns = g.NewQueryStruct("ProcedureReturns").
OptionalQueryStructField(
"ResultDataType",
g.NewQueryStruct("ProcedureReturnsResultDataType").
PredefinedQueryStructField("ResultDataType", "DataType", g.KeywordOptions().NoQuotes().Required()).
OptionalSQL("NULL").OptionalSQL("NOT NULL"),
g.KeywordOptions(),
).
OptionalQueryStructField(
"Table",
g.NewQueryStruct("ProcedureReturnsTable").
ListQueryStructField(
"Columns",
procedureColumn,
g.ParameterOptions().Parentheses().NoEquals(),
),
g.KeywordOptions().SQL("TABLE"),
)

var procedureSQLReturns = g.NewQueryStruct("ProcedureSQLReturns").
OptionalQueryStructField(
"ResultDataType",
g.NewQueryStruct("ProcedureReturnsResultDataType").
PredefinedQueryStructField("ResultDataType", "DataType", g.KeywordOptions().NoQuotes().Required()),
g.KeywordOptions(),
).
OptionalQueryStructField(
"Table",
g.NewQueryStruct("ProcedureReturnsTable").
ListQueryStructField(
"Columns",
procedureColumn,
g.ParameterOptions().Parentheses().NoEquals(),
),
g.KeywordOptions().SQL("TABLE"),
).
OptionalSQL("NOT NULL")

var (
procedureImport = g.NewQueryStruct("ProcedureImport").Text("Import", g.KeywordOptions().SingleQuotes().Required())
procedurePackage = g.NewQueryStruct("ProcedurePackage").Text("Package", g.KeywordOptions().SingleQuotes().Required())
)

var ProceduresDef = g.NewInterface(
"Procedures",
"Procedure",
g.KindOfT[SchemaObjectIdentifier](),
).CustomOperation(
"CreateForJava",
"https://docs.snowflake.com/en/sql-reference/sql/create-procedure#java-handler",
g.NewQueryStruct("CreateForJava").
Create().
OrReplace().
OptionalSQL("SECURE").
SQL("PROCEDURE").
Name().
ListQueryStructField(
"Arguments",
procedureArgument,
g.ParameterOptions().Parentheses().NoEquals(),
).
OptionalSQL("COPY GRANTS").
QueryStructField(
"Returns",
procedureReturns,
g.KeywordOptions().SQL("RETURNS").Required(),
).
SQL("LANGUAGE JAVA").
TextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes().Required()).
ListQueryStructField(
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
"Packages",
procedurePackage,
g.ParameterOptions().Parentheses().SQL("PACKAGES").Required(),
).
ListQueryStructField(
"Imports",
procedureImport,
g.ParameterOptions().Parentheses().SQL("IMPORTS"),
).
TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes().Required()).
ListAssignment("EXTERNAL_ACCESS_INTEGRATIONS", "AccountObjectIdentifier", g.ParameterOptions().Parentheses()).
ListAssignment("SECRETS", "Secret", g.ParameterOptions().Parentheses()).
OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("ExecuteAs", "*ExecuteAs", g.KeywordOptions()).
PredefinedQueryStructField("ProcedureDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")).
WithValidation(g.ValidIdentifier, "name"),
).CustomOperation(
"CreateForJavaScript",
"https://docs.snowflake.com/en/sql-reference/sql/create-procedure#javascript-handler",
g.NewQueryStruct("CreateForJavaScript").
Create().
OrReplace().
OptionalSQL("SECURE").
SQL("PROCEDURE").
Name().
ListQueryStructField(
"Arguments",
procedureArgument,
g.ParameterOptions().Parentheses().NoEquals(),
).
OptionalSQL("COPY GRANTS").
PredefinedQueryStructField("ResultDataType", "DataType", g.ParameterOptions().NoEquals().SQL("RETURNS").Required()).
OptionalSQL("NOT NULL").
SQL("LANGUAGE JAVASCRIPT").
PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("ExecuteAs", "*ExecuteAs", g.KeywordOptions()).
PredefinedQueryStructField("ProcedureDefinition", "string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS").Required()).
WithValidation(g.ValidIdentifier, "name"),
).CustomOperation(
"CreateForPython",
"https://docs.snowflake.com/en/sql-reference/sql/create-procedure#python-handler",
g.NewQueryStruct("CreateForPython").
Create().
OrReplace().
OptionalSQL("SECURE").
SQL("PROCEDURE").
Name().
ListQueryStructField(
"Arguments",
procedureArgument,
g.ParameterOptions().Parentheses().NoEquals(),
).
OptionalSQL("COPY GRANTS").
QueryStructField(
"Returns",
procedureReturns,
g.KeywordOptions().SQL("RETURNS").Required(),
).
SQL("LANGUAGE PYTHON").
TextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes().Required()).
ListQueryStructField(
"Packages",
procedurePackage,
g.ParameterOptions().Parentheses().SQL("PACKAGES").Required(),
).
ListQueryStructField(
"Imports",
procedureImport,
g.ParameterOptions().Parentheses().SQL("IMPORTS"),
).
TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes().Required()).
ListAssignment("EXTERNAL_ACCESS_INTEGRATIONS", "AccountObjectIdentifier", g.ParameterOptions().Parentheses()).
ListAssignment("SECRETS", "Secret", g.ParameterOptions().Parentheses()).
PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("ExecuteAs", "*ExecuteAs", g.KeywordOptions()).
PredefinedQueryStructField("ProcedureDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")).
WithValidation(g.ValidIdentifier, "name"),
).CustomOperation(
"CreateForScala",
"https://docs.snowflake.com/en/sql-reference/sql/create-procedure#scala-handler",
g.NewQueryStruct("CreateForScala").
Create().
OrReplace().
OptionalSQL("SECURE").
SQL("PROCEDURE").
Name().
ListQueryStructField(
"Arguments",
procedureArgument,
g.ParameterOptions().Parentheses().NoEquals(),
).
OptionalSQL("COPY GRANTS").
QueryStructField(
"Returns",
procedureReturns,
g.KeywordOptions().SQL("RETURNS").Required(),
).
SQL("LANGUAGE SCALA").
TextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes().Required()).
ListQueryStructField(
"Packages",
procedurePackage,
g.ParameterOptions().Parentheses().SQL("PACKAGES").Required(),
).
ListQueryStructField(
"Imports",
procedureImport,
g.ParameterOptions().Parentheses().SQL("IMPORTS"),
).
TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes().Required()).
OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("ExecuteAs", "*ExecuteAs", g.KeywordOptions()).
PredefinedQueryStructField("ProcedureDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")).
WithValidation(g.ValidIdentifier, "name"),
).CustomOperation(
sfc-gh-jcieslak marked this conversation as resolved.
Show resolved Hide resolved
"CreateForSQL",
"https://docs.snowflake.com/en/sql-reference/sql/create-procedure#snowflake-scripting-handler",
g.NewQueryStruct("CreateForSQL").
Create().
OrReplace().
OptionalSQL("SECURE").
SQL("PROCEDURE").
Name().
ListQueryStructField(
"Arguments",
procedureArgument,
g.ParameterOptions().Parentheses().NoEquals(),
).
OptionalSQL("COPY GRANTS").
QueryStructField(
"Returns",
procedureSQLReturns,
g.KeywordOptions().SQL("RETURNS").Required(),
).
SQL("LANGUAGE SQL").
PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("ExecuteAs", "*ExecuteAs", g.KeywordOptions()).
PredefinedQueryStructField("ProcedureDefinition", "string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS").Required()).
WithValidation(g.ValidIdentifier, "name"),
).AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-procedure",
g.NewQueryStruct("AlterProcedure").
Alter().
SQL("PROCEDURE").
IfExists().
Name().
PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()).
OptionalIdentifier("RenameTo", g.KindOfT[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")).
OptionalTextAssignment("SET COMMENT", g.ParameterOptions().SingleQuotes()).
OptionalTextAssignment("SET LOG_LEVEL", g.ParameterOptions().SingleQuotes()).
OptionalTextAssignment("SET TRACE_LEVEL", g.ParameterOptions().SingleQuotes()).
OptionalSQL("UNSET COMMENT").
OptionalSetTags().
OptionalUnsetTags().
PredefinedQueryStructField("ExecuteAs", "*ExecuteAs", g.KeywordOptions()).
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ValidIdentifierIfSet, "RenameTo").
WithValidation(g.ExactlyOneValueSet, "RenameTo", "SetComment", "SetLogLevel", "SetTraceLevel", "UnsetComment", "SetTags", "UnsetTags", "ExecuteAs"),
).DropOperation(
"https://docs.snowflake.com/en/sql-reference/sql/drop-procedure",
g.NewQueryStruct("DropProcedure").
Drop().
SQL("PROCEDURE").
IfExists().
Name().
PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()).
WithValidation(g.ValidIdentifier, "name"),
).ShowOperation(
"https://docs.snowflake.com/en/sql-reference/sql/show-procedures",
g.DbStruct("procedureRow").
Field("created_on", "string").
sfc-gh-jcieslak marked this conversation as resolved.
Show resolved Hide resolved
Field("name", "string").
Field("schema_name", "string").
Field("is_builtin", "string").
Field("is_aggregate", "string").
Field("is_ansi", "string").
Field("min_num_arguments", "int").
Field("max_num_arguments", "int").
Field("arguments", "string").
Field("description", "string").
Field("catalog_name", "string").
Field("is_table_function", "string").
Field("valid_for_clustering", "string").
Field("is_secure", "sql.NullString"),
g.PlainStruct("Procedure").
Field("CreatedOn", "string").
Field("Name", "string").
Field("SchemaName", "string").
Field("IsBuiltin", "bool").
Field("IsAggregate", "bool").
Field("IsAnsi", "bool").
Field("MinNumArguments", "int").
Field("MaxNumArguments", "int").
Field("Arguments", "string").
Field("Description", "string").
Field("CatalogName", "string").
Field("IsTableFunction", "bool").
Field("ValidForClustering", "bool").
Field("IsSecure", "bool"),
g.NewQueryStruct("ShowProcedures").
Show().
SQL("PROCEDURES").
OptionalLike().
OptionalIn(), // TODO: 'In' struct for procedures not support keyword "CLASS" now
).ShowByIdOperation().DescribeOperation(
g.DescriptionMappingKindSlice,
"https://docs.snowflake.com/en/sql-reference/sql/desc-procedure",
g.DbStruct("procedureDetailRow").
Field("property", "string").
Field("value", "string"),
g.PlainStruct("ProcedureDetail").
Field("Property", "string").
Field("Value", "string"),
g.NewQueryStruct("DescribeProcedure").
Describe().
SQL("PROCEDURE").
Name().
PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()).
WithValidation(g.ValidIdentifier, "name"),
)
Loading
Loading