didi / gendry

a golang library for sql builder
Apache License 2.0
1.6k stars 191 forks source link

NamedQuery 支持嵌套 map[string]interface{}, 实现过滤条件 #116

Open xiao-xiao-xiao opened 3 years ago

xiao-xiao-xiao commented 3 years ago
/*
{
    sql: `select {{foo}},{{bar}} from tb where address in {{addr}} and {{where}}`,
        data: map[string]interface{}{
        "foo":  "f1",
        "bar":  "f2",
        "addr": []string{"beijing", "shanghai", "chengdu"},
        "where": map[string]interface{}{
            "name in": []string{"1", "2"},
                         "age": 10,
         },
    },
    cond: `select ?,? from tb where address in (?,?,?) and (age=? AND name IN (?,?))`,,
    vals: []interface{}{"f1", "f2", "beijing", "shanghai", "chengdu", 10, "1", "2"},
}
*/

where := map[string]interface{}{
        "foo":  "f1",
        "bar":  "f2",
        "addr": []string{"beijing", "shanghai", "chengdu"},
        "where": map[string]interface{}{
            "name in": []string{"1", "2"},
                          "age": 10,
         },
    },

where["where"] = builder.Omity(where["where"],  []string{"age"})
// 使用此种方式来实现,复杂SQL, 部分条件需要过滤