diff --git a/cmd/relation.go b/cmd/relation.go index fdf5de22..0fdc1d43 100644 --- a/cmd/relation.go +++ b/cmd/relation.go @@ -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 diff --git a/cmd/sqlx.go b/cmd/sqlx.go index 7e3c943e..742819a7 100644 --- a/cmd/sqlx.go +++ b/cmd/sqlx.go @@ -41,6 +41,7 @@ type ( Parameter *view.Parameter Auth string Selector *view.Config + AllowNulls *bool } Column struct { diff --git a/view/connector.go b/view/connector.go index d86217df..395e15d5 100644 --- a/view/connector.go +++ b/view/connector.go @@ -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 @@ -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 } @@ -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() @@ -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") } @@ -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 { diff --git a/view/resource_test.go b/view/resource_test.go index 9395edc3..63b0408e 100644 --- a/view/resource_test.go +++ b/view/resource_test.go @@ -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"}`, }, }