- Update docs for custom where query (#36)
parent
4e9dbe10d0
commit
6d97f17e20
|
@ -65,6 +65,11 @@ Mapper构造时很简单,模板参数就是你要存取的Model的类型,构
|
|||
|
||||
上一节中,很多接口都需要输入条件对象参数,条件对象是Criteria类的实例,表示某种where条件,比如某个字段大于、等于、小于某个给定值,或者isNull之类的条件。
|
||||
|
||||
```c++
|
||||
template <typename T>
|
||||
Criteria(const std::string &colName, const CompareOperator &opera, T &&arg)
|
||||
```
|
||||
|
||||
条件对象的构造函数很简单,一般第一个参数是字段名,第二个参数是表示比较类型的枚举值,第三个字段是被比较的值。如果比较类型是IsNull或IsNotNull,不需要第三个参数。
|
||||
|
||||
比如:
|
||||
|
@ -81,6 +86,28 @@ Criteria(Users::Cols::_user_id,CompareOperator::EQ,1);
|
|||
|
||||
跟前面的写法等效,但是这个写法可以有助于编辑器的自动提示,效率更高并且不易出错;
|
||||
|
||||
Criteria类还提供了一个自定义构造函数,可以表示自定义的where条件。
|
||||
|
||||
```c++
|
||||
template <typename... Arguments>
|
||||
explicit Criteria(const CustomSql &sql, Arguments &&...args)
|
||||
```
|
||||
|
||||
构造函数的第一个参数是一个包含了占位符`$?`的`CustomSql`对象,而`CustomSql`类只是一个std::string的包装类。第二个不定参数代表绑定的参数,其行为于[execSqlAsync](CHN-08-1-数据库-Dbclient.md#execsqlasync)中的不定参数一致。
|
||||
|
||||
比如:
|
||||
```c++
|
||||
Criteria(CustomSql("tags @> $?"), "cloud");
|
||||
```
|
||||
|
||||
`CustomSql`类还拥有一个与之关联的自定义字面量,因此我们更推荐写成下面这样:
|
||||
|
||||
```c++
|
||||
Criteria("tags @> $?"_sql, "cloud");
|
||||
```
|
||||
|
||||
这条语句跟前面的写法等效。
|
||||
|
||||
条件对象支持与和或的运算,两个条件对象的与和或会构造出新的条件对象,这样可以方便的构造嵌套的where条件。比如:
|
||||
|
||||
```c++
|
||||
|
|
|
@ -65,9 +65,14 @@ Like DbClient, Mapper also provides asynchronous and synchronous interfaces. The
|
|||
|
||||
In the previous section, many interfaces required input criteria object parameters. The criteria object is an instance of the Criteria class, indicating a certain condition, such as a field greater than, equal to, less than a given value, or a condition such as `is Null`.
|
||||
|
||||
```c++
|
||||
template <typename T>
|
||||
Criteria(const std::string &colName, const CompareOperator &opera, T &&arg)
|
||||
```
|
||||
|
||||
The constructor of a criteria object is very simple. Generally, the first argument is the name of the field, the second argument is the enumeration value representing the comparison type, and the third argument is the value being compared. If the comparison type is IsNull or IsNotNull, the third parameter is not required.
|
||||
|
||||
E.g:
|
||||
E.g:
|
||||
|
||||
```c++
|
||||
Criteria("user_id",CompareOperator::EQ,1);
|
||||
|
@ -81,6 +86,29 @@ Criteria(Users::Cols::_user_id,CompareOperator::EQ,1);
|
|||
|
||||
This is equivalent to the previous one, but this can use the editor's automatic prompts, which is more efficient and less prone to errors;
|
||||
|
||||
The Criteria class also supports custom where conditions along with a custom constructor.
|
||||
|
||||
```c++
|
||||
template <typename... Arguments>
|
||||
explicit Criteria(const CustomSql &sql, Arguments &&...args)
|
||||
```
|
||||
|
||||
The first argument is a `CustomSql` object of sql statements with `$?` placeholders, while the `CustomSql` class is just a wrapper of a std::string. The second indefinite argument is a parameter pack represents the bound parameter, which behaves just like the ones in [execSqlAsync](ENG-08-1-DataBase-DbClient.md#execSqlAsync).
|
||||
|
||||
E.g:
|
||||
|
||||
```c++
|
||||
Criteria(CustomSql("tags @> $?"), "cloud");
|
||||
```
|
||||
|
||||
The `CustomSql` class also has a related user-defined string literal, so we recommend to write the following instead:
|
||||
|
||||
```c++
|
||||
Criteria("tags @> $?"_sql, "cloud");
|
||||
```
|
||||
|
||||
This is equivalent to the previous one.
|
||||
|
||||
Criteria objects support AND and OR operations. The sum of two criteria objects constructs a new criteria object, which makes it easy to construct nested conditions. For example:
|
||||
|
||||
```c++
|
||||
|
|
Loading…
Reference in New Issue