feat(\db-tool\): introduce unified db-tool package with postgres harness and tests (T0-T8)

This commit is contained in:
2026-04-30 09:06:44 +00:00
parent 395bddee94
commit b5dcbf08a1
27 changed files with 2417 additions and 1 deletions

View File

@@ -0,0 +1,29 @@
{ stdenv, gcc, libpq, lib, bash }:
stdenv.mkDerivation {
pname = "parse-uri";
version = "1.0";
src = ./.;
doCheck = false;
nativeBuildInputs = [ gcc ];
buildInputs = [ libpq ];
INCLUDES = "-I${libpq.dev}/include";
LDFLAGS = "-L${libpq.out}/lib -lpq";
buildPhase = ''
${bash}/bin/sh ./make.sh build
'';
installPhase = ''
mkdir -p $out/bin
cp target/parse-uri $out/bin/
'';
meta = {
description = "parse-uri";
license = lib.licenses.mit;
};
}

32
package/parse-uri/main.c Normal file
View File

@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <libpq-fe.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("only 1 argument allow, please provide uri\n");
exit(1);
}
const char *conninfo = argv[1];
char *errmsg = NULL;
PQconninfoOption *options = PQconninfoParse(conninfo, &errmsg);
if (!options) {
printf("Parse failed: %s\n", errmsg);
return 1;
}
for (PQconninfoOption *opt = options; opt->keyword != NULL; opt++) {
char upper[128];
size_t i = 0;
for (; opt->keyword[i] != '\0' && i < sizeof(upper)-1; i++)
upper[i] = toupper((unsigned char)opt->keyword[i]);
upper[i] = '\0';
printf("URI_%s=%s\n", upper, opt->val ? opt->val : "");
}
PQconninfoFree(options);
return 0;
}

41
package/parse-uri/make.sh Normal file
View File

@@ -0,0 +1,41 @@
#!/bin/sh
# Usage: make.sh [build|check] [--norun] [--debug] [--color]
PACKAGE_NAME="parse-uri"
check_dependencies() {
for dep in cc; do
if ! command -v "$dep" >/dev/null 2>&1; then
echo "Error: Required dependency '$dep' not found." >&2
exit 1
fi
done
}
check_dependencies
# Default flags
OPTFLAGS="-O2"
CFLAGS="-Wall -Wextra -Werror -pedantic"
STD_FLAGS="-std=c99"
MODE="${1:-build}"
shift
build() {
mkdir -p target
echo "# Build $PACKAGE_NAME"
# shellcheck disable=SC2086
cc $CFLAGS $OPTFLAGS $STD_FLAGS main.c -o "target/$PACKAGE_NAME" $LDFLAGS $INCLUDES
}
case "$MODE" in
build)
build
;;
check)
echo "No tests to run"
;;
*)
exit 1
;;
esac