How to Write a SQL Connector Function
  • 20 Sep 2022
  • 4 Minutes to read
  • Contributors

How to Write a SQL Connector Function


Article Summary

How to Write a SQL Connector Function

Use this guide to connect your Tulip App to a SQL Database.

Here's an example of how to use Tulip's Connectors feature to connect to a SQL database.

To add a new SQL Connector:

1- Go to the Connectors page by placing your cursor over Apps in the Menu Bar and choosing Connectors from the dropdown.

2- Click "Add Connector" in the top right of the screen, give your connector a name and select "SQL" as the connector type.

3- On the Environments tab of the new connector, select "Add Connection Details".

4- Fill in the appropriate details to connect to the database:

  • Running On ("Cloud Connector Host" unless you have set up a different setting)
  • Type
  • Server
  • Port
  • SSL (yes or no)
  • Database
  • Username
  • Password

5- Click the Test button to run a test signal. If it is successful, hit "Save".

6- Ensure that the Connector Host has access to the database (indicated by green "Online" label next to the server on the Connectors page)

Once you have successfully set up a new connector, you can now create Connector Functions to send or retrieve data from the external database. A connector function must be written in the relevant language of the target database.

To set up a new SQL connector function:

1- Click the "Add New Function" button on the Functions tab.

2- Name the new function (this allows you to pick the specific connector function in Triggers).

  1. Configure the function Inputs and Outputs

In the example above:

Inputs

  • configuration (text)

Outputs

  • subconfig_1
  • subconfig_2
  • subconfig_3

Query

SELECT subconfig_1, subconfig_2, subconfig_3 FROM webinar database  
WHERE configuration = $configuration$  
order by id desc  
limit 1 ```

A function can have multiple inputs and outputs. You will need at least one input or output in order to send/receive data from a Tulip App.

  • Click Add to add a new input or output
  • Give the field a recognizable name
  • Choose the data type (e.g. Text, Boolean, Integer, etc.)
  1. Add a valid SQL statement into the Query field. Note that the input variable names should be wrapped in $. Example:
$input$

.

  1. Test the function
  • If the function has inputs, enter valid values
  • Click "Test" in the top right.
  • View test results or error messages in the Test Results section

  1. In some cases, you may want to return multiple rows from a SQL table. To do this, check the box next to "Return Multiple Rows". This will return an Array full of Objects, where each object contains values from one row in the SQL table.

Query

SELECT name, num from numbers where num > $number$

(in the example above, number is an input)

Then, when you add your inputs and click "Test", you should see all the expected rows returned under "Test Results".

These can be stored in a Variable as an array full of Objects in the Trigger Editor.

7- Press "Save" when you are finished.

NOTE

Inputs and Outputs cannot be changed after saving a function. This is to prevent disruptions to apps that may be using a connector function.

If you need to add new inputs/outputs, you can copy the function and make changes in the new function.

For more information on how to write some commonly used SQL functions, reference, "Common SQL Connector Functions."

Further Reading


Did you find what you were looking for?

You can also head to community.tulip.co to post your question or see if others have faced a similar question!


Was this article helpful?