feat(structure): geometry subtype and srid editor for postgres
This commit is contained in:
15 files changed
+329
-3
No files matched your search
+98
@@ -0,0 +1,98 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { combinePostgresGeometryType, isPostgresGeometryDataType, POSTGRES_GEOMETRY_TYPES, postgresGeometrySridValue, postgresGeometryTypeValue } from "@/lib/table/tableStructureEditorState";
|
||||
|
||||
describe("postgres geometry column typmod helpers", () => {
|
||||
describe("combinePostgresGeometryType", () => {
|
||||
it("returns bare type when both sub-type and SRID are empty", () => {
|
||||
expect(combinePostgresGeometryType("geometry", "", "")).toBe("geometry");
|
||||
});
|
||||
|
||||
it("emits geometry(Point) for sub-type only", () => {
|
||||
expect(combinePostgresGeometryType("geometry", "Point", "")).toBe("geometry(Point)");
|
||||
});
|
||||
|
||||
it("emits geometry(GEOMETRY,4326) for SRID only (geometry(,4326) and geometry(4326) are rejected by PostGIS)", () => {
|
||||
expect(combinePostgresGeometryType("geometry", "", "4326")).toBe("geometry(GEOMETRY,4326)");
|
||||
});
|
||||
|
||||
it("emits geometry(Point,4326) for both", () => {
|
||||
expect(combinePostgresGeometryType("geometry", "Point", "4326")).toBe("geometry(Point,4326)");
|
||||
});
|
||||
|
||||
it("trims whitespace in inputs", () => {
|
||||
expect(combinePostgresGeometryType(" geography ", " Polygon ", " 3857 ")).toBe("geography(Polygon,3857)");
|
||||
});
|
||||
|
||||
it("emits geography(GEOMETRY,4326) for geography SRID only (verified against PostGIS)", () => {
|
||||
expect(combinePostgresGeometryType("geography", "", "4326")).toBe("geography(GEOMETRY,4326)");
|
||||
});
|
||||
|
||||
it("returns empty string when base type is empty", () => {
|
||||
expect(combinePostgresGeometryType("", "Point", "4326")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("postgresGeometryTypeValue / postgresGeometrySridValue", () => {
|
||||
it("parses geometry(Point,4326)", () => {
|
||||
expect(postgresGeometryTypeValue("geometry(Point,4326)")).toBe("Point");
|
||||
expect(postgresGeometrySridValue("geometry(Point,4326)")).toBe("4326");
|
||||
});
|
||||
|
||||
it("parses geography(Polygon,3857)", () => {
|
||||
expect(postgresGeometryTypeValue("geography(Polygon,3857)")).toBe("Polygon");
|
||||
expect(postgresGeometrySridValue("geography(Polygon,3857)")).toBe("3857");
|
||||
});
|
||||
|
||||
it("parses bare geometry with no typmod", () => {
|
||||
expect(postgresGeometryTypeValue("geometry")).toBe("");
|
||||
expect(postgresGeometrySridValue("geometry")).toBe("");
|
||||
});
|
||||
|
||||
it("parses whitespace variant geometry( Point , 4326 )", () => {
|
||||
expect(postgresGeometryTypeValue("geometry( Point , 4326 )")).toBe("Point");
|
||||
expect(postgresGeometrySridValue("geometry( Point , 4326 )")).toBe("4326");
|
||||
});
|
||||
|
||||
it("parses sub-type-only geometry(Point)", () => {
|
||||
expect(postgresGeometryTypeValue("geometry(Point)")).toBe("Point");
|
||||
expect(postgresGeometrySridValue("geometry(Point)")).toBe("");
|
||||
});
|
||||
|
||||
it("parses geometry(GEOMETRY,4326) as blank (GEOMETRY = any sub-type, normalized to empty)", () => {
|
||||
expect(postgresGeometryTypeValue("geometry(GEOMETRY,4326)")).toBe("");
|
||||
expect(postgresGeometrySridValue("geometry(GEOMETRY,4326)")).toBe("4326");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isPostgresGeometryDataType", () => {
|
||||
it("returns true for postgres + geometry", () => {
|
||||
expect(isPostgresGeometryDataType("postgres", "geometry(Point,4326)")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for postgres + bare geography", () => {
|
||||
expect(isPostgresGeometryDataType("postgres", "geography")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for gaussdb (Postgres family)", () => {
|
||||
expect(isPostgresGeometryDataType("gaussdb", "geometry(Point,4326)")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for mysql even with a geometry type string", () => {
|
||||
expect(isPostgresGeometryDataType("mysql", "geometry")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for postgres non-spatial types", () => {
|
||||
expect(isPostgresGeometryDataType("postgres", "varchar(255)")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
it("POSTGRES_GEOMETRY_TYPES includes the OGC simple-feature sub-types", () => {
|
||||
expect(POSTGRES_GEOMETRY_TYPES).toContain("Point");
|
||||
expect(POSTGRES_GEOMETRY_TYPES).toContain("LineString");
|
||||
expect(POSTGRES_GEOMETRY_TYPES).toContain("Polygon");
|
||||
expect(POSTGRES_GEOMETRY_TYPES).toContain("MultiPoint");
|
||||
expect(POSTGRES_GEOMETRY_TYPES).toContain("MultiPolygon");
|
||||
expect(POSTGRES_GEOMETRY_TYPES).toContain("GeometryCollection");
|
||||
});
|
||||
});
|
||||
@@ -1470,3 +1470,63 @@ export function buildStructureTargetLabel(connectionName: string | undefined, da
|
||||
if (tableName) parts.push(tableName);
|
||||
return parts.filter(Boolean).join(" / ");
|
||||
}
|
||||
|
||||
/** PostGIS `geometry(...)`/`geography(...)` typmod accepts these geometry sub-type
|
||||
* names (case-insensitive). An empty value means "no sub-type constraint". */
|
||||
export const POSTGRES_GEOMETRY_TYPES: readonly string[] = ["Point", "LineString", "Polygon", "MultiPoint", "MultiLineString", "MultiPolygon", "GeometryCollection", "CircularString", "CompoundCurve", "CurvePolygon", "MultiCurve", "MultiSurface", "PolyhedralSurface", "TIN", "Triangle"];
|
||||
|
||||
const POSTGRES_SPATIAL_TYPES = new Set(["geometry", "geography"]);
|
||||
const POSTGRES_LIKE_DATABASES = new Set<DatabaseType | undefined>(["postgres", "gaussdb", "kwdb", "opengauss", "highgo", "uxdb", "vastbase", "kingbase"]);
|
||||
|
||||
/** Whether a column is a PostGIS `geometry`/`geography` on a PostgreSQL-family
|
||||
* database — the case where the structure editor shows dedicated geometry-type
|
||||
* and SRID controls instead of the generic length input. */
|
||||
export function isPostgresGeometryDataType(dbType: DatabaseType | undefined, rawDataType: string): boolean {
|
||||
if (!POSTGRES_LIKE_DATABASES.has(dbType)) return false;
|
||||
const { baseType } = splitDataType(rawDataType);
|
||||
return POSTGRES_SPATIAL_TYPES.has(baseType.trim().toLowerCase());
|
||||
}
|
||||
|
||||
/** Geometry sub-type parsed from `geometry(Point,4326)` → `"Point"`. Returns empty
|
||||
* for bare `geometry` (no typmod) and for `geometry(GEOMETRY,srid)` — the latter is
|
||||
* PostGIS's storage form of "any sub-type" (what a blank sub-type + SRID compiles to),
|
||||
* normalized back to empty to match the user-facing "leave blank" intent. This keeps
|
||||
* the clear (X) button visually emptying the field instead of showing "GEOMETRY".
|
||||
* `geometry(,4326)` is invalid and never produced. */
|
||||
export function postgresGeometryTypeValue(rawDataType: string): string {
|
||||
const { params } = splitDataType(rawDataType);
|
||||
const geomType = splitPostgresGeometryParams(params).geomType;
|
||||
return geomType.toLowerCase() === "geometry" ? "" : geomType;
|
||||
}
|
||||
|
||||
/** SRID parsed from `geometry(Point,4326)` → `"4326"`. Empty when unspecified. */
|
||||
export function postgresGeometrySridValue(rawDataType: string): string {
|
||||
const { params } = splitDataType(rawDataType);
|
||||
return splitPostgresGeometryParams(params).srid;
|
||||
}
|
||||
|
||||
function splitPostgresGeometryParams(params: string): { geomType: string; srid: string } {
|
||||
const trimmed = params.trim();
|
||||
if (!trimmed) return { geomType: "", srid: "" };
|
||||
const commaIndex = trimmed.indexOf(",");
|
||||
if (commaIndex === -1) return { geomType: trimmed, srid: "" };
|
||||
return { geomType: trimmed.slice(0, commaIndex).trim(), srid: trimmed.slice(commaIndex + 1).trim() };
|
||||
}
|
||||
|
||||
/** Reassemble a PostGIS spatial type string from its parts.
|
||||
*
|
||||
* - both empty → bare `geometry` (no typmod)
|
||||
* - sub-type only → `geometry(Point)`
|
||||
* - SRID only → `geometry(GEOMETRY,4326)` (PostGIS rejects `geometry(,4326)` (syntax error at `,`) and `geometry(4326)` ("Invalid geometry type modifier"); `GEOMETRY` is a valid sub-type token meaning any geometry, so it is the canonical way to constrain SRID only)
|
||||
* - both → `geometry(Point,4326)`
|
||||
*/
|
||||
export function combinePostgresGeometryType(baseType: string, geomType: string, srid: string): string {
|
||||
const type = baseType.trim();
|
||||
const geom = geomType.trim();
|
||||
const sridValue = srid.trim();
|
||||
if (!type) return "";
|
||||
if (!geom && !sridValue) return type;
|
||||
if (!geom) return `${type}(GEOMETRY,${sridValue})`; // PostGIS rejects geometry(,4326) and geometry(4326); GEOMETRY = any sub-type, constrains SRID only
|
||||
if (!sridValue) return `${type}(${geom})`;
|
||||
return `${type}(${geom},${sridValue})`;
|
||||
}
|
||||
Reference in new issue
Block a user