/** * Generates INSERT SQL based on this model. Uses the dialect associated with this model database to format the * value literals. * Example: * <pre> * String sql = user.toInsert(); * //yields this output: * //INSERT INTO users (id, email, first_name, last_name) VALUES (1, 'mmonroe@yahoo.com', 'Marilyn', 'Monroe') * </pre> * @param replacements an array of strings, where odd values are to be replaced in the values of the attributes * and even values are replacements. For instance, your value is "O'Donnel", which contains * a single quote. In order to escape/replace it, you can: * * <code>person.toInsert(dialect, "'", "''")</code>, which will escape a single quote by two * single quotes. * * @return INSERT SQL based on this model. */ public String toInsert(String ... replacements) { return toInsert(metaModelLocal.getDialect(), replacements); }
/** * Generates UPDATE SQL based on this model. Uses the dialect associated with this model database to format the * value literals. * Example: * <pre> * String sql = user.toUpdate(); * //yields this output: * //UPDATE users SET email = 'mmonroe@yahoo.com', first_name = 'Marilyn', last_name = 'Monroe' WHERE id = 1 * </pre> * * @param replacements an array of strings, where odd values are to be replaced in the values of the attributes * and even values are replacements. For instance, your value is "O'Donnel", which contains * a single quote. In order to escape/replace it, you can: * * <code>person.toUpdate(dialect, "'", "''")</code>, which will escape a single quote by two * single quotes. * * @return UPDATE SQL based on this model. */ public String toUpdate(String ... replacements) { return toUpdate(metaModelLocal.getDialect(), replacements); }
/** * Use to see what SQL will be sent to the database. * * @param showParameters true to see parameter values, false not to. * @return SQL in a dialect for current connection which will be used if you start querying this * list. */ public String toSql(boolean showParameters) { String sql; if(forPaginator){ sql = metaModel.getDialect().formSelect(null, null, fullQuery, orderBys, limit, offset); }else{ sql = fullQuery != null ? fullQuery : metaModel.getDialect().formSelect(metaModel.getTableName(), null, subQuery, orderBys, limit, offset); } if (showParameters) { StringBuilder sb = new StringBuilder(sql).append(", with parameters: "); join(sb, params, ", "); sql = sb.toString(); } return sql; }
Object convertedObject = metaModelLocal.getDialect().overrideDriverTypeConversion( metaModelLocal, entry.getKey(), entry.getValue()); if (willAttributeModifyModel(entry.getKey(), convertedObject)) {
this.countQueryFull = "SELECT " + countQuery + " FROM " + from; } else { this.countQueryFull = metaModel.getDialect().selectCount(from); this.countQueryFull = metaModel.getDialect().selectCount(metaModel.getTableName()); } else { throw new IllegalArgumentException("cannot provide parameters with query: '*'"); this.countQueryFull = metaModel.getDialect().selectCount(metaModel.getTableName(), query);
public static boolean exists(Class<? extends Model> clazz, Object id) { MetaModel metaModel = metaModelOf(clazz); return null != new DB(metaModel.getDbName()).firstCell(metaModel.getDialect().selectExists(metaModel), id); }
/** * Returns true if record corresponding to the id of this instance exists in the DB. * * @return true if corresponding record exists in DB, false if it does not. */ public boolean exists(){ return null != new DB(metaModelLocal.getDbName()).firstCell(metaModelLocal.getDialect().selectExists(metaModelLocal), getId()); }
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<>()); } } }
public static <T extends Model, M extends T> void findWith(final Class<M> clazz, final ModelListener<T> listener, String query, Object... params) { long start = System.currentTimeMillis(); final MetaModel metaModel = metaModelOf(clazz); String sql = metaModel.getDialect().selectStar(metaModel.getTableName(), query); new DB(metaModel.getDbName()).find(sql, params).with(new RowListenerAdapter() { @Override public void onNext(Map<String, Object> row) { listener.onModel(instance(row, metaModel, clazz)); } }); LogFilter.logQuery(LOGGER, sql, null, start); }
boolean containsId = (attributes.get(metaModel.getIdName()) != null); // do not use containsKey boolean done; String query = metaModel.getDialect().insertParametrized(metaModel, columns, containsId); if (containsId || getCompositeKeys() != null) { compositeKeyPersisted = done = (1 == new DB(metaModel.getDbName()).exec(query, values.toArray()));
return 1; } else if (metaModel.hasAssociation(child.getClass(), Many2ManyAssociation.class)) { return new DB(metaModel.getDbName()).exec(metaModel.getDialect().deleteManyToManyAssociation( metaModel.getAssociationForTarget(child.getClass(), Many2ManyAssociation.class)), getId(), child.getId());
String parentTable = parentMM.getTableName(); String parentIdName = parentMM.getIdName(); String query = metaModelLocal.getDialect().selectStarParametrized(parentTable, parentIdName);
new DB(metaModel.getDbName()).exec(metaModel.getDialect().insertManyToManyAssociation(ass), getId(), child.getId()); } else {
public static Long count(Class<? extends Model> clazz) { MetaModel metaModel = metaModelOf(clazz); String sql = metaModel.getDialect().selectCount(metaModel.getTableName()); Long result; if (metaModel.cached()) { result = (Long) QueryCache.instance().getItem(metaModel.getTableName(), sql, null); if (result == null) { result = Convert.toLong(new DB(metaModel.getDbName()).firstCell(sql)); QueryCache.instance().addItem(metaModel.getTableName(), sql, null, result); }else { LogFilter.logQuery(LOGGER, sql, new Object[]{}, System.currentTimeMillis(), true); } } else { result = Convert.toLong(new DB(metaModel.getDbName()).firstCell(sql)); } return result; }
public static Long count(Class<? extends Model> clazz, String query, Object... params) { MetaModel metaModel = metaModelOf(clazz); String sql = metaModel.getDialect().selectCount(metaModel.getTableName(), query); Long result; if (metaModel.cached()) { result = (Long) QueryCache.instance().getItem(metaModel.getTableName(), sql, params); if (result == null) { result = Convert.toLong(new DB(metaModel.getDbName()).firstCell(sql, params)); QueryCache.instance().addItem(metaModel.getTableName(), sql, params, result); } } else { result = Convert.toLong(new DB(metaModel.getDbName()).firstCell(sql, params)); } return result; }
/** * Returns true if record corresponding to the id of this instance exists in the DB. * * @return true if corresponding record exists in DB, false if it does not. */ public boolean exists(){ return null != new DB(metaModelLocal.getDbName()).firstCell(metaModelLocal.getDialect().selectExists(metaModelLocal), getId()); }
public static boolean exists(Class<? extends Model> clazz, Object id) { MetaModel metaModel = metaModelOf(clazz); return null != new DB(metaModel.getDbName()).firstCell(metaModel.getDialect().selectExists(metaModel), id); }
/** * Returns true if record corresponding to the id of this instance exists in the DB. * * @return true if corresponding record exists in DB, false if it does not. */ public boolean exists(){ MetaModel metaModel = getMetaModelLocal(); return null != new DB(metaModel.getDbName()).firstCell(metaModel.getDialect().selectExists(metaModel), getId()); }
public static <T extends Model, M extends T> void findWith(final Class<M> clazz, final ModelListener<T> listener, String query, Object... params) { long start = System.currentTimeMillis(); final MetaModel metaModel = metaModelOf(clazz); String sql = metaModel.getDialect().selectStar(metaModel.getTableName(), query); new DB(metaModel.getDbName()).find(sql, params).with(new RowListenerAdapter() { @Override public void onNext(Map<String, Object> row) { listener.onModel(instance(row, metaModel, clazz)); } }); LogFilter.logQuery(logger, sql, null, start); }
public static <T extends Model, M extends T> void findWith(final Class<M> clazz, final ModelListener<T> listener, String query, Object... params) { long start = System.currentTimeMillis(); final MetaModel metaModel = metaModelOf(clazz); String sql = metaModel.getDialect().selectStar(metaModel.getTableName(), query); new DB(metaModel.getDbName()).find(sql, params).with(new RowListenerAdapter() { @Override public void onNext(Map<String, Object> row) { listener.onModel(instance(row, metaModel, clazz)); } }); LogFilter.logQuery(LOGGER, sql, null, start); }