-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
537 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package sqlconnect_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/rudderlabs/sqlconnect-go/sqlconnect" | ||
) | ||
|
||
func TestQueryDef(t *testing.T) { | ||
t.Run("with columns", func(t *testing.T) { | ||
table := sqlconnect.NewRelationRef("table") | ||
q := sqlconnect.QueryDef{ | ||
Table: &table, | ||
Columns: []string{"col1", "col2"}, | ||
Conditions: []*sqlconnect.QueryCondition{ | ||
{Column: "col1", Operator: "=", Value: "'1'"}, | ||
{Column: "col2", Operator: ">", Value: "2"}, | ||
}, | ||
OrderBy: &sqlconnect.QueryOrder{ | ||
Column: "col1", | ||
Order: "ASC", | ||
}, | ||
} | ||
|
||
sql := q.ToSQL(testDialect{}) | ||
expected := `SELECT "col1","col2" FROM "table" WHERE "col1" = '1' AND "col2" > 2 ORDER BY "col1" ASC` | ||
require.Equal(t, expected, sql, "query should be formatted correctly") | ||
}) | ||
|
||
t.Run("without columns", func(t *testing.T) { | ||
table := sqlconnect.NewRelationRef("table") | ||
q := sqlconnect.QueryDef{ | ||
Table: &table, | ||
Conditions: []*sqlconnect.QueryCondition{ | ||
{Column: "col1", Operator: "=", Value: "'1'"}, | ||
{Column: "col2", Operator: ">", Value: "2"}, | ||
}, | ||
} | ||
|
||
sql := q.ToSQL(testDialect{}) | ||
expected := `SELECT * FROM "table" WHERE "col1" = '1' AND "col2" > 2` | ||
require.Equal(t, expected, sql, "query should be formatted correctly") | ||
}) | ||
} | ||
|
||
type testDialect struct{} | ||
|
||
func (d testDialect) FormatTableName(name string) string { | ||
return name | ||
} | ||
|
||
func (d testDialect) QuoteIdentifier(name string) string { | ||
return fmt.Sprintf(`"%s"`, name) | ||
} | ||
|
||
func (d testDialect) QuoteTable(relation sqlconnect.RelationRef) string { | ||
if relation.Schema != "" { | ||
return fmt.Sprintf(`"%s"."%s"`, relation.Schema, relation.Name) | ||
} | ||
return fmt.Sprintf(`"%s"`, relation.Name) | ||
} |
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,30 @@ | ||
package base | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/rudderlabs/sqlconnect-go/sqlconnect" | ||
) | ||
|
||
func TestDialect(t *testing.T) { | ||
var d dialect | ||
t.Run("format table", func(t *testing.T) { | ||
formatted := d.FormatTableName("TaBle") | ||
require.Equal(t, "table", formatted, "table name should be lowercased") | ||
}) | ||
|
||
t.Run("quote identifier", func(t *testing.T) { | ||
quoted := d.QuoteIdentifier("column") | ||
require.Equal(t, `"column"`, quoted, "column name should be quoted with double quotes") | ||
}) | ||
|
||
t.Run("quote table", func(t *testing.T) { | ||
quoted := d.QuoteTable(sqlconnect.NewRelationRef("table")) | ||
require.Equal(t, `"table"`, quoted, "table name should be quoted with double quotes") | ||
|
||
quoted = d.QuoteTable(sqlconnect.NewRelationRef("table", sqlconnect.WithSchema("schema"))) | ||
require.Equal(t, `"schema"."table"`, quoted, "schema and table name should be quoted with double quotes") | ||
}) | ||
} |
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,30 @@ | ||
package bigquery | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/rudderlabs/sqlconnect-go/sqlconnect" | ||
) | ||
|
||
func TestDialect(t *testing.T) { | ||
var d dialect | ||
t.Run("format table", func(t *testing.T) { | ||
formatted := d.FormatTableName("TaBle") | ||
require.Equal(t, "table", formatted, "table name should be lowercased") | ||
}) | ||
|
||
t.Run("quote identifier", func(t *testing.T) { | ||
quoted := d.QuoteIdentifier("column") | ||
require.Equal(t, "`column`", quoted, "column name should be quoted with backticks") | ||
}) | ||
|
||
t.Run("quote table", func(t *testing.T) { | ||
quoted := d.QuoteTable(sqlconnect.NewRelationRef("table")) | ||
require.Equal(t, "`table`", quoted, "table name should be quoted with backticks") | ||
|
||
quoted = d.QuoteTable(sqlconnect.NewRelationRef("table", sqlconnect.WithSchema("schema"))) | ||
require.Equal(t, "`schema.table`", quoted, "schema and table name should be quoted with backticks") | ||
}) | ||
} |
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,18 @@ | ||
package bigquery_test | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/bigquery" | ||
integrationtest "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/integration_test" | ||
) | ||
|
||
func TestBigqueryDB(t *testing.T) { | ||
configJSON, ok := os.LookupEnv("BIGQUERY_TEST_ENVIRONMENT_CREDENTIALS") | ||
if !ok { | ||
t.Skip("skipping bigquery integration test due to lack of a test environment") | ||
} | ||
integrationtest.TestDatabaseScenarios(t, bigquery.DatabaseType, []byte(configJSON), strings.ToLower) | ||
} |
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,57 @@ | ||
package bigquery | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"cloud.google.com/go/bigquery" | ||
"google.golang.org/api/googleapi" | ||
"google.golang.org/api/iterator" | ||
|
||
"github.com/rudderlabs/sqlconnect-go/sqlconnect" | ||
) | ||
|
||
// SchemaExists uses the bigquery client instead of [INFORMATION_SCHEMA.SCHEMATA] due to absence of a region qualifier | ||
// https://cloud.google.com/bigquery/docs/information-schema-datasets-schemata#scope_and_syntax | ||
func (db *DB) SchemaExists(ctx context.Context, schemaRef sqlconnect.SchemaRef) (bool, error) { | ||
var exists bool | ||
if err := db.WithBigqueryClient(ctx, func(c *bigquery.Client) error { | ||
if _, err := c.Dataset(schemaRef.Name).Metadata(ctx); err != nil { | ||
var e *googleapi.Error | ||
if ok := errors.As(err, &e); ok { | ||
if e.Code == 404 { // not found | ||
return nil | ||
} | ||
} | ||
return err | ||
} | ||
exists = true | ||
return nil | ||
}); err != nil { | ||
return false, err | ||
} | ||
return exists, nil | ||
} | ||
|
||
// ListSchemas uses the bigquery client instead of [INFORMATION_SCHEMA.SCHEMATA] due to absence of a region qualifier | ||
// https://cloud.google.com/bigquery/docs/information-schema-datasets-schemata#scope_and_syntax | ||
func (db *DB) ListSchemas(ctx context.Context) ([]sqlconnect.SchemaRef, error) { | ||
var schemas []sqlconnect.SchemaRef | ||
if err := db.WithBigqueryClient(ctx, func(c *bigquery.Client) error { | ||
datasets := c.Datasets(ctx) | ||
for { | ||
var dataset *bigquery.Dataset | ||
dataset, err := datasets.Next() | ||
if err != nil { | ||
if err == iterator.Done { | ||
return nil | ||
} | ||
return err | ||
} | ||
schemas = append(schemas, sqlconnect.SchemaRef{Name: dataset.DatasetID}) | ||
} | ||
}); err != nil { | ||
return nil, err | ||
} | ||
return schemas, nil | ||
} |
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
Oops, something went wrong.