/** * 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"); }
/** * 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"); }
/** * 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"); } }
/** * 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"); }
/** * 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"); }