-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ScanAllSets to the api (#115)
* feat: add ScanAllSets * test: mssql for ScanAllSets * fix: ScanOne should close rows * Add a way to test mssql * fix linters * Fix godot --------- Co-authored-by: Georgy Savva <[email protected]>
- Loading branch information
1 parent
80c2cc6
commit d60e58c
Showing
11 changed files
with
201 additions
and
14 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
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,9 @@ | ||
version: "3.2" | ||
services: | ||
sql-server-db: | ||
image: mcr.microsoft.com/mssql/server:2017-latest | ||
ports: | ||
- "1433:1433" | ||
environment: | ||
SA_PASSWORD: "p@sSword" | ||
ACCEPT_EULA: "Y" |
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
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,78 @@ | ||
//go:build with_mssql | ||
// +build with_mssql | ||
|
||
package sqlscan_test | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/georgysavva/scany/v2/sqlscan" | ||
_ "github.com/microsoft/go-mssqldb" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
multipleSetsQueryMssql = ` | ||
SELECT * | ||
FROM ( | ||
VALUES ('foo val', 'bar val'), ('foo val 2', 'bar val 2'), ('foo val 3', 'bar val 3') | ||
) as t1 (foo, bar); | ||
SELECT * | ||
FROM ( | ||
VALUES ('egg val', 'bacon val') | ||
) as t2 (egg, bacon); | ||
` | ||
) | ||
|
||
func getEnv(key string, defaultValue string) string { | ||
value, exists := os.LookupEnv(key) | ||
if !exists { | ||
return defaultValue | ||
} | ||
return value | ||
} | ||
func TestMSScanAllSets(t *testing.T) { | ||
t.Parallel() | ||
testSqliteDB, err := sql.Open("sqlserver", getEnv("MSSQL_URL", "sqlserver://sa:p@sSword@localhost:1433?database=master")) | ||
if err != nil { | ||
require.NoError(t, err) | ||
} | ||
dbscanAPI, err := sqlscan.NewDBScanAPI() | ||
if err != nil { | ||
require.NoError(t, fmt.Errorf("new DB scan API: %w", err)) | ||
} | ||
api, err := sqlscan.NewAPI(dbscanAPI) | ||
if err != nil { | ||
require.NoError(t, fmt.Errorf("new API: %w", err)) | ||
} | ||
type testModel2 struct { | ||
Egg string | ||
Bacon string | ||
} | ||
|
||
expected1 := []*testModel{ | ||
{Foo: "foo val", Bar: "bar val"}, | ||
{Foo: "foo val 2", Bar: "bar val 2"}, | ||
{Foo: "foo val 3", Bar: "bar val 3"}, | ||
} | ||
expected2 := []*testModel2{ | ||
{Egg: "egg val", Bacon: "bacon val"}, | ||
} | ||
|
||
rows, err := testSqliteDB.Query(multipleSetsQueryMssql) | ||
require.NoError(t, err) | ||
|
||
var got1 []*testModel | ||
var got2 []*testModel2 | ||
err = api.ScanAllSets([]any{&got1, &got2}, rows) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, expected1, got1) | ||
assert.Equal(t, expected2, got2) | ||
} |
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