The WSO2 Data Services Server (DSS) is an enterprise-grade, lean, 100% open source, cloud-enabled, multi-tenant data service hosting and management platform. From the next series of posts, I'm going to explain several ways that we can use the WSO2 DSS to insert data into multiple tables.
1. Create a sample database.
I have named this database as 'order_sample'.
Before starting to go into detail, let us setup a simple environment to demonstrate our scenario. I'm going to use WSO2 Data services Server 3.0.1, which is the latest release at the time of this post. If you haven't downloaded it yet, you can download the product distribution through here. I will be using MySQL as the database during these posts.
In this introduction post, I'll be guiding you step by step through the process of creating a sample database to creating a sample data service that inserts data into two tables.
First, lets create the database.
In this introduction post, I'll be guiding you step by step through the process of creating a sample database to creating a sample data service that inserts data into two tables.
First, lets create the database.
Creating the sample database
1. Create a sample database.
I have named this database as 'order_sample'.
2. Create the tables.
I am creating 2 tables in this database. "orders" and "order_items". Im using the following SQL statement to create the database tables.
Creating the sample data service
1. Copy the MySQL connector jar
Before starting the server, we need to copy the MySQL connector jar to $DSS_HOME/repository/components/lib directory.
2. Start the server and login
The DSS server can be started using the wso2server.sh/wso2server.bat file located in $DSS_HOME/bin folder. For more information on configuration of the Data Services Server, please refer to the documentation.
2. Start the server and login
The DSS server can be started using the wso2server.sh/wso2server.bat file located in $DSS_HOME/bin folder. For more information on configuration of the Data Services Server, please refer to the documentation.
3. Create the sample data service.
To create a data service we have to navigate to the following location, from the management console.
Home > Manage > Services > Add > Data Service > Create
Note: Please refer the documentation for more detailed description on how to create a data service
I have given the name "SampleInsert" as my data service name.
Then Im going to construct my queries that needs to be executed. I have two queries, one to insert data in to the order_items table and another one to insert data to orders table.
Following is a snapshot of the "insert_order_items" query which I use to insert data to the order_item table. We need to pass 2 parameters for this query which are the order id and the item id.
The following image shows the "insert_orders" query which insert data to the order table. Even though this table has 3 columns we only pass values to "creation_time" and "created_by" columns. The "order_id" is an auto generated value from the database. Since we need this auto generated value for the "insert_order_items" query, we enable "Return Generated Keys" in this query. As a result of this query execution, the auto generated id will be returned to the client.
Next I'm going to define the operations that needs to be exposed from the data service. I expose 2 operations, "addOrder" and "addOrderItem".
Now we have finished creating our data service. I'm going to test this using the "TryIt" functionality of WSO2 DSS.
Following is the request and the response of the "addOrder" operation for the DSS.
Then we can use the above generated id to invoke the "addOrderItem" operation.
We can check the database to see whether our tables got updated correctly.
Home > Manage > Services > Add > Data Service > Create
Note: Please refer the documentation for more detailed description on how to create a data service
I have given the name "SampleInsert" as my data service name.
Next we need to give the data source information. I have given the connection details to the database we created earlier.
Then Im going to construct my queries that needs to be executed. I have two queries, one to insert data in to the order_items table and another one to insert data to orders table.
Following is a snapshot of the "insert_order_items" query which I use to insert data to the order_item table. We need to pass 2 parameters for this query which are the order id and the item id.
The following image shows the "insert_orders" query which insert data to the order table. Even though this table has 3 columns we only pass values to "creation_time" and "created_by" columns. The "order_id" is an auto generated value from the database. Since we need this auto generated value for the "insert_order_items" query, we enable "Return Generated Keys" in this query. As a result of this query execution, the auto generated id will be returned to the client.
Next I'm going to define the operations that needs to be exposed from the data service. I expose 2 operations, "addOrder" and "addOrderItem".
Following is the request and the response of the "addOrder" operation for the DSS.
Then we can use the above generated id to invoke the "addOrderItem" operation.
We can check the database to see whether our tables got updated correctly.
Conclusion
This post explains the basic method of inserting data into multiple tables using WSO2 DSS. Note that this method is not executed in a single database transaction. From my next post, I'll explain on how to insert data into multiple tables in a transactional manner.