Skip to content
Snippets Groups Projects
Commit 654b98ab authored by Julien Lopez's avatar Julien Lopez
Browse files

- Work on drivers:

	- add basic PostgreSQL driver;
	- ConnectionData is now the same for all drivers
	- remove useless QIRDriver
	- minor changes in OracleDriver
- Cleanup in IQIRVisitor
- Minor changes
parent b9881455
Branches
No related tags found
No related merge requests found
......@@ -2,4 +2,7 @@
.classpath
.externalToolBuilders
.project
.checkstyle
.checkstyle_checks.xml
.settings/
qir.jar
......@@ -4,6 +4,7 @@
<target name="CreateJar" description="Create Jar file">
<jar jarfile="qir.jar">
<fileset dir="bin"/>
<fileset dir="src"/>
</jar>
</target>
......
package qir.driver;
public class ConnectionData {
public final String sid;
public final String serverName;
public final int portNumber;
public final String userName;
public final String password;
public ConnectionData(String sid, String serverName, int portNumber, String userName, String password) {
this.sid = sid;
this.serverName = serverName;
this.portNumber = portNumber;
this.userName = userName;
this.password = password;
}
}
package qir.driver;
import java.sql.Connection;
import java.sql.SQLException;
import qir.ast.*;
import qir.ast.data.*;
public abstract class DBDriver {
protected Connection conn;
public abstract void openConnection(ConnectionData connData) throws SQLException;
public void closeConnection() throws SQLException {
if (conn == null)
return;
conn.close();
conn = null;
}
public abstract QIRList run(QIRNode root);
}
\ No newline at end of file
......@@ -10,15 +10,16 @@ import oracle.jdbc.pool.*;
import qir.ast.*;
import qir.ast.data.*;
import qir.ast.value.*;
import qir.driver.ConnectionData;
/**
* This driver is the intermediate between QIR and the Oracle database. This class is temporary. The
* database should ultimately take a QIR tree directly.
* This driver is the intermediate between QIR and the Oracle database.
*/
public class OracleDriver extends DBDriver {
private final Connection conn;
public OracleDriver(ConnectionData data) throws SQLException {
@Override
public void openConnection(ConnectionData data) throws SQLException {
if (conn != null)
closeConnection();
String connString = "jdbc:oracle:thin:@" + data.serverName + ":" + data.portNumber + ":" + data.sid;
OracleDataSource ods = new OracleDataSource();
ods.setUser(data.userName);
......@@ -26,6 +27,7 @@ public class OracleDriver extends DBDriver {
ods.setURL(connString);
ods.setLoginTimeout(1000);
conn = ods.getConnection();
ods.close();
}
@Override
......@@ -45,22 +47,6 @@ public class OracleDriver extends DBDriver {
}
}
public static class ConnectionData {
private final String sid;
private final String serverName;
private final int portNumber;
private final String userName;
private final String password;
public ConnectionData(String sid, String serverName, int portNumber, String userName, String password) {
this.sid = sid;
this.serverName = serverName;
this.portNumber = portNumber;
this.userName = userName;
this.password = password;
}
}
private void enableWalnut() throws SQLException {
// warm up database
String query = "select * from emp";
......
package qir.driver;
import java.sql.*;
import java.util.*;
import com.oracle.truffle.api.source.*;
import qir.ast.*;
import qir.ast.data.*;
import qir.ast.value.*;
import qir.driver.ConnectionData;
import qir.util.QIRPrintVisitor;
/**
* This driver is the intermediate between QIR and the Oracle database. This class is temporary. The
* database should ultimately take a QIR tree directly.
*/
public class PostgreSQLDriver extends DBDriver {
@Override
public void openConnection(ConnectionData connData) throws SQLException {
if (conn != null)
closeConnection();
String url = "jdbc:postgresql://" + connData.serverName + ":" + connData.portNumber + "/" + connData.sid;
Properties props = new Properties();
props.setProperty("user", connData.userName);
props.setProperty("password", connData.password);
conn = DriverManager.getConnection(url, props);
}
@Override
public QIRList run(QIRNode root) {
QIROracleVisitor visitor = new QIROracleVisitor();
QIRPrintVisitor debugVisitor = new QIRPrintVisitor();
String query = root.accept(visitor);
System.out.println("Query: " + query);
System.out.println("Query qir: " + root.accept(debugVisitor));
try {
QIRList result = executeQuery(query);
System.out.println("Results qir: " + result.accept(debugVisitor));
return result;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
private QIRList executeQuery(String query) throws SQLException {
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
int columnCount = rs.getMetaData().getColumnCount();
QIRList result = QIRLnil.getInstance();
Stack<QIRTuple> tmp = new Stack<>();
QIRNode data;
while (rs.next()) {
QIRTuple newTuple = QIRTnil.getInstance();
for (int i = columnCount; i >= 1; i--) {
switch (rs.getMetaData().getColumnType(i)) {
case Types.NUMERIC: {
// TODO: Handle overflow
data = new QIRNumber(SourceSection.createUnavailable("QIRNode", "QIRNumber"), rs.getLong(i));
break;
}
default: {
data = new QIRString(SourceSection.createUnavailable("QIRNode", "QIRString"), rs.getString(i));
break;
}
}
newTuple = new QIRTcons(SourceSection.createUnavailable("QIRNode", "QIRTcons"), rs.getMetaData().getColumnLabel(i), data, newTuple);
}
tmp.push(newTuple);
}
while (!tmp.isEmpty())
result = new QIRLcons(null, tmp.pop(), result);
return result;
}
}
}
\ No newline at end of file
package qir.driver;
import qir.ast.*;
import qir.ast.data.*;
import qir.util.*;
/**
* WORK IN PROGRESS: TODO: Add interface to stream results; add interface to get database info
* (table exists?, table schema, ...).
*/
public class QIRDriver {
private final DBDriver defaultDriver;
public QIRDriver(DBDriver defaultDriver) {
this.defaultDriver = defaultDriver;
}
public QIRList run(QIRNode root) {
QIRPrintVisitor debugVisitor = new QIRPrintVisitor();
System.out.println(root.accept(debugVisitor));
try {
return defaultDriver.run(root);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
\ No newline at end of file
......@@ -25,11 +25,14 @@ public interface IQIRVisitor<T> {
public abstract T visit(QIROrder qirOrder);
public default T visit(QIROperator qirOperator) {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " error: unsupported operator.");
throw new UnsupportedOperationException("Error: unsupported operator " + qirOperator.getClass().getSimpleName() + ".");
}
/**
* @param qirVariable A variable that should have been handled elsewhere.
*/
public default T visit(QIRVariable qirVariable) {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " error: we should not visit a QIRVariable.");
throw new UnsupportedOperationException("Error: we should not visit a QIRVariable.");
}
public abstract T visit(QIRLambda qirLambda);
......@@ -86,6 +89,9 @@ public interface IQIRVisitor<T> {
public abstract T visit(QIRTruffleNode qirTruffleNode);
/**
* @param qirNode An unsupported node.
*/
public default T visit(QIRNode qirNode) {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " error: unsupported node.");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment