| 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.math.BigInteger; |
| 9 |
import java.sql.Connection; |
| 10 |
import java.sql.DriverManager; |
| 11 |
import java.sql.PreparedStatement; |
| 12 |
import java.sql.ResultSet; |
| 13 |
import java.sql.SQLException; |
| 14 |
import java.sql.Statement; |
| 15 |
import java.sql.Types; |
| 16 |
import org.h2.tools.SimpleResultSet; |
| 17 |
|
| 18 |
/** |
| 19 |
* This sample application shows how to define and use |
| 20 |
* custom (user defined) functions in this database. |
| 21 |
*/ |
| 22 |
public class Function { |
| 23 |
|
| 24 |
/** |
| 25 |
* This method is called when executing this sample application from the |
| 26 |
* command line. |
| 27 |
* |
| 28 |
* @param args the command line parameters |
| 29 |
* @throws Exception on failure |
| 30 |
*/ |
| 31 |
public static void main(String... args) throws Exception { |
| 32 |
Class.forName("org.h2.Driver"); |
| 33 |
Connection conn = DriverManager.getConnection( |
| 34 |
"jdbc:h2:mem:", "sa", ""); |
| 35 |
Statement stat = conn.createStatement(); |
| 36 |
|
| 37 |
// Using a custom Java function |
| 38 |
stat.execute("CREATE ALIAS IS_PRIME FOR 'org.h2.samples.Function.isPrime'"); |
| 39 |
ResultSet rs; |
| 40 |
rs = stat.executeQuery("SELECT IS_PRIME(X), X " + |
| 41 |
"FROM SYSTEM_RANGE(1, 20) ORDER BY X"); |
| 42 |
while (rs.next()) { |
| 43 |
boolean isPrime = rs.getBoolean(1); |
| 44 |
if (isPrime) { |
| 45 |
int x = rs.getInt(2); |
| 46 |
System.out.println(x + " is prime"); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Calling the built-in 'table' function |
| 51 |
stat.execute("CREATE TABLE TEST(ID INT) AS " + |
| 52 |
"SELECT X FROM SYSTEM_RANGE(1, 100)"); |
| 53 |
PreparedStatement prep; |
| 54 |
prep = conn.prepareStatement( |
| 55 |
"SELECT * FROM TABLE(X INT=?, O INT=?) J " + |
| 56 |
"INNER JOIN TEST T ON J.X=T.ID ORDER BY J.O"); |
| 57 |
prep.setObject(1, new Integer[] { 30, 20 }); |
| 58 |
prep.setObject(2, new Integer[] { 1, 2 }); |
| 59 |
rs = prep.executeQuery(); |
| 60 |
while (rs.next()) { |
| 61 |
System.out.println(rs.getInt(1)); |
| 62 |
} |
| 63 |
prep.close(); |
| 64 |
rs.close(); |
| 65 |
|
| 66 |
// Using a custom function like table |
| 67 |
stat.execute("CREATE ALIAS MATRIX FOR 'org.h2.samples.Function.getMatrix'"); |
| 68 |
prep = conn.prepareStatement("SELECT * FROM MATRIX(?) " + |
| 69 |
"ORDER BY X, Y"); |
| 70 |
prep.setInt(1, 2); |
| 71 |
rs = prep.executeQuery(); |
| 72 |
while (rs.next()) { |
| 73 |
System.out.println(rs.getInt(1) + "/" + rs.getInt(2)); |
| 74 |
} |
| 75 |
prep.close(); |
| 76 |
|
| 77 |
// Creating functions with source code |
| 78 |
// in this case the JDK classes must be in the classpath |
| 79 |
// where the database is running |
| 80 |
stat.execute("create alias make_point as $$ " + |
| 81 |
"java.awt.Point newPoint(int x, int y) { " + |
| 82 |
"return new java.awt.Point(x, y); } $$"); |
| 83 |
// parameters of type OTHER (or OBJECT or JAVA_OBJECT) |
| 84 |
// are de-serialized to match the type |
| 85 |
stat.execute("create alias get_x as $$ " + |
| 86 |
"int pointX(java.awt.geom.Point2D p) { " + |
| 87 |
"return (int) p.getX(); } $$"); |
| 88 |
rs = stat.executeQuery("call get_x(make_point(10, 20))"); |
| 89 |
while (rs.next()) { |
| 90 |
System.out.println(rs.getString(1)); |
| 91 |
} |
| 92 |
|
| 93 |
stat.close(); |
| 94 |
conn.close(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Check if a value is a prime number. |
| 99 |
* |
| 100 |
* @param value the value |
| 101 |
* @return true if it is a prime number |
| 102 |
*/ |
| 103 |
public static boolean isPrime(int value) { |
| 104 |
return BigInteger.valueOf(value).isProbablePrime(100); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Execute a query. |
| 109 |
* |
| 110 |
* @param conn the connection |
| 111 |
* @param sql the SQL statement |
| 112 |
* @return the result set |
| 113 |
* @throws SQLException on failure |
| 114 |
*/ |
| 115 |
public static ResultSet query(Connection conn, String sql) throws SQLException { |
| 116 |
return conn.createStatement().executeQuery(sql); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Creates a simple result set with one row. |
| 121 |
* |
| 122 |
* @return the result set |
| 123 |
*/ |
| 124 |
public static ResultSet simpleResultSet() { |
| 125 |
SimpleResultSet rs = new SimpleResultSet(); |
| 126 |
rs.addColumn("ID", Types.INTEGER, 10, 0); |
| 127 |
rs.addColumn("NAME", Types.VARCHAR, 255, 0); |
| 128 |
rs.addRow(0, "Hello"); |
| 129 |
return rs; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Creates a simple result set with two columns. |
| 134 |
* |
| 135 |
* @param conn the connection |
| 136 |
* @param size the number of x and y values |
| 137 |
* @return the result set with two columns |
| 138 |
* @throws SQLException on failure |
| 139 |
*/ |
| 140 |
public static ResultSet getMatrix(Connection conn, Integer size) |
| 141 |
throws SQLException { |
| 142 |
SimpleResultSet rs = new SimpleResultSet(); |
| 143 |
rs.addColumn("X", Types.INTEGER, 10, 0); |
| 144 |
rs.addColumn("Y", Types.INTEGER, 10, 0); |
| 145 |
String url = conn.getMetaData().getURL(); |
| 146 |
if (url.equals("jdbc:columnlist:connection")) { |
| 147 |
return rs; |
| 148 |
} |
| 149 |
for (int s = size, x = 0; x < s; x++) { |
| 150 |
for (int y = 0; y < s; y++) { |
| 151 |
rs.addRow(x, y); |
| 152 |
} |
| 153 |
} |
| 154 |
return rs; |
| 155 |
} |
| 156 |
|
| 157 |
} |