Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#99: DBEngine requirement removed from DBSchema. Fixes #99. #100

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 20 additions & 35 deletions core/src/main/scala/za/co/absa/fadb/DBSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,45 @@ package za.co.absa.fadb
import za.co.absa.fadb.naming.NamingConvention

/**
* An abstract class, an ancestor to represent a database schema (each database function should be placed in a schema)
* An abstract class, an ancestor to represent a database schema
* The database name of the schema is derived from the class name based on the provided naming convention
* @param schemaNameOverride - in case the class name would not match the database schema name, this gives the
* possibility of override
* @param dBEngine - [[DBEngine]] to execute the functions with. Not directly needed for the DBSchema class, rather
* to be passed on to [[DBFunction]] members of the schema
* @param namingConvention - the [[za.co.absa.fadb.naming.NamingConvention NamingConvention]] prescribing how to convert a class name into a db object name
* @param namingConvention - the [[za.co.absa.fadb.naming.NamingConvention NamingConvention]]
* prescribing how to convert a class name into a db object name
*/
abstract class DBSchema(schemaNameOverride: Option[String] = None)
(implicit dBEngine: DBEngine, implicit val namingConvention: NamingConvention) {
abstract class DBSchema(schemaNameOverride: Option[String] = None)(implicit val namingConvention: NamingConvention)
{

def this(schemaNameOverride: String)
(implicit dBEngine: DBEngine, namingConvention: NamingConvention) {
this(Option(schemaNameOverride))(dBEngine, namingConvention)
def this(schemaNameOverride: String)(implicit namingConvention: NamingConvention) {
this(Option(schemaNameOverride))(namingConvention)
}

def this(dBEngine: DBEngine, schemaNameOverride: String)
(implicit namingConvention: NamingConvention) {
this(Option(schemaNameOverride))(dBEngine, namingConvention)
def this()(implicit namingConvention: NamingConvention) {
this(None)(namingConvention)
}

def this(dBEngine: DBEngine)
(implicit namingConvention: NamingConvention) {
this(None)(dBEngine, namingConvention)
}

def this(namingConvention: NamingConvention, schemaNameOverride:String)
(implicit dBEngine: DBEngine) {
this(Option(schemaNameOverride))(dBEngine, namingConvention)
}

def this(namingConvention: NamingConvention)
(implicit dBEngine: DBEngine) {
this(None)(dBEngine, namingConvention)
def this(namingConvention: NamingConvention, schemaNameOverride: String) {
this(Option(schemaNameOverride))(namingConvention)
}

/**
* To easy pass over to [[DBFunction]] members of the schema
*/
* To easy pass over to [[DBFunction]] members of the schema
*/
protected implicit val schema: DBSchema = this

/**
* Function to convert a class to the associated DB object name, based on the class' name. For transformation from the
* class name to usual db name the schema's [[za.co.absa.fadb.naming.NamingConvention NamingConvention]] is used.
* @param c - class which name to use to get the DB object name
* @return - the db object name
*/
* Function to convert a class to the associated DB object name, based on the class' name. For transformation from the
* class name to usual db name the schema's [[za.co.absa.fadb.naming.NamingConvention NamingConvention]] is used.
* @param c - class which name to use to get the DB object name
* @return - the db object name
*/
def objectNameFromClassName(c: Class[_]): String = {
namingConvention.fromClassNamePerConvention(c)
}

/**
* Name of the schema. Based on the schema's class name or provided override
*/
* Name of the schema. Based on the schema's class name or provided override
*/
val schemaName: String = schemaNameOverride.getOrElse(objectNameFromClassName(getClass))

}
4 changes: 2 additions & 2 deletions core/src/test/scala/za/co/absa/fadb/DBFunctionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class DBFunctionSuite extends AnyFunSuite {
override implicit val executor: ExecutionContext = ExecutionContext.Implicits.global
}

private object FooNamed extends DBSchema(EngineThrow)
private object FooNameless extends DBSchema(EngineThrow, "")
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) {
Expand Down
35 changes: 23 additions & 12 deletions core/src/test/scala/za/co/absa/fadb/DBSchemaSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,43 @@
package za.co.absa.fadb

import org.scalatest.funsuite.AnyFunSuite
import za.co.absa.fadb.naming.NamingConvention
import za.co.absa.fadb.naming.implementations.SnakeCaseNaming.Implicits.namingConvention

import scala.concurrent.{ExecutionContext, Future}

class DBSchemaSuite extends AnyFunSuite {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to add new tests to improve coverage:

test("schema name with naming convention without override") {
    object LowerCaseNamingConvention extends NamingConvention {
      def stringPerConvention(original: String): String = original.toLowerCase
    }
    class Bar extends DBSchema(LowerCaseNamingConvention, null)

    val schema = new Bar
    assert(schema.schemaName == "bar") // Assuming the naming convention converts "Bar" to "bar"
  }

test("schema name with naming convention with override") {
    object LowerCaseNamingConvention extends NamingConvention {
      def stringPerConvention(original: String): String = original.toLowerCase
    }
    class Bar extends DBSchema(LowerCaseNamingConvention, "bar")

    val schema = new Bar
    assert(schema.schemaName == "bar")
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @miroslavpojer. Will add it to the codebase. I think also that once we merge this code we should revisit existing projects utilizing this library, fix it's usage there and then update examples so users of the library have updated examples as part of this repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.


private object EngineThrow extends DBEngine {
override def run[R](query: QueryType[R]): Future[Seq[R]] = {
throw new Exception("Should never get here")
}

override implicit val executor: ExecutionContext = ExecutionContext.Implicits.global
}

test("schema name default") {
class Foo extends DBSchema(EngineThrow)
class Foo extends DBSchema

val schema = new Foo
assert(schema.schemaName == "foo")
}

test("schema name overridden") {
class Foo extends DBSchema(EngineThrow, "bar")
class Foo extends DBSchema("bar")

val schema = new Foo
assert(schema.schemaName == "bar")
}

test("schema name with naming convention without override") {
object LowerCaseNamingConvention extends NamingConvention {
def stringPerConvention(original: String): String = original.toLowerCase
}
class Bar extends DBSchema(LowerCaseNamingConvention, null)

val schema = new Bar
assert(schema.schemaName == "bar") // Assuming the naming convention converts "Bar" to "bar"
}

test("schema name with naming convention with override") {
object LowerCaseNamingConvention extends NamingConvention {
def stringPerConvention(original: String): String = original.toLowerCase
}
class Bar extends DBSchema(LowerCaseNamingConvention, "bar")
Copy link
Collaborator

@lsulak lsulak Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I would chose something other than bar, just to distinguish it? :) I mean, so that the schema name is actually overridden


val schema = new Bar
assert(schema.schemaName == "bar")
}

}
Loading