Package sc.db

Class DBTypeDescriptor

  • public class DBTypeDescriptor
    extends BaseTypeDescriptor
    Stores the metadata for a given type in the system that represents the mapping to a persistence storage layer. A given type can be persisted from one or more tables, in the same or different databases. There's a primary table, that will have one row per object instance. Auxiliary tables store alternate properties and can be optionally created. Multi-tables store multi-valued properties stored with 'multiRow' mode. A selectGroup allows collections of properties to be loaded at the same time. By default, a select group is created for each table.
    • Field Detail

      • DBTypeIdPropertyName

        public static final java.lang.String DBTypeIdPropertyName
        See Also:
        Constant Field Values
      • DBTypeIdColumnName

        public static final java.lang.String DBTypeIdColumnName
        See Also:
        Constant Field Values
      • LastModifiedPropertyName

        public static final java.lang.String LastModifiedPropertyName
        See Also:
        Constant Field Values
      • DBDynPropsColumnName

        public static final java.lang.String DBDynPropsColumnName
        See Also:
        Constant Field Values
      • DBDynPropsColumnType

        public static final java.lang.String DBDynPropsColumnType
        See Also:
        Constant Field Values
      • DBUnsetTypeId

        public static final int DBUnsetTypeId
        Two reserved type ids - either not specified or it's an abstract type so it's not created
        See Also:
        Constant Field Values
      • dbReadOnly

        public boolean dbReadOnly
        Turn off writes to the database - instead, these will be stored in memory and merged into query results for testing purposes
      • dbDisabled

        public boolean dbDisabled
        Turn off database entirely - store objects and run queries only in memory for testing
      • needsAutoId

        public boolean needsAutoId
        Set to true for types where the source has no 'id' property
      • defaultFetchGroup

        public java.lang.String defaultFetchGroup
      • typeInstances

        public java.util.concurrent.ConcurrentHashMap<java.lang.Object,​IDBObject> typeInstances
        The cache of instances for this type but only if baseType = null - otherwise, we use the baseType's typeInstances cache
      • schemaSQL

        public java.lang.String schemaSQL
        Commands in the database's DDL language (usually SQL) to be added after the generated create tables
      • runtimeMode

        public boolean runtimeMode
        False during the code-generation phase, true when connected to a database
      • expireTimeMillis

        public long expireTimeMillis
      • subTypesById

        public java.util.Map<java.lang.Integer,​DBTypeDescriptor> subTypesById
      • typeId

        public int typeId
      • typeIdList

        public int[] typeIdList
      • syncPropsInited

        public boolean syncPropsInited
      • liveDynTypes

        public boolean liveDynTypes
      • selectGroups

        public java.util.LinkedHashMap<java.lang.String,​SelectGroupQuery> selectGroups
    • Constructor Detail

      • DBTypeDescriptor

        public DBTypeDescriptor​(java.lang.Object typeDecl,
                                DBTypeDescriptor baseType,
                                int typeId,
                                java.lang.String dataSourceName,
                                TableDescriptor primary,
                                java.util.List<BaseQueryDescriptor> queries,
                                java.lang.String schemaSQL)
        Defines the type with the id properties of the primary table in tact so it can be used to create references from other types. Properties may be later added to the primary table and other tables added with initTables. This version is used from the code processor as it must create the descriptors in phases.
      • DBTypeDescriptor

        public DBTypeDescriptor​(java.lang.Object typeDecl,
                                DBTypeDescriptor baseType,
                                int typeId,
                                java.lang.String dataSourceName,
                                TableDescriptor primary,
                                java.util.List<TableDescriptor> auxTables,
                                java.util.List<TableDescriptor> multiTables,
                                java.util.List<BaseQueryDescriptor> queries,
                                java.lang.String versionPropName,
                                java.lang.String schemaSQL,
                                boolean needsAutoId)
        The constructor called from generated runtime code - but the generated code uses the static 'create' method that calls this
    • Method Detail

      • clearAllCaches

        public static void clearAllCaches()
      • getByType

        public static DBTypeDescriptor getByType​(java.lang.Object type,
                                                 boolean start)
      • getBaseByType

        public static BaseTypeDescriptor getBaseByType​(java.lang.Object type,
                                                       boolean start)
      • getByName

        public static DBTypeDescriptor getByName​(java.lang.String typeName,
                                                 boolean start)
      • getByTableName

        public static DBTypeDescriptor getByTableName​(java.lang.String tableName)
      • findSubType

        public DBTypeDescriptor findSubType​(java.lang.String subTypeName)
      • initTables

        public void initTables​(java.util.List<TableDescriptor> auxTables,
                               java.util.List<TableDescriptor> multiTables,
                               java.lang.String versionPropName,
                               boolean runtimeMode)
      • getTableByName

        public TableDescriptor getTableByName​(java.lang.String tableName)
      • getSelectGroupQuery

        public SelectGroupQuery getSelectGroupQuery​(java.lang.String selectGroup)
      • getFetchQueryForProperty

        public SelectGroupQuery getFetchQueryForProperty​(java.lang.String propName)
      • getFetchQueryForNum

        public sc.db.DBQuery getFetchQueryForNum​(int queryNum)
      • getNumFetchPropQueries

        public int getNumFetchPropQueries()
      • getPropertyDescriptor

        public DBPropertyDescriptor getPropertyDescriptor​(java.lang.String propNamePath)
      • getPropertyForColumn

        public DBPropertyDescriptor getPropertyForColumn​(java.lang.String colName)
      • addFetchQuery

        public void addFetchQuery​(sc.db.DBQuery query)
      • stringToId

        public java.lang.Object stringToId​(java.lang.String idStr)
      • findById

        public IDBObject findById​(java.lang.Object... idArgs)
      • findByIds

        public java.util.List<? extends IDBObject> findByIds​(java.lang.Object... ids)
      • findAll

        public java.util.List<? extends IDBObject> findAll​(java.util.List<java.lang.String> orderByNames,
                                                           int startIx,
                                                           int maxResults)
      • findBy

        public java.util.List<? extends IDBObject> findBy​(java.util.List<java.lang.String> propNames,
                                                          java.util.List<java.lang.Object> propValues,
                                                          java.lang.String selectGroup,
                                                          java.util.List<java.lang.String> orderByNames,
                                                          int startIx,
                                                          int maxResults)
      • convertToSQLSearchString

        public static java.lang.String convertToSQLSearchString​(java.lang.String text)
      • searchQuery

        public java.util.List<? extends IDBObject> searchQuery​(java.lang.String text,
                                                               java.util.List<java.lang.String> propNames,
                                                               java.util.List<java.lang.Object> propValues,
                                                               java.lang.String selectGroup,
                                                               java.util.List<java.lang.String> orderByProps,
                                                               int startIx,
                                                               int maxResults)
      • searchCountQuery

        public int searchCountQuery​(java.lang.String text,
                                    java.util.List<java.lang.String> propNames,
                                    java.util.List<java.lang.Object> propValues)
      • countAll

        public int countAll()
      • query

        public java.util.List<? extends IDBObject> query​(Query query,
                                                         java.lang.String selectGroup,
                                                         java.util.List<java.lang.String> orderByProps,
                                                         int startIx,
                                                         int maxResults)
      • count

        public int count​(Query query)
      • lookupInstById

        public IDBObject lookupInstById​(java.lang.Object id,
                                        int typeId,
                                        boolean createProto,
                                        boolean selectDefault)
        Lower level method used to lookup an instance of this type with a given id - based on the "highlander principle" - we try to ensure there's only one instance of an object with the same id to prevent aliasing, simplify code and caching and provide a 'transactional view' of objects with persistent state. If createProto is true, and the instance is not in the cache, a prototype is created with that and returned. If selectDefault is true, that prototype is populated with the default property group if the row exists and returned. If the row does not exist null is returned in that case.
      • createInstance

        public IDBObject createInstance()
      • createInstanceOfType

        public IDBObject createInstanceOfType​(int typeId)
      • createPrototype

        public IDBObject createPrototype​(boolean concreteOnly)
      • registerInstance

        public IDBObject registerInstance​(IDBObject inst,
                                          boolean clearTransient)
      • initSyncForInst

        public void initSyncForInst​(java.lang.Object inst,
                                    boolean onDemand,
                                    boolean resetState)
      • replaceInstance

        public void replaceInstance​(IDBObject inst)
      • removeInstance

        public boolean removeInstance​(DBObject dbObj,
                                      boolean remove)
      • clearTypeCache

        public void clearTypeCache()
      • getIdColumnValue

        public java.lang.Object getIdColumnValue​(java.lang.Object inst,
                                                 int ci)
      • getIdColumnType

        public java.lang.Object getIdColumnType​(int ci)
      • getIdDBColumnType

        public DBColumnType getIdDBColumnType​(int ci)
      • getIdPropertyType

        public java.lang.Object getIdPropertyType​(int ci)
      • resolve

        public void resolve()
      • initDBObject

        public void initDBObject​(DBObject obj)
      • matchQuery

        public java.util.List<? extends IDBObject> matchQuery​(DBObject proto,
                                                              java.util.List<java.lang.String> protoProps,
                                                              java.lang.String selectGroup,
                                                              java.util.List<java.lang.String> orderByProps,
                                                              int startIx,
                                                              int maxResults)
        The core "declarative query" method to return objects for this type matching a prototype object and list of protoProp names. The query returns matching objects for all properties (i.e. 'where clause' using 'and' for all properties in the list). Optionally provide an orderByProps list for the list of properties used in sorting the result. Use "-propName" for a descending order sort on that property and start and max properties for limit/offset in the result list.
      • matchOne

        public IDBObject matchOne​(DBObject proto,
                                  java.util.List<java.lang.String> props,
                                  java.lang.String selectGroup)
      • mergeResultLists

        public java.util.List<IDBObject> mergeResultLists​(java.util.List<IDBObject> primary,
                                                          java.util.List<IDBObject> other)
      • waitForSchemaReady

        public void waitForSchemaReady()
        Allow test or application threads to wait until the SchemaManager finishes updating/validating the schema before continuing
      • getSchemaReady

        public boolean getSchemaReady()
      • namedQuery

        public java.lang.Object namedQuery​(java.lang.String queryName,
                                           java.lang.Object... args)
      • getTypeIdList

        public int[] getTypeIdList()
      • getSubTypeByTypeId

        public DBTypeDescriptor getSubTypeByTypeId​(int typeId)
      • updateTypeDescriptor

        public void updateTypeDescriptor​(IDBObject newInst,
                                         DBObject dbObj)
      • enableLiveDynTypes

        public void enableLiveDynTypes()
      • invalidateExpiredItems

        public void invalidateExpiredItems()