Skip to content

Commit 4987a07

Browse files
committed
Documentation
1 parent 18cdceb commit 4987a07

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

src/site/markdown/docs/extending.md

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Extending MyBatis Dynamic SQL
22

3-
The library has been designed for extension from the very start of the design. We do not believe that the library
3+
The library has been designed for extension from the very beginning. We do not believe that the library
44
covers all possible uses, and we wanted to make it possible to add functionality that suits the needs of different
55
projects.
66

@@ -11,10 +11,18 @@ The SELECT support is the most complex part of the library, and also the part of
1111
extended. There are two main interfaces involved with extending the SELECT support. Picking which interface to
1212
implement is dependent on how you want to use your extension.
1313

14-
| Interface | Purpose |
15-
|------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
16-
| `org.mybatis.dynamic.sql.BasicColumn` | Use this interface if you want to add capabilities to a SELECT list or a GROUP BY expression. For example, creating a calculated column. |
17-
| `org.mybatis.dynamic.sql.BindableColumn` | Use this interface if you want to add capabilities to a WHERE clause. For example, creating a custom condition. |
14+
| Interface | Purpose |
15+
|------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
16+
| `org.mybatis.dynamic.sql.BasicColumn` | Use this interface if you want to add capabilities to a SELECT list or a GROUP BY expression. For example, using a database function. |
17+
| `org.mybatis.dynamic.sql.BindableColumn` | Use this interface if you want to add capabilities to a WHERE clause. For example, creating a custom condition. |
18+
19+
Rendering is the process of generating an appropriate SQL fragment to implement the function or calculated column.
20+
The library will call a method `render(RenderingContext)` in your implementation. This method should return an
21+
instance of `FragmentAndParameters` containing your desired SQL fragment and any bind parameters needed. Bind
22+
parameter markers can be calculated by calling the `RenderingContext.calculateParameterInfo()` method. That method will
23+
return a properly formatted bind marker for the SQL string, and a matching Map key you should use in your parameter map.
24+
In general, you do not need to worry about adding spacing, commas, etc. before or after your fragment - the library
25+
will properly format the final statement from all the different fragments.
1826

1927
See the following sections for examples.
2028

@@ -166,6 +174,37 @@ public class Upper extends AbstractUniTypeFunction<String, Upper> {
166174
}
167175
```
168176

177+
Note that `FragmentAndParameters` has a utility method that can simplify the implementation if you do not need to
178+
add any new parameters to the resulting fragment. For example, the UPPER function can be simplified as follows:
179+
180+
```java
181+
import org.mybatis.dynamic.sql.render.RenderingContext;
182+
import org.mybatis.dynamic.sql.select.function.AbstractUniTypeFunction;
183+
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
184+
185+
public class Upper extends AbstractUniTypeFunction<String, Upper> {
186+
187+
private Upper(BindableColumn<String> column) {
188+
super(column);
189+
}
190+
191+
@Override
192+
public FragmentAndParameters render(RenderingContext renderingContext) {
193+
return = column.render(renderingContext).mapFragment(f -> "upper(" + f + ")"); //$NON-NLS-1$ //$NON-NLS-2$
194+
}
195+
196+
@Override
197+
protected Upper copy() {
198+
return new Upper(column);
199+
}
200+
201+
public static Upper of(BindableColumn<String> column) {
202+
return new Upper(column);
203+
}
204+
}
205+
```
206+
207+
169208
### OperatorFunction Example
170209

171210
The following function implements the concatenate operator. Note that the operator can be applied to list of columns of

0 commit comments

Comments
 (0)