LightDB 是恒生电子股份有限公司研发的一款同时支持在线事务处理与在线分析处理的融合型分布式数据库产品 , 具备 SQL兼容性 高、 容量弹性伸缩 、金融级高可用、现 代硬件融合、纯 内存 计算 等核心特性 , 主要适用于 对 可用 性 、一致性要求较高 的系统。 由于是基于postgresql开发的,所以客户端开发完全兼容采用libpq库。示例程序包含了建表,及增,删,改,查等基本概念功能。
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
static void terminate(PGconn *conn)
{
if (conn != NULL)
{
fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
PQfinish(conn);
}
}
static int execute_sql(PGconn *conn, const char *sql)
{
int status = -1;
PGresult *result = PQexec(conn, sql);
/** Successful completion of a command returning no data */
if (PQresultStatus(result) == PGRES_COMMAND_OK)
{
status = 0;
}
PQclear(result);
return status;
}
static int create_table(PGconn *conn, const char *sql)
{
return execute_sql(conn, sql);
}
static int insert_data(PGconn *conn, const char *sql)
{
return execute_sql(conn, sql);
}
static int delete_data(PGconn *conn, const char *sql)
{
return execute_sql(conn, sql);
}
static int update_data(PGconn *conn, const char *sql)
{
return execute_sql(conn, sql);
}
static int select_data(PGconn *conn, const char *sql)
{
/** Set default status flag to error */
int status = -1;
PGresult *result = PQexec(conn, sql);
/** The query successfully executed */
if (PQresultStatus(result) == PGRES_TUPLES_OK)
{
/** Get result column length and row length */
int column_len = PQnfields(result);
int row_len = PQntuples(result);
int i, j;
for (i = 0; i < row_len; ++i)
{
for (j = 0; j < column_len; ++j)
{
printf("%-20s", PQgetvalue(result, i, j));
}
printf("\n");
}
/** Set successful flag */
status = 0;
}
PQclear(result);
return status;
}
int main(int argc, char **argv)
{
const char *conn_link = "host=127.0.0.1 port=5432 dbname=yanwf user=yanwf";
const char *create_table_sql = "CREATE SEQUENCE if not exists public.product_sequence start 1 increment 1;\
CREATE TABLE if not exists public.product (\
id SERIAL PRIMARY KEY,\
pname text NULL,\
price numeric NULL,\
ptype varchar(100) NULL\
);";
const char *insert_sql = "insert into public.product values (nextval('public.product_sequence'), 'iphone X', 6800, 'electric');";
const char *select_sql = "select * from public.product order by id";
const char *update_sql = "update public.product set price=6500 where id=1";
const char *delete_sql = "delete from public.product where id >5;";
PGconn *conn;
PGresult *result;
int status;
if (argc > 1)
{
conn_link = argv[1];
}
conn = PQconnectdb(conn_link);
if (PQstatus(conn) != CONNECTION_OK)
{
terminate(conn);
exit(1);
}
/** Set always secure search path */
result = PQexec(conn, "SELECT pg_catalog.set_config('search_path', '', false)");
status = PQresultStatus(result);
PQclear(result);
if (status != PGRES_TUPLES_OK)
{
terminate(conn);
exit(1);
}
const char *err_function = "";
do
{
status = create_table(conn, create_table_sql);
if (status != 0)
{
err_function = "create_table";
break;
}
status = insert_data(conn, insert_sql);
if (status != 0)
{
err_function = "insert_data";
break;
}
status = select_data(conn, select_sql);
if (status != 0)
{
err_function = "select_data";
break;
}
status = update_data(conn, update_sql);
if (status != 0)
{
err_function = "update_data";
break;
}
status = delete_data(conn, delete_sql);
if (status != 0)
{
err_function = "delete_data";
break;
}
} while(0);
/** Print error info */
if (status != 0)
{
fprintf(stderr, "%s failed to find, %s\n", err_function, PQerrorMessage(conn));
} else {
fprintf(stdout, "The query successfully completed\n");
}
/** Release pgsql link */
PQfinish(conn);
exit(status);
}
