/** * Returns {@link MetaModel} associated with table name. * * @param tableName name of table. * @return {@link MetaModel} associated with table name */ public static MetaModel metaModelFor(String tableName) { return Registry.instance().getMetaModel(tableName); }
QueryCache() { String cacheManagerClass = Registry.instance().getConfiguration().getCacheManager(); if(cacheManagerClass != null){ try{ Class cmc = Class.forName(cacheManagerClass); cacheManager = (CacheManager)cmc.newInstance(); enabled = true; }catch(InitException e){ throw e; }catch(Exception e){ throw new InitException("Failed to initialize a CacheManager. Please, ensure that the property " + "'cache.manager' points to correct class which extends '" + CacheManager.class.getName() + "' and provides a default constructor.", e); } }else{ cacheManager = new NopeCacheManager(); } }
private static void collectStatistics(String query, long time, boolean cacheHit) { if (Registry.instance().getConfiguration().collectStatistics() && !cacheHit) { Registry.instance().getStatisticsQueue().enqueue(new QueryExecutionEvent(query, time)); } }
private void discoverAssociationsFor(String source, String dbName) { discoverOne2ManyAssociationsFor(source, dbName); discoverMany2ManyAssociationsFor(source, dbName); }
public static String tableNameOf(Class<? extends Model> clazz) { return Registry.instance().getTableName(clazz); }
if (staticMetadataStatus != STATIC_METADATA_CHECKED && loadStaticMetadata()) return; String dbType = c.getMetaData().getDatabaseProductName(); Set<Class<? extends Model>> modelClasses = ModelFinder.getModelsForDb(dbName); registerModels(dbName, modelClasses, dbType); String[] tables = metaModels.getTableNames(dbName); Map<String, ColumnMetadata> metaParams = fetchMetaParams(databaseMetaData, dbType, table); registerColumnMetadata(table, metaParams); discoverAssociationsFor(table, dbName); processOverrides(modelClasses); } catch (Exception e) { initedDbs.remove(dbName);
private static ModelRegistry modelRegistryOf(Class<? extends Model> clazz) { return Registry.instance().modelRegistryOf(clazz); }
registerModel(dbName, modelClass, databaseProductName); Map<String, ColumnMetadata> metaParams = fetchMetaParams(databaseMetaData, databaseProductName, table); registerColumnMetadata(table, metaParams); processOverrides(getModelClasses(dbName)); discoverAssociationsFor(table, dbName);
private void deleteChildrenDeep(List<? extends Association> childAssociations){ for (Association association : childAssociations) { String targetTableName = metaModelOf(association.getTargetClass()).getTableName(); Class c = Registry.instance().getModelClass(targetTableName, false); if(c == null){// this model is probably not defined as a class, but the table exists! LogFilter.log(LOGGER, LogLevel.ERROR, "ActiveJDBC WARNING: failed to find a model class for: {}, maybe model is not defined for this table?" + " There might be a risk of running into integrity constrain violation if this model is not defined.", targetTableName); } else{ List<Model> dependencies = getAll(c); for (Model model : dependencies) { model.deleteCascade(); } } } }
List<Class<? extends Model>> modelClasses = getModelClasses(models); registerModels(dbName, modelClasses, databaseProductName); String[] tables = metaModels.getTableNames(dbName); Map<String, ColumnMetadata> metaParams = fetchMetaParams(databaseMetaData, databaseProductName, table); registerColumnMetadata(table, metaParams); discoverAssociationsFor(table, dbName);
this.query = query; this.params = params; String tableName = Registry.instance().getTableName(modelClass); this.metaModel = Registry.instance().getMetaModel(tableName);
static void purgeEdges(MetaModel metaModel) { //this is to eliminate side effects of cache on associations. //TODO: Need to write tests for cases; // 1. One to many relationship. Parent and child are cached. // When a new child inserted, the parent.getAll(Child.class) should see that // 2. Many to many. When a new join inserted, updated or deleted, the one.getAll(Other.class) should see the difference. //Purge associated targets List<Association> associations = metaModel.getAssociations(); for(Association association: associations){ Registry.cacheManager().purgeTableCache(metaModelOf(association.getTargetClass())); } //Purge edges in case this model represents a join List<String> edges = Registry.instance().getEdges(metaModel.getTableName()); for(String edge: edges){ Registry.cacheManager().purgeTableCache(edge); } }
@Override public void after() { Registry.instance().getStatisticsQueue().enqueue( new QueryExecutionEvent(getRoute().getController().getClass().getName() + "#" + getRoute().getActionName() + ":" + method(), System.currentTimeMillis() - start.get())); } }
processOverridesBelongsTo(modelClass, belongsToAnnotation); processOverridesBelongsTo(modelClass, belongsTo); String source = getTableName(modelClass); String target = getTableName(otherClass); String join = many2manyAnnotation.join(); String sourceFKName = many2manyAnnotation.sourceFKName(); MetaModel mm = getMetaModel(source); thisPk = mm.getIdName(); mm = getMetaModel(target); otherPk = mm.getIdName(); } catch (Exception e) { new BelongsToPolymorphicAssociation(getTableName(modelClass), getTableName(parentClass), typeLabel, parentClass.getName()); metaModels.getMetaModel(modelClass).addAssociation(belongsToPolymorphicAssociation); new OneToManyPolymorphicAssociation(getTableName(parentClass), getTableName(modelClass), typeLabel); metaModels.getMetaModel(parentClass).addAssociation(oneToManyPolymorphicAssociation);
static void purgeEdges(MetaModel metaModel) { //this is to eliminate side effects of cache on associations. //TODO: Need to write tests for cases; // 1. One to many relationship. Parent and child are cached. // When a new child inserted, the parent.getAll(Child.class) should see that // 2. Many to many. When a new join inserted, updated or deleted, the one.getAll(Other.class) should see the difference. //Purge associated targets List<Association> associations = metaModel.getAssociations(); for(Association association: associations){ QueryCache.instance().purgeTableCache(association.getTarget()); } //Purge edges in case this model represents a join List<String> edges = Registry.instance().getEdges(metaModel.getTableName()); for(String edge: edges){ QueryCache.instance().purgeTableCache(edge); } }
private void discoverMany2ManyAssociationsFor(String source, String dbName) { for (String potentialJoinTable : metaModels.getTableNames(dbName)) { String target = Inflector.getOtherName(source, potentialJoinTable); if (target != null && getMetaModel(target) != null && hasForeignKeys(potentialJoinTable, source, target)) { Class<? extends Model> sourceModelClass = metaModels.getModelClass(source); Class<? extends Model> targetModelClass = metaModels.getModelClass(target); Association associationSource = new Many2ManyAssociation(sourceModelClass, targetModelClass, potentialJoinTable, getMetaModel(source).getFKName(), getMetaModel(target).getFKName()); getMetaModel(source).addAssociation(associationSource); } } }
private boolean loadStaticMetadata() { try { Enumeration<URL> urls = Registry.instance().getClass().getClassLoader().getResources("activejdbc_metadata.json"); staticMetadataStatus = urls.hasMoreElements() ? STATIC_METADATA_LOADED : STATIC_METADATA_CHECKED; while(urls.hasMoreElements()) { URL url = urls.nextElement(); LogFilter.log(LOGGER, LogLevel.INFO, "Loading metadata from: {}", url.toExternalForm()); metaModels.fromJSON(Util.read(url.openStream())); } return staticMetadataStatus == STATIC_METADATA_LOADED; } catch(IOException e) { throw new InitException(e); } }
/** * Checks that the "join" table has foreign keys from "source" and "other" tables. Returns true * if "join" table exists and contains foreign keys of "source" and "other" tables, false otherwise. * * @param join - potential name of a join table. * @param source name of a "source" table * @param other name of "other" table. * @return true if "join" table exists and contains foreign keys of "source" and "other" tables, false otherwise. */ private boolean hasForeignKeys(String join, String source, String other) { String sourceFKName = getMetaModel(source).getFKName(); String otherFKName = getMetaModel(other).getFKName(); MetaModel joinMM = getMetaModel(join); return joinMM.hasAttribute(sourceFKName) && joinMM.hasAttribute(otherFKName); }
Map<String, ColumnMetadata> columns = getColumns(rs, dbType); rs.close(); columns = getColumns(rs, dbType); rs.close(); columns = getColumns(rs, dbType); rs.close();
public static String tableNameOf(Class<? extends Model> clazz) { return Registry.instance().getTableName(clazz); }