🐘 Radikant-PostgreSQL-C

 connector in C

Device Screen
postgresql logo 600x275

Introduction

Radikant PostgreSQL is a c library prototype that can connect directly to a PQ server. It depends on Radikant TLS and Socket for connection and  Radikant SASL for auth mechanism. A quasi SQL c strucuture is provided to make queryies readible. 

Login

FileEditViewProject
main.c — Radikant-PostgreSQL-C
// Radikant
#include <radikant-postgresql-c.h>
 
int main(){
printf("Test PQ (Radikant-PostgreSQL-C)\n");
printf("Connecting to 127.0.0.1:5432...\n");
 
pg_conn_t *conn = pq_connect("127.0.0.1", 5432);
pq_error_t err = pq_login(conn, "postgres", "mysecretpassword", "postgres");
 
return err;
}
Test PQ (Radikant-PostgreSQL-C) Connecting to 127.0.0.1:5432... 16:15:07 [INFO] Updated transcript. Total size: 146 16:15:07 [INFO] Sending Client Hello (146 bytes)... 16:15:07 [INFO] --- Handshake Loop: Waiting in state: CLIENT_WAIT_SERVER_HELLO (1) --- 16:15:07 [INFO] Updated transcript. Total size: 236 16:15:07 [INFO] Parsing Server Hello (86 bytes)... 16:15:07 [INFO] Legacy Version: 0x0303 (Ignored in TLS 1.3) 16:15:07 [INFO] Cipher Suite: TLS_AES_256_GCM_SHA384 (0x1302) 16:15:07 [SUCCESS] Handshake Complete! Connected to 127.0.0.1:5432 (TLS: Yes) Connection established (SSL)! Logging in... 16:15:07 [INFO] Encrypting 41 bytes for record type 23 [Auth] Authenticating with: SASL [Auth] Server offered SASL mechanisms: - SCRAM-SHA-256-PLUS - SCRAM-SHA-256 [Auth] SCRAM-SHA-256-PLUS available, but SASL library does not support it yet. Falling back to SCRAM-SHA-256. [Auth] Client selected SASL mechanism: SCRAM-SHA-256 [Auth] Authenticating with: SASL Final [Auth] Server Signature Verified (Channel Binding Successful!) [Auth] Authenticating with: Authentication OK Login Successful! 16:15:07 [INFO] Encrypting 2 bytes for record type 21

Connecting is a two step rocket, the pq_connect() function will establish a TLS connection to the PQ server, pq_login() will actually authenticate the user using SASL.

Query Insert

FileEditViewProject
insert_ops.c — Radikant-PostgreSQL-C
// Insert
pg_query_params_t insert_q = {
.type = PG_QUERY_INSERT,
.table = "public.employees",
.fields = "age, name, email",
.values = "'35', 'Sanne', 'sanne@radikant.com'"
};
 
pg_result_t *res = pg_query_execute(conn, &insert_q);
pq_clear(res);
16:44:37 [INFO] Inserted new employee 'Sanne' into public.employees.

This lib has Query Builder API that abstracts SQL command construction into a  pg_query_params_t struct. Instead of writing raw SQL strings, you can define the query's semantic components allowing the library to generate and execute the corresponding SQL statement.

Query Select

FileEditViewProject
query_ops.c — Radikant-PostgreSQL-C
// Select
pg_query_params_t q = {
.type = PG_QUERY_SELECT,
.table = "public.employees",
.fields = "name,age,email",
.sort = "name ASC",
.page = 1,
.per_page = 50
};
 
printf("Executing table query on '%s'...\n", q.table);
 
pg_table_t *table = pg_query_table(conn, &q);
 
if (table) {
pg_print_table(table);
pg_free_table(table);
}
Executing table query on 'public.employees'... 17:32:26 [INFO] Encrypting 1 bytes for record type 23 17:32:26 [INFO] Encrypting 4 bytes for record type 23 17:32:26 [INFO] Encrypting 72 bytes for record type 23 Query returned 39 rows, 3 columns: name (TEXT) age (INT8) email (TEXT) ------------ ----------- ------------------- Anna 30 anna@radikant.com Sanne 35 sanne@radikant.com Eva 32 eva@radikant.com Executing table query (all fields) on 'public.employees'...

The pg_table_t structure provides a convenient, column-oriented abstraction for PostgreSQL query results. It simplifies data manipulation by grouping values by column and is supported by high-level functions such as pg_query_table, which executes a query and returns a fully populated, typed table in a single step. With built-in utilities for memory management and visualization (like pg_print_table), it significantly reduces the boilerplate code typically required to parse and inspect database records in C.