forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from muroon/prepare
Prepared Statement
- Loading branch information
Showing
9 changed files
with
552 additions
and
5 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
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,72 @@ | ||
package athena | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func Test_getCatalog(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ctx context.Context | ||
want string | ||
want1 bool | ||
}{ | ||
{ | ||
name: "Default", | ||
ctx: context.Background(), | ||
want: "", | ||
want1: false, | ||
}, | ||
{ | ||
name: "SetCatalog", | ||
ctx: SetCatalog(context.Background(), "test_catalog"), | ||
want: "test_catalog", | ||
want1: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, got1 := getCatalog(tt.ctx) | ||
if got != tt.want { | ||
t.Errorf("getCatalog() got = %v, want %v", got, tt.want) | ||
} | ||
if got1 != tt.want1 { | ||
t.Errorf("getCatalog() got1 = %v, want %v", got1, tt.want1) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_getTimeout(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ctx context.Context | ||
want uint | ||
want1 bool | ||
}{ | ||
{ | ||
name: "Default", | ||
ctx: context.Background(), | ||
want: 0, | ||
want1: false, | ||
}, | ||
{ | ||
name: "SetTimeout", | ||
ctx: SetTimeout(context.Background(), 100), | ||
want: 100, | ||
want1: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, got1 := getTimeout(tt.ctx) | ||
if got != tt.want { | ||
t.Errorf("getTimeout() got = %v, want %v", got, tt.want) | ||
} | ||
if got1 != tt.want1 { | ||
t.Errorf("getTimeout() got1 = %v, want %v", got1, tt.want1) | ||
} | ||
}) | ||
} | ||
} |
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,53 @@ | ||
# Prepared Statements | ||
|
||
You can use [Prepared Statements on Athena](https://docs.aws.amazon.com/athena/latest/ug/querying-with-prepared-statements.html). | ||
|
||
## How to use | ||
|
||
``` | ||
db, _ := sql.Open("athena", "db=default&output_location=s3://results") | ||
// 1. Prepare | ||
stmt, _ := db.PrepareContext(ctx, "SELECT url, code FROM cloudfront WHERE code = ?") | ||
defer stmt.Close() // 3. Close | ||
// 2. Execute | ||
rows, _ := stmt.QueryContext(ctx, targetCode) | ||
defer rows.Close() | ||
for rows.Next() { | ||
var url string | ||
var code int | ||
rows.Scan(&url, &code) | ||
} | ||
``` | ||
|
||
### 1. Prepare | ||
- Run [PREPARE](https://docs.aws.amazon.com/athena/latest/ug/querying-with-prepared-statements.html#querying-with-prepared-statements-sql-statements) on Athena to create a Statement for use with Athena | ||
- Create a Stmt object and keep statement_name inside | ||
- The stmt object is valid until Close method is executed | ||
- Result Mode | ||
- Available under all Result Modes | ||
- ResultMode can be specified in PrepareContext | ||
``` | ||
rows, _ := stmt.PrepareContext(SetDLMode(ctx), sql) // Prepares a statement in DL Mode | ||
``` | ||
### 2. Execute | ||
- Run a prepared statement using [EXECUTE](https://docs.aws.amazon.com/athena/latest/ug/querying-with-prepared-statements.html#querying-with-prepared-statements-sql-statements) on Athena | ||
- You can specify parameters | ||
- Use QueryContext and ExecContext methods | ||
### 3. Close | ||
- Run [DEALLOCATE PREPARE](https://docs.aws.amazon.com/athena/latest/ug/querying-with-prepared-statements.html#querying-with-prepared-statements-sql-statements) and delete the prepared statement | ||
- Use the Close method of the Stmt object | ||
## Examples | ||
``` | ||
intParam := 1 | ||
stringParam := "string value" | ||
stmt, _ := db.PrepareContext(ctx, "SELECT * FROM test_table WHERE int_column = ? AND string_column = ?") | ||
rows, _ := stmt.QueryContext(ctx, intParam, stringParam) | ||
``` | ||
execute `SELECT * FROM test_table WHERE int_column = 1 and string_column = 'string value'` |
Oops, something went wrong.