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 { switch ex.Type {
case qcode.ValVar: 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) squoted(c.w, val)
} else { default:
io.WriteString(c.w, ` '{{`) io.WriteString(c.w, ` '{{`)
io.WriteString(c.w, ex.Val) io.WriteString(c.w, ex.Val)
io.WriteString(c.w, `}}'`) 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) return w.Write(v1)
} }
fmt.Println(">>>", tag, string(v))
return w.Write(escQuote(fields[0].Value)) return w.Write(escQuote(fields[0].Value))
} }
} }

View File

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