Connecting To HANA
Let's start by creating a new binary project and then configuring Cargo.toml to include the hdbconnect dependency.
cargo new hana_sample
[dependencies]
hdbconnect = "0.25"
Now that hdbconnect has been added as a dependency, we can connect to HANA using the Connection type as shown below in listing 1.
Listing 1
use hdbconnect::Connection;
fn main() {
let user = "USER_NAME";
let password = "PASSWORD";
let host = "HOST:PORT";
let mut connection = Connection::new(format!("hdbsql://{user}:{password}@{host}")).unwrap();
}
Executing Queries
The Connection type has several methods for executing queries, one of which is the query() method. This method takes an SQL statement as an str and returns a Result. If the Result is successful the Ok variant will contain a ResultSet. You can then use the ResultSet's try_into() method to translate the returned data into Rust types that implement serde::Deserialize.
Let's take a look at a few examples. The query in listing 2 below returns a single row with one value, which is deserialized into a String.
Listing 2
...
let result : String = connection.query("SELECT NOW() FROM DUMMY").unwrap().try_into().unwrap();
Queries that return a single row with multiple columns can be deserialized into a tuple.
Listing 3
let result : (String, String) = connection.query("SELECT CURRENT_DATE, CURRENT_TIME FROM DUMMY").unwrap().try_into().unwrap();
println!("{} {}", result.0, result.1);
In the code samples above, date/time values are deserialized into Strings. You can however use the HanaDate/HanaTime types that provide convenient methods to work with dates and times.
Listing 4
use hdbconnect::time::{ HanaDate, HanaTime };
...
let result : (HanaDate, HanaTime) = connection.query("SELECT CURRENT_DATE, CURRENT_TIME FROM DUMMY").unwrap().try_into().unwrap();
println!("Day : {}, Hour : {}", result.0.day(), result.1.hour());
Queries that return multiple rows can be deserialized into a vector that can then be used to iterate over the rows.
Listing 5
let result : Vec<(String, String)> = connection.query("SELECT SCHEMA_NAME, TABLE_NAME FROM SYS.TABLES LIMIT 5").unwrap().try_into().unwrap();
for row in result{
println!("{} - {}", row.0, row.1);
}
Alternatively, as the ResultSet type implements the Iterator trait, we can rewrite the code above as the following.
Listing 6
for result in connection.query("SELECT SCHEMA_NAME, TABLE_NAME FROM SYS.TABLES LIMIT 5").unwrap(){
let data : (String, String) = result.unwrap().try_into().unwrap();
println!("{} - {}", data.0, data.1);
}
Queries that return null values need to be handled correctly as Rust does not support NULL. For example, the code below will generate an error causing the program to crash with the following error thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Deserialization { source: ValueType("The value
Listing 7
let result : String = connection.query("SELECT NULL FROM DUMMY").unwrap().try_into().unwrap();
We can fix this error either by returning a default value using the IFNULL function in the SQL query or deserializing the value into an Option enum type as shown below.
Listing 8
// The query returns an empty string if the column value is null
let result : String = connection.query("SELECT IFNULL(NULL, '') FROM DUMMY").unwrap().try_into().unwrap();
// Deserialize the value into an Option enum type
let result : Option<String> = connection.query("SELECT NULL FROM DUMMY").unwrap().try_into().unwrap();
-
JSON_TABLE Function
In this article, we'll take a look at how to use the ++JSON_TABLE++ function to represent JSON data stored in a regular column as a relational table.
31 March 2023 - 2405 views -
If Table Exists Function
In this article, I explain how to create a function to determine if a table exists in SAP HANA.
31 October 2019 - 8594 views -
Connect To HANA DB Using NodeJs
This article explains how to connect to a SAP HANA system using the Node HANA client library.
28 October 2019 - 6963 views -
Connect To HANA DB Using Python
This article explains how to connect to a SAP HANA system using the Python hdbcli package.
11 October 2018 - 7730 views -
Adding Auto Numbers To Table Columns
This article explains how to create an auto generated column using IDENTITY.
14 March 2018 - 5956 views -
Connecting To SAP HANA Using PHP ODBC
This article describes how to install and configure PHP odbc to connect to a SAP HANA system on Ubuntu 16.
01 December 2017 - 7485 views -
Connect To HANA DB Using Java
This article describes how to connect to a HANA DB system using the ngdbc.jar Java driver.
03 November 2017 - 5244 views