feat: postgreact: hello world

This commit is contained in:
2025-04-15 18:23:42 +00:00
parent 1612799fb7
commit e607bf09fa
16 changed files with 193 additions and 7295 deletions

View File

@@ -0,0 +1,41 @@
{
lib,
stdenv,
postgresql,
...
}:
stdenv.mkDerivation {
pname = "postgreact";
version = "0.1";
src = ./.;
buildInputs = [
postgresql
];
buildPhase = ''
mkdir -p target
sh ./make.sh build
'';
installPhase = ''
mkdir -p $out/lib/postgresql $out/share/postgresql/extension
# Install compiled library
install -m 755 -D target/postgreact.so $out/lib/postgresql/postgreact.so
# Install control and SQL files
install -m 644 -D postgreact.control $out/share/postgresql/extension/postgreact.control
install -m 644 -D postgreact--0.1.sql $out/share/postgresql/extension/postgreact--0.1.sql
'';
meta = with lib; {
description = "PostgreSQL extension for reactive functions";
homepage = "https://github.com/yukkop/util.nix";
license = licenses.mit;
platforms = postgresql.meta.platforms;
maintainers = with maintainers; [ ];
};
}

102
package/c/postgreact/make.sh Executable file
View File

@@ -0,0 +1,102 @@
#!/bin/sh
# Usage: make.sh [build|watch] [--debug] [--color]
# Options:
# build Build the postgres extension (default if no mode is provided).
# watch Build the extension and watch for changes.
# --debug Build with -O0 (debug mode).
# --color Pass -fdiagnostics-color=always to compiler.
# help, --help Show this help message.
check_dependencies() {
for dep in gcc pg_config; do
if ! command -v "$dep" >/dev/null 2>&1; then
echo "Error: Required dependency '$dep' not found." >&2
exit 1
fi
done
# Check for either fswatch or inotifywait for watch mode
if [ "$MODE" = "watch" ] && ! command -v fswatch >/dev/null 2>&1 && ! command -v inotifywait >/dev/null 2>&1; then
echo "Error: Neither fswatch nor inotifywait found. Please install one of them." >&2
echo " On macOS: brew install fswatch" >&2
echo " On Linux: sudo apt install inotify-tools" >&2
exit 1
fi
}
print_help() {
cat <<EOF
Usage: $0 [build|watch] [--debug] [--color]
build Build the postgres extension (default).
watch Build the extension and watch for changes.
--debug Build with debug flags (-O0).
--color Force colored compiler diagnostics.
help, --help Display this help message.
EOF
}
# Show help if requested
case "$1" in
help|--help)
print_help
exit 0
;;
esac
# Default flags
OPTFLAGS="-O2"
CFLAGS="-Wall -Wextra -pedantic -fPIC"
COLOR_FLAG=""
DEBUG=0
# Process options
while [ $# -gt 0 ]; do
case "$1" in
--debug)
OPTFLAGS="-O0 -gdwarf-2 -g3 -Wno-error"
DEBUG=1
;;
--color)
COLOR_FLAG="-fdiagnostics-color=always"
;;
*)
break
;;
esac
shift
done
MODE="${1:-build}"
shift 2> /dev/null
if [ -n "$COLOR_FLAG" ]; then
CFLAGS="$CFLAGS $COLOR_FLAG"
fi
check_dependencies
# Get PostgreSQL include directory
PG_INCLUDE=$(pg_config --includedir-server)
PG_LIBDIR=$(pg_config --libdir)
case "$MODE" in
watch)
find . -type d | nix run .#watch -- 'sh ./make.sh build' -p '*.c' -p '*.h' 2>&1
;;
build)
mkdir -p target
echo "# Building PostgreSQL extension"
# shellcheck disable=SC2086
gcc $CFLAGS $OPTFLAGS -I$PG_INCLUDE -shared -o target/postgreact.so postgreact.c
# Copy extension files to target directory
cp postgreact.control target/
cp postgreact--0.1.sql target/
echo "Build complete. Files available in target/ directory."
;;
*)
print_help
exit 1
;;
esac

View File

@@ -0,0 +1,8 @@
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION postgreact" to load this file. \quit
-- Define the hello function that uses our C implementation
CREATE FUNCTION hello()
RETURNS text
AS 'postgreact', 'hello'
LANGUAGE C STRICT;

View File

@@ -0,0 +1,16 @@
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h" /* for text_to_cstring and cstring_to_text */
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/* Define the function hello */
PG_FUNCTION_INFO_V1(hello);
/* Implement the function */
Datum hello(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(cstring_to_text("Hello, world!"));
}

View File

@@ -0,0 +1,3 @@
comment = 'My first extension'
default_version = '0.1'
module_pathname = '$libdir/postgreact'