Debian Codegraph @

src/test/org/h2/samples/FunctionMultiReturn.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.samples;
7
8 import java.sql.Connection;
9 import java.sql.DriverManager;
10 import java.sql.PreparedStatement;
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.sql.Statement;
14 import java.sql.Types;
15
16 import org.h2.tools.SimpleResultSet;
17
18 /**
19 * User defined functions can return a result set,
20 * and can therefore be used like a table.
21 * This sample application uses such a function to convert
22 * polar to cartesian coordinates.
23 */
24 public class FunctionMultiReturn {
25
26 /**
27 * This method is called when executing this sample application from the
28 * command line.
29 *
30 * @param args the command line parameters
31 * @throws Exception on failure
32 */
33 public static void main(String... args) throws Exception {
34 Class.forName("org.h2.Driver");
35 Connection conn = DriverManager.getConnection(
36 "jdbc:h2:mem:", "sa", "");
37 Statement stat = conn.createStatement();
38 stat.execute("CREATE ALIAS P2C FOR 'org.h2.samples.FunctionMultiReturn.polar2Cartesian'");
39 PreparedStatement prep = conn.prepareStatement(
40 "SELECT X, Y FROM P2C(?, ?)");
41 prep.setDouble(1, 5.0);
42 prep.setDouble(2, 0.5);
43 ResultSet rs = prep.executeQuery();
44 while (rs.next()) {
45 double x = rs.getDouble(1);
46 double y = rs.getDouble(2);
47 System.out.println("result: (x=" + x + ", y="+y+")");
48 }
49
50 stat.execute("CREATE TABLE TEST(ID IDENTITY, R DOUBLE, A DOUBLE)");
51 stat.execute("INSERT INTO TEST(R, A) VALUES(5.0, 0.5), (10.0, 0.6)");
52 stat.execute("CREATE ALIAS P2C_SET FOR 'org.h2.samples.FunctionMultiReturn.polar2CartesianSet'");
53 rs = conn.createStatement().executeQuery(
54 "SELECT * FROM P2C_SET('SELECT * FROM TEST')");
55 while (rs.next()) {
56 double r = rs.getDouble("R");
57 double a = rs.getDouble("A");
58 double x = rs.getDouble("X");
59 double y = rs.getDouble("Y");
60 System.out.println("(r="+r+" a="+a+") :" +
61 " (x=" + x + ", y="+y+")");
62 }
63
64 stat.execute("CREATE ALIAS P2C_A FOR 'org.h2.samples.FunctionMultiReturn.polar2CartesianArray'");
65 rs = conn.createStatement().executeQuery(
66 "SELECT R, A, P2C_A(R, A) FROM TEST");
67 while (rs.next()) {
68 double r = rs.getDouble(1);
69 double a = rs.getDouble(2);
70 Double [] xy = rs.getObject(3, Double[].class);
71 double x = xy[0];
72 double y = xy[1];
73 System.out.println("(r=" + r + " a=" + a + ") :" +
74 " (x=" + x + ", y=" + y + ")");
75 }
76
77 rs = stat.executeQuery(
78 "SELECT R, A, ARRAY_GET(E, 1), ARRAY_GET(E, 2) " +
79 "FROM (SELECT R, A, P2C_A(R, A) E FROM TEST)");
80 while (rs.next()) {
81 double r = rs.getDouble(1);
82 double a = rs.getDouble(2);
83 double x = rs.getDouble(3);
84 double y = rs.getDouble(4);
85 System.out.println("(r="+r+" a="+a+") :" +
86 " (x=" + x + ", y="+y+")");
87 }
88 rs.close();
89
90 prep.close();
91 conn.close();
92 }
93
94 /**
95 * Convert polar coordinates to cartesian coordinates. The function may be
96 * called twice, once to retrieve the result columns (with null parameters),
97 * and the second time to return the data.
98 *
99 * @param r the distance from the point 0/0
100 * @param alpha the angle
101 * @return a result set with two columns: x and y
102 */
103 public static ResultSet polar2Cartesian(Double r, Double alpha) {
104 SimpleResultSet rs = new SimpleResultSet();
105 rs.addColumn("X", Types.DOUBLE, 0, 0);
106 rs.addColumn("Y", Types.DOUBLE, 0, 0);
107 if (r != null && alpha != null) {
108 double x = r * Math.cos(alpha);
109 double y = r * Math.sin(alpha);
110 rs.addRow(x, y);
111 }
112 return rs;
113 }
114
115 /**
116 * Convert polar coordinates to cartesian coordinates. The function may be
117 * called twice, once to retrieve the result columns (with null parameters),
118 * and the second time to return the data.
119 *
120 * @param r the distance from the point 0/0
121 * @param alpha the angle
122 * @return an array two values: x and y
123 */
124 public static Double[] polar2CartesianArray(Double r, Double alpha) {
125 double x = r * Math.cos(alpha);
126 double y = r * Math.sin(alpha);
127 return new Double[]{x, y};
128 }
129
130 /**
131 * Convert a set of polar coordinates to cartesian coordinates. The function
132 * may be called twice, once to retrieve the result columns (with null
133 * parameters), and the second time to return the data.
134 *
135 * @param conn the connection
136 * @param query the query
137 * @return a result set with the coordinates
138 * @throws SQLException on failure
139 */
140 public static ResultSet polar2CartesianSet(Connection conn, String query)
141 throws SQLException {
142 SimpleResultSet result = new SimpleResultSet();
143 result.addColumn("R", Types.DOUBLE, 0, 0);
144 result.addColumn("A", Types.DOUBLE, 0, 0);
145 result.addColumn("X", Types.DOUBLE, 0, 0);
146 result.addColumn("Y", Types.DOUBLE, 0, 0);
147 if (query != null) {
148 ResultSet rs = conn.createStatement().executeQuery(query);
149 while (rs.next()) {
150 double r = rs.getDouble("R");
151 double alpha = rs.getDouble("A");
152 double x = r * Math.cos(alpha);
153 double y = r * Math.sin(alpha);
154 result.addRow(r, alpha, x, y);
155 }
156 }
157 return result;
158 }
159
160 }