Search in sources :

Example 1 with ColumnType

use of com.nearinfinity.honeycomb.mysql.gen.ColumnType in project honeycomb by altamiracorp.

the class FieldParser method parse.

/**
     * Try to parse a string into a byte string based on a column type.
     *
     * @param val    String value
     * @param schema Column schema to base value parsing on.
     * @return Byte string
     * @throws ParseException The string value could not be parsed into the column type.
     */
public static ByteBuffer parse(String val, ColumnSchema schema) throws ParseException {
    checkNotNull(val, "Should not be parsing null. Something went terribly wrong.");
    checkNotNull(schema, "Column metadata is null.");
    ColumnType type = schema.getType();
    if (val.length() == 0 && type != ColumnType.STRING && type != ColumnType.BINARY) {
        if (schema.getIsNullable()) {
            return null;
        }
        throw new IllegalArgumentException("Expected a value for a" + " non-null SQL column, but no value was given.");
    }
    switch(type) {
        case LONG:
            return ByteBuffer.wrap(Longs.toByteArray(Long.parseLong(val)));
        case ULONG:
            BigInteger n = new BigInteger(val);
            if (n.compareTo(BigInteger.ZERO) == -1) {
                throw new IllegalArgumentException("negative value provided for unsigned column. value: " + val);
            }
            return ByteBuffer.wrap(Longs.toByteArray(n.longValue()));
        case DOUBLE:
            return ByteBuffer.wrap(Bytes.toBytes(Double.parseDouble(val)));
        case DATE:
            return extractDate(val, "yyyy-MM-dd", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy.MM.dd", "yyyyMMdd");
        case TIME:
            return extractDate(val, "HH:mm:ss", "HH:mm:ss", "HHmmss");
        case DATETIME:
            return extractDate(val, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy.MM.dd HH:mm:ss", "yyyyMMdd HHmmss");
        case DECIMAL:
            return extractDecimal(val, schema.getPrecision(), schema.getScale());
        case STRING:
        case BINARY:
        default:
            return ByteBuffer.wrap(val.getBytes(Charset.forName("UTF-8")));
    }
}
Also used : ColumnType(com.nearinfinity.honeycomb.mysql.gen.ColumnType) BigInteger(java.math.BigInteger)

Example 2 with ColumnType

use of com.nearinfinity.honeycomb.mysql.gen.ColumnType in project honeycomb by altamiracorp.

the class ColumnSchemaGenerator method next.

@Override
public ColumnSchema next() {
    ColumnType type = typeGen.next();
    ColumnSchema.Builder builder = ColumnSchema.builder(MYSQL_NAME_GEN.next(), type);
    switch(type) {
        case STRING:
        case BINARY:
            builder.setMaxLength(lengthGen.next());
            break;
        case LONG:
        case ULONG:
        case DOUBLE:
            builder.setIsAutoIncrement(RAND.nextBoolean());
            break;
        case DECIMAL:
            int precision = RAND.nextInt(66);
            int scale = RAND.nextInt(Math.max(31, precision));
            builder.setPrecision(precision).setScale(scale);
            break;
        default:
            break;
    }
    return builder.build();
}
Also used : ColumnType(com.nearinfinity.honeycomb.mysql.gen.ColumnType) ColumnSchema(com.nearinfinity.honeycomb.mysql.schema.ColumnSchema)

Aggregations

ColumnType (com.nearinfinity.honeycomb.mysql.gen.ColumnType)2 ColumnSchema (com.nearinfinity.honeycomb.mysql.schema.ColumnSchema)1 BigInteger (java.math.BigInteger)1