How to use the Firebird database with Rust language

Luis Fernando Batels
ITNEXT
Published in
3 min readMay 8, 2021

--

Introduction

Firstly, I want to show you the Rust programming language. It is an open source language that delivers a platform without runtime or garbage collector, that is really fast and has an awesome memory management. Very important too, it has an excellent documentation.

And for data storage, we have the Firebird database. It has many features, is stable and very fast.

We have made the rsfbclient crate for providing a secure API to access the Firebird.

Versão em Português.

Setting up the environment

You must have already installed the rust toolkit. And, if you want to use the native firebird connector, you will also need the Firebird installed.

And then, you just need to create a new rust project using the cargo:

cargo new fbtest

Testing the project:

cd fbtest
cargo run

Choosing the client variation

The rsfbclient crate provides three implementations to access the database: dynamic linking, dynamic loading and pure rust. Which one is enabled by features linking, dynamic_loading and pure_rust.

By default, the crate uses the first option. Therefore, we just need to add it on the dependencies section of the Cargo.toml:

[dependencies]
rsfbclient = “0.16.0”

Testing the project:

cargo run

Preparing the project

On the main.rs we need to import the crate and change the main function to return the errors generated by it:

Testing the project:

cargo run

Database connection

After choosing the client variation, we need to choose the connection type. The crate has two options: connection builder or string:

Using connection builder:

Using string:

Fetching data from database

Now, let’s get some rows from the database. In our test, we will use the mon$attachments table. The rsfbclient provides an API in which we just need to declare the expected type, and it will be in charge for performing the cast and the other operations.

With the connection opened, we just need to call the query method. This method can be used on the following formats:

Tuples:

Row struct:

In this format, you can access the columns dynamically.

Inserting data

To insert data on the database, or execute any other operator without cursor, we need to use the execute method. E.g:

Like the query method, this method also supports parameters with the following formats:

Tuples:

Structs:

All structs that implements the IntoParams trait, can be used as a parameter.

Closing the connection

Thanks to the scoping rules of rust, the connection will be closed as soon as the connection variable get out of scope.

Nevertheless, you can use the close method to close the connection manually.

Considerations

This article just show a small amount of interactions between the language and the database. Therefore, some features and resources was left out, but you can see them on crate documentation. Furthermore, we have already implemented many examples.

To follow the rsfbclient development, you can access our repository: https://github.com/fernandobatels/rsfbclient

Thanks for your attention and see you next time!

--

--