2019-10-26 07:34:29 +02:00
|
|
|
package psql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2019-12-09 07:48:18 +01:00
|
|
|
"strings"
|
2019-10-26 07:34:29 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/dosco/super-graph/qcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
errNotExpected = "Generated SQL did not match what was expected"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
qcompile *qcode.Compiler
|
|
|
|
pcompile *Compiler
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
qcompile, err = qcode.NewCompiler(qcode.Config{
|
|
|
|
Blocklist: []string{
|
|
|
|
"secret",
|
|
|
|
"password",
|
|
|
|
"token",
|
|
|
|
},
|
|
|
|
})
|
2019-11-28 07:25:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
err = qcompile.AddRole("user", "product", qcode.TRConfig{
|
2019-10-26 07:34:29 +02:00
|
|
|
Query: qcode.QueryConfig{
|
|
|
|
Columns: []string{"id", "name", "price", "users", "customers"},
|
|
|
|
Filters: []string{
|
|
|
|
"{ price: { gt: 0 } }",
|
|
|
|
"{ price: { lt: 8 } }",
|
|
|
|
},
|
|
|
|
},
|
2019-10-30 07:30:31 +01:00
|
|
|
Insert: qcode.InsertConfig{
|
|
|
|
Presets: map[string]string{
|
|
|
|
"user_id": "$user_id",
|
|
|
|
"created_at": "now",
|
|
|
|
"updated_at": "now",
|
|
|
|
},
|
|
|
|
},
|
2019-10-26 07:34:29 +02:00
|
|
|
Update: qcode.UpdateConfig{
|
|
|
|
Filters: []string{"{ user_id: { eq: $user_id } }"},
|
2019-10-30 07:30:31 +01:00
|
|
|
Presets: map[string]string{"updated_at": "now"},
|
2019-10-26 07:34:29 +02:00
|
|
|
},
|
|
|
|
Delete: qcode.DeleteConfig{
|
|
|
|
Filters: []string{
|
|
|
|
"{ price: { gt: 0 } }",
|
|
|
|
"{ price: { lt: 8 } }",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-11-28 07:25:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
err = qcompile.AddRole("anon", "product", qcode.TRConfig{
|
2019-10-26 07:34:29 +02:00
|
|
|
Query: qcode.QueryConfig{
|
|
|
|
Columns: []string{"id", "name"},
|
|
|
|
},
|
|
|
|
})
|
2019-11-28 07:25:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
err = qcompile.AddRole("anon1", "product", qcode.TRConfig{
|
2019-10-26 07:34:29 +02:00
|
|
|
Query: qcode.QueryConfig{
|
|
|
|
Columns: []string{"id", "name", "price"},
|
|
|
|
DisableFunctions: true,
|
|
|
|
},
|
|
|
|
})
|
2019-11-28 07:25:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
err = qcompile.AddRole("user", "users", qcode.TRConfig{
|
2019-10-26 07:34:29 +02:00
|
|
|
Query: qcode.QueryConfig{
|
|
|
|
Columns: []string{"id", "full_name", "avatar", "email", "products"},
|
|
|
|
},
|
|
|
|
})
|
2019-11-28 07:25:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
err = qcompile.AddRole("bad_dude", "users", qcode.TRConfig{
|
2019-10-26 07:34:29 +02:00
|
|
|
Query: qcode.QueryConfig{
|
2019-10-27 06:52:48 +01:00
|
|
|
Filters: []string{"false"},
|
|
|
|
DisableFunctions: true,
|
2019-10-26 07:34:29 +02:00
|
|
|
},
|
|
|
|
Insert: qcode.InsertConfig{
|
|
|
|
Filters: []string{"false"},
|
|
|
|
},
|
|
|
|
Update: qcode.UpdateConfig{
|
|
|
|
Filters: []string{"false"},
|
|
|
|
},
|
|
|
|
})
|
2019-11-28 07:25:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
err = qcompile.AddRole("user", "mes", qcode.TRConfig{
|
2019-10-26 07:34:29 +02:00
|
|
|
Query: qcode.QueryConfig{
|
|
|
|
Columns: []string{"id", "full_name", "avatar"},
|
|
|
|
Filters: []string{
|
|
|
|
"{ id: { eq: $user_id } }",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-11-28 07:25:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
err = qcompile.AddRole("user", "customers", qcode.TRConfig{
|
2019-10-26 07:34:29 +02:00
|
|
|
Query: qcode.QueryConfig{
|
|
|
|
Columns: []string{"id", "email", "full_name", "products"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
tables := []DBTable{
|
|
|
|
DBTable{Name: "customers", Type: "table"},
|
|
|
|
DBTable{Name: "users", Type: "table"},
|
|
|
|
DBTable{Name: "products", Type: "table"},
|
|
|
|
DBTable{Name: "purchases", Type: "table"},
|
|
|
|
DBTable{Name: "tags", Type: "table"},
|
2020-01-28 06:26:53 +01:00
|
|
|
DBTable{Name: "tag_count", Type: "json"},
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
columns := [][]DBColumn{
|
|
|
|
[]DBColumn{
|
2019-12-25 07:24:30 +01:00
|
|
|
DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true},
|
2019-12-09 07:48:18 +01:00
|
|
|
DBColumn{ID: 2, Name: "full_name", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 3, Name: "phone", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 4, Name: "email", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 5, Name: "encrypted_password", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 6, Name: "reset_password_token", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 7, Name: "reset_password_sent_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 8, Name: "remember_created_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 9, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 10, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}},
|
|
|
|
[]DBColumn{
|
2019-12-25 07:24:30 +01:00
|
|
|
DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true},
|
2019-12-09 07:48:18 +01:00
|
|
|
DBColumn{ID: 2, Name: "full_name", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 3, Name: "phone", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 4, Name: "avatar", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 5, Name: "email", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 6, Name: "encrypted_password", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 7, Name: "reset_password_token", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 8, Name: "reset_password_sent_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 9, Name: "remember_created_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 10, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 11, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}},
|
|
|
|
[]DBColumn{
|
2019-12-25 07:24:30 +01:00
|
|
|
DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true},
|
2019-12-09 07:48:18 +01:00
|
|
|
DBColumn{ID: 2, Name: "name", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 3, Name: "description", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 4, Name: "price", Type: "numeric(7,2)", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 5, Name: "user_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "users", FKeyColID: []int16{1}},
|
|
|
|
DBColumn{ID: 6, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 7, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 8, Name: "tsv", Type: "tsvector", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
2020-01-28 06:26:53 +01:00
|
|
|
DBColumn{ID: 9, Name: "tags", Type: "text[]", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tags", FKeyColID: []int16{3}, Array: true},
|
|
|
|
DBColumn{ID: 9, Name: "tag_count", Type: "json", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tag_count", FKeyColID: []int16{}}},
|
2019-12-09 07:48:18 +01:00
|
|
|
[]DBColumn{
|
2019-12-25 07:24:30 +01:00
|
|
|
DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true},
|
2019-12-09 07:48:18 +01:00
|
|
|
DBColumn{ID: 2, Name: "customer_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "customers", FKeyColID: []int16{1}},
|
|
|
|
DBColumn{ID: 3, Name: "product_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "products", FKeyColID: []int16{1}},
|
|
|
|
DBColumn{ID: 4, Name: "sale_type", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 5, Name: "quantity", Type: "integer", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 6, Name: "due_date", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 7, Name: "returned", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}},
|
|
|
|
[]DBColumn{
|
2019-12-25 07:24:30 +01:00
|
|
|
DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true},
|
2019-12-09 07:48:18 +01:00
|
|
|
DBColumn{ID: 2, Name: "name", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false},
|
|
|
|
DBColumn{ID: 3, Name: "slug", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false}},
|
2020-01-28 06:26:53 +01:00
|
|
|
[]DBColumn{
|
|
|
|
DBColumn{ID: 1, Name: "tag_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tags", FKeyColID: []int16{1}},
|
|
|
|
DBColumn{ID: 2, Name: "count", Type: "int", NotNull: false, PrimaryKey: false, UniqueKey: false}},
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tables {
|
|
|
|
tables[i].Key = strings.ToLower(tables[i].Name)
|
|
|
|
for n := range columns[i] {
|
|
|
|
columns[i][n].Key = strings.ToLower(columns[i][n].Name)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
schema := &DBSchema{
|
2019-12-02 16:52:22 +01:00
|
|
|
ver: 110000,
|
|
|
|
t: make(map[string]*DBTableInfo),
|
|
|
|
rm: make(map[string]map[string]*DBRel),
|
2019-10-26 07:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
aliases := map[string][]string{
|
|
|
|
"users": []string{"mes"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, t := range tables {
|
2019-12-09 07:48:18 +01:00
|
|
|
err := schema.addTable(t, columns[i], aliases)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, t := range tables {
|
|
|
|
err := schema.updateRelationships(t, columns[i])
|
|
|
|
if err != nil {
|
2019-11-28 07:25:46 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 07:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vars := NewVariables(map[string]string{
|
2019-11-21 08:14:12 +01:00
|
|
|
"admin_account_id": "5",
|
2019-10-26 07:34:29 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
pcompile = NewCompiler(Config{
|
|
|
|
Schema: schema,
|
|
|
|
Vars: vars,
|
|
|
|
})
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileGQLToPSQL(gql string, vars Variables, role string) ([]byte, error) {
|
|
|
|
qc, err := qcompile.Compile([]byte(gql), role)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, sqlStmt, err := pcompile.CompileEx(qc, vars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Println(string(sqlStmt))
|
|
|
|
|
|
|
|
return sqlStmt, nil
|
|
|
|
}
|