Skip to content

Commit

Permalink
Parking the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrishnasanka committed Apr 10, 2024
1 parent c43e065 commit 26a43bf
Show file tree
Hide file tree
Showing 17 changed files with 353 additions and 1,020 deletions.
63 changes: 33 additions & 30 deletions core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,38 @@ const (
// GraphJin struct is an instance of the GraphJin engine it holds all the required information like
// datase schemas, relationships, etc that the GraphQL to SQL compiler would need to do it's job.
type graphjin struct {
conf *Config
db *sql.DB
log *_log.Logger
fs FS
trace Tracer
dbtype string
dbinfo *sdata.DBInfo
schema *sdata.DBSchema
allowList *allow.List
encKey [32]byte
encKeySet bool
cache Cache
queries sync.Map
roles map[string]*Role
roleStmt string
roleStmtMD psql.Metadata
tmap map[string]qcode.TConfig
rtmap map[string]ResolverFn
rmap map[string]resItem
abacEnabled bool
qc *qcode.Compiler
pc *psql.Compiler
subs sync.Map
prod bool
prodSec bool
namespace string
pf []byte
opts []Option
done chan bool
conf *Config
db *sql.DB
log *_log.Logger
fs FS
trace Tracer
dbtype string
dbinfo *sdata.DBInfo
schemas []*sdata.DBSchema
allowList *allow.List
encKey [32]byte
encKeySet bool
cache Cache
queries sync.Map
roles map[string]*Role
roleStmt string
roleStmtMD psql.Metadata
tmap map[string]qcode.TConfig
// TODO: Update the naming of both these maps to be more
// meaningful and less confusing
rtmap map[string]ResolverFn
rmap map[string]ResItem
abacEnabled bool
qCodeCompiler *qcode.Compiler
pCodeCompiler *psql.Compiler
subs sync.Map
prod bool
prodSec bool
namespace string
// What kind of a prefix is this ?
pf []byte
opts []Option
done chan bool
}

type GraphJin struct {
Expand Down Expand Up @@ -485,7 +488,7 @@ func (gj *graphjin) query(c context.Context, r graphqlReq) (
return
}

if r.op == qcode.QTMutation && gj.schema.DBType() == "mysql" {
if r.op == qcode.QTMutation && gj.schemas[0].DBType() == "mysql" {
err = errors.New("mysql: mutations not supported")
return
}
Expand Down
20 changes: 11 additions & 9 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,25 @@ type Table struct {

// Configuration for a database table column
type Column struct {
Name string
Type string `jsonschema:"example=integer,example=text"`
Primary bool
Array bool
Name string
Type string `jsonschema:"example=integer,example=text"`
Primary bool
Array bool
// TODO: This forces us to do string manipulation, change this to have subfields for the relationship that would include schema level resolution
ForeignKey string `mapstructure:"related_to" json:"related_to" yaml:"related_to" jsonschema:"title=Related To,example=other_table.id_column,example=users.id"`
}

// Configuration for user role
type Role struct {
Name string
Comment string
Match string `jsonschema:"title=Related To,example=other_table.id_column,example=users.id"`
Tables []RoleTable `jsonschema:"title=Table Configuration for Role"`
tm map[string]*RoleTable
Name string
Comment string
Match string `jsonschema:"title=Related To,example=other_table.id_column,example=users.id"`
Tables []RoleTable `jsonschema:"title=Table Configuration for Role"`
tablemap map[string]*RoleTable
}

// Table configuration for a specific role (user role)
// TODO: figure out how we should hold info for multiple schemas
type RoleTable struct {
Name string
Schema string
Expand Down
11 changes: 7 additions & 4 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,17 @@ func (gj *graphjin) _initSchema() (err error) {
return
}

gj.schema, err = sdata.NewDBSchema(
schema, err = sdata.NewDBSchema(

Check failure on line 170 in core/core.go

View workflow job for this annotation

GitHub Actions / lint

undefined: sdata.NewDBSchema
gj.dbinfo,
getDBTableAliases(gj.conf))

if err != nil {
return
}

// Append schema to gj.schemas
gj.schemas = append(gj.schemas, schema)

return
}

Expand Down Expand Up @@ -206,16 +209,16 @@ func (gj *graphjin) initCompilers() (err error) {
Validators: valid.Validators,
}

gj.qc, err = qcode.NewCompiler(gj.schema, qcc)
gj.qCodeCompiler, err = qcode.NewCompiler(gj.schema, qcc)

Check failure on line 212 in core/core.go

View workflow job for this annotation

GitHub Actions / lint

gj.schema undefined (type *graphjin has no field or method schema)
if err != nil {
return
}

if err = addRoles(gj.conf, gj.qc); err != nil {
if err = addRoles(gj.conf, gj.qCodeCompiler); err != nil {
return
}

gj.pc = psql.NewCompiler(psql.Config{
gj.pCodeCompiler = psql.NewCompiler(psql.Config{
Vars: gj.conf.Vars,
DBType: gj.schema.DBType(),

Check failure on line 223 in core/core.go

View workflow job for this annotation

GitHub Actions / lint

gj.schema undefined (type *graphjin has no field or method schema)
DBVersion: gj.schema.DBVersion(),

Check failure on line 224 in core/core.go

View workflow job for this annotation

GitHub Actions / lint

gj.schema undefined (type *graphjin has no field or method schema)
Expand Down
4 changes: 2 additions & 2 deletions core/gstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (s *gstate) compileQueryForRole() (err error) {
vars = s.vmap
}

if st.qc, err = s.gj.qc.Compile(
if st.qc, err = s.gj.qCodeCompiler.Compile(
s.r.query,
vars,
s.role,
Expand All @@ -122,7 +122,7 @@ func (s *gstate) compileQueryForRole() (err error) {
}

var w bytes.Buffer
if st.md, err = s.gj.pc.Compile(&w, st.qc); err != nil {
if st.md, err = s.gj.pCodeCompiler.Compile(&w, st.qc); err != nil {
return
}

Expand Down
22 changes: 12 additions & 10 deletions core/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func (gj *graphjin) initConfig() error {
}

role.Match = sanitize(role.Match)
role.tm = make(map[string]*RoleTable)
role.tablemap = make(map[string]*RoleTable)

for n, t := range role.Tables {
role.tm[t.Schema+t.Name] = &role.Tables[n]
role.tablemap[t.Schema+t.Name] = &role.Tables[n]
}

gj.roles[k] = &c.Roles[i]
Expand All @@ -55,17 +55,17 @@ func (gj *graphjin) initConfig() error {
// If user role not defined then create it
if _, ok := gj.roles["user"]; !ok {
ur := Role{
Name: "user",
tm: make(map[string]*RoleTable),
Name: "user",
tablemap: make(map[string]*RoleTable),
}
gj.roles["user"] = &ur
}

// If anon role is not defined then create it
if _, ok := gj.roles["anon"]; !ok {
ur := Role{
Name: "anon",
tm: make(map[string]*RoleTable),
Name: "anon",
tablemap: make(map[string]*RoleTable),
}
gj.roles["anon"] = &ur
}
Expand Down Expand Up @@ -224,7 +224,9 @@ func addJsonTable(conf *Config, di *sdata.DBInfo, t Table) error {
return nil
}

func addVirtualTable(conf *Config, di *sdata.DBInfo, t Table) error {
func addVirtualTable(conf *Config, dbInfo *sdata.DBInfo, t Table) error {
// TODO:Update this method to go through each of the schemas in
// this database config
if len(t.Columns) == 0 {
return fmt.Errorf("polymorphic table: no id column specified")
}
Expand All @@ -234,12 +236,12 @@ func addVirtualTable(conf *Config, di *sdata.DBInfo, t Table) error {
return fmt.Errorf("polymorphic table: no 'related_to' specified on id column")
}

s, ok := c.getFK(di.Schema)
s, ok := c.getFK(dbInfo.Schema)

Check failure on line 239 in core/init.go

View workflow job for this annotation

GitHub Actions / lint

dbInfo.Schema undefined (type *sdata.DBInfo has no field or method Schema)
if !ok {
return fmt.Errorf("polymorphic table: foreign key must be <type column>.<foreign key column>")
}

di.VTables = append(di.VTables, sdata.VirtualTable{
dbInfo.VTables = append(dbInfo.VTables, sdata.VirtualTable{
Name: t.Name,
IDColumn: c.Name,
TypeColumn: s[1],
Expand Down Expand Up @@ -393,7 +395,7 @@ func addRole(qc *qcode.Compiler, r Role, t RoleTable, defaultBlock bool) error {
}

func (r *Role) GetTable(schema, name string) *RoleTable {
return r.tm[name]
return r.tablemap[name]
}

func (c *Column) getFK(defaultSchema string) ([3]string, bool) {
Expand Down
Loading

0 comments on commit 26a43bf

Please sign in to comment.