server side script
functions1 Hello world:
As first example is shown the typical hello world. The script hasn't any parameters.
<xsql-script name='hello_world'> <body> <println>Hello world</println> </body> </xsql-script>
Hello world
function helloWorld() { console.log('Hello world'); } helloWorld();
Hello world
2 Input arguments
The following example, executes an embedded script that has an input parameter. The example calculates the factorial of the number passed as a parameter. In the exemple the script is called embedded.
Unlike the xsql-script, server side script
is not a typed language.
<call> <args> <arg>5</arg> </args> <![CDATA[ <xsql-script> <args> <arg name='p_factor' type='smallint' /> </args> <body> <set name='m_result'>1</set> <for name='m_value' start='1' end='${p_factor}'> <do> <set name='m_result'> <mul><m_value /><m_result /></mul> </set> </do> </for> <println>FACTORIAL OF [<p_factor />] ...: <m_result /></println> </body> </xsql-script> ]]> </call>
FACTORIAL OF [5] ...: 120
function calcFactorial(pIntFactor) { var mIntResult = 1; for (var mIntValue = 1; mIntValue <= pIntFactor; mIntValue++) { mIntResult *= mIntValue; } console.log(`FACTORIAL OF [${pIntFactor}] ...: ${mIntResult}`); } calcFactorial(5);
FACTORIAL OF [5] ...: 120
3 Local functions:
A server side script
function is defined with the function keyword. The main differences in local functions between Xsql and sirver side script
are:
- The arguments of an script funcion has a defined type, instead in javascript functions has not typed parameters.
- A Xsql function returns any number of parameters included zero parameters.
Server side script
functions returns 0 or 1 parameter.
The following example is a script with a local function that is called in the main body of the script. The function returns a single parameter.
<call> <args> <arg>3</arg> </args> <![CDATA[ <xsql-script> <args> <arg name='p_x1' type='integer' /> </args> <body> <function name='f_local_test'> <args> <arg name='p_x1' type='integer' /> <!-- Integer Value 1 --> <arg name='p_x2' type='integer' /> <!-- Integer Value 2 --> </args> <body> <set name='m_mult'><mul><p_x1 /><p_x2 /></mul></set> <return><m_mult /></return> </body> </function> <set name='m_x2'>5</set> <f_local_test into='m_result'> <p_x1 /> <m_x2 /> </f_local_test> <println>Result: <m_result /></println> </body> </xsql-script> ]]> </call>
Result: 15
function testLocalFunction(pIntX1) { var mIntX2, mIntResult; function __localTest(pIntX1, pIntX2){ var mIntResult = pIntX1 * pIntX2; return mIntResult; } mIntX2 = 5; mIntResult = __localTest(pIntX1, mIntX2); console.log(`Result: ${mIntResult}`); } testLocalFunction(3);
Result: 15
4 Return values
XSQL-Script can return any number of values included zero values. Server side script
only allows the
return of a single variable or void. If you don't specify anything at the end of the program, a void will be returned.
Server side script
passes a value from a function back to the code that called it by using the return statement.
That value can be a constant value, a variable, or a calculation where the result of the calculation is returned.
4.1 Return a variable
<xsql-script> <body> <set name='m_retvalue'>1</set> <return> <m_retvalue /> </return> </body> </xsql-script>
function testReturnVar() { var mIntRetValue = 1; return mIntRetValue; } return testReturnVar();
4.2 Return a constant value
Like XSQL, Server side script
can return a constant value.
<xsql-script> <body> <return> <number>1</number> </return> </body> </xsql-script>
function testReturnConst() { return 1; } return testReturnConst();
4.3 Return a calculated expression
Like XSQL, Server side script
can return a calculated expression.
<call> <args> <arg>4</arg> </args> <![CDATA[ <xsql-script> <args> <arg name='p_num' type='integer' /> </args> <body> <return> <ifthen> <expr><eq><mod><p_num />2</mod>0</eq></expr> <string>Number <p_num /> is even</string> <string>Number <p_num /> is odd</string> </ifthen> </return> </body> </xsql-script> ]]> </call>
Number 4 is even
function testReturnExpression(pIntNum) { return `Number ${pIntNum} is ${(pIntNum % 2 == 0 ? 'even' : 'odd')}`; } return testReturnExpression(4);
Number 4 is even
4.4 Return two or more values
Server side script
does not allow to return more than one value. If a function requires the return of several values,
they must be grouped in an object to be returned. In js example function __convertToInches creates amb object to return 3 values.
<xsql-script> <body> <function name='local_convert_to_inches'> <args> <arg name='p_length' type='decimal' /> <arg name='p_width' type='decimal' /> <arg name='p_height' type='decimal' /> </args> <body> <set name='m_length'> <math.round><div><p_length />2.54</div>3</math.round> </set> <set name='m_width'> <math.round><div><p_width />2.54</div>3</math.round> </set> <set name='m_height'> <math.round><div><p_height />2.54</div>3</math.round> </set> <return> <m_length /> <m_width /> <m_height /> </return> </body> </function> <set name='m_length'>200</set> <set name='m_width'>150</set> <set name='m_height'>150</set> <local_convert_to_inches into='m_inches_length, m_inches_width, m_inches_height'> <m_length /> <m_width /> <m_height /> </local_convert_to_inches> <return> <m_inches_length /> <m_inches_width /> <m_inches_height /> </return> </body> </xsql-script>
(constant_1) (decimal) |
(constant_2) (decimal) |
(constant_3) (decimal) |
---|---|---|
78,740000 | 59,055000 | 59,055000 |
function convertToInches() { var mBcLength, mBcWidth, mBcHeight; var mObjDim; var INCH2CM = 2.54; function __convertToInches(pBcLength, pBcWidth, pBcHeight) { var mObjDim = {}; mObjDim.lengthInches = Ax.math.bc.scale(Ax.math.bc.div(pBcLength, INCH2CM), 3, Ax.math.bc.RoundingMode.HALF_UP); mObjDim.widthInches = Ax.math.bc.scale(Ax.math.bc.div(pBcWidth, INCH2CM), 3, Ax.math.bc.RoundingMode.HALF_UP); mObjDim.heightInches = Ax.math.bc.scale(Ax.math.bc.div(pBcHeight, INCH2CM), 3, Ax.math.bc.RoundingMode.HALF_UP); return mObjDim; } mBcLength = 200; mBcWidth = 150; mBcHeight = 150; mObjDim = __convertToInches(mBcLength, mBcWidth, mBcHeight); return mObjDim; } return convertToInches();
key | value |
---|---|
widthInches | 59.055 |
heightInches | 59.055 |
lengthInches | 78.740 |