use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class DseFunctionMetadata method describe.
@NonNull
@Override
default String describe(boolean pretty) {
ScriptBuilder builder = new ScriptBuilder(pretty);
builder.append("CREATE FUNCTION ").append(getKeyspace()).append(".").append(getSignature().getName()).append("(");
boolean first = true;
for (int i = 0; i < getSignature().getParameterTypes().size(); i++) {
if (first) {
first = false;
} else {
builder.append(",");
}
DataType type = getSignature().getParameterTypes().get(i);
CqlIdentifier name = getParameterNames().get(i);
builder.append(name).append(" ").append(type.asCql(false, pretty));
}
builder.append(")").increaseIndent().newLine().append(isCalledOnNullInput() ? "CALLED ON NULL INPUT" : "RETURNS NULL ON NULL INPUT").newLine().append("RETURNS ").append(getReturnType().asCql(false, true)).newLine();
// handle deterministic and monotonic
if (getDeterministic().orElse(false)) {
builder.append("DETERMINISTIC").newLine();
}
if (getMonotonicity().isPresent()) {
switch(getMonotonicity().get()) {
case FULLY_MONOTONIC:
builder.append("MONOTONIC").newLine();
break;
case PARTIALLY_MONOTONIC:
builder.append("MONOTONIC ON ").append(getMonotonicArgumentNames().get(0)).newLine();
break;
default:
break;
}
}
builder.append("LANGUAGE ").append(getLanguage()).newLine().append("AS '").append(getBody()).append("';");
return builder.build();
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class AggregateMetadata method describe.
@NonNull
@Override
default String describe(boolean pretty) {
ScriptBuilder builder = new ScriptBuilder(pretty);
builder.append("CREATE AGGREGATE ").append(getKeyspace()).append(".").append(getSignature().getName()).append("(");
boolean first = true;
for (int i = 0; i < getSignature().getParameterTypes().size(); i++) {
if (first) {
first = false;
} else {
builder.append(",");
}
DataType type = getSignature().getParameterTypes().get(i);
builder.append(type.asCql(false, pretty));
}
builder.increaseIndent().append(")").newLine().append("SFUNC ").append(getStateFuncSignature().getName()).newLine().append("STYPE ").append(getStateType().asCql(false, pretty));
if (getFinalFuncSignature().isPresent()) {
builder.newLine().append("FINALFUNC ").append(getFinalFuncSignature().get().getName());
}
if (getInitCond().isPresent()) {
Optional<String> formatInitCond = formatInitCond();
assert formatInitCond.isPresent();
builder.newLine().append("INITCOND ").append(formatInitCond.get());
}
return builder.append(";").build();
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class FunctionMetadata method describe.
@NonNull
@Override
default String describe(boolean pretty) {
ScriptBuilder builder = new ScriptBuilder(pretty);
builder.append("CREATE FUNCTION ").append(getKeyspace()).append(".").append(getSignature().getName()).append("(");
boolean first = true;
for (int i = 0; i < getSignature().getParameterTypes().size(); i++) {
if (first) {
first = false;
} else {
builder.append(",");
}
DataType type = getSignature().getParameterTypes().get(i);
CqlIdentifier name = getParameterNames().get(i);
builder.append(name).append(" ").append(type.asCql(false, pretty));
}
return builder.append(")").increaseIndent().newLine().append(isCalledOnNullInput() ? "CALLED ON NULL INPUT" : "RETURNS NULL ON NULL INPUT").newLine().append("RETURNS ").append(getReturnType().asCql(false, true)).newLine().append("LANGUAGE ").append(getLanguage()).newLine().append("AS '").append(getBody()).append("';").build();
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class GettableByIndex method getInt.
/**
* Returns the {@code i}th value as a Java primitive integer.
*
* <p>By default, this works with CQL type {@code int}.
*
* <p>Note that, due to its signature, this method cannot return {@code null}. If the CQL value is
* {@code NULL}, it will return {@code 0}. If this doesn't work for you, either call {@link
* #isNull(int)} before calling this method, or use {@code get(i, Integer.class)} instead.
*
* @throws IndexOutOfBoundsException if the index is invalid.
*/
default int getInt(int i) {
DataType cqlType = getType(i);
TypeCodec<Integer> codec = codecRegistry().codecFor(cqlType, Integer.class);
if (codec instanceof PrimitiveIntCodec) {
return ((PrimitiveIntCodec) codec).decodePrimitive(getBytesUnsafe(i), protocolVersion());
} else {
Integer value = get(i, codec);
return value == null ? 0 : value;
}
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class GettableByIndex method getObject.
/**
* Returns the {@code i}th value, converting it to the most appropriate Java type.
*
* <p>The {@link #codecRegistry()} will be used to look up a codec to handle the conversion.
*
* <p>Use this method to dynamically inspect elements when types aren't known in advance, for
* instance if you're writing a generic row logger. If you know the target Java type, it is
* generally preferable to use typed variants, such as the ones for built-in types ({@link
* #getBoolean(int)}, {@link #getInt(int)}, etc.), or {@link #get(int, Class)} and {@link
* #get(int, GenericType)} for custom types.
*
* <p>The definition of "most appropriate" is unspecified, and left to the appreciation of the
* {@link #codecRegistry()} implementation. By default, the driver uses the mapping described in
* the other {@code getXxx()} methods (for example {@link #getString(int) String for text, varchar
* and ascii}, etc).
*
* @throws IndexOutOfBoundsException if the index is invalid.
* @throws CodecNotFoundException if no codec can perform the conversion.
*/
@Nullable
default Object getObject(int i) {
DataType cqlType = getType(i);
TypeCodec<?> codec = codecRegistry().codecFor(cqlType);
return codec.decode(getBytesUnsafe(i), protocolVersion());
}
Aggregations