Skip to content

Commit

Permalink
Merge pull request #43 from DSL-UMD/distributed
Browse files Browse the repository at this point in the history
add stored procedure test
  • Loading branch information
gangliao authored Apr 1, 2019
2 parents 50e5f76 + 98575a8 commit a5e5aa1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions tests/VoltDBStoredProcedureTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.voltdb.*;

// https://docs.voltdb.com/tutorial/Part5.php
public class VoltDBStoredProcedureTest extends VoltProcedure {

public final SQLStmt sql1 =
new SQLStmt(
"WITH RECURSIVE cte AS ("
+ " SELECT id, parent FROM inodes d WHERE id = ?"
+ " UNION ALL"
+ " SELECT d.id, d.parent FROM cte"
+ " JOIN inodes d ON cte.id = d.parent"
+ " )"
+ " SELECT id FROM cte;");
public final SQLStmt sql2 = new SQLStmt("DELETE FROM inodes WHERE id = ?;");

public long run(long id) throws VoltAbortException {
voltQueueSQL(sql1, id);
VoltTable[] results = voltExecuteSQL();

if (results[0].getRowCount() < 1) {
return -1;
}
for (int i = 0; i < results[0].getRowCount(); ++i) {
voltQueueSQL(sql2, results[0].fetchRow(i).getLong(0));
}
voltExecuteSQL();

return 1;
}
}
18 changes: 16 additions & 2 deletions VoltDBTest.java → tests/VoltDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Types;
Expand Down Expand Up @@ -42,8 +43,8 @@ public static void main(String [] args) {
VoltDBTest db = VoltDBTest.getInstance();
Statement st = db.getConnection().createStatement();

// Select from table
ResultSet rs = st.executeQuery("SELECT * FROM dir");
// Select inodes from table
ResultSet rs = st.executeQuery("SELECT * FROM inodes;");
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
Expand All @@ -52,8 +53,21 @@ public static void main(String [] args) {
}
System.out.println("");
}

// declare the stored procedure in our schema
st.executeUpdate("DROP PROCEDURE VoltDBStoredProcedureTest;");
st.executeUpdate("CREATE PROCEDURE FROM CLASS VoltDBStoredProcedureTest;");
// call a stored procedure
CallableStatement proc = db.getConnection().prepareCall("{call VoltDBStoredProcedureTest(?)}");
proc.setLong(1, 1);
rs = proc.executeQuery();
while (rs.next()) {
System.out.printf("%s\n", rs.getString(1));
}

rs.close();
st.close();
proc.close();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down

0 comments on commit a5e5aa1

Please sign in to comment.