Class BaseTable

    • Constructor Summary

      Constructors 
      Constructor Description
      BaseTable​(IDBInterface dbInterface, java.lang.String tableName)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addTableIndex​(boolean unique, java.util.List<java.lang.String> columnList)
      Add an index to a table.
      protected void analyzeTable()
      Analyze this table.
      protected void beginTransaction()
      Begin a database transaction.
      java.lang.String buildConjunctionClause​(java.util.List outputParameters, ClauseDescription[] clauseDescriptions)  
      java.lang.String constructCountClause​(java.lang.String column)
      Construct a count clause.
      java.lang.String constructDistinctOnClause​(java.util.List outputParameters, java.lang.String baseQuery, java.util.List baseParameters, java.lang.String[] distinctFields, java.lang.String[] orderFields, boolean[] orderFieldsAscending, java.util.Map<java.lang.String,​java.lang.String> otherFields)
      Construct a 'distinct on (x)' filter.
      java.lang.String constructDoubleCastClause​(java.lang.String value)
      Construct a cast to a double value.
      java.lang.String constructOffsetLimitClause​(int offset, int limit)
      Construct an offset/limit clause.
      java.lang.String constructRegexpClause​(java.lang.String column, java.lang.String regularExpression, boolean caseInsensitive)
      Construct a regular-expression match clause.
      java.lang.String constructSubstringClause​(java.lang.String column, java.lang.String regularExpression, boolean caseInsensitive)
      Construct a regular-expression substring clause.
      protected void endTransaction()
      End a database transaction, either performing a commit or a rollback (depending on whether signalRollback() was called within the transaction).
      int findConjunctionClauseMax​(ClauseDescription[] otherClauseDescriptions)  
      java.lang.String getDatabaseCacheKey()  
      protected IDBInterface getDBInterface()  
      protected int getMaxInClause()
      Obtain the maximum number of individual items that should be present in an IN clause.
      protected int getMaxOrClause()
      Obtain the maximum number of individual clauses that should be present in a sequence of OR clauses.
      protected long getSleepAmt()
      Get a random amount to sleep for (to resolve a deadlock)
      protected java.util.Map getTableIndexes​(StringSet invalidateKeys, java.lang.String queryClass)
      Get a table's indexes.
      java.lang.String getTableName()  
      protected java.util.Map getTableSchema​(StringSet invalidateKeys, java.lang.String queryClass)
      Get the current table schema.
      java.lang.String getTransactionID()  
      int getWindowedReportMaxRows()
      Calculate history cutoff for windowed report queries.
      java.lang.String makeTableKey()
      Construct a key that is database specific, and applies to queries made against a specific table name.
      void noteModifications​(int insertCount, int modifyCount, int deleteCount)
      Note a number of inserts, modifications, or deletions to a specific table.
      protected void performAddIndex​(java.lang.String indexName, IndexDescription description)
      Add an index to a table.
      void performAlter​(java.util.Map columnMap, java.util.Map columnModifyMap, java.util.List<java.lang.String> columnDeleteList, StringSet invalidateKeys)
      Perform a table alter operation.
      void performCommit()
      Perform the transaction commit.
      protected void performCreate​(java.util.Map columnMap, StringSet invalidateKeys)
      Perform a table creation operation.
      protected void performDelete​(java.lang.String whereClause, java.util.List whereParameters, StringSet invalidateKeys)
      Perform a delete operation.
      protected void performDrop​(StringSet invalidateKeys)
      Perform a table drop operation.
      protected void performInsert​(java.util.Map parameterMap, StringSet invalidateKeys)
      Perform an insert operation.
      protected void performModification​(java.lang.String query, java.util.List params, StringSet invalidateKeys)
      Perform a general database modification query.
      protected IResultSet performQuery​(java.lang.String query, java.util.List params, StringSet cacheKeys, java.lang.String queryClass)
      Perform a general "data fetch" query.
      protected IResultSet performQuery​(java.lang.String query, java.util.List params, StringSet cacheKeys, java.lang.String queryClass, int resultLimit)
      Perform a general "data fetch" query, with the ability to provide a limit.
      void performRemoveIndex​(java.lang.String indexName)
      Remove an index.
      protected void performUpdate​(java.util.Map parameterMap, java.lang.String whereClause, java.util.List whereParameters, StringSet invalidateKeys)
      Perform an update operation.
      static java.util.Map prepareRowForSave​(BaseObject object, StringSet fieldSet)
      Read the specified fields from the specified object, and build a Map, which can be used to write the data to the database.
      static void readRow​(BaseObject object, IResultRow resultRow)
      Set up a base object from a database row.
      protected void reindexTable()
      Reindex this table.
      protected void signalRollback()
      Signal that a rollback should occur on the next endTransaction().
      protected void sleepFor​(long amt)
      Sleep for a specified amount, to resolve a deadlock
      • Methods inherited from class java.lang.Object

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

      • tableName

        protected java.lang.String tableName
    • Constructor Detail

      • BaseTable

        public BaseTable​(IDBInterface dbInterface,
                         java.lang.String tableName)
    • Method Detail

      • getTableName

        public java.lang.String getTableName()
      • getDatabaseCacheKey

        public java.lang.String getDatabaseCacheKey()
      • getTransactionID

        public java.lang.String getTransactionID()
      • performInsert

        protected void performInsert​(java.util.Map parameterMap,
                                     StringSet invalidateKeys)
                              throws ManifoldCFException
        Perform an insert operation.
        Parameters:
        invalidateKeys - are the cache keys that should be invalidated.
        parameterMap - is the map of column name/values to write.
        Throws:
        ManifoldCFException
      • performUpdate

        protected void performUpdate​(java.util.Map parameterMap,
                                     java.lang.String whereClause,
                                     java.util.List whereParameters,
                                     StringSet invalidateKeys)
                              throws ManifoldCFException
        Perform an update operation.
        Parameters:
        invalidateKeys - are the cache keys that should be invalidated.
        parameterMap - is the map of column name/values to write.
        whereClause - is the where clause describing the match (including the WHERE), or null if none.
        whereParameters - are the parameters that come with the where clause, if any.
        Throws:
        ManifoldCFException
      • performDelete

        protected void performDelete​(java.lang.String whereClause,
                                     java.util.List whereParameters,
                                     StringSet invalidateKeys)
                              throws ManifoldCFException
        Perform a delete operation.
        Parameters:
        invalidateKeys - are the cache keys that should be invalidated.
        whereClause - is the where clause describing the match (including the WHERE), or null if none.
        whereParameters - are the parameters that come with the where clause, if any.
        Throws:
        ManifoldCFException
      • performCreate

        protected void performCreate​(java.util.Map columnMap,
                                     StringSet invalidateKeys)
                              throws ManifoldCFException
        Perform a table creation operation.
        Parameters:
        columnMap - is the map describing the columns and types. NOTE that these are abstract types, which will be mapped to the proper types for the actual database inside this layer.
        invalidateKeys - are the cache keys that should be invalidated, if any.
        Throws:
        ManifoldCFException
      • performAlter

        public void performAlter​(java.util.Map columnMap,
                                 java.util.Map columnModifyMap,
                                 java.util.List<java.lang.String> columnDeleteList,
                                 StringSet invalidateKeys)
                          throws ManifoldCFException
        Perform a table alter operation.
        Parameters:
        columnMap - is the map describing the columns and types to add. These are in the same form as for performCreate.
        columnModifyMap - is the map describing the columns to modify. These are in the same form as for performCreate.
        columnDeleteList - is the list of column names to delete.
        invalidateKeys - are the cache keys that should be invalidated, if any.
        Throws:
        ManifoldCFException
      • addTableIndex

        protected void addTableIndex​(boolean unique,
                                     java.util.List<java.lang.String> columnList)
                              throws ManifoldCFException
        Add an index to a table.
        Parameters:
        unique - is a boolean that if true describes a unique index.
        columnList - is the list of columns that need to be included in the index, in order.
        Throws:
        ManifoldCFException
      • performAddIndex

        protected void performAddIndex​(java.lang.String indexName,
                                       IndexDescription description)
                                throws ManifoldCFException
        Add an index to a table.
        Parameters:
        indexName - is the optional name of the table index. If null, a name will be chosen automatically.
        description - is the index description.
        Throws:
        ManifoldCFException
      • performRemoveIndex

        public void performRemoveIndex​(java.lang.String indexName)
                                throws ManifoldCFException
        Remove an index.
        Parameters:
        indexName - is the name of the index to remove.
        Throws:
        ManifoldCFException
      • performDrop

        protected void performDrop​(StringSet invalidateKeys)
                            throws ManifoldCFException
        Perform a table drop operation.
        Parameters:
        invalidateKeys - are the cache keys that should be invalidated, if any.
        Throws:
        ManifoldCFException
      • getTableSchema

        protected java.util.Map getTableSchema​(StringSet invalidateKeys,
                                               java.lang.String queryClass)
                                        throws ManifoldCFException
        Get the current table schema.
        Parameters:
        invalidateKeys - are the cache keys, if needed (null if no cache desired).
        queryClass - is the LRU class name against which this query would be cached, or null if no LRU behavior desired.
        Returns:
        a map of column names & ColumnDescription's, or null.
        Throws:
        ManifoldCFException
      • getTableIndexes

        protected java.util.Map getTableIndexes​(StringSet invalidateKeys,
                                                java.lang.String queryClass)
                                         throws ManifoldCFException
        Get a table's indexes.
        Parameters:
        invalidateKeys - are the keys against which to cache the query, or null.
        queryClass - is the name of the query class, or null.
        Returns:
        a map of index names and IndexDescription objects, describing the indexes.
        Throws:
        ManifoldCFException
      • performModification

        protected void performModification​(java.lang.String query,
                                           java.util.List params,
                                           StringSet invalidateKeys)
                                    throws ManifoldCFException
        Perform a general database modification query.
        Parameters:
        query - is the query string.
        params - are the parameterized values, if needed.
        invalidateKeys - are the cache keys to invalidate.
        Throws:
        ManifoldCFException
      • performQuery

        protected IResultSet performQuery​(java.lang.String query,
                                          java.util.List params,
                                          StringSet cacheKeys,
                                          java.lang.String queryClass)
                                   throws ManifoldCFException
        Perform a general "data fetch" query.
        Parameters:
        query - is the query string.
        params - are the parameterized values, if needed.
        cacheKeys - are the cache keys, if needed (null if no cache desired).
        queryClass - is the LRU class name against which this query would be cached, or null if no LRU behavior desired.
        Returns:
        a resultset.
        Throws:
        ManifoldCFException
      • performQuery

        protected IResultSet performQuery​(java.lang.String query,
                                          java.util.List params,
                                          StringSet cacheKeys,
                                          java.lang.String queryClass,
                                          int resultLimit)
                                   throws ManifoldCFException
        Perform a general "data fetch" query, with the ability to provide a limit.
        Parameters:
        query - is the query string.
        params - are the parameterized values, if needed.
        cacheKeys - are the cache keys, if needed (null if no cache desired).
        queryClass - is the LRU class name against which this query would be cached, or null if no LRU behavior desired.
        resultLimit - is the maximum number of results desired.
        Returns:
        a resultset.
        Throws:
        ManifoldCFException
      • beginTransaction

        protected void beginTransaction()
                                 throws ManifoldCFException
        Begin a database transaction. This method call MUST be paired with an endTransaction() call, or database handles will be lost. If the transaction should be rolled back, then signalRollback() should be called before the transaction is ended. It is strongly recommended that the code that uses transactions be structured so that a try block starts immediately after this method call. The body of the try block will contain all direct or indirect calls to executeQuery(). After this should be a catch for every exception type, including Error, which should call the signalRollback() method, and rethrow the exception. Then, after that a finally{} block which calls endTransaction().
        Throws:
        ManifoldCFException
      • performCommit

        public void performCommit()
                           throws ManifoldCFException
        Perform the transaction commit. Calling this method does not relieve the coder of the responsibility of calling endTransaction(), as listed below. The purpose of a separate commit operation is to allow handling of situations where the commit generates a TRANSACTION_ABORT signal.
        Throws:
        ManifoldCFException
      • signalRollback

        protected void signalRollback()
        Signal that a rollback should occur on the next endTransaction().
      • endTransaction

        protected void endTransaction()
                               throws ManifoldCFException
        End a database transaction, either performing a commit or a rollback (depending on whether signalRollback() was called within the transaction).
        Throws:
        ManifoldCFException
      • getSleepAmt

        protected long getSleepAmt()
        Get a random amount to sleep for (to resolve a deadlock)
      • noteModifications

        public void noteModifications​(int insertCount,
                                      int modifyCount,
                                      int deleteCount)
                               throws ManifoldCFException
        Note a number of inserts, modifications, or deletions to a specific table. This is so we can decide when to do appropriate maintenance.
        Parameters:
        insertCount - is the number of inserts.
        modifyCount - is the number of updates.
        deleteCount - is the number of deletions.
        Throws:
        ManifoldCFException
      • makeTableKey

        public java.lang.String makeTableKey()
        Construct a key that is database specific, and applies to queries made against a specific table name.
      • constructDoubleCastClause

        public java.lang.String constructDoubleCastClause​(java.lang.String value)
        Construct a cast to a double value. On most databases this cast needs to be explicit, but on some it is implicit (and cannot be in fact specified).
        Parameters:
        value - is the value to be cast.
        Returns:
        the query chunk needed.
      • constructCountClause

        public java.lang.String constructCountClause​(java.lang.String column)
        Construct a count clause. On most databases this will be COUNT(col), but on some the count needs to be cast to a BIGINT, so CAST(COUNT(col) AS BIGINT) will be emitted instead.
        Parameters:
        column - is the column string to be counted.
        Returns:
        the query chunk needed.
      • constructRegexpClause

        public java.lang.String constructRegexpClause​(java.lang.String column,
                                                      java.lang.String regularExpression,
                                                      boolean caseInsensitive)
        Construct a regular-expression match clause. This method builds both the text part of a regular-expression match.
        Parameters:
        column - is the column specifier string.
        regularExpression - is the properly-quoted regular expression string, or "?" if a parameterized value is to be used.
        caseInsensitive - is true of the regular expression match is to be case insensitive.
        Returns:
        the query chunk needed, not padded with spaces on either side.
      • constructSubstringClause

        public java.lang.String constructSubstringClause​(java.lang.String column,
                                                         java.lang.String regularExpression,
                                                         boolean caseInsensitive)
        Construct a regular-expression substring clause. This method builds an expression that extracts a specified string section from a field, based on a regular expression.
        Parameters:
        column - is the column specifier string.
        regularExpression - is the properly-quoted regular expression string, or "?" if a parameterized value is to be used.
        caseInsensitive - is true if the regular expression match is to be case insensitive.
        Returns:
        the expression chunk needed, not padded with spaces on either side.
      • constructOffsetLimitClause

        public java.lang.String constructOffsetLimitClause​(int offset,
                                                           int limit)
        Construct an offset/limit clause. This method constructs an offset/limit clause in the proper manner for the database in question.
        Parameters:
        offset - is the starting offset number.
        limit - is the limit of result rows to return.
        Returns:
        the proper clause, with no padding spaces on either side.
      • constructDistinctOnClause

        public java.lang.String constructDistinctOnClause​(java.util.List outputParameters,
                                                          java.lang.String baseQuery,
                                                          java.util.List baseParameters,
                                                          java.lang.String[] distinctFields,
                                                          java.lang.String[] orderFields,
                                                          boolean[] orderFieldsAscending,
                                                          java.util.Map<java.lang.String,​java.lang.String> otherFields)
        Construct a 'distinct on (x)' filter. This filter wraps a query and returns a new query whose results are similar to POSTGRESQL's DISTINCT-ON feature. Specifically, for each combination of the specified distinct fields in the result, only the first such row is included in the final result.
        Parameters:
        outputParameters - is a blank list into which to put parameters. Null may be used if the baseParameters parameter is null.
        baseQuery - is the base query, which is another SELECT statement, without parens, e.g. "SELECT ..."
        baseParameters - are the parameters corresponding to the baseQuery.
        distinctFields - are the fields to consider to be distinct. These should all be keys in otherFields below.
        orderFields - are the otherfield keys that determine the ordering.
        orderFieldsAscending - are true for orderFields that are ordered as ASC, false for DESC.
        otherFields - are the rest of the fields to return, keyed by the AS name, value being the base query column value, e.g. "value AS key"
        Returns:
        a revised query that performs the necessary DISTINCT ON operation. The list outputParameters will also be appropriately filled in.
      • findConjunctionClauseMax

        public int findConjunctionClauseMax​(ClauseDescription[] otherClauseDescriptions)
      • buildConjunctionClause

        public java.lang.String buildConjunctionClause​(java.util.List outputParameters,
                                                       ClauseDescription[] clauseDescriptions)
      • getMaxInClause

        protected int getMaxInClause()
        Obtain the maximum number of individual items that should be present in an IN clause. Exceeding this amount will potentially cause the query performance to drop.
        Returns:
        the maximum number of IN clause members.
      • getMaxOrClause

        protected int getMaxOrClause()
        Obtain the maximum number of individual clauses that should be present in a sequence of OR clauses. Exceeding this amount will potentially cause the query performance to drop.
        Returns:
        the maximum number of OR clause members.
      • getWindowedReportMaxRows

        public int getWindowedReportMaxRows()
        Calculate history cutoff for windowed report queries.
        Returns:
        the cutoff history row count.
      • readRow

        public static void readRow​(BaseObject object,
                                   IResultRow resultRow)
        Set up a base object from a database row.
        Parameters:
        object - is the object to read into.
        resultRow - is the row to use to initialize the object.
      • prepareRowForSave

        public static java.util.Map prepareRowForSave​(BaseObject object,
                                                      StringSet fieldSet)
        Read the specified fields from the specified object, and build a Map, which can be used to write the data to the database.
        Parameters:
        fieldSet - is the set of fields.
        object - is the BaseObject to get the data from.
        Returns:
        the map.