|
| 1 | +# Exceptions Thrown by the Library |
| 2 | + |
| 3 | +The library will throw runtime exceptions in a variety of cases - most often when invalid SQL is detected. |
| 4 | + |
| 5 | +All exceptions are derived from `org.mybatis.dynamic.sql.exception.DynamicSqlException` which is, in turn, |
| 6 | +derived from `java.lang.RuntimeException`. |
| 7 | + |
| 8 | +The most important exceptions to think about are `InvalidSQLException` and `NonRenderingWhereClauseException`. We |
| 9 | +provide details about those exceptions below. |
| 10 | + |
| 11 | +## Invalid SQL Detection |
| 12 | + |
| 13 | +The library makes an effort to prevent the generation of invalid SQL. If invalid SQL is detected, the library will |
| 14 | +throw `InvalidSQLException` or one of it's derived exceptions. Invalid SQL can happen in different ways: |
| 15 | + |
| 16 | +1. Misuse of the DSL. For example, the DSL allows you to build an update statement with no "set" clauses. |
| 17 | + Even though technically allowed by the DSL, this would produce invalid SQL. |
| 18 | +2. Misuse of the Kotlin DSL. The Kotlin DSL provides a lot of flexibility for building statements and looks very close |
| 19 | + to native SQL, but that flexibility can be misused. For example, the Kotlin DSL would allow you to write an insert |
| 20 | + statement without an "into" clause. |
| 21 | +3. More common is a case when using the optional mappings in an insert or update statement. It is possible |
| 22 | + that all mappings would fail to render which would produce invalid SQL. For example, in a general insert statement |
| 23 | + you could specify many "set column to value when present" mappings that all had null values. All mappings would fail |
| 24 | + to render in that case which would cause invalid SQL. |
| 25 | + |
| 26 | +All of these exceptions can be avoided through proper use of the DSL and validation of input values. |
| 27 | + |
| 28 | +## Non Rendering Where Clauses |
| 29 | + |
| 30 | +Most conditions in a where clause provide optionality - they have `filter` methods that can cause the condition to be |
| 31 | +dropped from the where clause. If all the conditions in a where clause fail to render, then the where clause itself is |
| 32 | +dropped from the rendered SQL. This can be dangerous in that it can cause a statement to be generated that affects all |
| 33 | +rows in a table. For example, all rows could be deleted. As of version 1.4.1, the library will throw a |
| 34 | +`NonRenderingWhereClauseException` in this case out of an abundance of caution. This behavior can be overridden |
| 35 | +through either global configuration, or by configuring individual statements to allow for where clauses to be dropped. |
| 36 | + |
| 37 | +The important idea is that there are legitimate cases when it is reasonable to allow a where clause to not render, but |
| 38 | +the decision to allow that should be very intentional. See the "Configuration of the Library" page for further details. |
| 39 | + |
| 40 | +The exception will only be thrown if a where clause is coded but fails to render. If you do not code a where clause in |
| 41 | +a statement, then we assume that you intend for all rows to be affected. |
| 42 | + |
| 43 | +## Exception Details |
| 44 | + |
| 45 | +Details of the different exceptions follows: |
| 46 | + |
| 47 | +| Exception | Causes | |
| 48 | +|----------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 49 | +| `org.mybatis.dynamic.sql.exception.DuplicateTableAliasException` | Thrown if you attempt to join more than one table with the same alias in a select statement | |
| 50 | +| `org.mybatis.dynamic.sql.exception.DynamicSQLException` | Thrown when other more specific exceptions are not appropriate. One example is when reading a configuration property file causes an IOException. This is a rare occurrence. | |
| 51 | +| `org.mybatis.dynamic.sql.exception.InvalidSQLException` | Thrown if invalid SQL is detected. The most common causes are when all the optional column mappings in an insert or update statement fail to render. | |
| 52 | +| `org.mybatis.dynamic.sql.exception.NonRenderingWhereClauseException` | Thrown if all conditions in a where clause fail to render - which will cause the where clause to be dropped from the rendered SQL. This could cause a statement to inadvertently affect all rows in a table. This behavior can be changed with global or statement configuration. | |
| 53 | +| `org.mybatis.dynamic.sql.util.kotlin.KInvalidSqlException` | Thrown if invalid SQL is detected when using the Kotlin DSL. This exception is for specific misuses of the Kotlin DSL. It is derived from `InvalidSQLException` which can also occur when using the Kotlin DSL. | |
0 commit comments