@Override public Date parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // strip enclosing single quotes, if any if (ParseUtils.isQuoted(value)) value = ParseUtils.unquote(value); if (ParseUtils.isLongLiteral(value)) { try { return new Date(Long.parseLong(value)); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse timestamp value from \"%s\"", value)); } } try { return ParseUtils.parseDate(value); } catch (ParseException e) { throw new InvalidTypeException( String.format("Cannot parse timestamp value from \"%s\"", value)); } }
/** * Double quote the given string; double quotes are escaped. If the given string is null, this * method returns a quoted empty string ({@code ""}). * * @param value The value to double quote. * @return The double quoted string. */ public static String doubleQuote(String value) { return quote(value, '"'); }
/** * Quote a keyspace, table or column identifier to make it case sensitive. * * <p>CQL identifiers, including keyspace, table and column ones, are case insensitive by default. * Case sensitive identifiers can however be provided by enclosing the identifier in double quotes * (see the <a href="http://cassandra.apache.org/doc/cql3/CQL.html#identifiers">CQL * documentation</a> for details). If you are using case sensitive identifiers, this method can be * used to enclose such identifiers in double quotes, making them case sensitive. * * <p>Note that <a * href="https://docs.datastax.com/en/cql/3.0/cql/cql_reference/keywords_r.html">reserved CQL * keywords</a> should also be quoted. You can check if a given identifier is a reserved keyword * by calling {@link #isReservedCqlKeyword(String)}. * * @param id the keyspace or table identifier. * @return {@code id} enclosed in double-quotes, for use in methods like {@link #getReplicas}, * {@link #getKeyspace}, {@link KeyspaceMetadata#getTable} or even {@link * Cluster#connect(String)}. */ public static String quote(String id) { return ParseUtils.doubleQuote(id); }
@Override public String format(Long value) { if (value == null) return "NULL"; return ParseUtils.quote(ParseUtils.formatTime(value)); } }
@Override public java.time.Instant parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // strip enclosing single quotes, if any if (isQuoted(value)) value = unquote(value); if (isLongLiteral(value)) { try { return java.time.Instant.ofEpochMilli(parseLong(value)); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse timestamp value from \"%s\"", value)); } } try { return java.time.Instant.from(PARSER.parse(value)); } catch (java.time.format.DateTimeParseException e) { throw new InvalidTypeException( String.format("Cannot parse timestamp value from \"%s\"", value)); } } }
@Override public Long parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // enclosing single quotes required, even for long literals if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("time values must be enclosed by single quotes"); value = value.substring(1, value.length() - 1); if (ParseUtils.isLongLiteral(value)) { try { return Long.parseLong(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse time value from \"%s\"", value), e); } } try { return ParseUtils.parseTime(value); } catch (ParseException e) { throw new InvalidTypeException( String.format("Cannot parse time value from \"%s\"", value), e); } }
int idx = ParseUtils.skipSpaces(value, 0); if (value.charAt(idx++) != '(') throw new InvalidTypeException( value, idx, value.charAt(idx))); idx = ParseUtils.skipSpaces(value, idx); int n; try { n = ParseUtils.skipCQLValue(value, idx); } catch (IllegalArgumentException e) { throw new InvalidTypeException( i += 1; idx = ParseUtils.skipSpaces(value, idx); if (value.charAt(idx) == ')') return v; if (value.charAt(idx) != ',') idx = ParseUtils.skipSpaces(value, idx);
if (!isQuoted(text, quoteChar)) return text; String search = emptyQuoted(quoteChar); int nbMatch = 0; int start = -1;
/** * Return {@code true} if the given string is surrounded by double quotes, and {@code false} * otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded by double quotes, and {@code false} * otherwise. */ public static boolean isDoubleQuoted(String value) { return isQuoted(value, '\"'); }
/** * Unquote the given string if it is quoted; single quotes are unescaped. If the given string is * not quoted, it is returned without any modification. * * @param value The string to unquote. * @return The unquoted string. */ public static String unquote(String value) { return unquote(value, '\''); }
int idx = ParseUtils.skipSpaces(value, 0); if (value.charAt(idx++) != '{') throw new InvalidTypeException( idx = ParseUtils.skipSpaces(value, idx); n = ParseUtils.skipCQLId(value, idx); } catch (IllegalArgumentException e) { throw new InvalidTypeException( idx = ParseUtils.skipSpaces(value, idx); if (value.charAt(idx++) != ':') throw new InvalidTypeException( "Cannot parse UDT value from \"%s\", at character %d expecting ':' but got '%c'", value, idx, value.charAt(idx))); idx = ParseUtils.skipSpaces(value, idx); n = ParseUtils.skipCQLValue(value, idx); } catch (IllegalArgumentException e) { throw new InvalidTypeException( idx = ParseUtils.skipSpaces(value, idx); if (value.charAt(idx) == '}') return v; if (value.charAt(idx) != ',') idx = ParseUtils.skipSpaces(value, idx);
@Override public java.time.LocalTime parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // enclosing single quotes required, even for long literals if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("time values must be enclosed by single quotes"); value = value.substring(1, value.length() - 1); if (ParseUtils.isLongLiteral(value)) { long nanosOfDay; try { nanosOfDay = Long.parseLong(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse time value from \"%s\"", value), e); } return java.time.LocalTime.ofNanoOfDay(nanosOfDay); } try { return java.time.LocalTime.parse(value); } catch (RuntimeException e) { throw new InvalidTypeException( String.format("Cannot parse time value from \"%s\"", value), e); } } }
static String handleId(String id) { // Shouldn't really happen for this method, but no reason to fail here if (id == null) return null; boolean isAlphanumericLowCase = true; boolean isAlphanumeric = true; for (int i = 0; i < id.length(); i++) { char c = id.charAt(i); if (c >= 65 && c <= 90) { // A-Z isAlphanumericLowCase = false; } else if (!((c >= 48 && c <= 57) // 0-9 || (c == 95) // _ (underscore) || (c >= 97 && c <= 122) // a-z )) { isAlphanumeric = false; isAlphanumericLowCase = false; break; } } if (isAlphanumericLowCase) { return id; } if (isAlphanumeric) { return id.toLowerCase(); } // Check if it's enclosed in quotes. If it is, remove them and unescape internal double quotes return ParseUtils.unDoubleQuote(id); }
String parseTypeName() { idx = skipSpaces(str, idx); return readNextIdentifier(); }
@Test(groups = "unit") public void should_round_up() throws ParseException { Date timestamp = parseDate("2011-02-03T04:05:16.789Z"); assertThat(MILLISECOND.roundUp(timestamp)).isEqualTo(parseDate("2011-02-03T04:05:16.789Z")); assertThat(SECOND.roundUp(timestamp)).isEqualTo(parseDate("2011-02-03T04:05:16.999Z")); assertThat(MINUTE.roundUp(timestamp)).isEqualTo(parseDate("2011-02-03T04:05:59.999Z")); assertThat(HOUR.roundUp(timestamp)).isEqualTo(parseDate("2011-02-03T04:59:59.999Z")); assertThat(DAY.roundUp(timestamp)).isEqualTo(parseDate("2011-02-03T23:59:59.999Z")); assertThat(MONTH.roundUp(timestamp)).isEqualTo(parseDate("2011-02-28T23:59:59.999Z")); assertThat(YEAR.roundUp(timestamp)).isEqualTo(parseDate("2011-12-31T23:59:59.999Z")); }
if (text == null || text.isEmpty()) return emptyQuoted(quoteChar);
private int skipLiteral(String value, int idx) { try { return ParseUtils.skipCQLValue(value, idx); } catch (IllegalArgumentException e) { throw new InvalidTypeException( String.format( "Cannot parse list value from \"%s\", invalid CQL value at character %d", value, idx), e); } } }
@Override public Timestamp parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // strip enclosing single quotes, if any if (isQuoted(value)) value = unquote(value); if (isLongLiteral(value)) { try { return new Timestamp(parseLong(value)); } catch (NumberFormatException e) { throw new InvalidTypeException(String.format("Cannot parse timestamp value from \"%s\"", value)); } } try { return new Timestamp(PARSER.parseMillis(value)); } catch (RuntimeException e) { throw new InvalidTypeException(String.format("Cannot parse timestamp value from \"%s\"", value)); } } }