Skip to content

Commit

Permalink
lucy: rename DomainType to FamilyType
Browse files Browse the repository at this point in the history
  • Loading branch information
supercmmetry committed Feb 6, 2020
1 parent b21841a commit 59bc4c3
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 88 deletions.
20 changes: 0 additions & 20 deletions core/domain.go

This file was deleted.

20 changes: 20 additions & 0 deletions core/family.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lucy

type FamilyType uint

const (
Where FamilyType = iota
Relation FamilyType = iota
Creation FamilyType = iota
Updation FamilyType = iota
Deletion FamilyType = iota
SetTarget FamilyType = iota
Unknown FamilyType = iota
And FamilyType = iota
Or FamilyType = iota
MiscNodeName FamilyType = iota
Model FamilyType = iota
AndStr FamilyType = iota
OrStr FamilyType = iota
WhereStr FamilyType = iota
)
20 changes: 10 additions & 10 deletions core/lucy.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (l *Database) Find(param interface{}) *Database {
return l
}

l.addQuery(Query{DomainType: SetTarget, Params: Marshal(param), Output: param})
l.addQuery(Query{FamilyType: SetTarget, Params: Marshal(param), Output: param})
l.Error = l.layer.Sync()

return l
Expand All @@ -74,9 +74,9 @@ func (l *Database) Where(I_ interface{}, I ...interface{}) *Database {

if reflect.TypeOf(I_) == reflect.TypeOf(Exp{}) {
Exp := I_
l.addQuery(Query{DomainType: Where, Params: Exp})
l.addQuery(Query{FamilyType: Where, Params: Exp})
} else if reflect.TypeOf(I_) == reflect.TypeOf("") {
l.addQuery(Query{DomainType: WhereStr, Params: Format(I_.(string), I)})
l.addQuery(Query{FamilyType: WhereStr, Params: Format(I_.(string), I)})
} else {
l.Error = e.Error(e.UnrecognizedExpression)
}
Expand All @@ -89,7 +89,7 @@ func (l *Database) Create(params interface{}) *Database {
return l
}

l.addQuery(Query{DomainType: Creation, Params: Marshal(params)})
l.addQuery(Query{FamilyType: Creation, Params: Marshal(params)})
l.Error = l.layer.Sync()

return l
Expand All @@ -102,9 +102,9 @@ func (l *Database) And(I_ interface{}, I ...interface{}) *Database {

if reflect.TypeOf(I_) == reflect.TypeOf(Exp{}) {
Exp := I_
l.addQuery(Query{DomainType: And, Params: Exp})
l.addQuery(Query{FamilyType: And, Params: Exp})
} else if reflect.TypeOf(I_) == reflect.TypeOf("") {
l.addQuery(Query{DomainType: AndStr, Params: Format(I_.(string), I)})
l.addQuery(Query{FamilyType: AndStr, Params: Format(I_.(string), I)})
} else {
l.Error = e.Error(e.UnrecognizedExpression)
}
Expand All @@ -119,9 +119,9 @@ func (l *Database) Or(I_ interface{}, I ...interface{}) *Database {

if reflect.TypeOf(I_) == reflect.TypeOf(Exp{}) {
Exp := I_
l.addQuery(Query{DomainType: Or, Params: Exp})
l.addQuery(Query{FamilyType: Or, Params: Exp})
} else if reflect.TypeOf(I_) == reflect.TypeOf("") {
l.addQuery(Query{DomainType: OrStr, Params: Format(I_.(string), I)})
l.addQuery(Query{FamilyType: OrStr, Params: Format(I_.(string), I)})
} else {
l.Error = e.Error(e.UnrecognizedExpression)
}
Expand All @@ -134,7 +134,7 @@ func (l *Database) By(name string) *Database {
return l
}

l.addQuery(Query{DomainType: MiscNodeName, Params: name})
l.addQuery(Query{FamilyType: MiscNodeName, Params: name})

return l
}
Expand All @@ -144,7 +144,7 @@ func (l *Database) Model(i interface{}) *Database {
return l
}

l.addQuery(Query{DomainType: Model, Params: Marshal(i)})
l.addQuery(Query{FamilyType: Model, Params: Marshal(i)})

return l
}
Expand Down
2 changes: 1 addition & 1 deletion core/query.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lucy

type Query struct {
DomainType DomainType
FamilyType FamilyType
Params interface{}
Output interface{}
}
117 changes: 60 additions & 57 deletions core/query_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import (
e "lucy/errors"
)


type QueryCradle struct {
Exps Queue
Ops Queue
dom, pdom DomainType
deps map[DomainType]struct{}
Out interface{}
Exps Queue
Ops Queue
family, prevFamily FamilyType
deps map[FamilyType]struct{}
Out interface{}
}

func (c *QueryCradle) init() {
c.dom = Unknown
c.pdom = Unknown
c.family = Unknown
c.prevFamily = Unknown
c.Exps.Init()
c.Ops.Init()
c.deps = make(map[DomainType]struct{})
c.deps = make(map[FamilyType]struct{})
}

type QueryRuntime interface {
Expand Down Expand Up @@ -65,15 +64,15 @@ func (q *QueryEngine) Sync() error {

qr := qri.(Query)

cradle.dom = qr.DomainType
switch qr.DomainType {
cradle.family = qr.FamilyType
switch qr.FamilyType {
case Where:
{
if cradle.pdom == Where {
if cradle.prevFamily== Where {
return e.Error(e.CorruptedQueryChain)
}
exp := qr.Params.(Exp)
for k,v := range exp {
for k, v := range exp {
exp[k] = Format("?", v) // Sanitize values

// Detect injection in keys
Expand All @@ -86,28 +85,29 @@ func (q *QueryEngine) Sync() error {

cradle.deps[Where] = struct{}{}
}
case WhereStr: {
if cradle.pdom == Where {
return e.Error(e.CorruptedQueryChain)
}
param := qr.Params.(string)
case WhereStr:
{
if cradle.prevFamily == Where {
return e.Error(e.CorruptedQueryChain)
}
param := qr.Params.(string)

if s, ok := q.Runtime.CheckForInjection(param); ok {
return e.Error(e.QueryInjection, e.Severity(s))
}
if s, ok := q.Runtime.CheckForInjection(param); ok {
return e.Error(e.QueryInjection, e.Severity(s))
}

cradle.Exps.Push(param)
cradle.Ops.Push(cradle.dom)
cradle.Exps.Push(param)
cradle.Ops.Push(cradle.family)

cradle.deps[Where] = struct{}{}
}
cradle.deps[Where] = struct{}{}
}
case And:
{
if _, ok := cradle.deps[Where]; !ok {
return e.Error(e.UnsatisfiedDependency)
}
exp := qr.Params.(Exp)
for k,v := range exp {
for k, v := range exp {
exp[k] = Format("?", v) // Sanitize values

// Detect injection in keys
Expand All @@ -116,28 +116,29 @@ func (q *QueryEngine) Sync() error {
}
}
cradle.Exps.Push(exp)
cradle.Ops.Push(cradle.dom)
cradle.Ops.Push(cradle.family)
}
case AndStr:{
if _, ok := cradle.deps[Where]; !ok {
return e.Error(e.UnsatisfiedDependency)
}
param := qr.Params.(string)
case AndStr:
{
if _, ok := cradle.deps[Where]; !ok {
return e.Error(e.UnsatisfiedDependency)
}
param := qr.Params.(string)

if s, ok := q.Runtime.CheckForInjection(param); ok {
return e.Error(e.QueryInjection, e.Severity(s))
}
if s, ok := q.Runtime.CheckForInjection(param); ok {
return e.Error(e.QueryInjection, e.Severity(s))
}

cradle.Exps.Push(param)
cradle.Ops.Push(cradle.dom)
}
cradle.Exps.Push(param)
cradle.Ops.Push(cradle.family)
}
case Or:
{
if _, ok := q.cradle.deps[Where]; !ok {
return e.Error(e.UnsatisfiedDependency)
}
exp := qr.Params.(Exp)
for k,v := range exp {
for k, v := range exp {
exp[k] = Format("?", v) // Sanitize values

// Detect injection in keys
Expand All @@ -146,24 +147,25 @@ func (q *QueryEngine) Sync() error {
}
}
cradle.Exps.Push(exp)
cradle.Ops.Push(cradle.dom)
cradle.Ops.Push(cradle.family)
}
case OrStr:{
case OrStr:
{
if _, ok := q.cradle.deps[Where]; !ok {
return e.Error(e.UnsatisfiedDependency)
}
{
if _, ok := q.cradle.deps[Where]; !ok {
return e.Error(e.UnsatisfiedDependency)
}

param := qr.Params.(string)
param := qr.Params.(string)

if s, ok := q.Runtime.CheckForInjection(param); ok {
return e.Error(e.QueryInjection, e.Severity(s))
}
if s, ok := q.Runtime.CheckForInjection(param); ok {
return e.Error(e.QueryInjection, e.Severity(s))
}

cradle.Exps.Push(param)
cradle.Ops.Push(cradle.dom)
cradle.Exps.Push(param)
cradle.Ops.Push(cradle.family)
}
}
}
case SetTarget:
{
/* If the 'Where' clause is used in conjunction with 'SetTarget (aka) Find' ,
Expand All @@ -180,14 +182,15 @@ func (q *QueryEngine) Sync() error {

cradle.Out = qr.Output
}
case MiscNodeName: {
cradle.Ops.Push(MiscNodeName)
cradle.Exps.Push(qr.Params)
}
case MiscNodeName:
{
cradle.Ops.Push(MiscNodeName)
cradle.Exps.Push(qr.Params)
}
}

cradle.pdom = cradle.dom
cradle.prevFamily = cradle.family
}

return nil
}
}

0 comments on commit 59bc4c3

Please sign in to comment.