uf_row_exists() to replace Find()

Share this topic:

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, PB's Find() function displays the message "Expression is not valid" which doesn't help a lot (you have no idea where to look for the problem in your huge application :twisted: ). The function uf_row_exists() displays an additional message which:

A. Explains exactly what and where happened so the bug will be found quickly and easily.
B. Displays the incorrect expression (usually it is built dynamically, so our function saves extra debugging while re-producing the bug).
C. Displays the DW's DataObject and suggests to check if it has all the fields, mentioned 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
Acc:   public
Dscr:   Reports if the DW has a row which satisfies the passed search expression.
      See more details here: http://forum.powerbuilder.us/viewtopic.php?f=4&t=83
Arg:   as_search_expr - the logical expression to search by
Ret:   boolean
Thr:   n_ex
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."))
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
Site Admin
Posts: 112
Joined: 19 Feb 2013, 20:33

Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 1 guest

uf_row_exists() to replace Find()

Share this topic:

If you think that this site is not too bad, please LIKE it in Facebook. Thanks!

free counters

eXTReMe Tracker