diff --git a/core/src/processing/core/Table.java b/core/src/processing/core/Table.java index 08d400a47..4abbe7ca3 100644 --- a/core/src/processing/core/Table.java +++ b/core/src/processing/core/Table.java @@ -25,6 +25,7 @@ package processing.core; import java.io.*; import java.lang.reflect.Array; +import java.sql.*; import java.util.*; import processing.core.PApplet; @@ -147,6 +148,62 @@ public class Table implements Iterable { } + public Table(ResultSet rs) { + this(); + try { + ResultSetMetaData rsmd = rs.getMetaData(); + + int columnCount = rsmd.getColumnCount(); + setColumnCount(columnCount); + + for (int col = 0; col < columnCount; col++) { + setColumnTitle(col, rsmd.getColumnName(col + 1)); + + int type = rsmd.getColumnType(col + 1); + switch (type) { // TODO these aren't tested. nor are they complete. + case Types.INTEGER: + case Types.TINYINT: + case Types.SMALLINT: + setColumnType(col, INT); + break; + case Types.BIGINT: + setColumnType(col, LONG); + break; + case Types.FLOAT: + setColumnType(col, FLOAT); + break; + case Types.DECIMAL: + case Types.DOUBLE: + case Types.REAL: + setColumnType(col, DOUBLE); + break; + } + } + + while (rs.next()) { + for (int col = 0; col < columnCount; col++) { + switch (columnTypes[col]) { + case STRING: setString(rowCount, col, rs.getString(col+1)); break; + case INT: setInt(rowCount, col, rs.getInt(col+1)); break; + case LONG: setLong(rowCount, col, rs.getLong(col+1)); break; + case FLOAT: setFloat(rowCount, col, rs.getFloat(col+1)); break; + case DOUBLE: setDouble(rowCount, col, rs.getDouble(col+1)); break; + default: throw new IllegalArgumentException("column type " + columnTypes[col] + " not supported."); + } + } +// String[] row = new String[columnCount]; +// for (int col = 0; col < columnCount; col++) { +// row[col] = rs.getString(col + 1); +// } +// addRow(row); + } + + } catch (SQLException s) { + throw new RuntimeException(s); + } + } + + /** * Guess whether this file is tab separated or comma separated by checking * whether there are more tabs or commas in the first 100 characters. @@ -1177,6 +1234,10 @@ public class Table implements Iterable { doubleData[row] = missingDouble; } break; + case CATEGORICAL: + int[] indexData = (int[]) columns[col]; + indexData[row] = columnCategories[col].index(piece); + break; default: throw new IllegalArgumentException("That's not a valid column type."); } @@ -1391,6 +1452,121 @@ public class Table implements Iterable { } + static public Iterator createIterator(final ResultSet rs) { + return new Iterator() { + boolean already; + + public boolean hasNext() { + already = true; + try { + return rs.next(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + + public Row next() { + if (!already) { + try { + rs.next(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } else { + already = false; + } + + return new Row() { + public double getDouble(int column) { + try { + return rs.getDouble(column); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public double getDouble(String columnName) { + try { + return rs.getDouble(columnName); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public float getFloat(int column) { + try { + return rs.getFloat(column); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public float getFloat(String columnName) { + try { + return rs.getFloat(columnName); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public int getInt(int column) { + try { + return rs.getInt(column); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public int getInt(String columnName) { + try { + return rs.getInt(columnName); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public long getLong(int column) { + try { + return rs.getLong(column); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public long getLong(String columnName) { + try { + return rs.getLong(columnName); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public String getString(int column) { + try { + return rs.getString(column); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public String getString(String columnName) { + try { + return rs.getString(columnName); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + }; + } + + public void remove() { + throw new IllegalArgumentException("remove() not supported"); + } + }; + } + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . diff --git a/core/todo.txt b/core/todo.txt index 5c72f2386..9a6e596bb 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -2,6 +2,10 @@ X remove textMode(SCREEN) X added expand(long) and expand(double) because of Table +_ change how beginRecord() works.. passing around PApplet vs PGraphics is gross +_ have to pass PApplet just to make the rendering work to both renderers +_ should instead be a renderer that splits things out + _ boolean sketchFullscreen() ? _ for more options, just integrate the fs library? _ http://www.superduper.org/processing/fullscreen_api/