Skip to content

Commit 16ddec3

Browse files
authored
Merge pull request #20 from lwhite1/16_lw
16 lw
2 parents 569329d + c836457 commit 16ddec3

File tree

5 files changed

+449
-0
lines changed

5 files changed

+449
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ data/test.csv
1010
data/train.csv
1111
data/train_filter.csv
1212

13+
derby.log
14+
CoffeeDB/
15+
1316
testfolder/
1417

1518
*.iml

src/main/java/com/github/lwhite1/tablesaw/api/Table.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.lwhite1.tablesaw.api;
22

3+
import com.github.lwhite1.tablesaw.io.jdbc.SqlResultSetReader;
34
import com.github.lwhite1.tablesaw.table.Projection;
45
import com.github.lwhite1.tablesaw.table.Relation;
56
import com.github.lwhite1.tablesaw.table.Rows;
@@ -25,6 +26,8 @@
2526
import org.roaringbitmap.RoaringBitmap;
2627

2728
import java.io.IOException;
29+
import java.sql.ResultSet;
30+
import java.sql.SQLException;
2831
import java.util.*;
2932

3033
import static com.github.lwhite1.tablesaw.sorting.Sort.Order;
@@ -682,4 +685,11 @@ public static Table fromCSV(ColumnType[] types, String fileName) {
682685
}
683686
return t;
684687
}
688+
689+
/**
690+
* Returns a new Table with the given name, and containing the data in the given resultset
691+
*/
692+
public static Table create(ResultSet resultSet, String tableName) throws SQLException {
693+
return SqlResultSetReader.read(resultSet, tableName);
694+
}
685695
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.github.lwhite1.tablesaw.io.jdbc;
2+
3+
import com.github.lwhite1.tablesaw.api.ColumnType;
4+
import com.github.lwhite1.tablesaw.api.Table;
5+
import com.github.lwhite1.tablesaw.columns.Column;
6+
import com.github.lwhite1.tablesaw.io.TypeUtils;
7+
import com.google.common.base.Preconditions;
8+
import com.google.common.collect.ImmutableMap;
9+
10+
import java.sql.ResultSet;
11+
import java.sql.ResultSetMetaData;
12+
import java.sql.SQLException;
13+
import java.sql.Types;
14+
15+
/**
16+
* Creates a Table from the result of a SQL query, by passing the jdbc resultset to the constructor
17+
*/
18+
public class SqlResultSetReader {
19+
20+
// Maps from supported SQL types to their Tablesaw 'equivalents'
21+
private static final ImmutableMap<Integer, ColumnType> SQL_TYPE_TO_TABLESAW_TYPE =
22+
new ImmutableMap.Builder<Integer, ColumnType>()
23+
.put(Types.BINARY, ColumnType.BOOLEAN)
24+
.put(Types.BOOLEAN, ColumnType.BOOLEAN)
25+
26+
.put(Types.DATE, ColumnType.LOCAL_DATE)
27+
.put(Types.TIME, ColumnType.LOCAL_TIME)
28+
.put(Types.TIMESTAMP, ColumnType.LOCAL_DATE_TIME)
29+
30+
.put(Types.DECIMAL, ColumnType.FLOAT)
31+
.put(Types.DOUBLE, ColumnType.FLOAT)
32+
.put(Types.FLOAT, ColumnType.FLOAT)
33+
.put(Types.NUMERIC, ColumnType.FLOAT)
34+
.put(Types.REAL, ColumnType.FLOAT)
35+
36+
.put(Types.INTEGER, ColumnType.INTEGER)
37+
.put(Types.SMALLINT, ColumnType.SHORT_INT)
38+
.put(Types.TINYINT, ColumnType.SHORT_INT)
39+
.put(Types.BIGINT, ColumnType.LONG_INT)
40+
.put(Types.BIT, ColumnType.SHORT_INT)
41+
42+
.put(Types.CHAR, ColumnType.CATEGORY)
43+
.put(Types.LONGVARCHAR, ColumnType.CATEGORY)
44+
.put(Types.LONGNVARCHAR, ColumnType.CATEGORY)
45+
.put(Types.NCHAR, ColumnType.CATEGORY)
46+
.put(Types.NVARCHAR, ColumnType.CATEGORY)
47+
.put(Types.VARCHAR, ColumnType.CATEGORY)
48+
.build();
49+
50+
/**
51+
* Returns a new table with the given tableName, constructed from the given result set
52+
* @throws SQLException
53+
*/
54+
public static Table read(ResultSet resultSet, String tableName) throws SQLException {
55+
56+
ResultSetMetaData metaData = resultSet.getMetaData();
57+
Table table = new Table(tableName);
58+
59+
// Setup the columns and add to the table
60+
for (int i = 1; i <= metaData.getColumnCount(); i++) {
61+
String name = metaData.getColumnName(i);
62+
63+
ColumnType type = SQL_TYPE_TO_TABLESAW_TYPE.get(metaData.getColumnType(i));
64+
Preconditions.checkState(type != null,
65+
"No column type found for %s as specified for column %s", metaData.getColumnType(i), name);
66+
67+
Column newColumn = TypeUtils.newColumn(name, type);
68+
table.addColumn(newColumn);
69+
}
70+
71+
// Add the rows
72+
while (resultSet.next()) {
73+
for (int i = 1; i <= metaData.getColumnCount(); i++) {
74+
Column column = table.column(i -1); // subtract 1 because results sets originate at 1 not 0
75+
column.addCell(resultSet.getString(i));
76+
}
77+
}
78+
return table;
79+
}
80+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.github.lwhite1.tablesaw.io.jdbc;
2+
3+
import com.github.lwhite1.tablesaw.api.Table;
4+
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.ResultSet;
8+
import java.sql.Statement;
9+
10+
import static com.github.lwhite1.tablesaw.util.TestDb.*;
11+
12+
/**
13+
* Tests for creating Tables from JDBC result sets using SqlResutSetReader
14+
*/
15+
public class SqlResultSetReaderTest {
16+
17+
18+
public static void main(String[] args) throws Exception {
19+
20+
// Create a named constant for the URL.
21+
// NOTE: This value is specific for Java DB.
22+
final String DB_URL = "jdbc:derby:CoffeeDB;create=true";
23+
24+
// Create a connection to the database.
25+
Connection conn = DriverManager.getConnection(DB_URL);
26+
27+
// If the DB already exists, drop the tables.
28+
dropTables(conn);
29+
30+
// Build the Coffee table.
31+
buildCoffeeTable(conn);
32+
33+
// Build the Customer table.
34+
buildCustomerTable(conn);
35+
36+
// Build the UnpaidInvoice table.
37+
buildUnpaidOrderTable(conn);
38+
39+
try (Statement stmt = conn.createStatement()) {
40+
String sql;
41+
sql = "SELECT * FROM coffee";
42+
try (ResultSet rs = stmt.executeQuery(sql)) {
43+
Table coffee = SqlResultSetReader.read(rs, "Coffee");
44+
System.out.println(coffee.structure().print());
45+
System.out.println(coffee.print());
46+
}
47+
48+
sql = "SELECT * FROM Customer";
49+
try (ResultSet rs = stmt.executeQuery(sql)) {
50+
Table customer = SqlResultSetReader.read(rs, "Customer");
51+
System.out.println(customer.structure().print());
52+
System.out.println(customer.print());
53+
}
54+
55+
sql = "SELECT * FROM UnpaidOrder";
56+
try (ResultSet rs = stmt.executeQuery(sql)) {
57+
Table unpaidInvoice = SqlResultSetReader.read(rs, "Unpaid Invoice");
58+
System.out.println(unpaidInvoice.structure().print());
59+
System.out.println(unpaidInvoice.print());
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)