Skip to content

Commit

Permalink
added AllowNulls to the generator
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Aug 16, 2022
1 parent 143230a commit 3f65169
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cmd/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,19 @@ func (s *serverBuilder) updateViewMeta(table *Table, aView *view.View) error {
if err := json.Unmarshal([]byte(viewHint), tableMeta); err != nil {
return err
}

if tableMeta.Selector != nil {
aView.Selector = tableMeta.Selector
}

if tableMeta.Cache != nil {
aView.Cache = tableMeta.Cache
}

if tableMeta.AllowNulls != nil {
aView.AllowNulls = tableMeta.AllowNulls
}

if tableMeta.Connector != "" {
if _, err := s.addViewConn(tableMeta.Connector, aView); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type (
Parameter *view.Parameter
Auth string
Selector *view.Config
AllowNulls *bool
}

Column struct {
Expand Down
20 changes: 13 additions & 7 deletions view/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type (
Driver string `json:",omitempty"`
DSN string `json:",omitempty"`

_dsn string
//TODO add secure password storage
db func() (*sql.DB, error)
initialized bool
Expand Down Expand Up @@ -54,6 +55,9 @@ func (c *Connector) Init(ctx context.Context, connectors Connectors) error {
return nil
}

c._dsn = c.DSN
c.DSN = ""

if c.initialized {
return nil
}
Expand Down Expand Up @@ -88,7 +92,7 @@ func (c *Connector) DB() (*sql.DB, error) {
}

var err error
dsn := c.DSN
dsn := c._dsn
var secret *scy.Secret
if c.Secret != nil {
secrets := scy.New()
Expand All @@ -113,6 +117,10 @@ func (c *Connector) DB() (*sql.DB, error) {
//Validate check if connector was configured properly.
//Name, Driver and DSN are required.
func (c *Connector) Validate() error {
if c.initialized {
return nil
}

if c.Name == "" {
return fmt.Errorf("connector name was empty")
}
Expand All @@ -121,23 +129,21 @@ func (c *Connector) Validate() error {
return fmt.Errorf("connector driver was empty")
}

if c.DSN == "" {
if notEmptyOf(c._dsn, c.DSN) == "" {
return fmt.Errorf("connector dsn was empty")
}
return nil
}

func (c *Connector) inherit(connector *Connector) {
if c.DSN == "" {
c.DSN = connector.DSN
}
c._dsn = notEmptyOf(c._dsn, c.DSN, connector._dsn, connector.DSN)

if c.Driver == "" {
c.Driver = connector.Driver
}

if c.DSN == "" {
c.DSN = connector.DSN
if c._dsn == "" {
c._dsn = connector._dsn
}

if c.db == nil {
Expand Down
2 changes: 1 addition & 1 deletion view/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNewResourceFromURL(t *testing.T) {
}{
{
url: "case001",
expect: `{"SourceURL":"testdata/case001/resource.yaml","Connectors":[{"Name":"mydb","Driver":"sqlite3","DSN":"/tmp/view/mydb.db"}],"Views":[{"Connector":{"Name":"mydb","Driver":"sqlite3","DSN":"/tmp/view/mydb.db"},"Name":"events","Alias":"t","Table":"events","Columns":[{"Name":"id","DataType":"Int"},{"Name":"quantity","DataType":"Float"},{"Name":"event_type_id","DataType":"Int"}],"CaseFormat":"lu","Selector":{"Constraints":{"Criteria":false,"OrderBy":false,"Limit":false,"Offset":false,"Projection":false}},"Template":{"Source":"events","Schema":{"Cardinality":"One"},"PresenceSchema":{"Cardinality":"One"}},"Schema":{"Cardinality":"One"},"MatchStrategy":"read_matched","Batch":{"Parent":10000},"Logger":{"Name":""},"Caser":5}],"ModTime":"2022-07-13T19:18:05+02:00"}`,
expect: `{"SourceURL":"testdata/case001/resource.yaml","Connectors":[{"Name":"mydb","Driver":"sqlite3"}],"Views":[{"Connector":{"Name":"mydb","Driver":"sqlite3"},"Name":"events","Alias":"t","Table":"events","Columns":[{"Name":"id","DataType":"Int"},{"Name":"quantity","DataType":"Float"},{"Name":"event_type_id","DataType":"Int"}],"CaseFormat":"lu","Selector":{"Constraints":{"Criteria":false,"OrderBy":false,"Limit":false,"Offset":false,"Projection":false}},"Template":{"Source":"events","Schema":{"Cardinality":"One"},"PresenceSchema":{"Cardinality":"One"}},"Schema":{"Cardinality":"One"},"MatchStrategy":"read_matched","Batch":{"Parent":10000},"Logger":{"Name":""},"Caser":5}],"ModTime":"2022-07-13T19:18:05+02:00"}`,
},
}

Expand Down

0 comments on commit 3f65169

Please sign in to comment.