Documentation Home
X DevAPI User Guide
Download this Manual

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

Pre-General Availability Draft: 2017-08-18

3.5 MySQL Shell Automatic Code Execution

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

var result = session.sql('show databases').execute();
var city_res = db.cities.find().execute();

The call of the execute() function above causes the operation to be executed and return 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 the 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 User Guide.

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 examples above in MySQL Shell and removing the assignment operation shows they are automatically executed.

mysql-js> session.sql('show databases');

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)

User Comments
Sign Up Login You must be logged in to post a comment.