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
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 ). 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:
/********************************************************************************************************************** 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).