Thursday, April 22, 2010

Pythonic PL/SQL

Pythonic PL/SQL exists!

I imagine it will grow in fits and starts as my job needs dictate, but it shouldn't ever be abandoned, since I'm incorporating it gleefully into my own production code.

So far, format (Python 3-style string formatting) and join are available. I'm not using object-oriented PL/SQL at all (I find it very unsatisfactory), so instead of
','.join(mylist), you use pythonic.join(',', mylist).

I'm very open to contributions from others! There's plenty of room to participate, since implementation has barely begun.

There are Quest Code Tester tests for it, too, but unfortunately that's a closed-source project and I have to check on what the rules are for releasing tests produced with it.

Of course, most hard-core PL/SQL developers will already have written or found code to handle basic, common operations like this. But, by sticking close to names and APIs we're familiar with from another increasingly well-known language, I hope to reduce the amount of brainspace you need to remember how to do it all.

Usage samples::
crewlist pythonic.string_list
:= pythonic.string_list ('Wash', 'Kaylee', 'Mal');
crewdictionary pythonic.string_dictionary;
template VARCHAR2 (100);
DBMS_OUTPUT.PUT_LINE (pythonic.join ('; ', crewlist));
-- output: "Wash; Kaylee; Mal"
SELECT name FROM crew ORDER BY name;
DBMS_OUTPUT.PUT_LINE (pythonic.join (' *** ', curs));
-- output: "Kaylee *** Mal *** Wash"
template := '{2}! {1} says we''d better replace that catalyzer!';
DBMS_OUTPUT.PUT_LINE (pythonic.format (template, NULL, 'Kaylee', 'Captain'));
-- output: "Captain! Kaylee says we'd better replace that catalyzer!"
DBMS_OUTPUT.PUT_LINE (pythonic.format (template, crewlist));
-- output: "Mal! Kaylee says we'd better replace that catalyzer!"
crewdictionary ('First Officer') := 'Zoe';
crewdictionary ('Public Relations') := 'Jayne';
template := '{First Officer}: (to {Public Relations}) "I can hurt you."';
DBMS_OUTPUT.PUT_LINE (pythonic.format (template, crewdictionary));
-- output: Zoe: (to Jayne) "I can hurt you."

No comments: