It is very simple to Determine Whether Two Date Ranges Overlap. My case is a little more difficult, but there has to be a simple solution.
I have following tables:
CREATE TABLE catalog_version
(
id character varying(36) NOT NULL,
name character varying(255),
valid_from timestamp without time zone,
valid_to timestamp without time zone,
)
CREATE TABLE catalog_article
(
id character varying(36) NOT NULL,
code character varying(100),
catalog_version_id character varying(36),
valid_from timestamp without time zone,
valid_to timestamp without time zone,
)
Following restrictions:
- Catalog 1..1 - 0..n catalog versions.
- Catalog versions 1..1 - 0..n catalog articles.
- valid_from and valid_to are optional for BOTH versions and articles.
- If an article doesn't have a date_from, then it is valid when the version begins.
- If an article doesn't have a date_to, then it is valid until the version expires.
- There can't be two articles with the same code that are valid at the same time.
- The framework uses a query builder meaning that I can't use any database specific features.
Is there a way to detect in a simple way, if there are already valid articles at a given time, if a user tries to define a new catalog article?
Examples:
----------------------------------------Version A---------------------------------------I
I--Article ABC--I
I--Article ABC--I
Valid cases:
I-Article ABC-I
I-Article ABC-----------------
--Article ABC--I
Invalid cases:
-----------------------------------------Article ABC-------I
I----------Article ABC-----------------------------------
Does anyone have any idea if there is a way to simplify this task? The only solution I have at hand is to make AND-conditions for every possible case.