Java Code Examples for microsoft.sql.DateTimeOffset

The following examples show how to use microsoft.sql.DateTimeOffset. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: das   Source File: SQLValidation.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Mock a object according to the SQL Type
 *
 * @param javaSqlTypes The specified SQL Type @see java.sql.CheckTypes
 * @return Mocked object
 */

private static Object mockSQLValue(int javaSqlTypes) {
    switch (javaSqlTypes) {
        case Types.BIT:
        case Types.TINYINT:
        case Types.SMALLINT:
        case Types.INTEGER:
        case Types.BIGINT:
            return 0;
        case Types.REAL:
        case Types.FLOAT:
        case Types.DOUBLE:
        case Types.DECIMAL:
            return 0.0;
        case Types.NUMERIC:
            return BigDecimal.ZERO;
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
        case Types.NULL:
        case Types.OTHER:
            return null;
        case Types.CHAR:
            return "X";
        case Types.DATE:
            return Date.valueOf("2012-01-01");
        case Types.TIME:
            return Time.valueOf("10:00:00");
        case Types.TIMESTAMP:
            return Timestamp.valueOf("2012-01-01 10:00:00");
        case microsoft.sql.Types.DATETIMEOFFSET:
            return DateTimeOffset.valueOf(Timestamp.valueOf("2012-01-01 10:00:00"), 0);
        case Types.VARCHAR:
        case Types.NVARCHAR:
        case Types.LONGNVARCHAR:
        case Types.LONGVARCHAR:
            return "TT";
        case 10001: // uniqueidentifier
            return "C4AECF65-1D5C-47B6-BFFC-0C9550C4E158";
        default:
            return null;

    }
}
 
Example 2
Source Project: das   Source File: SQLValidation.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Parse the String value to java Object according to different SQL Type
 *
 * @param javaSqlTypes The SQL CheckTypes @see java.sql.CheckTypes
 * @param val The string value
 * @return Java Object
 */
private static Object parseSQLValue(int javaSqlTypes, String val) {
    if (null == val || val.equalsIgnoreCase("null")){
        return null;
    }
    switch (javaSqlTypes) {
        case Types.BIT:
            return Integer.parseInt(val) == 0 ? 0 : 1;
        case Types.TINYINT:
            return Byte.parseByte(val);
        case Types.SMALLINT:
            return Short.parseShort(val);
        case Types.INTEGER:
            return Integer.parseInt(val);
        case Types.BIGINT:
            return Long.parseLong(val);
        case Types.REAL:
            return Float.parseFloat(val);
        case Types.FLOAT:
        case Types.DOUBLE:
            return Double.parseDouble(val);
        case Types.DECIMAL:
        case Types.NUMERIC:
            return BigDecimal.valueOf(Double.parseDouble(val));
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            return val.getBytes();
        case Types.NULL:
        case Types.OTHER:
            return null;
        case Types.CHAR:
            return val;
        case Types.DATE:
            // return Date.valueOf(val);
            return parseDate(val);
        case Types.TIME:
            return Time.valueOf(val);
        case Types.TIMESTAMP:
            return Timestamp.valueOf(val);
        case microsoft.sql.Types.DATETIMEOFFSET:
            return DateTimeOffset.valueOf(Timestamp.valueOf(val), 0);
        case Types.VARCHAR:
        case Types.NVARCHAR:
        case Types.LONGNVARCHAR:
        case Types.LONGVARCHAR:
            return val;
        case 10001: // uniqueidentifier
            return val;
        default:
            return null;

    }
}
 
Example 3
Source Project: das   Source File: JavaColumnNameResultSetExtractor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<AbstractParameterHost> extract(ResultSet rs) throws SQLException {
    List<AbstractParameterHost> allColumns = new ArrayList<>();
    try {
        boolean isMySql = DbUtils.isMySqlServer(alldbs_id);
        Map<String, Integer> columnSqlType = DbUtils.getColumnSqlType(alldbs_id, tableName);
        Map<String, Class<?>> typeMapper = DbUtils.getSqlType2JavaTypeMaper(alldbs_id, tableName);
        Map<String, String> columnComment;
        columnComment = DbUtils.getSqlserverColumnComment(alldbs_id, tableName);

        if (columnSqlType != null && columnSqlType.size() > 0) {
            while (rs.next()) {
                JavaParameterHost host = new JavaParameterHost();
                String typeName = rs.getString(TYPE_NAME);
                String columnName = rs.getString(COLUMN_NAME);
                host.setName(columnName);
                host.setSqlType(columnSqlType.get(host.getName()));
                Class<?> javaClass = null;
                if (typeMapper != null && typeMapper.containsKey(host.getName())) {
                    javaClass = typeMapper.get(host.getName());
                } else {
                    javaClass = CodeGenConsts.jdbcSqlTypeToJavaClass.get(host.getSqlType());
                }

                int columnSize = rs.getInt(COLUMN_SIZE);
                // host.setLength(columnSize);
                if (javaClass == null) {
                    if (typeName != null && typeName.equalsIgnoreCase("sql_variant")) {
                        return null;
                    } else if (typeName != null && typeName.equalsIgnoreCase("datetimeoffset")) {
                        javaClass = DateTimeOffset.class;
                    } else {
                        return null;
                    }
                }

                // bit to byte[]
                if (isMySql && typeName.equals("BIT") && columnSize > 1) {
                    javaClass = byte[].class;
                }

                host.setJavaClass(javaClass);
                host.setIndex(rs.getInt(ORDINAL_POSITION));
                host.setIdentity(rs.getString(IS_AUTOINCREMENT).equalsIgnoreCase("YES"));
                String remarks = rs.getString(REMARKS);
                if (remarks == null) {
                    String description = columnComment.get(columnName.toLowerCase());
                    remarks = description == null ? "" : description;
                }

                host.setComment(remarks.replace("\n", " "));
                host.setDefaultValue(rs.getString(COLUMN_DEF));
                host.setDbCategory(dbCategory);
                int dataType = rs.getInt(DATA_TYPE);
                host.setDataType(dataType);
                allColumns.add(host);
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }
    return allColumns;
}
 
Example 4
Source Project: elastic-db-tools-for-java   Source File: MultiShardResultSet.java    License: MIT License 4 votes vote down vote up
public DateTimeOffset getDateTimeOffset(int ordinal) throws SQLException {
    return ((SQLServerResultSet) getCurrentResultSet()).getDateTimeOffset(ordinal);
}
 
Example 5
Source Project: elastic-db-tools-for-java   Source File: MultiShardResultSet.java    License: MIT License 4 votes vote down vote up
public DateTimeOffset getDateTimeOffset(String columnName) throws SQLException {
    return ((SQLServerResultSet) getCurrentResultSet()).getDateTimeOffset(columnName);
}
 
Example 6
Source Project: dal   Source File: JavaColumnNameResultSetExtractor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<AbstractParameterHost> extract(ResultSet rs) throws SQLException {
    List<AbstractParameterHost> allColumns = new ArrayList<>();
    try {
        boolean isMySql = DbUtils.isMySqlServer(allInOneName);
        Map<String, Integer> columnSqlType = DbUtils.getColumnSqlType(allInOneName, tableName);
        Map<String, Class<?>> typeMapper = DbUtils.getSqlType2JavaTypeMaper(allInOneName, tableName);
        Map<String, String> columnComment;
        columnComment = DbUtils.getSqlserverColumnComment(allInOneName, tableName);

        if (columnSqlType != null && columnSqlType.size() > 0) {
            while (rs.next()) {
                JavaParameterHost host = new JavaParameterHost();
                String typeName = rs.getString(TYPE_NAME);
                String columnName = rs.getString(COLUMN_NAME);
                host.setName(columnName);
                host.setSqlType(columnSqlType.get(host.getName()));
                Class<?> javaClass = null;
                if (typeMapper != null && typeMapper.containsKey(host.getName())) {
                    javaClass = typeMapper.get(host.getName());
                } else {
                    javaClass = Consts.jdbcSqlTypeToJavaClass.get(host.getSqlType());
                }

                int columnSize = rs.getInt(COLUMN_SIZE);
                // host.setLength(columnSize);
                if (javaClass == null) {
                    if (typeName != null && typeName.equalsIgnoreCase("sql_variant")) {
                        return null;
                    } else if (typeName != null && typeName.equalsIgnoreCase("datetimeoffset")) {
                        javaClass = DateTimeOffset.class;
                    } else {
                        return null;
                    }
                }

                // bit to byte[]
                if (isMySql && typeName.equals("BIT") && columnSize > 1) {
                    javaClass = byte[].class;
                }

                host.setJavaClass(javaClass);
                host.setIndex(rs.getInt(ORDINAL_POSITION));
                host.setIdentity(rs.getString(IS_AUTOINCREMENT).equalsIgnoreCase("YES"));
                String remarks = rs.getString(REMARKS);
                if (remarks == null) {
                    String description = columnComment.get(columnName.toLowerCase());
                    remarks = description == null ? "" : description;
                }

                host.setComment(remarks.replace("\n", " "));
                host.setDefaultValue(rs.getString(COLUMN_DEF));
                host.setDbCategory(dbCategory);
                int dataType = rs.getInt(DATA_TYPE);
                host.setDataType(dataType);
                allColumns.add(host);
            }
        }
    } catch (Exception e) {
        LoggerManager.getInstance().error(e);
    }
    return allColumns;
}
 
Example 7
Source Project: dal   Source File: SQLValidation.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Mock a object according to the SQL Type
 *
 * @param javaSqlTypes The specified SQL Type @see java.sql.Types
 * @return Mocked object
 */

private static Object mockSQLValue(int javaSqlTypes) {
    switch (javaSqlTypes) {
        case java.sql.Types.BIT:
        case java.sql.Types.TINYINT:
        case java.sql.Types.SMALLINT:
        case java.sql.Types.INTEGER:
        case java.sql.Types.BIGINT:
            return 0;
        case java.sql.Types.REAL:
        case java.sql.Types.FLOAT:
        case java.sql.Types.DOUBLE:
        case java.sql.Types.DECIMAL:
            return 0.0;
        case java.sql.Types.NUMERIC:
            return BigDecimal.ZERO;
        case java.sql.Types.BINARY:
        case java.sql.Types.VARBINARY:
        case java.sql.Types.LONGVARBINARY:
        case java.sql.Types.NULL:
        case java.sql.Types.OTHER:
            return null;
        case java.sql.Types.CHAR:
            return "X";
        case java.sql.Types.DATE:
            return java.sql.Date.valueOf("2012-01-01");
        case java.sql.Types.TIME:
            return Time.valueOf("10:00:00");
        case java.sql.Types.TIMESTAMP:
            return Timestamp.valueOf("2012-01-01 10:00:00");
        case microsoft.sql.Types.DATETIMEOFFSET:
            return DateTimeOffset.valueOf(Timestamp.valueOf("2012-01-01 10:00:00"), 0);
        case java.sql.Types.VARCHAR:
        case java.sql.Types.NVARCHAR:
        case java.sql.Types.LONGNVARCHAR:
        case java.sql.Types.LONGVARCHAR:
            return "TT";
        case 10001: // uniqueidentifier
            return "C4AECF65-1D5C-47B6-BFFC-0C9550C4E158";
        default:
            return null;

    }
}
 
Example 8
Source Project: dal   Source File: SQLValidation.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Parse the String value to java Object according to different SQL Type
 *
 * @param javaSqlTypes The SQL Types @see java.sql.Types
 * @param val The string value
 * @return Java Object
 */
private static Object parseSQLValue(int javaSqlTypes, String val) {
    if (null == val || val.equalsIgnoreCase("null"))
        return null;
    switch (javaSqlTypes) {
        case java.sql.Types.BIT:
            return Integer.parseInt(val) == 0 ? 0 : 1;
        case java.sql.Types.TINYINT:
            return Byte.parseByte(val);
        case java.sql.Types.SMALLINT:
            return Short.parseShort(val);
        case java.sql.Types.INTEGER:
            return Integer.parseInt(val);
        case java.sql.Types.BIGINT:
            return Long.parseLong(val);
        case java.sql.Types.REAL:
            return Float.parseFloat(val);
        case java.sql.Types.FLOAT:
        case java.sql.Types.DOUBLE:
            return Double.parseDouble(val);
        case java.sql.Types.DECIMAL:
        case java.sql.Types.NUMERIC:
            return BigDecimal.valueOf(Double.parseDouble(val));
        case java.sql.Types.BINARY:
        case java.sql.Types.VARBINARY:
        case java.sql.Types.LONGVARBINARY:
            return val.getBytes();
        case java.sql.Types.NULL:
        case java.sql.Types.OTHER:
            return null;
        case java.sql.Types.CHAR:
            return val;
        case java.sql.Types.DATE:
            // return Date.valueOf(val);
            return parseDate(val);
        case java.sql.Types.TIME:
            return Time.valueOf(val);
        case java.sql.Types.TIMESTAMP:
            return Timestamp.valueOf(val);
        case microsoft.sql.Types.DATETIMEOFFSET:
            return DateTimeOffset.valueOf(Timestamp.valueOf(val), 0);
        case java.sql.Types.VARCHAR:
        case java.sql.Types.NVARCHAR:
        case java.sql.Types.LONGNVARCHAR:
        case java.sql.Types.LONGVARCHAR:
            return val;
        case 10001: // uniqueidentifier
            return val;
        default:
            return null;

    }
}
 
Example 9
Source Project: datacollector   Source File: JdbcUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static Map<String, String> getMinMaxOffsetValueHelper(
    String minMaxQuery,
    DatabaseVendor vendor,
    Connection connection,
    String schema,
    String tableName,
    QuoteChar quoteChar,
    Collection<String> offsetColumnNames
) throws SQLException {
  Map<String, String> minMaxOffsetValues = new HashMap<>();
  final String qualifiedTableName = TableContextUtil.getQuotedQualifiedTableName(
      schema,
      tableName,
      quoteChar.getQuoteCharacter()
  );
  for (String offsetColumn : offsetColumnNames) {
    final String qualifiedOffsetColumn = TableContextUtil.getQuotedObjectName(offsetColumn, quoteChar.getQuoteCharacter());
    final String minMaxOffsetQuery = String.format(minMaxQuery, qualifiedOffsetColumn, qualifiedTableName);
    LOG.debug("Issuing {} offset query: {}",
          minMaxQuery.equals(MIN_OFFSET_VALUE_QUERY) ? "MINIMUM" : "MAXIMUM", minMaxOffsetQuery);
    try (
      Statement st = connection.createStatement();
      ResultSet rs = st.executeQuery(minMaxOffsetQuery)
    ) {
      if (rs.next()) {
        String minMaxValue = null;
        final int colType = rs.getMetaData().getColumnType(MIN_MAX_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);

        switch (vendor) {
          case ORACLE:
            if(TableContextUtil.VENDOR_PARTITIONABLE_TYPES.get(DatabaseVendor.ORACLE).contains(colType)) {
              switch (colType) {
                case TableContextUtil.TYPE_ORACLE_TIMESTAMP_WITH_LOCAL_TIME_ZONE:
                case TableContextUtil.TYPE_ORACLE_TIMESTAMP_WITH_TIME_ZONE:
                  OffsetDateTime offsetDateTime = rs.getObject(MIN_MAX_OFFSET_VALUE_QUERY_RESULT_SET_INDEX, OffsetDateTime.class);
                  if(offsetDateTime != null) {
                    minMaxValue = offsetDateTime.toZonedDateTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
                  }
                  break;
                default:
                  throw new IllegalStateException(Utils.format("Unexpected type: {}", colType));
              }
            }
            break;

          case SQL_SERVER:
            if(TableContextUtil.VENDOR_PARTITIONABLE_TYPES.get(DatabaseVendor.SQL_SERVER).contains(colType)) {
              if (colType == TableContextUtil.TYPE_SQL_SERVER_DATETIMEOFFSET) {
                DateTimeOffset dateTimeOffset = rs.getObject(MIN_MAX_OFFSET_VALUE_QUERY_RESULT_SET_INDEX, DateTimeOffset.class);
                if (dateTimeOffset != null) {
                  minMaxValue = dateTimeOffset.getOffsetDateTime().toZonedDateTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
                }
              }
            }
            break;
        }

        if(minMaxValue == null) {
          switch (colType) {
            case Types.DATE:
              java.sql.Date date = rs.getDate(MIN_MAX_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
              if (date != null) {
                minMaxValue = String.valueOf(
                    getEpochMillisFromSqlDate(date)
                );
              }
              break;
            case Types.TIME:
              java.sql.Time time = rs.getTime(MIN_MAX_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
              if (time != null) {
                minMaxValue = String.valueOf(
                    getEpochMillisFromSqlTime(time)
                );
              }
              break;
            case Types.TIMESTAMP:
              Timestamp timestamp = rs.getTimestamp(MIN_MAX_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
              if (timestamp != null) {
                final Instant instant = timestamp.toInstant();
                minMaxValue = String.valueOf(instant.toEpochMilli());
              }
              break;
            default:
              minMaxValue = rs.getString(MIN_MAX_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
              break;
          }
        }
        if (minMaxValue != null) {
          minMaxOffsetValues.put(offsetColumn, minMaxValue);
        }
      } else {
        LOG.warn("Unable to get minimum offset value using query {}; result set had no rows", minMaxOffsetQuery);
      }
    }
  }

  return minMaxOffsetValues;
}