|
8 | 8 | import org.fugerit.java.core.db.dao.FieldList; |
9 | 9 | import org.fugerit.java.core.lang.helpers.StringUtils; |
10 | 10 |
|
| 11 | +/** |
| 12 | + * |
| 13 | + * Query helper for select operations. |
| 14 | + * |
| 15 | + * {@link QueryHelper} for basic informations. |
| 16 | + * |
| 17 | + * @author Matteo Franci a.k.a. Fugerit |
| 18 | + * |
| 19 | + */ |
11 | 20 | public class SelectHelper extends QueryHelper { |
12 | 21 |
|
| 22 | + /** |
| 23 | + * Constant for empty string |
| 24 | + */ |
13 | 25 | public static final String MODE_NONE = ""; |
14 | 26 |
|
| 27 | + /** |
| 28 | + * Constant for 'AND' |
| 29 | + */ |
15 | 30 | public static final String MODE_AND = " AND "; |
16 | 31 |
|
| 32 | + /** |
| 33 | + * Constant for 'OR' |
| 34 | + */ |
17 | 35 | public static final String MODE_OR = " OR "; |
18 | 36 |
|
| 37 | + /** |
| 38 | + * Constant for '=' |
| 39 | + */ |
19 | 40 | public static final String COMPARE_EQUAL = " = "; |
20 | 41 |
|
| 42 | + /** |
| 43 | + * Constant for '<>' |
| 44 | + */ |
21 | 45 | public static final String COMPARE_DIFFERENT = " <> "; |
22 | 46 |
|
| 47 | + |
| 48 | + /** |
| 49 | + * Constant for '<' |
| 50 | + */ |
23 | 51 | public static final String COMPARE_LT = " < "; |
24 | 52 |
|
| 53 | + /** |
| 54 | + * Constant for '>' |
| 55 | + */ |
25 | 56 | public static final String COMPARE_GT = " > "; |
26 | 57 |
|
| 58 | + /** |
| 59 | + * Constant for 'LIKE' |
| 60 | + */ |
27 | 61 | public static final String COMPARE_LIKE = " LIKE "; |
28 | 62 |
|
| 63 | + /** |
| 64 | + * Constant for '<=' |
| 65 | + */ |
29 | 66 | public static final String COMPARE_LT_EQUAL = " <= "; |
30 | | - |
| 67 | + |
| 68 | + /** |
| 69 | + * Constant for '>=' |
| 70 | + */ |
31 | 71 | public static final String COMPARE_GT_EQUAL = " >= "; |
32 | | - |
| 72 | + |
| 73 | + /** |
| 74 | + * Constant for 'ASC' |
| 75 | + */ |
33 | 76 | public static final String ORDER_ASC = "ASC"; |
34 | 77 |
|
| 78 | + /** |
| 79 | + * Constant for 'DESC' |
| 80 | + */ |
35 | 81 | public static final String ORDER_DESC = "DESC"; |
36 | 82 |
|
37 | 83 | /** |
@@ -61,54 +107,187 @@ public static SelectHelper newCustomSelectHelper( String table, FieldList fl, bo |
61 | 107 | return helper; |
62 | 108 | } |
63 | 109 |
|
| 110 | + /** |
| 111 | + * Constructor for a SelectHelper |
| 112 | + * |
| 113 | + * @param table the table to be wrapped |
| 114 | + * @param fl the field list |
| 115 | + */ |
64 | 116 | public SelectHelper( String table, FieldList fl ) { |
65 | 117 | super( table, fl ); |
66 | 118 | this.firstParam = true; |
67 | 119 | this.orderByList = new ArrayList<>(); |
68 | 120 | } |
69 | 121 |
|
| 122 | + /** |
| 123 | + * Initialize this helper. |
| 124 | + * |
| 125 | + */ |
70 | 126 | public void initSelectEntity() { |
71 | 127 | this.getQuery().append( "SELECT * FROM " ); |
72 | 128 | this.getQuery().append( this.getTable() ); |
73 | 129 | } |
74 | 130 |
|
75 | | - public boolean addParam( String columnName, Object value, String mode, String compare ) { |
| 131 | + /** |
| 132 | + * If no field has been set, WHERE will be added, otherwise 'mode' will be added. |
| 133 | + * |
| 134 | + * @param mode the mode to be added ( AND, OR ) |
| 135 | + * @return <code>true</code> if WHERE has been added, <code>false</code> otherwise. |
| 136 | + * |
| 137 | + * @since 8.0.1 |
| 138 | + */ |
| 139 | + public boolean addWhereOrMode( String mode ) { |
| 140 | + boolean res = this.firstParam; |
| 141 | + if ( this.firstParam ) { |
| 142 | + this.firstParam = false; |
| 143 | + this.getQuery().append( " WHERE " ); |
| 144 | + } else { |
| 145 | + this.getQuery().append( mode ); |
| 146 | + } |
| 147 | + return res; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Add a parameter to the query. |
| 152 | + * The parameter is added if and only if the value is not null. |
| 153 | + * If you want to add a null comparison use addNullComparison() method() |
| 154 | + * |
| 155 | + * @param columnName the name of the column |
| 156 | + * @param value the value |
| 157 | + * @param mode the mode (AND, OR) |
| 158 | + * @param compare the comparison type ( = , <>, >, <, <=, >=, LIKE ) |
| 159 | + * @param parameter the parameter (for example section '?' or 'UPPER(?)') |
| 160 | + * @return <code>true</code> if the parameter has been added, <code>false</code> otherwise. |
| 161 | + * |
| 162 | + * @since 8.0.1 |
| 163 | + */ |
| 164 | + public boolean addParam( String columnName, Object value, String mode, String compare, String parameter ) { |
76 | 165 | boolean added = (value != null); |
77 | 166 | if ( added ) { |
78 | | - if ( this.firstParam ) { |
79 | | - this.firstParam = false; |
80 | | - this.getQuery().append( " WHERE " ); |
81 | | - } else { |
82 | | - this.getQuery().append( mode ); |
83 | | - } |
| 167 | + this.addWhereOrMode(mode); |
84 | 168 | this.getQuery().append( columnName ); |
85 | 169 | this.getQuery().append( compare ); |
86 | | - this.getQuery().append( QUESTION_MARK ); |
| 170 | + this.getQuery().append( parameter ); |
87 | 171 | this.getFields().addField( value ); |
88 | 172 | } |
89 | 173 | return added; |
90 | 174 | } |
| 175 | + |
| 176 | + /** |
| 177 | + * Add a NULL check to the query. |
| 178 | + * |
| 179 | + * @param columnName the name of the column |
| 180 | + * @param isNull <code>true</code> to add 'IS NULL' check, <code>false</code> to add 'IS NOT NULL' check. |
| 181 | + * @param mode the mode (AND, OR) |
| 182 | + * @return <code>true</code> if the parameter has been added, <code>false</code> otherwise. |
| 183 | + * |
| 184 | + * @since 8.0.1 |
| 185 | + */ |
| 186 | + public boolean addNullComparison( String columnName, String mode, boolean isNull ) { |
| 187 | + boolean res = true; |
| 188 | + this.addWhereOrMode(mode); |
| 189 | + this.getQuery().append( columnName ); |
| 190 | + if ( isNull ) { |
| 191 | + this.getQuery().append( " IS NULL " ); |
| 192 | + } else { |
| 193 | + this.getQuery().append( " IS NOT NULL " ); |
| 194 | + } |
| 195 | + return res; |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + /** |
| 200 | + * Add a parameter to the query. |
| 201 | + * The parameter is added if and only if the value is not null. |
| 202 | + * If you want to add a null comparison use addNullComparison() method() |
| 203 | + * The parameter will be added as a parameter (?) for prepared statement |
| 204 | + * |
| 205 | + * @param columnName the name of the column |
| 206 | + * @param value the value |
| 207 | + * @param mode the mode (AND, OR) |
| 208 | + * @param compare the comparison type ( = , <>, >, <, <=, >=, LIKE ) |
| 209 | + * @return <code>true</code> if the parameter has been added, <code>false</code> otherwise. |
| 210 | + * |
| 211 | + */ |
| 212 | + public boolean addParam( String columnName, Object value, String mode, String compare ) { |
| 213 | + return this.addParam(columnName, value, mode, compare, QUESTION_MARK); |
| 214 | + } |
91 | 215 |
|
| 216 | + /** |
| 217 | + * Add a parameter to the query. |
| 218 | + * The parameter is added if and only if the value is not null. |
| 219 | + * If you want to add a null comparison use addNullComparison() method(). |
| 220 | + * The parameter will be added as a parameter (?) for prepared statement. |
| 221 | + * The mode will be OR and the comparison = |
| 222 | + * |
| 223 | + * @param columnName the name of the column |
| 224 | + * @param value the value |
| 225 | + * @return <code>true</code> if the parameter has been added, <code>false</code> otherwise. |
| 226 | + */ |
92 | 227 | public boolean orEqualParam( String columnName, Object value ) { |
93 | 228 | return this.addParam( columnName , value, MODE_OR, COMPARE_EQUAL ); |
94 | 229 | } |
95 | | - |
| 230 | + |
| 231 | + /** |
| 232 | + * Add a parameter to the query. |
| 233 | + * The parameter is added if and only if the value is not null. |
| 234 | + * If you want to add a null comparison use addNullComparison() method(). |
| 235 | + * The parameter will be added as a parameter (?) for prepared statement. |
| 236 | + * The mode will be OR and the comparison LIKE |
| 237 | + * |
| 238 | + * @param columnName the name of the column |
| 239 | + * @param value the value |
| 240 | + * @return <code>true</code> if the parameter has been added, <code>false</code> otherwise. |
| 241 | + */ |
96 | 242 | public boolean orLikeParam( String columnName, Object value ) { |
97 | 243 | return this.addParam( columnName , value, MODE_OR, COMPARE_LIKE ); |
98 | 244 | } |
99 | 245 |
|
| 246 | + /** |
| 247 | + * Add a parameter to the query. |
| 248 | + * The parameter is added if and only if the value is not null. |
| 249 | + * If you want to add a null comparison use addNullComparison() method(). |
| 250 | + * The parameter will be added as a parameter (?) for prepared statement. |
| 251 | + * The mode will be AND and the comparison = |
| 252 | + * |
| 253 | + * @param columnName the name of the column |
| 254 | + * @param value the value |
| 255 | + * @return <code>true</code> if the parameter has been added, <code>false</code> otherwise. |
| 256 | + */ |
100 | 257 | public boolean andEqualParam( String columnName, Object value ) { |
101 | 258 | return this.addParam( columnName , value, MODE_AND, COMPARE_EQUAL ); |
102 | 259 | } |
103 | 260 |
|
| 261 | + /** |
| 262 | + * Add a parameter to the query. |
| 263 | + * The parameter is added if and only if the value is not null. |
| 264 | + * If you want to add a null comparison use addNullComparison() method(). |
| 265 | + * The parameter will be added as a parameter (?) for prepared statement. |
| 266 | + * The mode will be AND and the comparison LIKE |
| 267 | + * |
| 268 | + * @param columnName the name of the column |
| 269 | + * @param value the value |
| 270 | + * @return <code>true</code> if the parameter has been added, <code>false</code> otherwise. |
| 271 | + */ |
104 | 272 | public boolean andLikeParam( String columnName, Object value ) { |
105 | 273 | return this.addParam( columnName , value, MODE_AND, COMPARE_LIKE ); |
106 | 274 | } |
107 | 275 |
|
| 276 | + /** |
| 277 | + * Add ORDER BY to the query. |
| 278 | + * |
| 279 | + * @param columnName the column name |
| 280 | + * @param orderByMode the order mode (ASC, DESC) |
| 281 | + */ |
108 | 282 | public void addOrderBy( String columnName, String orderByMode ) { |
109 | 283 | this.orderByList.add( new OrderByHandler(columnName, orderByMode) ); |
110 | 284 | } |
111 | 285 |
|
| 286 | + /** |
| 287 | + * Directly add ORDER BY to the query. |
| 288 | + * |
| 289 | + * @param orderBy string "ORDER BY ${orderBy}" will be added to the query buffer. |
| 290 | + */ |
112 | 291 | public void addOrderBy( String orderBy ) { |
113 | 292 | if ( StringUtils.isNotEmpty( orderBy ) ) { |
114 | 293 | this.appendToQuery( " ORDER BY " ); |
|
0 commit comments