Java Code Examples for org.apache.calcite.sql.SqlUtil#validateCharset()

The following examples show how to use org.apache.calcite.sql.SqlUtil#validateCharset() . 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: NlsString.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Internal constructor; other constructors must call it. */
private NlsString(String stringValue, ByteString bytesValue,
    String charsetName, SqlCollation collation) {
  if (charsetName != null) {
    this.charsetName = charsetName.toUpperCase(Locale.ROOT);
    this.charset = SqlUtil.getCharset(charsetName);
  } else {
    this.charsetName = null;
    this.charset = null;
  }
  if ((stringValue != null) == (bytesValue != null)) {
    throw new IllegalArgumentException("Specify stringValue or bytesValue");
  }
  if (bytesValue != null) {
    if (charsetName == null) {
      throw new IllegalArgumentException("Bytes value requires charset");
    }
    SqlUtil.validateCharset(bytesValue, charset);
  } else {
    // Java string can be malformed if LATIN1 is required.
    if (this.charsetName != null
        && (this.charsetName.equals("LATIN1")
        || this.charsetName.equals("ISO-8859-1"))) {
      if (!charset.newEncoder().canEncode(stringValue)) {
        throw RESOURCE.charsetEncoding(stringValue, charset.name()).ex();
      }
    }
  }
  this.collation = collation;
  this.stringValue = stringValue;
  this.bytesValue = bytesValue;
}
 
Example 2
Source File: NlsString.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Internal constructor; other constructors must call it. */
private NlsString(String stringValue, ByteString bytesValue,
    String charsetName, SqlCollation collation) {
  if (charsetName != null) {
    this.charsetName = charsetName.toUpperCase(Locale.ROOT);
    this.charset = SqlUtil.getCharset(charsetName);
  } else {
    this.charsetName = null;
    this.charset = null;
  }
  if ((stringValue != null) == (bytesValue != null)) {
    throw new IllegalArgumentException("Specify stringValue or bytesValue");
  }
  if (bytesValue != null) {
    if (charsetName == null) {
      throw new IllegalArgumentException("Bytes value requires charset");
    }
    SqlUtil.validateCharset(bytesValue, charset);
  } else {
    // Java string can be malformed if LATIN1 is required.
    if (this.charsetName != null
        && (this.charsetName.equals("LATIN1")
        || this.charsetName.equals("ISO-8859-1"))) {
      if (!charset.newEncoder().canEncode(stringValue)) {
        throw RESOURCE.charsetEncoding(stringValue, charset.name()).ex();
      }
    }
  }
  this.collation = collation;
  this.stringValue = stringValue;
  this.bytesValue = bytesValue;
}