Add support for SQL in variables

This commit is contained in:
Vikram Rangnekar 2020-02-29 10:35:35 +05:30
parent c82a7bff0d
commit a1fb89b762
3 changed files with 19 additions and 25 deletions

View File

@ -1127,9 +1127,15 @@ func (c *compilerContext) renderVal(ex *qcode.Exp, vars map[string]string, col *
switch ex.Type {
case qcode.ValVar:
if val, ok := vars[ex.Val]; ok {
val, ok := vars[ex.Val]
switch {
case ok && strings.HasPrefix(val, "sql:"):
io.WriteString(c.w, ` (`)
io.WriteString(c.w, val[4:])
io.WriteString(c.w, `)`)
case ok:
squoted(c.w, val)
} else {
default:
io.WriteString(c.w, ` '{{`)
io.WriteString(c.w, ex.Val)
io.WriteString(c.w, `}}'`)

View File

@ -57,8 +57,6 @@ func argMap(ctx context.Context, vars []byte) func(w io.Writer, tag string) (int
return w.Write(v1)
}
fmt.Println(">>>", tag, string(v))
return w.Write(escQuote(fields[0].Value))
}
}

View File

@ -125,7 +125,9 @@ database:
# Define additional variables here to be used with filters
variables:
admin_account_id: "5"
#admin_account_id: "5"
admin_account_id: "sql:select id from users where admin = true limit 1"
# Field and table names that you wish to block
blocklist:
@ -168,26 +170,14 @@ tables:
table: users
roles_query: "SELECT * FROM users WHERE id = $user_id"
#roles_query: "SELECT * FROM users WHERE id = $user_id"
roles:
- name: anon
tables:
- name: products
limit: 10
- name: users
query:
columns: ["id", "name", "description" ]
aggregation: false
insert:
block: false
update:
block: false
delete:
block: false
limit: 10
- name: user
tables:
@ -215,8 +205,8 @@ roles:
delete:
block: true
- name: admin
match: id = 1000
tables:
- name: users
filters: []
# - name: admin
# match: id = 1000
# tables:
# - name: users
# filters: []