You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/site/markdown/docs/extending.md
+82-33Lines changed: 82 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ A calculated column can be used anywhere in a SELECT statement. If you don't ne
21
21
```java
22
22
publicclassCountAllimplementsBasicColumn {
23
23
24
-
privateOptional<String> alias=Optional.empty();
24
+
privateString alias;
25
25
26
26
publicCountAll() {
27
27
super();
@@ -34,13 +34,13 @@ public class CountAll implements BasicColumn {
34
34
35
35
@Override
36
36
publicOptional<String>alias() {
37
-
return alias;
37
+
returnOptional.ofNullable(alias);
38
38
}
39
39
40
40
@Override
41
41
publicCountAllas(Stringalias) {
42
42
CountAll copy =newCountAll();
43
-
copy.alias =Optional.of(alias);
43
+
copy.alias = alias;
44
44
return copy;
45
45
}
46
46
}
@@ -52,64 +52,113 @@ This class is used to implement the `count(*)` function in a SELECT list. There
52
52
2.`as` - this method can be called by the user to add an alias to the column expression. In the method you should return a new instance of the object, with the alias passed by the user.
53
53
3.`alias` - this method is called by the default renderer to obtain the column alias for the select list. If there is no alias, then returning Optional.empty() will disable setting a column alias.
54
54
55
-
###Writing a Custom Where Condition
55
+
## Writing Custom Functions
56
56
57
-
If you want to use your calculated column in a WHERE clause in addition the select list and the GROUP BY clause, then you must implement `org.mybatis.dynamic.sql.BindableColumn`. This interface extends the `BasicColumn` interface from above and adds two methods. An example from the library is the `org.mybatis.dynamic.sql.select.function.Add` class:
57
+
Relational database vendors provide hundreds of functions in their SQL dialects to aid with queries and offload processing to the database servers. This library does not try to implement every function that exists. This library also does not provide any abstraction over the different functions on different databases. For example, bitwise operator support is non-standard and it would be difficult to provide a function in this library that worked on every database. So we take the approach of supplying examples for a few very common functions, and making it relatively easy to write your own functions.
58
+
59
+
The supplied functions are all in the `org.mybatis.dynamic.sql.select.function` package. They are all implemented as `BindableColumn` - meaning that they can appear in a select list or a where clause.
60
+
61
+
We provide some base classes that you can easily extend to write functions of your own. Those classes are as follows:
62
+
63
+
Note: the base classes are all in the `org.mybatis.dynamic.sql.select.function` package.
64
+
65
+
| Interface | Purpose|
66
+
|-----------|--------|
67
+
|`o.m.d.s.s.f.AbstractTypeConvertingFunction`| Extend this class if you want to build a function that changes a column data type. For example, using a database function to calculate the Base64 String for a binary field. |
68
+
|`o.m.d.s.s.f.AbstractUniTypeFunction`| Extend this class if you want to build a function that does not change a column data type. For example UPPER(), LOWER(), etc. |
69
+
|`o.m.d.s.s.f.OperatorFunction`| Extend this class if you want to build a function the implements an operator. For example column1 + column2. |
70
+
71
+
### AbstractTypeConvertingFunction Example
72
+
73
+
The following function uses HSQLDB's `TO_BASE64` function to calculate the BASE64 string for a binary field. Note that the function changes the data type from `byte[]` to `String`.
This class implements the idea of adding two numeric columns together in a SELECT statement. This class accepts two other columns as parameters and then overrides `renderWithTableAlias` to render the addition. The `alias` and `as` methods work as described above.
139
+
### OperatorFunction Example
107
140
108
-
The two additional methods are:
141
+
The following function implements the concatenate operator. Note that the operator can be applied to list of columns of arbitrary length:
109
142
110
-
1.`jdbcType` - returns the JDBC Type of the column for the WHERE clause. This is used by the MyBatis3 rendering strategy to render a full MyBatis parameter expression.
111
-
2.`typeHandler` - returns a type handler if specified by the user. Again, this is used by the MyBatis3 rendering strategy to render a full MyBatis parameter expression.
The library supplies implementations for several common database functions. We do not try to implement a large set of functions. Rather we supply some common functions as examples and make it relatively easy to write your own implementations of functions you might want to use that are not supplied by the library. See the page "Extending the Library" for informtion about how to write your own functions.
4
+
5
+
The supplied functions are all in the `org.mybatis.dynamic.sql.select.function` package. In addition, there are static methods in the `SqlBuilder` to access all functions.
6
+
7
+
In the following table...
8
+
9
+
- "Function Class" is implementing class in the `org.mybatis.dynamic.sql.select.function` package
10
+
- "Example" shows the static method from the `SqlBuilder` class
11
+
- "Rendered Result" shows the rendered SQL fragment generated by the function
Note especially the `OperatorFunction` - you can use this function to easily implement operators supported by your database. For example, MySQL supports a number of bitwise operators that can be easily implemented with this function.
0 commit comments