uf_row_exists() to replace Find()


Link to this posting

Postby Ursego » 04 Apr 2013, 09:33

The function uf_row_exists() reports if the DW has a row which satisfies the passed search expression. This function should be used for DW row existence check instead of the built-in DW's Find() function. Advantages:

1. Decreases the number of code lines in the calling script because the variable ll_found_row must not be declared and checked if it's more than 0. So, the fragment

Code: Select all
long ll_found_row

ll_found_row = dw_test.Find(ls_search_expr, 1, dw_test.RowCount())
if ll_found_row > 0 then...

shrinks to one line:

Code: Select all
if dw_test.uf_row_exists(ls_search_expr) then...

2. If the search expression is incorrect, the standard Find() function displays the message "Expression is not valid" which only makes us angry - there is no clue where exactly the error has occurred, so we have no idea where to look for the problem in our huge application which has hundreds and hundreds of scripts :twisted: ). The function uf_row_exists() explains what exactly happened, so problem spot will be localized quickly and easily. It displays a much more useful message which provides the following information:

The expression, sent to Find().
That allows us to quickly find the script where it is built. For example, if the expression is emp_id=12345OR emp_role=3, then we can search for the string uf_row_exists("emp_id= in PowerBuilder, find the fragment where the expression is built dynamically, and figure out why there is no space before the OR.

The DataObject of the searched DW.
The function also suggests to check if it has all the fields, mentioned in the expression. You might forget to add the new field to the DataObject, so you simply go and add it - with no need to investigate WHERE exactly the error occurred.

So, uf_row_exists() covers two the most likely reasons why Find() fails: incorrectly built expression, and non-existing field in the expression.

The function is created in a DW ancestor class and uses the exceptions mechanism described here - change it if you want to display the messages in another way. Go ahead:

Code: Select all
/**********************************************************************************************************************
Dscr:         Reports if the DW has a row which satisfies the passed search expression.
            More details: http://code.intfast.ca/viewtopic.php?f=4&t=83
***********************************************************************************************************************
Arg:         as_search_expr - the logical expression to search by
***********************************************************************************************************************
Ret:         boolean
***********************************************************************************************************************
Developer:   Michael Zuskin -  http://linkedin.com/in/zuskin | http://code.intfast.ca/
**********************************************************************************************************************/
long   ll_row_count
long   ll_row

ll_row_count = this.RowCount()
if ll_row_count = 0 then return false

as_search_expr = Trim(as_search_expr)

choose case true
case IsNull(as_search_expr), as_search_expr = ''
   f_throw(PopulateError(1, "Arg as_search_expr is empty.")) // f_throw(): http://code.intfast.ca/viewtopic.php?f=2&t=1
case this.DataObject = ''
   f_throw(PopulateError(2, "DW has no DataObject."))
end choose

ll_row = this.Find(as_search_expr, 1, ll_row_count)
choose case ll_row
case is > 0
   return true
case 0
   return false
case -1 // General error
   f_throw(PopulateError(3, "Find() failed."))
case -5 // Bad argument
   f_throw(PopulateError(4, "Invalid search expression passed to Find():~r~n~r~n~"" + as_search_expr + &
                  "~"~r~n~r~nCheck if all the fields, mentioned in the expression, exist in " + this.DataObject + "."))
end choose

return false

The function can be also created in the DataStore's ancestor and for DataWindowChild (for the last one it should be stored in another object - for example, the DW's ancestor or the util NVO - and accept the DWC as a parameter).
User avatar
Ursego
Site Admin
 
Posts: 131
Joined: 19 Feb 2013, 20:33



IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:




cron
free counters

eXTReMe Tracker