Compare commits

...

5 Commits

15 changed files with 179 additions and 42 deletions

View File

@ -28,12 +28,19 @@ func Filter(w *bytes.Buffer, b []byte, keys []string) error {
var k []byte
state := expectKey
instr := false
slash := 0
for i := 0; i < len(b); i++ {
if instr && b[i] == '\\' {
slash++
continue
}
if b[i] == '"' && (slash%2 == 0) {
instr = !instr
}
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
@ -70,7 +77,7 @@ func Filter(w *bytes.Buffer, b []byte, keys []string) error {
state = expectKeyClose
s = i
case state == expectKeyClose && (b[i-1] != '\\' && b[i] == '"'):
case state == expectKeyClose && (b[i] == '"' && (slash%2 == 0)):
state = expectColon
k = b[(s + 1):i]
@ -80,7 +87,7 @@ func Filter(w *bytes.Buffer, b []byte, keys []string) error {
case state == expectValue && b[i] == '"':
state = expectString
case state == expectString && (b[i-1] != '\\' && b[i] == '"'):
case state == expectString && (b[i] == '"' && (slash%2 == 0)):
e = i
case state == expectValue && b[i] == '[':

View File

@ -52,12 +52,19 @@ func Get(b []byte, keys [][]byte) []Field {
n := 0
instr := false
slash := 0
for i := 0; i < len(b); i++ {
if instr && b[i] == '\\' {
slash++
continue
}
if b[i] == '"' && (slash%2 == 0) {
instr = !instr
}
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
@ -73,7 +80,7 @@ func Get(b []byte, keys [][]byte) []Field {
state = expectKeyClose
s = i
case state == expectKeyClose && (b[i-1] != '\\' && b[i] == '"'):
case state == expectKeyClose && (b[i] == '"' && (slash%2 == 0)):
state = expectColon
k = b[(s + 1):i]
@ -84,7 +91,7 @@ func Get(b []byte, keys [][]byte) []Field {
state = expectString
s = i
case state == expectString && (b[i-1] != '\\' && b[i] == '"'):
case state == expectString && (b[i] == '"' && (slash%2 == 0)):
e = i
case state == expectValue && b[i] == '[':
@ -155,6 +162,8 @@ func Get(b []byte, keys [][]byte) []Field {
state = expectKey
e = 0
}
slash = 0
}
return res[:n]

View File

@ -2,7 +2,9 @@ package jsn
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"testing"
)
@ -163,7 +165,9 @@ var (
input6 = `
{"users" : [{"id" : 1, "email" : "vicram@gmail.com", "slug" : "vikram-rangnekar", "threads" : [], "threads_cursor" : null}, {"id" : 3, "email" : "marareilly@lang.name", "slug" : "raymundo-corwin", "threads" : [{"id" : 9, "title" : "Et alias et aut porro praesentium nam in voluptatem reiciendis quisquam perspiciatis inventore eos quia et et enim qui amet."}, {"id" : 25, "title" : "Ipsam quam nemo culpa tempore amet optio sit sed eligendi autem consequatur quaerat rem velit quibusdam quibusdam optio a voluptatem."}], "threads_cursor" : 25}], "users_cursor" : 3}`
input7, _ = ioutil.ReadFile("test.json")
input7, _ = ioutil.ReadFile("test7.json")
input8, _ = ioutil.ReadFile("test8.json")
)
func TestGet(t *testing.T) {
@ -268,6 +272,23 @@ func TestGet3(t *testing.T) {
}
}
func TestGet4(t *testing.T) {
exp := `"# \n\n@@@java\npackage main\n\nimport (\n \"net/http\"\n \"strings\"\n\n \"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n r := gin.Default()\n r.LoadHTMLGlob(\"templates/*\")\n\n r.GET(\"/\", handleIndex)\n r.GET(\"/to/:name\", handleIndex)\n r.Run()\n}\n\n// Hello is page data for the template\ntype Hello struct {\n Name string\n}\n\nfunc handleIndex(c *gin.Context) {\n name := c.Param(\"name\")\n if name != \"\" {\n name = strings.TrimPrefix(c.Param(\"name\"), \"/\")\n }\n c.HTML(http.StatusOK, \"hellofly.tmpl\", gin.H{\"Name\": name})\n}\n@@@\n\n\\"`
exp = strings.ReplaceAll(exp, "@", "`")
values := Get(input8, [][]byte{[]byte("body")})
if string(values[0].Key) != "body" {
t.Fatal("unexpected key")
}
if string(values[0].Value) != exp {
fmt.Println(string(values[0].Value))
t.Fatal("unexpected value")
}
}
func TestValue(t *testing.T) {
v1 := []byte("12345")
if !bytes.Equal(Value(v1), v1) {

View File

@ -11,12 +11,19 @@ func Keys(b []byte) [][]byte {
st := NewStack()
ae := 0
instr := false
slash := 0
for i := 0; i < len(b); i++ {
if instr && b[i] == '\\' {
slash++
continue
}
if b[i] == '"' && (slash%2 == 0) {
instr = !instr
}
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
@ -52,7 +59,7 @@ func Keys(b []byte) [][]byte {
state = expectKeyClose
s = i
case state == expectKeyClose && (b[i-1] != '\\' && b[i] == '"'):
case state == expectKeyClose && (b[i] == '"' && (slash%2 == 0)):
state = expectColon
k = b[(s + 1):i]
@ -63,7 +70,7 @@ func Keys(b []byte) [][]byte {
state = expectString
s = i
case state == expectString && (b[i-1] != '\\' && b[i] == '"'):
case state == expectString && (b[i] == '"' && (slash%2 == 0)):
e = i
case state == expectValue && b[i] == '{':
@ -135,6 +142,7 @@ func Keys(b []byte) [][]byte {
e = 0
}
slash = 0
}
return res

View File

@ -12,6 +12,11 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
return errors.New("'from' and 'to' must be of the same length")
}
if len(from) == 0 || len(to) == 0 {
_, err := w.Write(b)
return err
}
h := xxhash.New()
tmap := make(map[uint64]int, len(from))
@ -33,17 +38,24 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
ws, we := -1, len(b)
instr := false
slash := 0
for i := 0; i < len(b); i++ {
if instr && b[i] == '\\' {
slash++
continue
}
// skip any left padding whitespace
if ws == -1 && (b[i] == '{' || b[i] == '[') {
ws = i
}
if b[i] == '"' && (slash%2 == 0) {
instr = !instr
}
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
@ -59,7 +71,7 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
state = expectKeyClose
s = i
case state == expectKeyClose && (b[i-1] != '\\' && b[i] == '"'):
case state == expectKeyClose && (b[i] == '"' && (slash%2 == 0)):
state = expectColon
if _, err := h.Write(b[(s + 1):i]); err != nil {
return err
@ -73,7 +85,7 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
state = expectString
s = i
case state == expectString && (b[i-1] != '\\' && b[i] == '"'):
case state == expectString && (b[i] == '"' && (slash%2 == 0)):
e = i
case state == expectValue && b[i] == '[':
@ -167,6 +179,8 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
e = 0
d = 0
}
slash = 0
}
if ws == -1 || (ws == 0 && we == len(b)) {

View File

@ -12,12 +12,19 @@ func Strip(b []byte, path [][]byte) []byte {
pm := false
state := expectKey
instr := false
slash := 0
for i := 0; i < len(b); i++ {
if instr && b[i] == '\\' {
slash++
continue
}
if b[i] == '"' && (slash%2 == 0) {
instr = !instr
}
if state == expectObjClose || state == expectListClose {
if b[i-1] != '\\' && b[i] == '"' {
instr = !instr
}
if !instr {
switch b[i] {
case '{', '[':
@ -33,7 +40,7 @@ func Strip(b []byte, path [][]byte) []byte {
state = expectKeyClose
s = i
case state == expectKeyClose && (b[i-1] != '\\' && b[i] == '"'):
case state == expectKeyClose && (b[i] == '"' && (slash%2 == 0)):
state = expectColon
if pi == len(path) {
pi = 0
@ -50,7 +57,7 @@ func Strip(b []byte, path [][]byte) []byte {
state = expectString
s = i
case state == expectString && (b[i-1] != '\\' && b[i] == '"'):
case state == expectString && (b[i] == '"' && (slash%2 == 0)):
e = i
case state == expectValue && b[i] == '[':
@ -107,6 +114,8 @@ func Strip(b []byte, path [][]byte) []byte {
state = expectKey
e = 0
}
slash = 0
}
return ob

7
jsn/test8.json Normal file
View File

@ -0,0 +1,7 @@
{
"data": {
"slug": "javapackage-mainimport-nethttp-strings-githubcomgi-2786",
"published": true,
"body": "# \n\n```java\npackage main\n\nimport (\n \"net/http\"\n \"strings\"\n\n \"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n r := gin.Default()\n r.LoadHTMLGlob(\"templates/*\")\n\n r.GET(\"/\", handleIndex)\n r.GET(\"/to/:name\", handleIndex)\n r.Run()\n}\n\n// Hello is page data for the template\ntype Hello struct {\n Name string\n}\n\nfunc handleIndex(c *gin.Context) {\n name := c.Param(\"name\")\n if name != \"\" {\n name = strings.TrimPrefix(c.Param(\"name\"), \"/\")\n }\n c.HTML(http.StatusOK, \"hellofly.tmpl\", gin.H{\"Name\": name})\n}\n```\n\n\\"
}
}

View File

@ -3,7 +3,6 @@ package psql
import (
"errors"
"fmt"
"io"
"strings"
@ -61,7 +60,6 @@ func (c *compilerContext) renderBaseColumns(
if err := c.renderColumnFunction(sel, ti, col, i); err != nil {
return nil, false, err
}
fmt.Println(">>>> isAgg", cn, sel.Name)
isAgg = true
}

View File

@ -93,7 +93,7 @@ func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer, vars Variables) (
io.WriteString(c.w, `SELECT json_build_object(`)
for _, id := range qc.Roots {
root := &qc.Selects[id]
if root.SkipRender {
if root.SkipRender || len(root.Cols) == 0 {
continue
}
@ -126,6 +126,10 @@ func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer, vars Variables) (
if id < closeBlock {
sel := &c.s[id]
if len(sel.Cols) == 0 {
continue
}
ti, err := c.schema.GetTable(sel.Name)
if err != nil {
return 0, err

View File

@ -66,7 +66,14 @@ func NewDBSchema(info *DBInfo, aliases map[string][]string) (*DBSchema, error) {
}
for i, t := range info.Tables {
err := schema.updateRelationships(t, info.Columns[i])
err := schema.firstDegreeRels(t, info.Columns[i])
if err != nil {
return nil, err
}
}
for i, t := range info.Tables {
err := schema.secondDegreeRels(t, info.Columns[i])
if err != nil {
return nil, err
}
@ -131,8 +138,7 @@ func (s *DBSchema) addTable(
return nil
}
func (s *DBSchema) updateRelationships(t DBTable, cols []DBColumn) error {
jcols := make([]DBColumn, 0, len(cols))
func (s *DBSchema) firstDegreeRels(t DBTable, cols []DBColumn) error {
ct := t.Key
cti, ok := s.t[ct]
if !ok {
@ -230,6 +236,51 @@ func (s *DBSchema) updateRelationships(t DBTable, cols []DBColumn) error {
if err := s.SetRel(ft, ct, rel2); err != nil {
return err
}
}
return nil
}
func (s *DBSchema) secondDegreeRels(t DBTable, cols []DBColumn) error {
jcols := make([]DBColumn, 0, len(cols))
ct := t.Key
cti, ok := s.t[ct]
if !ok {
return fmt.Errorf("invalid foreign key table '%s'", ct)
}
for i := range cols {
c := cols[i]
if len(c.FKeyTable) == 0 {
continue
}
// Foreign key column name
ft := strings.ToLower(c.FKeyTable)
ti, ok := s.t[ft]
if !ok {
return fmt.Errorf("invalid foreign key table '%s'", ft)
}
// This is an embedded relationship like when a json/jsonb column
// is exposed as a table
if c.Name == c.FKeyTable && len(c.FKeyColID) == 0 {
continue
}
if len(c.FKeyColID) == 0 {
continue
}
// Foreign key column id
fcid := c.FKeyColID[0]
if _, ok := ti.ColIDMap[fcid]; !ok {
return fmt.Errorf("invalid foreign key column id '%d' for table '%s'",
fcid, ti.Name)
}
jcols = append(jcols, c)
}
@ -322,6 +373,9 @@ func (s *DBSchema) GetTable(table string) (*DBTableInfo, error) {
}
func (s *DBSchema) SetRel(child, parent string, rel *DBRel) error {
sp := strings.ToLower(flect.Singularize(parent))
pp := strings.ToLower(flect.Pluralize(parent))
sc := strings.ToLower(flect.Singularize(child))
pc := strings.ToLower(flect.Pluralize(child))
@ -333,9 +387,6 @@ func (s *DBSchema) SetRel(child, parent string, rel *DBRel) error {
s.rm[pc] = make(map[string]*DBRel)
}
sp := strings.ToLower(flect.Singularize(parent))
pp := strings.ToLower(flect.Pluralize(parent))
if _, ok := s.rm[sc][sp]; !ok {
s.rm[sc][sp] = rel
}

View File

@ -19,6 +19,10 @@ func (rt RelType) String() string {
}
func (re *DBRel) String() string {
if re.Type == RelOneToManyThrough {
return fmt.Sprintf("'%s.%s' --(Through: %s)--> '%s.%s'",
re.Left.Table, re.Left.Col, re.Through, re.Right.Table, re.Right.Col)
}
return fmt.Sprintf("'%s.%s' --(%s)--> '%s.%s'",
re.Left.Table, re.Left.Col, re.Type, re.Right.Table, re.Right.Col)
}

View File

@ -92,7 +92,14 @@ func getTestSchema() *DBSchema {
}
for i, t := range tables {
err := schema.updateRelationships(t, columns[i])
err := schema.firstDegreeRels(t, columns[i])
if err != nil {
log.Fatal(err)
}
}
for i, t := range tables {
err := schema.secondDegreeRels(t, columns[i])
if err != nil {
log.Fatal(err)
}

View File

@ -222,6 +222,10 @@ func (c *compilerContext) renderDelete(qc *qcode.QCode, w io.Writer,
quoted(c.w, ti.Name)
io.WriteString(c.w, ` WHERE `)
if root.Where == nil {
return 0, errors.New("'where' clause missing in delete mutation")
}
if err := c.renderWhere(root, ti); err != nil {
return 0, err
}

View File

@ -59,12 +59,6 @@ func buildRoleStmt(gql, vars []byte, role string) ([]stmt, error) {
return nil, err
}
// For the 'anon' role in production only compile
// queries for tables defined in the config file.
if conf.Production && ro.Name == "anon" && !hasTablesWithConfig(qc, ro) {
return nil, errors.New("query contains tables with no 'anon' role config")
}
stmts := []stmt{stmt{role: ro, qc: qc}}
w := &bytes.Buffer{}