/** * Changes the type of the source to a Float object; */ @Override protected Float changeType(Class<?> sourceClass, Object source) { if (Number.class.isAssignableFrom(sourceClass)) return ((Number) source).floatValue(); else if (Date.class.isAssignableFrom(sourceClass)) return (float) ((Date) source).getTime(); else if (java.sql.Date.class.isAssignableFrom(sourceClass)) return (float) ((java.sql.Date) source).getTime(); else if (Time.class.isAssignableFrom(sourceClass)) return (float) ((Time) source).getTime(); else if (Timestamp.class.isAssignableFrom(sourceClass)) return (float) ((Timestamp) source).getTime(); else return super.changeType(sourceClass, source); }
private void checkPreparedStatementForTestBug50348(Connection testConn, Timestamp timestamp, Time time, String expectedTimestamp, String expectedTime) throws SQLException { PreparedStatement testPstmt = testConn.prepareStatement("SELECT ?, ?"); testPstmt.setTimestamp(1, timestamp); testPstmt.setTime(2, time); this.rs = testPstmt.executeQuery(); this.rs.next(); String timestampAsString = new String(this.rs.getBytes(1)); String timeAsString = new String(this.rs.getBytes(2)); String alert = expectedTimestamp.equals(timestampAsString) && expectedTime.equals(timeAsString) ? "" : " <-- (!)"; System.out.printf("[PS] expected: '%s' | '%s'%n", expectedTimestamp, expectedTime); System.out.printf(" actual: '%s' | '%s' %s%n", timestampAsString, timeAsString, alert); assertEquals(expectedTimestamp, timestampAsString); assertEquals(expectedTime, timeAsString); }
@Override void apply(PreparedStatement stmt, int stmtIndex, DataFrameRow<R, C> row) { final R rowKey = row.key(); try { switch (rowKeyType) { case BIT: stmt.setBoolean(stmtIndex, rowKeyMapper.applyAsBoolean(rowKey)); break; case BOOLEAN: stmt.setBoolean(stmtIndex, rowKeyMapper.applyAsBoolean(rowKey)); break; case TINYINT: stmt.setInt(stmtIndex, rowKeyMapper.applyAsInt(rowKey)); break; case SMALLINT: stmt.setInt(stmtIndex, rowKeyMapper.applyAsInt(rowKey)); break; case FLOAT: stmt.setDouble(stmtIndex, rowKeyMapper.applyAsDouble(rowKey)); break; case INTEGER: stmt.setInt(stmtIndex, rowKeyMapper.applyAsInt(rowKey)); break; case BIGINT: stmt.setLong(stmtIndex, rowKeyMapper.applyAsLong(rowKey)); break; case DOUBLE: stmt.setDouble(stmtIndex, rowKeyMapper.applyAsDouble(rowKey)); break; case DECIMAL: stmt.setDouble(stmtIndex, rowKeyMapper.applyAsDouble(rowKey)); break; case VARCHAR: stmt.setString(stmtIndex, (String)rowKeyMapper.apply(rowKey)); break; case DATE: stmt.setDate(stmtIndex, (Date)rowKeyMapper.apply(rowKey)); break; case TIME: stmt.setTime(stmtIndex, (Time)rowKeyMapper.apply(rowKey)); break; case DATETIME: stmt.setTimestamp(stmtIndex, (Timestamp)rowKeyMapper.apply(rowKey)); break; default: throw new IllegalStateException("Unsupported column type:" + rowKeyType); } } catch (Exception ex) { throw new DataFrameException("Failed to apply row key to SQL statement at " + rowKey, ex); } }
public void testDateTypesToBigInt() throws Exception { final int TOTAL_RECORDS = 1 * 10; long offset = TimeZone.getDefault().getRawOffset(); String table = getTableName().toUpperCase(); ColumnGenerator[] cols = new ColumnGenerator[] { HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(0), "date", Types.DATE, HCatFieldSchema.Type.BIGINT, 0, 0, 0 - offset, new Date(70, 0, 1), KeyType.NOT_A_KEY), HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(1), "time", Types.TIME, HCatFieldSchema.Type.BIGINT, 0, 0, 36672000L - offset, new Time(10, 11, 12), KeyType.NOT_A_KEY), HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(2), "timestamp", Types.TIMESTAMP, HCatFieldSchema.Type.BIGINT, 0, 0, 36672000L - offset, new Timestamp(70, 0, 1, 10, 11, 12, 0), KeyType.NOT_A_KEY), }; List<String> addlArgsArray = new ArrayList<String>(); addlArgsArray.add("--map-column-hive"); addlArgsArray.add("COL0=bigint,COL1=bigint,COL2=bigint"); runHCatExport(addlArgsArray, TOTAL_RECORDS, table, cols); }
/** * Set a parameter to a java.sql.Time value. The driver converts this to a * SQL TIME value when it sends it to the database, using the given * timezone. * * @param parameterIndex * the first parameter is 1...)); * @param x * the parameter value * @param tz * the timezone to use * * @throws java.sql.SQLException * if a database access error occurs */ private void setTimeInternal(int parameterIndex, Time x, Calendar targetCalendar, TimeZone tz, boolean rollForward) throws java.sql.SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.TIME); } else { checkClosed(); if (!this.useLegacyDatetimeCode) { newSetTimeInternal(parameterIndex, x, targetCalendar); } else { Calendar sessionCalendar = getCalendarInstanceForSessionOrNew(); x = TimeUtil.changeTimezone(this.connection, sessionCalendar, targetCalendar, x, tz, this.connection.getServerTimezoneTZ(), rollForward); setInternal(parameterIndex, "'" + x.toString() + "'"); } this.parameterTypes[parameterIndex - 1 + getParameterIndexOffset()] = Types.TIME; } }
/** * Appends a time to the underlying command; * * @param time * @return */ public DatabaseCommand appendTime(Time time) { this.text.append('\''); this.text.append(time); this.text.append('\''); return this; }
void createFactHeader(int nFactId, int nClientId) { Date date = new Date(); Time time = new Time(date.getTime()); String csDate = String.valueOf(time); sql("insert into VIT102 (ID, CLIENTID, FACTDATE) VALUES (#1, #2, #3)") .value(1, nFactId) .value(2, nClientId) .value(3, csDate); }
public static Timestamp getNormalisedTimestamp(Time t) { synchronized (tempCalDefault) { setTimeInMillis(tempCalDefault, System.currentTimeMillis()); resetToDate(tempCalDefault); long value = getTimeInMillis(tempCalDefault) + t.getTime(); return new Timestamp(value); } }
/** * Changes the type of the source object to this adapter's data type; */ @Override protected Time changeType(Class<?> sourceClass, Object source) { if (Date.class.isAssignableFrom(sourceClass)) return new Time(((Date) source).getTime()); else if (Number.class.isAssignableFrom(sourceClass)) return new Time(((Number) source).longValue()); else return super.changeType(sourceClass, source); }
@Test() public void baseRowSetTest0014() throws Exception { Calendar cal = Calendar.getInstance(); brs = new StubBaseRowSet(); brs.setTime(1, Time.valueOf(LocalTime.now()), cal); assertTrue(checkCalendarParam(1, cal)); }
public Time getTime(String parameterName, Calendar cal) throws SQLException { try { if (this.wrappedStmt != null) { return ((CallableStatement) this.wrappedStmt).getTime(parameterName, cal); } throw SQLError.createSQLException("No operations allowed after statement closed", SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor); } catch (SQLException sqlEx) { checkAndFireConnectionError(sqlEx); } return null; }
@Override public Time getTimeFast(int columnIndex, Calendar targetCalendar, TimeZone tz, boolean rollForward, MySQLConnection conn, ResultSetImpl rs) throws SQLException { if (isNull(columnIndex)) { return null; } findAndSeekToOffset(columnIndex); long length = this.rowFromServer.readFieldLength(); int offset = this.rowFromServer.getPosition(); return getTimeFast(columnIndex, this.rowFromServer.getByteBuffer(), offset, (int) length, targetCalendar, tz, rollForward, conn, rs); }
@Override public Time getTimeFast(int columnIndex, Calendar targetCalendar, TimeZone tz, boolean rollForward, MySQLConnection conn, ResultSetImpl rs) throws SQLException { byte[] columnValue = this.internalRowData[columnIndex]; if (columnValue == null) { return null; } return getTimeFast(columnIndex, this.internalRowData[columnIndex], 0, columnValue.length, targetCalendar, tz, rollForward, conn, rs); }
@Test public void testGetTimeLabel() throws SQLException { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(1970, 0, 1, 14, 6, 15); assertNotNull(subject.getTime(TIME_COL_NOT_NULL)); assertEquals(new Time(cal.getTimeInMillis()), subject.getTime(TIME_COL_NOT_NULL)); assertEquals(false, subject.wasNull()); assertNull(subject.getTime(TIME_COL_NULL)); assertTrue(subject.wasNull()); }
@Test public void test19() { Time t = Time.valueOf("08:30:59"); Time t2 = new Time(t.getTime()); assertFalse(t.after(t2), "Error t.after(t2) = true"); assertFalse(t2.after(t), "Error t2.after(t) = true"); }
final static Time fastTimeCreate(int hour, int minute, int second, Calendar targetCalendar, ExceptionInterceptor exceptionInterceptor) throws SQLException { if (hour < 0 || hour > 23) { throw SQLError.createSQLException("Illegal hour value '" + hour + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor); } if (minute < 0 || minute > 59) { throw SQLError.createSQLException( "Illegal minute value '" + minute + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor); } if (second < 0 || second > 59) { throw SQLError.createSQLException( "Illegal minute value '" + second + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor); } Calendar cal = (targetCalendar == null) ? new GregorianCalendar() : targetCalendar; synchronized (cal) { java.util.Date origCalDate = cal.getTime(); try { cal.clear(); // Set 'date' to epoch of Jan 1, 1970 cal.set(1970, 0, 1, hour, minute, second); long timeAsMillis = cal.getTimeInMillis(); return new Time(timeAsMillis); } finally { cal.setTime(origCalDate); } } }
protected Time fastTimeCreate(Calendar cal, int hour, int minute, int second) throws SQLException { synchronized (checkClosed().getConnectionMutex()) { if (!this.useLegacyDatetimeCode) { return TimeUtil.fastTimeCreate(hour, minute, second, cal, getExceptionInterceptor()); } if (cal == null) { cal = getFastDefaultCalendar(); } return TimeUtil.fastTimeCreate(cal, hour, minute, second, getExceptionInterceptor()); } }
private static Object long2Obj(Object value, String type, Locale locale) { String fromType = "Long"; Long lng = (Long) value; if ("String".equalsIgnoreCase(type) || DataType.STRING.equalsIgnoreCase(type)) { return getNf(locale).format(lng.toString()); } else if ("Double".equalsIgnoreCase(type) || DataType.DOUBLE.equalsIgnoreCase(type)) { return new Double(lng.toString()); } else if ("Float".equalsIgnoreCase(type) || DataType.FLOAT.equalsIgnoreCase(type)) { return new Float(lng.toString()); } else if ("BigDecimal".equalsIgnoreCase(type) || DataType.BIGDECIMAL.equalsIgnoreCase(type)) { return new BigDecimal(lng.toString()); } else if ("Long".equalsIgnoreCase(type) || DataType.LONG.equalsIgnoreCase(type)) { return value; } else if ("Integer".equalsIgnoreCase(type) || DataType.INTEGER.equalsIgnoreCase(type)) { return new Integer(lng.toString()); } else if ("Date".equalsIgnoreCase(type) || DataType.DATE.equalsIgnoreCase(type)) { return new java.util.Date(lng); } else if ("java.sql.Date".equalsIgnoreCase(type)) { return new Date(lng); } else if ("Time".equalsIgnoreCase(type) || DataType.TIME.equalsIgnoreCase(type)) { return new Time(lng); } else if ("Timestamp".equalsIgnoreCase(type) || DataType.TIMESTAMP.equalsIgnoreCase(type)) { return new Timestamp(lng); } else { throw new DataParseException(String.format(support, fromType, type)); } }
/** * @see java.sql.CallableStatement#getTime(int) */ public Time getTime(int parameterIndex) throws SQLException { synchronized (checkClosed().getConnectionMutex()) { ResultSetInternalMethods rs = getOutputParameters(parameterIndex); Time retValue = rs.getTime(mapOutputParameterIndexToRsIndex(parameterIndex)); this.outputParamWasNull = rs.wasNull(); return retValue; } }
/** * @see java.sql.CallableStatement#getTime(java.lang.String, java.util.Calendar) */ public Time getTime(String parameterName, Calendar cal) throws SQLException { synchronized (checkClosed().getConnectionMutex()) { ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be from ?= Time retValue = rs.getTime(fixParameterName(parameterName), cal); this.outputParamWasNull = rs.wasNull(); return retValue; } }
/** * Change the given times from one timezone to another * * @param conn * the current connection to the MySQL server * @param t * the times to change * @param fromTz * the timezone to change from * @param toTz * the timezone to change to * * @return the times changed to the timezone 'toTz' */ public static Time changeTimezone(MySQLConnection conn, Calendar sessionCalendar, Calendar targetCalendar, Time t, TimeZone fromTz, TimeZone toTz, boolean rollForward) { if ((conn != null)) { if (conn.getUseTimezone() && !conn.getNoTimezoneConversionForTimeType()) { // Convert the timestamp from GMT to the server's timezone Calendar fromCal = Calendar.getInstance(fromTz); fromCal.setTime(t); int fromOffset = fromCal.get(Calendar.ZONE_OFFSET) + fromCal.get(Calendar.DST_OFFSET); Calendar toCal = Calendar.getInstance(toTz); toCal.setTime(t); int toOffset = toCal.get(Calendar.ZONE_OFFSET) + toCal.get(Calendar.DST_OFFSET); int offsetDiff = fromOffset - toOffset; long toTime = toCal.getTime().getTime(); if (rollForward) { toTime += offsetDiff; } else { toTime -= offsetDiff; } Time changedTime = new Time(toTime); return changedTime; } else if (conn.getUseJDBCCompliantTimezoneShift()) { if (targetCalendar != null) { Time adjustedTime = new Time(jdbcCompliantZoneShift(sessionCalendar, targetCalendar, t)); return adjustedTime; } } } return t; }
Time getNativeTimeViaParseConversion(int columnIndex, Calendar targetCalendar, TimeZone tz, boolean rollForward) throws SQLException { if (this.useUsageAdvisor) { issueConversionViaParsingWarning("getTime()", columnIndex, this.thisRow.getColumnValue(columnIndex - 1), this.fields[columnIndex - 1], new int[] { MysqlDefs.FIELD_TYPE_TIME }); } String strTime = getNativeString(columnIndex); return getTimeFromString(strTime, targetCalendar, columnIndex, tz, rollForward); }
/** * Converts a string in JDBC date escape format to a * <code>Time</code> value. * * @param s date in format <code>hh:mm:ss</code> * @return corresponding <code>Time</code> value * @exception java.lang.IllegalArgumentException if the given argument * does not have the format <code>hh:mm:ss</code> */ public static Time timeValue(String s) { if (s == null) { throw new java.lang.IllegalArgumentException( Trace.getMessage(Trace.HsqlDateTime_null_string)); } return Time.valueOf(s); }
public abstract Time getTimeFast(int columnIndex, Calendar targetCalendar, TimeZone tz, boolean rollForward, MySQLConnection conn, ResultSetImpl rs) throws SQLException;
/** {@inheritDoc} */ public void setTime(int parameterIndex, Time x) throws SQLException { }
public abstract Time getNativeTime(int columnIndex, Calendar targetCalendar, TimeZone tz, boolean rollForward, MySQLConnection conn, ResultSetImpl rs) throws SQLException;
@Override public void setTime(int parameterIndex, Time x) throws SQLException { this.stmt.setTime(parameterIndex, x); }
@Override public void updateTime(int columnIndex, Time x) throws SQLException { throw new UnsupportedOperationException("Not supported yet."); }
@Override public void updateTime(String columnLabel, Time x) throws SQLException { // TODO Auto-generated method stub }
@Override public Time getTime(String columnLabel, Calendar cal) throws SQLException { return null; }
public void updateTime(int columnIndex, Time x) throws SQLException { throw new SQLFeatureNotSupportedException(BlancoGenericJdbcConstants.MESSAGE_NO_UPDATE_SUPPORTED); }
@Override public Time getTime(String columnLabel, Calendar cal) throws SQLException { throw new UnsupportedOperationException("Not supported yet."); }
@Test public void testTimeFormatHHmmssSupported() throws ParseException { SupportedTypes bean = CommandLine.populateCommand(new SupportedTypes(), "-Time", "23:59:58"); assertEquals("Time", new Time(new SimpleDateFormat("HH:mm:ss").parse("23:59:58").getTime()), bean.aTimeField); }
@Test public void test_Time_withType() throws Exception { assertObjectWithType(new Time(System.currentTimeMillis()), Time.class); }
@Override public Time getTime(String columnLabel) throws SQLException { throw new UnsupportedOperationException("Not supported yet."); }