Swiftybase
Function StackDatabaseMysqlExamples

First Or Null Query

Example 1: Select First Or Null

In this example, we'll use the MySQL Execute function with the Type set to First Or Null to fetch a single record from MySQL.

Add MySQL Execute Function

Start by adding the MySQL Execute function. Give it a Result Name and set the Type to First Or Null.

Write the SQL Query

In the function configuration, write the SQL query you want to use to fetch data.

Assume we have a users table and want to fetch a user by their id. The users table schema looks like this:

CREATE TABLE `users` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `email` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `age` int unsigned NOT NULL,
  `activated` tinyint(1) NOT NULL DEFAULT '1',
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

Now write the following query:

SELECT email FROM users WHERE id = ?

Fill the Arguments

Provide the value for the id parameter.

[7]

Define a Type

Next, define a primitive type like String to hold the email of the user.

Check SqlRes

Map Struct Fields to SQL Query

Since you're selecting only one field from the database, you don't need to map fields. Just provide an empty List[string] value.

Testing

Once everything is configured, the result (if found) will be available in the Data field as an optional value type. You should check the Found property to determine whether the data exists.

If no data is found, the result will be empty and the Found field will be false.

When Found is false, the value is excluded from encoding. However, in the stack, you can still check this flag to confirm the data was not found.

On this page