/** * Returns <code>true</code> if has {@link SqlMetadata}. */ default boolean hasMetadata() { return null != getMetadata(); }
/** * Executes batch update and returns the affected rows. * * @see PreparedStatement#executeBatch() */ int[] executeBatchUpdate(SqlContext context,Object[] batchParams,BatchPreparedStatementHandler<Db> preparedStatementHandler) throws IllegalStateException, NestedSQLException;
@Override public <T> T executeQuery(ResultSetReader<T> reader, Object[] args) { return cmd.executeQuery(this, args, reader); }
@Override public int executeUpdate() { return cmd.executeUpdate(this,null); }
if(null != exists){ if(Strings.isEmpty(dbType) && !Strings.isEmpty(exists.getDbType())){ return; if(!Strings.isEmpty(dbType) && Strings.isEmpty(exists.getDbType())){ override = true; throw new SqlConfigException("Found duplicated sql key '" + key + "', xmls : " + exists.getSource() + "," + reader.getSource());
protected void createSqlOperation(RestdContext ctx, RestdModel model, RestdOperationDef od, SqlOperationDef op, SqlCommand sc) { if (!sc.hasMetadata()) { throw new ApiConfigException("Sql '" + op.getKey() + "' has no metadata!"); SqlMetadata sm = sc.getMetadata(); if (sm.isUnknown()) { throw new ApiConfigException("Sql '" + op.getKey() + "' is unknown!");
protected void createSqlOperation(RestdContext ctx, RestdModel model, RestdOperationDef od, SqlOperationDef sod) { OrmMetadata om = ctx.getDao().getOrmContext().getMetadata(); SqlCommand sc = null; if(!Strings.isEmpty(sod.getKey())) { sc = om.tryGetSqlCommand(sod.getKey()); if (null == sc) { throw new ApiConfigException("Sql key '" + sod.getKey() + "' not found"); } }else if(!Strings.isEmpty(sod.getScript())) { OrmContext oc = ctx.getDao().getOrmContext(); try { sc = oc.getSqlFactory().createSqlCommand(oc, sod.getScript()).prepare(oc); }catch (Exception e) { throw new ApiConfigException("Error parsing sql of operation '" + sod.getName() + "', " + e.getMessage(), e); } } createSqlOperation(ctx, model, od, sod, sc); }
public String process() { //@include(key;required=true|false) StringBuilder sb = new StringBuilder(chars.length); for(pos=0;pos<chars.length;pos++) { char c = chars[pos]; if(c == '@') { int mark = pos; if(nextInclude()) { String content = scanIncludeContent(); if(null != content) { SqlFragment fragment = context.getMetadata().tryGetSqlFragment(content); if(null == fragment) { throw new SqlConfigException("The included sql fragment '" + content + "' not found in sql '" + command + "', check " + command.getSource()); } String fragmentContent = fragment.getContent(); if(!Strings.containsIgnoreCase(fragmentContent, SqlIncludeProcessor.AT_INCLUDE)) { sb.append(fragmentContent); }else{ sb.append(new SqlIncludeProcessor(context,command,fragmentContent).process()); } continue; } } sb.append(Chars.substring(chars, mark, pos)); } sb.append(c); } return sb.toString(); }
protected boolean matchContext(SqlReaderContext context, SqlCommand sql) { if(!Strings.isEmpty(sql.getDbType())) { final String currentDbType = context.getConfigContext().getDb().getType(); if(!currentDbType.equalsIgnoreCase(sql.getDbType())) { return false; } } if(!Strings.isEmpty(sql.getDataSourceName())) { final String currentDataSourceName = context.getConfigContext().getName(); if(!currentDataSourceName.equalsIgnoreCase(sql.getDataSourceName())) { return false; } } return true; }
@Override public long count() { return command.executeCount(this, params()); }
@Override public SqlClause getSqlClause() { return command.getSqlClause(); } }
@Override public boolean resolveInjectValue(BeanDefinition bd, Object bean, ReflectValued v, Annotation a, Out<Object> value) { if (!leap.orm.dao.DaoCommand.class.equals(v.getType())) { return false; } KeyAndDataSource kds; if (a.annotationType().equals(Inject.class)) { kds = resolveSqlIdentity(bd, v, (Inject) a); } else if (a.annotationType().equals(SqlKey.class)) { kds = resolveSqlIdentity(bd, v, (SqlKey) a); } else { return false; } SqlCommand sql = sqls.tryGetSqlCommand(kds.key); if (null == sql) { throw new BeanCreationException("Sql key '" + kds.key + "' not found, check the bean : " + bd); } Dao dao; if (!Strings.isEmpty(sql.getDataSourceName())) { dao = Dao.get(sql.getDataSourceName()); } else if (!Strings.isEmpty(kds.dataSource)) { dao = Dao.get(kds.dataSource); } else { dao = Dao.get(); } value.set(new SimpleDaoCommand(dao, sql)); return true; }
protected int executeUpdate(){ return sqlCommand.executeUpdate(this, params); } }
command.prepare(context);
protected void createMetadata(OrmContext oc, MetaApi api) { mappingDatabase(oc.getDb(), api); MappingSchema ms = ormConverter.toMappingSchema(oc, api); for(EntityMapping em : ms.getEntityMappings()) { oc.getMetadataManager().createEntity(oc, em); } //load sqls oc.getMetadataManager().loadSqls(oc); //convert sqls Map<String, SqlCommand> commands = ormConverter.toSqlCommands(oc, api); commands.forEach((k,c) -> { SqlCommand old = oc.getMetadata().tryGetSqlCommand(k); if(null != old) { log.info("The sql command '{}' from '{}' will be replaced by '{}'", k, old.getSource(), c.getSource()); oc.getMetadata().removeSqlCommand(k); } oc.getMetadata().addSqlCommand(k, c); }); }
@Override public long count() { return command.executeCount(this, params()); }
@Override public int executeUpdate(Params params) { return cmd.executeUpdate(this, params); }
@Override public <T> T executeQuery(ResultSetReader<T> reader) { return cmd.executeQuery(this, null, reader); }
@Override public Object apply(ActionParams params) { Map<String,Object> map = params.toMap(); Object result; if(command.getMetadata().isSelect()) { //todo: page query, total count Query query = dao.createQuery(command).params(map); if(null != returnType) { if(returnType.isSimpleType()) { result = Converts.convert(query.scalarValueOrNull(), returnType.asSimpleType().getJavaType()); }else if(returnType.isCollectionType() && returnType.asCollectionType().getElementType().isSimpleType()) { result = query.scalars().list(returnType.asCollectionType().getElementType().asSimpleType().getJavaType()); }else { result = query.list(); } }else { result = query.list(); } }else{ //todo: the return type must be simple type result = dao.executeUpdate(command, map); if(null != returnType) { result = Converts.convert(result, returnType.asSimpleType().getJavaType()); } } return ApiResponse.of(result); }
@Override public int[] execute() { final SqlCommand primaryCommand = metadata.getSqlCommand(em.getEntityName(), SqlCommand.DELETE_COMMAND_NAME); final SqlCommand secondaryCommand = em.hasSecondaryTable() ? context.getSqlFactory().createDeleteCommand(context, em, true) : null; if(null == secondaryCommand) { return primaryCommand.executeBatchUpdate(this, idParameters); } return dao.doTransaction((s) -> { secondaryCommand.executeBatchUpdate(this, idParameters); return primaryCommand.executeBatchUpdate(this, idParameters); }); } }