Java Code Examples for org.apache.calcite.util.NlsString#getCollation()

The following examples show how to use org.apache.calcite.util.NlsString#getCollation() . 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 File: SqlUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the type of an {@link org.apache.calcite.util.NlsString}.
 *
 * <p>The type inherits the The NlsString's {@link Charset} and
 * {@link SqlCollation}, if they are set, otherwise it gets the system
 * defaults.
 *
 * @param typeFactory Type factory
 * @param str         String
 * @return Type, including collation and charset
 */
public static RelDataType createNlsStringType(
    RelDataTypeFactory typeFactory,
    NlsString str) {
  Charset charset = str.getCharset();
  if (null == charset) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = str.getCollation();
  if (null == collation) {
    collation = SqlCollation.COERCIBLE;
  }
  RelDataType type =
      typeFactory.createSqlType(
          SqlTypeName.CHAR,
          str.getValue().length());
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  return type;
}
 
Example 2
Source File: SqlUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the type of an {@link org.apache.calcite.util.NlsString}.
 *
 * <p>The type inherits the The NlsString's {@link Charset} and
 * {@link SqlCollation}, if they are set, otherwise it gets the system
 * defaults.
 *
 * @param typeFactory Type factory
 * @param str         String
 * @return Type, including collation and charset
 */
public static RelDataType createNlsStringType(
    RelDataTypeFactory typeFactory,
    NlsString str) {
  Charset charset = str.getCharset();
  if (null == charset) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = str.getCollation();
  if (null == collation) {
    collation = SqlCollation.COERCIBLE;
  }
  RelDataType type =
      typeFactory.createSqlType(
          SqlTypeName.CHAR,
          str.getValue().length());
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  return type;
}
 
Example 3
Source File: SqlLiteralChainOperator.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startList("", "");
  SqlCollation collation = null;
  for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
    SqlLiteral rand = (SqlLiteral) operand.e;
    if (operand.i > 0) {
      // SQL:2003 says there must be a newline between string
      // fragments.
      writer.newlineAndIndent();
    }
    if (rand instanceof SqlCharStringLiteral) {
      NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
      if (operand.i == 0) {
        collation = nls.getCollation();

        // print with prefix
        writer.literal(nls.asSql(true, false));
      } else {
        // print without prefix
        writer.literal(nls.asSql(false, false));
      }
    } else if (operand.i == 0) {
      // print with prefix
      rand.unparse(writer, leftPrec, rightPrec);
    } else {
      // print without prefix
      if (rand.getTypeName() == SqlTypeName.BINARY) {
        BitString bs = (BitString) rand.getValue();
        writer.literal("'" + bs.toHexString() + "'");
      } else {
        writer.literal("'" + rand.toValue() + "'");
      }
    }
  }
  if (collation != null) {
    collation.unparse(writer, 0, 0);
  }
  writer.endList(frame);
}
 
Example 4
Source File: SqlLiteralChainOperator.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startList("", "");
  SqlCollation collation = null;
  for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
    SqlLiteral rand = (SqlLiteral) operand.e;
    if (operand.i > 0) {
      // SQL:2003 says there must be a newline between string
      // fragments.
      writer.newlineAndIndent();
    }
    if (rand instanceof SqlCharStringLiteral) {
      NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
      if (operand.i == 0) {
        collation = nls.getCollation();

        // print with prefix
        writer.literal(nls.asSql(true, false, writer.getDialect()));
      } else {
        // print without prefix
        writer.literal(nls.asSql(false, false, writer.getDialect()));
      }
    } else if (operand.i == 0) {
      // print with prefix
      rand.unparse(writer, leftPrec, rightPrec);
    } else {
      // print without prefix
      if (rand.getTypeName() == SqlTypeName.BINARY) {
        BitString bs = (BitString) rand.getValue();
        writer.literal("'" + bs.toHexString() + "'");
      } else {
        writer.literal("'" + rand.toValue() + "'");
      }
    }
  }
  if (collation != null) {
    collation.unparse(writer);
  }
  writer.endList(frame);
}