@SuppressWarnings({ "rawtypes", "unchecked" }) private MetaResultSet createResultSet(List iterable, Class clazz, String... names) { final List<ColumnMetaData> columns = new ArrayList<ColumnMetaData>(); final List<Field> fields = new ArrayList<Field>(); final List<String> fieldNames = new ArrayList<String>(); for (String name : names) { final int index = fields.size(); final String fieldName = AvaticaUtils.toCamelCase(name); final Field field; try { field = clazz.getField(fieldName); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } columns.add(columnMetaData(name, index, field.getType(), true)); fields.add(field); fieldNames.add(fieldName); } CursorFactory cursorFactory = CursorFactory.record(clazz, fields, fieldNames); Signature signature = new Signature(columns, "", null, Collections.<String, Object> emptyMap(), cursorFactory, StatementType.SELECT); StatementHandle sh = this.createStatement(connection().handle); Frame frame = new Frame(0, true, iterable); return MetaResultSet.create(connection().id, sh.id, true, signature, frame); }
protected static ColumnMetaData.StructType drillFieldMetaData(Class<?> clazz) { final List<ColumnMetaData> list = new ArrayList<>(); for (Field field : clazz.getFields()) { if (Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) { NotNull notNull = field.getAnnotation(NotNull.class); boolean notNullable = (notNull != null || field.getType().isPrimitive()); list.add( drillColumnMetaData( AvaticaUtils.camelToUpper(field.getName()), list.size(), field.getType(), notNullable)); } } return ColumnMetaData.struct(list); }
/** Builds a lattice. */ public Lattice build() { LatticeStatisticProvider.Factory statisticProvider = this.statisticProvider != null ? AvaticaUtils.instantiatePlugin( LatticeStatisticProvider.Factory.class, this.statisticProvider) : Lattices.CACHED_SQL; Preconditions.checkArgument(rootSchema.isRoot(), "must be root schema"); return new Lattice(rootSchema, ImmutableList.copyOf(nodes), auto, algorithm, algorithmMaxMillis, statisticProvider, rowCountEstimate, columns, defaultMeasureListBuilder.build(), tileListBuilder.build()); }
/** * Converts an array of {@code long} values to an array of {@code int} * values, truncating values outside the legal range for an {@code int} * to {@link Integer#MIN_VALUE} or {@link Integer#MAX_VALUE}. * * @param longs An array of {@code long}s * @return An array of {@code int}s */ public static int[] toSaturatedInts(long[] longs) { final int[] ints = new int[longs.length]; for (int i = 0; i < longs.length; i++) { ints[i] = toSaturatedInt(longs[i]); } return ints; }
<T> DatabaseProperty(T defaultValue) { this.defaultValue = defaultValue; final String methodName = AvaticaUtils.toCamelCase(name()); try { this.method = DatabaseMetaData.class.getMethod(methodName); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } this.type = AvaticaUtils.box(method.getReturnType()); assert defaultValue == null || defaultValue.getClass() == type; }
private InputStream traceResponse(InputStream in) { if (CalcitePrepareImpl.DEBUG) { try { final byte[] bytes = AvaticaUtils.readFullyToBytes(in); in.close(); System.out.println("Response: " + new String(bytes, StandardCharsets.UTF_8)); // CHECKSTYLE: IGNORE 0 in = new ByteArrayInputStream(bytes); } catch (IOException e) { throw new RuntimeException(e); } } return in; }
private void append(StringBuilder buf, Object o) { if (o == null) { buf.append("null"); } else if (o.getClass().isArray()) { append(buf, AvaticaUtils.primitiveList(o)); } else { buf.append(o); } }
/** Reads the contents of an input stream and returns as a string. */ public static byte[] readFullyToBytes(InputStream inputStream, UnsynchronizedBuffer buffer) throws IOException { // Variant that lets us use a pooled Buffer return _readFully(inputStream, buffer); }
/** Builds a lattice. */ public Lattice build() { LatticeStatisticProvider.Factory statisticProvider = this.statisticProvider != null ? AvaticaUtils.instantiatePlugin( LatticeStatisticProvider.Factory.class, this.statisticProvider) : Lattices.CACHED_SQL; Preconditions.checkArgument(rootSchema.isRoot(), "must be root schema"); final ImmutableList.Builder<Column> columnBuilder = ImmutableList.<Column>builder() .addAll(baseColumns) .addAll(derivedColumnsByName.values()); return new Lattice(rootSchema, rootNode, auto, algorithm, algorithmMaxMillis, statisticProvider, rowCountEstimate, columnBuilder.build(), ImmutableSortedSet.copyOf(defaultMeasureSet), tileListBuilder.build()); }
/** * Converts an array of {@code long} values to an array of {@code int} * values, truncating values outside the legal range for an {@code int} * to {@link Integer#MIN_VALUE} or {@link Integer#MAX_VALUE}. * * @param longs An array of {@code long}s * @return An array of {@code int}s */ public static int[] toSaturatedInts(long[] longs) { final int[] ints = new int[longs.length]; for (int i = 0; i < longs.length; i++) { ints[i] = toSaturatedInt(longs[i]); } return ints; }
<T> DatabaseProperty(T defaultValue) { this.defaultValue = defaultValue; final String methodName = AvaticaUtils.toCamelCase(name()); Method localMethod = null; try { localMethod = DatabaseMetaData.class.getMethod(methodName); } catch (NoSuchMethodException e) { // Pass, localMethod stays null. } if (null == localMethod) { this.method = null; this.type = null; this.isJdbc = false; } else { this.method = localMethod; this.type = AvaticaUtils.box(method.getReturnType()); this.isJdbc = true; } // It's either: 1) not a JDBC method, 2) has no default value, // 3) the defaultValue is of the expected type assert !isJdbc || defaultValue == null || defaultValue.getClass() == type; }
private InputStream traceResponse(InputStream in) { if (CalcitePrepareImpl.DEBUG) { try { final byte[] bytes = AvaticaUtils.readFullyToBytes(in); in.close(); System.out.println("Response: " + new String(bytes, StandardCharsets.UTF_8)); in = new ByteArrayInputStream(bytes); } catch (IOException e) { throw new RuntimeException(e); } } return in; }
@Override public Object getObject() { final Object object = super.getObject(); if (object == null || object instanceof List) { return object; } // The object can be java array in case of user-provided class for row // storage. return AvaticaUtils.primitiveList(object); }
/** Reads the contents of an input stream and returns as a string. */ public static byte[] readFullyToBytes(InputStream inputStream, UnsynchronizedBuffer buffer) throws IOException { // Variant that lets us use a pooled Buffer return _readFully(inputStream, buffer); }
private static DataSource parseDataSource(Map<String, Object> operand) throws IOException { final String connection = (String) operand.get("connection"); Map<String, Object> jdbcConfig; if (connection != null) { try (InputStream connConfig = ClassLoader.getSystemResourceAsStream(connection)) { jdbcConfig = new ObjectMapper().readValue( connConfig, new TypeReference<Map<String, Object>>(){} ); } } else { jdbcConfig = operand; } final String dataSourceName = (String) jdbcConfig.get("dataSource"); if (dataSourceName != null) { return AvaticaUtils.instantiatePlugin(DataSource.class, dataSourceName); } final String jdbcUrl = (String) jdbcConfig.get("jdbcUrl"); final String jdbcDriver = (String) jdbcConfig.get("jdbcDriver"); final String jdbcUser = (String) jdbcConfig.get("jdbcUser"); final String jdbcPassword = (String) jdbcConfig.get("jdbcPassword"); return dataSource(jdbcUrl, jdbcDriver, jdbcUser, jdbcPassword); }
private <E> MetaResultSet createResultSet(Enumerable<E> enumerable, Class clazz, String... names) { final List<ColumnMetaData> columns = new ArrayList<>(); final List<Field> fields = new ArrayList<>(); final List<String> fieldNames = new ArrayList<>(); for (String name : names) { final int index = fields.size(); final String fieldName = AvaticaUtils.toCamelCase(name); final Field field; try { field = clazz.getField(fieldName); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } columns.add(columnMetaData(name, index, field.getType(), false)); fields.add(field); fieldNames.add(fieldName); } //noinspection unchecked final Iterable<Object> iterable = (Iterable<Object>) (Iterable) enumerable; return createResultSet(Collections.emptyMap(), columns, CursorFactory.record(clazz, fields, fieldNames), new Frame(0, true, iterable)); }
@JsonCreator public PrepareAndExecuteRequest( @JsonProperty("connectionId") String connectionId, @JsonProperty("statementId") int statementId, @JsonProperty("sql") String sql, @JsonProperty("maxRowsTotal") long maxRowCount, @JsonProperty("maxRowsInFirstFrame") int maxRowsInFirstFrame) { this.connectionId = connectionId; this.statementId = statementId; this.sql = sql; this.maxRowCount = maxRowCount; this.maxRowsInFirstFrame = maxRowsInFirstFrame; }
<T> DatabaseProperty(T defaultValue) { this.defaultValue = defaultValue; final String methodName = AvaticaUtils.toCamelCase(name()); Method localMethod = null; try { localMethod = DatabaseMetaData.class.getMethod(methodName); } catch (NoSuchMethodException e) { // Pass, localMethod stays null. } if (null == localMethod) { this.method = null; this.type = null; this.isJdbc = false; } else { this.method = localMethod; this.type = AvaticaUtils.box(method.getReturnType()); this.isJdbc = true; } // It's either: 1) not a JDBC method, 2) has no default value, // 3) the defaultValue is of the expected type assert !isJdbc || defaultValue == null || defaultValue.getClass() == type; }
/** Reads the contents of an input stream and returns as a string. */ public static byte[] readFullyToBytes(InputStream inputStream) throws IOException { return readFullyToBytes(inputStream, new UnsynchronizedBuffer(1024)); }
protected static ColumnMetaData.StructType fieldMetaData(Class clazz) { final List<ColumnMetaData> list = new ArrayList<ColumnMetaData>(); for (Field field : clazz.getFields()) { if (Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) { list.add( columnMetaData( AvaticaUtils.camelToUpper(field.getName()), list.size() + 1, field.getType())); } } return ColumnMetaData.struct(list); }