UL4ON is a lightweight text-based cross-platform object serialization format.
The functions in the Postgres schema ul4on make it possible to output UL4ON
encoded data that can then be parsed by any of the UL4ON implementations in
Python, Java and Javascript:
Define the following Postgres function:
create or replace function foo.ul4on_test()
returns text
language plpgsql
as $$
declare
v_dump text;
v_backrefs jsonb;
begin
call ul4on.begindict(v_dump, v_backrefs);
call ul4on.keystr(v_dump, v_backrefs, 'firstname', 'John');
call ul4on.keystr(v_dump, v_backrefs, 'lastname', 'Doe');
call ul4on.keydate(v_dump, v_backrefs, 'birthday', '2000-02-29'::date);
call ul4on.key(v_dump, v_backrefs, 'emails');
call ul4on.beginlist(v_dump, v_backrefs);
call ul4on.str(v_dump, v_backrefs, 'john@example.org');
call ul4on.str(v_dump, v_backrefs, 'jdoe@example.net');
call ul4on.endlist(v_dump, v_backrefs);
call ul4on.enddict(v_dump, v_backrefs);
return v_dump;
end;Then you can call this function and parse the result with the following Python code:
import psycopg
from ll import ul4on
db = psycopg.connect(...)
c = db.cursor()
c.execute("select foo.ul4on_test()")
dump = c.fetchone()[0]
data = ul4on.loads(dump)
print(data)This will print the parsed data::
{
'firstname': 'John',
'lastname': 'Doe',
'birthday': datetime.date(2000, 2, 29),
'emails': ['john@example.org', 'jdoe@example.net']
}vSQL provides a way to build Postgres SQL queries safely and dynamically using UL4 expressions. Instead of manually concatenating strings, you can express query logic with vSQL (a variant of UL4), which is then compiled into proper SQL. This approach eliminates the risky parts of query construction, effectively preventing SQL injection attacks, while offering the expressive power of an ORM without the overhead.
vsqlimpl.sql includes the "vSQL standard library" for Postgres. Import that
into your Postgres database to be able to use vSQL with that database.
The Python documentation contains more info about UL4ON and vSQL
- Walter Dörwald