コード例 #1
0
  private void writeRemoveWith(Table table, Field field) throws IOException {
    ArrayList<String> params = new ArrayList<String>();
    params.add(SqlUtil.getJavaTypeFor(field.type));
    params.add(field.name);

    // remove with UNIQUE
    writer.emitEmptyLine();
    writer.beginMethod(
        "void",
        "remove" + Util.capitalize(table.name) + "With" + Util.capitalize(field.name),
        EnumSet.of(Modifier.PUBLIC),
        params.toArray(new String[params.size()]));
    writer.emitStatement(
        "ContentProviderOperation.Builder operationBuilder = ContentProviderOperation.newDelete("
            + mModel.getContentProviderName()
            + "."
            + SqlUtil.URI(table)
            + ")");
    writer.emitStatement(
        "operationBuilder.withSelection("
            + mModel.getDbClassName()
            + "."
            + SqlUtil.ROW_COLUMN(table, field)
            + " + \"=?\", new String[]{String.valueOf("
            + field.name
            + ")})");
    insertAddOpBlock();
    writer.endMethod();
  }
コード例 #2
0
  private void emitTableCRUD(Table table) throws Exception {
    // Default array params for all rows
    ArrayList<String> params = new ArrayList<String>();
    for (Field row : table.fields) {

      params.add(SqlUtil.getJavaTypeFor(row.type));
      params.add(row.name);
    }

    ArrayList<String> paramsWithUnique = new ArrayList<String>();
    paramsWithUnique.add(SqlUtil.getJavaTypeFor(table.getPrimaryKey().type));
    paramsWithUnique.add(table.getPrimaryKey().name);

    ArrayList<String> updateParams = new ArrayList<String>();
    // TODO : add unique id
    updateParams.addAll(params);

    Iterator<Constraint> constraintiter;

    writer.emitEmptyLine();
    writer.emitJavadoc(
        table.name + " OPERATIONS\nall operations require this client to first run start");
    writer.emitEmptyLine();

    // Add through ContentProviderOperation
    writer.beginMethod(
        "void",
        "add" + Util.capitalize(table.name),
        EnumSet.of(Modifier.PUBLIC),
        params.toArray(new String[params.size()]));
    writer.emitStatement(
        "ContentProviderOperation.Builder operationBuilder = ContentProviderOperation.newInsert("
            + mModel.getContentProviderName()
            + "."
            + SqlUtil.URI(table)
            + ")");
    for (Field row : table.fields) {
      writer.emitStatement(
          "operationBuilder.withValue("
              + mModel.getDbClassName()
              + "."
              + SqlUtil.ROW_COLUMN(table, row)
              + ","
              + row.name
              + ")");
    }
    insertAddOpBlock();
    writer.endMethod();

    // Removes
    writeRemoveWith(table, table.getPrimaryKey());

    constraintiter = table.constraints.iterator();

    while (constraintiter.hasNext()) {
      Constraint constraint = constraintiter.next();
      if (constraint.type.equals(Constraint.Type.UNIQUE)) {
        final String[] fields = SqlUtil.getFieldsFromConstraint(constraint);
        for (int i = 0; i < fields.length; i++) {
          writeRemoveWith(table, table.getFieldByName(fields[i]));
        }
      }
    }

    // Remove All results
    writer.emitEmptyLine();
    writer.beginMethod(
        "void", "removeAll" + Util.capitalize(table.name), EnumSet.of(Modifier.PUBLIC));
    writer.emitStatement(
        "ContentProviderOperation.Builder operationBuilder = ContentProviderOperation.newDelete("
            + mModel.getContentProviderName()
            + "."
            + SqlUtil.URI(table)
            + ")");
    insertAddOpBlock();
    writer.endMethod();

    // Update through ContentProviderOperation
    writer.emitEmptyLine();
    writer.beginMethod(
        "void",
        "update" + Util.capitalize(table.name),
        EnumSet.of(Modifier.PUBLIC),
        updateParams.toArray(new String[updateParams.size()]));
    writer.emitStatement(
        "ContentProviderOperation.Builder operationBuilder = ContentProviderOperation.newUpdate("
            + mModel.getContentProviderName()
            + "."
            + SqlUtil.URI(table)
            + ")");
    for (Field row : table.fields) {
      writer.emitStatement(
          "operationBuilder.withValue("
              + mModel.getDbClassName()
              + "."
              + SqlUtil.ROW_COLUMN(table, row)
              + ","
              + row.name
              + ")");
    }
    insertAddOpBlock();
    writer.endMethod();
  }