Exemplo n.º 1
0
  static void processProperties(
      JSONObject entity, ViewEntityModel viewModel, DbContext ctx, EntityModelVersion version) {
    JSONArray properties = entity.getJSONArray("properties");
    for (int p = 0; p < properties.length(); p++) {
      JSONObject property = properties.getJSONObject(p);
      EntityModel child;
      String alias;

      if (property.has("model")) {
        String model = property.getString("model");
        if (model.equals(viewModel.getRootModel().getName())) {
          child = viewModel.getRootModel();
          alias = viewModel.getRootAlias();
        } else {
          ViewEntityModel.EntityRelationship relationship =
              viewModel.getModel(property.getString("model"));
          child = relationship.getModel();
          alias = relationship.getAlias();
        }
      } else {
        child = viewModel;
        alias = viewModel.getRootAlias();
      }

      String propertyName = property.getString("name");
      if ("*".equals(propertyName)) {
        for (EntityProperty childProperty : child.getAllProperties(version.getVersion())) {
          String name = childProperty.getName();
          if (childProperty.getModel() instanceof ViewEntityModel
              && childProperty.getAlias() != null) name = childProperty.getAlias();
          EntityProperty cp =
              version.newProperty(
                  childProperty.getType(), name, childProperty.isNullable(), null, null);
          cp.setModelAlias(alias);
          cp.setOriginalModel(childProperty.getModel());
        }
      } else {
        EntityProperty childProperty = null;
        try {
          childProperty = child.getProperty(version.getVersion(), propertyName);
        } catch (IllegalArgumentException ignore) {
        }
        EntityProperty cp =
            version.newProperty(
                childProperty == null
                    ? EntityProperty.Type.valueOf(property.getString("type"))
                    : childProperty.getType(),
                property.getString("name"),
                !property.has("nullable") || property.getBoolean("nullable"),
                property.has("alias") ? property.getString("alias") : null,
                property.has("sql") ? property.getString("sql") : null);
        cp.setModelAlias(alias);
        if (childProperty != null) cp.setOriginalModel(childProperty.getModel());
      }
    }
  }
Exemplo n.º 2
0
 static List<EntityProperty> getProperties(JSONObject query, EntityModel m, Version version) {
   List<EntityProperty> props;
   if (query.has("properties")) {
     props = new ArrayList<EntityProperty>();
     JSONArray names = query.getJSONArray("properties");
     for (int n = 0; n < names.length(); n++) {
       props.add(m.getProperty(version, names.getString(n)));
     }
   } else props = m.getAllProperties(version);
   return props;
 }
Exemplo n.º 3
0
 void renderMappers(Configuration cfg) throws IOException {
   for (EntityModel model : getAllModels()) {
     Map<String, Object> params = new HashMap<String, Object>();
     params.put("model", model);
     params.put("version", currentVersion());
     for (Query query : model.getSelectQueries()) {
       params.put("query", query);
       writeFile(
           defaultPackageName + ".mapper.cursor",
           model.getName() + query.getName() + "Mapper",
           getTemplate(cfg, "java", "cursorobjectmapper"),
           params);
     }
     params.put("prefix", "insert");
     params.put("suffix", "");
     writeFile(
         defaultPackageName + ".mapper.content.insert",
         model.getName() + "Mapper",
         getTemplate(cfg, "java", "contentvaluesmapper"),
         params);
     for (Query query : model.getUpdateQueries()) {
       params.put("prefix", "update");
       params.put("suffix", query.getName());
       writeFile(
           defaultPackageName + ".mapper.content.update",
           model.getName() + query.getName() + "Mapper",
           getTemplate(cfg, "java", "contentvaluesmapper"),
           params);
     }
   }
 }
Exemplo n.º 4
0
 void renderModels(Configuration cfg) throws IOException {
   List<EntityModel> allModels = getAllModels();
   Map<String, Object> params = new HashMap<String, Object>();
   params.put("models", allModels);
   params.put("packageName", defaultPackageName + ".providers");
   params.put("authority", authority);
   params.put("version", currentVersion());
   writeFile(
       defaultPackageName + ".providers", "Schemas", getTemplate(cfg, "java", "schema"), params);
   for (EntityModel model : allModels) {
     if (!model.isRender()) continue;
     params = new HashMap<String, Object>();
     params.put("model", model);
     params.put("version", currentVersion());
     params.put("properties", model.getAllProperties(null));
     writeFile(
         model.getPackageName() + ".model",
         model.getName(),
         getTemplate(cfg, "java", "model"),
         params);
   }
 }
Exemplo n.º 5
0
 static void processQueries(JSONObject queries, EntityModel m, Version version) {
   if (queries != null) {
     if (queries.has("select")) {
       JSONArray select = queries.getJSONArray("select");
       for (int s = 0; s < select.length(); s++) {
         JSONObject query = select.getJSONObject(s);
         List<EntityProperty> props = getProperties(query, m, version);
         m.newSelectQuery(
             query.getString("id"),
             query.isNull("selection") ? null : query.getString("selection"),
             query.isNull("orderBy") ? null : query.getString("orderBy"),
             props);
       }
     }
     if (queries.has("update")) {
       JSONArray update = queries.getJSONArray("update");
       for (int s = 0; s < update.length(); s++) {
         JSONObject query = update.getJSONObject(s);
         List<EntityProperty> props = getProperties(query, m, version);
         m.newUpdateQuery(
             query.getString("id"),
             query.isNull("selection") ? null : query.getString("selection"),
             props);
       }
     }
     if (queries.has("delete")) {
       JSONArray delete = queries.getJSONArray("delete");
       for (int s = 0; s < delete.length(); s++) {
         JSONObject query = delete.getJSONObject(s);
         m.newDeleteQuery(
             query.getString("id"),
             query.isNull("selection") ? null : query.getString("selection"));
       }
     }
   }
 }
Exemplo n.º 6
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", "");
 }
Exemplo n.º 7
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;
  }
Exemplo n.º 8
0
  void renderDbHelper(Configuration cfg) throws IOException {
    Version lastVersion = null;
    for (Version version : versions) {
      if (lastVersion != null) {
        List<String> sql = new ArrayList<String>();
        switch (version.getType()) {
          case DIFF:
            {
              Version.Diff diff = lastVersion.diff(version);
              for (EntityModel removed : diff.removedModels) {
                if (removed instanceof ViewEntityModel) sql.add(renderDropStructure(removed, cfg));
              }
              for (EntityModel removed : diff.removedModels) {
                if (!(removed instanceof ViewEntityModel))
                  sql.add(renderDropStructure(removed, cfg));
              }
              for (EntityIndex removed : diff.removedIndexes) {
                sql.add(renderDropIndex(removed, cfg));
              }
              for (EntityModel added : diff.addedModels) {
                if (!(added instanceof ViewEntityModel))
                  sql.add(renderCreateStructure(added, added.getProperties(version), cfg));
              }
              for (EntityModel added : diff.addedModels) {
                if (added instanceof ViewEntityModel)
                  sql.add(renderCreateStructure(added, added.getProperties(version), cfg));
              }
              for (EntityIndex added : diff.addedIndexes) {
                sql.add(renderCreateIndex(added, cfg));
              }
              diffProperties(sql, lastVersion, version, diff, cfg);
              break;
            }
          case DIFF_PROPERTIES:
            {
              Version.Diff diff = lastVersion.diff(version);
              diffProperties(sql, lastVersion, version, diff, cfg);
              break;
            }
          case ADD_MODELS:
            for (EntityModel model : version.getModels()) {
              sql.add(renderCreateStructure(model, model.getProperties(version), cfg));
              for (EntityIndex index : model.getIndexes(version)) {
                sql.add(renderCreateIndex(index, cfg));
              }
            }
            break;
          case DROP_MODELS:
            for (EntityModel removed : version.getModels()) {
              if (removed instanceof ViewEntityModel) sql.add(renderDropStructure(removed, cfg));
            }
            for (EntityModel removed : version.getModels()) {
              if (!(removed instanceof ViewEntityModel)) sql.add(renderDropStructure(removed, cfg));
            }
            break;
          case ADD_PROPERTIES:
            {
              for (EntityModel model : version.getModels()) {
                for (EntityProperty property : model.getProperties(version)) {
                  sql.add(renderAddProperty(property, cfg));
                }
              }
              break;
            }
          case ADD_INDEXES:
            {
              for (EntityModel model : version.getModels()) {
                for (EntityIndex index : model.getIndexes(version)) {
                  sql.add(renderCreateIndex(index, cfg));
                }
              }
              break;
            }
          case DROP_PROPERTIES:
            {
              for (EntityModel model : version.getModels()) {
                for (EntityProperty property : model.getProperties(version)) {
                  sql.add(renderDropProperty(property, cfg));
                }
              }
              break;
            }
          case DROP_INDEXES:
            {
              for (EntityModel model : version.getModels()) {
                for (EntityIndex index : model.getIndexes(version)) {
                  sql.add(renderDropIndex(index, cfg));
                }
              }
              break;
            }
        }

        version.setSql(sql);
      } else {
        List<String> sql = new ArrayList<String>();
        for (EntityModel model : version.getModels()) {
          sql.add(renderCreateStructure(model, model.getProperties(version), cfg));
          for (EntityIndex index : model.getIndexes(version)) {
            sql.add(renderCreateIndex(index, cfg));
          }
        }
        version.setSql(sql);
      }
      lastVersion = version;
    }

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("baseVersion", baseVersion());
    params.put("versions", versions);
    params.put("versionCount", versions.size());
    params.put("packageName", defaultPackageName + ".providers");
    params.put("defaultPackage", defaultPackageName);
    params.put("databaseName", databaseName);
    writeFile(
        defaultPackageName + ".providers",
        "NucleiDbHelper",
        getTemplate(cfg, "java", "dbhelper"),
        params);
  }