public static QualifiedName procedureName( String... namespaceAndName ) { return functionSignature( namespaceAndName ).build().name(); } }
private FunctionResult( UserFunctionSignature signature ) { this.name = signature.name().toString(); this.signature = signature.toString(); this.description = signature.description().orElse( "" ); } }
@Description( "List all user functions in the DBMS." ) @Procedure( name = "dbms.functions", mode = DBMS ) public Stream<FunctionResult> listFunctions() { securityContext.assertCredentialsNotExpired(); return graph.getDependencyResolver().resolveDependency( Procedures.class ).getAllFunctions().stream() .sorted( Comparator.comparing( a -> a.name().toString() ) ) .map( FunctionResult::new ); }
@Override public UserAggregator create( Context ctx ) throws ProcedureException { throw new ProcedureException( Status.Procedure.ProcedureRegistrationFailed, signature().description().orElse( "Failed to load " + signature().name() ) ); } }
@Override public AnyValue apply( Context ctx, AnyValue[] input ) throws ProcedureException { throw new ProcedureException( Status.Procedure.ProcedureRegistrationFailed, signature().description().orElse( "Failed to load " + signature().name() ) ); } }
SubFunction( TemporalFunction<T> base, String name, List<FieldSignature> input, String description ) { this.function = base; this.signature = new UserFunctionSignature( new QualifiedName( new String[] {base.signature.name().name()}, name ), input, base.signature.outputType(), null, ALLOWED, description, true ); }
@Test public void shouldSignalNonExistingFunction() { // When assertThat(procs.function( signature.name() ), nullValue()); }
@Override public Object result() throws ProcedureException { try { return typeChecker.typeCheck( resultMethod.invoke(aggregator) ); } catch ( Throwable throwable ) { if ( throwable instanceof Status.HasStatus ) { throw new ProcedureException( ((Status.HasStatus) throwable).status(), throwable, throwable.getMessage() ); } else { Throwable cause = ExceptionUtils.getRootCause( throwable ); throw new ProcedureException( Status.Procedure.ProcedureCallFailed, throwable, "Failed to invoke function `%s`: %s", signature.name(), "Caused by: " + (cause != null ? cause : throwable) ); } } }
@Test public void shouldAllowOverridingProcedureName() throws Throwable { // When CallableUserFunction proc = compile( FunctionWithOverriddenName.class ).get( 0 ); // Then assertEquals("org.mystuff.thisisActuallyTheName", proc.signature().name().toString() ); }
@Test public void shouldAllowOverridingProcedureName() throws Throwable { // When CallableUserAggregationFunction method = compile( FunctionWithOverriddenName.class ).get( 0 ); // Then assertEquals("org.mystuff.thisisActuallyTheName", method.signature().name().toString() ); }
@Test public void shouldGetRegisteredFunction() throws Throwable { // When procs.register( function ); // Then assertThat( procs.function( signature.name() ).signature(), equalTo( signature ) ); }
/** * Register a new function. * * @param function the function. */ public void register( CallableUserFunction function, boolean overrideCurrentImplementation ) throws ProcedureException { UserFunctionSignature signature = function.signature(); QualifiedName name = signature.name(); CallableUserFunction oldImplementation = functions.get( name ); if ( oldImplementation == null ) { functions.put( name, function, signature.caseInsensitive() ); } else { if ( overrideCurrentImplementation ) { functions.put( name, function, signature.caseInsensitive() ); } else { throw new ProcedureException( Status.Procedure.ProcedureRegistrationFailed, "Unable to register function, because the name `%s` is already in use.", name ); } } }
/** * Register a new function. * * @param function the function. */ public void register( CallableUserAggregationFunction function, boolean overrideCurrentImplementation ) throws ProcedureException { UserFunctionSignature signature = function.signature(); QualifiedName name = signature.name(); CallableUserFunction oldImplementation = functions.get( name ); if ( oldImplementation == null ) { aggregationFunctions.put( name, function, signature.caseInsensitive() ); } else { if ( overrideCurrentImplementation ) { aggregationFunctions.put( name, function, signature.caseInsensitive() ); } else { throw new ProcedureException( Status.Procedure.ProcedureRegistrationFailed, "Unable to register aggregation function, because the name `%s` is already in use.", name ); } } }
@Test public void shouldCallRegisteredFunction() throws Throwable { // Given procs.register( function ); // When Object result = procs.callFunction( new BasicContext(), signature.name(), new AnyValue[] {numberValue( 1337 )} ); // Then assertThat( result , equalTo( Values.of(1337) ) ); }
@Test public void shouldNotAllowCallingNonExistingFunction() throws Throwable { // Expect exception.expect( ProcedureException.class ); exception.expectMessage( "There is no function with the name `org.myproc` registered for this " + "database instance. Please ensure you've spelled the " + "function name correctly and that the function is properly deployed." ); // When procs.callFunction( new BasicContext(), signature.name(), new AnyValue[] {numberValue( 1337 )} ); }
@Test public void shouldMakeContextAvailable() throws Throwable { // Given Key<String> someKey = key("someKey", String.class); procs.register( new CallableUserFunction.BasicUserFunction( signature ) { @Override public AnyValue apply( Context ctx, AnyValue[] input ) throws ProcedureException { return Values.stringValue( ctx.get( someKey ) ); } } ); BasicContext ctx = new BasicContext(); ctx.put( someKey, "hello, world" ); // When Object result = procs.callFunction( ctx, signature.name(), new AnyValue[0] ); // Then assertThat( result, equalTo(Values.of("hello, world") ) ); }
for ( CallableUserFunction func : funcs ) String name = func.signature().name().name(); func.apply( new BasicContext(), new AnyValue[0] ); switch ( name )
for ( CallableUserAggregationFunction func : funcs ) String name = func.signature().name().name(); func.create( new BasicContext()); switch ( name )