All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.augustnagro.magnum.SqlNameMapper.scala Maven / Gradle / Ivy

The newest version!
package com.augustnagro.magnum

/** Mapping from scala terms to sql terms */
trait SqlNameMapper:
  def toColumnName(scalaName: String): String
  def toTableName(scalaName: String): String

object SqlNameMapper:

  /** Converts camelCase scala names to snake_case */
  object CamelToSnakeCase extends SqlNameMapper:

    def toColumnName(scalaName: String): String = toCase(scalaName)

    def toTableName(scalaName: String): String = toCase(scalaName)

    private def toCase(scalaName: String): String =
      val res = StringBuilder().append(scalaName.head.toLower)
      for i <- 1 until scalaName.length do
        val c = scalaName.charAt(i)
        if c.isUpper then res.append('_').append(c.toLower)
        else res.append(c)
      res.result()

  object CamelToUpperSnakeCase extends SqlNameMapper:
    def toColumnName(scalaName: String): String = toCase(scalaName)

    def toTableName(scalaName: String): String = toCase(scalaName)

    private def toCase(scalaName: String): String =
      val res = StringBuilder().append(scalaName.head.toUpper)
      for i <- 1 until scalaName.length do
        val c = scalaName.charAt(i)
        if c.isUpper then res.append('_').append(c)
        else res.append(c.toUpper)
      res.result()

  /** SqlNameMapper that keeps the same case as the provided scala names */
  object SameCase extends SqlNameMapper:
    def toColumnName(scalaName: String): String = scalaName
    def toTableName(scalaName: String): String = scalaName
end SqlNameMapper




© 2015 - 2025 Weber Informatics LLC | Privacy Policy