Hoel
C Database abstraction library with json based language
|
Database abstraction library written in C.
Simple and easy to use database access library. Works with SQLite 3, MariaDB and PostgreSQL databases. Uses a JSON-based language with jansson
to execute simple queries based on one table.
See the online documentation for a doxygen format of the API documentation.
Install Jansson library for JSON manipulation, minimum version 2.4. On a Debian-based platform, run the following command:
Install Hoel database dependencies based on your requirements:
libsqlite3-dev
libmariadb-dev
libpq-dev
Hoel is available in multiple distributions as official package. Check out your distribution documentation to install the package automatically.
You can install Hoel with a pre-compiled package available in the release pages. jansson
, sqlite3
, libmariadb-client
and libpq
development files packages are required to install Hoel. The packages files hoel-dev-full_*
contain the libraries orcania
, yder
and hoel
. It's also compiled with all the database backends.
For example, to install Hoel with the hoel-dev-full_2.3.0_Debian_stretch_x86_64.tar.gz
package downloaded on the releases
page, you must execute the following commands:
If there's no package available for your distribution, you can recompile it manually using CMake
or Makefile
.
CMake minimum 3.5 is required.
Last Hoel release: https://github.com/babelouest/hoel/releases/latest/
Run the CMake script in a sub-directory, example:
The available options for CMake are:
-DWITH_SQLITE3=[on|off]
(default on
): Enable/disable SQLite3 database backend-DWITH_MARIADB=[on|off]
(default on
): Enable/disable MariaDB database backend-DWITH_PGSQL=[on|off]
(default on
): Enable/disable PostgreSQL database backend-DWITH_JOURNALD=[on|off]
(default on
): Build with journald (SystemD) support for logging-DBUILD_STATIC=[on|off]
(default off
): Build the static archive in addition to the shared library-DBUILD_HOEL_TESTING=[on|off]
(default off
): Build unit tests-DBUILD_HOEL_DOCUMENTATION=[on|off]
(default off
): Build the documentation, doxygen is required-DINSTALL_HEADER=[on|off]
(default on
): Install header file hoel.h
-DBUILD_RPM=[on|off]
(default off
): Build RPM package when running make package
-DCMAKE_BUILD_TYPE=[Debug|Release]
(default Release
): Compile with debugging symbols or notClone, compile and install Orcania and Yder libraries.
Last Orcania release: https://github.com/babelouest/orcania/releases/latest/
Last Yder release: https://github.com/babelouest/yder/releases/latest/
Last Hoel release: https://github.com/babelouest/hoel/releases/latest/
By default, Hoel is compiled with the 3 databases support. If you don't need one or more database, follow these instructions
Add DISABLE_SQLITE=1 to the make
command:
Add DISABLE_MARIADB=1 to the make
command:
Add DISABLE_POSTGRESQL=1 to the make
command:
You can disable 2 databases backends to keep just one, simply add both parameters to the make
command:
By default, the shared library and the header file will be installed in the /usr/local
location. To change this setting, you can modify the DESTDIR
value in the src/Makefile
.
To use Hoel in your code, include the file hoel.h
.
Use the flag -lhoel
to include Hoel library in the linking process.
When specified, some functions return H_OK
on success, and other values otherwise. H_OK
is 0, other values are non-0 values. The defined errors list is the following:
Some function return allocated values. When the value is not a structure, you must use the function h_free
to clean it. Otherwise, use the dedicated functions.
To create a connection to a database, use its dedicated function
All these functions return a struct _h_connection * on success. This pointer will be needed on every call to Hoel functions.
When you no longer need your connection, close it using the function h_close_db
. This will close the connection to the database and free the memory allocated by the connection.
The connection must be cleaned when it's no longer needed.
If you need to escape parameters, you can use the functions h_escape_string
, the returned value must be h_free'd after use.
The meaning of existence of the function h_escape_string_with_quotes
is because of PostgreSQL ways to escape an unsafe string.
TL;DR:
h_escape_string_with_quotes
only if you use a PostgreSQL database.h_escape_string_with_quotes
and h_escape_string
if you use only SQLite3 and/or MariaDB databases.If the unsafe string contains backslashes "\"
, the escaped string will have the pattern ‘E’<safe>', otherwise it will have the pattern
'<safe>'. Since the function
h_escape_stringmust return the safe string only, without surroundings quotes
', the format
E'<safe>'` is incompatible with Hoel API.
Therefore, using h_escape_string
with PostgreSQL could lead to undefined behaviour and more importantly exploitable bugs.
To avoid this, on a PostgreSQL database, an escaped string using h_escape_string
that should return the format ‘E’<safe>'will return
NULL, and an escaped string using
h_escape_string_with_quoteswill return the format
E'<safe>'`.
When you need to run a query with a where clause using multiple parameters, such as ‘WHERE col1='a’ AND (col2='b' OR col3=5) AND col4=42.3, you can use the operator
raw`:
In some cases, you may need to build the where clause with multiple variables. In hoel 1.4.27, the function h_build_where_clause
was introduced to help that. Please note that this function is still in Beta.
Then, to build the where clause above using h_build_where_clause
, you can use the following code:
Note that if you use constant litteral for integer or double values, you should cast them first:
To execute a SQL query, you can use the function h_execute_query
which will run the query in the database specified by the parameter conn
. If a result
parameter is specified, the result of the query (if any) will be stored in the result
structure.
The struct _h_result
is a structure containing the values returned by a query. The definition of the structure is:
The data value is a 2 dimensional array with struct _h_data
variables. A struct _h_data
is defined as:
where type
can be the following values:
t_data
will point to a struct _h_type_*
corresponding to the type. The struct _h_type_*
available are:
To clean a result or a data structure, you can use its dedicated functions:
If you need the last id generated after an insert query, you can use the following function:
You can use additional functions for specific needs. All these function will use h_execute_query
but check input parameters before.
Hoel allows to use JSON objects for simple queries with jansson
library. In the simple JSON queries, a JSON object called json_t * j_query
is used to generate the query.
All json_t *
returned and updated values must be free after use.
A j_query
has the following form:
A where
clause is a JSON object containing a series of clauses. A clause can have 2 different forms:
col_name: value
col_name: {operator: "operator_value", value: value}
In the first case, col_name: value
, the clause becomes col_name = value
. Value is always escaped.
In the second case, col_name: {operator: "operator_value", value: value}
, depending on the operator
value, the clause can have different forms:
operator: "NOT NULL"
, the clause becomes col_name IS NOT NULL
operator: "raw"
, the value
value becomes the clause itself, not escaped, for example in ‘{ "operator": "raw", "value": "LIKE ’%value6%'" }, the clause becomes
col6 LIKE 'value6'
otherwise, the clause becomes
col_name operator value`, value is escapedAll clauses are separated by an AND
operator.
As en example, here is a JSON object and its generated where clause:
JSON object:
SQL Where clause:
If you need less simple clauses, you can build it on your own and use the h_execute_query
or the h_execute_query_json
functions.
The simple JSON queries functions are:
The function h_last_insert_id
returns the last inserted id in a json_t *
format.
See examples
folder for detailed sample source codes.