From 93e3acc08de19f8c55762c2ea4fc7461caed2af8 Mon Sep 17 00:00:00 2001 From: David Benedeki Date: Thu, 6 Jul 2023 15:06:28 +0200 Subject: [PATCH 1/5] #51: Simplify usage by offering combined classes * `DBMultipleResultFunction` added * `DBMultipleResultFunctionWithStatusSupport` added * `DBOptionalResultFunction` added * `DBOptionalResultFunctionWithStatusSupport` added * `DBSingleResultFunction` added * `DBSingleResultFunctionWithStatusSupport` --- .../za/co/absa/fadb/slick/pg/DBFunction.scala | 278 ++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala diff --git a/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala b/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala new file mode 100644 index 00000000..1cbb4054 --- /dev/null +++ b/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala @@ -0,0 +1,278 @@ +/* + * Copyright 2023 ABSA Group Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package za.co.absa.fadb.slick.pg + +import za.co.absa.fadb.DBSchema +import za.co.absa.fadb.slick.{SlickFunction, SlickFunctionWithStatusSupport, SlickPgEngine} +import za.co.absa.fadb.DBFunction.{DBMultipleResultFunction => CoreDBMultipleResultFunction, DBOptionalResultFunction => CoreDBOptionalResultFunction, DBSingleResultFunction => CoreDBSingleResultFunction} + +object DBFunction { + /** + * Represents a function returning a set (in DB sense) of rows + * + * @param functionNameOverride - in case the class name would not match the database function name, this gives the + * possibility of override + * @param schema - the schema the function belongs into + * @param dBEngine - the database engine that is supposed to execute the function (presumably contains + * connection to the database + * @tparam I - the type covering the input fields of the database function + * @tparam R - the type covering the returned fields from the database function + */ + abstract class DBMultipleResultFunction[I, R](functionNameOverride: Option[String] = None) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) + extends CoreDBMultipleResultFunction[I, R, SlickPgEngine](functionNameOverride) + with SlickFunction[I, R] { + + def this(functionNameOverride: String) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema, functionNameOverride: String) + (implicit dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema) + (implicit dBEngine: SlickPgEngine) = { + this(None)(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine, functionNameOverride: String) + (implicit schema: DBSchema) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine) + (implicit schema: DBSchema) = { + this(None)(schema, dBEngine) + } + } + + /** + * Represents a function returning exactly one record + * + * @param functionNameOverride - in case the class name would not match the database function name, this gives the + * possibility of override + * @param schema - the schema the function belongs into + * @param dBEngine - the database engine that is supposed to execute the function (presumably contains + * connection to the database + * @tparam I - the type covering the input fields of the database function + * @tparam R - the type covering the returned fields from the database function + */ + abstract class DBSingleResultFunction[I, R](functionNameOverride: Option[String] = None) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) + extends CoreDBSingleResultFunction[I, R, SlickPgEngine](functionNameOverride) + with SlickFunction[I, R] { + + def this(functionNameOverride: String) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema, functionNameOverride: String) + (implicit dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema) + (implicit dBEngine: SlickPgEngine) = { + this(None)(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine, functionNameOverride: String) + (implicit schema: DBSchema) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine) + (implicit schema: DBSchema) = { + this(None)(schema, dBEngine) + } + } + + /** + * Represents a function returning one optional record + * + * @param functionNameOverride - in case the class name would not match the database function name, this gives the + * possibility of override + * @param schema - the schema the function belongs into + * @param dBEngine - the database engine that is supposed to execute the function (presumably contains + * connection to the database + * @tparam I - the type covering the input fields of the database function + * @tparam R - the type covering the returned fields from the database function + */ + abstract class DBOptionalResultFunction[I, R](functionNameOverride: Option[String] = None) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) + extends CoreDBOptionalResultFunction[I, R, SlickPgEngine](functionNameOverride) + with SlickFunction[I, R]{ + + def this(functionNameOverride: String) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema, functionNameOverride: String) + (implicit dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema) + (implicit dBEngine: SlickPgEngine) = { + this(None)(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine, functionNameOverride: String) + (implicit schema: DBSchema) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine) + (implicit schema: DBSchema) = { + this(None)(schema, dBEngine) + } + } + +// --------------------------------------------------------------------------------------------------------------------- + + /** + * Represents a function returning a set (in DB sense) of rows + * + * @param functionNameOverride - in case the class name would not match the database function name, this gives the + * possibility of override + * @param schema - the schema the function belongs into + * @param dBEngine - the database engine that is supposed to execute the function (presumably contains + * connection to the database + * @tparam I - the type covering the input fields of the database function + * @tparam R - the type covering the returned fields from the database function + */ + abstract class DBMultipleResultFunctionWithStatusSupport[I, R](functionNameOverride: Option[String] = None) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) + extends CoreDBMultipleResultFunction[I, R, SlickPgEngine](functionNameOverride) + with SlickFunctionWithStatusSupport[I, R] { + + def this(functionNameOverride: String) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema, functionNameOverride: String) + (implicit dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema) + (implicit dBEngine: SlickPgEngine) = { + this(None)(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine, functionNameOverride: String) + (implicit schema: DBSchema) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine) + (implicit schema: DBSchema) = { + this(None)(schema, dBEngine) + } + } + + /** + * Represents a function returning exactly one record + * + * @param functionNameOverride - in case the class name would not match the database function name, this gives the + * possibility of override + * @param schema - the schema the function belongs into + * @param dBEngine - the database engine that is supposed to execute the function (presumably contains + * connection to the database + * @tparam I - the type covering the input fields of the database function + * @tparam R - the type covering the returned fields from the database function + */ + abstract class DBSingleResultFunctionWithStatusSupport[I, R](functionNameOverride: Option[String] = None) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) + extends CoreDBSingleResultFunction[I, R, SlickPgEngine](functionNameOverride) + with SlickFunctionWithStatusSupport[I, R] { + + def this(functionNameOverride: String) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema, functionNameOverride: String) + (implicit dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema) + (implicit dBEngine: SlickPgEngine) = { + this(None)(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine, functionNameOverride: String) + (implicit schema: DBSchema) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine) + (implicit schema: DBSchema) = { + this(None)(schema, dBEngine) + } + } + + /** + * Represents a function returning one optional record + * + * @param functionNameOverride - in case the class name would not match the database function name, this gives the + * possibility of override + * @param schema - the schema the function belongs into + * @param dBEngine - the database engine that is supposed to execute the function (presumably contains + * connection to the database + * @tparam I - the type covering the input fields of the database function + * @tparam R - the type covering the returned fields from the database function + */ + abstract class DBOptionalResultFunctionWithStatusSupport[I, R](functionNameOverride: Option[String] = None) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) + extends CoreDBOptionalResultFunction[I, R, SlickPgEngine](functionNameOverride) + with SlickFunctionWithStatusSupport[I, R]{ + + def this(functionNameOverride: String) + (implicit schema: DBSchema, dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema, functionNameOverride: String) + (implicit dBEngine: SlickPgEngine) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(schema: DBSchema) + (implicit dBEngine: SlickPgEngine) = { + this(None)(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine, functionNameOverride: String) + (implicit schema: DBSchema) = { + this(Option(functionNameOverride))(schema, dBEngine) + } + + def this(dBEngine: SlickPgEngine) + (implicit schema: DBSchema) = { + this(None)(schema, dBEngine) + } + } + +} From 60c912a931ead0a4912c473a26e70ece39af411b Mon Sep 17 00:00:00 2001 From: David Benedeki <14905969+benedeki@users.noreply.github.com> Date: Mon, 10 Jul 2023 15:10:18 +0200 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: Ladislav Sulak --- .../main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala b/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala index 1cbb4054..2a49cc27 100644 --- a/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala +++ b/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala @@ -18,7 +18,11 @@ package za.co.absa.fadb.slick.pg import za.co.absa.fadb.DBSchema import za.co.absa.fadb.slick.{SlickFunction, SlickFunctionWithStatusSupport, SlickPgEngine} -import za.co.absa.fadb.DBFunction.{DBMultipleResultFunction => CoreDBMultipleResultFunction, DBOptionalResultFunction => CoreDBOptionalResultFunction, DBSingleResultFunction => CoreDBSingleResultFunction} +import za.co.absa.fadb.DBFunction.{ + DBMultipleResultFunction => CoreDBMultipleResultFunction, + DBOptionalResultFunction => CoreDBOptionalResultFunction, + DBSingleResultFunction => CoreDBSingleResultFunction +} object DBFunction { /** @@ -64,7 +68,7 @@ object DBFunction { } /** - * Represents a function returning exactly one record + @see [[za.co.absa.fadb.DBFunction.DBSingleResultFunction]] * * @param functionNameOverride - in case the class name would not match the database function name, this gives the * possibility of override From 52bc10ae160d91459ba0af93aaf5e8566d223d54 Mon Sep 17 00:00:00 2001 From: David Benedeki Date: Tue, 11 Jul 2023 08:10:44 +0200 Subject: [PATCH 3/5] * ScalaDoc rather as reference --- .../za/co/absa/fadb/slick/pg/DBFunction.scala | 73 +++++-------------- 1 file changed, 17 insertions(+), 56 deletions(-) diff --git a/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala b/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala index 2a49cc27..7cf03d80 100644 --- a/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala +++ b/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala @@ -19,22 +19,15 @@ package za.co.absa.fadb.slick.pg import za.co.absa.fadb.DBSchema import za.co.absa.fadb.slick.{SlickFunction, SlickFunctionWithStatusSupport, SlickPgEngine} import za.co.absa.fadb.DBFunction.{ - DBMultipleResultFunction => CoreDBMultipleResultFunction, - DBOptionalResultFunction => CoreDBOptionalResultFunction, + DBMultipleResultFunction => CoreDBMultipleResultFunction, + DBOptionalResultFunction => CoreDBOptionalResultFunction, DBSingleResultFunction => CoreDBSingleResultFunction } object DBFunction { /** - * Represents a function returning a set (in DB sense) of rows - * - * @param functionNameOverride - in case the class name would not match the database function name, this gives the - * possibility of override - * @param schema - the schema the function belongs into - * @param dBEngine - the database engine that is supposed to execute the function (presumably contains - * connection to the database - * @tparam I - the type covering the input fields of the database function - * @tparam R - the type covering the returned fields from the database function + * Combines [[za.co.absa.fadb.DBFunction.DBMultipleResultFunction DBMultipleResultFunction]] with Slick and Postgres support + * @see See [[za.co.absa.fadb.DBFunction.DBMultipleResultFunction]] and [[za.co.absa.fadb.slick.SlickFunction]] for methods description */ abstract class DBMultipleResultFunction[I, R](functionNameOverride: Option[String] = None) (implicit schema: DBSchema, dBEngine: SlickPgEngine) @@ -68,15 +61,8 @@ object DBFunction { } /** - @see [[za.co.absa.fadb.DBFunction.DBSingleResultFunction]] - * - * @param functionNameOverride - in case the class name would not match the database function name, this gives the - * possibility of override - * @param schema - the schema the function belongs into - * @param dBEngine - the database engine that is supposed to execute the function (presumably contains - * connection to the database - * @tparam I - the type covering the input fields of the database function - * @tparam R - the type covering the returned fields from the database function + * Combines [[za.co.absa.fadb.DBFunction.DBSingleResultFunction DBSingleResultFunction]] with Slick and Postgres support + * @see See [[za.co.absa.fadb.DBFunction.DBSingleResultFunction]] and [[za.co.absa.fadb.slick.SlickFunction]] for methods description */ abstract class DBSingleResultFunction[I, R](functionNameOverride: Option[String] = None) (implicit schema: DBSchema, dBEngine: SlickPgEngine) @@ -110,15 +96,8 @@ object DBFunction { } /** - * Represents a function returning one optional record - * - * @param functionNameOverride - in case the class name would not match the database function name, this gives the - * possibility of override - * @param schema - the schema the function belongs into - * @param dBEngine - the database engine that is supposed to execute the function (presumably contains - * connection to the database - * @tparam I - the type covering the input fields of the database function - * @tparam R - the type covering the returned fields from the database function + * Combines [[za.co.absa.fadb.DBFunction.DBOptionalResultFunction DBOptionalResultFunction]] with Slick and Postgres support + * @see See [[za.co.absa.fadb.DBFunction.DBOptionalResultFunction]] and [[za.co.absa.fadb.slick.SlickFunction]] for methods description */ abstract class DBOptionalResultFunction[I, R](functionNameOverride: Option[String] = None) (implicit schema: DBSchema, dBEngine: SlickPgEngine) @@ -154,15 +133,9 @@ object DBFunction { // --------------------------------------------------------------------------------------------------------------------- /** - * Represents a function returning a set (in DB sense) of rows - * - * @param functionNameOverride - in case the class name would not match the database function name, this gives the - * possibility of override - * @param schema - the schema the function belongs into - * @param dBEngine - the database engine that is supposed to execute the function (presumably contains - * connection to the database - * @tparam I - the type covering the input fields of the database function - * @tparam R - the type covering the returned fields from the database function + * Combines [[za.co.absa.fadb.DBFunction.DBMultipleResultFunction DBMultipleResultFunction]] with Slick, Postgres and + * status codes support + * @see See [[za.co.absa.fadb.DBFunction.DBMultipleResultFunction]] and [[za.co.absa.fadb.slick.SlickFunctionWithStatusSupport]] for methods description */ abstract class DBMultipleResultFunctionWithStatusSupport[I, R](functionNameOverride: Option[String] = None) (implicit schema: DBSchema, dBEngine: SlickPgEngine) @@ -196,15 +169,9 @@ object DBFunction { } /** - * Represents a function returning exactly one record - * - * @param functionNameOverride - in case the class name would not match the database function name, this gives the - * possibility of override - * @param schema - the schema the function belongs into - * @param dBEngine - the database engine that is supposed to execute the function (presumably contains - * connection to the database - * @tparam I - the type covering the input fields of the database function - * @tparam R - the type covering the returned fields from the database function + * Combines [[za.co.absa.fadb.DBFunction.DBSingleResultFunction DBSingleResultFunction]] with Slick, Postgres and + * status codes support + * @see See [[za.co.absa.fadb.DBFunction.DBSingleResultFunction]] and [[za.co.absa.fadb.slick.SlickFunctionWithStatusSupport]] for methods description */ abstract class DBSingleResultFunctionWithStatusSupport[I, R](functionNameOverride: Option[String] = None) (implicit schema: DBSchema, dBEngine: SlickPgEngine) @@ -238,15 +205,9 @@ object DBFunction { } /** - * Represents a function returning one optional record - * - * @param functionNameOverride - in case the class name would not match the database function name, this gives the - * possibility of override - * @param schema - the schema the function belongs into - * @param dBEngine - the database engine that is supposed to execute the function (presumably contains - * connection to the database - * @tparam I - the type covering the input fields of the database function - * @tparam R - the type covering the returned fields from the database function + * Combines [[za.co.absa.fadb.DBFunction.DBOptionalResultFunction DBOptionalResultFunction]] with Slick, Postgres and + * status codes support + * @see See [[za.co.absa.fadb.DBFunction.DBOptionalResultFunction]] and [[za.co.absa.fadb.slick.SlickFunctionWithStatusSupport]] for methods description */ abstract class DBOptionalResultFunctionWithStatusSupport[I, R](functionNameOverride: Option[String] = None) (implicit schema: DBSchema, dBEngine: SlickPgEngine) From 0cb1e229412b72c7036462d8bbde8d76a8a2f8ac Mon Sep 17 00:00:00 2001 From: David Benedeki Date: Tue, 11 Jul 2023 08:12:57 +0200 Subject: [PATCH 4/5] * LICENCE fix --- slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala b/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala index 7cf03d80..52ce1840 100644 --- a/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala +++ b/slick/src/main/scala/za/co/absa/fadb/slick/pg/DBFunction.scala @@ -1,5 +1,5 @@ /* - * Copyright 2023 ABSA Group Limited + * Copyright 2022 ABSA Group Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 1e2375b3170eb32b368beec33db3b9d722cd9577 Mon Sep 17 00:00:00 2001 From: David Benedeki Date: Wed, 23 Aug 2023 18:15:38 +0200 Subject: [PATCH 5/5] * --- slick/src/main/scala/za/co/absa/fadb/slick/SlickFunction.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slick/src/main/scala/za/co/absa/fadb/slick/SlickFunction.scala b/slick/src/main/scala/za/co/absa/fadb/slick/SlickFunction.scala index 06933c95..bd1b3a35 100644 --- a/slick/src/main/scala/za/co/absa/fadb/slick/SlickFunction.scala +++ b/slick/src/main/scala/za/co/absa/fadb/slick/SlickFunction.scala @@ -20,7 +20,7 @@ import slick.jdbc.{GetResult, SQLActionBuilder} import za.co.absa.fadb.DBFunctionFabric /** - * Mix-in trait to use [[za.co.absa.fadb.DBFunction DBFunction]] with [[SlickPgEngine]]. Implements the abstract function `query` + * Mix-in trait to use with [[za.co.absa.fadb.DBFunction DBFunction]] and Slick engine like [[SlickPgEngine]]. Implements the abstract function `query` * @tparam I - The input type of the function * @tparam R - The return type of the function */