1 Statement commons
<xsql-script> <body> <null name='m_x' /> <!-- Statement 1 --> <set name='m_x'>5</set> <!-- Statement 2 --> <set name='m_y'>6</set> <!-- Statement 3 --> <set name='m_z'><add><m_x /><m_y /></add></set> <!-- Statement 4 --> <println><m_z /></println> <!-- Statement 5 --> </body> </xsql-script>
var mIntX, mIntZ; // Statement 1 mIntX = 5; // Statement 2 mIntY = 6; // Statement 3 mIntZ = mIntX + mIntY; // Statement 4 console.log(mIntZ); // Statement 5
1.1 Semicolons
Semicolons separate server side script
statements. Be careful, it's not required but it's highly recomendable.
<xsql-script> <body> <set name='m_x'>3</set> <!-- Declare and assign the value 3 to m_x --> <set name='m_y'>2</set> <!-- Declare and assign the value 2 to m_y --> <set name='m_z'><add><m_x /><m_y /></add></set> <!-- Declare and assign the sum of m_x and m_y to m_z --> <println><m_z /></println> <!-- Show the value of m_z --> </body> </xsql-script>
var mIntX, mIntY, mIntZ; // Declare 3 variables mIntX = 3; // Assign the value 3 to a mIntY = 2; // Assign the value 2 to b mIntZ = mIntX + mIntY; // Assign the sum of mIntX and mIntY to mIntZ console.log(mIntZ); // Show the value of mIntZ
When separated by semicolons, multiple statements on one line are allowed
var mIntX, mIntY, mIntZ; mIntX = 3; mIntY = 2; mIntZ = mIntX + mIntY; console.log(mIntZ); // Show the value of mIntZ
1.2 Whitespaces
You can add white space to your script to make it more readable.
Multiple spaces are ignored.
<xsql-script> <body> <!-- The following lines are equivalent: --> <set name='m_name'><string>George<string></set> <set name='m_name'> <string>George<string> </set> <set name='m_x'><add><number>1</number><number>2</number></add></set> </body> </xsql-script>
// The following lines are equivalent: --> var mStrName = "George"; var mStrName="George"; // It's a good practice to put spaces around operators ( = + - * / ): var mIntX = 1 + 2;
For best readability, programmers often like to avoid code lines longer than 80 characters.
If a server side script
statement does not fit on one line, the best place to break it is after an operator:
1.3 Blocks
Statements can be grouped together in code blocks, inside tags < >...</ > or curly brackets {...}
The purpose of code blocks is to define statements to be executed together.
One place you will find statements grouped together in blocks, is in functions
<xsql-script> <body> <println>STATEMENT 1</println> <println>STATEMENT 2</println> <println>STATEMENT 3</println> </body> </xsql-script>
STATEMENT 1
STATEMENT 2
STATEMENT 3
function testBlocks() { console.log('STATEMENT 1'); console.log('STATEMENT 2'); console.log('STATEMENT 3'); }
STATEMENT 1
STATEMENT 2
STATEMENT 3
1.4 Server code script reserved words
A reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a reserved word may have no meaning.
You cannot use these reserved words as variables, labels, or function names:
abstract | arguments | await | boolean | break |
byte | case | catch | char | class |
const | continue | debugger | default | delete |
do | continue | debugger | default | delete |
double | else | enum | eval | export |
extends | false | final | finally | float |
for | function | goto | if | implements |
import | in | instanceof | int | interface |
let | long | native | new | null |
package | private | protected | public | return |
short | static | super | switch | synchronized |
this | throw | throws | transient | true |
try | typeof | var | void | volatile |
while | with | yield |
2 Syntax
Syntax is the set of rules, how programs are constructed:
<xsql-script> <body> <!-- Declare variables --> <null name='m_x' /> <null name='m_y' /> <null name='m_z' /> <!-- Assign values --> <set name='m_x'>5</set> <set name='m_y'>6</set> <!-- Compute values --> <set name='m_z'><add><m_x /><m_y /></add></set> </body> </xsql-script>
var mIntX, mIntY, mIntZ; // Declare Variables // Assign Values mIntX = 5; mIntY = 6; mIntZ = mIntX + mIntY; // Compute Values
2.1 Values
Syntax defines two types of values:
- Fixed values (literals)
- Variable values (Variables)
2.2 Literals
Numbers are written with or without decimals:
<xsql-script> <body> <set name='m_x'>10.50</set> <set name='m_y'>1001</set> </body> </xsql-script>
var mBcX = 10.50; var mIntY = 1001;
Strings are text, written within double or single quotes in server code script
.: "Mike Ewing" 'Mike Ewing'
<xsql-script> <body> <set name='m_text'>THIS IS A TEXT</set> </body> </xsql-script>
var mStrText1 = 'THIS IS A TEXT'; var mStrText2 = "THIS IS A TEXT";
2.3 Variables
In a programming language, variables are used to store data values.
Server side script uses the var keyword to declare variables.
An equal sign is used to assign values to variables.
<xsql-script> <body> <set name='m_variable'>THIS IS A VARIABLE VALUE</set> </body> </xsql-script>
var mStrVariable = 'THIS IS A VARIABLE VALUE';
2.4 Operators
In computer programming, operators are constructs defined within programming languages which behave generally like functions, but which differ syntactically or semantically.
Common simple examples include arithmetic (e.g. addition with +), comparison (e.g. "greater than" with >), and logical operations (e.g. AND, also written && in some languages). More involved examples include assignment (usually = or :=), field access in a record or object (usually .), and the scope resolution operator (often :: or .). Languages usually define a set of built-in operators, and in some cases allow users to add new meanings to existing operators or even define completely new operators.
<xsql-script> <body> <!-- arithmetic operators --> <mul><add><number>5</number>6</add>10</mul> <!-- Assigment operator --> <set name='m_x'>5</set> </body> </xsql-script>
(5 + 6) * 10 // Arithmetic operators var x = 5; // Assigment operator
2.5 Expression
Expression is a syntactic entity in a programming language that may be evaluated to determine its value. It is a combination of one or more constants, variables, functions, and operators that the programming language interprets (according to its particular rules of precedence and of association) and computes to produce ("to return", in a stateful environment) another value.
<xsql-script> <body> <mul><add><number>5</number>6</add>10</mul> </body> </xsql-script>
(5 + 6) * 10
2.6 Comment
A comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.
<xsql-script> <body> <!-- This is a comment --> <println>TEST</println> <!-- <println>TEST</println> --> <!-- Won't be printed --> </body> </xsql-script>
TEST
// Code after double slashes // or between /* and */ is treated as a comment. console.log("TEST"); /* Won't be printed */ //console.log("TEST");
TEST
2.7 Identifiers
Identifiers are tokens (also called symbols) which name the language entities. Some of the kinds of entities an
identifier might denote include variables, types, labels, subroutines, and packages. In server code script
, the first
character must be a letter, or an underscore (_), or a dollar sign ($). Subsequent characters may be letters, digits,
underscores, or dollar signs.
2.8 Variable styling
Snake Case:
first_name, last_name, master_card, inter_city.
Upper Camel Case (Pascal Case):
FirstName, LastName, MasterCard, InterCity.
Lower Camel Case:
We recommend to use lower camel case in server side script
firstName, lastName, masterCard, interCity.
2.9 Miscellaneous
All server side script
identifiers are case sensitive.
The variables lastName and lastname, are two different variables:
server side script
uses the Unicode character set.
3 Comments
Comments can be used to explain code, and to make it more readable. Comments can also be used to prevent execution, when testing alternative code.
3.1 Single Line Comments
Single line comments start with //.
Any text between // and the end of the line will be ignored by server side script
(will not be executed).
<xsql-script> <body> <!-- single-line comment between each code line --> <!-- COMMENT 1 --> <set name='m_x'>1</set> <!-- COMMENT 2 --> <set name='m_x'>25</set> <!-- single line comment at the end of each line to explain the code --> <set name='m_x'>12</set> <!-- Declare m_x, give it the value of 12 --> <set name='m_x'><add><m_x />3</add></set> <!-- Declare m_y, give it the value of m_x + 3 --> </body> </xsql-script>
// single-line comment before each code line // COMMENT 1 var mIntX = 1; // COMMENT 2 var mIntX = 25 // single line comment at the end of each line to explain the code var mIntX = 12; // Declare mIntX, give it the value of 12 var mIntY = mIntX + 3; // Declare mIntY, give it the value of mIntX + 3
3.2 Multi-line Comments
Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by server side code.
<xsql-script> <body> <!-- ========================================== --> <!-- This is a example of a multiline code. --> <!-- It uses more than one line to write the --> <!-- comment --> <!-- ========================================== --> <set name='m_x'>1</set> </body> </xsql-script>
/** This is a example of a multiline code. * It uses more than one line to write the * comment */ var mIntX = 1;
3.3 Using Comments to Prevent Execution
Using comments to prevent execution of code is suitable for code testing.
<xsql-script> <body> <!-- Comment a single-line of code --> <!-- <set name='m_x'>1</set> --> <!-- Comment multiple-lines of code --> <comment> <set name='m_x'>25</set> <set name='m_y'>12</set> </comment> </body> </xsql-script>
// Comment a single-line of code // var mIntX = 1 // Comment multiple-lines of code /** var mIntX = 25; var mIntY = 12 */
4 Variables
Variables are containers for storing data values.
<xsql-script> <body> <set name='m_x'>2</set> <set name='m_y'>7</set> <set name='m_z'><add><m_x /><m_y /></add></set> <return><m_z /></return> </body> </xsql-script>
9
var x = 2; var y = 7; var z = x + y; return z;
9
4.1 Using let and const
It's allowed the use of the const keyword to define a variable that cannot be reassigned, and the let keyword to define a variable with restricted scope.
4.2 Use of variable
We use variables in expressions. Variables are containers for storing data values
<xsql-script name='test_comment'> <body> <set name='m_x'>2</set> <set name='m_y'>7</set> <set name='m_z'><add><m_x /><m_y /></add></set> <return><m_z /></return> </body> </xsql-script>
9
var x = 2; var y = 7; var z = x + y; return z;
9
4.3 Server side script Identifiers
All server side script
variables must be identified with unique names. These unique names are called identifiers.
The general rules for constructing names for variables (unique identifiers) are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter
- Names can also begin with $ and _ (but we will not use it in this tutorial)
- Names are case sensitive (y and Y are different variables)
- Reserved wordscannot be used as names
server side script
identifiers are case-sensitive.4.4 The Assignment Operator
In server side script, the equal sign (=) is an "assignment" operator, not an "equal to" operator.
<xsql-script> <body> <set name='m_x'>2</set> </body> </xsql-script>
var x = 2;
4.5 Declaring server side script Variables
You declare a server side script
variable with the var keyword:
<xsql-script name='test_comment'> <body> <!-- After the declaration, the variable has the value NULL --> <null name='m_fruit' /> <!-- To assign a value to the variable, use the tag set --> <set name='m_fruit'>Banana</set> <!-- It's possible to assign a value to the variable when it's declared --> <set name='m_dogs'>4</set> <!-- show stored values in variables --> <println>m_fruit = <m_fruit /></println> <println>m_dogs = <m_dogs /></println> </body> </xsql-script>
m_fruit = Banana
m_dogs = 4
// After the declaration, the variable has no value (technically it has the value of undefined). var mStrFruit; // To assign a value to the variable, use the equal sign: mStrFruit = 'Banana'; //It's possible to assign a value to the variable whe it's declared var mIntDogs = 4; // show stored values in variables console.log("mStrFruit = " + mStrFruit); console.log("mIntDogs = " + mIntDogs);
mStrFruit = Banana
mIntDogs = 4
4.6 One Statement, Many Variables
You can declare many variables in one statement.
<xsql-script name='test_comment'> <body> <!-- In xsql script it's only possible to define a variable per line --> <set name='m_x'>2</set> <set name='m_y'>7.25</set> <set name='m_z'>Michael</set> </body> </xsql-script>
//Start the statement with var and separate the variables by comma: var mIntX = 2, mBcY = 7.25, mStrZ = "Michael"; //A declaration can span multiple lines: var mIntA = 2, mBcB = 7.25, mStrC = "Michael";
4.7 Value = undefined
In computer programs, variables are often declared without a value. The value can be something that has to be calculated, or something that will be provided later.
<xsql-script name='test_comment'> <body> <!-- Variables are initializated as null in XSQL --> <null name='m_x' /> <println><m_x /></println> </body> </xsql-script>
null
// Variables are initializated as undefined in server side script var mIntX; console.log(mIntX)
undefined
4.8 Re-Declaring server side script Variables
server side script
variable, it will not lose its value.<xsql-script name='test_redeclare'> <body> <!-- redeclaration of variables in XSQL means assign to them a new value --> <set name='m_x'>2</set> <null name='m_x' /> </body> </xsql-script>
null
// If you re-declare a server side script variable, it will not lose its value. var mIntx = 2; var mIntx;
2