Class JdbcDocumentCollection

  • All Implemented Interfaces:
    DocumentCollection, DocumentSequence, SafelyCloseable, FlyweightPrototype<DocumentCollection>, Closeable, Serializable, AutoCloseable

    public class JdbcDocumentCollection
    extends AbstractDocumentCollection
    implements Serializable
    A DocumentCollection corresponding to the result of a query in a relational database.

    An instance of this class is based on a query. The query should produce two fixed columns: the first, named id, must be an increasing integer which act as an identifier (i.e., as a key); the second, named title, must be a text field and will be used as a title. The remaining columns will be indexed, and the name of the corresponding field will be the name of the column (use judiciously AS).

    In complex queries, the specification id for the first column could be ambiguous; in that case, you can provide an alternate (and hopefully more precise) specification.

    At construction time, the query is executed, obtaining a bijection between the values of the identifier and document indices. The bijection is exposed by the methods id2doc(int) and doc2id(int). The class tolerates additions to the database (and they will be skipped), but deletions will cause errors.

    This class provides a main method with a flexible syntax that serialises a query into a document collection.

    See Also:
    Serialized Form
    • Field Detail

      • id2doc

        protected final Int2IntMap id2doc
        The map from database identifiers to documents.
      • doc2id

        protected final int[] doc2id
        The map (as an array) from documents to database identifiers.
      • dbUri

        protected final String dbUri
        The URI pointing at the database.
      • factory

        protected final DocumentFactory factory
        The factory to be used by this collection.
      • select

        protected final String select
        The query generating the collection (without the SELECT keyword).
      • idSpec

        protected final String idSpec
        The spec for the id field; by default it is id, but in complex query it could be ambiguous.
      • where

        protected final String where
        The WHERE part of the query generating the collection (without the WHERE keyword), or null.
      • connection

        protected transient Connection connection
        The currently open connection, if any.
    • Constructor Detail

      • JdbcDocumentCollection

        public JdbcDocumentCollection​(String dbUri,
                                      String jdbcDriverName,
                                      String select,
                                      String where,
                                      DocumentFactory factory)
                               throws SQLException,
                                      ClassNotFoundException
        Creates a document collection based on the result set of an SQL query using id as id specifier.

        Beware. This class is not guaranteed to work if the database is deleted or modified after creation!

        Parameters:
        dbUri - a JDBC URI pointing at the database.
        jdbcDriverName - the name of a JDBC driver, or null if you do not want to load a driver.
        select - the SQL query generating the collection (without the SELECT keyword), except for the WHERE part.
        where - the WHERE part (without the WHERE keyword) of the SQL query generating the collection, or null.
        factory - the factory that will be used to create documents.
        Throws:
        SQLException
        ClassNotFoundException
      • JdbcDocumentCollection

        public JdbcDocumentCollection​(String dbUri,
                                      String jdbcDriverName,
                                      String select,
                                      String idSpec,
                                      String where,
                                      DocumentFactory factory)
                               throws SQLException,
                                      ClassNotFoundException
        Creates a document collection based on the result set of an SQL query.

        Beware. This class is not guaranteed to work if the database is deleted or modified after creation!

        Parameters:
        dbUri - a JDBC URI pointing at the database.
        jdbcDriverName - the name of a JDBC driver, or null if you do not want to load a driver.
        select - the SQL query generating the collection (without the SELECT keyword), except for the WHERE part.
        idSpec - the complete SQL spec for the id (necessary for complex queries with multiple tables).
        where - the WHERE part (without the WHERE keyword) of the SQL query generating the collection, or null.
        factory - the factory that will be used to create documents.
        Throws:
        SQLException
        ClassNotFoundException