1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Client
  5. Special Operations
  6. UpdateMath

UpdateMath

The UpdateMath() method calls an API with the update operation enabled and modify the numeric value of a specific column using the provided mathematical formula.
Result
The UniREST Server application will update the given column value recalculating it with the given formula. Then, it will reply with the recalculated value. Since this operation requires identifying a specific, existing record inside a table, and a specific column, you must provide the necessary Keys and column name.

This method is pretty useful if you need to quickly recalculate a numeric value of a table record because it does this operation directly into the table.

The method just requires that the API has the Update operation checked. The other settings will be ignored.

_ = #UniRESTClient.Async#.$UpdateMath$(%api, tableData, fieldToUpdate, formula, callBack%);

api|string
The API address, under the API class.
__
tableData|object
Optional values to send to the server. It must be an instance of the table class in use.
__
fieldToUpdate|string
The name of the specific column to update. It must be a numeric value (FLOAT).
__
formula|string
A valid mathematical formula.
__
callBack|Action<float, bool>
The function to call on operation completed.

float
It’s the result of the applied formula.
In case of error, it’s the original value stored in the Database table, without any modification.
__
bool
true if everything is ok, false if an error occurred.

In case of error, the UniRESTClient.DBerror property will contain the following “error code”:
RECORD_NOT_FOUND which means that the record can’t be located;
FORMULA_ERROR which means that the given formula has a wring format.

Formula format

The formula parameter must respect these rules:

  • it must be a string;
  • it must contain only numbers (positive, negative, integer, float), parenthesis, and the mathematical operations +, -, *, /;
  • optionally, it can contain the X character (an uppercase X) that will be considered equal to the value found in the Database table.

Examples

In the following example, the formula is applied to a column that contains the value 50.

FormulaValidityReturned value
+100OK
A value of 100 will be added to the table’s column value.
150
*2OK
The table’s column value will be multiplied by 2.
100
X + 10.5OK
A value of 10.5 will be added to the table’s column value.
60.5
100 + (X * 2)OK
The table’s column value will be multiplied by 2, and 100 will be added
to the multiplication result.
200
X + (X / 2)OK
The table’s column value will be divided by 2, and the division result will be added
to the table’s column value.
75
X ^2ERROR
^ is an invalid character.
50
X + AERROR
A is an invalid character.
50
100 +XXERROR
X character must by typed once only.
50
100ERROR
It’s just a number, not a mathematical formula.
50
+ERROR
It’s just a mathematical operator, not a formula.
50

Example

“Players” Database table
In this table, each record is a single player with its characteristics.

UpdateMath() example
In the following C# example, we imagine that the Conan character has taken a magic medicine. 500 points now increase his health level:

_ = UniRESTClient.Async.UpdateMath(
     API.player_data,
     new DB.Players
     {
         id = UniRESTClient.UserID
     },
     "health",
     "+500",
     (float result, bool ok) =>
     {
          if (ok)
          {
               // now result should be 1500
               Debug.Log("The health is now: " + result);
          }
          else
          {
               // result should be 1000 (the original value)
               Debug.LogError("Error. The health is: " + result);
          }
     }
);