Sunday, September 7, 2008

First birthday

A quick post because this blog is exactly one year old today. In this year, the blog has had 19,227 visits and 24,782 page views. A picture of Google Analytics:



Some other facts:

Going to Oracle OpenWorld

Last Friday I finally got approved to visit Oracle OpenWorld as a blogger. It came as a surprise, because I had to wait 5 weeks for the approval. This can be fine if they had said in advance that an approval process can take up to 5 weeks. But without such a notice, it's hard to imagine why judging that a blog is indeed about Oracle and is not brand sparking new, can take that long. During those weeks I thought I had messed up my registration. Especially since I didn't fill in the blog's URL anywhere initially. Turned out you had to use the "Company name" field for the URL. But all is well now; I saved my company 2000+ dollars for a full conference pass. Which is still like some 100 euros nowadays :-).

The flight and hotel are booked as well. The hotels nearby the Moscone Center were already booked, and the closest one available was 14 kilometres (9 miles) away. It is not even called San Francisco anymore, but Brisbane. I think it must have a nice view at the San Francisco Bay though. On the other hand, this photograph makes me a little worried about being able to sleep if airplanes are flying this close. So if you see me yawning, it doesn't necessarily mean the presentation is boring...

It's hardly possible that a session is boring. There are just so many good sessions to choose from. At any time I will have a hard time choosing between several interesting ones. I filled up my entire schedule with presentations. At the end I realized that I completely missed all Oracle Develop sessions. And these are - most of the times - even better than the regular ones. And then there's the unconference. This will likely lead to some rescheduling as well. So I have decided to pick out only a few sessions I don't want to miss. At other time slots I have not decided anything yet. If you have some good tips about sessions or activities you think I am or should be interested in, I'd like to hear about it.

The sessions I will definitely try to attend:

  • Sunday 11.45 - 12.45
    All About Encryption
    by Thomas Kyte

  • Sunday 19.00 - ?
    Blogger's meetup at Thirsty Bear's Restaurant and Brewery

  • Monday 14.30 - 15.30
    Performance Fundamentals for Oracle Database 10g and Oracle Database 11g
    by John Beresniewicz, Uri Shaft and Graham Wood

  • Monday 17.30 - 18.30
    Web 2.0 Development with Oracle Application Express
    by Carl Backstrom

  • Tuesday 9.30 - 10.30
    The best way ...
    by Thomas Kyte

  • Tuesday 14.30 - 15.30
    The Power of the Oracle Application Express Repository
    by Patrick Wolf

  • Wednesday 11.30 - 12.30
    Oracle Application Express and Oracle XML database: a match made in the database
    by Mark Drake

  • Thursday 9.00 - 10.00
    The Danish Experiment: Oracle Database 11g Shock Upgrades
    by Mughees Minhas and Mogens Norgaard

  • Thursday 15.00 - 16.00
    Oracle Performance Management: Real-World Case Studies
    by Gaya Vaidyanatha


What a crappy software by the way, this schedule builder. It's Oracle unworthy. In my opinion, the user interface should be such that we can choose a time slot and then the software provides a list with the available sessions. Now you just get a huge list where titles don't fit into the box. To see the entire title of a session - and more importantly: the speaker - you have to double click the line. A popup box then appears which you cannot close, because the title is on top of the close button most of the time. You have to enlarge the box to separate the title from the close button. To be able to enlarge the box, it's possible you'll have to move the box a bit to the left. Oh well, it's better than having no such facility at all.

Enough whining, I'm going to OpenWorld for the first time and I am really looking forward to it!

Thursday, September 4, 2008

Apex training

Just finished a three day APEX training by John Scott and Dimitri Gielis of Apex Evangelists in my home city. It consisted of 10 one-and-a-half-hour presentations, one hands-on session and a quiz. The topics of the 10 presentations were well chosen. No time was wasted by explaining basic stuff. Instead they focused on some lesser known topics you won't necessarily encounter, like AJAX and JavaScript, customizing templates, security and debugging, to name my favourite ones. Unfortunately, there were also a few that I thought were either less interesting, a bit long winded or not properly explained. But overall it was interesting and I had lots of "wow, I didn't know this was possible as well" moments.

The best part - I think - was the hands-on session. There is no better way of learning than doing things yourself. Especially since the topic here was AJAX/Javascript, two areas I am not too familiar with. So this was a great opportunity to experience how relatively easy it is.

The quiz was hilarious at times. I can imagine the fun John and Dimitri must have had when coming up with some of the multiple choice options. The quiz was won by Rutger de Ruiter, who was just started a new APEX blog as you can see in the link.

If you have a little experience with APEX and want to learn more of this product, that is so nicely integrated with our beloved Oracle database, then I can definitely recommend this training.

One wish though: I have seen several "killer" features, as Dimitri likes to call it. But my killer feature would be to have APEX do real database authentication using proxy authentication. As it is now, you are always "APEX_PUBLIC_USER" in the database, or the user you specify in your database access descriptor. It would be so nice to be yourself, so features like auditing, database roles and privileges and virtual private database etcetera, would work out of the box. And yes, I know you can code around it using v('APP_USER'), but I like things as simple as they need to be, and coded or configured once: inside the database.

Monday, August 25, 2008

Implementing dynamic data constraints

Previous post: Implementing inter-entity rules



Create rules

These rules either originate from a decomposed inter-entity rule or they were invented on their own. Again you have two choices of implementing them: using database triggers or incorporating in your api's. And again it's a matter of whether you are willing to lose performance or not. You'll probably want to be consistent throughout all business rule implementations. Stick with a database trigger approach or with an api approach. On the plus side for database triggers this time, some create rules can be validated using a before statement insert trigger instead of for each row. This means you won't have to pay a performance penalty for the occasional rule like "You may not create a project assignment when the OUT_OF_BUDGET system variable is set." Although most create rules require querying some related record and will suffer the row-by-row processing performance penalty. When using an api approach, you'll again code all create rules to be able to handle a set of records for maximum performance.


Attribute transition rules

This is a category where it is actually convenient to use a database trigger. It's because of the easy access to the :old and :new values in a row level trigger. The example "Allowed transitions for civil state of an employee are Unmarried -> Married -> Divorced -> Married." can be implemented like this:

rwijk@ORA11G> create table employees
2 ( name varchar2(10) primary key
3 , civil_state varchar2(1) check (civil_state in ('U','M','D'))
4 )
5 /

Table created.

rwijk@ORA11G> insert into employees
2 select 'ALAN', 'U' from dual union all
3 select 'BOB', 'M' from dual union all
4 select 'CLARK', 'D' from dual
5 /

3 rows created.

rwijk@ORA11G> create trigger employees_bru
2 before update of civil_state on employees for each row
3 begin
4 if :old.civil_state || :new.civil_state not in ('UM','MD','DM')
5 then
6 raise_application_error(-20000,'Illegal transition of civil state');
7 end if;
8 end;
9 /

Trigger created.

rwijk@ORA11G> update employees
2 set civil_state = 'D'
3 where name = 'BOB'
4 /

1 row updated.

rwijk@ORA11G> update employees
2 set civil_state = 'U'
3 where name = 'BOB'
4 /
update employees
*
ERROR at line 1:
ORA-20000: Illegal transition of civil state
ORA-06512: at "RWIJK.EMPLOYEES_BRU", line 4
ORA-04088: error during execution of trigger 'RWIJK.EMPLOYEES_BRU'


Using the api approach you will have api-procedures for "getting_married" or "getting_a_divorce" where the check will be in the where clause. An error message can then be raised when 0 rows are updated (if sql%rowcount = 0 then raise_application_error...).


Transferable relationship rules

This one is easy using an api approach: don't offer an api-procedure that transfers a relationship. Using a database trigger approach is also quite simple: "if updating('<fk-column>') then raise_application_error(...); end if;"


Other update rules, modify rules and other delete rules

The same holds true for these rules as described in the section "Create rules".


Relationship rules

Relationship rules were also mentioned as a static data constraint, and now they appear again as a dynamic data constraint. Strictly speaking it is right to split them into two categories, although it is a little fancy. The dynamic relationship rules are implemented by the keywords ON DELETE CASCADE and ON DELETE SET NULL in the foreign key constraint and by the foreign key constraint itself. The example "If a department that still has employees is deleted, give an error" is what happens if you define a regular foreign key constraint. The ON DELETE CASCADE will automatically delete child records when the master is deleted, instead of erroring out. And the ON DELETE SET NULL will automatically orphan child records when the master is deleted.

Next post: Implementing change event rules

Thursday, August 21, 2008

Implementing inter-entity rules

Previous post in this serie: Implementing entity rules.



Relationship rules

Relationship rules are implemented by foreign keys. This seems very straightforward, but there is a little more to it. You might have to add a not null constraint to the foreign key column to implement the optionality of the relationship. Or an extra unique key constraint on the foreign key column to implement a 1:1 relationship. And when you have allowed N:M relationships in your entity-relationship-diagram, you might even have to implement extra intersection tables.

Also, a relationship rule doesn't necessarily have to be an inter-entity rule, in case of a recursive relationship, when a row refers to a row of the same entity. Because the drawing in an entity relationship diagram of such a relationship when drawn from the top bar of the entity to the side bar, resembles a pig's ear, it's often called as such. The best known example of such a relationship rule is the "EMP.MGR -> EMP.EMPNO" relationship in the EMP-table. In this case it's really an entity rule. But this change of classification doesn't matter much, since no code is involved, the relationship rules are typically not mentioned separately.


Restricted relationship rules

The example in the picture above "An employee can only be managed by an employee with job 'MANAGER'" is a restricted relationship rule, but it isn't exactly "inter-entity". But in general, they are. And no matter if the referring entity is the same entity or another one, the implementation of these kind of rules has become quite easy. The trick is to have a fast refreshable materialized view with a join of both ends of the relationship and the relevant attributes. Once you have the rows joined together into one materialized view row, the implementation is as easy as creating one check constraint. An example:

rwijk@ORA11G> create table myemps as select * from emp where 1=0
2 /

Table created.

rwijk@ORA11G> create materialized view log on myemps with rowid
2 /

Materialized view log created.

rwijk@ORA11G> create materialized view myemps_and_their_managers
2 refresh fast on commit
3 as
4 select e.rowid erid
5 , m.rowid mrid
6 , m.job
7 from myemps e
8 , myemps m
9 where e.mgr = m.empno
10 /

Materialized view created.

rwijk@ORA11G> alter table myemps_and_their_managers
2 add constraint mgr_is_manager_ck
3 check (job = 'MANAGER')
4 /

Table altered.

rwijk@ORA11G> insert into myemps (empno,ename,job,mgr)
2 values (1,'ALAN','MANAGER',null)
3 /

1 row created.

rwijk@ORA11G> insert into myemps (empno,ename,job,mgr)
2 values (2,'BOB','CLERK',1)
3 /

1 row created.

rwijk@ORA11G> commit
2 /

Commit complete.

rwijk@ORA11G> insert into myemps (empno,ename,job,mgr)
2 values (3,'CLIFF','SALESMAN',2)
3 /

1 row created.

rwijk@ORA11G> commit
2 /
commit
*
ERROR at line 1:
ORA-12008: error in materialized view refresh path
ORA-02290: check constraint (RWIJK.MGR_IS_MANAGER_CK) violated


Other inter-entity rules

For most other inter-entity rules the check-constraint-on-a-fast-refreshable-materialized-view solution will work as well, since most inter-entity rules describe a condition that exists between various attributes of different entities. The materialized view can take care of putting the related data in a single row and then the check constraint is a piece of cake.

The example in the picture above "The project assignment start and end dates for an employee should lie between the start and end dates defined for the project" can therefore be implemented in exactly the same way as restricted relationship rules:

rwijk@ORA11G> create table projects
2 ( id number(9) primary key
3 , name varchar2(30) not null
4 , startdate date not null
5 , enddate date not null
6 )
7 /

Table created.

rwijk@ORA11G> create table project_assignments
2 ( id number(9) primary key
3 , employee varchar2(10) not null
4 , project_id number(9) not null references projects(id)
5 , startdate date not null
6 , enddate date not null
7 )
8 /

Table created.

rwijk@ORA11G> create materialized view log on projects with rowid
2 /

Materialized view log created.

rwijk@ORA11G> create materialized view log on project_assignments with rowid
2 /

Materialized view log created.

rwijk@ORA11G> create materialized view projects_date_ranges
2 refresh fast on commit
3 as
4 select p.rowid prid
5 , pa.rowid parid
6 , p.startdate project_startdate
7 , p.enddate project_enddate
8 , pa.startdate assignment_startdate
9 , pa.enddate assignment_enddate
10 from projects p
11 , project_assignments pa
12 where p.id = pa.project_id
13 /

Materialized view created.

rwijk@ORA11G> alter table projects_date_ranges
2 add constraint valid_assignment_periods_ck
3 check ( assignment_startdate >= project_startdate
4 and assignment_enddate <= project_enddate
5 )
6 /

Table altered.

rwijk@ORA11G> insert into projects
2 values (1,'MJPS',date '2008-01-01',date '2008-12-31')
3 /

1 row created.

rwijk@ORA11G> insert into project_assignments
2 values (1,'ALAN',1,date '2008-01-01',date '2008-06-30')
3 /

1 row created.

rwijk@ORA11G> commit
2 /

Commit complete.

rwijk@ORA11G> insert into project_assignments
2 values (2,'BOB',1,date '2008-01-01',date '2009-01-01')
3 /

1 row created.

rwijk@ORA11G> commit
2 /
commit
*
ERROR at line 1:
ORA-12008: error in materialized view refresh path
ORA-02290: check constraint (RWIJK.VALID_ASSIGNMENT_PERIODS_CK) violated


But the question is: can all other inter-entity rules be implemented using a materialized view? All the ones I can think of, can be, since the attributes are always somehow related and can be joined. I would be very interested to know if someone who is reading this knows an inter-entity rule that cannot be implemented using materialized views. Theoretically such rules must exist.

And when you come across such a rule, you would have to go back to the old method of decomposing an inter-entity rule into several dynamic data constraints, one for each triggering moment. And then handle each one separately. For example, let's just assume that the business rule "The project assignment start and end dates for an employee should lie between the start and end dates defined for the project" cannot be implemented using materialized views. The triggering moments are:
  • insert of a new project assignment
  • update of an existing project assignment start and/or end date
  • update of the relation between project and project assignment
  • update of a project's start and/or end date

If you choose database triggers to implement these decomposed rules, you'll have to accomodate for all four situations and write code for each situation. When using an api approach, you'll have to map these four triggering moments to your existing api functions and procedures and see which one of these triggering moments is actually possible. You can maybe skip a few implementations this way. And then don't forget to check which business rules to enforce when adding an extra api procedure. And make sure the resulting code, whether using the database trigger approach or the api approach, takes care of serializing access for a correct implementation.

Next post: Implementing dynamic data constraints

Monday, August 18, 2008

The trouble with triggers

The September/October 2008 issue of Oracle magazine is out. In this issue you'll find an article by Tom Kyte called The Trouble with Triggers. It ties in nicely with the current series in this blog about implementing business rules. And if I wasn't able to convince you that database triggers are evil, I'm sure that Tom will.

Monday, August 11, 2008

Implementing entity rules

Previous post in this serie: Implementing attribute and tuple rules



Attribute rules and tuple rules were fairly easy. The fun part starts with the entity rules and - in a next post - the inter-entity rules. According to the picture above, entity rules come in two flavours: unique identifier rules and other entity rules. However, since the other entity rules can be implemented in two clearly distinct ways, I will describe a separate subclass which I will call aggregation entity rules. But first things first:


Unique Identifier Rules

The implementation of rules belonging to this subclass is trivial at first glance: by a unique constraint and the accompanying unique index. However, not all unique identifier rules are simply bound to one column or a set of columns. But with the help of function based unique indexes you can implement the complexer ones as well. An example:

Business rule: A customer can only have one non-processed order

rwijk@ORA11G> create table orders
2 ( id number(9)
3 , customer_id number(9)
4 , processed_indicator varchar2(1)
5 , constraint inv_processed_domain_ck check (processed_indicator in ('Y','N'))
6 )
7 /

Tabel is aangemaakt.

rwijk@ORA11G> create unique index non_processed_orders_uk on orders
2 ( decode(processed_indicator,'N',customer_id)
3 )
4 /

Index is aangemaakt.

rwijk@ORA11G> insert into orders values (1, 101, 'Y')
2 /

1 rij is aangemaakt.

rwijk@ORA11G> insert into orders values (2, 101, 'Y')
2 /

1 rij is aangemaakt.

rwijk@ORA11G> insert into orders values (3, 101, 'N')
2 /

1 rij is aangemaakt.

rwijk@ORA11G> insert into orders values (4, 101, 'N')
2 /
insert into orders values (4, 101, 'N')
*
FOUT in regel 1:
.ORA-00001: Schending van UNIQUE-beperking (RWIJK.NON_PROCESSED_ORDERS_UK).


rwijk@ORA11G> insert into orders values (4, 101, 'Y')
2 /

1 rij is aangemaakt.


It is even possible to create a deterministic function and use it in a unique function based index. So this subclass can be implemented entirely declarative.


Aggregation rules

All former other entity rules that check for some aggregated value, will be called aggregation rules here. The "No more than 20 departments are allowed" example mentioned in the picture is an aggregation rule. These rules can be implemented by a check constraint on a on commit refreshable materialized view. This means they will be validated at the transaction level instead of at statement level. This is just perfect for these kind of rules, because for an aggregation rule to stay valid, you'll sometimes have to modify more than one row. Here is an example:

Business rule: The sum of all percentages per month for a customer has to be 100%.

rwijk@ORA11G> create table payment_percentages
2 ( customer_id number(9)
3 , month number(2,0)
4 , percentage number(3,0)
5 )
6 /

Tabel is aangemaakt.

rwijk@ORA11G> create materialized view log on payment_percentages
2 with rowid (customer_id,percentage) including new values
3 /

Snapshotlog is aangemaakt.

rwijk@ORA11G> create materialized view mv
2 refresh fast on commit
3 as
4 select customer_id
5 , sum(percentage) total_percentage
6 , count(*) count_all
7 , count(percentage) count_percentage
8 from payment_percentages
9 group by customer_id
10 /

Snapshot is aangemaakt.

rwijk@ORA11G> alter materialized view mv
2 add constraint mv_total_percentage_100_ck
3 check (total_percentage = 100)
4 /

Snapshot is gewijzigd.

rwijk@ORA11G> desc mv
Naam Null? Type
----------------------------------------- -------- ----------------------------
CUSTOMER_ID NUMBER(9)
TOTAL_PERCENTAGE NUMBER
COUNT_ALL NUMBER
COUNT_PERCENTAGE NUMBER

And that's all there is to it. Note that some extra aggregates were added to the materialized views. This is mandatory as can be read here in the documentation about restriction on fast refresh on materialized views with aggregates. The materialized view log will capture all changes, the materialized will be refreshed on commit time, and the check constraint will automatically validate the new data. Here is an example of how to deal with inserting and updating:

rwijk@ORA11G> insert into payment_percentages
2 select 1
3 , level
4 , 10
5 from dual
6 connect by level <= 8
7 /

8 rijen zijn aangemaakt.

rwijk@ORA11G> insert into payment_percentages
2 select 1
3 , 8+ level
4 , 5
5 from dual
6 connect by level <= 4
7 /

4 rijen zijn aangemaakt.

rwijk@ORA11G> commit
2 /

Commit is voltooid.


This commit is when the check constraint mv_total_percentage_100_ck on the materialized view gets validated. The sum of all percentages is calculated and it is 100%, so no error is raised.

rwijk@ORA11G> select customer_id
2 , month
3 , sum(percentage)
4 from payment_percentages
5 group by rollup ((customer_id,month))
6 order by month
7 /

CUSTOMER_ID MONTH SUM(PERCENTAGE)
----------- ---------- ---------------
1 1 10
1 2 10
1 3 10
1 4 10
1 5 10
1 6 10
1 7 10
1 8 10
1 9 5
1 10 5
1 11 5
1 12 5
100

13 rijen zijn geselecteerd.

rwijk@ORA11G> update payment_percentages
2 set percentage = 15
3 where customer_id = 1
4 and month = 11
5 /

1 rij is bijgewerkt.

rwijk@ORA11G> commit
2 /
commit
*
FOUT in regel 1:
.ORA-12008: Fout in pad voor vernieuwen van snapshot.
ORA-02290: CHECK-beperking (RWIJK.MV_TOTAL_PERCENTAGE_100_CK) is geschonden.


But now the sum is more than 100% and so the above error is raised. To successfully change a few values, several separate update statements are needed, like this:

rwijk@ORA11G> update payment_percentages
2 set percentage = 15
3 where customer_id = 1
4 and month = 11
5 /

1 rij is bijgewerkt.

rwijk@ORA11G> update payment_percentages
2 set percentage = 8
3 where customer_id = 1
4 and month = 2
5 /

1 rij is bijgewerkt.

rwijk@ORA11G> update payment_percentages
2 set percentage = 2
3 where customer_id = 1
4 and month = 5
5 /

1 rij is bijgewerkt.

rwijk@ORA11G> commit
2 /

Commit is voltooid.

rwijk@ORA11G> select customer_id
2 , month
3 , sum(percentage)
4 from payment_percentages
5 group by rollup ((customer_id,month))
6 order by month
7 /

CUSTOMER_ID MONTH SUM(PERCENTAGE)
----------- ---------- ---------------
1 1 10
1 2 8
1 3 10
1 4 10
1 5 2
1 6 10
1 7 10
1 8 10
1 9 5
1 10 5
1 11 15
1 12 5
100

13 rijen zijn geselecteerd.

And the sum is 100% again.


Other entity rules

Because of the split-off of the aggregation rules, this subclass has become a little smaller, but it still represents a good proportion of all business rules. The most known example of an other entity rule is an overlap check on several periods (startdate and enddate) belonging to a same group. This subclass is one we cannot solve declaratively, or at least it is not known by me. So we have to make a choice here: implement them using database triggers or implement them inside your api.

Business rule: The periods of one customer are not allowed to overlap.

Implementing other entity rules with database triggers

You can see an example of a period overlap check - amidst the implementation of two other entity rules - in this post about a test with RuleGen. Check for the "check_period_overlap" procedure. As you can see, the trickier parts are to serialize access by customer_id and circumventing the mutating table error. Trickier, but standard. The advantage of using database triggers to enforce your other entity rules, is that once it is in place, you are covered. And although I generally think that database triggers are evil, the static data constraint rules are the only place they can be acceptable if you are willing to lose performance.

Implementing other entity rules inside your api

If you are not willing to compromise performance then you should take this approach. In the previously mentioned post you'll also see an example of validating this rule using an api. It is implemented here as part of that one api procedure. But what to do when more than one api procedure can violate the business rule? You'll want to prevent double coding, but you'll also want it to be optimized for single row online inserts and updates, as well as in batch inserts and updates at night. An answer to this problem is to code a business rule validation procedure only once and have it accept a table of values/records by creating a SQL type. The SQL type will be the input parameter of the validation procedure. The single row update procedure will call it with a table with 1 value/record, and the batch update procedure will deliver many more records. An example:

rwijk@ORA11G> create table subscriptions
2 ( id number(9) primary key
3 , customer_id number(9) not null
4 , startdate date not null
5 , enddate date
6 , amount number(6)
7 , unique (customer_id,startdate)
8 )
9 /

Tabel is aangemaakt.

rwijk@ORA11G> insert /*+ append */ into subscriptions
2 select level
3 , ceil(level/2)
4 , case mod(level,2) when 0 then date '2006-01-01' else date '2007-01-01' end
5 , case mod(level,2) when 0 then date '2007-01-01' else date '2008-01-01' end
6 , 0
7 from dual
8 connect by level <= 10000
9 /

10000 rijen zijn aangemaakt.

rwijk@ORA11G> create type tt_customer_ids is table of number(9);
2 /

Type is aangemaakt.

rwijk@ORA11G> create package subscriptions_api
2 as
3 procedure single_row_update
4 ( p_id in number
5 , p_startdate in date
6 , p_enddate in date
7 );
8 procedure batch_update
9 ;
10 end subscriptions_api;
11 /

Package is aangemaakt.

rwijk@ORA11G> create package body subscriptions_api
2 as
3 procedure lock_customers(p_customer_ids tt_customer_ids)
4 is
5 l_lock_dummy integer;
6 begin
7 for i in 1 .. p_customer_ids.count
8 loop
9 l_lock_dummy := dbms_lock.request
10 ( release_on_commit => true
11 , id => dbms_utility.get_hash_value
12 ( name => 'contracts_pkg.lock_customer' || to_char(p_customer_ids(i))
13 , base => 1
14 , hash_size => power(2,30)
15 )
16 );
17 end loop;
18 end lock_customers
19 ;
20 function exists_overlap(p_customer_ids tt_customer_ids) return boolean
21 is
22 l_customer_id_dummy subscriptions.customer_id%type;
23 begin
24 select customer_id
25 into l_customer_id_dummy
26 from ( select s.customer_id
27 , sign
28 ( lead(s.startdate,1,date '9999-12-31')
29 over
30 (partition by s.customer_id order by s.startdate)
31 - nvl(s.enddate,date '9999-12-31')
32 ) sgn
33 from subscriptions s
34 where s.customer_id in
35 ( select /*+ cardinality (c 1) */
36 column_value
37 from table(p_customer_ids) c
38 )
39 )
40 where sgn = -1
41 ;
42 return true;
43 exception
44 when no_data_found then
45 return false;
46 when too_many_rows then
47 return true;
48 end exists_overlap
49 ;
50 procedure check_period_overlap (p_customer_ids tt_customer_ids)
51 is
52 begin
53 if exists_overlap(p_customer_ids)
54 then
55 raise_application_error
56 ( -20001
57 , 'Periods of subscription of one customer cannot overlap.'
58 );
59 end if;
60 end check_period_overlap
61 ;
62 procedure single_row_update
63 ( p_id in number
64 , p_startdate in date
65 , p_enddate in date
66 )
67 is
68 l_customer_ids tt_customer_ids;
69 begin
70 update subscriptions
71 set startdate = p_startdate
72 , enddate = p_enddate
73 where id = p_id
74 returning customer_id bulk collect into l_customer_ids
75 ;
76 lock_customers(l_customer_ids);
77 check_period_overlap(l_customer_ids)
78 ;
79 end single_row_update
80 ;
81 procedure batch_update
82 is
83 l_customer_ids tt_customer_ids;
84 begin
85 update subscriptions
86 set enddate = enddate + interval '1' year
87 where amount = 0
88 returning customer_id bulk collect into l_customer_ids
89 ;
90 lock_customers(l_customer_ids);
91 check_period_overlap(l_customer_ids)
92 ;
93 end batch_update
94 ;
95 end subscriptions_api;
96 /

Package-body is aangemaakt.

rwijk@ORA11G> begin
2 subscriptions_api.single_row_update
3 ( p_id => 1234
4 , p_startdate => date '2006-01-01'
5 , p_enddate => date '2008-01-01'
6 );
7 end;
8 /
begin
*
FOUT in regel 1:
.ORA-20001: Periods of subscription of one customer cannot overlap.
ORA-06512: in "RWIJK.SUBSCRIPTIONS_API", regel 55
ORA-06512: in "RWIJK.SUBSCRIPTIONS_API", regel 77
ORA-06512: in regel 2


rwijk@ORA11G> begin
2 subscriptions_api.batch_update;
3 end;
4 /
begin
*
FOUT in regel 1:
.ORA-20001: Periods of subscription of one customer cannot overlap.
ORA-06512: in "RWIJK.SUBSCRIPTIONS_API", regel 55
ORA-06512: in "RWIJK.SUBSCRIPTIONS_API", regel 91
ORA-06512: in regel 2

A few things to note:
  1. All customer records are validated in this implementation, which is good when only few records per customer are expected. When lots of records per customer are expected, this will be less feasible.

  2. The cardinality hint is used to not use the default blocksize cardinality (8K here), but 1. The resulting plan will therefore favour the use of an index on customer_id (and startdate).

  3. You can extend this example by using error logging and capturing all records that violate the business rule, and even report all customer_id's and periods in the error message.

  4. When implementing several business rules in your api, you will want to use only one SQL type, so the resulting type will have to accomodate for all columns needed in all business rules.

  5. I have not much experience with this method yet, but I will use and test it in the near future. If anything noteworthy will occur, it will be on this blog of course.


Next post: Implementing inter-entity rules