|
| 1 | +package actions |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 7 | + "github.com/aws/aws-sdk-go-v2/service/redshiftdata" |
| 8 | + "github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools" |
| 9 | + "log" |
| 10 | +) |
| 11 | + |
| 12 | +// RedshiftDataActions wraps RedshiftData actions. |
| 13 | +type RedshiftDataActions struct { |
| 14 | + RedshiftDataClient *redshiftdata.Client |
| 15 | +} |
| 16 | + |
| 17 | +// snippet-start:[gov2.redshift.RedshiftQuery.struct] |
| 18 | + |
| 19 | +// RedshiftQuery makes it easier to deal with RedshiftQuery objects. |
| 20 | +type RedshiftQuery struct { |
| 21 | + Result interface{} |
| 22 | + Input redshiftdata.DescribeStatementInput |
| 23 | + Context context.Context |
| 24 | +} |
| 25 | + |
| 26 | +// snippet-end:[gov2.redshift.RedshiftQuery.struct] |
| 27 | + |
| 28 | +// snippet-start:[gov2.redshift.ExecuteStatement |
| 29 | + |
| 30 | +// ExecuteStatement calls the ExecuteStatement operation from the RedshiftDataClient |
| 31 | +func (actor RedshiftDataActions) ExecuteStatement(ctx context.Context, input redshiftdata.ExecuteStatementInput) (*redshiftdata.ExecuteStatementOutput, error) { |
| 32 | + |
| 33 | + return actor.RedshiftDataClient.ExecuteStatement(ctx, &input) |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +// snippet-end:[gov2.redshift.ExecuteStatement |
| 38 | + |
| 39 | +// snippet-start:[gov2.redshift.ExecuteBatchStatement |
| 40 | + |
| 41 | +// ExecuteBatchStatement calls the BatchExecuteStatement operation from the RedshiftDataClient |
| 42 | +func (actor RedshiftDataActions) ExecuteBatchStatement(ctx context.Context, input redshiftdata.BatchExecuteStatementInput) (*redshiftdata.BatchExecuteStatementOutput, error) { |
| 43 | + return actor.RedshiftDataClient.BatchExecuteStatement(ctx, &input) |
| 44 | +} |
| 45 | + |
| 46 | +// snippet-end:[gov2.redshift.ExecuteBatchStatement |
| 47 | + |
| 48 | +// snippet-start:[gov2.redshift.ListDatabases] |
| 49 | + |
| 50 | +// ListDatabases lists all databases in the given cluster. |
| 51 | +func (actor RedshiftDataActions) ListDatabases(ctx context.Context, clusterId string, databaseName string, userName string) error { |
| 52 | + input := redshiftdata.ListDatabasesInput{ |
| 53 | + ClusterIdentifier: aws.String(clusterId), |
| 54 | + Database: aws.String(databaseName), |
| 55 | + DbUser: aws.String(userName), |
| 56 | + } |
| 57 | + |
| 58 | + output, err := actor.RedshiftDataClient.ListDatabases(ctx, &input) |
| 59 | + if err != nil { |
| 60 | + log.Printf("Failed to list databases: %v\n", err) |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + for _, database := range output.Databases { |
| 65 | + log.Printf("The database name is : %s\n", database) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +// snippet-end:[gov2.redshift.ListDatabases] |
| 71 | + |
| 72 | +// snippet-start:[gov2.redshift.CreateTable] |
| 73 | + |
| 74 | +// CreateTable creates a table named <tableName> in the <databaseName> database with the given arguments. |
| 75 | +func (actor RedshiftDataActions) CreateTable(ctx context.Context, clusterId string, databaseName string, tableName string, userName string, args []string) error { |
| 76 | + sql := "CREATE TABLE " + tableName + " (" + |
| 77 | + "id bigint identity(1, 1), " + |
| 78 | + "PRIMARY KEY (id)" |
| 79 | + for _, value := range args { |
| 80 | + sql += ", " + value |
| 81 | + } |
| 82 | + sql += ");" |
| 83 | + createTableInput := &redshiftdata.ExecuteStatementInput{ |
| 84 | + ClusterIdentifier: aws.String(clusterId), |
| 85 | + Database: aws.String(databaseName), |
| 86 | + DbUser: aws.String(userName), |
| 87 | + Sql: aws.String(sql), |
| 88 | + } |
| 89 | + |
| 90 | + output, err := actor.RedshiftDataClient.ExecuteStatement(ctx, createTableInput) |
| 91 | + if err != nil { |
| 92 | + log.Printf("Failed to create table: %v\n", err) |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + log.Println("Table created:", *output.Id) |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// snippet-end:[gov2.redshift.CreateTable] |
| 101 | + |
| 102 | +// snippet-start:[gov2.redshift.DeleteTable] |
| 103 | + |
| 104 | +// DeleteTable drops the table named <tableName> from the <databaseName> database. |
| 105 | +func (actor RedshiftDataActions) DeleteTable(ctx context.Context, clusterId string, databaseName string, tableName string, userName string) (bool, error) { |
| 106 | + sql := "DROP TABLE " + tableName |
| 107 | + deleteTableInput := &redshiftdata.ExecuteStatementInput{ |
| 108 | + ClusterIdentifier: aws.String(clusterId), |
| 109 | + Database: aws.String(databaseName), |
| 110 | + DbUser: aws.String(userName), |
| 111 | + Sql: aws.String(sql), |
| 112 | + } |
| 113 | + |
| 114 | + output, err := actor.RedshiftDataClient.ExecuteStatement(ctx, deleteTableInput) |
| 115 | + if err != nil { |
| 116 | + log.Printf("Failed to delete table "+tableName+" from "+databaseName+" database: %v\n", err) |
| 117 | + return false, err |
| 118 | + } |
| 119 | + |
| 120 | + log.Println(tableName+" table deleted from "+databaseName+" database:", *output.Id) |
| 121 | + return true, nil |
| 122 | +} |
| 123 | + |
| 124 | +// snippet-end:[gov2.redshift.DeleteTable] |
| 125 | + |
| 126 | +// snippet-start:[gov2.redshift.DeleteRows] |
| 127 | + |
| 128 | +// DeleteDataRows deletes all rows from the given table. |
| 129 | +func (actor RedshiftDataActions) DeleteDataRows(ctx context.Context, clusterId string, databaseName string, tableName string, userName string, pauser demotools.IPausable) (bool, error) { |
| 130 | + deleteRows := &redshiftdata.ExecuteStatementInput{ |
| 131 | + ClusterIdentifier: aws.String(clusterId), |
| 132 | + Database: aws.String(databaseName), |
| 133 | + DbUser: aws.String(userName), |
| 134 | + Sql: aws.String("DELETE FROM " + tableName + ";"), |
| 135 | + } |
| 136 | + |
| 137 | + result, err := actor.RedshiftDataClient.ExecuteStatement(ctx, deleteRows) |
| 138 | + if err != nil { |
| 139 | + log.Printf("Failed to execute batch statement: %v\n", err) |
| 140 | + return false, err |
| 141 | + } |
| 142 | + describeInput := redshiftdata.DescribeStatementInput{ |
| 143 | + Id: result.Id, |
| 144 | + } |
| 145 | + query := RedshiftQuery{ |
| 146 | + Context: ctx, |
| 147 | + Result: result, |
| 148 | + Input: describeInput, |
| 149 | + } |
| 150 | + err = actor.WaitForQueryStatus(query, pauser, true) |
| 151 | + if err != nil { |
| 152 | + log.Printf("Failed to execute delete query: %v\n", err) |
| 153 | + return false, err |
| 154 | + } |
| 155 | + |
| 156 | + log.Printf("Successfully executed delete statement\n") |
| 157 | + return true, nil |
| 158 | +} |
| 159 | + |
| 160 | +// snippet-end:[gov2.redshift.DeleteRows] |
| 161 | + |
| 162 | +// snippet-start:[gov2.redshift.WaitForQueryStatus] |
| 163 | + |
| 164 | +// WaitForQueryStatus waits until the given RedshiftQuery object has succeeded or failed. |
| 165 | +func (actor RedshiftDataActions) WaitForQueryStatus(query RedshiftQuery, pauser demotools.IPausable, showProgress bool) error { |
| 166 | + done := false |
| 167 | + attempts := 0 |
| 168 | + maxWaitCycles := 30 |
| 169 | + for done == false { |
| 170 | + describeOutput, err := actor.RedshiftDataClient.DescribeStatement(query.Context, &query.Input) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + if describeOutput.Status == "FAILED" { |
| 175 | + return errors.New("failed to describe statement") |
| 176 | + } |
| 177 | + if attempts >= maxWaitCycles { |
| 178 | + return errors.New("timed out waiting for statement") |
| 179 | + } |
| 180 | + if showProgress { |
| 181 | + log.Print(".") |
| 182 | + } |
| 183 | + if describeOutput.Status == "FINISHED" { |
| 184 | + done = true |
| 185 | + } |
| 186 | + attempts++ |
| 187 | + pauser.Pause(attempts) |
| 188 | + } |
| 189 | + return nil |
| 190 | +} |
| 191 | + |
| 192 | +// snippet-end:[gov2.redshift.WaitForQueryStatus] |
| 193 | + |
| 194 | +// snippet-start:[gov2.redshift.GetStatementResult] |
| 195 | + |
| 196 | +// GetStatementResult returns the result of the statement with the given id. |
| 197 | +func (actor RedshiftDataActions) GetStatementResult(ctx context.Context, statementId string) (*redshiftdata.GetStatementResultOutput, error) { |
| 198 | + getStatementResultOutput, err := actor.RedshiftDataClient.GetStatementResult(ctx, &redshiftdata.GetStatementResultInput{ |
| 199 | + Id: aws.String(statementId), |
| 200 | + }) |
| 201 | + if err != nil { |
| 202 | + return nil, err |
| 203 | + } |
| 204 | + return getStatementResultOutput, nil |
| 205 | +} |
| 206 | + |
| 207 | +// snippet-end:[gov2.redshift.GetStatementResult] |
0 commit comments