/** * Delete this object using the specified database connection. * * @param db The database connection to use. */ public void delete(SQLiteDatabase db) { EntityMapping mapping = getEntityMappingEnsureSchema(db); if (!mTransient) { mapping.delete(db, this); } }
private EntityMapping getEntityMappingEnsureSchema(SQLiteDatabase db) { EntityMapping map = getEntityMapping(); if (!map.mSchemaCreated) { map.createSchema(db); } return map; }
static EntityMapping getEntityMappingEnsureSchema( SQLiteDatabase db, Class<? extends Entity> clz) { EntityMapping map = getEntityMapping(clz); if (!map.mSchemaCreated) { map.createSchema(db); } return map; }
/** * Insert or update this object using the specified database connection. * * @param db The database connection to use. * @return The primary key of the inserted item (if object was transient), or -1 if an update was * performed. */ public int save(SQLiteDatabase db) { EntityMapping mapping = getEntityMappingEnsureSchema(db); int result = -1; if (mTransient) { result = mapping.insert(db, this); mTransient = false; } else { mapping.update(db, this); } return result; }
/** * Find the columns name in the formula and replace its by qualify columns name: 'name' -> * 't_reference.name' */ private String qualifyFormulaWithReferenceName( String formula, String referenceName, String modelProperty) { EntityMapping em = MetaComponent.get(referenceName).getEntityMapping(); Iterator<String> it = em.getColumns().iterator(); while (it.hasNext()) { String column = it.next(); if (formula.contains(column)) { formula = formula.replace( column, getQualifyColumnName(modelProperty, referenceName + "." + column)); } } return formula; }
/* * Package private - used by Query as well as locally... */ static EntityMapping getEntityMapping(Class<? extends Entity> clz) { EntityMapping mapping = entityMappings.get(clz); if (mapping == null) { // build map entityMappings.put(clz, mapping = EntityMapping.build(clz)); } return mapping; }
// Not concerned too much about reflective annotation access in this // method, since this only runs once per model class... static EntityMapping build(Class<? extends Entity> clz) { EntityMapping mapping = new EntityMapping(); mapping.mMappedClass = clz; Table table = clz.getAnnotation(Table.class); if (table != null) { mapping.mTableName = table.name(); } else { mapping.mTableName = MATCH_DOTDOLLAR.matcher(clz.getName()).replaceAll(""); } ArrayList<String> seenFields = new ArrayList<String>(); for (Field f : clz.getFields()) { // Blithely ignore this field if we've already seen one with same name - // Java field hiding allows this to happen and if it does, without this // we'd be adding the same column name twice. // // We might as well also ignore it here if it's inverse, since we'll // never want to access it via the mapping. Column colAnn = f.getAnnotation(Column.class); boolean inverse = colAnn != null && colAnn.inverse(); if (!seenFields.contains(f.getName()) && !inverse) { Column col = f.getAnnotation(Column.class); String name; if (col != null) { // empty is default, means we should use field name... if ("".equals(name = col.name())) { name = f.getName(); } if (col.primaryKey()) { mapping.mPrimaryKey = f; mapping.mPrimaryKeyColumnName = name; } } else { name = f.getName(); } // Try to default primary key if we don't have one yet... if (mapping.mPrimaryKey == null) { if ("_id".equals(name) || "id".equals(name)) { mapping.mPrimaryKey = f; mapping.mPrimaryKeyColumnName = name; } } mapping.mFields.add(f); mapping.mColumnNames.add(name); seenFields.add(f.getName()); } } if (mapping.mPrimaryKey == null) { // Error at this point - we must have a primary key! Log.e(TAG, "No primary key specified or determined for " + clz); throw new ORMDroidException( "No primary key was specified, and a default could not be determined for " + clz); } return mapping; }