private TInstance javaDateTInstance(Object value) {
   int jdbcType;
   if (value instanceof java.sql.Date) {
     jdbcType = Types.DATE;
   } else if (value instanceof java.sql.Time) {
     jdbcType = Types.TIME;
   } else {
     jdbcType = Types.TIMESTAMP;
   }
   return typesTranslator.typeClassForJDBCType(jdbcType).instance(true);
 }
 public ValueSource valuefromObject(Object value, ServerType type) {
   if (value instanceof Date) {
     TInstance dateType = javaDateTInstance(value);
     Value dateValue = new Value(dateType);
     typesTranslator.setTimestampMillisValue(
         dateValue,
         ((Date) value).getTime(),
         (value instanceof java.sql.Timestamp) ? ((java.sql.Timestamp) value).getNanos() : 0);
     TInstance targetType = type.getType();
     if (dateType.equals(targetType)) return dateValue;
     TExecutionContext context =
         new TExecutionContext(Collections.singletonList(dateType), targetType, null);
     Value result = new Value(targetType);
     targetType.typeClass().fromObject(context, dateValue, result);
     return result;
   } else {
     // TODO this is inefficient, but I want to get it working.
     return ValueSources.valuefromObject(value, type.getType());
   }
 }
  /** Append the given value to the buffer. */
  public void appendValue(ValueSource value, ServerType type, boolean binary) throws IOException {
    if (!binary) {
      // Handle unusual text encoding of binary types.
      switch (type.getBinaryEncoding()) {
        case BINARY_OCTAL_TEXT:
          processBinaryText(value);
          break;

        default:
          type.getType().format(value, appender);
          break;
      }
    } else {
      switch (type.getBinaryEncoding()) {
        case BINARY_OCTAL_TEXT:
          getByteStream().write(value.getBytes());
          break;
        case INT_8:
          getDataStream().write((byte) typesTranslator.getIntegerValue(value));
          break;
        case INT_16:
          getDataStream().writeShort((short) typesTranslator.getIntegerValue(value));
          break;
        case INT_32:
          getDataStream().writeInt((int) typesTranslator.getIntegerValue(value));
          break;
        case INT_64:
          getDataStream().writeLong(typesTranslator.getIntegerValue(value));
          break;
        case FLOAT_32:
          getDataStream().writeFloat(value.getFloat());
          break;
        case FLOAT_64:
          getDataStream().writeDouble(value.getDouble());
          break;
        case STRING_BYTES:
          getByteStream().write(value.getString().getBytes(encoding));
          break;
        case BOOLEAN_C:
          getDataStream().write(value.getBoolean() ? 1 : 0);
          break;
        case TIMESTAMP_FLOAT64_SECS_2000_NOTZ:
          getDataStream()
              .writeDouble(
                  seconds2000NoTZ(typesTranslator.getTimestampMillisValue(value))
                      + typesTranslator.getTimestampNanosValue(value) / 1.0e9);
          break;
        case TIMESTAMP_INT64_MICROS_2000_NOTZ:
          getDataStream()
              .writeLong(
                  seconds2000NoTZ(typesTranslator.getTimestampMillisValue(value)) * 1000000L
                      + typesTranslator.getTimestampNanosValue(value) / 1000);
          break;
        case DAYS_2000:
          getDataStream().writeInt(days2000(typesTranslator.getTimestampMillisValue(value)));
          break;
        case TIME_FLOAT64_SECS_NOTZ:
          getDataStream().writeDouble(timeSecsNoTZ(typesTranslator.getTimestampMillisValue(value)));
          break;
        case TIME_INT64_MICROS_NOTZ:
          getDataStream()
              .writeLong(timeSecsNoTZ(typesTranslator.getTimestampMillisValue(value)) * 1000000L);
          break;
        case DECIMAL_PG_NUMERIC_VAR:
          for (short d : pgNumericVar(typesTranslator.getDecimalValue(value))) {
            getDataStream().writeShort(d);
          }
          break;
        case UUID:
          getDataStream().write(AkGUID.uuidToBytes((java.util.UUID) value.getObject()));
          break;
        case NONE:
        default:
          throw new UnsupportedOperationException("No binary encoding for " + type);
      }
    }
  }
Example #4
0
 private static void handleModifyColumnNode(
     ModifyColumnNode modNode,
     AISBuilder builder,
     Table tableCopy,
     TypesTranslator typesTranslator) {
   AkibanInformationSchema aisCopy = tableCopy.getAIS();
   Column column = tableCopy.getColumn(modNode.getColumnName());
   assert column != null : modNode.getColumnName();
   switch (modNode.getNodeType()) {
     case NodeTypes.MODIFY_COLUMN_DEFAULT_NODE:
       if (modNode.isAutoincrementColumn()) {
         int autoIncType = (int) modNode.getAutoinc_create_or_modify_Start_Increment();
         switch (autoIncType) {
           case ColumnDefinitionNode.CREATE_AUTOINCREMENT:
             {
               if (column.getIdentityGenerator() != null) {
                 throw new ColumnAlreadyGeneratedException(column);
               }
               TableName name = tableCopy.getName();
               TableDDL.setAutoIncrement(
                   builder, name.getSchemaName(), name.getTableName(), modNode);
             }
             break;
           case ColumnDefinitionNode.MODIFY_AUTOINCREMENT_INC_VALUE:
             throw new UnsupportedSQLException("SET INCREMENT BY", modNode);
           case ColumnDefinitionNode.MODIFY_AUTOINCREMENT_RESTART_VALUE:
             // Note: Also handled above
             throw new UnsupportedSQLException("RESTART WITH", modNode);
           default:
             throw new IllegalStateException("Unknown autoIncType: " + autoIncType);
         }
       } else {
         // DROP DEFAULT will come though as a NULL default, clears both GENERATED and DEFAULT
         Sequence seq = column.getIdentityGenerator();
         if (seq != null) {
           column.setDefaultIdentity(null);
           column.setIdentityGenerator(null);
           aisCopy.removeSequence(seq.getSequenceName());
         }
         String[] defaultValueFunction =
             TableDDL.getColumnDefault(
                 modNode, tableCopy.getName().getSchemaName(), tableCopy.getName().getTableName());
         column.setDefaultValue(defaultValueFunction[0]);
         column.setDefaultFunction(defaultValueFunction[1]);
       }
       break;
     case NodeTypes.MODIFY_COLUMN_CONSTRAINT_NODE: // Type only comes from NULL
       column.setType(column.getType().withNullable(true));
       break;
     case NodeTypes.MODIFY_COLUMN_CONSTRAINT_NOT_NULL_NODE: // Type only comes from NOT NULL
       column.setType(column.getType().withNullable(false));
       break;
     case NodeTypes.MODIFY_COLUMN_TYPE_NODE: // All but [NOT] NULL comes from type
       {
         TInstance type =
             typesTranslator.typeForSQLType(modNode.getType()).withNullable(column.getNullable());
         if (false) {
           // TODO: Determine whether compatible, does affect sequence, etc.
           column.setType(type);
         } else {
           tableCopy.dropColumn(modNode.getColumnName());
           builder.column(
               tableCopy.getName().getSchemaName(),
               tableCopy.getName().getTableName(),
               column.getName(),
               column.getPosition(),
               type,
               false, // column.getInitialAutoIncrementValue() != null
               column.getDefaultValue(),
               column.getDefaultFunction());
         }
       }
       break;
     default:
       throw new IllegalStateException("Unexpected node type: " + modNode);
   }
 }