Class JdbcSchema

  • All Implemented Interfaces:
    Schema

    public class JdbcSchema
    extends java.lang.Object
    implements Schema
    Implementation of Schema 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.

    • 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)
      static 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.
      private Pair<java.lang.String,​java.lang.String> getCatalogSchema​(java.sql.Connection connection)
      Returns a pair of (catalog, schema) for the current connection.
      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 as Schema.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)  
      private java.util.List<java.lang.Integer> version​(java.sql.DatabaseMetaData metaData)
      Returns [major, minor] version from a database metadata.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • dataSource

        final javax.sql.DataSource dataSource
      • catalog

        final java.lang.String catalog
      • schema

        final java.lang.String schema
      • tableMap

        private com.google.common.collect.ImmutableMap<java.lang.String,​JdbcTable> tableMap
      • snapshot

        private final boolean snapshot
      • THREAD_METADATA

        public static final java.lang.ThreadLocal<JdbcSchema.Foo> THREAD_METADATA
      • VERSION_ORDERING

        private static final com.google.common.collect.Ordering<java.lang.Iterable<java.lang.Integer>> VERSION_ORDERING
    • 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 source
        dialect - SQL dialect
        convention - Calling convention
        catalog - Catalog name, or null
        schema - 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 schema
        name - Name
        operand - Map of property/value pairs
        Returns:
        A JdbcSchema
      • createDialect

        @Deprecated
        public static SqlDialect createDialect​(javax.sql.DataSource dataSource)
        Returns 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 as Schema.getTable(String).

        Even if this method returns true, the maps are not modified. Calcite stores the defined objects in a wrapper object.

        Specified by:
        isMutable in interface Schema
        Returns:
        Whether the user is allowed to create new tables, functions and sub-schemas in this schema
      • 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.
        Specified by:
        snapshot in interface Schema
        Parameters:
        version - The current schema version
        Returns:
        the schema snapshot.
      • 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 interface Schema
        Parameters:
        parentSchema - Parent schema
        name - 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 interface Schema
        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 interface Schema
        Returns:
        Names of the functions in this schema
      • computeTables

        private com.google.common.collect.ImmutableMap<java.lang.String,​JdbcTable> computeTables()
      • version

        private java.util.List<java.lang.Integer> version​(java.sql.DatabaseMetaData metaData)
                                                   throws java.sql.SQLException
        Returns [major, minor] version from a database metadata.
        Throws:
        java.sql.SQLException
      • getCatalogSchema

        private Pair<java.lang.String,​java.lang.String> getCatalogSchema​(java.sql.Connection connection)
                                                                        throws java.sql.SQLException
        Returns a pair of (catalog, schema) for the current connection.
        Throws:
        java.sql.SQLException
      • 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.
        Specified by:
        getTable in interface Schema
        Parameters:
        name - Table name
        Returns:
        Table, or null
      • 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 interface Schema
        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.
        Specified by:
        getType in interface Schema
        Parameters:
        name - Table name
        Returns:
        Table, or null
      • 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 interface Schema
        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 interface Schema
        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 interface Schema
        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)