Example #1
0
 String renderDropStructure(EntityModel model, Configuration cfg) throws IOException {
   StringWriter out = new StringWriter();
   try {
     Map<String, Object> params = new HashMap<String, Object>();
     params.put("model", model);
     if (model instanceof ViewEntityModel)
       getTemplate(cfg, "sql", "model_drop_view").process(params, out);
     else if (model.getExtensions().contains("fts3"))
       getTemplate(cfg, "sql", "model_drop_fts3").process(params, out);
     else getTemplate(cfg, "sql", "model_drop").process(params, out);
   } catch (TemplateException err) {
     throw new IOException(err);
   }
   return out.toString().replaceAll("\\n", "").replaceAll("\\r", "");
 }
Example #2
0
  static EntityModel processEntity(JSONObject entity, DbContext ctx, Version version) {
    if (entity.has("rootModel")) { // if there is a rootModel property, we're dealing with a view
      processEntityView(entity, ctx);
    } else { // otherwise an entity table
      EntityModelVersion v =
          ctx.currentVersion()
              .newModel(
                  entity.getString("name"), !entity.has("render") || entity.getBoolean("render"))
              .currentVersion();
      if (entity.has("properties")) {
        JSONArray properties = entity.getJSONArray("properties");
        for (int p = 0; p < properties.length(); p++) {
          JSONObject property = properties.getJSONObject(p);
          v.newProperty(
              property.has("type")
                  ? EntityProperty.Type.valueOf(property.getString("type"))
                  : EntityProperty.Type.UNKNOWN,
              property.getString("name"),
              property.has("nullable") && property.getBoolean("nullable"));
        }
      }
    }

    EntityModel m = ctx.getModel(entity.getString("name"));
    if (m == null)
      throw new NullPointerException("Model " + entity.getString("name") + " not found");
    if (entity.has("extensions")) {
      JSONArray extensions = entity.getJSONArray("extensions");
      for (int p = 0; p < extensions.length(); p++) {
        m.getExtensions().add(extensions.getString(p));
      }
    }

    if (entity.has("queries")) processQueries(entity.getJSONObject("queries"), m, version);

    if (entity.has("indexes")) {
      JSONArray indexes = entity.getJSONArray("indexes");
      for (int i = 0; i < indexes.length(); i++) {
        JSONObject index = indexes.getJSONObject(i);

        List<EntityIndexProperty> properties = new ArrayList<EntityIndexProperty>();

        if (index.has("properties")) {
          JSONArray indexProperties = index.getJSONArray("properties");
          for (int p = 0; p < indexProperties.length(); p++) {
            Object prop = indexProperties.get(p);
            EntityIndexProperty property = new EntityIndexProperty();
            if (prop instanceof String) {
              property.setProperty(m.getProperty(m.currentVersion().getVersion(), (String) prop));
              property.setOrder(EntityIndexProperty.Order.DEFAULT);
            } else {
              JSONObject o = (JSONObject) prop;
              property.setProperty(
                  m.getProperty(m.currentVersion().getVersion(), o.getString("name")));
              property.setOrder(
                  o.has("order")
                      ? EntityIndexProperty.Order.valueOf(index.getString("order"))
                      : EntityIndexProperty.Order.DEFAULT);
            }
            properties.add(property);
          }
        }

        m.currentVersion()
            .newIndex(
                index.has("type")
                    ? EntityIndex.Type.valueOf(index.getString("type"))
                    : EntityIndex.Type.DEFAULT,
                index.getString("name"),
                properties);
      }
    }

    return m;
  }