-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introuduce
AnalyticsSqlBuilder
to handle DB-specific queries
- Loading branch information
1 parent
2738f17
commit c8afc2f
Showing
8 changed files
with
355 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...rvices/dhis-service-analytics/src/main/java/org/hisp/dhis/db/sql/AnalyticsSqlBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2004-2024, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the HISP project nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.db.sql; | ||
|
||
/** | ||
* Interface for resolving specific SQL queries for analytics, that requires custom logic that can't | ||
* be resolved by the default <code>SqlBuilder</code> implementations. | ||
*/ | ||
public interface AnalyticsSqlBuilder { | ||
/** | ||
* Returns the correct SQL based on the underlying database for fetching the event data values. | ||
* | ||
* @return a SQL snippet. | ||
*/ | ||
String getEventDataValues(); | ||
} |
75 changes: 75 additions & 0 deletions
75
...his-service-analytics/src/main/java/org/hisp/dhis/db/sql/AnalyticsSqlBuilderProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2004-2024, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the HISP project nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.db.sql; | ||
|
||
import java.util.Objects; | ||
import org.hisp.dhis.analytics.table.setting.AnalyticsTableSettings; | ||
import org.hisp.dhis.db.model.Database; | ||
import org.hisp.dhis.external.conf.DhisConfigurationProvider; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** Provider of {@link AnalyticsSqlBuilder} implementations. */ | ||
@Service | ||
public class AnalyticsSqlBuilderProvider { | ||
private final AnalyticsSqlBuilder analyticsSqlBuilder; | ||
|
||
public AnalyticsSqlBuilderProvider(AnalyticsTableSettings config) { | ||
Objects.requireNonNull(config); | ||
this.analyticsSqlBuilder = getSqlBuilder(config); | ||
} | ||
|
||
/** | ||
* Returns a {@link AnalyticsSqlBuilder} implementation based on the system configuration. | ||
* | ||
* @return a {@link AnalyticsSqlBuilder}. | ||
*/ | ||
public AnalyticsSqlBuilder getAnalyticsSqlBuilder() { | ||
return analyticsSqlBuilder; | ||
} | ||
|
||
/** | ||
* Returns the appropriate {@link AnalyticsSqlBuilder} implementation based on the system | ||
* configuration. | ||
* | ||
* @param config the {@link DhisConfigurationProvider}. | ||
* @return a {@link AnalyticsSqlBuilder}. | ||
*/ | ||
private AnalyticsSqlBuilder getSqlBuilder(AnalyticsTableSettings config) { | ||
Database database = config.getAnalyticsDatabase(); | ||
String catalog = config.getAnalyticsDatabaseCatalog(); | ||
String driverFilename = config.getAnalyticsDatabaseDriverFilename(); | ||
|
||
Objects.requireNonNull(database); | ||
|
||
return switch (database) { | ||
case DORIS -> new DorisAnalyticsSqlBuilder(); | ||
case CLICKHOUSE -> new ClickhouseAnalyticsSqlBuilder(); | ||
default -> new PostgresAnalyticsSqlBuilder(); | ||
}; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...s-service-analytics/src/main/java/org/hisp/dhis/db/sql/ClickhouseAnalyticsSqlBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2004-2024, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the HISP project nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.db.sql; | ||
|
||
public class ClickhouseAnalyticsSqlBuilder implements AnalyticsSqlBuilder { | ||
@Override | ||
public String getEventDataValues() { | ||
return "ev.eventdatavalues"; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...s/dhis-service-analytics/src/main/java/org/hisp/dhis/db/sql/DorisAnalyticsSqlBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2004-2024, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the HISP project nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.db.sql; | ||
|
||
public class DorisAnalyticsSqlBuilder implements AnalyticsSqlBuilder { | ||
@Override | ||
public String getEventDataValues() { | ||
return "ev.eventdatavalues"; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...his-service-analytics/src/main/java/org/hisp/dhis/db/sql/PostgresAnalyticsSqlBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2004-2024, University of Oslo | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the HISP project nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package org.hisp.dhis.db.sql; | ||
|
||
public class PostgresAnalyticsSqlBuilder implements AnalyticsSqlBuilder { | ||
|
||
/** | ||
* Returns a subquery that expand the event datavalue jsonb with two additional fields: | ||
* | ||
* <ul> | ||
* <li>value_name: the name of the organisation unit that the datavalue is associated with | ||
* <li>value_code: the code of the organisation unit that the datavalue is associated with | ||
* </ul> | ||
* | ||
* @return a SQL subquery. | ||
*/ | ||
@Override | ||
public String getEventDataValues() { | ||
return """ | ||
(select json_object_agg(l2.keys, l2.datavalue) as value | ||
from ( | ||
select l1.uid, | ||
l1.keys, | ||
json_strip_nulls(json_build_object( | ||
'value', l1.eventdatavalues -> l1.keys ->> 'value', | ||
'created', l1.eventdatavalues -> l1.keys ->> 'created', | ||
'storedBy', l1.eventdatavalues -> l1.keys ->> 'storedBy', | ||
'lastUpdated', l1.eventdatavalues -> l1.keys ->> 'lastUpdated', | ||
'providedElsewhere', l1.eventdatavalues -> l1.keys -> 'providedElsewhere', | ||
'value_name', (select ou.name | ||
from organisationunit ou | ||
where ou.uid = l1.eventdatavalues -> l1.keys ->> 'value'), | ||
'value_code', (select ou.code | ||
from organisationunit ou | ||
where ou.uid = l1.eventdatavalues -> l1.keys ->> 'value'))) as datavalue | ||
from (select inner_evt.*, jsonb_object_keys(inner_evt.eventdatavalues) keys | ||
from event inner_evt) as l1) as l2 | ||
where l2.uid = ev.uid | ||
group by l2.uid)::jsonb | ||
"""; | ||
} | ||
} |
Oops, something went wrong.