Transaction lock not releasing on the last record - progress-4gl

I have an include file with 2 functions. One of the functions works perfectly fine so I won't include it in this. I will include the function that is causing the issue.
The ss_update function is the one causing me issue and not releasing the lock as I assumed it would. I finally got to work this way adding the find current screenstate no-lock. statement. I wondering if someone can explain this to me and if there is a better way to handle this situation.
FUNCTION ss_update RETURNS INTEGER
( INPUT iUserName AS CHAR,
INPUT iScreenName AS CHAR,
INPUT iWidgetName AS CHAR,
INPUT iWidgetValue AS CHAR ):
DEFINE VARIABLE retStatus AS INTEGER NO-UNDO.
FIND ScreenState EXCLUSIVE-LOCK WHERE ScreenState.userName = iUserName AND
ScreenState.screenName = iScreenName AND
ScreenState.widgetName = iWidgetName NO-ERROR.
IF AVAIL ScreenState THEN
DO:
IF ScreenState.widgetValue <> iWidgetValue THEN
DO:
ASSIGN
ScreenState.widgetValue = iWidgetValue.
END.
retStatus = 1.
END.
IF NOT AVAIL ScreenState THEN
DO:
CREATE ScreenState.
ASSIGN
ScreenState.screenStateId = NEXT-VALUE(seq-ScreenStateId)
ScreenState.userName = iUserName
ScreenState.screenName = iScreenName
ScreenState.widgetName = iWidgetName
ScreenState.widgetValue = iWidgetValue.
retStatus = 2.
END.
/* This was added to release the lock. */
FIND CURRENT screenstate NO-LOCK.
RETURN retStatus.
END FUNCTION.
I have code that will call the update function several times in a row. Like this...
ss_update(USERID(LDBNAME(1)), "FindComp2.w", "t-ActiveOnly", t-ActiveOnly:SCREEN-VALUE).
ss_update(USERID(LDBNAME(1)), "FindComp2.w", "t-BadAdd", t-BadAdd:SCREEN-VALUE).
ss_update(USERID(LDBNAME(1)), "FindComp2.w", "LastCompany", company.companyId).
ss_update(USERID(LDBNAME(1)), "FindComp2.w", "rs-Filter", rs-Filter:SCREEN-VALUE).
ss_update(USERID(LDBNAME(1)), "FindComp2.w", "cb-Salesman", cb-Salesman:SCREEN-VALUE).
ss_update(USERID(LDBNAME(1)), "FindComp2.w", "cb-Search", cb-Search:SCREEN-VALUE).
ss_update(USERID(LDBNAME(1)), "FindComp2.w", "scr-Search", TRIM(scr-Search:SCREEN-VALUE)).
The problem I was having was that progress wasn't releasing the lock from the last called ss_update function. I had to add find current screenstate no-lock to downgrade the lock. This just seems ugly and not properly coded and was wondering why this happened and what is the proper way to handle this such issue.

Your record buffers are weakly scoped and there is likely a reference to ScreenState somewhere in the program that includes this function.
The function is likely "borrowing" the record from the main block.
To fix it there are several possibilities. It is quick and dirty but one of the things that I like to do is to add:
define buffer ScreenState for ScreenState.
at the top of the function definition. This may look a little odd but what it does is to force all references to ScreenState to be local to the function. It stops the accidental "borrowing" of scope.
The ultimate solution is to strong scope the records and declare explicit transactions. That code would look like this:
FUNCTION ss_update RETURNS INTEGER
( INPUT iUserName AS CHAR,
INPUT iScreenName AS CHAR,
INPUT iWidgetName AS CHAR,
INPUT iWidgetValue AS CHAR ):
define buffer ScreenState for ScreenState. /* prevent accidents from happening... */
define buffer updScreenState for ScreenState. /* used for updates */
DEFINE VARIABLE retStatus AS INTEGER NO-UNDO.
do for updScreenState transaction:
FIND updScreenState EXCLUSIVE-LOCK WHERE
updScreenState.userName = iUserName AND
updScreenState.screenName = iScreenName AND
updScreenState.widgetName = iWidgetName NO-ERROR.
IF available updScreenState THEN
DO:
IF updScreenState.widgetValue <> iWidgetValue THEN
DO:
ASSIGN
updScreenState.widgetValue = iWidgetValue.
END.
retStatus = 1.
END.
IF NOT available updScreenState THEN
DO:
CREATE updScreenState.
ASSIGN
updScreenState.screenStateId = NEXT-VALUE(seq-ScreenStateId)
updScreenState.userName = iUserName
updScreenState.screenName = iScreenName
updScreenState.widgetName = iWidgetName
updScreenState.widgetValue = iWidgetValue.
retStatus = 2.
END.
end.
RETURN retStatus.
END FUNCTION.
The code above defines both ScreenState and updScreenState -- strictly speaking plain old ScreenState does nothing since there are no references to it. But if someone comes along later (or if I somehow missed one) it will prevent accidental references from having side-effects.
Using updScreenState makes it clear and obvious that the buffer is for update purposes.
The explicit TRANSACTION keyword clearly defines where you expect a transaction to start -- if the compiler objects to that then it is telling you that your code is trying to do something that you didn't expect.
The DO FOR block is what "strong scopes" the updScreenState buffer. The compiler will object if there are stray free references to updScreenState lying around outside that block.

Related

Call a script with definitions in a function

We have a script that defines values to names similar to #define in c. For example:
script.m:
ERR_NOERROR = 0;
ERR_FATAL = 1;
This script already exists and is used for value replacement when reading data from files.
Now we have a function (or more) that does some analysis and we would like to use the same definition in this function to avoid magic numbers. But when the script is called from the function we get an error.
Attempt to add "ERR_NOERROR" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
And this does not help much in the understanding of the problem.
The question is how can we make these definitions visible/usable in the functions with having to copying it every time.
Example:
function foo = bar(a)
run(script.m) %also tried running it without the run command
if a == ERR_NOERROR
foo = 5;
else
foo = 6;
end
end
edit:
There was a nested function,below in the function which I was not aware of. This explains the problem.
This kind of scoping error happens when you use nested or anonymous function within a function. The solution is well documented.
To your case, you can avoid nested function, or "Convert the script to a function and pass the variable using arguments", as the documentation suggests.
EDIT: I should have made it clear that the error occurs even if the script is not called within the nested function. Similar scenario is that, in debug mode (by setting up a break point), it will be an error if one tries to create a temporal variable to test something.
This is not a direct answer, rather a recommendation to switch to another method, which will not be mixing scope and workspace.
Instead of defining your constant in a script, you could make a class containing only constant properties. ex: code for error_codes.m:
classdef error_codes
% ---------------------------------------------------------------------
% Constant error code definition
% ---------------------------------------------------------------------
properties (Constant = true)
noerror = 0 ;
fatal = 1 ;
errorlvl2 = 2 ;
errorlvl3 = 3 ;
warning = -1 ;
% etc ...
end
end
I use this style for many different type of constants. For tidiness, I groups them all in a Matlab package directory (The directories which starts with a + character.
The added benefit of using constant class properties is the safety that the values cannot be changed in the middle of the code (your variables defined in a script could easily be overwritten by a careless user).
So assuming my file error_codes.m is placed in a folder:
\...somepath...\+Constants\error_codes.m
and of course the folder +Constants is on the MATLAB path, then to use it as in your example, instead of calling the script, just initialise an instance of the class, then use the constant values when you need them:
function foo = bar(a)
ERR = Constants.error_codes ;
if a == ERR.noerror
foo = 5;
else
foo = 6;
end
or it can works in switch statement too:
switch a
case ERR.noerror
foo = 5 ;
case ERR.warning
foo = 42 ;
case ERR.fatal
foo = [] ;
end

4GL ABL Openedge loop through handle?

here is my current code
def var hbTT as handle.
for each Cust:
hbTT:buffer-create().
assign
hbTT::Name = Cust.Name
hbTT::address = Cust.Address.
end.
now what I want to do is to loop through hbtt. How can I do that?
I tried
for each hbTT:
/* Do something */
end.
the error I get is
unknown or ambiguous table hbTT. (725)
thank you
You won't be able to do a loop that way, as for each requires a static name.
Instead, try this:
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
create query hQuery.
hQuery:set-buffers(hbtt).
hquery:query-prepare('for each tt'). /* <-- Where tt is the original buffer name */
hquery:query-open().
hquery:get-first().
do while not hquery:query-off-end:
disp hbtt::name hbtt::address .
hquery:get-next().
end.

1-line try/catch equivalent in MATLAB

I have a situation in MATLAB where I want to try to assign a struct field into a new variable, like this:
swimming = fish.carp;
BUT the field carp may or may not be defined. Is there a way to specify a default value in case carp is not a valid field? For example, in Perl I would write
my $swimming = $fish{carp} or my $swimming = 0;
where 0 is the default value and or specifies the action to be performed if the assignment fails. Seems like something similar should exist in MATLAB, but I can't seem to find any documentation of it. For the sake of code readability I'd rather not use an if statement or a try/catch block, if I can help it.
You can make your own function to handle this and keep the code rather clear. Something like:
swimming = get_struct(fish, 'carp', 0);
with
function v = get_struct(s, f, d)
if isfield(s, f)
v = s.(f); % Struct value
else
v = d; % Default value
end
Best,
From what I know, you can't do it in one line in MATLAB. MATLAB logical constructs require explicit if/else statements and can't do it in one line... like in Perl or Python.
What you can do is check to see if the fish structure contains the carp field. If it isn't, then you can set the default value to be 0.
Use isfield to help you do that. Therefore:
if isfield(fish, 'carp')
swimming = fish.carp;
else
swimming = 0;
end
Also, as what Ratbert said, you can put it into one line with commas... but again, you still need that if/else construct:
if isfield(fish,'carp'), swimming = fish.carp; else, swimming = 0;
Another possible workaround is to declare a custom function yourself that takes in a structure and a field, and allow it to return the value at the field, or 0.
function [out] = get_field(S, field)
if isfield(S, field)
out = S.(field);
else
out = 0;
end
Then, you can do this:
swimming = get_field(fish, 'carp');
swimming will either by 0, or fish.carp. This way, it doesn't sacrifice code readability, but you'll need to create a custom function to do what you want.
If you don't like to define a custom function in a separate function file - which is certainly a good option - you can define two anonymous functions at the beginning of your script instead.
helper = {#(s,f) 0, #(s,f) s.(f)}
getfieldOrDefault = #(s,f) helper{ isfield(s,f) + 1 }(s,f)
With the definition
fish.carp = 42
and the function calls
a = getfieldOrDefault(fish,'carp')
b = getfieldOrDefault(fish,'codfish')
you get for the first one
a = 42
and the previous defined default value for the second case
b = 0

How to execute procedure from List with parameters in Progress 4gl?

I have some list like this
DEFINE VARIABLE procedurelist AS CHARACTER EXTENT 5
INITIAL [ "1.p", "2.p", "3.p", "4.p", "5.p"].
but this all procedures with input-output parameters and i want to execute this procedure, How can i do this? I have no idea how to do this.
The base of your solution is the RUN VALUE statement.
The manual states.
VALUE( extern-expression ) An expression that returns the name of the (local or remote) external procedure you want to run....
This basically means that you can input a string with the value of a program (or procedure) into your RUN statement.
If all input-output parameters are exactly the same you can do like this:
DEFINE VARIABLE procedurelist AS CHARACTER EXTENT 5 INITIAL [ "1.p", "2.p", "3.p", "4.p", "5.p"].
DEFINE VARIABLE iExtent AS INTEGER NO-UNDO.
DEFINE VARIABLE cVariable AS CHARACTER NO-UNDO.
DO iExtent = 1 TO EXTENT(procedurelist):
RUN VALUE(procedurelist[iExtent]) (INPUT-OUTPUT cVariable).
END.
If the parameters are different it gets trickier (but not impossible). The CREATE CALL and the Call Object can help you there. In this case you would need some kind of way to keep track of the different parameters as well.
Here's a basic example taken directly from the online help:
DEFINE VARIABLE hCall AS HANDLE NO-UNDO.
CREATE CALL hCall.
/* Invoke hello.p non-persistently */
hCall:CALL-NAME = "hello.p".
/* Sets CALL-TYPE to the default */
hCall:CALL-TYPE = PROCEDURE-CALL-TYPE.
hCall:NUM-PARAMETERS = 1.
hCall:SET-PARAMETER(1, "CHARACTER", "INPUT", "HELLO WORLD").
hCall:INVOKE.
/* Clean up */
DELETE OBJECT hCall.

Preventing Function Overriding in Lua Table

In my program when two functions with the same name are defined for the same table, I want my program to give an error. What's happening is that it's simply just calling the last function and executing it.
Here's a sample code
Class{'Cat'}
function Cat:meow( )
print("Meow!")
end
function Cat:meow()
print("Mmm")
end
kitty = Cat:create()
kitty:meow()
The result of the execution is only: "Mmm"
Instead I want something like an error message to be given.
Unfortunately, __newindex does not intercept assignments to fields which already exist. So the only way to do this is to keep Cat empty and store all its contents in a proxy table.
I don't know the nature of your OOP library, so you'll have to incorporate this example on your own:
local Cat_mt = {}
-- Hide the proxy table in closures.
do
local proxy = {}
function Cat_mt:__index(key)
return proxy[key]
end
function Cat_mt:__newindex(key, value)
if proxy[key] ~= nil then
error("Don't change that!")
end
proxy[key] = value
end
end
Cat = setmetatable({}, Cat_mt)