OSDN Git Service

accessにつながるようになった気がする
[ea2ddl/ea2ddl.git] / ea2ddl-gen / dbflute / templates / om / java / allcommon / s2dao / internal / sqlhandler / InternalBasicHandler.vm
1 ${database.allClassCopyright}package ${glPackageBaseCommonS2DaoInternalSqlHandler};\r
2 \r
3 import java.sql.Connection;\r
4 import java.sql.Statement;\r
5 import java.sql.PreparedStatement;\r
6 import java.sql.ResultSet;\r
7 import java.sql.SQLException;\r
8 \r
9 import javax.sql.DataSource;\r
10 \r
11 import org.seasar.extension.jdbc.StatementFactory;\r
12 import org.seasar.extension.jdbc.ValueType;\r
13 import org.seasar.extension.jdbc.types.ValueTypes;\r
14 import org.seasar.framework.exception.SQLRuntimeException;\r
15 \r
16 import ${glPackageBaseCommon}.${glQLog};\r
17 import ${glPackageBaseCommon}.${glDBFluteConfig};\r
18 import ${glPackageBaseCommon}.${glInternalMapContextName};\r
19 import ${glPackageBaseCommonCBean}.${glConditionBeanContextName};\r
20 import ${glPackageBaseCommonCBean}.${glConditionBeanInterfaceName};\r
21 import ${glPackageBaseCommonCBeanOutsidesql}.${glOutsideSqlContextName};\r
22 import ${glPackageBaseCommonException}.${glEntityAlreadyExistsException};\r
23 import ${glPackageBaseCommonException}.${glSQLFailureException};\r
24 import ${glPackageBaseCommonS2DaoInternalSqlLog}.${glInternalSqlLogRegistry};\r
25 import ${glPackageBaseCommonS2DaoInternalUtil}.${glInternalBindVariableUtil};\r
26 import ${glPackageBaseCommonUtil}.${glSimpleSystemUtil};\r
27 \r
28 /**\r
29  * @author ${database.ClassAuthor}\r
30  */\r
31 public class ${glInternalBasicHandler} {\r
32 \r
33     // ===================================================================================\r
34     //                                                                           Attribute\r
35     //                                                                           =========\r
36     private DataSource dataSource;\r
37     private String sql;\r
38     private StatementFactory statementFactory;\r
39     private Object[] loggingMessageSqlArgs;\r
40 \r
41         // ===================================================================================\r
42     //                                                                         Constructor\r
43     //                                                                         ===========\r
44     public ${glInternalBasicHandler}(DataSource ds, StatementFactory statementFactory) {\r
45         setDataSource(ds);\r
46         setStatementFactory(statementFactory);\r
47     }\r
48 \r
49     public ${glInternalBasicHandler}(DataSource ds, String sql, StatementFactory statementFactory) {\r
50         setDataSource(ds);\r
51                 setSql(sql);\r
52         setStatementFactory(statementFactory);\r
53     }\r
54 \r
55         // ===================================================================================\r
56     //                                                           Basic Method for SubClass\r
57     //                                                           =========================\r
58     protected void bindArgs(PreparedStatement ps, Object[] args, Class<?>[] argTypes) {\r
59         if (args == null) {\r
60             return;\r
61         }\r
62         for (int i = 0; i < args.length; ++i) {\r
63             ValueType valueType = getValueType(argTypes[i]);\r
64             try {\r
65                 valueType.bindValue(ps, i + 1, args[i]);\r
66             } catch (SQLException e) {\r
67                 handleSQLException(e, ps);\r
68             }\r
69         }\r
70     }\r
71 \r
72     protected Class<?>[] getArgTypes(Object[] args) {\r
73         if (args == null) {\r
74             return null;\r
75         }\r
76         Class<?>[] argTypes = new Class[args.length];\r
77         for (int i = 0; i < args.length; ++i) {\r
78             Object arg = args[i];\r
79             if (arg != null) {\r
80                 argTypes[i] = arg.getClass();\r
81             }\r
82         }\r
83         return argTypes;\r
84     }\r
85 \r
86     protected String getCompleteSql(Object[] args) {\r
87         return ${glInternalBindVariableUtil}.getCompleteSql(sql, args);\r
88     }\r
89 \r
90     protected String getBindVariableText(Object bindVariable) {\r
91         return ${glInternalBindVariableUtil}.getBindVariableText(bindVariable);\r
92     }\r
93 \r
94     protected ValueType getValueType(Class<?> clazz) {\r
95         return ValueTypes.getValueType(clazz);\r
96     }\r
97 \r
98     protected void logSql(Object[] args, Class<?>[] argTypes) {\r
99         if (${glQLog}.isLogEnabled() || ${glInternalSqlLogRegistry}.existsSqlLogRegistry()) {\r
100             final String completeSql = getCompleteSql(args);\r
101                         if (isContainsLineSeparatorInSql()) {\r
102                 ${glQLog}.log(getLineSeparator() + completeSql);\r
103                         } else {\r
104                 ${glQLog}.log(completeSql);\r
105                         }\r
106                     if (${glInternalSqlLogRegistry}.existsSqlLogRegistry()) {\r
107                             final Object sqlLogRegistry = ${glInternalSqlLogRegistry}.findContainerSqlLogRegistry();\r
108                                 if (sqlLogRegistry != null) {\r
109                                     ${glInternalSqlLogRegistry}.push(getSql(), completeSql, args, argTypes, sqlLogRegistry);\r
110                                 }\r
111                         }\r
112         }\r
113     }\r
114         \r
115         protected boolean isContainsLineSeparatorInSql() {\r
116             return sql != null ? sql.contains(getLineSeparator()) : false;\r
117         }\r
118 \r
119     // ===================================================================================\r
120     //                                                                   Exception Handler\r
121     //                                                                   =================\r
122     protected void handleSQLException(SQLException e, Statement statement) {\r
123         handleSQLException(e, statement, false);\r
124     }\r
125 \r
126     protected void handleSQLException(SQLException e, Statement statement, boolean uniqueConstraintValid) {\r
127         String completeSql = buildLoggingMessageSql();\r
128         new SQLExceptionHandler().handleSQLException(e, statement, uniqueConstraintValid, completeSql);\r
129     }\r
130 \r
131     protected String buildLoggingMessageSql() {\r
132         String completeSql = null;\r
133         if (sql != null && loggingMessageSqlArgs != null) {\r
134             try {\r
135                 completeSql = getCompleteSql(loggingMessageSqlArgs);\r
136             } catch (RuntimeException ignored) {\r
137             }\r
138         }\r
139         return completeSql;\r
140     }\r
141 \r
142     public static class SQLExceptionHandler {\r
143 \r
144             public void handleSQLException(SQLException e, Statement statement) {\r
145                 handleSQLException(e, statement, false);\r
146             }\r
147             \r
148             public void handleSQLException(SQLException e, Statement statement, boolean uniqueConstraintValid) {\r
149                 handleSQLException(e, statement, uniqueConstraintValid, null);\r
150             }\r
151             \r
152             public void handleSQLException(SQLException e, Statement statement, boolean uniqueConstraintValid, String completeSql) {\r
153                 if (isSqlExceptionOldStyleHandling()) {\r
154                     throw new SQLRuntimeException(e);\r
155                 }\r
156                 if (uniqueConstraintValid && isUniqueConstraintException(e)) {\r
157                     throwEntityAlreadyExistsException(e, statement, completeSql);\r
158                 }\r
159                 throwSQLFailureException(e, statement, completeSql);\r
160             }\r
161         \r
162             protected boolean isUniqueConstraintException(SQLException e) {\r
163                 ${glDBFluteConfig}.UniqueConstraintDeterminator determinator = getUniqueConstraintDeterminator();\r
164                 if (determinator != null) {\r
165                     return determinator.isUniqueConstraintException(e);\r
166                 }\r
167                 return ${glConditionBeanContextName}.isUniqueConstraintException(extractSQLState(e), e.getErrorCode());\r
168             }\r
169         \r
170             protected ${glDBFluteConfig}.UniqueConstraintDeterminator getUniqueConstraintDeterminator() {\r
171                 return ${glDBFluteConfig}.getInstance().getUniqueConstraintDeterminator();\r
172             }\r
173         \r
174             protected boolean isSqlExceptionOldStyleHandling() {\r
175                 return ${glDBFluteConfig}.getInstance().isSqlExceptionOldStyleHandling();\r
176             }\r
177         \r
178             protected void throwEntityAlreadyExistsException(SQLException e, Statement statement, String completeSql) {\r
179                 String msg = "Look! Read the message below." + getLineSeparator();\r
180                 msg = msg + "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" + getLineSeparator();\r
181                 msg = msg + "The entity already exists on the database!" + getLineSeparator();\r
182                 msg = msg + getLineSeparator();\r
183                 msg = msg + "[Advice]" + getLineSeparator();\r
184                 msg = msg + "Please confirm the primary key whether it already exists on the database." + getLineSeparator();\r
185                 msg = msg + "And confirm the unique constraint for other columns." + getLineSeparator();\r
186                 msg = msg + getLineSeparator();\r
187                 msg = msg + "[SQLState]" + getLineSeparator() + extractSQLState(e) + getLineSeparator();\r
188                 msg = msg + getLineSeparator();\r
189                 msg = msg + "[ErrorCode]" + getLineSeparator() + e.getErrorCode() + getLineSeparator();\r
190                 msg = msg + getLineSeparator();\r
191                 msg = msg + "[SQLException]" + getLineSeparator() + e.getClass().getName() + getLineSeparator();\r
192                 msg = msg + e.getMessage() + getLineSeparator();\r
193             SQLException nextEx = e.getNextException();\r
194             if (nextEx != null) {\r
195                     msg = msg + getLineSeparator();\r
196                     msg = msg + "[NextException]" + getLineSeparator();\r
197                 msg = msg + nextEx.getClass().getName() + getLineSeparator();\r
198                     msg = msg + nextEx.getMessage() + getLineSeparator();\r
199                 SQLException nextNextEx = nextEx.getNextException();\r
200                 if (nextNextEx != null) {\r
201                         msg = msg + getLineSeparator();\r
202                         msg = msg + "[NextNextException]" + getLineSeparator();\r
203                     msg = msg + nextNextEx.getClass().getName() + getLineSeparator();\r
204                         msg = msg + nextNextEx.getMessage() + getLineSeparator();\r
205                 }\r
206             }\r
207             Object invokeName = extractBehaviorInvokeName();\r
208             if (invokeName != null) {\r
209                     msg = msg + getLineSeparator();\r
210                 msg = msg + "[Behavior]" + getLineSeparator();\r
211                 msg = msg + invokeName + getLineSeparator();\r
212             }\r
213             if (hasConditionBean()) {\r
214                     msg = msg + getLineSeparator();\r
215                 msg = msg + "[ConditionBean]" + getLineSeparator();\r
216                 msg = msg + getConditionBean().getClass().getName() + getLineSeparator();\r
217             }\r
218             if (hasOutsideSqlContext()) {\r
219                     msg = msg + getLineSeparator();\r
220                 msg = msg + "[OutsideSql]" + getLineSeparator();\r
221                 msg = msg + getOutsideSqlContext().getOutsideSqlPath() + getLineSeparator();\r
222                     msg = msg + getLineSeparator();\r
223                 msg = msg + "[ParameterBean]" + getLineSeparator();\r
224                 Object pmb = getOutsideSqlContext().getParameterBean();\r
225                 if (pmb != null) {\r
226                     msg = msg + pmb.getClass().getName() + getLineSeparator();\r
227                     msg = msg + pmb + getLineSeparator();\r
228                 } else {\r
229                     msg = msg + pmb + getLineSeparator();\r
230                 }\r
231             }\r
232                 if (statement != null) {\r
233                     msg = msg + getLineSeparator();\r
234                     msg = msg + "[Statement]" + getLineSeparator();\r
235                 msg = msg + statement.getClass().getName() + getLineSeparator();\r
236                 }\r
237             if (completeSql != null) {\r
238                     msg = msg + getLineSeparator();\r
239                 msg = msg + "[Display SQL]" + getLineSeparator();\r
240                 msg = msg + completeSql + getLineSeparator();\r
241             }\r
242                 msg = msg + "* * * * * * * * * */";\r
243                 throw new ${glEntityAlreadyExistsException}(msg, e);\r
244             }\r
245         \r
246             protected void throwSQLFailureException(SQLException e, Statement statement, String completeSql) {\r
247                 String msg = "Look! Read the message below." + getLineSeparator();\r
248                 msg = msg + "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" + getLineSeparator();\r
249                 msg = msg + "The SQL failed to execute!" + getLineSeparator();\r
250                 msg = msg + getLineSeparator();\r
251                 msg = msg + "[Advice]" + getLineSeparator();\r
252                 msg = msg + "Please confirm the SQLException message." + getLineSeparator();\r
253                 msg = msg + getLineSeparator();\r
254                 msg = msg + "[SQLState]" + getLineSeparator() + extractSQLState(e) + getLineSeparator();\r
255                 msg = msg + getLineSeparator();\r
256                 msg = msg + "[ErrorCode]" + getLineSeparator() + e.getErrorCode() + getLineSeparator();\r
257                 msg = msg + getLineSeparator();\r
258                 msg = msg + "[SQLException]" + getLineSeparator() + e.getClass().getName() + getLineSeparator();\r
259                 msg = msg + e.getMessage() + getLineSeparator();\r
260             SQLException nextEx = e.getNextException();\r
261             if (nextEx != null) {\r
262                     msg = msg + getLineSeparator();\r
263                     msg = msg + "[NextException]" + getLineSeparator();\r
264                 msg = msg + nextEx.getClass().getName() + getLineSeparator();\r
265                     msg = msg + nextEx.getMessage() + getLineSeparator();\r
266                 SQLException nextNextEx = nextEx.getNextException();\r
267                 if (nextNextEx != null) {\r
268                         msg = msg + getLineSeparator();\r
269                         msg = msg + "[NextNextException]" + getLineSeparator();\r
270                     msg = msg + nextNextEx.getClass().getName() + getLineSeparator();\r
271                         msg = msg + nextNextEx.getMessage() + getLineSeparator();\r
272                 }\r
273             }\r
274             Object invokeName = extractBehaviorInvokeName();\r
275             if (invokeName != null) {\r
276                     msg = msg + getLineSeparator();\r
277                 msg = msg + "[Behavior]" + getLineSeparator();\r
278                 msg = msg + invokeName + getLineSeparator();\r
279             }\r
280             if (hasConditionBean()) {\r
281                     msg = msg + getLineSeparator();\r
282                 msg = msg + "[ConditionBean]" + getLineSeparator();\r
283                 msg = msg + getConditionBean().getClass().getName() + getLineSeparator();\r
284             }\r
285             if (hasOutsideSqlContext()) {\r
286                     msg = msg + getLineSeparator();\r
287                 msg = msg + "[OutsideSql]" + getLineSeparator();\r
288                 msg = msg + getOutsideSqlContext().getOutsideSqlPath() + getLineSeparator();\r
289                     msg = msg + getLineSeparator();\r
290                 msg = msg + "[ParameterBean]" + getLineSeparator();\r
291                 Object pmb = getOutsideSqlContext().getParameterBean();\r
292                 if (pmb != null) {\r
293                     msg = msg + pmb.getClass().getName() + getLineSeparator();\r
294                     msg = msg + pmb + getLineSeparator();\r
295                 } else {\r
296                     msg = msg + pmb + getLineSeparator();\r
297                 }\r
298             }\r
299                 if (statement != null) {\r
300                     msg = msg + getLineSeparator();\r
301                     msg = msg + "[Statement]" + getLineSeparator();\r
302                 msg = msg + statement.getClass().getName() + getLineSeparator();\r
303                 }\r
304             if (completeSql != null) {\r
305                     msg = msg + getLineSeparator();\r
306                 msg = msg + "[Display SQL]" + getLineSeparator();\r
307                 msg = msg + completeSql + getLineSeparator();\r
308             }\r
309             msg = msg + "* * * * * * * * * */";\r
310             throw new ${glSQLFailureException}(msg, e);\r
311         }\r
312 \r
313         protected String extractSQLState(SQLException e) {\r
314             String sqlState = e.getSQLState();\r
315             if (sqlState != null) {\r
316                 return sqlState;\r
317             }\r
318 \r
319             // Next\r
320             SQLException nextEx = e.getNextException();\r
321             if (nextEx == null) {\r
322                 return null;\r
323             }\r
324             sqlState = nextEx.getSQLState();\r
325             if (sqlState != null) {\r
326                 return sqlState;\r
327             }\r
328 \r
329             // Next Next\r
330             SQLException nextNextEx = nextEx.getNextException();\r
331             if (nextNextEx == null) {\r
332                 return null;\r
333             }\r
334             sqlState = nextNextEx.getSQLState();\r
335             if (sqlState != null) {\r
336                 return sqlState;\r
337             }\r
338 \r
339             // Next Next Next\r
340             SQLException nextNextNextEx = nextNextEx.getNextException();\r
341             if (nextNextNextEx == null) {\r
342                 return null;\r
343             }\r
344             sqlState = nextNextNextEx.getSQLState();\r
345             if (sqlState != null) {\r
346                 return sqlState;\r
347             }\r
348 \r
349             // It doesn't use recursive call by design because JDBC is unpredictable fellow.\r
350             return null;\r
351         }\r
352 \r
353         protected String extractBehaviorInvokeName() {\r
354             final Object behaviorInvokeName = ${glInternalMapContextName}.getObject("df:BehaviorInvokeName");\r
355             if (behaviorInvokeName == null) {\r
356                 return null;\r
357             }\r
358             final Object clientInvokeName = ${glInternalMapContextName}.getObject("df:ClientInvokeName");\r
359             final Object byPassInvokeName = ${glInternalMapContextName}.getObject("df:ByPassInvokeName");\r
360             final StringBuilder sb = new StringBuilder();\r
361             boolean existsPath = false;\r
362             if (clientInvokeName != null) {\r
363                 existsPath = true;\r
364                 sb.append(clientInvokeName);\r
365             }\r
366             if (byPassInvokeName != null) {\r
367                 existsPath = true;\r
368                 sb.append(byPassInvokeName);\r
369             }\r
370             sb.append(behaviorInvokeName);\r
371             if (existsPath) {\r
372                 sb.append("...");\r
373             }\r
374             return sb.toString();\r
375         }\r
376 \r
377         protected boolean hasConditionBean() {\r
378             return ${glConditionBeanContextName}.isExistConditionBeanOnThread();\r
379         }\r
380 \r
381         protected ${glConditionBeanInterfaceName} getConditionBean() {\r
382             return ${glConditionBeanContextName}.getConditionBeanOnThread();\r
383         }\r
384 \r
385         protected boolean hasOutsideSqlContext() {\r
386             return ${glOutsideSqlContextName}.isExistOutsideSqlContextOnThread();\r
387         }\r
388 \r
389         protected ${glOutsideSqlContextName} getOutsideSqlContext() {\r
390             return ${glOutsideSqlContextName}.getOutsideSqlContextOnThread();\r
391         }\r
392 \r
393         protected String getLineSeparator() {\r
394             return ${glSimpleSystemUtil}.getLineSeparator();\r
395         }\r
396     }\r
397 \r
398     // ===================================================================================\r
399     //                                                                      JDBC Delegator\r
400     //                                                                      ==============\r
401     protected Connection getConnection() {\r
402         if (dataSource == null) {\r
403             throw new IllegalStateException("The dataSource should not be null!");\r
404         }\r
405         try {\r
406             return dataSource.getConnection();\r
407         } catch (SQLException e) {\r
408             handleSQLException(e, null);\r
409             return null;// Unreachable!\r
410         }\r
411     }\r
412 \r
413     protected PreparedStatement prepareStatement(Connection conn) {\r
414         if (sql == null) {\r
415             throw new IllegalStateException("The sql should not be null!");\r
416         }\r
417         return statementFactory.createPreparedStatement(conn, sql);\r
418     }\r
419 \r
420     protected int executeUpdate(PreparedStatement ps) {\r
421         try {\r
422             return ps.executeUpdate();\r
423         } catch (SQLException e) {\r
424             handleSQLException(e, ps, true);\r
425             return 0;// Unreachable!\r
426         }\r
427     }\r
428 \r
429     protected void setFetchSize(Statement statement, int fetchSize) {\r
430         if (statement == null) {\r
431             return;\r
432         }\r
433         try {\r
434             statement.setFetchSize(fetchSize);\r
435         } catch (SQLException e) {\r
436             handleSQLException(e, statement);\r
437         }\r
438     }\r
439 \r
440     protected void setMaxRows(Statement statement, int maxRows) {\r
441         if (statement == null) {\r
442             return;\r
443         }\r
444         try {\r
445             statement.setMaxRows(maxRows);\r
446         } catch (SQLException e) {\r
447             handleSQLException(e, statement);\r
448         }\r
449     }\r
450 \r
451     protected void close(Statement statement) {\r
452         if (statement == null) {\r
453             return;\r
454         }\r
455         try {\r
456             statement.close();\r
457         } catch (SQLException e) {\r
458             handleSQLException(e, statement);\r
459         }\r
460     }\r
461 \r
462     protected void close(ResultSet resultSet) {\r
463         if (resultSet == null) {\r
464             return;\r
465         }\r
466         try {\r
467             resultSet.close();\r
468         } catch (SQLException e) {\r
469             handleSQLException(e, null);\r
470         }\r
471     }\r
472 \r
473     protected void close(Connection conn) {\r
474         if (conn == null) {\r
475             return;\r
476         }\r
477         try {\r
478             conn.close();\r
479         } catch (SQLException e) {\r
480             handleSQLException(e, null);\r
481         }\r
482     }\r
483 \r
484     // ===================================================================================\r
485     //                                                                       Assist Helper\r
486     //                                                                       =============\r
487     // It needs this method if the target database doest not support line comment.\r
488     protected String removeLineComment(final String sql) { // With removing CR!\r
489         if (sql == null || sql.trim().length() == 0) {\r
490             return sql;\r
491         }\r
492         final StringBuilder sb = new StringBuilder();\r
493         final String[] lines = sql.split("\n");\r
494         for (String line : lines) {\r
495             if (line == null) {\r
496                 continue;\r
497             }\r
498             line = line.replaceAll("\r", ""); // Remove CR!\r
499             if (line.startsWith("--")) {\r
500                 continue;\r
501             }\r
502             sb.append(line).append("\n");\r
503         }\r
504         return sb.toString();\r
505     }\r
506 \r
507     // ===================================================================================\r
508     //                                                                      General Helper\r
509     //                                                                      ==============\r
510     protected String getLineSeparator() {\r
511         return ${glSimpleSystemUtil}.getLineSeparator();\r
512     }\r
513 \r
514         // ===================================================================================\r
515     //                                                                            Accessor\r
516     //                                                                            ========\r
517     public DataSource getDataSource() {\r
518         return dataSource;\r
519     }\r
520 \r
521     public void setDataSource(DataSource dataSource) {\r
522         this.dataSource = dataSource;\r
523     }\r
524 \r
525     public String getSql() {\r
526         return sql;\r
527     }\r
528 \r
529     public void setSql(String sql) {\r
530 #if ($database.isRemoveLineCommentFromExecutedSql())\r
531         sql = removeLineComment(sql);\r
532 #end\r
533         this.sql = sql;\r
534     }\r
535 \r
536     public StatementFactory getStatementFactory() {\r
537         return statementFactory;\r
538     }\r
539 \r
540     public void setStatementFactory(StatementFactory statementFactory) {\r
541         this.statementFactory = statementFactory;\r
542     }\r
543 \r
544     public void setLoggingMessageSqlArgs(Object[] loggingMessageSqlArgs) {\r
545         this.loggingMessageSqlArgs = loggingMessageSqlArgs;\r
546     }\r
547 }\r