1 5 package org.h2.samples; 6 7 import java.sql.Connection ; 8 import java.sql.DriverManager ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.sql.Statement ; 13 import java.sql.Types ; 14 15 import org.h2.tools.SimpleResultSet; 16 17 public class FunctionMultiReturn { 18 19 public static void main(String [] args) throws Exception { 20 Class.forName("org.h2.Driver"); 21 Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", ""); 22 Statement stat = conn.createStatement(); 23 stat.execute("CREATE ALIAS P2C FOR \"org.h2.samples.FunctionMultiReturn.polar2Cartesian\" "); 24 PreparedStatement prep = conn.prepareStatement("SELECT X, Y FROM P2C(?, ?)"); 25 prep.setDouble(1, 5.0); 26 prep.setDouble(2, 0.5); 27 ResultSet rs = prep.executeQuery(); 28 while(rs.next()) { 29 double x = rs.getDouble(1); 30 double y = rs.getDouble(2); 31 System.out.println("result: (x=" + x + ", y="+y+")"); 32 } 33 34 stat.execute("CREATE TABLE TEST(ID IDENTITY, R DOUBLE, A DOUBLE)"); 35 stat.execute("INSERT INTO TEST(R, A) VALUES(5.0, 0.5), (10.0, 0.6)"); 36 stat.execute("CREATE ALIAS P2C_SET FOR \"org.h2.samples.FunctionMultiReturn.polar2CartesianSet\" "); 37 rs = conn.createStatement().executeQuery("SELECT * FROM P2C_SET('SELECT * FROM TEST')"); 38 while(rs.next()) { 39 double r = rs.getDouble("R"); 40 double a = rs.getDouble("A"); 41 double x = rs.getDouble("X"); 42 double y = rs.getDouble("Y"); 43 System.out.println("(r="+r+" a="+a+") : (x=" + x + ", y="+y+")"); 44 } 45 46 stat.execute("CREATE ALIAS P2C_A FOR \"org.h2.samples.FunctionMultiReturn.polar2CartesianArray\" "); 47 rs = conn.createStatement().executeQuery("SELECT R, A, P2C_A(R, A) FROM TEST"); 48 while(rs.next()) { 49 double r = rs.getDouble(1); 50 double a = rs.getDouble(2); 51 Object o = rs.getObject(3); 52 Object [] xy = (Object []) o; 53 double x = ((Double )xy[0]).doubleValue(); 54 double y = ((Double )xy[1]).doubleValue(); 55 System.out.println("(r="+r+" a="+a+") : (x=" + x + ", y="+y+")"); 56 } 57 58 rs = conn.createStatement().executeQuery("SELECT R, A, ARRAY_GET(E, 1), ARRAY_GET(E, 2) FROM (SELECT R, A, P2C_A(R, A) E FROM TEST)"); 59 while(rs.next()) { 60 double r = rs.getDouble(1); 61 double a = rs.getDouble(2); 62 double x = rs.getDouble(3); 63 double y = rs.getDouble(4); 64 System.out.println("(r="+r+" a="+a+") : (x=" + x + ", y="+y+")"); 65 } 66 67 conn.close(); 68 } 69 70 74 public static ResultSet polar2Cartesian(Double r, Double alpha) throws SQLException { 75 SimpleResultSet rs = new SimpleResultSet(); 76 rs.addColumn("X", Types.DOUBLE, 0, 0); 77 rs.addColumn("Y", Types.DOUBLE, 0, 0); 78 if(r != null && alpha != null) { 79 double x = r.doubleValue() * Math.cos(alpha.doubleValue()); 80 double y = r.doubleValue() * Math.sin(alpha.doubleValue()); 81 rs.addRow(new Object [] { new Double (x), new Double (y) }); 82 } 83 return rs; 84 } 85 86 90 public static Object [] polar2CartesianArray(Double r, Double alpha) throws SQLException { 91 double x = r.doubleValue() * Math.cos(alpha.doubleValue()); 92 double y = r.doubleValue() * Math.sin(alpha.doubleValue()); 93 return new Object []{new Double (x), new Double (y)}; 94 } 95 96 public static ResultSet polar2CartesianSet(Connection conn, String query) throws SQLException { 97 SimpleResultSet result = new SimpleResultSet(); 98 result.addColumn("R", Types.DOUBLE, 0, 0); 99 result.addColumn("A", Types.DOUBLE, 0, 0); 100 result.addColumn("X", Types.DOUBLE, 0, 0); 101 result.addColumn("Y", Types.DOUBLE, 0, 0); 102 if(query != null) { 103 ResultSet rs = conn.createStatement().executeQuery(query); 104 while(rs.next()) { 105 double r = rs.getDouble("R"); 106 double alpha = rs.getDouble("A"); 107 double x = r * Math.cos(alpha); 108 double y = r * Math.sin(alpha); 109 result.addRow(new Object [] { new Double (r), new Double (alpha), new Double (x), new Double (y) }); 110 } 111 } 112 return result; 113 } 114 115 } 116 | Popular Tags |