private AkCollator collator(
     ComparisonCondition cond, TPreparedExpression left, TPreparedExpression right) {
   TInstance leftInstance = left.resultType();
   TInstance rightInstance = right.resultType();
   TClass tClass = leftInstance.typeClass();
   assert tClass.compatibleForCompare(rightInstance.typeClass())
       : tClass + " != " + rightInstance.typeClass();
   if (tClass.underlyingType() != UnderlyingType.STRING) return null;
   CharacterTypeAttributes leftAttributes = StringAttribute.characterTypeAttributes(leftInstance);
   CharacterTypeAttributes rightAttributes =
       StringAttribute.characterTypeAttributes(rightInstance);
   return TString.mergeAkCollators(leftAttributes, rightAttributes);
 }
 private static Value convertFromObject(Object object, TInstance type, QueryContext queryContext) {
   Value in = fromObject(object);
   TInstance inType = in.getType();
   Value out = null;
   if (!inType.equals(type)) {
     TExecutionContext context =
         new TExecutionContext(Collections.singletonList(in.getType()), type, queryContext);
     out = new Value(type);
     type.typeClass().fromObject(context, in, out);
   } else {
     out = in;
   }
   return out;
 }
 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());
   }
 }
 private TPreparedExpression assembleCastExpression(
     CastExpression castExpression,
     ColumnExpressionContext columnContext,
     SubqueryOperatorAssembler subqueryAssembler) {
   TInstance toType = castExpression.getType();
   TPreparedExpression expr =
       assembleExpression(castExpression.getOperand(), columnContext, subqueryAssembler);
   if (toType == null) return expr;
   TInstance sourceInstance = expr.resultType();
   if (sourceInstance == null) // CAST(NULL as FOOTYPE)
   {
     toType = toType.withNullable(true);
     return new TNullExpression(toType);
   } else if (!toType.equals(sourceInstance)) {
     // Do type conversion.
     TCast tcast = registryService.getCastsResolver().cast(sourceInstance, toType);
     if (tcast == null) {
       throw new NoSuchCastException(sourceInstance, toType);
     }
     expr = new TCastExpression(expr, tcast, toType);
   }
   return expr;
 }
 /** Give a <code>ValueSource</code> whose {@link #jdbcType} claims
  * to be one of the integer types (<code>TINYINT</code>,
  * <code>SMALLINT</code>, <code>INTEGER</code>,
  * <code>BIGINT<code>), get the integer value.  Needed because of
  * <code>UNSIGNED</code> and <code>YEAR</code> types.
  */
 public long getIntegerValue(ValueSource value) {
   switch (TInstance.underlyingType(value.getType())) {
     case INT_8:
       return value.getInt8();
     case INT_16:
       return value.getInt16();
     case UINT_16:
       return value.getUInt16();
     case INT_32:
       return value.getInt32();
     case INT_64:
     default:
       return value.getInt64();
   }
 }
 /** Does this type represent a signed numeric type? */
 public boolean isTypeSigned(TInstance type) {
   TClass tclass = TInstance.tClass(type);
   if (tclass == null) return false;
   switch (tclass.jdbcType()) {
     case Types.BIGINT:
     case Types.DECIMAL:
     case Types.DOUBLE:
     case Types.FLOAT:
     case Types.INTEGER:
     case Types.NUMERIC:
     case Types.REAL:
     case Types.SMALLINT:
     case Types.TINYINT:
       return true;
     default:
       return false;
   }
 }
 /** @see #getIntegerValue */
 public void setIntegerValue(ValueTarget target, long value) {
   switch (TInstance.underlyingType(target.getType())) {
     case INT_8:
       target.putInt8((byte) value);
       break;
     case INT_16:
       target.putInt16((short) value);
       break;
     case UINT_16:
       target.putUInt16((char) value);
       break;
     case INT_32:
       target.putInt32((int) value);
       break;
     case INT_64:
     default:
       target.putInt64(value);
   }
 }
  public Class<?> jdbcClass(TInstance type) {
    TClass tclass = TInstance.tClass(type);
    if (tclass == null) return Object.class;
    int jdbcType = tclass.jdbcType();
    switch (jdbcType) {
      case Types.DECIMAL:
      case Types.NUMERIC:
        return BigDecimal.class;
      case Types.BOOLEAN:
        return Boolean.class;
      case Types.TINYINT:
        return Byte.class;
      case Types.BINARY:
      case Types.BIT:
      case Types.LONGVARBINARY:
      case Types.VARBINARY:
      case Types.BLOB:
        return byte[].class;
      case Types.DATE:
        return java.sql.Date.class;
      case Types.DOUBLE:
        return Double.class;
      case Types.FLOAT:
      case Types.REAL:
        return Float.class;
      case Types.INTEGER:
        return Integer.class;
      case Types.BIGINT:
        return Long.class;
      case Types.SMALLINT:
        return Short.class;
      case Types.CHAR:
      case Types.LONGNVARCHAR:
      case Types.LONGVARCHAR:
      case Types.NCHAR:
      case Types.NVARCHAR:
      case Types.VARCHAR:
      case Types.CLOB:
        return String.class;
      case Types.TIME:
        return java.sql.Time.class;
      case Types.TIMESTAMP:
        return java.sql.Timestamp.class;

        /*
        case Types.ARRAY:
            return java.sql.Array.class;
        case Types.BLOB:
            return java.sql.Blob.class;
        case Types.CLOB:
            return java.sql.Clob.class;
        case Types.NCLOB:
            return java.sql.NClob.class;
        case Types.REF:
            return java.sql.Ref.class;
        case Types.ROWID:
            return java.sql.RowId.class;
        case Types.SQLXML:
            return java.sql.SQLXML.class;
        */

      case Types.NULL:
      case Types.DATALINK:
      case Types.DISTINCT:
      case Types.JAVA_OBJECT:
      case Types.OTHER:
      case Types.STRUCT:
      default:
        break;
    }
    if (tclass == AkResultSet.INSTANCE) return java.sql.ResultSet.class;
    return Object.class;
  }
 public int jdbcType(TInstance type) {
   TClass tclass = TInstance.tClass(type);
   if (tclass == null) return Types.OTHER;
   else return tclass.jdbcType();
 }
 public boolean isTypeUnsigned(TInstance type) {
   TClass tclass = TInstance.tClass(type);
   if (tclass == null) return false;
   else return tclass.isUnsigned();
 }
 public static Value valuefromObject(Object object, TInstance type, QueryContext queryContext) {
   Value value = new Value(type);
   if (object == null) {
     value.putNull();
   } else if (object instanceof String) {
     // This is the common case, so let's test for it first
     if (TInstance.underlyingType(type) == UnderlyingType.STRING)
       value.putString((String) object, null);
     else if (type == null) {
       type =
           MString.VARCHAR.instance(
               ((String) object).length(),
               StringFactory.DEFAULT_CHARSET.ordinal(),
               StringFactory.NULL_COLLATION_ID,
               false);
       value = new Value(type, (String) object);
     }
   } else if (type == null) {
     value = fromObject(object);
   } else if (object instanceof BlobRef) {
     value.putObject(object);
   } else {
     switch (TInstance.underlyingType(type)) {
       case INT_8:
       case INT_16:
       case UINT_16:
       case INT_32:
       case INT_64:
         if (object instanceof Number) valueFromLong(((Number) object).longValue(), value);
         break;
       case FLOAT:
         if (object instanceof Number) value.putFloat(((Number) object).floatValue());
         break;
       case DOUBLE:
         if (object instanceof Number) value.putDouble(((Number) object).doubleValue());
         break;
       case BYTES:
         if (object instanceof byte[]) value.putBytes((byte[]) object);
         else if (object instanceof ByteSource)
           value.putBytes(((ByteSource) object).toByteSubarray());
         else if (object instanceof JTSSpatialObject) {
           if (AkBlob.isBlob(type.typeClass())) {
             byte[] content = Spatial.serializeWKB((JTSSpatialObject) object);
             value.putObject(new BlobRef(content));
           }
         }
         break;
       case STRING:
         if ((object instanceof JTSSpatialObject)) {
           value = new Value(type, Spatial.serializeWKT((JTSSpatialObject) object));
         } else {
           throw new IllegalArgumentException("Unsafe toString(): " + object.getClass());
         }
         break;
       case BOOL:
         if (object instanceof Boolean) value.putBool((Boolean) object);
         break;
     }
   }
   if (!value.hasAnyValue()) {
     if (type == null) {
       value = fromObject(object);
     } else {
       value = convertFromObject(object, type, queryContext);
     }
   }
   return value;
 }
 public static UnderlyingType underlyingType(ValueSource source) {
   return TInstance.underlyingType(source.getType());
 }
 public static TClass tClass(ValueSource source) {
   return TInstance.tClass(source.getType());
 }
 private TClass tClass(TPreparedExpression left) {
   TInstance type = left.resultType();
   return type == null ? null : type.typeClass();
 }