Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.jdbc;
import org.apache.calcite.DataContext;
import org.apache.calcite.adapter.java.AbstractQueryableTable;
import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.avatica.AvaticaStatement;
import org.apache.calcite.avatica.AvaticaUtils;
import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.Meta;
import org.apache.calcite.avatica.MetaImpl;
import org.apache.calcite.avatica.NoSuchStatementException;
import org.apache.calcite.avatica.QueryState;
import org.apache.calcite.avatica.remote.TypedValue;
import org.apache.calcite.jdbc.CalcitePrepare.Context;
import org.apache.calcite.linq4j.Enumerable;
import org.apache.calcite.linq4j.Enumerator;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.QueryProvider;
import org.apache.calcite.linq4j.Queryable;
import org.apache.calcite.linq4j.function.Function1;
import org.apache.calcite.linq4j.function.Functions;
import org.apache.calcite.linq4j.function.Predicate1;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeFactoryImpl;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.runtime.FlatLists;
import org.apache.calcite.runtime.Hook;
import org.apache.calcite.schema.Schema;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.schema.Table;
import org.apache.calcite.schema.impl.AbstractTableQueryable;
import org.apache.calcite.server.CalciteServerStatement;
import org.apache.calcite.sql.SqlJdbcFunctionCall;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.util.Holder;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* Helper for implementing the {@code getXxx} methods such as
* {@link org.apache.calcite.avatica.AvaticaDatabaseMetaData#getTables}.
*/
public class CalciteMetaImpl extends MetaImpl {
static final Driver DRIVER = new Driver();
public CalciteMetaImpl(CalciteConnectionImpl connection) {
super(connection);
this.connProps
.setAutoCommit(false)
.setReadOnly(false)
.setTransactionIsolation(Connection.TRANSACTION_NONE);
this.connProps.setDirty(false);
}
static Predicate1 namedMatcher(final Pat pattern) {
if (pattern.s == null || pattern.s.equals("%")) {
return Functions.truePredicate1();
}
final Pattern regex = likeToRegex(pattern);
return v1 -> regex.matcher(v1.getName()).matches();
}
static Predicate1 matcher(final Pat pattern) {
if (pattern.s == null || pattern.s.equals("%")) {
return Functions.truePredicate1();
}
final Pattern regex = likeToRegex(pattern);
return v1 -> regex.matcher(v1).matches();
}
/** Converts a LIKE-style pattern (where '%' represents a wild-card, escaped
* using '\') to a Java regex. */
public static Pattern likeToRegex(Pat pattern) {
StringBuilder buf = new StringBuilder("^");
char[] charArray = pattern.s.toCharArray();
int slash = -2;
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
if (slash == i - 1) {
buf.append('[').append(c).append(']');
} else {
switch (c) {
case '\\':
slash = i;
break;
case '%':
buf.append(".*");
break;
case '[':
buf.append("\\[");
break;
case ']':
buf.append("\\]");
break;
default:
buf.append('[').append(c).append(']');
}
}
}
buf.append("$");
return Pattern.compile(buf.toString());
}
@Override public StatementHandle createStatement(ConnectionHandle ch) {
final StatementHandle h = super.createStatement(ch);
final CalciteConnectionImpl calciteConnection = getConnection();
calciteConnection.server.addStatement(calciteConnection, h);
return h;
}
@Override public void closeStatement(StatementHandle h) {
final CalciteConnectionImpl calciteConnection = getConnection();
final CalciteServerStatement stmt;
try {
stmt = calciteConnection.server.getStatement(h);
} catch (NoSuchStatementException e) {
// statement is not valid; nothing to do
return;
}
// stmt.close(); // TODO: implement
calciteConnection.server.removeStatement(h);
}
private MetaResultSet createResultSet(Enumerable enumerable,
Class clazz, String... names) {
final List columns = new ArrayList<>();
final List fields = new ArrayList<>();
final List 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