33-- REQUIRE: src/ore_block_u64_8_256/types.sql
44-- REQUIRE: src/hmac_256/types.sql
55
6-
7-
6+ -- ! @brief Extract ciphertext from encrypted JSONB value
7+ -- !
8+ -- ! Extracts the ciphertext (c field) from a raw JSONB encrypted value.
9+ -- ! The ciphertext is the base64-encoded encrypted data.
10+ -- !
11+ -- ! @param val JSONB Raw encrypted value containing 'c' field
12+ -- ! @return Text Base64-encoded ciphertext string
13+ -- ! @throws Exception if 'c' field is not present in JSONB
14+ -- !
15+ -- ! @example
16+ -- ! -- Extract ciphertext from JSONB literal
17+ -- ! SELECT eql_v2.ciphertext('{"c":"AQIDBA==","i":{"unique":"..."}}'::jsonb);
18+ -- !
19+ -- ! @see eql_v2.ciphertext(eql_v2_encrypted)
20+ -- ! @see eql_v2.meta_data
821CREATE FUNCTION eql_v2 .ciphertext(val jsonb)
922 RETURNS text
1023 IMMUTABLE STRICT PARALLEL SAFE
1730 END;
1831$$ LANGUAGE plpgsql;
1932
20-
33+ -- ! @brief Extract ciphertext from encrypted column value
34+ -- !
35+ -- ! Extracts the ciphertext from an encrypted column value. Convenience
36+ -- ! overload that unwraps eql_v2_encrypted type and delegates to JSONB version.
37+ -- !
38+ -- ! @param val eql_v2_encrypted Encrypted column value
39+ -- ! @return Text Base64-encoded ciphertext string
40+ -- ! @throws Exception if encrypted value is malformed
41+ -- !
42+ -- ! @example
43+ -- ! -- Extract ciphertext from encrypted column
44+ -- ! SELECT eql_v2.ciphertext(encrypted_email) FROM users;
45+ -- !
46+ -- ! @see eql_v2.ciphertext(jsonb)
47+ -- ! @see eql_v2.meta_data
2148CREATE FUNCTION eql_v2 .ciphertext(val eql_v2_encrypted)
2249 RETURNS text
2350 IMMUTABLE STRICT PARALLEL SAFE
@@ -27,26 +54,70 @@ AS $$
2754 END;
2855$$ LANGUAGE plpgsql;
2956
30-
57+ -- ! @brief State transition function for grouped_value aggregate
58+ -- ! @internal
59+ -- !
60+ -- ! Returns the first non-null value encountered. Used as state function
61+ -- ! for the grouped_value aggregate to select first value in each group.
62+ -- !
63+ -- ! @param $1 JSONB Accumulated state (first non-null value found)
64+ -- ! @param $2 JSONB New value from current row
65+ -- ! @return JSONB First non-null value (state or new value)
66+ -- !
67+ -- ! @see eql_v2.grouped_value
3168CREATE FUNCTION eql_v2 ._first_grouped_value(jsonb, jsonb)
3269RETURNS jsonb AS $$
3370 SELECT COALESCE($1 , $2 );
3471$$ LANGUAGE sql IMMUTABLE;
3572
36-
73+ -- ! @brief Return first non-null encrypted value in a group
74+ -- !
75+ -- ! Aggregate function that returns the first non-null encrypted value
76+ -- ! encountered within a GROUP BY clause. Useful for deduplication or
77+ -- ! selecting representative values from grouped encrypted data.
78+ -- !
79+ -- ! @param input JSONB Encrypted values to aggregate
80+ -- ! @return JSONB First non-null encrypted value in group
81+ -- !
82+ -- ! @example
83+ -- ! -- Get first email per user group
84+ -- ! SELECT user_id, eql_v2.grouped_value(encrypted_email)
85+ -- ! FROM user_emails
86+ -- ! GROUP BY user_id;
87+ -- !
88+ -- ! -- Deduplicate encrypted values
89+ -- ! SELECT DISTINCT ON (user_id)
90+ -- ! user_id,
91+ -- ! eql_v2.grouped_value(encrypted_ssn) as primary_ssn
92+ -- ! FROM user_records
93+ -- ! GROUP BY user_id;
94+ -- !
95+ -- ! @see eql_v2._first_grouped_value
3796CREATE AGGREGATE eql_v2 .grouped_value(jsonb) (
3897 SFUNC = eql_v2 ._first_grouped_value ,
3998 STYPE = jsonb
4099);
41100
42-
43- --
44- -- Adds eql_v2.check_encrypted constraint to the column_name in table_name
45- --
46- -- Executes the ALTER TABLE statement
47- -- `ALTER TABLE {table_name} ADD CONSTRAINT eql_v2_encrypted_check_{column_name} CHECK (eql_v2.check_encrypted({column_name}))`
48- --
49- --
101+ -- ! @brief Add validation constraint to encrypted column
102+ -- !
103+ -- ! Adds a CHECK constraint to ensure column values conform to encrypted data
104+ -- ! structure. Constraint uses eql_v2.check_encrypted to validate format.
105+ -- ! Called automatically by eql_v2.add_column.
106+ -- !
107+ -- ! @param table_name TEXT Name of table containing the column
108+ -- ! @param column_name TEXT Name of column to constrain
109+ -- ! @return Void
110+ -- !
111+ -- ! @example
112+ -- ! -- Manually add constraint (normally done by add_column)
113+ -- ! SELECT eql_v2.add_encrypted_constraint('users', 'encrypted_email');
114+ -- !
115+ -- ! -- Resulting constraint:
116+ -- ! -- ALTER TABLE users ADD CONSTRAINT eql_v2_encrypted_check_encrypted_email
117+ -- ! -- CHECK (eql_v2.check_encrypted(encrypted_email));
118+ -- !
119+ -- ! @see eql_v2.add_column
120+ -- ! @see eql_v2.remove_encrypted_constraint
50121CREATE FUNCTION eql_v2 .add_encrypted_constraint(table_name TEXT , column_name TEXT )
51122 RETURNS void
52123AS $$
@@ -55,13 +126,22 @@ AS $$
55126 END;
56127$$ LANGUAGE plpgsql;
57128
58-
59- --
60- -- Removes the eql_v2.check_encrypted constraint from the column_name in table_name
61- --
62- -- Executes the ALTER TABLE statement
63- -- `ALTER TABLE {table_name} DROP CONSTRAINT eql_v2_encrypted_check_{column_name}`
64- --
129+ -- ! @brief Remove validation constraint from encrypted column
130+ -- !
131+ -- ! Removes the CHECK constraint that validates encrypted data structure.
132+ -- ! Called automatically by eql_v2.remove_column. Uses IF EXISTS to avoid
133+ -- ! errors if constraint doesn't exist.
134+ -- !
135+ -- ! @param table_name TEXT Name of table containing the column
136+ -- ! @param column_name TEXT Name of column to unconstrain
137+ -- ! @return Void
138+ -- !
139+ -- ! @example
140+ -- ! -- Manually remove constraint (normally done by remove_column)
141+ -- ! SELECT eql_v2.remove_encrypted_constraint('users', 'encrypted_email');
142+ -- !
143+ -- ! @see eql_v2.remove_column
144+ -- ! @see eql_v2.add_encrypted_constraint
65145CREATE FUNCTION eql_v2 .remove_encrypted_constraint(table_name TEXT , column_name TEXT )
66146 RETURNS void
67147AS $$
@@ -70,7 +150,21 @@ AS $$
70150 END;
71151$$ LANGUAGE plpgsql;
72152
73-
153+ -- ! @brief Extract metadata from encrypted JSONB value
154+ -- !
155+ -- ! Extracts index terms (i) and version (v) from a raw JSONB encrypted value.
156+ -- ! Returns metadata object containing searchable index terms without ciphertext.
157+ -- !
158+ -- ! @param val JSONB Raw encrypted value
159+ -- ! @return JSONB Metadata object with 'i' (index terms) and 'v' (version) fields
160+ -- !
161+ -- ! @example
162+ -- ! -- Extract metadata to inspect index terms
163+ -- ! SELECT eql_v2.meta_data('{"c":"...","i":{"unique":"abc123"},"v":1}'::jsonb);
164+ -- ! -- Returns: {"i":{"unique":"abc123"},"v":1}
165+ -- !
166+ -- ! @see eql_v2.meta_data(eql_v2_encrypted)
167+ -- ! @see eql_v2.ciphertext
74168CREATE FUNCTION eql_v2 .meta_data(val jsonb)
75169 RETURNS jsonb
76170 IMMUTABLE STRICT PARALLEL SAFE
@@ -83,7 +177,22 @@ AS $$
83177 END;
84178$$ LANGUAGE plpgsql;
85179
86-
180+ -- ! @brief Extract metadata from encrypted column value
181+ -- !
182+ -- ! Extracts index terms and version from an encrypted column value.
183+ -- ! Convenience overload that unwraps eql_v2_encrypted type and
184+ -- ! delegates to JSONB version.
185+ -- !
186+ -- ! @param val eql_v2_encrypted Encrypted column value
187+ -- ! @return JSONB Metadata object with 'i' (index terms) and 'v' (version) fields
188+ -- !
189+ -- ! @example
190+ -- ! -- Inspect index terms for encrypted column
191+ -- ! SELECT user_id, eql_v2.meta_data(encrypted_email) as email_metadata
192+ -- ! FROM users;
193+ -- !
194+ -- ! @see eql_v2.meta_data(jsonb)
195+ -- ! @see eql_v2.ciphertext
87196CREATE FUNCTION eql_v2 .meta_data(val eql_v2_encrypted)
88197 RETURNS jsonb
89198 IMMUTABLE STRICT PARALLEL SAFE
0 commit comments