/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/ext/JdbcTable.java#1 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package saffron.ext; import openjava.mop.OJClass; import openjava.ptree.*; import saffron.ImplementableTable; import saffron.Schema; import saffron.opt.Cluster; import saffron.opt.Implementor; import saffron.rel.Rel; import saffron.rel.TableAccess; /** * A <code>JdbcTable</code> implements {@link Table} by connecting to a JDBC * database. * * @author jhyde * @since 10 November, 2001 * @version $Id: //guest/julian_hyde/saffron/src/main/saffron/ext/JdbcTable.java#1 $ **/ public class JdbcTable implements ImplementableTable { Schema schema; String name; OJClass rowType; public JdbcTable(Schema schema, String name, OJClass rowType) { this.schema = schema; this.name = name; this.rowType = rowType; } public JdbcTable(Schema schema, String name, Class rowType) { this(schema, name, OJClass.forClass(rowType)); } // implement Table public Schema getSchema() { return schema; } // implement Table public OJClass getRowType() { return rowType; } public String getName() { return name; } public double getRowCount() { return 100; } // implement Table public Rel toRel(Cluster cluster, Expression connectionExp) { return new TableAccess(cluster, this, connectionExp); } // implement ImplementableTable public void implement(Rel rel, Implementor implementor) { // Generate // // java.sql.Statement statement = null; // try { // statement = connection.getConnection().createStatement(); // java.sql.ResultSet resultSet = statement.executeQuery( // "select * from table"); // while (resultSet.next()) { // Emp emp = new Emp(resultSet); // <<child>> // } // } catch (java.sql.SQLException e) { // throw new saffron.runtime.SaffronError(e); // } finally { // if (stmt != null) { // try { // stmt.close(); // } catch {} // } // } StatementList stmtList = implementor.getStatementList(); Variable varStmt = implementor.newVariable(), varRs = implementor.newVariable(), varRow = implementor.newVariable(), varEx = implementor.newVariable(); StatementList whileBody = new StatementList( // Emp emp = new Emp(resultSet); // <<body>> new VariableDeclaration( TypeName.forOJClass(rowType), varRow.toString(), new AllocationExpression( rowType, new ExpressionList( varRs)))); implementor.bind(rel, varRow); implementor.generateParentBody(rel, whileBody); stmtList.add( // java.sql.Statement stmt = null; new VariableDeclaration( new TypeName("java.sql.Statement"), varStmt.toString(), Literal.constantNull())); String queryString = "select * from " + getName(); stmtList.add( new TryStatement( // try { new StatementList( // statement = // connection.getConnection().createStatement(); new ExpressionStatement( new AssignmentExpression( varStmt, AssignmentExpression.EQUALS, new MethodCall( new MethodCall( ((TableAccess) rel).getConnectionExp(), "getConnection", null), "createStatement", null))), // java.sql.ResultSet resultSet = // statement.executeQuery( // "select * from table"); new VariableDeclaration( new TypeName("java.sql.ResultSet"), varRs.toString(), new MethodCall( varStmt, "executeQuery", new ExpressionList( Literal.makeLiteral(queryString)))), // while (resultSet.next()) { new WhileStatement( new MethodCall( varRs, "next", null), // Emp emp = new Emp(resultSet); // <<body>> whileBody)), new CatchList( // catch (java.sql.SQLException e) { // throw new saffron.runtime.SaffronError(e); // } new CatchBlock( new Parameter( new TypeName("java.sql.SQLException"), varEx.toString()), new StatementList( new ThrowStatement( new AllocationExpression( new TypeName( "saffron.runtime.SaffronError"), new ExpressionList( varEx)))))), // finally { // if (stmt != null) { // try { // stmt.close(); // } catch (java.sql.SQLException e) {} // } // } new StatementList( new IfStatement( new BinaryExpression( varStmt, BinaryExpression.NOTEQUAL, Literal.constantNull()), new StatementList( new TryStatement( new StatementList( new ExpressionStatement( new MethodCall( varStmt, "close", null))), new CatchList( new CatchBlock( new Parameter( new TypeName( "java.sql.SQLException"), varEx.toString()), new StatementList())))))))); } } // End JdbcTable.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 1801 | Julian Hyde |
saffron: add ObjectSchema; rules can now be matched more than once; started to implement correlations in queries in from list. |
||
#2 | 1474 | Julian Hyde |
saffron: Aggregations are working. Renamed 'aggregator' to 'aggregation'. |
||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |