/** * Create a new alias based on source code. * * @param schema the schema * @param id the id * @param name the name * @param source the source code * @param force create the object even if the class or method does not exist * @param bufferResultSetToLocalTemp whether the result should be buffered * @return the database object */ public static FunctionAlias newInstanceFromSource( Schema schema, int id, String name, String source, boolean force, boolean bufferResultSetToLocalTemp) { FunctionAlias alias = new FunctionAlias(schema, id, name); alias.source = source; alias.bufferResultSetToLocalTemp = bufferResultSetToLocalTemp; alias.init(force); return alias; }
@Override public synchronized void removeChildrenAndResources(Session session) { database.removeMeta(session, getId()); className = null; methodName = null; javaMethods = null; invalidate(); }
getMethodSignature(m).equals(methodName)) { JavaMethod javaMethod = new JavaMethod(m, i); for (JavaMethod old : list) {
JavaMethod[] methods; try { methods = alias.getJavaMethods(); } catch (DbException e) { methods = new JavaMethod[0]; alias.getSchema().getName(), identifier(alias.getName()), alias.getJavaClassName(), alias.getJavaMethodName(), replaceNullWithEmpty(alias.getComment()), "" + alias.getId(), alias.getSource() JavaMethod[] methods; try { methods = alias.getJavaMethods(); } catch (DbException e) { methods = new JavaMethod[0]; alias.getSchema().getName(), identifier(alias.getName()),
JavaMethod[] methods; try { methods = alias.getJavaMethods(); } catch (DbException e) { methods = new JavaMethod[0]; alias.getSchema().getName(), identifier(alias.getName()), alias.getJavaClassName(), alias.getSource() JavaMethod[] methods; try { methods = alias.getJavaMethods(); } catch (DbException e) { methods = new JavaMethod[0]; alias.getSchema().getName(), identifier(alias.getName()), alias.getJavaClassName(), alias.getSchema().getName(), identifier(alias.getName()),
@Override public String getSQL() { StatementBuilder buff = new StatementBuilder(); // TODO always append the schema once FUNCTIONS_IN_SCHEMA is enabled if (functionAlias.getDatabase().getSettings().functionsInSchema || !functionAlias.getSchema().getName().equals(Constants.SCHEMA_MAIN)) { buff.append( Parser.quoteIdentifier(functionAlias.getSchema().getName())) .append('.'); } buff.append(Parser.quoteIdentifier(functionAlias.getName())).append('('); for (Expression e : args) { buff.appendExceptFirst(", "); buff.append(e.getSQL()); } return buff.append(')').toString(); }
/** * Find the Java method that matches the arguments. * * @param args the argument list * @return the Java method * @throws DbException if no matching method could be found */ public JavaMethod findJavaMethod(Expression[] args) { load(); int parameterCount = args.length; for (JavaMethod m : javaMethods) { int count = m.getParameterCount(); if (count == parameterCount || (m.isVarArgs() && count <= parameterCount + 1)) { return m; } } throw DbException.get(ErrorCode.METHOD_NOT_FOUND_1, getName() + " (" + className + ", parameter count: " + parameterCount + ")"); }
for (int i = 0; i < aliases.size(); i++) { FunctionAlias alias = (FunctionAlias) aliases.get(i); int returnsResult = alias.getDataType() == Value.NULL ? DatabaseMetaData.procedureNoResult : DatabaseMetaData.procedureReturnsResult; add(rows, new String[] { identifier(alias.getName()), // ALIAS_NAME alias.getJavaClassName(), // JAVA_CLASS alias.getJavaMethodName(), // JAVA_METHOD ""+DataType.convertTypeToSQLType(alias.getDataType()), // DATA_TYPE ""+ alias.getColumnClasses().length, // COLUMN_COUNT INT ""+ returnsResult, // RETURNS_RESULT SMALLINT for (int i = 0; i < aliases.size(); i++) { FunctionAlias alias = (FunctionAlias) aliases.get(i); Class[] columns = alias.getColumnClasses(); for (int j = 0; j < columns.length; j++) { Class clazz = columns[j]; identifier(alias.getName()), // ALIAS_NAME alias.getJavaClassName(), // JAVA_CLASS alias.getJavaMethodName(), // JAVA_METHOD "" + j, // POS INT "P" + (j+1), // COLUMN_NAME
private void init(boolean force) { try { // at least try to compile the class, otherwise the data type is not // initialized if it could be load(); } catch (DbException e) { if (!force) { throw e; } } }
@Override public String getName() { return functionAlias.getName(); }
@Override public String getDropSQL() { return "DROP ALIAS IF EXISTS " + getSQL(); }
public FunctionAlias(Database db, int id, String name, String javaClassMethod, boolean force) throws SQLException { initDbObjectBase(db, id, name, Trace.FUNCTION); int paren = javaClassMethod.indexOf('('); int lastDot = javaClassMethod.lastIndexOf('.', paren < 0 ? javaClassMethod.length() : paren); if (lastDot < 0) { throw Message.getSQLException(ErrorCode.SYNTAX_ERROR_1, javaClassMethod); } className = javaClassMethod.substring(0, lastDot); methodName = javaClassMethod.substring(lastDot + 1); try { // at least try to load the class, otherwise the data type is not // initialized if it could be load(); } catch (SQLException e) { if (!force) { throw e; } } }
GridSqlFunction res = new GridSqlFunction(alias.getSchema().getName(), f.getName());
public JavaFunction(FunctionAlias functionAlias, Expression[] args) { this.functionAlias = functionAlias; this.javaMethod = functionAlias.findJavaMethod(args); this.args = args; }
public int update() throws SQLException { session.commit(true); session.getUser().checkAdmin(); Database db = session.getDatabase(); if (db.findFunctionAlias(aliasName) != null) { if (!ifNotExists) { throw Message.getSQLException(ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, aliasName); } } else { int id = getObjectId(false, true); FunctionAlias functionAlias = new FunctionAlias(db, id, aliasName, javaClassMethod, force); db.addDatabaseObject(session, functionAlias); } return 0; }
JavaMethod[] methods; try { methods = alias.getJavaMethods(); } catch (DbException e) { methods = new JavaMethod[0]; alias.getSchema().getName(), identifier(alias.getName()), alias.getJavaClassName(), alias.getSource() JavaMethod[] methods; try { methods = alias.getJavaMethods(); } catch (DbException e) { methods = new JavaMethod[0]; alias.getSchema().getName(), identifier(alias.getName()), alias.getJavaClassName(), alias.getSchema().getName(), identifier(alias.getName()),
@Override public String getSQL() { StatementBuilder buff = new StatementBuilder(); // TODO always append the schema once FUNCTIONS_IN_SCHEMA is enabled if (functionAlias.getDatabase().getSettings().functionsInSchema || !functionAlias.getSchema().getName().equals(Constants.SCHEMA_MAIN)) { buff.append( Parser.quoteIdentifier(functionAlias.getSchema().getName())) .append('.'); } buff.append(Parser.quoteIdentifier(functionAlias.getName())).append('('); for (Expression e : args) { buff.appendExceptFirst(", "); buff.append(e.getSQL()); } return buff.append(')').toString(); }
/** * Find the Java method that matches the arguments. * * @param args the argument list * @return the Java method * @throws DbException if no matching method could be found */ public JavaMethod findJavaMethod(Expression[] args) { load(); int parameterCount = args.length; for (JavaMethod m : javaMethods) { int count = m.getParameterCount(); if (count == parameterCount || (m.isVarArgs() && count <= parameterCount + 1)) { return m; } } throw DbException.get(ErrorCode.METHOD_NOT_FOUND_1, getName() + " (" + className + ", parameter count: " + parameterCount + ")"); }