Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix pl/pgsql #123

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions cibuild/linkweb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,6 @@ else

fi

# ../../src/common/libpgcommon_shlib.a"
# ./src/common/libpgcommon.a: binary file matches
# ./src/common/libpgcommon_shlib.a: binary file matches
# error: undefined symbol: fsync_pgdata (referenced by root reference (e.g. compiled C/C++ code))
# error: undefined symbol: get_restricted_token (referenced by root reference (e.g. compiled C/C++ code))
# error: undefined symbol: pg_malloc_extended (referenced by root reference (e.g. compiled C/C++ code))
# error: undefined symbol: pg_realloc (referenced by root reference (e.g. compiled C/C++ code))
# error: undefined symbol: pg_strdup (referenced by root reference (e.g. compiled C/C++ code))
# error: undefined symbol: simple_prompt (referenced by root reference (e.g. compiled C/C++ code))



## \
# /opt/python-wasm-sdk/devices/emsdk/usr/lib/libxml2.a \
# /opt/python-wasm-sdk/devices/emsdk/usr/lib/libgeos.a \
# /opt/python-wasm-sdk/devices/emsdk/usr/lib/libgeos_c.a \
# /opt/python-wasm-sdk/devices/emsdk/usr/lib/libproj.a"

# /data/git/pglite-build/pglite/postgres/libgeosall.so
# /data/git/pglite-build/pglite/postgres/libduckdb.so"


# ? -sLZ4=1 -sENVIRONMENT=web
# -sSINGLE_FILE => Uncaught SyntaxError: Cannot use 'import.meta' outside a module (at postgres.html:1:6033)
# -sENVIRONMENT=web => XHR
Expand All @@ -115,7 +93,7 @@ EMCC_WEB="-sNO_EXIT_RUNTIME=1 -sFORCE_FILESYSTEM=1"
if ${PGES6:-true}
then
# es6
MODULE="-g0 -Os -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module --shell-file ${GITHUB_WORKSPACE}/tests/repl.html"
MODULE="-g0 -O2 -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module --shell-file ${GITHUB_WORKSPACE}/tests/repl.html"
else
# local debug fast build
MODULE="-g3 -O0 -sMODULARIZE=0 -sEXPORT_ES6=0 --shell-file ${GITHUB_WORKSPACE}/tests/repl.html"
Expand Down Expand Up @@ -214,7 +192,6 @@ emcc $EMCC_WEB -fPIC -sMAIN_MODULE=2 \
mkdir -p ${WEBROOT}

cp -v postgres.* ${WEBROOT}/
#cp ${PGROOT}/lib/libecpg.so ${WEBROOT}/
cp ${PGROOT}/sdk/*.tar ${WEBROOT}/
for tarf in ${WEBROOT}/*.tar
do
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"pnpm":{"patchedDependencies":{"[email protected].0":"patches/[email protected].0.patch"}}}
{"pnpm":{"patchedDependencies":{"[email protected].1":"patches/[email protected].1.patch"}}}
2 changes: 1 addition & 1 deletion packages/pglite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"comlink": "^4.4.1",
"concurrently": "^8.2.2",
"http-server": "^14.1.1",
"pg-protocol": "^1.6.0",
"pg-protocol": "^1.6.1",
"playwright": "^1.42.1",
"prettier": "3.2.5",
"tinytar": "^0.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/scripts/bundle-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function findAndReplaceInDir(
async function main() {
await fs.copyFile("./release/postgres.wasm", "./dist/postgres.wasm");
await fs.copyFile("./release/postgres.data", "./dist/postgres.data");
// await fs.copyFile("./release/postgres.so", "./dist/postgres.so");
await fs.copyFile("./release/postgres.js", "./dist/postgres.js");
await fs.copyFile("./release/vector.tar.gz", "./dist/vector.tar.gz");
await findAndReplaceInDir(
"./dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/extensionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tinyTar from "tinytar";
import { IN_NODE } from "./utils.js";
import type { PostgresMod } from "./postgres.js";
import type { PostgresMod } from "./postgresMod.js";

export async function loadExtensionBundle(
bundlePath: URL,
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/idbfs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FilesystemBase } from "./types.js";
import type { FS, PostgresMod } from "../postgres.js";
import type { FS, PostgresMod } from "../postgresMod.js";
import { PGDATA } from "./index.js";
import { dumpTar } from "./tarUtils.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/memoryfs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FilesystemBase } from "./types.js";
import type { PostgresMod, FS } from "../postgres.js";
import type { PostgresMod, FS } from "../postgresMod.js";
import { dumpTar } from "./tarUtils.js";

export class MemoryFS extends FilesystemBase {
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/nodefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import { FilesystemBase } from "./types.js";
import { PGDATA } from "./index.js";
import type { PostgresMod, FS } from "../postgres.js";
import type { PostgresMod, FS } from "../postgresMod.js";
import { dumpTar } from "./tarUtils.js";

export class NodeFS extends FilesystemBase {
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/tarUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { tar, untar, type TarFile, REGTYPE, DIRTYPE } from "tinytar";
import { FS } from "../postgres.js";
import { FS } from "../postgresMod.js";
import { PGDATA } from "./index.js";

export async function dumpTar(FS: FS, dbname?: string): Promise<File | Blob> {
Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/fs/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PostgresMod, FS } from "../postgres.js";
import type { PostgresMod, FS } from "../postgresMod.js";

export type FsType = "nodefs" | "idbfs" | "memoryfs";

Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Mutex } from "async-mutex";
import PostgresModFactory, { type PostgresMod } from "./postgres.js";
import PostgresModFactory, { type PostgresMod } from "./postgresMod.js";
import { type Filesystem, parseDataDir, loadFs } from "./fs/index.js";
import { makeLocateFile } from "./utils.js";
import { parseResults } from "./parse.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ts-ignore
import PostgresModFactory from "../release/postgres.js";
// This file in placed in the build from ../release/postgres.js
import PostgresModFactory from "./postgres.js";

// Uses the types from @types/emscripten

Expand Down
2 changes: 1 addition & 1 deletion packages/pglite/tests/pgvector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test("pgvector", async (t) => {
},
{
name: "vec",
dataTypeID: 12772,
dataTypeID: 12345,
},
{
name: "distance",
Expand Down
44 changes: 44 additions & 0 deletions packages/pglite/tests/plpgsql.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import test from "ava";
import { PGlite } from "../dist/index.js";

test("can create and call function", async (t) => {
const db = new PGlite();
await db.exec(`
CREATE EXTENSION IF NOT EXISTS plpgsql;
CREATE OR REPLACE FUNCTION test_func()
RETURNS TEXT AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
`);

const res = await db.query("SELECT test_func();");
t.is(res.rows[0].test_func, "test");
});

test("can create and call complex function", async (t) => {
const db = new PGlite();
await db.exec(`
CREATE EXTENSION IF NOT EXISTS plpgsql;
CREATE OR REPLACE FUNCTION calculate_factorial(n INT) RETURNS INT AS $$
DECLARE
result INT := 1;
BEGIN
IF n < 0 THEN
RAISE EXCEPTION 'The input cannot be negative.';
ELSIF n = 0 OR n = 1 THEN
RETURN result;
ELSE
FOR i IN 2..n LOOP
result := result * i;
END LOOP;
RETURN result;
END IF;
END;
$$ LANGUAGE plpgsql;
`);

const res = await db.query("SELECT calculate_factorial(5) AS result;");
t.is(res.rows[0].result, 120);
});
3 changes: 2 additions & 1 deletion packages/pglite/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath } from "url";
const thisFile = fileURLToPath(new URL(import.meta.url));
const root = path.dirname(thisFile);

let replaceAssertPlugin = {
const replaceAssertPlugin = {
name: "replace-assert",
setup(build: any) {
// Resolve `assert` to a blank file
Expand All @@ -31,6 +31,7 @@ export default defineConfig({
},
clean: true,
format: ["esm"],
external: ["./postgres.js"],
esbuildOptions(options, context) {
options.inject = [
"src/polyfills/buffer.ts",
Expand Down
Loading
Loading