oos-utils icon indicating copy to clipboard operation
oos-utils copied to clipboard

Functions to convert a key value pair to a formatted string

Open janihur opened this issue 9 years ago • 5 comments

The functions are mainly useful for logging.

Example:

v_foo := 42;
-- returns a string: '(v_foo = 42)'
kvn('v_foo', v_foo);
kv('v_foo', v_foo);

v_bar := null;
-- returns a string: '(v_bar = NULL)'
kvn('v_bar', v_bar);
-- returns an empty string (null)
kv('v_bar', v_bar);

Proposed function declarations for common types:

--
-- Return a string: (p_key = p_value) and if p_value is null: (p_key =
-- NULL). Useful for logging.
--

function kvn(p_key in varchar2, p_value in varchar2) return varchar2;
function kvn(p_key in varchar2, p_value in number) return varchar2;
function kvn(p_key in varchar2, p_value in date,
             p_fmt in varchar2 default 'YYYY-MM-DD HH24:MI:SS')
  return varchar2;
function kvn(p_key in varchar2, p_value in boolean) return varchar2;

--
-- Return a string: (p_key = p_value) and empty string (null) if p_value is
-- null. Useful for logging.
--

function kv(p_key in varchar2, p_value in varchar2) return varchar2;
function kv(p_key in varchar2, p_value in number) return varchar2;
function kv(p_key in varchar2, p_value in date,
            p_fmt in varchar2 default 'YYYY-MM-DD HH24:MI:SS')
  return varchar2;
function kv(p_key in varchar2, p_value in boolean) return varchar2;

janihur avatar May 20 '16 04:05 janihur

@janihur This sounds similar to what we use for Logger: https://github.com/OraOpenSource/Logger/blob/master/docs/Logger%20API.md#append_param

If this is primarily for logging then perhaps we may not need to add it as Logger has this main feature.

martindsouza avatar May 23 '16 01:05 martindsouza

I checked the logger. Yes - the feature is essentially the same. I use these functions to feed data to my super simple logging "framework" that eats strings only (varchar2 and clob). The main reason why variable stringification and logging are separate is A) the evolution of the legacy system I'm current working with and B) the need to feed the same data to different logging systems.

In one way it makes sense to have this with logger but on the other hand I don't think one should assume too much what other libraries oos-util users have available. IMO from oos-util point of view it's irrelevant what features other OraOpenSource projects have.

I think that I'll work on the feature a bit more in https://github.com/janihur/oos-utils-sandbox as I got some inspiration from the Logger. I will let you (@martindsouza) know here when ready. I might also introduce my logging system there (even I think it's not in the scope of oos-util, but maybe an alternative for Logger). Meanwhile this issue can be labelled with enhancement/future/whatever.

janihur avatar May 24 '16 05:05 janihur

An example of the usage scope of kv and kvn functions is now available in https://github.com/janihur/oos-utils-sandbox/blob/master/oos_issue_91_example.sql

janihur avatar May 30 '16 07:05 janihur

I agree with @janihur. The fact that oos-utils is (will be) used in a project does not imply that Logger is used too.

jzaldokas avatar Jun 03 '16 12:06 jzaldokas

@janihur I agree that we should not assume dependancies on Logger or any other packages.

I've tagged this for a future release. For 1.0.0 we won't add it in.

martindsouza avatar Aug 08 '16 05:08 martindsouza