Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#134 new sprintf function allowing multiple key/value replacement #138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 100 additions & 1 deletion source/packages/oos_util_string.pkb
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,106 @@ as
return l_return;

end sprintf;



/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'll accept this function

* Does string replacement of keys by values
*
* @example
* SELECT oos_util_string.sprintf('Hello {firstName} {lastName}!',
* t_tab_key_value(t_rec_key_value('firstName', 'Nuno'),
* t_rec_key_value('lastName', 'Alves')
* )
* ) demo
* FROM dual;
*
* DEMO
* ------------------------------
* Hello Nuno Alves!
*
* @issue #134
*
* @author Nuno Alves
* @created 12-Nov-2016
* @param p_str Messsage to format using %s and %d replacement strings
* @param p_key_values Nested table of key value pairs to be replaced
* @param p_left_pattern Left string pattern, '{' by default
* @param p_right_pattern Right string pattern, '}' by default
* @return p_msg with strings replaced
*/
function sprintf(
p_str in varchar2,
p_key_values in t_tab_key_value,
p_left_pattern in varchar2 default '{',
p_right_pattern in varchar2 default '}')
return varchar2
as
l_return varchar2(4000);
begin
l_return := p_str;
if p_key_values IS NOT NULL AND p_key_values.exists(1)
then
for i in 1 .. p_key_values.count
loop
l_return := REPLACE(l_return, p_left_pattern || p_key_values(i).key || p_right_pattern, p_key_values(i).value);
end loop;
end if;

return l_return;
end sprintf;


/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all the code is in one PR I'll accept this but remove this function. It's already handled in standard sprintf

* Does string replacement by string position
*
* @example
* select oos_util_string.sprintf('hello {1} {2}!', t_tab_vc2('Nuno','Alves')) demo
* from dual;
*
* DEMO
* ------------------------------
* Hello Nuno Alves!
*
* select oos_util_string.sprintf('hello {2} {1}!', t_tab_vc2('Nuno','Alves')) demo
* from dual;
*
* DEMO
* ------------------------------
* Hello Alves Nuno!
* @issue #134
*
* @author Nuno Alves
* @created 12-Nov-2016
* @param p_str Messsage to format using %s and %d replacement strings
* @param p_tab_vc2 Nested table of replacement strings
* @param p_left_pattern Left string pattern, '{' by default
* @param p_right_pattern Right string pattern, '}' by default
* @return p_msg with strings replaced
*/
function sprintf(
p_str in varchar2,
p_tab_vc2 in t_tab_vc2,
p_left_pattern in varchar2 default '{',
p_right_pattern in varchar2 default '}')
return varchar2
as
l_tab_key_value t_tab_key_value := t_tab_key_value();
l_return varchar2(4000);
begin
l_return := p_str;
if p_tab_vc2 IS NOT NULL AND p_tab_vc2.exists(1)
then
for i in 1 .. p_tab_vc2.count
loop
l_tab_key_value.extend(1);
l_tab_key_value(i) := t_rec_key_value(i, p_tab_vc2(i));
end loop;
end if;

l_return := sprintf(l_return, l_tab_key_value, p_left_pattern, p_right_pattern);

return l_return;
end sprintf;

/**
* Converts delimited string to array
Expand Down
14 changes: 14 additions & 0 deletions source/packages/oos_util_string.pks
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ as
p_s9 in varchar2 default null,
p_s10 in varchar2 default null)
return varchar2;

function sprintf(
p_str in varchar2,
p_key_values in t_tab_key_value,
p_left_pattern in varchar2 default '{',
p_right_pattern in varchar2 default '}')
return varchar2;

function sprintf(
p_str in varchar2,
p_tab_vc2 in t_tab_vc2,
p_left_pattern in varchar2 default '{',
p_right_pattern in varchar2 default '}')
return varchar2;

function string_to_table(
p_string in clob,
Expand Down
13 changes: 13 additions & 0 deletions source/types/T_REC_KEY_VALUE.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--------------------------------------------------------
-- DDL for Type T_REC_KEY_VALUE
--------------------------------------------------------

CREATE OR REPLACE TYPE "T_REC_KEY_VALUE"
AS
OBJECT
(
KEY VARCHAR2(4000),
VALUE VARCHAR2(4000)
);

/
7 changes: 7 additions & 0 deletions source/types/T_TAB_KEY_VALUE.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--------------------------------------------------------
-- DDL for Type T_TAB_KEY_VALUE
--------------------------------------------------------

CREATE OR REPLACE TYPE "T_TAB_KEY_VALUE" AS TABLE OF T_REC_KEY_VALUE;

/
7 changes: 7 additions & 0 deletions source/types/T_TAB_VC2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--------------------------------------------------------
-- DDL for Type T_TAB_VC2
--------------------------------------------------------

CREATE OR REPLACE TYPE "T_TAB_VC2" as table of varchar2(4000);

/