Skip to content

Commit

Permalink
#99: DBEngine requirement removed from DBSchema. Fixes #99.
Browse files Browse the repository at this point in the history
  • Loading branch information
salamonpavel committed Nov 30, 2023
1 parent 91f4e39 commit 4f3084c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 415 deletions.
143 changes: 24 additions & 119 deletions core/src/main/scala/za/co/absa/fadb/DBFunction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,12 @@ import scala.concurrent.Future
* @tparam R - the type covering the returned fields from the database function
* @tparam E - the type of the [[DBEngine]] engine
*/
abstract class DBFunction[I, R, E <: DBEngine](functionNameOverride: Option[String] = None)
(implicit val schema: DBSchema, val dBEngine: E) extends DBFunctionFabric {
abstract class DBFunction[I, R, E <: DBEngine](schemaName: String, functionNameOverride: Option[String] = None)
(implicit val dBEngine: E, namingConvention: NamingConvention) extends DBFunctionFabric {

/* alternative constructors for different availability of input parameters */
def this(functionNameOverride: String)
(implicit schema: DBSchema, dBEngine: E) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(schema: DBSchema, functionNameOverride: String)
(implicit dBEngine: E) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

/* only one constructor of a class can have default values for parameters*/
def this(schema: DBSchema)
(implicit dBEngine: E) = {
this(None)(schema, dBEngine)
}

def this(dBEngine: E, functionNameOverride: String)
(implicit schema: DBSchema) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(dBEngine: E)
(implicit schema: DBSchema) = {
this(None)(schema, dBEngine)
// constructor that takes function name override as string instead of option
def this(schemaName: String, functionNameOverride: String)(implicit dBEngine: E, namingConvention: NamingConvention) {
this(schemaName, Option(functionNameOverride))
}

/**
Expand All @@ -72,16 +50,18 @@ abstract class DBFunction[I, R, E <: DBEngine](functionNameOverride: Option[Stri
* Name of the function, based on the class name, unless it is overridden in the constructor
*/
val functionName: String = {
val fn = functionNameOverride.getOrElse(schema.objectNameFromClassName(getClass))
if (schema.schemaName.isEmpty) {
fn
} else {
s"${schema.schemaName}.$fn"
}
val baseFunctionName = functionNameOverride.getOrElse {
val className = this.getClass.getSimpleName
val cleanClassName = className.lastIndexOf('$') match {
case -1 => className
case x => className.substring(0, x)
}
namingConvention.stringPerConvention(cleanClassName)
}
if (schemaName.isEmpty) baseFunctionName
else s"$schemaName.$baseFunctionName"
}

def namingConvention: NamingConvention = schema.namingConvention

/**
* List of fields to select from the DB function. Expected to be based on the return type `R`
* @return - list of fields to select
Expand All @@ -107,34 +87,9 @@ object DBFunction {
* @tparam R - the type covering the returned fields from the database function
* @tparam E - the type of the [[DBEngine]] engine
*/
abstract class DBMultipleResultFunction[I, R, E <: DBEngine](functionNameOverride: Option[String] = None)
(implicit schema: DBSchema, dBEngine: E)
extends DBFunction[I, R, E](functionNameOverride) {

def this(functionNameOverride: String)
(implicit schema: DBSchema, dBEngine: E) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(schema: DBSchema, functionNameOverride: String)
(implicit dBEngine: E) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(schema: DBSchema)
(implicit dBEngine: E) = {
this(None)(schema, dBEngine)
}

def this(dBEngine: E, functionNameOverride: String)
(implicit schema: DBSchema) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(dBEngine: E)
(implicit schema: DBSchema) = {
this(None)(schema, dBEngine)
}
abstract class DBMultipleResultFunction[I, R, E <: DBEngine](schemaName: String, functionNameOverride: Option[String] = None)
(implicit dBEngine: E, namingConvention: NamingConvention)
extends DBFunction[I, R, E](schemaName, functionNameOverride) {

/**
* For easy and convenient execution of the DB function call
Expand All @@ -156,34 +111,9 @@ object DBFunction {
* @tparam R - the type covering the returned fields from the database function
* @tparam E - the type of the [[DBEngine]] engine
*/
abstract class DBSingleResultFunction[I, R, E <: DBEngine](functionNameOverride: Option[String] = None)
(implicit schema: DBSchema, dBEngine: E)
extends DBFunction[I, R, E](functionNameOverride) {

def this(functionNameOverride: String)
(implicit schema: DBSchema, dBEngine: E) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(schema: DBSchema, functionNameOverride: String)
(implicit dBEngine: E) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(schema: DBSchema)
(implicit dBEngine: E) = {
this(None)(schema, dBEngine)
}

def this(dBEngine: E, functionNameOverride: String)
(implicit schema: DBSchema) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(dBEngine: E)
(implicit schema: DBSchema) = {
this(None)(schema, dBEngine)
}
abstract class DBSingleResultFunction[I, R, E <: DBEngine](schemaName: String, functionNameOverride: Option[String] = None)
(implicit dBEngine: E, namingConvention: NamingConvention)
extends DBFunction[I, R, E](schemaName, functionNameOverride) {

/**
* For easy and convenient execution of the DB function call
Expand All @@ -204,34 +134,9 @@ object DBFunction {
* @tparam R - the type covering the returned fields from the database function
* @tparam E - the type of the [[DBEngine]] engine
*/
abstract class DBOptionalResultFunction[I, R, E <: DBEngine](functionNameOverride: Option[String] = None)
(implicit schema: DBSchema, dBEngine: E)
extends DBFunction[I, R, E](functionNameOverride) {

def this(functionNameOverride: String)
(implicit schema: DBSchema, dBEngine: E) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(schema: DBSchema, functionNameOverride: String)
(implicit dBEngine: E) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(schema: DBSchema)
(implicit dBEngine: E) = {
this(None)(schema, dBEngine)
}

def this(dBEngine: E, functionNameOverride: String)
(implicit schema: DBSchema) = {
this(Option(functionNameOverride))(schema, dBEngine)
}

def this(dBEngine: E)
(implicit schema: DBSchema) = {
this(None)(schema, dBEngine)
}
abstract class DBOptionalResultFunction[I, R, E <: DBEngine](schemaName: String, functionNameOverride: Option[String] = None)
(implicit dBEngine: E, namingConvention: NamingConvention)
extends DBFunction[I, R, E](schemaName, functionNameOverride) {

/**
* For easy and convenient execution of the DB function call
Expand Down
15 changes: 6 additions & 9 deletions core/src/test/scala/za/co/absa/fadb/DBFunctionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,25 @@ class DBFunctionSuite extends AnyFunSuite {
override implicit val executor: ExecutionContext = ExecutionContext.Implicits.global
}

private object FooNamed extends DBSchema
private object FooNameless extends DBSchema("")

test("Function name check"){
case class MyFunction(override val schema: DBSchema) extends DBFunction[Unit, Unit, DBEngine](schema) {
case class MyFunction(schemaName: String) extends DBFunction[Unit, Unit, DBEngine](schemaName) {
override protected def query(values: Unit): dBEngine.QueryType[Unit] = neverHappens
}

val fnc1 = MyFunction(FooNamed)
val fnc2 = MyFunction(FooNameless)
val fnc1 = MyFunction("foo_named")
val fnc2 = MyFunction("")

assert(fnc1.functionName == "foo_named.my_function")
assert(fnc2.functionName == "my_function")
}

test("Function name override check"){
case class MyFunction(override val schema: DBSchema) extends DBFunction[Unit, Unit, DBEngine](schema, "bar") {
case class MyFunction(schemaName: String) extends DBFunction[Unit, Unit, DBEngine](schemaName, "bar") {
override protected def query(values: Unit): dBEngine.QueryType[Unit] = neverHappens
}

val fnc1 = MyFunction(FooNamed)
val fnc2 = MyFunction(FooNameless)
val fnc1 = MyFunction("foo_named")
val fnc2 = MyFunction("")

assert(fnc1.functionName == "foo_named.bar")
assert(fnc2.functionName == "bar")
Expand Down
37 changes: 0 additions & 37 deletions core/src/test/scala/za/co/absa/fadb/DBSchemaSuite.scala

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4f3084c

Please sign in to comment.