Managing data using Ballerina vs Go

Anupama Pathirage
Ballerina Swan Lake Tech Blog
3 min readDec 19, 2018
Photo by Fabian Grohs on Unsplash

In this blog post, I am going to explore how we can manage data in relational databases using Go[1] and Ballerina[2] languages. Mainly I will compare the differences in syntax for creating tables, inserting data and select data. For this comparison, the samples are written in a way that it perform the same task while producing the same output. This comparison and syntax is based on Ballerina 0.990.0 version and Go 1.8.3 version.

These samples are based on MySQL DB and following script is used to create the database.

Following are few common observations across all samples.

Importing the database driver
-Go: In the import statement of sql package we need to mention the driver for the specific database we want to use.
- Ballerina: We need to manually copy the JDBC driver jar file to the $BALLERINA_HOME/bre/lib folder.

Creating the DB Connection
- Go: sql.Open() does not establish any connections to the database, nor does it validate driver connection parameters. Instead, it simply prepares the database abstraction for later use.
- Ballerina: Client represent the connection pool (not a single connection) and depending on the properties of the pool, the pool can open the connections immediately or on demand.

Prepared statements
- Go: can use Prepare() method for later queries or executions. Query() method internally actually prepares, executes, and closes a prepared statement.
- Ballerina: No separate method to create prepared statements. SQL client remote functions internally prepares, executes and closes a prepared statement at each execution.

Parameter Passing
- Go:The syntax for placeholder parameters in prepared statements is database-specific. For example, in MySQL we have to use ‘?’, in PostgreSQL it is $1, $2 etc and in Oracle we have to use :col1 , :col2 etc.
- Ballerina: We always have to use ‘?’ for parameter palace holders.

Close the DB Connection
- Go: Use Close() to close the database handle and it is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.
- Ballerina: Use stop() to shut down the connection pool. Similar to Go the DB client should be long lived.

Handling Errors
-Go: Almost all operations with database/sql types return an error as the last value. we need to check these errors always and never ignore them. Also errors can be returned when iterating Result sets as well.
-Ballerina: All DB client endpoint remote functions returns error as well. So need to check them always. In ballerina if any error occurred during the iteration it will result in a panic.

Create Table

In both languages create table have similar syntax and similar lines of code.

Using Go:

Using Ballerina:

Insert Data

In Go, the updated row count and generated IDs should be fetched using different function calls. In Ballerina those information are returned by the same function call which actually performs the update. So in Ballerina we have less no of lines to achieve the same, but the returned tuple de-structuring looks bit complex for a new user. Error handling also reduced due to the single function call.

Using Go:

Using Ballerina:

Select Data

In both languages we have a function to issue the DB query and get the output result rows by passing query parameters as well. In ballerina we have builtin table data type, which makes the iteration of the result set is really easy by using either foreach or while loop. In both languages, we need to define a matching data holder which can be used to store data column values of a given row. In Go, a var is created with the matching fields and in Ballerina a record is used. Since record is a first class type in Ballerina, we can easily convert it to other data types such as map, json etc.

Using Go:

Using Ballerina:

Following is a summary of common data management features across the two languages.

References:

[1] https://golang.org/
[2] https://ballerina.io/
[3] http://go-database-sql.org/
[4] https://ballerina.io/learn/by-example/

--

--

Anupama Pathirage
Ballerina Swan Lake Tech Blog

Open Source Contributor | Developer — Ballerina Language| Director of Engineering — WSO2 | Travel 🏝 . Photography 📸 | 🇱🇰 | Twitter: https://bit.ly/356icnr