예제 #1
0
  public static boolean visit(MappingVisitor visitor, SQLSelectQueryBlock x) {
    if (x.getSelectList().size() == 0) {
      fillSelectList(visitor, x);
    }

    if (x.getSelectList().size() == 1) {
      if (x.getSelectList().get(0).getExpr() instanceof SQLAllColumnExpr) {
        x.getSelectList().clear();
        fillSelectList(visitor, x);
      }
    }

    if (x.getFrom() == null) {
      Entity firstEntity = visitor.getFirstEntity();
      SQLExprTableSource from =
          new SQLExprTableSource(new SQLIdentifierExpr(firstEntity.getName()));
      x.setFrom(from);
    }

    for (SQLSelectItem item : x.getSelectList()) {
      item.setParent(x);
    }

    return true;
  }
예제 #2
0
  public static boolean visit(MappingVisitor visitor, SQLIdentifierExpr x) {
    String propertyName = x.getName();

    Property property = null;
    for (Entity entity : visitor.getEntities().values()) {
      property = entity.getProperty(propertyName);
      if (property != null) {
        break;
      }
    }

    if (property == null) {
      throw new DruidMappingException("property not found : " + propertyName);
    }

    String dbColumName = property.getDbColumnName();
    x.setName(dbColumName);

    if (x.getParent() instanceof SQLSelectItem) {
      SQLSelectItem selectItem = (SQLSelectItem) x.getParent();
      if (selectItem.getAlias() == null) {
        selectItem.setAlias('"' + property.getName() + '"');
      }
    }

    return false;
  }
예제 #3
0
  public static void fillSelectList(MappingVisitor visitor, SQLSelectQueryBlock x) {
    Entity entity = visitor.getFirstEntity();

    for (Property item : entity.getProperties().values()) {
      x.getSelectList()
          .add(
              new SQLSelectItem(new SQLIdentifierExpr(item.getName()), '"' + item.getName() + '"'));
    }
  }
예제 #4
0
  public static boolean visit(MappingVisitor visitor, SQLExprTableSource x) {
    SQLExpr expr = x.getExpr();

    if (expr instanceof SQLIdentifierExpr) {
      SQLIdentifierExpr tableExpr = (SQLIdentifierExpr) expr;
      String entityName = tableExpr.getName();

      Entity entity = visitor.getEntity(entityName);

      if (entity == null) {
        throw new DruidMappingException("entity not foudn : " + entityName);
      }

      tableExpr.setName(entity.getTableName());
    }

    if (x.getAlias() != null) {
      visitor.getTableSources().put(x.getAlias(), x);
    }

    return false;
  }
예제 #5
0
  protected void setUp() throws Exception {
    Entity entity = new Entity();
    entity.setName("设备");
    entity.setDescription("Device");
    entity.setTableName("device");

    entity.addProperty(new Property("编号", "", "serviceTag"));
    entity.addProperty(new Property("IP地址", "", "ip"));

    engine.addEntity(entity);
  }
예제 #6
0
 public static void setDataSource(MappingEngine engine, SQLInsertStatement stmt) {
   if (stmt.getTableSource() == null) {
     Entity entity = engine.getFirstEntity();
     stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
   }
 }