Package org.apache.calcite.adapter.jdbc
Class JdbcSchema
- java.lang.Object
-
- org.apache.calcite.adapter.jdbc.JdbcSchema
-
- All Implemented Interfaces:
Schema
public class JdbcSchema extends java.lang.Object implements Schema
Implementation ofSchema
that is backed by a JDBC data source.The tables in the JDBC data source appear to be tables in this schema; queries against this schema are executed against those tables, pushing down as much as possible of the query logic to SQL.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JdbcSchema.Factory
Schema factory that creates aJdbcSchema
.-
Nested classes/interfaces inherited from interface org.apache.calcite.schema.Schema
Schema.TableType
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.lang.String
catalog
(package private) JdbcConvention
convention
(package private) javax.sql.DataSource
dataSource
SqlDialect
dialect
(package private) java.lang.String
schema
private boolean
snapshot
private com.google.common.collect.ImmutableMap<java.lang.String,JdbcTable>
tableMap
-
Constructor Summary
Constructors Modifier Constructor Description JdbcSchema(javax.sql.DataSource dataSource, SqlDialect dialect, JdbcConvention convention, java.lang.String catalog, java.lang.String schema)
Creates a JDBC schema.private
JdbcSchema(javax.sql.DataSource dataSource, SqlDialect dialect, JdbcConvention convention, java.lang.String catalog, java.lang.String schema, com.google.common.collect.ImmutableMap<java.lang.String,JdbcTable> tableMap)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static void
close(java.sql.Connection connection, java.sql.Statement statement, java.sql.ResultSet resultSet)
private com.google.common.collect.ImmutableMap<java.lang.String,JdbcTable>
computeTables()
static JdbcSchema
create(SchemaPlus parentSchema, java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> operand)
Creates a JdbcSchema, taking credentials from a map.static JdbcSchema
create(SchemaPlus parentSchema, java.lang.String name, javax.sql.DataSource dataSource, java.lang.String catalog, java.lang.String schema)
static JdbcSchema
create(SchemaPlus parentSchema, java.lang.String name, javax.sql.DataSource dataSource, SqlDialectFactory dialectFactory, java.lang.String catalog, java.lang.String schema)
static SqlDialect
createDialect(javax.sql.DataSource dataSource)
Deprecated.UsecreateDialect(SqlDialectFactory, DataSource)
insteadstatic SqlDialect
createDialect(SqlDialectFactory dialectFactory, javax.sql.DataSource dataSource)
Returns a suitable SQL dialect for the given data source.static javax.sql.DataSource
dataSource(java.lang.String url, java.lang.String driverClassName, java.lang.String username, java.lang.String password)
Creates a JDBC data source with the given specification.javax.sql.DataSource
getDataSource()
Expression
getExpression(SchemaPlus parentSchema, java.lang.String name)
Returns the expression by which this schema can be referenced in generated code.java.util.Set<java.lang.String>
getFunctionNames()
Returns the names of the functions in this schema.protected com.google.common.collect.Multimap<java.lang.String,Function>
getFunctions()
java.util.Collection<Function>
getFunctions(java.lang.String name)
Returns a list of functions in this schema with the given name, or an empty list if there is no such function.(package private) RelProtoDataType
getRelDataType(java.lang.String catalogName, java.lang.String schemaName, java.lang.String tableName)
(package private) RelProtoDataType
getRelDataType(java.sql.DatabaseMetaData metaData, java.lang.String catalogName, java.lang.String schemaName, java.lang.String tableName)
Schema
getSubSchema(java.lang.String name)
Returns a sub-schema with a given name, or null.java.util.Set<java.lang.String>
getSubSchemaNames()
Returns the names of this schema's child schemas.Table
getTable(java.lang.String name)
Returns a table with a given name, or null if not found.private com.google.common.collect.ImmutableMap<java.lang.String,JdbcTable>
getTableMap(boolean force)
java.util.Set<java.lang.String>
getTableNames()
Returns the names of the tables in this schema.RelProtoDataType
getType(java.lang.String name)
Returns a type with a given name, or null if not found.java.util.Set<java.lang.String>
getTypeNames()
Returns the names of the types in this schema.protected java.util.Map<java.lang.String,RelProtoDataType>
getTypes()
boolean
isMutable()
Returns whether the user is allowed to create new tables, functions and sub-schemas in this schema, in addition to those returned automatically by methods such asSchema.getTable(String)
.private RelDataType
parseTypeString(RelDataTypeFactory typeFactory, java.lang.String typeString)
Given "INTEGER", returns BasicSqlType(INTEGER).Schema
snapshot(SchemaVersion version)
Returns the snapshot of this schema as of the specified time.private RelDataType
sqlType(RelDataTypeFactory typeFactory, int dataType, int precision, int scale, java.lang.String typeString)
-
-
-
Field Detail
-
dataSource
final javax.sql.DataSource dataSource
-
catalog
final java.lang.String catalog
-
schema
final java.lang.String schema
-
dialect
public final SqlDialect dialect
-
convention
final JdbcConvention convention
-
tableMap
private com.google.common.collect.ImmutableMap<java.lang.String,JdbcTable> tableMap
-
snapshot
private final boolean snapshot
-
-
Constructor Detail
-
JdbcSchema
public JdbcSchema(javax.sql.DataSource dataSource, SqlDialect dialect, JdbcConvention convention, java.lang.String catalog, java.lang.String schema)
Creates a JDBC schema.- Parameters:
dataSource
- Data sourcedialect
- SQL dialectconvention
- Calling conventioncatalog
- Catalog name, or nullschema
- Schema name pattern
-
JdbcSchema
private JdbcSchema(javax.sql.DataSource dataSource, SqlDialect dialect, JdbcConvention convention, java.lang.String catalog, java.lang.String schema, com.google.common.collect.ImmutableMap<java.lang.String,JdbcTable> tableMap)
-
-
Method Detail
-
create
public static JdbcSchema create(SchemaPlus parentSchema, java.lang.String name, javax.sql.DataSource dataSource, java.lang.String catalog, java.lang.String schema)
-
create
public static JdbcSchema create(SchemaPlus parentSchema, java.lang.String name, javax.sql.DataSource dataSource, SqlDialectFactory dialectFactory, java.lang.String catalog, java.lang.String schema)
-
create
public static JdbcSchema create(SchemaPlus parentSchema, java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> operand)
Creates a JdbcSchema, taking credentials from a map.- Parameters:
parentSchema
- Parent schemaname
- Nameoperand
- Map of property/value pairs- Returns:
- A JdbcSchema
-
createDialect
@Deprecated public static SqlDialect createDialect(javax.sql.DataSource dataSource)
Deprecated.UsecreateDialect(SqlDialectFactory, DataSource)
insteadReturns a suitable SQL dialect for the given data source.- Parameters:
dataSource
- The data source
-
createDialect
public static SqlDialect createDialect(SqlDialectFactory dialectFactory, javax.sql.DataSource dataSource)
Returns a suitable SQL dialect for the given data source.
-
dataSource
public static javax.sql.DataSource dataSource(java.lang.String url, java.lang.String driverClassName, java.lang.String username, java.lang.String password)
Creates a JDBC data source with the given specification.
-
isMutable
public boolean isMutable()
Description copied from interface:Schema
Returns whether the user is allowed to create new tables, functions and sub-schemas in this schema, in addition to those returned automatically by methods such asSchema.getTable(String)
.Even if this method returns true, the maps are not modified. Calcite stores the defined objects in a wrapper object.
-
snapshot
public Schema snapshot(SchemaVersion version)
Description copied from interface:Schema
Returns the snapshot of this schema as of the specified time. The contents of the schema snapshot should not change over time.
-
getDataSource
public javax.sql.DataSource getDataSource()
-
getExpression
public Expression getExpression(SchemaPlus parentSchema, java.lang.String name)
Description copied from interface:Schema
Returns the expression by which this schema can be referenced in generated code.- Specified by:
getExpression
in interfaceSchema
- Parameters:
parentSchema
- Parent schemaname
- Name of this schema- Returns:
- Expression by which this schema can be referenced in generated code
-
getFunctions
protected com.google.common.collect.Multimap<java.lang.String,Function> getFunctions()
-
getFunctions
public final java.util.Collection<Function> getFunctions(java.lang.String name)
Description copied from interface:Schema
Returns a list of functions in this schema with the given name, or an empty list if there is no such function.- Specified by:
getFunctions
in interfaceSchema
- Parameters:
name
- Name of function- Returns:
- List of functions with given name, or empty list
-
getFunctionNames
public final java.util.Set<java.lang.String> getFunctionNames()
Description copied from interface:Schema
Returns the names of the functions in this schema.- Specified by:
getFunctionNames
in interfaceSchema
- Returns:
- Names of the functions in this schema
-
computeTables
private com.google.common.collect.ImmutableMap<java.lang.String,JdbcTable> computeTables()
-
getTable
public Table getTable(java.lang.String name)
Description copied from interface:Schema
Returns a table with a given name, or null if not found.
-
getTableMap
private com.google.common.collect.ImmutableMap<java.lang.String,JdbcTable> getTableMap(boolean force)
-
getRelDataType
RelProtoDataType getRelDataType(java.lang.String catalogName, java.lang.String schemaName, java.lang.String tableName) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getRelDataType
RelProtoDataType getRelDataType(java.sql.DatabaseMetaData metaData, java.lang.String catalogName, java.lang.String schemaName, java.lang.String tableName) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
sqlType
private RelDataType sqlType(RelDataTypeFactory typeFactory, int dataType, int precision, int scale, java.lang.String typeString)
-
parseTypeString
private RelDataType parseTypeString(RelDataTypeFactory typeFactory, java.lang.String typeString)
Given "INTEGER", returns BasicSqlType(INTEGER). Given "VARCHAR(10)", returns BasicSqlType(VARCHAR, 10). Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2).
-
getTableNames
public java.util.Set<java.lang.String> getTableNames()
Description copied from interface:Schema
Returns the names of the tables in this schema.- Specified by:
getTableNames
in interfaceSchema
- Returns:
- Names of the tables in this schema
-
getTypes
protected java.util.Map<java.lang.String,RelProtoDataType> getTypes()
-
getType
public RelProtoDataType getType(java.lang.String name)
Description copied from interface:Schema
Returns a type with a given name, or null if not found.
-
getTypeNames
public java.util.Set<java.lang.String> getTypeNames()
Description copied from interface:Schema
Returns the names of the types in this schema.- Specified by:
getTypeNames
in interfaceSchema
- Returns:
- Names of the tables in this schema
-
getSubSchema
public Schema getSubSchema(java.lang.String name)
Description copied from interface:Schema
Returns a sub-schema with a given name, or null.- Specified by:
getSubSchema
in interfaceSchema
- Parameters:
name
- Sub-schema name- Returns:
- Sub-schema with a given name, or null
-
getSubSchemaNames
public java.util.Set<java.lang.String> getSubSchemaNames()
Description copied from interface:Schema
Returns the names of this schema's child schemas.- Specified by:
getSubSchemaNames
in interfaceSchema
- Returns:
- Names of this schema's child schemas
-
close
private static void close(java.sql.Connection connection, java.sql.Statement statement, java.sql.ResultSet resultSet)
-
-