Home » SQL & PL/SQL » SQL & PL/SQL » Reflection (merged 3)
Reflection (merged 3) [message #390988] Tue, 10 March 2009 11:03 Go to next message
Yurkech
Messages: 9
Registered: March 2009
Junior Member
Hi,

I need to call method of some object by his name (method name) inside other method of this object, but the name of method is calculated dynamicly. For example:

CREATE OR REPLACE
TYPE BODY test_obj AS
constructor test_obj as
return self as result
begin
...
return;
end;

member procedure run(methodName varchar2) is
begin
self.methodName;
-- how can I do this call correctly
end;
END;

Usage:

declare
t test_obj;
begin
t := test_obj();
t.run('some_method');
end;

I tried:
execute immediate 'self.' || methodName;
execute immediate 'begin self.' || methodName || ' end;';

but Oracle cann't resolve 'self' (reference to this object)

Can anybody answer does Oracle support this kind of reflection, and if yes, how can I do it?

Thanks a lot!
Re: Reflection [message #390993 is a reply to message #390988] Tue, 10 March 2009 11:13 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
do not cross/multi-post
Re: Reflection [message #390994 is a reply to message #390988] Tue, 10 March 2009 11:13 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
do not cross/multi-post
Re: Reflection [message #390995 is a reply to message #390988] Tue, 10 March 2009 11:13 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
do not cross/multi-post
Re: Reflection [message #390996 is a reply to message #390995] Tue, 10 March 2009 11:15 Go to previous messageGo to next message
Michel Cadot
Messages: 64137
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
These are "reflections" Smile

Regards
Michel
Re: Reflection (merged 3) [message #390997 is a reply to message #390988] Tue, 10 March 2009 11:15 Go to previous messageGo to next message
Michel Cadot
Messages: 64137
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code (See SQL Formatter), use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version (4 decimals).

Regards
Michel
Re: Reflection (merged 3) [message #390999 is a reply to message #390988] Tue, 10 March 2009 11:17 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
EXECUTE IMMEDIATE is designed to invoke DDL by PL/SQL & NOT invoke other PL/SQL procedure.
Re: Reflection (merged 3) [message #391006 is a reply to message #390988] Tue, 10 March 2009 11:38 Go to previous messageGo to next message
Yurkech
Messages: 9
Registered: March 2009
Junior Member
Oracle 10.2

Maybe there is some other way (not through execute immediate)?
using pl/sql features?

Thanks
Re: Reflection (merged 3) [message #391007 is a reply to message #390988] Tue, 10 March 2009 11:41 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
You need to help us by following the Posting Guidelines as stated below.
http://www.orafaq.com/forum/t/88153/0/
So we can help you & please be consistent & correct in your postings.

Post DDL for tables.
Post DML for test data.

Post expected/desired results.

use CASE construct
Re: Reflection (merged 3) [message #391011 is a reply to message #390988] Tue, 10 March 2009 12:04 Go to previous messageGo to next message
Yurkech
Messages: 9
Registered: March 2009
Junior Member
Sorry, but I just want to understand how can I call some method by his name in pl/sql...

It's not relate to any tables or data...

There is object ref and method name in varchar.
Expected result: call this method for this instance.

Probable use case I mentioned below.

I'm just not sure that pl/sql allow to do this operation at all

Thanks
Re: Reflection (merged 3) [message #391013 is a reply to message #390988] Tue, 10 March 2009 12:10 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
>Sorry, but I just want to understand how can I call some method by his name in pl/sql...
Do the same as how you invoke your "run" procedure.

http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/toc.htm

Re: Reflection (merged 3) [message #391014 is a reply to message #390988] Tue, 10 March 2009 12:18 Go to previous messageGo to next message
Yurkech
Messages: 9
Registered: March 2009
Junior Member
but this is a static way, I need to do it programmatic in runtime Sad
Re: Reflection (merged 3) [message #391015 is a reply to message #390988] Tue, 10 March 2009 12:20 Go to previous messageGo to next message
BlackSwan
Messages: 25046
Registered: January 2009
Location: SoCal
Senior Member
> I need to do it programmatic in runtime
use CASE "at runtime" to invoke desired procedure!
Re: Reflection (merged 3) [message #391125 is a reply to message #390988] Wed, 11 March 2009 03:06 Go to previous messageGo to next message
Yurkech
Messages: 9
Registered: March 2009
Junior Member
CREATE OR REPLACE 
TYPE BODY test_obj AS
  constructor test_obj as
    return self as result
  begin
    ...
    return;
  end;

  member procedure run_do_methods() is
    m_procedures procedure_name_tab; 
  begin
	m_procedures := self.get_procedures('DO%');
    for v_loop_index in 1 .. m_startup_procedures.count
    loop
      execute immediate 'self.' || m_startup_procedures(v_loop_index);  --it doesn't work
	  -- from this example you can understand that I can't use CASE, cause of test_obj doesn't know about method name - they implemented only in CHILD types
	  -- and may be a lots of child classes with absolutly different 'do' methods
	  --as I understood 'execute immediate' is only for SQL, so does PL/SQL is enable to do something like this
    end loop;      
  end;
  
  member function get_procedures(mask varchar)
        return procedure_name_tab is
  begin
	--using select from system.all_procedures I can get all procedure of some type matching mask 
	--return array of names
  end;
END;

-- testA_obj extend test_obj
CREATE OR REPLACE 
TYPE BODY testA_obj under test_obj AS
  constructor testA_obj as
    return self as result
  begin
    ...
    return;
  end;
  
  member procedure doA is
  begin
	-- doA
	dbms_output.put_line('A')
  end;
	
  member procedure doB is
  begin
	-- doB
	dbms_output.put_line('B')
  end;
  
  ...
  
  member procedure some_method is
  begin
	-- do something
	dbms_output.put_line('Something')
  end;
  
END;  

Usage:

declare
  t testA_obj;
begin
  t := testA_obj();
  t.run_do_methods();
end;


Expected:
A
B

[Mod-Edit: Frank added [code]-tags to improve readability. Please do so yourself in the future]

[Updated on: Wed, 11 March 2009 03:21] by Moderator

Report message to a moderator

Re: Reflection (merged 3) [message #391256 is a reply to message #390988] Wed, 11 March 2009 09:55 Go to previous messageGo to next message
Yurkech
Messages: 9
Registered: March 2009
Junior Member
so? does anybody have ideas how to solve this issue? Smile
Re: Reflection (merged 3) [message #427943 is a reply to message #391256] Mon, 26 October 2009 09:10 Go to previous message
guga_java
Messages: 1
Registered: October 2009
Junior Member
you solved this problem?
Previous Topic: PL/SQL STORED PROCEDURE output problem
Next Topic: Removing CSV File Using UTL_FILE
Goto Forum:
  


Current Time: Thu Dec 08 06:24:29 CST 2016

Total time taken to generate the page: 0.14101 seconds