Skip to content

Commit

Permalink
Merge pull request #6 from websudos/release/enums
Browse files Browse the repository at this point in the history
Adding enumeration support.
  • Loading branch information
alexflav23 committed Dec 12, 2015
2 parents 46ed9ae + 9b99924 commit 886cf19
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 25 deletions.
17 changes: 9 additions & 8 deletions .travis.yml
Expand Up @@ -19,8 +19,12 @@ scala:
# Emails to notify
notifications:
slack:
rooms:
- websudos: P9QNXx1ZGFnDHp3v3jUqtB8k
- websudos:P9QNXx1ZGFnDHp3v3jUqtB8k
email:
- dev@websudos.co.uk




email:
- dev@websudos.com
Expand All @@ -35,20 +39,17 @@ sudo: false

jdk:
- oraclejdk7
- oraclejdk8
- openjdk7


before_script:
- travis_retry sbt ++$TRAVIS_SCALA_VERSION update
- mysql -e "CREATE DATABASE morpheus_test;"
- mysql -e "CREATE USER 'morpheus'@'localhost' IDENTIFIED BY 'morpheus23!';"
- mysql -e "SET PASSWORD FOR 'travis'@'localhost' = PASSWORD('morpheus23!')"

script:
- sbt ++$TRAVIS_SCALA_VERSION "project morpheus-dsl" clean coverage test coverageReport
- sbt ++$TRAVIS_SCALA_VERSION "project morpheus-mysql" clean coverage test coverageReport
- sbt ++$TRAVIS_SCALA_VERSION "project morpheus-testkit" clean coverage test coverageReport
- sbt ++$TRAVIS_SCALA_VERSION coverageAggregate
- sbt ++$TRAVIS_SCALA_VERSION clean coverage test coverageAggregate

after_success:
- sbt ++$TRAVIS_SCALA_VERSION coveralls
- sbt ++$TRAVIS_SCALA_VERSION coveralls
Expand Up @@ -122,7 +122,7 @@ abstract class AbstractEnumColumn[Owner <: BaseTable[Owner, Record, TableRow], R

override def qb: SQLBuiltQuery = SQLBuiltQuery(name).pad.append(sqlType)

override def sqlType: String = DefaultSQLDataTypes.varchar
override def sqlType: String = s"${DefaultSQLDataTypes.varchar}(200)"
}

abstract class AbstractOptionalEnumColumn[Owner <: BaseTable[Owner, Record, TableRow], Record, TableRow <: Row, EnumType <: Enumeration](table: BaseTable[Owner, Record, TableRow], enum: EnumType)
Expand Down
Expand Up @@ -30,12 +30,14 @@

package com.websudos.morpheus

import com.websudos.morpheus.column.{DefaultForeignKeyConstraints, AbstractColumn}
import com.websudos.morpheus.column.{AbstractColumn, DefaultForeignKeyConstraints}
import com.websudos.morpheus.dsl.DefaultImportsDefinition
import com.websudos.morpheus.mysql.query.{MySQLRootSelectQuery, MySQLSelectQuery}
import com.websudos.morpheus.operators.MySQLOperatorSet
import com.websudos.morpheus.query.{AssignUnchainned, Unchainned, Ungroupped, Unlimited, Unordered, Unterminated}

import scala.util.Try


package object mysql extends DefaultImportsDefinition
with MySQLImplicits
Expand All @@ -60,4 +62,17 @@ package object mysql extends DefaultImportsDefinition
type Result = com.websudos.morpheus.mysql.MySQLResult

type Table[Owner <: BaseTable[Owner, Record, MySQLRow], Record] = com.websudos.morpheus.mysql.MySQLTable[Owner, Record]

def enumToQueryConditionPrimitive[T <: Enumeration](enum: T)(implicit ev: SQLPrimitive[String]): SQLPrimitive[T#Value] = {
new SQLPrimitive[T#Value] {

override def sqlType: String = ev.sqlType

override def fromRow(row: com.websudos.morpheus.Row, name: String): Try[T#Value] = {
Try { enum.withName(row.string(name)) }
}

override def toSQL(value: T#Value): String = ev.toSQL(value.toString)
}
}
}
Expand Up @@ -66,19 +66,19 @@ private[morpheus] class MySQLInsertSyntaxBlock(query: String, tableName: String)
class MySQLRootInsertQuery[T <: BaseTable[T, _, MySQLRow], R](table: T, st: MySQLInsertSyntaxBlock, rowFunc: MySQLRow => R)
extends RootInsertQuery[T, R, MySQLRow](table, st, rowFunc) {

def delayed: MySQLInsertQuery[T, R, Ungroupped, Unordered, Unlimited, Unchainned, AssignUnchainned, Unterminated] = {
def delayed: MySQLInsertQuery.Default[T, R] = {
new MySQLInsertQuery(table, st.delayed, rowFunc)
}

def lowPriority: MySQLInsertQuery[T, R, Ungroupped, Unordered, Unlimited, Unchainned, AssignUnchainned, Unterminated] = {
def lowPriority: MySQLInsertQuery.Default[T, R] = {
new MySQLInsertQuery(table, st.lowPriority, rowFunc)
}

def highPriority: MySQLInsertQuery[T, R, Ungroupped, Unordered, Unlimited, Unchainned, AssignUnchainned, Unterminated] = {
def highPriority: MySQLInsertQuery.Default[T, R] = {
new MySQLInsertQuery(table, st.highPriority, rowFunc)
}

def ignore: MySQLInsertQuery[T, R, Ungroupped, Unordered, Unlimited, Unchainned, AssignUnchainned, Unterminated] = {
def ignore: MySQLInsertQuery.Default[T, R] = {
new MySQLInsertQuery(table, st.ignore, rowFunc)
}

Expand All @@ -99,3 +99,7 @@ class MySQLInsertQuery[T <: BaseTable[T, _, MySQLRow],
valuePart: ValuePart = Defaults.EmptyValuePart,
lightweightPart: LightweightPart = Defaults.EmptyLightweightPart
) extends InsertQuery[T, R, MySQLRow, Group, Order, Limit, Chain, AssignChain, Status](table: T, init, rowFunc) {}

object MySQLInsertQuery {
type Default[T <: BaseTable[T, _, MySQLRow], R] = MySQLInsertQuery[T, R, Ungroupped, Unordered, Unlimited, Unchainned, AssignUnchainned, Unterminated]
}
@@ -0,0 +1,15 @@
package com.websudos.morpheus.mysql

import com.websudos.morpheus.mysql.tables.{TestEnumeration, EnumerationRecord}
import com.websudos.util.testing._

trait Samplers {
implicit object EnumerationRecordSampler extends Sample[EnumerationRecord] {
def sample: EnumerationRecord = {
EnumerationRecord(
id = gen[Int],
enum = oneOf(TestEnumeration)
)
}
}
}
Expand Up @@ -46,8 +46,6 @@ class CreateQueryDBTest extends FlatSpec with MySQLSuite {

it should "create a new table in the database if the table doesn't exist" in {

Console.println(BasicTable.create.ifNotExists.engine(InnoDB).queryString)


BasicTable.create.ifNotExists.engine(InnoDB).execute.successful { _ => }
}
Expand Down
Expand Up @@ -43,7 +43,7 @@ class InsertQueryDBTest extends FlatSpec with MySQLSuite {

override def beforeAll(): Unit = {
super.beforeAll()
Await.ready(BasicTable.create.ifNotExists.engine(InnoDB).future(), 3.seconds)
Await.result(BasicTable.create.ifNotExists.engine(InnoDB).future(), 3.seconds)
}

it should "store a record in the database and retrieve it by id" in {
Expand Down
Expand Up @@ -16,7 +16,7 @@

package com.websudos.morpheus.mysql

import com.websudos.morpheus.mysql.tables.BasicRecord
import com.websudos.morpheus.mysql.tables.{TestEnumeration, EnumerationRecord, BasicRecord}
import com.websudos.util.testing._

package object db {
Expand All @@ -29,4 +29,13 @@ package object db {
)
}
}

implicit object EnumerationRecordSamplers extends Sample[EnumerationRecord] {
def sample: EnumerationRecord = {
EnumerationRecord(
id = gen[Int],
enum = oneOf(TestEnumeration)
)
}
}
}
@@ -0,0 +1,42 @@
package com.websudos.morpheus.mysql.db.specialized

import com.websudos.morpheus.mysql.{Samplers, _}
import com.websudos.morpheus.mysql.db.MySQLSuite
import com.websudos.morpheus.mysql.tables.{TestEnumeration, EnumerationRecord, EnumerationTable}
import com.websudos.util.testing._
import org.scalatest.FlatSpec

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

class EnumerationColumnTest extends FlatSpec with MySQLSuite with Samplers {

override def beforeAll(): Unit = {
super.beforeAll()
Await.result(EnumerationTable.create.ifNotExists.engine(InnoDB).future(), 5.seconds)
}

implicit val enumPrimitive: SQLPrimitive[TestEnumeration#Value] = {
enumToQueryConditionPrimitive(TestEnumeration)
}

it should "store a record with an enumeration defined inside it" in {
val record = gen[EnumerationRecord]

val chain = for {
store <- EnumerationTable.insert
.value(_.id, record.id)
.value(_.enum, record.enum)
.future()
get <- EnumerationTable.select.where(_.id eqs record.id).one
} yield get

whenReady(chain) {
res => {
res.value shouldEqual record
}
}

}
}
Expand Up @@ -31,6 +31,10 @@
package com.websudos.morpheus.mysql.tables

import com.websudos.morpheus.mysql._
import com.websudos.morpheus.mysql.query.MySQLInsertQuery
import com.websudos.morpheus.query.InsertQuery

import scala.concurrent.{ExecutionContext, Future}

case class IndexedRecord(id: Int, value: Long)

Expand Down Expand Up @@ -165,3 +169,32 @@ class BasicTable extends Table[BasicTable, BasicRecord] {
}

object BasicTable extends BasicTable


trait TestEnumeration extends Enumeration {
val EnumOne = Value("One")
val EnumTwo = Value("Two")
}

object TestEnumeration extends TestEnumeration

case class EnumerationRecord(
id: Int,
enum: TestEnumeration#Value
)

class EnumerationTable extends Table[EnumerationTable, EnumerationRecord] {

object id extends IntColumn(this) with PrimaryKey[Int] with Autoincrement with NotNull

object enum extends EnumColumn[EnumerationTable, EnumerationRecord, TestEnumeration](this, TestEnumeration)

def fromRow(row: Row): EnumerationRecord = {
EnumerationRecord(
id = id(row),
enum = enum(row)
)
}
}

object EnumerationTable extends EnumerationTable
8 changes: 4 additions & 4 deletions project/Build.scala
@@ -1,11 +1,11 @@

import com.twitter.sbt.StandardProject
import com.twitter.sbt.VersionManagement
import sbt.Keys._
import sbt._

object Build extends Build {

val UtilVersion = "0.9.11"
val UtilVersion = "0.10.0"
val FinagleVersion = "6.25.0"
val SparkVersion = "1.2.1"
val FinaglePostgres = "0.1.0-SNAPSHOT"
Expand Down Expand Up @@ -67,7 +67,7 @@ object Build extends Build {

val sharedSettings: Seq[Def.Setting[_]] = Seq(
organization := "com.websudos",
version := "0.2.3",
version := "0.2.4",
scalaVersion := "2.11.7",
crossScalaVersions := Seq("2.10.5", "2.11.7"),
resolvers ++= Seq(
Expand Down Expand Up @@ -95,7 +95,7 @@ object Build extends Build {
),
fork in Test := true,
javaOptions in Test ++= Seq("-Xmx2G")
) ++ net.virtualvoid.sbt.graph.Plugin.graphSettings ++ bintrayPublishing ++ StandardProject.newSettings
) ++ net.virtualvoid.sbt.graph.Plugin.graphSettings ++ bintrayPublishing ++ VersionManagement.newSettings

lazy val morpheus = Project(
id = "morpheus",
Expand Down
6 changes: 3 additions & 3 deletions project/plugins.sbt
Expand Up @@ -12,11 +12,11 @@ addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.7.5")

// addSbtPlugin("com.twitter" %% "scrooge-sbt-plugin" % "3.18.1")

addSbtPlugin("com.websudos" % "sbt-scoverage" % "1.3.2")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.3.3")

addSbtPlugin("org.scoverage" %% "sbt-coveralls" % "1.0.0")
addSbtPlugin("org.scoverage" %% "sbt-coveralls" % "1.0.3")

// addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.6.0")
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.7.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.6.4")

Expand Down

0 comments on commit 886cf19

Please sign in to comment.