Last active
December 11, 2015 06:19
-
-
Save julianhyde/4558355 to your computer and use it in GitHub Desktop.
Some code I added to mondrian.rolap.SqlStatement to record every SQL statement executed by Mondrian, and its results, as a JSON document.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // In the SqlStatement class | |
| private static FileWriter fw; | |
| private static PrintWriter pw; | |
| private static Set<String> statements = new HashSet<String>(); | |
| static { | |
| try { | |
| fw = new FileWriter(new File("/tmp/x.sql")); | |
| pw = new PrintWriter(fw); | |
| } catch (IOException e) { | |
| // ignore | |
| } | |
| } | |
| // In execute() method | |
| if (statements.add(sql)) { | |
| if (statements.size() == 1) { | |
| pw.println("{"); | |
| pw.println("queries: ["); | |
| } | |
| pw.println(" {"); | |
| pw.println(" \"sql\": " + Util.quoteJavaString(sql) + ","); | |
| pw.println(" \"columns\": ["); | |
| statement = jdbcConnection.createStatement(); | |
| ResultSet rset = statement.executeQuery(sql); | |
| final ResultSetMetaData metaData = rset.getMetaData(); | |
| final int columnCount = metaData.getColumnCount(); | |
| List<String> types = new ArrayList<String>(); | |
| for (int i = 0; i < columnCount; i++) { | |
| String type = javaType(metaData.getColumnType(i + 1)); | |
| types.add(type); | |
| pw.println(" {\"name\": " + Util.quoteJavaString(metaData.getColumnName(i + 1)) + ", \"type\": \"" + type + "\"},"); | |
| } | |
| pw.println(" ],"); | |
| pw.println(" \"rows\": ["); | |
| while (rset.next()) { | |
| pw.print(" ["); | |
| for (int i = 0; i < columnCount; i++) { | |
| if (i > 0) { | |
| pw.print(", "); | |
| } | |
| final String string = rset.getString(i + 1); | |
| if (types.get(i).equals("String")) { | |
| pw.print(Util.quoteJavaString(string)); | |
| } else { | |
| pw.print(string); | |
| } | |
| } | |
| pw.println("],"); | |
| } | |
| pw.println(" ]"); | |
| rset.close(); | |
| statement.close(); | |
| pw.println(" },"); | |
| pw.flush(); | |
| } | |
| // New method | |
| private String javaType(int columnType) throws SQLException { | |
| switch (columnType) { | |
| case Types.INTEGER: | |
| return "int"; | |
| case Types.SMALLINT: | |
| return "short"; | |
| case Types.VARCHAR: | |
| return "String"; | |
| case Types.DECIMAL: | |
| return "BigDecimal"; | |
| case Types.BIT: | |
| return "boolean"; | |
| case Types.BIGINT: | |
| return "long"; | |
| default: | |
| throw new AssertionError("unknown type" + columnType); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment