Skip to content

Instantly share code, notes, and snippets.

@lauris
Created January 12, 2018 19:40
Show Gist options
  • Save lauris/7b3864624a7650daaf6632313d6b806d to your computer and use it in GitHub Desktop.
Save lauris/7b3864624a7650daaf6632313d6b806d to your computer and use it in GitHub Desktop.
package schema
import scalikejdbc.{DBSession, WrappedResultSet, SQL}
import scala.collection.immutable.ListMap
case class FK(name: String, columns: String, referenced_table: String, referenced_columns: String, on_delete: String, on_update: String)
abstract class BaseTable(private val _db: String, private val _table: String)(implicit session: DBSession) {
def createColumn(meta: Map[String, Option[String]]): Int
def createIndex(name: String, indextype: String, columns: String): Int
def deleteIndex(name: String): Int
def deleteFK(name: String): Int
def createFK(fk: FK): Int
def createTable: Int
def deleteTable: Int
def deleteColumn(column: String): Int
def renameTable(new_table_name: String): Int
def createDatabase(charset: Option[String]): Int
def dropDatabase: Int
def changeColumnParameter(meta: ListMap[String, Option[String]], param: String, value: String): Int
def columnDefinition(meta: Map[String, Option[String]]): String
def columnMeta(field: String): Option[ListMap[String, Option[String]]]
def columnsMeta(): List[ListMap[String, Option[String]]]
def indexes(): List[ListMap[String, Option[String]]]
def foreignKeys(): List[ListMap[String, Option[String]]]
def triggers(): List[ListMap[String, Option[String]]]
def characterSets(): List[String]
def listTables(): List[String]
val colDefOrder: List[String]
// shortcut for a quoted table name
def table = quote(_table)
// shortcut for a quoted db name
def db = quote(_db)
// quote an identifier
def quote(value: String): String = {
List(quoteChar, value, quoteChar).mkString
}
// used only to escape column comments
def sillyEscape(value: String) = value.replace("'", "''")
// used to quote table and column names
val quoteChar = session.connection.getMetaData.getIdentifierQuoteString
/**
* Create native ListMap from WrappedResultSet
* @param rs WrappedResultSet
* @return ListMap[String, Option[String]]
*/
def toMap(rs: WrappedResultSet): ListMap[String, Option[String]] = {
val columnCount = rs.metaData.getColumnCount
(1 to columnCount).foldLeft(ListMap[String, Option[String]]()) { (result, i) =>
val label = rs.metaData.getColumnLabel(i)
result + (label -> rs.stringOpt(i))
}
}
}
object Table {
def instance(driver: String, db: String, fqTable: String)(implicit session: DBSession) = {
val table = fqTable.split('.').last
val schema = if (fqTable.split('.').length > 1) fqTable.split('.').headOption.getOrElse("public") else "public"
driver match {
case "mysql" => new MySQLTable(db, table)(session)
case "postgresql" => new PostgresTable(db, table, schema)(session)
case "redshift" => new RedshiftTable(db, table)(session)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment