-- type definition

ALTER FUNCTION unit_in(cstring)
	SET search_path = @extschema@;

-- prefix/unit definition tables

CREATE TABLE unit_prefixes (
	prefix varchar(32) PRIMARY KEY,
	factor double precision NOT NULL,
	definition text, -- original definition, informational
	dump boolean DEFAULT true
);

SELECT pg_catalog.pg_extension_config_dump('unit_prefixes', 'WHERE dump');

COPY unit_prefixes (prefix, factor, definition, dump) FROM '@MODULEDIR@/unit_prefixes.data';

CREATE TABLE unit_units (
	name varchar(32) PRIMARY KEY,
	unit unit NOT NULL,
	shift double precision, -- NULL means 0.0 here
	definition text, -- original definition, informational
	dump boolean DEFAULT true
);

SELECT pg_catalog.pg_extension_config_dump('unit_units', 'WHERE dump');

COPY unit_units (name, unit, shift, definition, dump) FROM '@MODULEDIR@/unit_units.data';

-- operators

CREATE FUNCTION sqrt(unit)
	RETURNS unit
	AS '$libdir/unit', 'unit_sqrt'
	LANGUAGE C IMMUTABLE STRICT;

CREATE OPERATOR |/ (
	rightarg = unit,
	procedure = sqrt
);

CREATE FUNCTION cbrt(unit)
	RETURNS unit
	AS '$libdir/unit', 'unit_cbrt'
	LANGUAGE C IMMUTABLE STRICT;

CREATE OPERATOR ||/ (
	rightarg = unit,
	procedure = cbrt
);

ALTER FUNCTION unit_at(unit, cstring)
	SET search_path = @extschema@;

-- internal functions

CREATE FUNCTION unit_is_hashed(cstring)
	RETURNS bool
	AS '$libdir/unit'
	LANGUAGE C VOLATILE STRICT;

CREATE FUNCTION unit_reset()
	RETURNS void
	AS '$libdir/unit'
	LANGUAGE C VOLATILE STRICT;
