Пример #1
0
  /**
   * Generate the toString() method
   *
   * @param columnTypes - mapping from column names to sql types
   * @param colNames - ordered list of column names for table.
   * @param sb - StringBuilder to append code to
   */
  private void generateToString(
      Map<String, Integer> columnTypes, String[] colNames, StringBuilder sb) {

    sb.append("  public String toString() {\n");
    sb.append("    StringBuilder sb = new StringBuilder();\n");

    boolean first = true;
    for (String col : colNames) {
      int sqlType = columnTypes.get(col);
      String javaType = connManager.toJavaType(sqlType);
      if (null == javaType) {
        LOG.error("No Java type for SQL type " + sqlType);
        continue;
      }

      if (!first) {
        // TODO(aaron): Support arbitrary record delimiters
        sb.append("    sb.append(\",\");\n");
      }

      first = false;

      String stringExpr = stringifierForType(javaType, col);
      if (null == stringExpr) {
        LOG.error("No toString method for Java type " + javaType);
        continue;
      }

      sb.append("    sb.append(" + stringExpr + ");\n");
    }

    sb.append("    return sb.toString();\n");
    sb.append("  }\n");
  }
Пример #2
0
  /**
   * Generate the readFields() method used by the database
   *
   * @param columnTypes - mapping from column names to sql types
   * @param colNames - ordered list of column names for table.
   * @param sb - StringBuilder to append code to
   */
  private void generateDbRead(
      Map<String, Integer> columnTypes, String[] colNames, StringBuilder sb) {

    sb.append("  public void readFields(ResultSet __dbResults) throws SQLException {\n");

    int fieldNum = 0;

    for (String col : colNames) {
      fieldNum++;

      int sqlType = columnTypes.get(col);
      String javaType = connManager.toJavaType(sqlType);
      if (null == javaType) {
        LOG.error("No Java type for SQL type " + sqlType);
        continue;
      }

      String getterMethod = dbGetterForType(javaType);
      if (null == getterMethod) {
        LOG.error("No db getter method for Java type " + javaType);
        continue;
      }

      sb.append(
          "    this."
              + col
              + " = JdbcWritableBridge."
              + getterMethod
              + "("
              + fieldNum
              + ", __dbResults);\n");
    }

    sb.append("  }\n");
  }
Пример #3
0
 @After
 public void tearDown() {
   try {
     manager.close();
   } catch (SQLException sqlE) {
     LOG.error("Got SQLException: " + sqlE.toString());
     fail("Got SQLException: " + sqlE.toString());
   }
 }
Пример #4
0
  /**
   * Generate a member field and getter method for each column
   *
   * @param columnTypes - mapping from column names to sql types
   * @param colNames - ordered list of column names for table.
   * @param sb - StringBuilder to append code to
   */
  private void generateFields(
      Map<String, Integer> columnTypes, String[] colNames, StringBuilder sb) {

    for (String col : colNames) {
      int sqlType = columnTypes.get(col);
      String javaType = connManager.toJavaType(sqlType);
      if (null == javaType) {
        LOG.error("Cannot resolve SQL type " + sqlType);
        continue;
      }

      sb.append("  private " + javaType + " " + col + ";\n");
      sb.append("  public " + javaType + " get_" + col + "() {\n");
      sb.append("    return " + col + ";\n");
      sb.append("  }\n");
    }
  }
Пример #5
0
  /**
   * Generate the write() method used by the database
   *
   * @param columnTypes - mapping from column names to sql types
   * @param colNames - ordered list of column names for table.
   * @param sb - StringBuilder to append code to
   */
  private void generateDbWrite(
      Map<String, Integer> columnTypes, String[] colNames, StringBuilder sb) {

    sb.append("  public void write(PreparedStatement __dbStmt) throws SQLException {\n");

    int fieldNum = 0;

    for (String col : colNames) {
      fieldNum++;

      int sqlType = columnTypes.get(col);
      String javaType = connManager.toJavaType(sqlType);
      if (null == javaType) {
        LOG.error("No Java type for SQL type " + sqlType);
        continue;
      }

      String setterMethod = dbSetterForType(javaType);
      if (null == setterMethod) {
        LOG.error("No db setter method for Java type " + javaType);
        continue;
      }

      sb.append(
          "    JdbcWritableBridge."
              + setterMethod
              + "("
              + col
              + ", "
              + fieldNum
              + ", "
              + sqlType
              + ", __dbStmt);\n");
    }

    sb.append("  }\n");
  }
Пример #6
0
  /**
   * Generate the write() method used by the Hadoop RPC system
   *
   * @param columnTypes - mapping from column names to sql types
   * @param colNames - ordered list of column names for table.
   * @param sb - StringBuilder to append code to
   */
  private void generateHadoopWrite(
      Map<String, Integer> columnTypes, String[] colNames, StringBuilder sb) {

    sb.append("  public void write(DataOutput __dataOut) throws IOException {\n");

    for (String col : colNames) {
      int sqlType = columnTypes.get(col);
      String javaType = connManager.toJavaType(sqlType);
      if (null == javaType) {
        LOG.error("No Java type for SQL type " + sqlType);
        continue;
      }

      String setterMethod = rpcSetterForMaybeNull(javaType, "__dataOut", col);
      if (null == setterMethod) {
        LOG.error("No RPC setter method for Java type " + javaType);
        continue;
      }

      sb.append(setterMethod);
    }

    sb.append("  }\n");
  }
Пример #7
0
  /** Generate the ORM code for the class. */
  public void generate() throws IOException {
    Map<String, Integer> columnTypes = connManager.getColumnTypes(tableName);

    String[] colNames = options.getColumns();
    if (null == colNames) {
      colNames = connManager.getColumnNames(tableName);
    }

    // Generate the Java code
    StringBuilder sb = generateClassForColumns(columnTypes, colNames);

    // Write this out to a file.
    String codeOutDir = options.getCodeOutputDir();

    // TODO(aaron): Allow package subdirectory (that goes in sourceFilename).
    String sourceFilename = tableName + ".java";
    String filename = codeOutDir + sourceFilename;

    LOG.debug("Writing source file: " + filename);
    LOG.debug("Table name: " + tableName);
    StringBuilder sbColTypes = new StringBuilder();
    for (String col : colNames) {
      Integer colType = columnTypes.get(col);
      sbColTypes.append(col + ":" + colType + ", ");
    }
    String colTypeStr = sbColTypes.toString();
    LOG.debug("Columns: " + colTypeStr);

    compileManager.addSourceFile(sourceFilename);

    // Create any missing parent directories.
    File file = new File(filename);
    String dirname = file.getParent();
    if (null != dirname) {
      boolean mkdirSuccess = new File(dirname).mkdirs();
      if (!mkdirSuccess) {
        LOG.debug("Could not create directory tree for " + dirname);
      }
    }

    OutputStream ostream = null;
    Writer writer = null;
    try {
      ostream = new FileOutputStream(filename);
      writer = new OutputStreamWriter(ostream);
      writer.append(sb.toString());
    } finally {
      if (null != writer) {
        try {
          writer.close();
        } catch (IOException ioe) {
          // ignored because we're closing.
        }
      }

      if (null != ostream) {
        try {
          ostream.close();
        } catch (IOException ioe) {
          // ignored because we're closing.
        }
      }
    }
  }