Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Generic Read Method in a class

Former Member
0 Likes
895

Hi Experts,

I want to create a class which contains a generic READ method. This method will return the records based on the query string and table passed to it as import parameters. The method will first search the records from the BUFFER(static attributes) and if not found in buffer then it will hit the respective Database.

Can any one please help me on this?

Thanks

Shilpa

3 REPLIES 3
Read only

MarcinPciak
Active Contributor
0 Likes
607

Hi,

You can use such logic


"definition
method read_generic importing i_tabname type string  "table name to read
                                                           i_query type string   "your query here
                                        exporting  r_line type ref to data.  "generic work area which will be used to return line
                                       
"implementation
method read_generic.   
   field-sybmols <wa> type any.
   data: query_tab type table of string.

   create data r_line type (i_tabname).  
   assign r_line->* to <wa>. "now <wa> has structure of db table 
 
   "some conditons here to read buffer
   ...
   "if not met than read table directly

    "build dynamic conditions
    append i_query to query_tab.

    select * from (itabname) into <wa> where (query_tab).
    endselect.

endmethod.

Now you call your method like


data: lr_line type ref to data.
field-symbols <line> type any.

call method read_generic exporitng i_tabname = 'DB_TABLE_NAME' 
                                                                  i_query = 'FIELD1 = ''X'' and FIELD2 = ''Y'' '
                                                                  r_line = lr_line.   "<- here you have the result

"last thing is to get value of returend record 
assign lr_line->* to <line>.   "<line> holds the result

Regards

Marcin

Read only

0 Likes
607

Hi Marcin,

I found this thread but how do I get results as internale table?

Say I want to read table 'KNA1' with country = 'US' and my export parameter would be the dynamic table with data in it.

If I change table name to KNVV with company '0002' I would get the dynamic table with structure like knvv.

Your example just give me the data of the last record of the table.

DATA: LR_LINE TYPE REF TO DATA.

FIELD-SYMBOLS <LINE> TYPE ANY.

DATA: LV_STRING TYPE STRING.

FIELD-SYMBOLS: <ITAB> TYPE ANY TABLE.

CLEAR LV_STRING.

CONCATENATE 'LAND1 =' ' ''AL''' INTO LV_STRING SEPARATED BY SPACE.

CALL METHOD ZCL_GENERIC_READ=>READ_CUSTOMER

EXPORTING

I_TABNAME = 'KNA1'

I_QUERY = LV_STRING

IMPORTING

E_DATALINE = LR_LINE.

ASSIGN LR_LINE->* TO <LINE>.I am new to RTTS, so I want to learn this concept.

Thanks much,

Read only

alex_campbell
Contributor
0 Likes
607

One thing to keep in mind with buffering dynamic queries, is that if the results of a first query are a subset of the results of a subsequent query, your subsequent query won't be accurate (it will be missing some records). For example:

people:

John Smith

Adam Smith

John Doe

Query 1: Search for John Smith

Check buffer for John Smith. Buffer is empty, so no records found.

SELECT * FROM people WHERE firstname = 'John' and lastname = 'Smith'

Returns 1 record for John Smith, which is saved to the buffer

Query 2: Search for John

Check buffer for John. 1 record (John Smith) is found

Returns 1 record for John Smith (this is an ERROR, because John Doe should be returned as well!)

If you want to handle this perfectly correctly, your buffer table should have the key field as the query string, and the internal table of results as the value field.