From dbWatch 12.2.1, one of the engines available in properties is javascript.

Example that sums each column from an sql result:

<property>
   <key>scripttest</key>
   <compatability>instance[databasetype='sqlserver']</compatability>
   <value engine="sql">select 1, 2, 3</value>
   <value engine="javascript">
      <![CDATA[
         try {
            var sum = 0;
            var rows = input.getRows();
            var j;
            for(j = 0; j < rows.length; j++) 
            {
               var row = rows[j];
               var i;
               for(i = 0; i < row.length(); i++) {
                  sum = sum + row.get(i).asLong();;
               }
            }

            result.setProperty("scripttest", sum);
         } catch (err) {
            result.setProperty(2, err.message);       				
         }
      ]]>
   </value>		
   <valid-for>5s</valid-for>
</property>


In the javascript code there are two speicial objects to be aware of: input and result.

Input

If foreach=“row” or foreach=“column” is specified input contains the values for a row/column of the previous step as a ResultList.
Otherwise input contains all the values from the previous step as a Table object.

Table has the methods:

ResultList[] getRows() Returns an array of ResultList objects representing each row
ResultList[] getColumns() Returns an array of ResultList objects representing each column

ResultList has the methods:

int length() Returns the number of columns
ResultWrapper get(int colNumber) Returns the value for this column.

ResultWrapper has the methods:

long asLong() Returns the value as a long
long asLong(long default) Returns the value as a long, using the default if the result is null or not convertible to long
String asString() Returns the value as a String.
double asDouble() Returns the value as double, using 0 if the value is null or not convertible to double
double asDouble(double default) Returns the value as double, using the default if the value is null or not convertible to double

Result

Result is where you put the result of this step, it is a JavascriptPropertiesWrapper object.

JavascriptPropertiesWrapper has the following methods:

void setProperty(String name, Object value) Sets a property
void addProperty(String name, Object value) Adds a property
void setStatus(int status, String details) Sets the status. Used when the property is a standard task.
void setStatus(short status, String details) Sets the status. Used when the property is a standard task.
long toLong(long value) Utility function for converting String to long.
ResultWrapper last(String name, Object default) Gets the value this property had after the last execution.
ResultWrapper[] last(String name, Object default) Gets the values this property had after the last execution.

← Importing resources / Other →

Feedback

Was this helpful?

Yes No
You indicated this topic was not helpful to you ...
Could you please leave a comment telling us why? Thank you!
Thanks for your feedback.

Post your comment on this topic.

Post Comment