PanLex Database Functions

List of functions
Schema Name Result data type Argument data types Type Volatility Owner Language Source code Description
public amrm void integer, integer normal volatile pool plpgsql --create or replace function amrm
--(integer, integer)
--returns void language plpgsql as
declare
dnvar integer;
begin
-- Identify a denotation of the specified
-- approver with the specified expression.
select dn into dnvar from dn, mn
where ex = $1
and mn.mn = dn.mn
and ap = $2;
-- If it exists:
if dnvar is not null
then
---- Delete the denotation.
perform dnrm (dnvar, true, false);
end if;
end;
In: ex of an expression and ap of an approver. Act: Delete an arbitrary denotation, if any, of the approver with the expression and the denotation’s word classifications, metadata, and if orphaned meaning. Use: To prepare an expression in an ambiguity-prohibiting variety for assignment of a meaning to it.
public apad integer text, text, text, text, text, text, integer, integer, integer, text, text, text, text, text, text, integer normal volatile pool plpgsql --create or replace function apad
--(text, text, text, text, text, text, integer, integer,
--integer, text, text, text, text, text, text, integer)
--returns integer language plpgsql as
declare
apvar integer;
begin
-- If the label is blank:
if (length ($1)) = 0
then
---- Report the error and quit.
return -1;
end if;
-- Identify an available approver ID.
apvar := (apid ());
-- Add the specified approver.
insert into ap values
(
apvar, default, $1, $2, $3, $4, $5, $6,
$7, $8, $9, $10, $11, $12, $13, $14, $15
);
-- If the user is to be entitled to
-- edit the approver without being a
-- superuser:
if $16 > 0
then
---- Entitle the user to edit the approver.
insert into au values (apvar, $16);
end if;
-- Return the approver’s ID.
return apvar;
end;
In: tt, ur, bn, au, ti, pb, yr, uq, ui, ul, li, ip, co, ad, and fp of a new approver and us of a user that is to be entitled to edit the approver or 0 if none is. Act: Add the approver and, if so specified, make the user an editor of it. Out: -1 = tt blank, other integer = ap of the approver.
public apid integer   normal volatile pool sql --create or replace function apid ()
--returns integer language sql as
select min (ap) from (
(
select 1 as ap
union
select ap + 1 as ap from ap
)
except select ap from ap
) as avail;
Out: smallest available approver ap.
public appw boolean integer, integer, character normal volatile pool sql --create or replace function appw
--(integer, integer, character)
--returns boolean language sql as
select
($3 = (select pw from us where us = $2))
and (
(select ad from us where us = $2)
or ($2 = (select us from au where ap = $1 and us = $2))
);
In: ap of an approver and us and pw of a user. Out: whether the password is the user’s and the user is either a superuser or an editor of the approver.
public aprm record integer, OUT mndel integer, OUT midel integer, OUT dfdel integer, OUT dmdel integer, OUT dndel integer, OUT wcdel integer, OUT mddel integer normal volatile pool plpgsql --create or replace function aprm
--(integer, out mndel integer,
--out midel integer, out dfdel integer,
--out dmdel integer, out dndel integer,
--out wcdel integer, out mddel integer)
--returns record language plpgsql as
declare
begin
-- Remove all word classifications of the
-- approver.
insert into wcid
select wc from wc, dn, mn
where ap = $1
and dn.mn = mn.mn
and wc.dn = dn.dn;
delete from wc
using dn, mn
where ap = $1
and dn.mn = mn.mn
and wc.dn = dn.dn;
-- Identify the number of deletions.
get diagnostics wcdel = row_count;
-- Remove all metadata of the approver.
insert into mdid
select md from md, dn, mn
where ap = $1
and dn.mn = mn.mn
and md.dn = dn.dn;
delete from md
using dn, mn
where ap = $1
and dn.mn = mn.mn
and md.dn = dn.dn;
-- Identify the number of deletions.
get diagnostics mddel = row_count;
-- Remove all denotations of the approver.
insert into dnid
select dn from dn, mn
where ap = $1
and dn.mn = mn.mn;
delete from dn
using mn
where ap = $1
and dn.mn = mn.mn;
-- Identify the number of deletions.
get diagnostics dndel = row_count;
-- Remove all meaning identifiers of the
-- approver.
delete from mi
using mn
where ap = $1
and mi.mn = mn.mn;
-- Identify the number of deletions.
get diagnostics midel = row_count;
-- Remove all definitions of the approver.
insert into dfid
select df from df, mn
where ap = $1
and df.mn = mn.mn;
delete from df
using mn
where ap = $1
and df.mn = mn.mn;
-- Identify the number of deletions.
get diagnostics dfdel = row_count;
-- Remove all domains of the approver.
insert into dmid
select dm from dm, mn
where ap = $1
and dm.mn = mn.mn;
delete from dm
using mn
where ap = $1
and dm.mn = mn.mn;
-- Identify the number of deletions.
get diagnostics dmdel = row_count;
-- Remove all meanings of the approver.
insert into mnid
select mn from mn
where ap = $1;
delete from mn
where ap = $1;
-- Identify the number of deletions.
get diagnostics mndel = row_count;
-- Report the deletion counts.
return;
end;
In: ap of an approver. Act: Delete all approvals of the approver. Out[0-6]: counts of deleted meanings, meaning identifiers, definitions, domains, denotations, word classifications, and metadata.
public dfad integer integer, integer, text normal volatile pool plpgsql --create or replace function dfad
--(integer, integer, text)
--returns integer language plpgsql as
declare
dfvar integer;
mnvar integer;
lvvar integer;
begin
-- Identify the specified definition.
select df into dfvar from df
where mn = $1
and lv = $2
and tt = $3;
-- If it exists:
if dfvar is not null
then
---- Report the reason and quit.
return -1;
end if;
-- Identify the specified meaning.
select mn into mnvar from mn
where mn = $1;
-- If it doesn’t exist:
if mnvar is null
then
---- Report the reason and quit.
return -2;
end if;
-- Identify the specified variety.
select lv into lvvar from lv
where lv = $2;
-- If it doesn’t exist:
if lvvar is null
then
---- Report the reason and quit.
return -3;
end if;
-- Identify the new definition.
select * into dfvar from dfgt ();
-- Record it.
insert into df values (dfvar, $1, $2, $3);
-- Return the definition.
return dfvar;
end;
In: mn, lv, and tt of a definition. Act: Add the definition. Out: -1 = definition exists, -2 = no mn, -3 = no lv, other integer = df.
public dfgt integer   normal volatile pool plpgsql --create or replace function dfgt ()
--returns integer language plpgsql as
declare
dfvar integer;
ndfvar integer;
begin
-- Identify the smallest available
-- definition ID.
select min (df) into dfvar from dfid;
-- Make it unavailable.
delete from dfid
where df = dfvar;
-- Identify another available definition.
select df into ndfvar from dfid;
-- If there is none:
if ndfvar is null
then
---- Record the next definition as available.
---- Precondition: The definition with an ID
---- 1 larger than the largest ID of any
---- definition is recorded as available.
insert into dfid values (dfvar + 1);
end if;
-- Return the available definition.
return dfvar;
end;
Act: Revise the list of available definition IDs. Out: the next available definition ID.
public dfrm integer integer normal volatile pool plpgsql --create or replace function dfrm
--(integer) returns integer language plpgsql as
declare
dfvar df;
begin
-- Identify the definition.
select * into dfvar from df
where df = $1;
-- If it doesn’t exist:
if dfvar.df is null
then
---- Report the reason and quit.
return 1;
end if;
-- Record the definition ID as available.
insert into dfid values ($1);
-- Delete the definition.
delete from df
where df = $1;
-- Delete the defined meaning if nothing
-- else refers to it.
perform mnck (dfvar.mn);
-- Return success.
return 0;
end;
In: df of a definition. Act: Delete the definition and record its ID as available. Out: 1 = no df, 0 = success.
public dmad integer integer, integer, text normal volatile pool plpgsql --create or replace function dmad
--(integer, integer, text)
--returns integer language plpgsql as
declare
dmvar integer;
exvar integer;
mnvar integer;
lvvar integer;
begin
-- If the specified text is blank:
if $3 = ''
then
---- Report the reason and quit.
return -1;
end if;
-- Identify the specified meaning.
select mn into mnvar from mn
where mn = $1;
-- If it doesn’t exist:
if mnvar is null
then
---- Report the reason and quit.
return -2;
end if;
-- Identify the specified variety.
select lv into lvvar from lv
where lv = $2;
-- If it doesn’t exist:
if lvvar is null
then
---- Report the reason and quit.
return -3;
end if;
-- Identify the specified expression.
select ex into exvar from ex
where lv = $2 and tt = $3;
-- If it doesn’t exist:
if exvar is null
then
---- Create it.
select * into exvar from exad ($2, $3);
-- Otherwise, if it exists:
else
---- Identify the domain specification.
select dm into dmvar from dm
where mn = $1
and ex = exvar;
---- If it exists:
if dmvar is not null
then
------ Report the reason and quit.
return -4;
end if;
end if;
-- Identify the new domain specification.
select * into dmvar from dmgt ();
-- Record it.
insert into dm values (dmvar, $1, exvar);
-- Return its ID.
return dmvar;
end;
In: mn, lv, and tt of a domain specification. Act: Add the domain specification and, if necessary, its expression. Out: -1 = blank tt, -2 = no mn, -3 = no lv, -4 = domain description exists, other integer = dm.
public dmgt integer   normal volatile pool plpgsql --create or replace function dmgt ()
--returns integer language plpgsql as
declare
dmvar integer;
ndmvar integer;
begin
-- Identify the smallest available
-- domain ID.
select min (dm) into dmvar from dmid;
-- Make it unavailable.
delete from dmid
where dm = dmvar;
-- Identify another missing domain.
select dm into ndmvar from dmid;
-- If there is none:
if ndmvar is null
then
---- Record the next domain as available.
---- Precondition: The domain with an ID
---- 1 larger than the largest ID of any
---- domain is recorded as available.
insert into dmid values (dmvar + 1);
end if;
-- Return the available domain.
return dmvar;
end;
Act: Revise the list of available domain IDs. Out: the next available domain ID.
public dmrm integer integer normal volatile pool plpgsql --create or replace function dmrm
--(integer) returns integer language plpgsql as
declare
dmvar dm;
begin
-- Identify the domain.
select * into dmvar from dm
where dm = $1;
-- If it doesn’t exist:
if dmvar.dm is null
then
---- Report the reason and quit.
return 1;
end if;
-- Record the domain ID as available.
insert into dmid values ($1);
-- Delete the domain.
delete from dm
where dm = $1;
-- Delete the described meaning if nothing
-- else refers to it.
perform mnck (dmvar.mn);
-- Return success.
return 0;
end;
In: dm of a domain. Act: Delete the domain and record its ID as available. Out: 1 = no dm, 0 = success.
public dnad record integer, integer, OUT mnout integer, OUT dnout integer normal volatile pool plpgsql --create or replace function dnad
--(integer, integer, out mnout integer, out dnout integer)
--returns record language plpgsql as
declare
exvar ex;
amvar boolean;
dnvar integer;
begin
-- Identify the properties of the expression.
select * into exvar from ex where ex = $1;
-- If it doesn't exist:
if exvar.ex is null
then
---- Report the reason and quit.
mnout := -1;
dnout := -1;
return;
end if;
-- Identify whether the expression’s variety
-- permits ambiguity.
select am into amvar from lv
where lv = exvar.lv;
-- If not:
if not amvar
then
---- Delete any conflicting denotation.
perform amrm (exvar.ex, $2);
end if;
-- Record the new meaning and identify it.
select * into mnout from mnad ($2);
-- Identify the new denotation.
select * into dnout from dngt ();
-- Record it.
insert into dn values (dnout, mnout, $1);
-- Return the meaning and the denotation.
return;
end;
In: ex of an expression and ap of an approver. Act: Create a denotation by the approver with the expression and a new meaning, replacing any conflicting denotation. Out[0]: -1 = no ex, other integer = meaning. Out[1]: -1 = no ex, other integer = denotation.
public dnad record integer, integer, text, OUT exout integer, OUT dnout integer normal volatile pool plpgsql --create or replace function dnad
--(integer, integer, text,
--out exout integer, out dnout integer)
--returns record language plpgsql as
declare
apvar integer;
exnew boolean;
exvar integer;
lvvar lv;
begin
-- Identify the meaning’s approver.
select ap into apvar from mn
where mn = $1;
-- If it, and thus the meaning, don’t exist:
if apvar is null
then
---- Report the reason and quit.
exout := -1;
dnout := -1;
end if;
-- Identify the expression.
select ex into exvar from ex
where lv = $2
and tt = $3;
-- If it doesn’t exist:
if exvar is null
then
---- Identify this.
exnew := true;
---- Add it.
select * into exvar
from exad ($2, $3);
---- If the addition failed:
if exvar < 0
then
------ Report the reason and quit.
exout := -2;
dnout := -2;
return;
end if;
-- Otherwise, i.e. if the expression already
-- exists:
else
---- Identify this.
exnew := false;
end if;
-- Identify the properties of the variety.
select * into lvvar from lv
where lv = $2;
-- If the expression already existed and the
-- variety prohibits ambiguity:
if not (lvvar.am or exnew)
then
---- Delete any conflicting denotation.
perform amrm (exvar, apvar);
end if;
-- If the variety prohibits synonymy:
if not lvvar.sy
then
---- Delete any conflicting denotation.
perform syrm ($1, $2);
end if;
-- Identify the new denotation.
select * into dnout from dngt ();
-- Add it.
insert into dn values (dnout, $1, exvar);
-- Return the expression and the denotation.
return;
end;
In: mn of a meaning and lv and tt of an expression. Act: Create the expression if necessary and a denotation with the meaning and the expression, replacing any conflicting denotations. Out[0]: -1 = no mn, -2 = ex not added, other integer = expression. Out[1]: -1 = no mn, -2 = ex not added, other integer = denotation.
public dnad record integer, text, integer, OUT exout integer, OUT mnout integer, OUT dnout integer normal volatile pool plpgsql --create or replace function dnad
--(integer, text, integer, out exout integer,
--out mnout integer, out dnout integer)
--returns record language plpgsql as
declare
amvar boolean;
exnew boolean;
lvvar lv;
begin
-- Identify the expression.
select ex into exout from ex
where lv = $1
and tt = $2;
-- If it doesn’t exist:
if exout is null
then
---- Identify this.
exnew := true;
---- Add it.
select * into exout
from exad ($1, $2);
---- If the addition failed:
if exout < 0
then
------ Report the reason and quit.
exout := -1;
mnout := -1;
dnout := -1;
return;
end if;
-- Otherwise, i.e. if the expression
-- already exists:
else
---- Identify this.
exnew := false;
end if;
-- Identify whether the variety permits
-- ambiguity.
select am into amvar from lv
where lv = $1;
-- If the expression already existed and the
-- variety prohibits ambiguity:
if amvar and not exnew
then
---- Delete any conflicting denotation.
perform amrm (exout, $3);
end if;
-- Record the new meaning and identify it.
select * into mnout from mnad ($3);
-- Identify the new denotation.
select * into dnout from dngt ();
-- Record it.
insert into dn values (dnout, mnout, exout);
-- Return the expression, the meaning, and
-- the denotation.
return;
end;
In: lv of a variety, tt of an expression, and ap of an approver. Act: Create the expression if necessary and a denotation by the approver with the expression and a new meaning, replacing any conflicting denotation. Out[0]: -1 = ex not added, other integer = expression. Out[1]: -1 = ex not added, other integer = meaning. Out[2]: -1 = ex not added, other integer = denotation.
public dnad0 integer integer, integer normal volatile pool plpgsql --create or replace function dnad0
--(integer, integer)
--returns integer language plpgsql as
declare
apvar integer;
dnvar integer;
lvvar lv;
mnvar integer;
mxvar integer;
syvar integer;
begin
-- Identify the properties of the
-- expression’s variety.
select lv.* into lvvar from lv, ex
where ex = $1
and lv.lv = ex.lv;
-- If the expression doesn’t exist:
if lvvar.lv is null
then
---- Report the error and quit.
return -1;
end if;
-- Identify the meaning’s approver.
select ap into apvar from mn
where mn = $2;
-- If the meaning doesn’t exist:
if apvar is null
then
---- Report the error and quit.
return -2;
end if;
-- Identify the specified denotation.
select dn into dnvar from dn
where ex = $1
and mn = $2;
-- If the denotation exists:
if dnvar is not null
then
---- Report the superfluity and quit.
return -3;
end if;
-- If the expression’s variety prohibits synonymy:
if not lvvar.sy
then
---- Delete any existing denotation with the
---- meaning and an expression in the variety.
perform syrm ($2, lvvar.lv);
end if;
-- If the variety prohibits ambiguity:
if not lvvar.am
then
---- Delete any denotation of the approver with
---- the expression.
perform amrm ($1, apvar);
end if;
-- Identify the new denotation.
select * into dnvar from dngt ();
-- Record it.
insert into dn values (dnvar, $2, $1);
-- Return the denotation.
return dnvar;
end;
In: ex of an expression and mn of a meaning. Act: Create a denotation with the expression and the meaning, replacing any conflicting denotation. Out: -1 = no ex, -2 = denotation exists, other integer = denotation.
public dnct integer integer normal volatile pool sql --create or replace function dnct
--(integer)
--returns integer language sql as
select cast (count (ex) as integer)
from dn, mn
where ap = $1
and dn.mn = mn.mn;
In: ap of an approver. Out: count of the approver’s denotations.
public dngt integer   normal volatile pool plpgsql --create or replace function dngt ()
--returns integer language plpgsql as
declare
dnvar integer;
ndnvar integer;
begin
-- Identify the smallest available
-- denotation ID.
select min (dn) into dnvar from dnid;
-- Make it unavailable.
delete from dnid
where dn = dnvar;
-- Identify another missing denotation.
select dn into ndnvar from dnid;
-- If there is none:
if ndnvar is null
then
---- Record the next denotation as available.
---- Precondition: The denotation with an ID
---- 1 larger than the largest ID of any
---- denotation is recorded as available.
insert into dnid values (dnvar + 1);
end if;
-- Return the available denotation.
return dnvar;
end;
Act: Revise the list of available denotation IDs. Out: the next available denotation ID.
public dnrm integer integer normal volatile pool plpgsql --create or replace function dnrm (integer)
--returns integer language plpgsql as
declare
dnvar dn;
begin
-- Identify the denotation.
select * into dnvar from dn
where dn = $1;
-- If it doesn’t exist:
if dnvar.dn is null
then
---- Report the error and quit.
return 1;
end if;
-- Delete all word classifications
-- of the denotation.
insert into wcid
select wc from wc
where dn = $1;
delete from wc
where dn = $1;
-- Delete all metadata of the denotation.
insert into mdid
select md from md
where dn = $1;
delete from md
where dn = $1;
-- Delete the denotation.
insert into dnid values ($1);
delete from dn
where dn = $1;
-- Delete the denotation’s meaning if nothing
-- else refers to it.
perform mnck (dnvar.mn);
-- Delete the denotation’s expression if nothing
-- else refers to it.
perform exck (dnvar.ex);
-- Return success.
return 0;
end;
In: dn of a denotation. Act: Delete the denotation, its word classifications, its metadata, and, if thereby orphaned, its meaning, recording their IDs as available. Out: 1 = no dn, 0 = success.
public dnrm integer integer, boolean, boolean normal volatile pool plpgsql --create or replace function dnrm
--(integer, boolean, boolean)
--returns integer language plpgsql as
declare
dnvar dn;
begin
-- Identify the denotation.
select * into dnvar from dn
where dn = $1;
-- If it doesn’t exist:
if dnvar.dn is null
then
---- Report the error and quit.
return 1;
end if;
-- Delete all word classifications
-- of the denotation.
insert into wcid
select wc from wc
where dn = $1;
delete from wc
where dn = $1;
-- Delete all metadata of the denotation.
insert into mdid
select md from md
where dn = $1;
delete from md
where dn = $1;
-- Delete the denotation.
insert into dnid values ($1);
delete from dn
where dn = $1;
-- If the denotation’s meaning is to be
-- deleted if orphaned:
if $2
then
---- Delete it if orphaned.
perform mnck (dnvar.mn);
end if;
-- If the denotation’s expression is to be
-- deleted if orphaned:
if $3
then
---- Delete it if orphaned.
perform exck (dnvar.ex);
end if;
-- Return success.
return 0;
end;
In: dn of a denotation, whether to delete its meaning if orphaned, and whether to delete its expression if orphaned. Act: Delete the denotation, its word classifications, its metadata, and if orphaned and to be deleted its meaning and expression, recording their IDs as available. Out: 1 = no dn, 0 = success.
public ex integer integer, text normal volatile pool sql --create or replace function ex (integer, text)
--returns integer language sql as
select ex from ex
where lv = $1
and tt = $2;
In: lv of a variety and tt of an expression. Out: ex of the expression.
public exad integer integer, text normal volatile pool plpgsql --create or replace function exad
--(integer, text)
--returns integer language plpgsql as
declare
exvar integer;
lvvar integer;
begin
-- If the specified text is blank:
if $2 = ''
then
---- Report the error and quit.
return -1;
end if;
-- Identify the variety.
select lv into lvvar from lv
where lv = $1;
-- If it doesn’t exist:
if lvvar is null
then
---- Report the error and quit.
return -2;
end if;
-- Identify the expression.
select ex into exvar from ex
where lv = $1 and tt = $2;
-- If it doesn’t exist:
if exvar is null
then
---- Identify a new ID for it.
select * into exvar from exgt ();
---- Add the expression.
insert into ex (ex, lv, tt)
values (exvar, $1, $2);
end if;
-- Report the expression’s ID.
return exvar;
end;
In: lv and tt of an expression. Act: Add the expression, if it doesn’t exist. Out: -1 = tt blank, -2 = no lv, other integer = ex.
public exck void integer normal volatile pool plpgsql --create or replace function exck
--(integer) returns void language plpgsql as
declare
exvar integer;
begin
-- Identify the expression.
select ex into exvar from ex
where ex = $1;
-- If it doesn’t exist:
if exvar is null
then
---- Quit.
return;
end if;
-- Identify a denotation with the
-- specified expression.
select ex into exvar from dn
where ex = $1;
-- If there is any:
if exvar is not null
then
---- Stop checking.
return;
end if;
-- Identify a domain whose value is the
-- specified expression.
select ex into exvar from dm
where ex = $1;
-- If there is any:
if exvar is not null
then
---- Stop checking.
return;
end if;
-- Delete the expression.
insert into exid values ($1);
delete from ex
where ex = $1;
end;
In: ex of an expression. Act: If the expression exists and is not the expression of any denotation or domain, delete it.
public exgt integer   normal volatile pool plpgsql --create or replace function exgt ()
--returns integer language plpgsql as
declare
exvar integer;
nexvar integer;
begin
-- Identify the smallest available expression ID.
select min (ex) into exvar from exid;
-- Make it unavailable.
delete from exid
where ex = exvar;
-- Identify another missing expression.
select ex into nexvar from exid;
-- If there is none:
if nexvar is null
then
---- Record the next expression as available.
---- Precondition: The expression with an ID
---- 1 larger than the largest ID of any
---- expression is recorded as available.
insert into exid values (exvar + 1);
end if;
-- Return the deleted expression.
return exvar;
end;
Act: Revise the list of available expression IDs. Out: the next available expression ID.
public exn integer integer normal volatile pool sql --create or replace function exn (integer)
--returns integer language sql as
select cast (count (ex) as integer) from ex
where lv = $1;
In: lv of a variety. Out: count of the expressions in the variety
public exrm void integer normal volatile pool plpgsql --create or replace function exrm (integer)
--returns void language plpgsql as
declare
exvar integer;
begin
-- Identify the expression.
select ex into exvar from ex
where ex = $1;
-- If it exists:
if exvar is not null
then
---- Delete its denotations and their word
---- classifications, metadata, and orphaned
---- meanings.
perform dnrm (dn, true, false) from dn
where ex = $1;
---- Record the expression ID as available.
insert into exid values ($1);
---- Delete the expression.
delete from ex where ex = $1;
end if;
-- Return.
return;
end;
In: ex of an expression. Act: Delete the expression, all denotations with it, all their word classifications and metadata, and all meanings orphaned by the deletion of the denotations.
public exs SETOF record integer, OUT ex integer, OUT tt text normal volatile pool sql --create or replace function exs
--(integer, out ex integer, out tt text)
--returns setof record language sql as
select ex, tt from ex
where lv = $1
order by tt;
In: lv of a variety. Out: ex and tt of each expression in the variety.
public exttmd smallint integer, text normal volatile pool plpgsql --create or replace function exttmd
--(integer, text)
--returns smallint language plpgsql as
declare
delvar record;
exvar ex;
tvar ex;
begin
-- Identify the facts about the expression
-- (source expression).
select * into exvar from ex
where ex = $1;
-- If the source expression doesn't exist:
if exvar.ex is null
then
---- Report the reason and quit.
return 1;
end if;
-- If the new text is blank:
if $2 = ''
then
---- Report the reason and quit.
return 2;
end if;
-- If the new text is the existing one:
if $2 = exvar.tt
then
---- Report success.
return 0;
end if;
-- Identify the existing expression in the
-- variety with the new text (target
-- expression):
select * into tvar from ex
where lv = exvar.lv
and tt = $2;
-- If it doesn’t exist:
if tvar.ex is null
then
---- Amend the source expression's text.
update ex
set tt = $2
where ex = $1;
-- Otherwise, i.e. if the target expression
-- exists:
else
---- Delete every word classification whose
---- denotation’s expression is the source
---- expression if another word classification’s
---- denotation has the same meaning, the target
---- expression, and the same value.
for delvar in
select wc1.wc
from wc as wc1, wc as wc2, dn as dn1, dn as dn2
where dn1.ex = $1
and wc1.dn = dn1.dn
and dn2.ex = tvar.ex
and dn2.mn = dn1.mn
and wc2.dn = dn2.dn
and wc2.ex = wc1.ex
loop
delete from wc
where wc = delvar.wc;
insert into wcid values (delvar.wc);
end loop;
---- Delete every metadatum whose denotation’s
---- expression is the source expression if
---- another metadatum’s denotation has the
---- same meaning, the target expression,
---- the same variable, and the same value.
for delvar in
select md1.md
from md as md1, md as md2, dn as dn1, dn as dn2
where dn1.ex = $1
and md1.dn = dn1.dn
and dn2.ex = tvar.ex
and dn2.mn = dn1.mn
and md2.dn = dn2.dn
and md2.vb = md1.vb
and md2.vl = md1.vl
loop
delete from md
where md = delvar.md;
insert into mdid values (delvar.md);
end loop;
---- Change the denotation of every word
---- classification whose denotation’s
---- expression is the source expression to
---- the denotation that has the same meaning
---- and the target expression, if it exists.
update wc
set dn = dn2.dn
from dn as dn1, dn as dn2
where dn1.ex = $1
and wc.dn = dn1.dn
and dn2.ex = tvar.ex
and dn2.mn = dn1.mn;
---- Change the denotation of every metadatum
---- whose denotation’s expression is the
---- source expression to the denotation that
---- has the same meaning and the target
---- expression, if it exists.
update md
set dn = dn2.dn
from dn as dn1, dn as dn2
where dn1.ex = $1
and md.dn = dn1.dn
and dn2.ex = tvar.ex
and dn2.mn = dn1.mn;
---- Delete every denotation with the source
---- expression if another denotation has the
---- same meaning and the target expression.
for delvar in
select dn1.dn from dn as dn1, dn as dn2
where dn1.ex = $1
and dn2.ex = tvar.ex
and dn2.mn = dn1.mn
loop
delete from dn
where dn = delvar.dn;
insert into dnid values (delvar.dn);
end loop;
---- Change the expression of every denotation
---- with the source expression to the target
---- expression.
update dn
set ex = tvar.ex
where ex = $1;
---- Delete every domain specification with the
---- source expression if another domain
---- specification has the same meaning and the
---- target expression.
for delvar in
select dm1.dm from dm as dm1, dm as dm2
where dm1.ex = $1
and dm2.ex = tvar.ex
and dm2.mn = dm1.mn
loop
delete from dm
where dm = delvar.dm;
insert into dmid values (delvar.dm);
end loop;
---- Change the expression of every domain
---- specification with the source expression to
---- the target expression.
update dm
set ex = tvar.ex
where ex = $1;
---- Delete the source expression.
perform exrm ($1);
end if;
-- Return success.
return 0;
end;
In: ex of an expression and a text. Act: Make the text the expression’s new tt if no expression in the same variety has it, or otherwise merge the expressions, their denotations, their denotations’ word classes and metadata, and their domain specifications. Out: 1 = no ex, 2 = blank tt, 0 = success.
public exx record integer, OUT lc character, OUT vc smallint, OUT lvtt text, OUT ex integer, OUT extt text normal volatile pool sql --create or replace function exx
--(integer, out lc character (3), out vc smallint,
--out lvtt text, out ex integer, out extt text)
--returns record language sql as
select
lc, vc, lv.tt as lvtt, ex, ex.tt as extt
from ex, lv
where ex = $1
and lv.lv = ex.lv;
In: ex of an expression. Out: lc, vc, lv tt, ex, and ex tt of the expression.
public idck text   normal volatile pool plpgsql --create or replace function idck ()
--returns text language plpgsql as
declare
bad text;
clash integer;
ctid integer;
ctmain integer;
maxid integer;
maxmain integer;
begin
-- Initialize result as blank.
bad := '';
-- Check expression IDs.
select count (ex) from exid into ctid;
select count (ex) from ex into ctmain;
select max (ex) from exid into maxid;
select max (ex) from ex into maxmain;
select ex.ex into clash from ex, exid
where exid.ex = ex.ex;
if ctid + ctmain != maxid
or maxid <= maxmain
or clash is not null
then
bad := bad || 'ex ';
end if;
-- Check meaning IDs.
select count (mn) from mnid into ctid;
select count (mn) from mn into ctmain;
select max (mn) from mnid into maxid;
select max (mn) from mn into maxmain;
select mn.mn into clash from mn, mnid
where mnid.mn = mn.mn;
if ctid + ctmain != maxid
or maxid <= maxmain
or clash is not null
then
bad := bad || 'mn ';
end if;
-- Check denotation IDs.
select count (dn) from dnid into ctid;
select count (dn) from dn into ctmain;
select max (dn) from dnid into maxid;
select max (dn) from dn into maxmain;
select dn.dn into clash from dn, dnid
where dnid.dn = dn.dn;
if ctid + ctmain != maxid
or maxid <= maxmain
or clash is not null
then
bad := bad || 'dn ';
end if;
-- Check definition IDs.
select count (df) from dfid into ctid;
select count (df) from df into ctmain;
select max (df) from dfid into maxid;
select max (df) from df into maxmain;
select df.df into clash from df, dfid
where dfid.df = df.df;
if ctid + ctmain != maxid
or maxid <= maxmain
or clash is not null
then
bad := bad || 'df ';
end if;
-- Check domain IDs.
select count (dm) from dmid into ctid;
select count (dm) from dm into ctmain;
select max (dm) from dmid into maxid;
select max (dm) from dm into maxmain;
select dm.dm into clash from dm, dmid
where dmid.dm = dm.dm;
if ctid + ctmain != maxid
or maxid <= maxmain
or clash is not null
then
bad := bad || 'dm ';
end if;
-- Check word-classification IDs.
select count (wc) from wcid into ctid;
select count (wc) from wc into ctmain;
select max (wc) from wcid into maxid;
select max (wc) from wc into maxmain;
select wc.wc into clash from wc, wcid
where wcid.wc = wc.wc;
if ctid + ctmain != maxid
or maxid <= maxmain
or clash is not null
then
bad := bad || 'wc ';
end if;
-- Check metadatum IDs.
select count (md) from mdid into ctid;
select count (md) from md into ctmain;
select max (md) from mdid into maxid;
select max (md) from md into maxmain;
select md.md into clash from md, mdid
where mdid.md = md.md;
if ctid + ctmain != maxid
or maxid <= maxmain
or clash is not null
then
bad := bad || 'md ';
end if;
return bad;
end;
Out: string containing right-space-padded names of tables whose “id” tables are defective or blank if none.
public idw void   normal volatile pool plpgsql --create or replace function idw ()
--returns void language plpgsql as
declare
maxid integer;
idvar integer;
begin
-- Process meanings.
-- Empty the table of available IDs.
truncate mnid;
-- Identify the largest ID.
select max (mn) into maxid from mn;
-- Initiate the table of available IDs
-- with the next integer after that.
insert into mnid values (maxid + 1);
-- If any IDs are missing:
if maxid > (select count (mn) from mn)
then
---- For each possibly missing ID:
for id in 1 .. maxid loop
------ Identify the meaning with it.
select mn into idvar
from mn
where mn = id;
------ If it is missing:
if idvar is null
then
-------- Add it to the table of available IDs.
insert into mnid values (idvar);
end if;
end loop;
end if;
-- Process expressions.
-- Empty the table of available IDs.
truncate exid;
-- Identify the largest ID.
select max (ex) into maxid from ex;
-- Initiate the table of available IDs
-- with the next integer after that.
insert into exid values (maxid + 1);
-- If any IDs are missing:
if maxid > (select count (ex) from ex)
then
---- For each possibly missing ID:
for id in 1 .. maxid loop
------ Identify the expression with it.
select ex into idvar
from ex
where ex = id;
------ If it is missing:
if idvar is null
then
-------- Add it to the table of available IDs.
insert into exid values (id);
end if;
end loop;
end if;
-- Process denotations.
-- Empty the table of available IDs.
truncate dnid;
-- Identify the largest ID.
select max (dn) into maxid from dn;
-- Initiate the table of available IDs
-- with the next integer after that.
insert into dnid values (maxid + 1);
-- If any IDs are missing:
if maxid > (select count (dn) from dn)
then
---- For each possibly missing ID:
for id in 1 .. maxid loop
------ Identify the denotation with it.
select dn into idvar
from dn
where dn = id;
------ If it is missing:
if idvar is null
then
-------- Add it to the table of available IDs.
insert into dnid values (id);
end if;
end loop;
end if;
-- Process definitions.
-- Empty the table of available IDs.
truncate dfid;
-- Identify the largest ID.
select max (df) into maxid from df;
-- Initiate the table of available IDs
-- with the next integer after that.
insert into dfid values (maxid + 1);
-- If any IDs are missing:
if maxid > (select count (df) from df)
then
---- For each possibly missing ID:
for id in 1 .. maxid loop
------ Identify the definition with it.
select df into idvar
from df
where df = id;
------ If it is missing:
if idvar is null
then
-------- Add it to the table of available IDs.
insert into dfid values (id);
end if;
end loop;
end if;
-- Process domains.
-- Empty the table of available IDs.
truncate dmid;
-- Identify the largest ID.
select max (dm) into maxid from dm;
-- Initiate the table of available IDs
-- with the next integer after that.
insert into dmid values (maxid + 1);
-- If any IDs are missing:
if maxid > (select count (dm) from dm)
then
---- For each possibly missing ID:
for id in 1 .. maxid loop
------ Identify the domain with it.
select dm into idvar
from dm
where dm = id;
------ If it is missing:
if idvar is null
then
-------- Add it to the table of available IDs.
insert into dmid values (id);
end if;
end loop;
end if;
-- Process word classifications.
-- Empty the table of available IDs.
truncate wcid;
-- Identify the largest ID.
select max (wc) into maxid from wc;
-- Initiate the table of available IDs
-- with the next integer after that.
insert into wcid values (maxid + 1);
-- If any IDs are missing:
if maxid > (select count (wc) from wc)
then
---- For each possibly missing ID:
for id in 1 .. maxid loop
------ Identify the word classification with it.
select wc into idvar
from wc
where wc = id;
------ If it is missing:
if idvar is null
then
-------- Add it to the table of available IDs.
insert into wcid values (id);
end if;
end loop;
end if;
-- Process metadata.
-- Empty the table of available IDs.
truncate mdid;
-- Identify the largest ID.
select max (md) into maxid from md;
-- Initiate the table of available IDs
-- with the next integer after that.
insert into mdid values (maxid + 1);
-- If any IDs are missing:
if maxid > (select count (md) from md)
then
---- For each possibly missing ID:
for id in 1 .. maxid loop
------ Identify the metadatum with it.
select md into idvar
from md
where md = id;
------ If it is missing:
if idvar is null
then
-------- Add it to the table of available IDs.
insert into mdid values (id);
end if;
end loop;
end if;
end;
Act: Repopulate the tables of available IDs.
public ixck void   normal volatile pool plpgsql --create or replace function ixck ()
--returns void language plpgsql as
declare
begin
perform * from ex order by ex limit 1000;
perform * from ex order by lv, tt limit 1000;
perform * from ex order by lv limit 1000;
perform * from dn order by dn limit 1000;
perform * from dn order by mn, ex limit 1000;
perform * from dn order by ex limit 1000;
perform * from dn order by mn limit 1000;
perform * from mn order by mn limit 1000;
perform * from mn order by ap limit 1000;
end;
Act: Make the server traverse and thus cache all large indices.
public lcmd smallint integer, character normal volatile pool plpgsql --create or replace function lcmd
--(integer, character (3))
--returns smallint language plpgsql as
declare
lcvar character (3);
lvvar lv;
uvar smallint;
vcvar smallint;
begin
-- Identify the facts about the variety.
select * into lvvar from lv
where lv = $1;
-- If the variety doesn't exist:
if lvvar.lv is null
then
---- Report the reason and quit.
return 1;
end if;
-- If the new ISO code is the existing one:
if $2 = lvvar.lc
then
---- Return success.
return 0;
end if;
-- Identify the new ISO code as a valid code.
select lc into lcvar from lc
where lc = $2;
-- If it doesn't exist:
if lcvar is null
then
---- Report the reason and quit.
return 2;
end if;
-- Initialize the to-be-tested variety code.
vcvar := 0;
-- Initialize the last-tested variety
-- code as used.
uvar := 0;
-- Until the last-tested variety code is
-- unused:
while uvar is not null loop
---- Determine whether the to-be-tested variety
---- code is used.
select vc into uvar from lv
where lc = $2
and vc = vcvar;
---- Increment the to-be-tested variety code.
vcvar := vcvar + 1;
end loop;
-- Amend the variety's ISO code and variety
-- code.
update lv
set lc = $2, vc = vcvar - 1
where lv = $1;
-- Return success.
return 0;
end;
In: lv of a variety and lc of a language. Act: Make the lc the variety’s new lc and give the variety the first unused vc. Out: 1 = no lv, 2 = no lc, 0 = success.
public lcvc character integer normal volatile pool sql --create or replace function lcvc (integer)
--returns character (7) language sql as
select (lc || '-' || (to_char (vc, 'FM009'))) from lv where lv = $1;
In: lv of a variety. Out: variety’s UI.
public lcvctt record integer, OUT lcvc character, OUT tt text normal volatile pool sql --create or replace function lcvctt
--(integer, out lcvc character (7), out tt text)
--returns record language sql as
select (lc || '-' || (to_char (vc, 'FM009'))), tt from lv where lv = $1;
In: lv of a variety. Out: variety’s UI and label.
public lv integer character, integer normal volatile pool sql --create or replace function lv
--(character, integer)
--returns integer language sql as
select lv from lv
where lc = $1
and vc = $2;
In: lc and vc of a variety. Out: lv of the variety.
public lvad integer character, boolean, boolean, text, integer normal volatile pool plpgsql --create or replace function lvad
--(character (3), boolean, boolean,
--text, integer)
--returns integer language plpgsql as
declare
ivar integer;
lcvar character (3);
lvvar integer;
usvar integer;
uvar smallint;
vcvar smallint;
begin
-- Identify the ISO code.
select lc into lcvar from lc
where lc = $1;
-- If it does not exist:
if lcvar is null
then
---- Report the reason and quit.
return -1;
end if;
-- If the label is blank:
if length ($4) = 0
then
---- Report the reason and quit.
return -2;
end if;
-- Identify the smallest unused variety ID.
select min (lvu) into lvvar from
(select 1 as lvu union select lv + 1 as lvu from lv
except select lv as lvu from lv) as subq;
-- Identify an existing variety with the ISO code.
select lv into ivar from lv
where lc = $1;
-- If there is none:
if ivar is null
then
---- Add a variety with the ISO code and
---- 0 as the variety code.
insert into lv values
(lvvar, $1, 0, $2, $3, $4);
-- Otherwise, i.e. if a variety with the
-- ISO code exists:
else
---- Identify the smallest unused variety
---- code among varieties with the ISO code.
select min (vcu) into vcvar from
(select 0 as vcu union select vc + 1 as vcu from lv
where lc = $1 except select vc as vcu from lv
where lc = $1) as subq;
---- Add the specified variety with it.
insert into lv values
(lvvar, $1, vcvar, $2, $3, $4);
end if;
-- If a user is to be entitled to edit the
-- variety:
if $5 > 0
then
---- Make the user an editor of it.
insert into lu values (lvvar, $5);
end if;
-- Return the variety’s ID.
return lvvar;
end;
In: lc, sy, am, and tt of a variety and us of a user that is to be entitled to edit the variety or 0 if none is. Act: Add the specified variety and, if so specified, make the user an editor of it. Out: -1 = bad lc, -2 = no tt, other integer = lv of the variety.
public lvpw boolean integer, integer, character normal volatile pool sql --create or replace function lvpw
--(integer, integer, character)
--returns boolean language sql as
select
($3 = (select pw from us where us = $2))
and (
(select ad from us where us = $2)
or ($2 = (select us from lu where lv = $1 and us = $2))
);
In: lv of a variety and us and pw of a user. Out: whether the password is the user’s and the user is either a superuser or an editor of the variety.
public lvs SETOF integer integer normal volatile pool sql --create or replace function lvs (integer)
--returns setof integer language sql as
select av.lv from av, lv
where av.ap = $1
and lv.lv = av.lv
order by lc, vc;
In: ap of an approver. Out: lv of each declared variety of the approver.
public lvttmd smallint integer, text normal volatile pool plpgsql --create or replace function lvttmd (integer, text)
--returns smallint language plpgsql as
declare
lvvar lv;
begin
-- Identify the facts about the variety.
select * into lvvar from lv
where lv = $1;
-- If the variety doesn't exist:
if lvvar.lv is null
then
---- Report the reason and quit.
return 1;
end if;
-- If the new label is blank:
if $2 = ''
then
---- Report the reason and quit.
return 2;
end if;
-- If the new label is the existing one:
if $2 = lvvar.tt
then
---- Return success.
return 0;
end if;
-- Amend the variety's label.
update lv
set tt = $2
where lv = $1;
-- Return success.
return 0;
end;
In: lv of a variety and a text. Act: Make the text the variety’s new tt. Out: 1 = no lv, 2 = blank tt, 0 = success.
public lvx record integer, OUT lc character, OUT vc smallint, OUT tt text normal volatile pool sql --create or replace function lvx
--(integer, out lc character (3), out vc smallint, out tt text)
--returns record language sql as
select lc, vc, tt from lv
where lv = $1;
In: lv of a variety. Out: lc, vc, and tt of the variety.
public lvxs SETOF record OUT lv integer, OUT lc character, OUT vc smallint, OUT tt text normal volatile pool sql --create or replace function lvxs
--(out lv integer, out lc character (3),
--out vc smallint, out tt text)
--returns setof record language sql as
select lv, lc, vc, tt
from lv
order by lc, vc;
Out: lv, lc, vc, and tt of each variety.
public mdad integer integer, text, text normal volatile pool plpgsql --create or replace function mdad
--(integer, text, text)
--returns integer language plpgsql as
declare
dnvar integer;
mdvar integer;
begin
-- Identify the specified metadatum.
select md into mdvar from md
where dn = $1
and vb = $2
and vl = $3;
-- If it exists:
if mdvar is not null
then
---- Report the reason and quit.
return -1;
end if;
-- Identify the specified denotation.
select dn into dnvar from dn
where dn = $1;
-- If it doesn’t exist:
if dnvar is null
then
---- Report the reason and quit.
return -2;
end if;
-- Identify the new metadatum.
select * into mdvar from mdgt ();
-- Record it.
insert into md values (mdvar, $1, $2, $3);
-- Return the metadatum.
return mdvar;
end;
In: dn, vb, and vl of a metadatum. Act: Add the metadatum. Out: -1 = metadatum exists, -2 = no dn, other integer = md.
public mdgt integer   normal volatile pool plpgsql --create or replace function mdgt ()
--returns integer language plpgsql as
declare
mdvar integer;
nmdvar integer;
begin
-- Identify the smallest available
-- metadatum ID.
select min (md) into mdvar from mdid;
-- Make it unavailable.
delete from mdid
where md = mdvar;
-- Identify another missing metadatum.
select md into nmdvar from mdid;
-- If there is none:
if nmdvar is null
then
---- Record the next metadatum as available.
---- Precondition: The metadatum with an ID
---- 1 larger than the largest ID of any
---- metadatum is recorded as available.
insert into mdid values (mdvar + 1);
end if;
-- Return the available metadatum.
return mdvar;
end;
Act: Revise the list of available metadatum IDs. Out: the next available metadatum ID.
public mdrm void integer normal volatile pool sql --create or replace function mdrm
--(integer) returns void language sql as
insert into mdid
select md from md
where md = $1;
delete from md
where md = $1;
In: md of a metadatum. Act: Delete the metadatum, recording its ID as available.
public miad integer integer, text normal volatile pool plpgsql --create or replace function miad
--(integer, text)
--returns integer language plpgsql as
declare
mnvar integer;
begin
-- Identify the specified meaning.
select mn into mnvar from mn
where mn = $1;
-- If it doesn’t exist:
if mnvar is null
then
---- Report the reason and quit.
return 1;
end if;
-- Delete any existing identifier of the
-- meaning.
delete from mi
where mn = $1;
-- Record the identifier.
insert into mi values ($1, $2);
-- Return a success report.
return 0;
end;
In: mn and tt of a meaning identifier. Act: Add the meaning identifier, replacing any identifier of the meaning. Out: 1 = no mn, 0 = success.
public mnad integer integer normal volatile pool plpgsql --create or replace function mnad (integer)
--returns integer language plpgsql as
declare
mnvar integer;
begin
-- Identify a new meaning ID.
select * into mnvar from mngt ();
-- Record the new meaning.
insert into mn values (mnvar, $1);
-- Return the meaning ID.
return mnvar;
end;
In: ap of an approver. Act: Record a new meaning of the approver. Out: meaning ID.
public mnck void integer normal volatile pool plpgsql --create or replace function mnck
--(integer) returns void language plpgsql as
declare
mnvar integer;
begin
-- Identify the meaning.
select mn into mnvar from mn
where mn = $1;
-- If it doesn’t exist:
if mnvar is null
then
---- Quit.
return;
end if;
-- Identify a denotation with the
-- specified meaning.
select mn into mnvar from dn
where mn = $1;
-- If there is any:
if mnvar is not null
then
---- Stop checking.
return;
end if;
-- Identify a definition with the
-- specified meaning.
select mn into mnvar from df
where mn = $1;
-- If there is any:
if mnvar is not null
then
---- Stop checking.
return;
end if;
-- Identify a domain with the
-- specified meaning.
select mn into mnvar from dm
where mn = $1;
-- If there is any:
if mnvar is not null
then
---- Stop checking.
return;
end if;
-- Identify a meaning identifier with the
-- specified meaning.
select mn into mnvar from mi
where mn = $1;
-- If there is any:
if mnvar is not null
then
---- Stop checking.
return;
end if;
-- Delete the meaning.
insert into mnid values ($1);
delete from mn
where mn = $1;
end;
In: mn of a meaning. Act: If the meaning exists and is not the meaning of any denotation, definition, domain, or meaning identifier, delete it.
public mngt integer   normal volatile pool plpgsql --create or replace function mngt ()
--returns integer language plpgsql as
declare
mnvar integer;
nmnvar integer;
begin
-- Identify the smallest available meaning ID.
select min (mn) into mnvar from mnid;
-- Make it unavailable.
delete from mnid
where mn = mnvar;
-- Identify another missing meaning.
select mn into nmnvar from mnid;
-- If there is none:
if nmnvar is null
then
---- Record the next meaning as available.
---- Precondition: The meaning with an ID
---- 1 larger than the largest ID of the
---- meaning of any denotation, meaning
---- identifier, definition, or domain is
---- recorded as available.
insert into mnid values (mnvar + 1);
end if;
-- Return the deleted meaning.
return mnvar;
end;
Act: Revise the list of available meaning IDs. Out: the next available meaning ID.
public mnrm integer integer normal volatile pool plpgsql --create or replace function mnrm (integer)
--returns integer language plpgsql as
declare
dnvar record;
mnvar integer;
begin
-- Identify the specified meaning.
select mn into mnvar from mn
where mn = $1;
-- If it doesn’t exist:
if mnvar is null
then
---- Report the reason and quit.
return 1;
end if;
-- Delete the meaning identifier, if any.
delete from mi
where mn = $1;
-- Delete all definitions of the meaning.
insert into dfid
select df from df
where mn = $1;
delete from df
where mn = $1;
-- Delete all domains of the meaning.
insert into dmid
select dm from dm
where mn = $1;
delete from dm
where mn = $1;
-- For each denotation with the meaning:
for dnvar in
select dn from dn
where mn = $1
loop
---- Delete it and its word classifications,
---- metadata, and if orphaned expression.
perform dnrm (dnvar.dn, false, true);
end loop;
-- Record its ID as available.
insert into mnid values ($1);
-- Delete the meaning.
delete from mn where mn = $1;
-- Report success.
return 0;
end;
In: mn of a meaning. Act: Delete the meaning and its identifier, definitions, domains, and denotations.
public nml integer integer, text, text normal volatile pool plpgsql --create or replace function nml
--(integer, text, text)
--returns integer language plpgsql as
declare
lvvar lv;
mdvar integer;
begin
-- Identify the facts about the variety.
select * into lvvar from lv
where lv = $1;
-- If the variety doesn't exist:
if lvvar.lv is null
then
---- Report the reason and quit.
return -1;
end if;
-- If the old text is blank:
if $2 = ''
then
---- Report the reason and quit.
return -2;
end if;
-- If the old and new texts are identical:
if $3 = $2
then
---- Return success.
return 0;
end if;
---- Replace every instance of the old text
---- with the new text in every expression
---- in the variety in any denotation containing
---- the old text, making the word class and
---- metadatum tables conform.
select count (exttmd (ex.ex, replace (tt, $2, $3)))
into mdvar
from ex, dn
where lv = $1
and position ($2 in tt) > 0
and dn.ex = ex.ex;
-- Return the count of expressions changed.
return mdvar;
end;
In: lv of a variety and two texts. Act: Replace every instance of the first with the second text in the text of each expression in the variety in any denotation. Out: 0 = texts identical, -1 = no lv, -2 = old text blank, other integer = count of changed expressions.
public pl void   normal volatile pool sql --create or replace function pl ()
--returns void language sql as
delete from pl0;
insert into pl0
select tt, ex.ex, mn.mn from dn, ex, mn
where ap = 1
and lv = 1127
and dn.ex = ex.ex
and dn.mn = mn.mn
order by tt;
delete from pl1;
insert into pl1
select pl0.mn, lv, ex.ex from pl0, dn, ex
where dn.mn = pl0.mn
and ex.ex = dn.ex
and lv != 1127
order by pl0.mn, lv, ex.ex;
Act: Populate pl0 and pl1.
public syrm void integer, integer normal volatile pool plpgsql --create or replace function syrm
--(integer, integer)
--returns void language plpgsql as
declare
exvar integer;
dnvar integer;
begin
-- Identify an expression in the specified
-- variety that has the specified meaning.
select ex.ex into exvar from dn, ex
where mn = $1
and ex.ex = dn.ex
and lv = $2;
-- If there is any:
if exvar is not null
then
---- Identify the denotation with the specified
---- meaning and with that expression.
select dn into dnvar from dn
where mn = $1
and ex = exvar;
---- If it exists:
if dnvar is not null
then
------ Delete the denotation and its word
------ classifications and metadata.
perform dnrm (dnvar, false, true);
end if;
end if;
end;
In: mn of a meaning and lv of a variety. Act: Delete an arbitrary denotation, if any, with the meaning and with an expression in the variety, the denotation’s word classifications and metadata, and if orphaned the expression. Use: To prepare a meaning for assignment to an expression in a synonymy-prohibiting variety.
public td text text normal volatile pool plperlu # create or replace function td (text)
# returns text language plperlu as
use encoding 'utf8';
# Make Perl interpret the script and standard files as UTF-8 rather than bytes.
use strict;
use Unicode::Normalize;
my $td = (&NFKD ($_[0]));
# Initialize the degradation of the text as its
# compatibility decomposition (Normalization Form KD).
$td = (lc $td);
# Make the degradation lower-case.
$td =~ s#ı#i#g;
# Replace all instances of “ı” with “i” in the
# degradation.
$td =~ s#[^\p{Ll}\p{Lo}]##g;
# Remove all non-basic characters from the degradation.
# This leaves undegraded many characters that
# arguably merit degradation and does not deal with
# transscriptal confusion or transliteration.
return $td;
# Return the degradation.
In: a text. Out: the degradation of the text.
public tdau trigger   trigger volatile pool plperlu # create or replace function tdau ()
## returns trigger language plperlu as
use encoding 'utf8';
# Make Perl interpret the script and standard files as UTF-8 rather than bytes.
use strict;
use Unicode::Normalize;
my $tt = ($_TD->{new}{tt});
# Identify the new expression text.
my $td = (&NFKD ($tt));
# Initialize its degradation as its compatibility decomposition (Normalization Form KD).
my $td = (lc $td);
# Make the degradation lower-case.
$td =~ s#ı#i#g;
# Replace all instances of “ı” with “i” in the degradation.
$td =~ s#[^\p{Ll}\p{Lo}]##g;
# Remove all non-basic characters from the degradation.
# This leaves undegraded many characters that arguably merit degradation and does not deal with
# transscriptal confusion or transliteration.
$_TD->{new}{td} = $td;
# Identify the degradation.
return "MODIFY";
# Return the modified row.
Act: Set column td to the degradation of column tt.
public tdw integer   normal volatile pool plperlu # create or replace function tdw ()
# returns void language plperl as
use encoding 'utf8';
# Make Perl interpret the script and standard files as UTF-8 rather than bytes.
use strict;
use Unicode::Normalize;
my $sth = (spi_query ('select * from ex'));
# Identify a query to read table ex.
my ($ex, $lv, $rowr, %row, $rv, $td, $tt);
my $changed = 0;
# Initialize the count of degradations.
$rv = (spi_exec_query ('update ex set td = null'));
# Delete column ex.td.
while (defined ($rowr = spi_fetchrow ($sth))) {
# Until all its rows are read:
%row = %$rowr;
# Identify the row as a table.
$ex = $row{ex};
# Identify the expression’s ID.
$lv = $row{lv};
# Identify the expression’s language variety.
$tt = $row{tt};
# Identify the expression’s text.
$td = (&NFKD ($tt));
# Initialize its degradation as its compatibility decomposition (Normalization Form KD).
$td = (lc $td);
# Make the degradation lower-case.
$td =~ s#ı#i#g;
# Replace all instances of “ı” with “i” in the degradation.
$td =~ s#[^\p{Ll}\p{Lo}]##g;
# Remove all non-basic characters from the degradation.
# This leaves undegraded many characters that arguably merit degradation and does not deal with
# transscriptal confusion or transliteration.
if ($td ne $tt) {
# If the degradation differs from the text:
$td =~ s#'#''#g;
# Double any ASCII apostrophes in the degradation.
$rv = (spi_exec_query ("insert into extemp values ($ex, '$td')"));
# Record the degradation in the temporary file.
$changed++;
# Increment the degradation count.
}
}
# $rv = (spi_exec_query ('update ex set td = tt where td is null'));
# Make column ex.td identical to column ex.tt wherever no degradation has been recorded.
return $changed;
# Return the degradation count.
Act: Populate column ex.td.
public tr SETOF record integer, integer, OUT exid integer, OUT extt text normal volatile pool sql --create or replace function tr
--(integer, integer,
--out exid integer, out extt text)
--returns setof record language sql as
select distinct dn2.ex as exid, tt as extt
from dn as dn1, dn as dn2, ex
where dn1.ex = $1
and dn2.mn = dn1.mn
and dn2.ex != $1
and ex.ex = dn2.ex
and lv = $2
order by extt;
In: ex of an expression and lv of a variety. Out: ex and tt of each expression in the variety, other than the expression, that has at least 1 meaning of the expression.
public traps SETOF record integer, integer, OUT ap integer, OUT tt text normal volatile pool sql --create or replace function traps
--(integer, integer, out ap integer, out tt text)
--returns setof record language sql as
select distinct ap.ap, tt
from dn as dn1, dn as dn2, mn, ap
where dn1.ex = $1
and dn2.ex = $2
and dn2.mn = dn1.mn
and mn.mn = dn1.mn
and ap.ap = mn.ap
order by tt;
In: ex of 2 expressions. Out: ap and tt of each approver of at least 1 meaning shared by the expressions.
public trlv SETOF record integer, OUT lv integer, OUT lc character, OUT vc smallint, OUT tt text normal volatile pool sql --create or replace function trlv
--(integer, out lv integer, out lc character (3),
--out vc smallint, out tt text)
--returns setof record language sql as
select distinct lv.lv, lc, vc, lv.tt
from dn as dn1, dn as dn2, ex, lv
where dn1.ex = $1
and dn2.mn = dn1.mn
and dn2.ex != $1
and ex.ex = dn2.ex
and lv.lv = ex.lv
order by lc, vc
In: ex of an expression. Out: lv, lc, vc, and tt of each variety of any translation or synonym of the expression (i.e. of any other expression of any denotation with any meaning of any denotation of the expression).
public trmns SETOF integer integer, integer, integer normal volatile pool sql --create or replace function trmns
--(integer, integer, integer)
--returns setof integer language sql as
select mn.mn from dn as dn1, dn as dn2, mn
where dn1.ex = $1
and dn2.ex = $2
and dn2.mn = dn1.mn
and mn.mn = dn1.mn
and mn.ap = $3
order by mn;
In: ex of 2 expressions and ap of an approver. Out: mn of each meaning of the approver shared by the expressions.
public trp2 text integer, integer normal volatile pool plpgsql --create or replace function trp2
--(integer, integer)
--returns text language plpgsql as
declare
mnvar integer;
begin
-- Identify the meaning assigned to the
-- specified expression by PanLex.
select mn into mnvar from pl0
where ex = $1;
-- Return a result.
return trp2a (mnvar, $2);
end;
In: ex of a PanLex expression and lv of a variety. Out: text of the translation by PanLex of the expression into the variety or, if none, of the most-attested translation into the variety from the translations by PanLex of the expression, or blank string if none.
public trp2 text text, integer normal volatile pool plpgsql --create or replace function trp2
--(text, integer)
--returns text language plpgsql as
declare
mnvar integer;
begin
-- Identify the meaning assigned to the
-- specified expression by PanLex.
select mn into mnvar from pl0
where tt = $1;
-- Return a result.
return trp2a (mnvar, $2);
end;
In: tt of a PanLex expression and lv of a variety. Out: text of the translation by PanLex of the expression into the variety or, if none, of the most-attested translation into the variety from the translations by PanLex of the expression, or blank string if none.
public trp2a text integer, integer normal volatile pool plpgsql --create or replace function trp2a
--(integer, integer)
--returns text language plpgsql as
declare
exvar integer;
trvar record;
ttvar text;
begin
-- If the specified meaning is undefined:
if $1 is null
then
---- Return a failure result.
return '';
end if;
-- Identify the expression in the specified
-- variety to which PanLex has assigned the
-- specified meaning.
select ex into exvar from pl1
where mn = $1
and lv = $2;
-- If it exists:
if exvar is not null
then
---- Identify its text.
select tt into ttvar from ex
where ex = exvar;
---- Return it.
return ttvar;
-- Otherwise, i.e. if it doesn’t exist:
else
---- Identify the most-attested translation
---- into the specified variety from the
---- expressions to which PanLex has assigned
---- the specified meaning. (Not using a
---- subquery multiplies the cost by about 10.)
select dn2.ex, count (dn2.dn) as dns into trvar
from pl1, dn as dn1, dn as dn2, (
select ex from ex
where lv = $2
) as subq
where pl1.mn = $1
and dn1.ex = pl1.ex
and dn2.mn = dn1.mn
and dn2.ex = subq.ex
group by dn2.ex
order by dns desc;
---- If it doesn’t exist:
if trvar.ex is null
then
------ Return a failure result.
return '';
end if;
---- Identify its text.
select tt into ttvar from ex
where ex = trvar.ex;
---- Return it.
return ttvar;
end if;
end;
In: mn of a meaning and lv of a variety. Out: tt of the expression in the variety to which PanLex has assigned the meaning, or, if none, of the most-attested translation into the variety of the expressions to which PanLex has assigned the meaning, or, if none, blank string.
public trtrmns SETOF record integer, integer, integer, OUT mn0 integer, OUT mn1 integer normal volatile pool sql --create or replace function trtrmns
--(integer, integer, integer,
--out mn0 integer, out mn1 integer)
--returns setof record language sql as
select dn1.mn, dn2.mn
from dn as dn1, dn as dn2, dn as dn3, dn as dn4
where dn1.ex = $1
and dn2.ex = $2
and dn3.ex = $3
and dn4.ex = $3
and dn2.mn != dn1.mn
and dn3.mn = dn1.mn
and dn4.mn = dn2.mn
In: ex of 3 expressions. Out: each mn shared by expressions 0 and 2 and mn shared by expressions 1 and 2.
public trtrmnxs SETOF record integer, integer, integer, OUT mn0 integer, OUT ap0 integer, OUT tt0 text, OUT mn1 integer, OUT ap1 integer, OUT tt1 text normal volatile pool sql --create or replace function trtrmnxs
--(integer, integer, integer,
--out mn0 integer, out ap0 integer, out tt0 text,
--out mn1 integer, out ap1 integer, out tt1 text)
--returns setof record language sql as
select mns.mn0, ap0.ap, ap0.tt, mns.mn1, ap1.ap, ap1.tt
from trtrmns ($1, $2, $3) as mns,
mn as mn0, mn as mn1, ap as ap0, ap as ap1
where mn0.mn = mns.mn0
and mn1.mn = mns.mn1
and ap0.ap = mn0.ap
and ap1.ap = mn1.ap
order by ap0.tt, ap1.tt;
In: ex of 3 expressions. Out: mn, ap, and ap tt of each meaning in a 2-stage translation path between expressions 0 and 1 via expression 2.
public trtrms SETOF integer integer, integer normal volatile pool sql --create or replace function trtrms
--(integer, integer)
--returns setof integer language sql as
select distinct dn3.ex
from dn as dn1, dn as dn2, dn as dn3, dn as dn4
where dn1.ex = $1
and dn2.ex = $2
and dn2.mn != dn1.mn
and dn3.mn = dn1.mn
and dn4.mn = dn2.mn
and dn4.ex = dn3.ex
and dn3.ex != $1
and dn3.ex != $2;
In: ex of 2 expressions. Out: ex of each expression other than the expressions that shares distinct meanings with the expressions.
public trtrmxs SETOF record integer, integer, OUT lv integer, OUT lc character, OUT vc smallint, OUT lvtt text, OUT ex integer, OUT extt text normal volatile pool sql --create or replace function trtrmxs
--(integer, integer, out lv integer,
--out lc character (3), out vc smallint,
--out lvtt text, out ex integer, out extt text)
--returns setof record language sql as
select lv.lv, lc, vc, lv.tt as lvtt, ex.ex, ex.tt as extt
from (select * from trtrms ($1, $2)) as mex (ex),
ex, lv
where ex.ex = mex.ex
and lv.lv = ex.lv
order by lc, vc, extt;
In: ex of 2 expressions. Out: lv, lc, vc, lv tt, ex, and ex tt of each expression other than the expressions that shares distinct meanings with the expressions.
public trtrs SETOF record integer, integer, OUT exid integer, OUT extt text normal volatile pool sql --create or replace function trtrs
--(integer, integer,
--out exid integer, out extt text)
--returns setof record language sql as
select ex.ex as exid, tt as extt
from (
select distinct dn2.ex from (
select distinct dn4.ex from dn as dn3, dn as dn4
where dn3.ex = $1
and dn4.mn = dn3.mn
and dn4.ex != $1
) as trs, dn as dn1, dn as dn2
where dn1.ex = trs.ex
and dn2.mn = dn1.mn
and dn2.ex != dn1.ex
) as trtrs, ex
where ex.ex = trtrs.ex
and lv = $2
order by tt;
In: ex of an expression and lv of a variety. Out: ex and tt of each translation into the variety of each translation of the expression.
public usad integer text, text, text, text, character normal volatile pool plpgsql --create or replace function usad
--(text, text, text, text, character (32))
--returns integer language plpgsql as
declare
usvar integer;
begin
-- If the alias is blank:
if (length ($2)) = 0
then
---- Report the reason and quit.
return -1;
end if;
-- If the alias is an existing user’s alias:
if (select count (us) from us where al = $2) > 0
then
---- Report the reason and quit.
return -2;
end if;
-- If the password is blank:
if (length ($5)) = 0
then
---- Report the reason and quit.
return -3;
end if;
-- Identify an available user ID.
usvar := (usid ());
-- Add the specified user.
insert into us (us, nm, al, sm, ht, pw)
values (usvar, $1, $2, $3, $4, $5);
-- Return the user's ID.
return usvar;
end;
In: nm, al, sm, ht, and pw of a new user. Act: Add the user. Out: -1 = no al, -2 = used al, -3 = no pw, other integer = us.
public usid integer   normal volatile pool sql --create or replace function usid ()
--returns integer language sql as
select min (us) from (
(
select 1 as us
union
select us + 1 as us from us
)
except select us from us
) as avail;
Out: smallest available user us.
public uspw boolean integer, character normal volatile pool sql --create or replace function uspwck
--(integer, character (32))
--returns boolean language sql as
select
(
$1 is not null
and $2 is not null
and (select pw from us where us = $1) = $2
and (select ok from us where us = $1)
)
;
In: us and pw of a user. Out: whether the password is the user's.
public wcad integer integer, text normal volatile pool plpgsql --create or replace function wcad
--(integer, text)
--returns integer language plpgsql as
declare
dnvar integer;
exvar integer;
wcvar integer;
begin
-- Identify the specified word-class’s
-- PanLex expression.
select ex into exvar from wcex
where tt = $2;
-- If it doesn’t exist:
if exvar is null
then
---- Report the reason and quit.
return -1;
end if;
-- Identify the specified denotation.
select dn into dnvar from dn
where dn = $1;
-- If it doesn’t exist:
if dnvar is null
then
---- Report the reason and quit.
return -2;
end if;
-- Identify the word classification.
select wc into wcvar from wc
where dn = dnvar
and ex = exvar;
-- If it does not exist:
if wcvar is null
then
---- Identify the new one.
select * into wcvar from wcgt ();
---- Record it.
insert into wc values (wcvar, $1, exvar);
end if;
-- Return the word classification.
return wcvar;
end;
In: dn and tt of ex of a word classification. Act: Add the word classification. Out: -1 = no tt, -2 = no dn, other integer = wc.
public wcgt integer   normal volatile pool plpgsql --create or replace function wcgt ()
--returns integer language plpgsql as
declare
wcvar integer;
nwcvar integer;
begin
-- Identify the smallest available word
-- classification ID.
select min (wc) into wcvar from wcid;
-- Make it unavailable.
delete from wcid
where wc = wcvar;
-- Identify another missing word class.
select wc into nwcvar from wcid;
-- If there is none:
if nwcvar is null
then
---- Record the next word classification as
---- available.
---- Precondition: The word classification with
---- an ID 1 larger than the largest ID of any
---- word classification is recorded as available.
insert into wcid values (wcvar + 1);
end if;
-- Return the available word classification.
return wcvar;
end;
Act: Revise the list of available word class IDs. Out: the next available word class ID.
public wcrm void integer normal volatile pool sql --create or replace function wcrm
--(integer) returns void language sql as
insert into wcid
select wc from wc
where wc = $1;
delete from wc
where wc = $1;
In: wc of a word classification. Act: Delete the word classification, recording its ID as available.