Class DbMapping


  • public final class DbMapping
    extends java.lang.Object
    A DbMapping describes how a certain type of Nodes is to mapped to a relational database table. Basically it consists of a set of JavaScript property-to- Database row bindings which are represented by instances of the Relation class.
    • Constructor Summary

      Constructors 
      Constructor Description
      DbMapping​(Application app, java.lang.String parentTypeName)
      Create an internal DbMapping used for "virtual" mappings aka collections, mountpoints etc.
      DbMapping​(Application app, java.lang.String typename, java.util.Properties props)
      Create a DbMapping from a type.properties property file
      DbMapping​(Application app, java.lang.String typename, java.util.Properties props, boolean virtual)
      Create a DbMapping from a type.properties property file
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addDependency​(DbMapping dbmap)
      Register a DbMapping that depends on this DbMapping, so that collections of other mapping should be reloaded if data on this mapping is updated.
      void addJoinConstraints​(java.lang.StringBuffer s, java.lang.String pre)
      Add constraints to select query string to join object references
      protected void appendCondition​(java.lang.StringBuffer q, java.lang.String column, java.lang.String val)
      Append a sql-condition for the given column which must have the value given to the given StringBuffer.
      protected void appendCondition​(java.lang.StringBuffer q, java.lang.String column, java.lang.String[] values)
      Append a sql-condition for the given column which must have one of the values contained inside the given Set to the given StringBuffer.
      static boolean areStorageCompatible​(DbMapping dbm1, DbMapping dbm2)
      Static utility method to check whether two DbMappings use the same storage.
      java.lang.String columnNameToProperty​(java.lang.String columnName)
      Translate a database column name to an object property name according to this mapping.
      Relation columnNameToRelation​(java.lang.String columnName)
      Translate a database column name to an object property name according to this mapping.
      Application getApplication()
      Get the application this DbMapping belongs to.
      java.lang.String getAppName()
      Get the name of this mapping's application
      DbColumn getColumn​(java.lang.String columnName)  
      DbColumn[] getColumns()
      Return an array of DbColumns for the relational table mapped by this DbMapping.
      java.sql.Connection getConnection()
      Get a JDBC connection for this DbMapping.
      DbSource getDbSource()
      Get the DbSource object for this DbMapping.
      java.lang.String getDbSourceName()
      Get the dbsource name used for this type mapping.
      Relation getExactPropertyRelation​(java.lang.String propname)  
      java.lang.String getExtends()
      Get the name of this type's parent type, if any.
      java.lang.String getExtensionId()
      get the id-value of this extension
      java.lang.String[] getExtensions()
      Returns the Set of Prototypes extending this prototype
      DbMapping getGroupbyMapping()
      If subnodes are grouped by one of their properties, return the db-mapping with the right relations to create the group-by nodes
      java.lang.String getIDField()
      Get the primary key column name for objects using this mapping.
      java.lang.String getIDgen()  
      java.lang.String getInsert()  
      Relation[] getJoins()
      Return the array of relations that are fetched with objects of this type.
      long getLastDataChange()
      Get the last time something changed in our data
      long getLastTypeChange()
      Get the last time something changed in the Mapping
      java.lang.String getNameField()
      Get the column used for (internal) names of objects of this type.
      protected long getNewID​(long dbmax)
      Helper method to generate a new ID.
      ParentInfo[] getParentInfo()  
      DbMapping getParentMapping()
      Get the mapping we inherit from, or null
      java.lang.String getParentSetting()  
      java.util.Properties getProperties()
      Get our ResourceProperties
      java.util.Enumeration getPropertyEnumeration()
      Return an enumeration of all properties defined by this db mapping.
      DbMapping getPropertyMapping​(java.lang.String propname)  
      java.lang.String[] getPropertyNames()
      Return the list of defined property names as String array.
      Relation getPropertyRelation​(java.lang.String propname)  
      java.lang.String getPrototypeField()
      Get the column used for names of prototype.
      java.lang.String getPrototypeName​(java.lang.String id)
      Looks up the prototype name identified by the given id, returing our own type name if it can't be resolved
      java.lang.StringBuffer getSelect​(Relation rel)
      Get a StringBuffer initialized to the first part of the select statement for objects defined by this DbMapping
      java.lang.String getStorageTypeName()
      Return the name of the prototype which specifies the storage location (dbsource + tablename) for this type, or null if it is stored in the embedded db.
      java.lang.String getSubnodeGroupby()  
      DbMapping getSubnodeMapping()  
      Relation getSubnodeRelation()  
      java.util.Properties getSubProperties​(java.lang.String prefix)  
      java.lang.String getTableName()
      Get the table name used for this type mapping.
      java.lang.String getTypeName()
      Get the name of the object type this DbMapping belongs to.
      java.lang.StringBuffer getUpdate()  
      WrappedNodeManager getWrappedNodeManager()  
      protected boolean inheritsStorage()
      Check whether this DbMapping inherits its storage location from its parent mapping.
      boolean isGroup()
      Find if this DbMapping describes a group node.
      boolean isH2()
      Is the database behind this a H2 db?
      boolean isInstanceOf​(java.lang.String other)
      Return true if this db mapping represents the prototype indicated by the string argument, either itself or via one of its parent prototypes.
      boolean isMySQL()
      Is the database behind this a MySQL db?
      boolean isOracle()
      Is the database behind this an Oracle db?
      boolean isPostgreSQL()
      Is the database behind this a PostgreSQL db?
      boolean isRelational()
      Tell whether this data mapping maps to a relational database table.
      boolean isStorageCompatible​(DbMapping other)
      Tell if this DbMapping uses the same storage as the given DbMapping.
      boolean isVirtual()
      Find if this DbMapping describes a virtual node (collection, mountpoint, groupnode)
      boolean needsPersistence()
      Find whether a node with this DbMapping must be stored in the database.
      boolean needsQuotes​(java.lang.String columnName)
      Return true if values for the column identified by the parameter need to be quoted in SQL queries.
      boolean needsUpdate()
      Tell the type manager whether we need update() to be called
      java.lang.String propertyToColumnName​(java.lang.String propName)
      Translate an object property name to a database column name according to this mapping.
      Relation propertyToRelation​(java.lang.String propName)
      Translate an object property name to a database column name according to this mapping.
      void remove()
      Method in interface Updatable.
      protected void setIndirectDataChange()
      Set the last time something changed in the data.
      void setLastDataChange()
      Set the last time something changed in the data, propagating the event to mappings that depend on us through an additionalTables switch.
      void setPropertyRelation​(Relation rel)  
      java.lang.String toString()
      Return a string representation for this DbMapping
      void update()
      Read the mapping from the Properties.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • DbMapping

        public DbMapping​(Application app,
                         java.lang.String parentTypeName)
        Create an internal DbMapping used for "virtual" mappings aka collections, mountpoints etc.
      • DbMapping

        public DbMapping​(Application app,
                         java.lang.String typename,
                         java.util.Properties props,
                         boolean virtual)
        Create a DbMapping from a type.properties property file
      • DbMapping

        public DbMapping​(Application app,
                         java.lang.String typename,
                         java.util.Properties props)
        Create a DbMapping from a type.properties property file
    • Method Detail

      • needsUpdate

        public boolean needsUpdate()
        Tell the type manager whether we need update() to be called
      • update

        public void update()
        Read the mapping from the Properties. Return true if the properties were changed. The read is split in two, this method and the rewire method. The reason is that in order for rewire to work, all other db mappings must have been initialized and registered.
      • getExtensions

        public java.lang.String[] getExtensions()
        Returns the Set of Prototypes extending this prototype
        Returns:
        the Set of Prototypes extending this prototype
      • getPrototypeName

        public java.lang.String getPrototypeName​(java.lang.String id)
        Looks up the prototype name identified by the given id, returing our own type name if it can't be resolved
        Parameters:
        id - the id specified for the prototype
        Returns:
        the name of the extending prototype
      • getExtensionId

        public java.lang.String getExtensionId()
        get the id-value of this extension
      • remove

        public void remove()
        Method in interface Updatable.
      • getConnection

        public java.sql.Connection getConnection()
                                          throws java.lang.ClassNotFoundException,
                                                 java.sql.SQLException
        Get a JDBC connection for this DbMapping.
        Throws:
        java.lang.ClassNotFoundException
        java.sql.SQLException
      • getDbSource

        public DbSource getDbSource()
        Get the DbSource object for this DbMapping. The DbSource describes a JDBC data source including URL, JDBC driver, username and password.
      • getDbSourceName

        public java.lang.String getDbSourceName()
        Get the dbsource name used for this type mapping.
      • getTableName

        public java.lang.String getTableName()
        Get the table name used for this type mapping.
      • getApplication

        public Application getApplication()
        Get the application this DbMapping belongs to.
      • getAppName

        public java.lang.String getAppName()
        Get the name of this mapping's application
      • getTypeName

        public java.lang.String getTypeName()
        Get the name of the object type this DbMapping belongs to.
      • getExtends

        public java.lang.String getExtends()
        Get the name of this type's parent type, if any.
      • getIDField

        public java.lang.String getIDField()
        Get the primary key column name for objects using this mapping.
      • getNameField

        public java.lang.String getNameField()
        Get the column used for (internal) names of objects of this type.
      • getPrototypeField

        public java.lang.String getPrototypeField()
        Get the column used for names of prototype.
      • columnNameToProperty

        public java.lang.String columnNameToProperty​(java.lang.String columnName)
        Translate a database column name to an object property name according to this mapping.
      • propertyToColumnName

        public java.lang.String propertyToColumnName​(java.lang.String propName)
        Translate an object property name to a database column name according to this mapping. If no mapping is found, the property name is returned, assuming property and column names are equal.
      • columnNameToRelation

        public Relation columnNameToRelation​(java.lang.String columnName)
        Translate a database column name to an object property name according to this mapping.
      • propertyToRelation

        public Relation propertyToRelation​(java.lang.String propName)
        Translate an object property name to a database column name according to this mapping.
      • getParentSetting

        public java.lang.String getParentSetting()
        Returns:
        the parent info as unparsed string.
      • getParentInfo

        public ParentInfo[] getParentInfo()
        Returns:
        the parent info array, which tells an object of this type how to determine its parent object.
      • getSubnodeMapping

        public DbMapping getSubnodeMapping()
        Returns:
        ...
      • getPropertyMapping

        public DbMapping getPropertyMapping​(java.lang.String propname)
        Parameters:
        propname - ...
        Returns:
        ...
      • getGroupbyMapping

        public DbMapping getGroupbyMapping()
        If subnodes are grouped by one of their properties, return the db-mapping with the right relations to create the group-by nodes
      • setPropertyRelation

        public void setPropertyRelation​(Relation rel)
        Parameters:
        rel - ...
      • getSubnodeRelation

        public Relation getSubnodeRelation()
        Returns:
        ...
      • getPropertyNames

        public java.lang.String[] getPropertyNames()
        Return the list of defined property names as String array.
      • getPropertyRelation

        public Relation getPropertyRelation​(java.lang.String propname)
        Parameters:
        propname - ...
        Returns:
        ...
      • getExactPropertyRelation

        public Relation getExactPropertyRelation​(java.lang.String propname)
        Parameters:
        propname - ...
        Returns:
        ...
      • getSubnodeGroupby

        public java.lang.String getSubnodeGroupby()
        Returns:
        ...
      • getIDgen

        public java.lang.String getIDgen()
        Returns:
        ...
      • isRelational

        public boolean isRelational()
        Tell whether this data mapping maps to a relational database table. This returns true if a datasource is specified, even if it is not a valid one. Otherwise, objects with invalid mappings would be stored in the embedded db instead of an error being thrown, which is not what we want.
      • getColumns

        public DbColumn[] getColumns()
                              throws java.lang.ClassNotFoundException,
                                     java.sql.SQLException
        Return an array of DbColumns for the relational table mapped by this DbMapping.
        Throws:
        java.lang.ClassNotFoundException
        java.sql.SQLException
      • getJoins

        public Relation[] getJoins()
        Return the array of relations that are fetched with objects of this type.
      • getColumn

        public DbColumn getColumn​(java.lang.String columnName)
                           throws java.lang.ClassNotFoundException,
                                  java.sql.SQLException
        Parameters:
        columnName - ...
        Returns:
        ...
        Throws:
        java.lang.ClassNotFoundException - ...
        java.sql.SQLException - ...
      • getSelect

        public java.lang.StringBuffer getSelect​(Relation rel)
        Get a StringBuffer initialized to the first part of the select statement for objects defined by this DbMapping
        Parameters:
        rel - the Relation we use to select. Currently only used for optimizer hints. Is null if selecting by primary key.
        Returns:
        the StringBuffer containing the first part of the select query
      • getInsert

        public java.lang.String getInsert()
                                   throws java.lang.ClassNotFoundException,
                                          java.sql.SQLException
        Returns:
        ...
        Throws:
        java.lang.ClassNotFoundException
        java.sql.SQLException
      • getUpdate

        public java.lang.StringBuffer getUpdate()
        Returns:
        ...
      • needsQuotes

        public boolean needsQuotes​(java.lang.String columnName)
                            throws java.sql.SQLException,
                                   java.lang.ClassNotFoundException
        Return true if values for the column identified by the parameter need to be quoted in SQL queries.
        Throws:
        java.sql.SQLException
        java.lang.ClassNotFoundException
      • addJoinConstraints

        public void addJoinConstraints​(java.lang.StringBuffer s,
                                       java.lang.String pre)
        Add constraints to select query string to join object references
      • isOracle

        public boolean isOracle()
        Is the database behind this an Oracle db?
        Returns:
        true if the dbsource is using an oracle JDBC driver
      • isMySQL

        public boolean isMySQL()
        Is the database behind this a MySQL db?
        Returns:
        true if the dbsource is using a MySQL JDBC driver
      • isPostgreSQL

        public boolean isPostgreSQL()
        Is the database behind this a PostgreSQL db?
        Returns:
        true if the dbsource is using a PostgreSQL JDBC driver
      • isH2

        public boolean isH2()
        Is the database behind this a H2 db?
        Returns:
        true if the dbsource is using a H2 JDBC driver
      • toString

        public java.lang.String toString()
        Return a string representation for this DbMapping
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation
      • getLastTypeChange

        public long getLastTypeChange()
        Get the last time something changed in the Mapping
        Returns:
        time of last mapping change
      • getLastDataChange

        public long getLastDataChange()
        Get the last time something changed in our data
        Returns:
        time of last data change
      • setLastDataChange

        public void setLastDataChange()
        Set the last time something changed in the data, propagating the event to mappings that depend on us through an additionalTables switch.
      • setIndirectDataChange

        protected void setIndirectDataChange()
        Set the last time something changed in the data. This is already an indirect data change triggered by a mapping we depend on, so we don't propagate it to mappings that depend on us through an additionalTables switch.
      • getNewID

        protected long getNewID​(long dbmax)
        Helper method to generate a new ID. This is only used in the special case when using the select(max) method and the underlying table is still empty.
        Parameters:
        dbmax - the maximum value already stored in db
        Returns:
        a new and hopefully unique id
      • getPropertyEnumeration

        public java.util.Enumeration getPropertyEnumeration()
        Return an enumeration of all properties defined by this db mapping.
        Returns:
        the property enumeration
      • getStorageTypeName

        public java.lang.String getStorageTypeName()
        Return the name of the prototype which specifies the storage location (dbsource + tablename) for this type, or null if it is stored in the embedded db.
      • inheritsStorage

        protected boolean inheritsStorage()
        Check whether this DbMapping inherits its storage location from its parent mapping. The raison d'etre for this is that we need to detect inherited storage even if the dbsource and table are explicitly set in the extended mapping.
        Returns:
        true if this mapping shares its parent mapping storage
      • areStorageCompatible

        public static boolean areStorageCompatible​(DbMapping dbm1,
                                                   DbMapping dbm2)
        Static utility method to check whether two DbMappings use the same storage.
        Returns:
        true if both use the embedded database or the same relational table.
      • isStorageCompatible

        public boolean isStorageCompatible​(DbMapping other)
        Tell if this DbMapping uses the same storage as the given DbMapping.
        Returns:
        true if both use the embedded database or the same relational table.
      • isInstanceOf

        public boolean isInstanceOf​(java.lang.String other)
        Return true if this db mapping represents the prototype indicated by the string argument, either itself or via one of its parent prototypes.
      • getParentMapping

        public DbMapping getParentMapping()
        Get the mapping we inherit from, or null
        Returns:
        the parent DbMapping, or null
      • getProperties

        public java.util.Properties getProperties()
        Get our ResourceProperties
        Returns:
        our properties
      • getSubProperties

        public java.util.Properties getSubProperties​(java.lang.String prefix)
      • addDependency

        protected void addDependency​(DbMapping dbmap)
        Register a DbMapping that depends on this DbMapping, so that collections of other mapping should be reloaded if data on this mapping is updated.
        Parameters:
        dbmap - the DbMapping that depends on us
      • appendCondition

        protected void appendCondition​(java.lang.StringBuffer q,
                                       java.lang.String column,
                                       java.lang.String[] values)
                                throws java.sql.SQLException,
                                       java.lang.ClassNotFoundException
        Append a sql-condition for the given column which must have one of the values contained inside the given Set to the given StringBuffer.
        Parameters:
        q - the StringBuffer to append to
        column - the column which must match one of the values
        values - the list of values
        Throws:
        java.sql.SQLException
        java.lang.ClassNotFoundException
      • appendCondition

        protected void appendCondition​(java.lang.StringBuffer q,
                                       java.lang.String column,
                                       java.lang.String val)
                                throws java.sql.SQLException,
                                       java.lang.ClassNotFoundException
        Append a sql-condition for the given column which must have the value given to the given StringBuffer.
        Parameters:
        q - the StringBuffer to append to
        column - the column which must match one of the values
        val - the value
        Throws:
        java.sql.SQLException
        java.lang.ClassNotFoundException
      • isVirtual

        public boolean isVirtual()
        Find if this DbMapping describes a virtual node (collection, mountpoint, groupnode)
        Returns:
        true if this instance describes a virtual node.
      • isGroup

        public boolean isGroup()
        Find if this DbMapping describes a group node.
        Returns:
        true if this instance describes a group node.
      • needsPersistence

        public boolean needsPersistence()
        Find whether a node with this DbMapping must be stored in the database. This is true if this mapping defines a non-virtual node, or a virtual node with non-relational child objects.
        Returns:
        true if this node needs to be stored in the db, false otherwise