Skip to content

Commit

Permalink
fixup! feat: sqlconnect library
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum committed Mar 6, 2024
1 parent 3c4d5cc commit c8aac4e
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 42 deletions.
19 changes: 19 additions & 0 deletions sqlconnect/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/bigquery"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/databricks"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/mysql"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/postgres"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/snowflake"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/trino"
)

type (
BigQuery = bigquery.Config
Databricks = databricks.Config
Mysql = mysql.Config
Postgres = postgres.Config
Snowflake = snowflake.Config
Trino = trino.Config
)
6 changes: 3 additions & 3 deletions sqlconnect/internal/bigquery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Config struct {
UseLegacyMappings bool `json:"useLegacyMappings"`
}

func ParseConfig(configJSON json.RawMessage) (config Config, err error) {
err = json.Unmarshal(configJSON, &config)
return
// Parse parses the given JSON into the config
func (c *Config) Parse(configJSON json.RawMessage) error {
return json.Unmarshal(configJSON, c)
}
3 changes: 2 additions & 1 deletion sqlconnect/internal/bigquery/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const (

// NewDB creates a new client
func NewDB(configJSON json.RawMessage) (*DB, error) {
config, err := ParseConfig(configJSON)
var config Config
err := config.Parse(configJSON)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions sqlconnect/internal/databricks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ type Config struct {
UseLegacyMappings bool `json:"useLegacyMappings"`
}

func ParseConfig(configJson json.RawMessage) (config Config, err error) {
err = json.Unmarshal(configJson, &config)
func (c *Config) Parse(configJson json.RawMessage) error {
err := json.Unmarshal(configJson, c)
if err != nil {
return
return err
}
// if catalog is empty from the UI, use default "hive_metastore"
if config.Catalog == "" {
config.Catalog = "hive_metastore"
if c.Catalog == "" {
c.Catalog = "hive_metastore"
}
return
return nil
}
6 changes: 2 additions & 4 deletions sqlconnect/internal/databricks/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const (

// NewDB creates a new postgres-specific client
func NewDB(configJson json.RawMessage) (*DB, error) {
config, err := ParseConfig(configJson)
var config Config
err := config.Parse(configJson)
if err != nil {
return nil, err
}
Expand All @@ -43,9 +44,6 @@ func NewDB(configJson json.RawMessage) (*DB, error) {
}

db := sql.OpenDB(connector)
if err != nil {
return nil, err
}
db.SetConnMaxIdleTime(config.MaxConnIdleTime)

return &DB{
Expand Down
12 changes: 6 additions & 6 deletions sqlconnect/internal/mysql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func (c Config) TLS() (string, error) {
}
}

func ParseConfig(input json.RawMessage) (config Config, err error) {
err = json.Unmarshal(input, &config)
func (c *Config) Parse(input json.RawMessage) error {
err := json.Unmarshal(input, c)
if err != nil {
return
return err
}
if !config.SkipHostValidation {
err = util.ValidateHost(config.Host)
if !c.SkipHostValidation {
return util.ValidateHost(c.Host)
}
return
return nil
}
7 changes: 4 additions & 3 deletions sqlconnect/internal/mysql/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (

func TestConfig(t *testing.T) {
t.Run("host validation", func(t *testing.T) {
_, err := mysql.ParseConfig([]byte(`{"host": "localhost"}`))
var config mysql.Config
err := config.Parse([]byte(`{"host": "localhost"}`))
require.Error(t, err, "should not allow localhost")

_, err = mysql.ParseConfig([]byte(`{"host": "127.0.0.1"}`))
err = config.Parse([]byte(`{"host": "127.0.0.1"}`))
require.Error(t, err, "should not allow 127.0.0.1")

_, err = mysql.ParseConfig([]byte(`{"host": "0.0.0.0"}`))
err = config.Parse([]byte(`{"host": "0.0.0.0"}`))
require.Error(t, err, "should not allow 0.0.0.0")
})

Expand Down
3 changes: 2 additions & 1 deletion sqlconnect/internal/mysql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (

// NewDB creates a new postgres-specific client
func NewDB(configJSON json.RawMessage) (*DB, error) {
config, err := ParseConfig(configJSON)
var config Config
err := config.Parse(configJSON)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions sqlconnect/internal/postgres/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func (c Config) ConnectionString() string {
return fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s sslmode=%s", c.Host, c.Port, c.DBName, c.User, c.Password, sslMode)
}

func ParseConfig(input json.RawMessage) (config Config, err error) {
err = json.Unmarshal(input, &config)
func (c *Config) Parse(input json.RawMessage) error {
err := json.Unmarshal(input, c)
if err != nil {
return
return err
}
if !config.SkipHostValidation {
err = util.ValidateHost(config.Host)
if !c.SkipHostValidation {
return util.ValidateHost(c.Host)
}
return
return nil
}
3 changes: 2 additions & 1 deletion sqlconnect/internal/postgres/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (

// NewDB creates a new postgres-specific client
func NewDB(credentialsJSON json.RawMessage) (*DB, error) {
config, err := ParseConfig(credentialsJSON)
var config Config
err := config.Parse(credentialsJSON)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion sqlconnect/internal/redshift/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (
)

func NewDB(credentialsJSON json.RawMessage) (*DB, error) {
config, err := postgres.ParseConfig(credentialsJSON)
var config postgres.Config
err := config.Parse(credentialsJSON)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions sqlconnect/internal/snowflake/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (c Config) ConnectionString() (dsn string, err error) {
return
}

func ParseConfig(configJSON json.RawMessage) (config Config, err error) {
err = json.Unmarshal(configJSON, &config)
return
func (c *Config) Parse(configJSON json.RawMessage) error {
return json.Unmarshal(configJSON, c)
}
3 changes: 2 additions & 1 deletion sqlconnect/internal/snowflake/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (

// NewDB creates a new postgres-specific client
func NewDB(configJSON json.RawMessage) (*DB, error) {
config, err := ParseConfig(configJSON)
var config Config
err := config.Parse(configJSON)
if err != nil {
return nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions sqlconnect/internal/trino/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ func (c Config) ConnectionString() (string, error) {
return dsn, nil
}

func ParseConfig(input json.RawMessage) (config Config, err error) {
err = json.Unmarshal(input, &config)
func (c *Config) Parse(input json.RawMessage) error {
err := json.Unmarshal(input, c)
if err != nil {
return
return err
}
err = util.ValidateHost(config.Host)
return
return util.ValidateHost(c.Host)
}
3 changes: 2 additions & 1 deletion sqlconnect/internal/trino/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (

// NewDB creates a new postgres-specific client
func NewDB(configJSON json.RawMessage) (*DB, error) {
config, err := ParseConfig(configJSON)
var config Config
err := config.Parse(configJSON)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c8aac4e

Please sign in to comment.