use of org.h2.expression.Function in project h2database by h2database.
the class Parser method readIdentifierWithSchema.
// TODO: why does this function allow defaultSchemaName=null - which resets
// the parser schemaName for everyone ?
private String readIdentifierWithSchema(String defaultSchemaName) {
if (currentTokenType != IDENTIFIER) {
throw DbException.getSyntaxError(sqlCommand, parseIndex, "identifier");
}
String s = currentToken;
read();
schemaName = defaultSchemaName;
if (readIf(".")) {
schemaName = s;
if (currentTokenType != IDENTIFIER) {
throw DbException.getSyntaxError(sqlCommand, parseIndex, "identifier");
}
s = currentToken;
read();
}
if (equalsToken(".", currentToken)) {
if (equalsToken(schemaName, database.getShortName())) {
read(".");
schemaName = s;
if (currentTokenType != IDENTIFIER) {
throw DbException.getSyntaxError(sqlCommand, parseIndex, "identifier");
}
s = currentToken;
read();
}
}
return s;
}
use of org.h2.expression.Function in project h2database by h2database.
the class CreateView method update.
@Override
public int update() {
session.commit(true);
session.getUser().checkAdmin();
Database db = session.getDatabase();
TableView view = null;
Table old = getSchema().findTableOrView(session, viewName);
if (old != null) {
if (ifNotExists) {
return 0;
}
if (!orReplace || TableType.VIEW != old.getTableType()) {
throw DbException.get(ErrorCode.VIEW_ALREADY_EXISTS_1, viewName);
}
view = (TableView) old;
}
int id = getObjectId();
String querySQL;
if (select == null) {
querySQL = selectSQL;
} else {
ArrayList<Parameter> params = select.getParameters();
if (params != null && !params.isEmpty()) {
throw DbException.getUnsupportedException("parameters in views");
}
querySQL = select.getPlanSQL();
}
Column[] columnTemplatesAsUnknowns = null;
Column[] columnTemplatesAsStrings = null;
if (columnNames != null) {
columnTemplatesAsUnknowns = new Column[columnNames.length];
columnTemplatesAsStrings = new Column[columnNames.length];
for (int i = 0; i < columnNames.length; ++i) {
// non table expressions are fine to use unknown column type
columnTemplatesAsUnknowns[i] = new Column(columnNames[i], Value.UNKNOWN);
// table expressions can't have unknown types - so we use string instead
columnTemplatesAsStrings[i] = new Column(columnNames[i], Value.STRING);
}
}
if (view == null) {
if (isTableExpression) {
view = TableView.createTableViewMaybeRecursive(getSchema(), id, viewName, querySQL, null, columnTemplatesAsStrings, session, false, /* literalsChecked */
isTableExpression, true, /* isPersistent */
db);
} else {
view = new TableView(getSchema(), id, viewName, querySQL, null, columnTemplatesAsUnknowns, session, false, /* allow recursive */
false, /* literalsChecked */
isTableExpression, true);
}
} else {
// TODO support isTableExpression in replace function...
view.replace(querySQL, columnTemplatesAsUnknowns, session, false, force, false);
view.setModified();
}
if (comment != null) {
view.setComment(comment);
}
if (old == null) {
db.addSchemaObject(session, view);
db.unlockMeta(session);
} else {
db.updateMeta(session, view);
}
return 0;
}
use of org.h2.expression.Function in project h2database by h2database.
the class TcpServer method stopServer.
/**
* Stop a running server. This method is called via reflection from the
* STOP_SERVER function.
*
* @param port the port where the server runs, or 0 for all running servers
* @param password the password (or null)
* @param shutdownMode the shutdown mode, SHUTDOWN_NORMAL or SHUTDOWN_FORCE.
*/
public static void stopServer(int port, String password, int shutdownMode) {
if (port == 0) {
for (int p : SERVERS.keySet().toArray(new Integer[0])) {
if (p != 0) {
stopServer(p, password, shutdownMode);
}
}
return;
}
TcpServer server = SERVERS.get(port);
if (server == null) {
return;
}
if (!server.managementPassword.equals(password)) {
return;
}
if (shutdownMode == SHUTDOWN_NORMAL) {
server.stopManagementDb();
server.stop = true;
try {
Socket s = NetUtils.createLoopbackSocket(port, false);
s.close();
} catch (Exception e) {
// try to connect - so that accept returns
}
} else if (shutdownMode == SHUTDOWN_FORCE) {
server.stop();
}
server.shutdown();
}
use of org.h2.expression.Function in project h2database by h2database.
the class TestSpatial method getRandomGeometryTable.
/**
* Generate a result set with random geometry data.
* Used as an ALIAS function.
*
* @param seed the random seed
* @param rowCount the number of rows
* @param minX the smallest x
* @param maxX the largest x
* @param minY the smallest y
* @param maxY the largest y
* @param maxLength the maximum length
* @return a result set
*/
public static ResultSet getRandomGeometryTable(final long seed, final long rowCount, final double minX, final double maxX, final double minY, final double maxY, final double maxLength) {
SimpleResultSet rs = new SimpleResultSet(new SimpleRowSource() {
private final Random random = new Random(seed);
private int currentRow;
@Override
public Object[] readRow() throws SQLException {
if (currentRow++ < rowCount) {
return new Object[] { getRandomGeometry(random, minX, maxX, minY, maxY, maxLength) };
}
return null;
}
@Override
public void close() {
// nothing to do
}
@Override
public void reset() throws SQLException {
random.setSeed(seed);
}
});
rs.addColumn("the_geom", Types.OTHER, "GEOMETRY", Integer.MAX_VALUE, 0);
return rs;
}
use of org.h2.expression.Function in project h2database by h2database.
the class Function method main.
/**
* This method is called when executing this sample application from the
* command line.
*
* @param args the command line parameters
*/
public static void main(String... args) throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
Statement stat = conn.createStatement();
// Using a custom Java function
stat.execute("CREATE ALIAS IS_PRIME " + "FOR \"org.h2.samples.Function.isPrime\" ");
ResultSet rs;
rs = stat.executeQuery("SELECT IS_PRIME(X), X " + "FROM SYSTEM_RANGE(1, 20) ORDER BY X");
while (rs.next()) {
boolean isPrime = rs.getBoolean(1);
if (isPrime) {
int x = rs.getInt(2);
System.out.println(x + " is prime");
}
}
// Calling the built-in 'table' function
stat.execute("CREATE TABLE TEST(ID INT) AS " + "SELECT X FROM SYSTEM_RANGE(1, 100)");
PreparedStatement prep;
prep = conn.prepareStatement("SELECT * FROM TABLE(X INT=?, O INT=?) J " + "INNER JOIN TEST T ON J.X=T.ID ORDER BY J.O");
prep.setObject(1, new Integer[] { 30, 20 });
prep.setObject(2, new Integer[] { 1, 2 });
rs = prep.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
prep.close();
rs.close();
// Using a custom function like table
stat.execute("CREATE ALIAS MATRIX " + "FOR \"org.h2.samples.Function.getMatrix\" ");
prep = conn.prepareStatement("SELECT * FROM MATRIX(?) " + "ORDER BY X, Y");
prep.setInt(1, 2);
rs = prep.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1) + "/" + rs.getInt(2));
}
prep.close();
// Creating functions with source code
// in this case the JDK classes must be in the classpath
// where the database is running
stat.execute("create alias make_point as $$ " + "java.awt.Point newPoint(int x, int y) { " + "return new java.awt.Point(x, y); } $$");
// parameters of type OTHER (or OBJECT or JAVA_OBJECT)
// are de-serialized to match the type
stat.execute("create alias get_x as $$ " + "int pointX(java.awt.geom.Point2D p) { " + "return (int) p.getX(); } $$");
rs = stat.executeQuery("call get_x(make_point(10, 20))");
while (rs.next()) {
System.out.println(rs.getString(1));
}
stat.close();
conn.close();
}
Aggregations