/** * Name of ID column. * * @return Name of ID column. */ public String getIdName() { return metaModelLocal.getIdName(); }
@Override protected void appendEmptyRow(MetaModel metaModel, StringBuilder query) { query.append('(').append(metaModel.getIdName()).append(") VALUES (DEFAULT)"); } }
@Override protected void appendEmptyRow(MetaModel metaModel, StringBuilder query) { query.append('(').append(metaModel.getIdName()).append(") VALUES (") .append(metaModel.getIdGeneratorCode() != null ? metaModel.getIdGeneratorCode() : "NULL") .append(')'); }
/** * Finds all attribute names except for id. * * @return all attribute names except for id. */ public Set<String> getAttributeNamesSkipId() { if (attributeNamesNoId == null) {//no one cares about unfortunate multi-threading timing with 2 instances created //if someone does, use DCL with volatile Set<String> attributesNames = new CaseInsensitiveSet(getAttributeNames()); attributesNames.remove(getIdName()); attributeNamesNoId = attributesNames; } return attributeNamesNoId; }
@Override public String insertParametrized(MetaModel metaModel, List<String> columns, boolean containsId) { StringBuilder query = new StringBuilder().append("INSERT INTO ").append(metaModel.getTableName()).append(' '); if (columns.isEmpty()) { appendEmptyRow(metaModel, query); } else { boolean addIdGeneratorCode = (!containsId && metaModel.getIdGeneratorCode() != null); query.append('('); if (addIdGeneratorCode) { query.append(metaModel.getIdName()).append(", "); } join(query, columns, ", "); query.append(") VALUES ("); if (addIdGeneratorCode) { query.append(metaModel.getIdGeneratorCode()).append(", "); } appendQuestions(query, columns.size()); query.append(')'); } return query.toString(); }
String idName = metaModel.getIdName();
@Override public String insert(MetaModel metaModel, Map<String, Object> attributes, String ... replacements) { StringBuilder query = new StringBuilder().append("INSERT INTO ").append(metaModel.getTableName()).append(' '); if (attributes.isEmpty()) { appendEmptyRow(metaModel, query); } else { boolean addIdGeneratorCode = (metaModel.getIdGeneratorCode() != null && attributes.get(metaModel.getIdName()) == null); // do not use containsKey query.append('('); if (addIdGeneratorCode) { query.append(metaModel.getIdName()).append(", "); } join(query, attributes.keySet(), ", "); query.append(") VALUES ("); if (addIdGeneratorCode) { query.append(metaModel.getIdGeneratorCode()).append(", "); } Iterator<Object> it = attributes.values().iterator(); appendValue(query, it.next()); while (it.hasNext()) { query.append(", "); appendValue(query, it.next(), replacements); } query.append(')'); } return query.toString(); }
private void processParent(BelongsToAssociation association) { if (delegate.isEmpty()) { // no need to process parents if no models selected. return; } //need to remove duplicates because more than one child can belong to the same parent. Set<Object> distinctParentIds = collectDistinct(association.getFkName()); distinctParentIds.remove(null); // remove null parent id if (distinctParentIds.isEmpty()) { return; } final MetaModel parentMetaModel = metaModelOf(association.getTargetClass()); final Map<Object, Model> parentById = new HashMap<>(); StringBuilder query = new StringBuilder().append(parentMetaModel.getIdName()).append(" IN ("); appendQuestions(query, distinctParentIds.size()); query.append(')'); for (Model parent : new LazyList<>(query.toString(), parentMetaModel, distinctParentIds.toArray())) { parentById.put(parent.getId(), parent); } //now that we have the parents in the has, we need to distribute them into list of children that are //stored in the delegate. for (Model child : delegate) { // parent could be null, which is fine child.setCachedParent(parentById.get(child.get(association.getFkName()))); } }
/** * @author Evan Leonard */ private void processPolymorphicParent(BelongsToPolymorphicAssociation association) { if (delegate.isEmpty()) { // no need to process children if no models selected. return; } //need to remove duplicates because more than one child can belong to the same parent. Set<Object> distinctParentIds = collectDistinct("parent_id", "parent_type", association.getParentClassName()); distinctParentIds.remove(null); // remove null parent id if (distinctParentIds.isEmpty()) { return; } final MetaModel parentMetaModel = metaModelOf(association.getTargetClass()); final Map<Object, Model> parentById = new HashMap<>(); StringBuilder query = new StringBuilder().append(parentMetaModel.getIdName()).append(" IN ("); appendQuestions(query, distinctParentIds.size()); query.append(')'); for (Model parent : new LazyList<>(query.toString(), parentMetaModel, distinctParentIds.toArray())) { parentById.put(association.getParentClassName() + ":" + parent.getId(), parent); } //now that we have the parents in the has, we need to distribute them into list of children that are //stored in the delegate. for (Model child : delegate) { // parent could be null, which is fine child.setCachedParent(parentById.get(association.getParentClassName() + ":" + child.get("parent_id"))); } }
private void processPolymorphicChildren(OneToManyPolymorphicAssociation association) { if (delegate.isEmpty()) {//no need to process children if no models selected. return; } MetaModel childMetaModel = metaModelOf(association.getTargetClass()); Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); StringBuilder query = new StringBuilder().append("parent_id IN ("); appendQuestions(query, ids.size()); query.append(") AND parent_type = '").append(association.getTypeLabel()).append('\''); for (Model child : new LazyList<>(query.toString(), childMetaModel, ids.toArray()).orderBy(childMetaModel.getIdName())) { if (childrenByParentId.get(child.get("parent_id")) == null) { childrenByParentId.put(child.get("parent_id"), new SuperLazyList<>()); } childrenByParentId.get(child.get("parent_id")).add(child); } for (T parent : delegate) { List<Model> children = childrenByParentId.get(parent.getId()); if (children != null) { parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
private void processChildren(OneToManyAssociation association) { if(delegate.isEmpty()){//no need to process children if no models selected. return; } final MetaModel childMetaModel = metaModelOf(association.getTargetClass()); final String fkName = association.getFkName(); final Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); StringBuilder query = new StringBuilder().append(fkName).append(" IN ("); appendQuestions(query, ids.size()); query.append(')'); for (Model child : new LazyList<>(query.toString(), childMetaModel, ids.toArray()).orderBy(childMetaModel.getIdName())) { if(childrenByParentId.get(child.get(fkName)) == null){ childrenByParentId.put(child.get(fkName), new SuperLazyList<>()); } childrenByParentId.get(child.get(fkName)).add(child); } for(T parent : delegate){ List<Model> children = childrenByParentId.get(parent.getId()); if(children != null){ parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
boolean containsId = (attributes.get(metaModel.getIdName()) != null); // do not use containsKey boolean done; String query = metaModel.getDialect().insertParametrized(metaModel, columns, containsId); compositeKeyPersisted = done = (1 == new DB(metaModel.getDbName()).exec(query, values.toArray())); } else { Object id = new DB(metaModel.getDbName()).execInsert(query, metaModel.getIdName(), values.toArray()); attributes.put(metaModel.getIdName(), id); done = (id != null);
public static <T extends Model> T findById(Class<T> clazz, Object id) { if (id == null) { return null; } MetaModel metaModel = metaModelOf(clazz); LazyList<T> list = new LazyList<T>(metaModel.getIdName() + " = ?", metaModel, id).limit(1); return list.isEmpty() ? null : list.get(0); }
private void processManyToManyOverrides(Many2Many many2manyAnnotation, Class<? extends Model> modelClass){ Class<? extends Model> otherClass = many2manyAnnotation.other(); String source = metaModels.getTableName(modelClass); String target = metaModels.getTableName(otherClass); String join = many2manyAnnotation.join(); String sourceFKName = many2manyAnnotation.sourceFKName(); String targetFKName = many2manyAnnotation.targetFKName(); String otherPk; String thisPk; try { Method m = modelClass.getMethod("getMetaModel"); MetaModel mm = (MetaModel) m.invoke(modelClass); thisPk = mm.getIdName(); m = otherClass.getMethod("getMetaModel"); mm = (MetaModel) m.invoke(otherClass); otherPk = mm.getIdName(); } catch (Exception e) { throw new InitException("failed to determine PK name in many to many relationship", e); } Association many2many1 = new Many2ManyAssociation(modelClass, otherClass, join, sourceFKName, targetFKName, otherPk); metaModels.getMetaModel(source).addAssociation(many2many1); Association many2many2 = new Many2ManyAssociation(otherClass, modelClass, join, targetFKName, sourceFKName, thisPk); metaModels.getMetaModel(target).addAssociation(many2many2); }
private void processManyToMany(Many2ManyAssociation association) { if(delegate.isEmpty()){//no need to process other if no models selected. return; } final MetaModel childMetaModel = metaModelOf(association.getTargetClass()); final Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); List<Map> childResults = new DB(childMetaModel.getDbName()).findAll(childMetaModel.getDialect().selectManyToManyAssociation( association, "the_parent_record_id", ids.size()), ids.toArray()); for(Map res: childResults){ Model child = ModelDelegate.instance(res, childMetaModel); Object parentId = res.get("the_parent_record_id"); if(childrenByParentId.get(parentId) == null){ childrenByParentId.put(parentId, new SuperLazyList<>()); } childrenByParentId.get(parentId).add(child); } for(T parent : delegate){ List<Model> children = childrenByParentId.get(parent.getId()); if (children != null) { parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
query.append(" WHERE ").append(metaModel.getIdName()).append(" = ?"); values.add(getId()); if(metaModel.hasPartitionIDs()){
String parentIdName = parentMM.getIdName(); String query = metaModelLocal.getDialect().selectStarParametrized(parentTable, parentIdName);
String subQuery; String targetId = metaModelOf(targetModelClass).getIdName(); MetaModel targetMM = metaModelOf(targetModelClass); String targetTable = targetMM.getTableName();
@Override protected void appendEmptyRow(MetaModel metaModel, StringBuilder query) { query.append('(').append(metaModel.getIdName()).append(") VALUES (") .append(metaModel.getIdGeneratorCode() != null ? metaModel.getIdGeneratorCode() : "NULL") .append(')'); }