📄 timestamparith.java
字号:
} abstract class OneTest { final int interval; // FRAC_SECOND_INTERVAL, SECOND_INTERVAL, ... or YEAR_INTERVAL final String expectedSQLState; // Null if no SQLException is expected final String expectedMsg; // Null if no SQLException is expected String sql; OneTest( int interval, String expectedSQLState, String expectedMsg) { this.interval = interval; this.expectedSQLState = expectedSQLState; this.expectedMsg = expectedMsg; } void runTest() { sql = composeSQL(); ResultSet rs = null; try { rs = stmt.executeQuery( sql); checkResultSet( rs, sql); if( expectedSQLState != null) reportFailure( "Statement '" + sql + "' did not generate an exception"); } catch( SQLException sqle) { checkSQLException( "Statement", sqle); } if( rs != null) { try { rs.close(); } catch( SQLException sqle){}; rs = null; } try { rs = executePS(); checkResultSet( rs, sql); if( expectedSQLState != null) reportFailure( "PreparedStatement '" + sql + "' did not generate an exception"); } catch( SQLException sqle) { checkSQLException( "PreparedStatement", sqle); } if( rs != null) { try { rs.close(); } catch( SQLException sqle){}; rs = null; } } // end of RunTest private void checkResultSet( ResultSet rs, String sql) throws SQLException { if( rs.next()) { checkResultRow( rs, sql); if( rs.next()) reportFailure( "'" + sql + "' returned more than one row."); } else reportFailure( "'" + sql + "' did not return any rows."); } // end of checkResultSet private void checkSQLException( String type, SQLException sqle) { if( expectedSQLState != null) { if( ! expectedSQLState.equals( sqle.getSQLState())) reportFailure( "Incorrect SQLState from " + type + " '" + sql + "' expected " + expectedSQLState + " got " + sqle.getSQLState()); else if( expectedMsg != null && ! expectedMsg.equals( sqle.getMessage())) reportFailure( "Incorrect exception message from " + type + " '" + sql + "' expected '" + expectedMsg + "' got '" + sqle.getMessage() + "'"); } else { reportFailure( "Unexpected exception from " + type + " '" + sql + "'"); reportSQLException( sqle); } } // end of checkSQLException abstract String composeSQL(); abstract void checkResultRow( ResultSet rs, String sql) throws SQLException; abstract ResultSet executePS() throws SQLException; } class OneDiffTest extends OneTest { private final java.util.Date ts1; private final java.util.Date ts2; final int expectedDiff; protected boolean expectNull; OneDiffTest( int interval, java.util.Date ts1, java.util.Date ts2, int expectedDiff, String expectedSQLState, String expectedMsg) { super( interval, expectedSQLState, expectedMsg); this.ts1 = ts1; this.ts2 = ts2; this.expectedDiff = expectedDiff; expectNull = (ts1 == null) || (ts2 == null); } String composeSQL() { return composeSqlStr( "DIFF", interval, dateTimeToLiteral( ts1), dateTimeToLiteral( ts2)); } void checkResultRow( ResultSet rs, String sql) throws SQLException { int actualDiff = rs.getInt(1); if( rs.wasNull()) { if( !expectNull) reportFailure( "Unexpected null result from '" + sql + "'."); } else { if( expectNull) reportFailure( "Expected null result from '" + sql + "'."); else if( actualDiff != expectedDiff) reportFailure( "Unexpected result from '" + sql + "'. Expected " + expectedDiff + " got " + actualDiff + "."); } } ResultSet executePS() throws SQLException { setDateTime( tsDiffPS[interval], 1, ts1); setDateTime( tsDiffPS[interval], 2, ts2); return tsDiffPS[interval].executeQuery(); } } // end of class OneDiffTest class OneStringDiffTest extends OneDiffTest { private final String ts1; private final String ts2; OneStringDiffTest( int interval, String ts1, String ts2, int expectedDiff, String expectedSQLState, String expectedMsg) { super( interval, (java.util.Date) null, (java.util.Date) null, expectedDiff, expectedSQLState, expectedMsg); this.ts1 = ts1; this.ts2 = ts2; expectNull = (ts1 == null) || (ts2 == null); } String composeSQL() { return composeSqlStr( "DIFF", interval, dateTimeToLiteral( ts1), dateTimeToLiteral( ts2)); } ResultSet executePS() throws SQLException { tsDiffPS[interval].setString( 1, ts1); tsDiffPS[interval].setString( 2, ts2); return tsDiffPS[interval].executeQuery(); } } // end of class OneStringDiffTest class OneAddTest extends OneTest { private final java.util.Date ts; final int count; final java.sql.Timestamp expected; OneAddTest( int interval, int count, java.util.Date ts, java.sql.Timestamp expected, String expectedSQLState, String expectedMsg) { super( interval, expectedSQLState, expectedMsg); this.count = count; this.ts = ts; this.expected = expected; } String composeSQL() { return composeSqlStr( "ADD", interval, String.valueOf( count), dateTimeToLiteral( ts)); } void checkResultRow( ResultSet rs, String sql) throws SQLException { java.sql.Timestamp actual = rs.getTimestamp( 1); if( rs.wasNull() || actual == null) { if( expected != null) reportFailure( "Unexpected null result from '" + sql + "'."); } else { if( expected == null) reportFailure( "Expected null result from '" + sql + "'."); else if( ! actual.equals( expected)) reportFailure( "Unexpected result from '" + sql + "'. Expected " + expected.toString() + " got " + actual.toString() + "."); } } ResultSet executePS() throws SQLException { tsAddPS[interval].setInt( 1, count); setDateTime( tsAddPS[interval], 2, ts); return tsAddPS[interval].executeQuery(); } } // end of class OneAddTest class OneStringAddTest extends OneAddTest { private final String ts; OneStringAddTest( int interval, int count, String ts, java.sql.Timestamp expected, String expectedSQLState, String expectedMsg) { super( interval, count, (java.util.Date) null, expected, expectedSQLState, expectedMsg); this.ts = ts; } String composeSQL() { return composeSqlStr( "ADD", interval, String.valueOf( count), dateTimeToLiteral( ts)); } ResultSet executePS() throws SQLException { tsAddPS[interval].setInt( 1, count); tsAddPS[interval].setString( 2, ts); return tsAddPS[interval].executeQuery(); } } // end of class OneStringAddTest}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -