Documentation Home
X DevAPI User Guide
Download this Manual
PDF (US Ltr) - 1.4Mb
PDF (A4) - 1.4Mb


X DevAPI User Guide  /  CRUD Operations  /  MySQL Shell Automatic Code Execution

3.5 MySQL Shell Automatic Code Execution

When you use X DevAPI in a programming language that fully specifies the syntax to be used, for example, when executing SQL statements through an X DevAPI session or working with any of the CRUD operations, the actual operation is performed only when the execute() function is called. For example:

var result = mySession.sql('show databases').execute()
var result2 = myColl.find().execute()

The call of the execute() function above causes the operation to be executed and returns a Result object. The returned Result object is then assigned to a variable, and the assignment is the last operation executed, which returns no data. Such operations can also return a Result object, which is used to process the information returned from the operation.

Alternatively, MySQL Shell provides the following usability features that make it easier to work with X DevAPI interactively:

  • Automatic execution of CRUD and SQL operations.

  • Automatic processing of results.

To achieve this functionality MySQL Shell monitors the result of the last operation executed every time you enter a statement. The combination of these features makes using the MySQL Shell interactive mode ideal for prototyping code, as operations are executed immediately and their results are displayed without requiring any additional coding. For more information see MySQL Shell 8.4.

Automatic Code Execution

If MySQL Shell detects that a CRUD operation ready to execute has been returned, it automatically calls the execute() function. Repeating the example above in MySQL Shell and removing the assignment operation shows the operation is automatically executed.

mysql-js> mySession.sql('show databases')
mysql-js> myColl.find()

MySQL Shell executes the SQL operation, and as mentioned above, once this operation is executed a Result object is returned.

Automatic Result Processing

If MySQL Shell detects that a Result object is going to be returned, it automatically processes it, printing the result data in the best format possible. There are different types of Result objects and the format changes across them.

mysql-js> db.countryInfo.find().limit(1)
[

    {

        "GNP": 828,

        "IndepYear": null,

        "Name": "Aruba",

        "_id": "ABW",

        "demographics": {

            "LifeExpectancy": 78.4000015258789,

            "Population": 103000

        },

        "geography": {

            "Continent": "North America",

            "Region": "Caribbean",

            "SurfaceArea": 193

        },

        "government": {

            "GovernmentForm": "Nonmetropolitan Territory of The Netherlands",

            "HeadOfState": "Beatrix"

        }

    }

]

1 document in set (0.00 sec)