From 7391f0f2ce4b66cad69902930a3064e16f5b9ec6 Mon Sep 17 00:00:00 2001 From: narawat Date: Fri, 24 Jul 2026 12:45:14 +0700 Subject: [PATCH] add execute_postgres_sql --- src/dbUtil.jl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/dbUtil.jl b/src/dbUtil.jl index 4e64ffb..4bd03a4 100644 --- a/src/dbUtil.jl +++ b/src/dbUtil.jl @@ -8,6 +8,36 @@ using ..util #[PENDING] update code to use JSON # ---------------------------------------------- 100 --------------------------------------------- # +""" Execute SQL against a PostgreSQL database using LibPQ connection string. + +# Arguments +- `pg_conn_str::AbstractString`: PostgreSQL connection string in format "host=... port=... dbname=... user=... password=..." +- `sql::AbstractString`: SQL query to execute + +# Returns +- `LibPQ.Result` on success, `nothing` on failure + +# Example +```julia +pg_conn_str = "host=localhost port=5432 dbname=mydb user=myuser password=mypass" +sql = "SELECT * FROM wine;" +result = execute_postgres_sql(pg_conn_str, sql) +``` +""" +function execute_postgres_sql(pg_conn_str::T, sql::T) where {T<:AbstractString} + db_connection = LibPQ.Connection(pg_conn_str) + result = nothing + try + result = LibPQ.execute(db_connection, sql) + catch e + @error e + LibPQ.close(db_connection) + end + + LibPQ.close(db_connection) + return result +end + """ dictToPostgresKeyValueString - Convert dictionary to PostgreSQL key-value string format