Skip to content

Commit 9b99924

Browse files
committed
Adding enumeration support.
1 parent 46ed9ae commit 9b99924

File tree

12 files changed

+142
-25
lines changed

12 files changed

+142
-25
lines changed

.travis.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ scala:
1919
# Emails to notify
2020
notifications:
2121
slack:
22-
rooms:
23-
- websudos: P9QNXx1ZGFnDHp3v3jUqtB8k
22+
- websudos:P9QNXx1ZGFnDHp3v3jUqtB8k
23+
email:
24+
- dev@websudos.co.uk
25+
26+
27+
2428

2529
email:
2630
- dev@websudos.com
@@ -35,20 +39,17 @@ sudo: false
3539

3640
jdk:
3741
- oraclejdk7
42+
- oraclejdk8
3843
- openjdk7
3944

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

4751
script:
48-
- sbt ++$TRAVIS_SCALA_VERSION "project morpheus-dsl" clean coverage test coverageReport
49-
- sbt ++$TRAVIS_SCALA_VERSION "project morpheus-mysql" clean coverage test coverageReport
50-
- sbt ++$TRAVIS_SCALA_VERSION "project morpheus-testkit" clean coverage test coverageReport
51-
- sbt ++$TRAVIS_SCALA_VERSION coverageAggregate
52+
- sbt ++$TRAVIS_SCALA_VERSION clean coverage test coverageAggregate
5253

5354
after_success:
54-
- sbt ++$TRAVIS_SCALA_VERSION coveralls
55+
- sbt ++$TRAVIS_SCALA_VERSION coveralls

morpheus-dsl/src/main/scala/com/websudos/morpheus/column/StringColumns.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ abstract class AbstractEnumColumn[Owner <: BaseTable[Owner, Record, TableRow], R
122122

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

125-
override def sqlType: String = DefaultSQLDataTypes.varchar
125+
override def sqlType: String = s"${DefaultSQLDataTypes.varchar}(200)"
126126
}
127127

128128
abstract class AbstractOptionalEnumColumn[Owner <: BaseTable[Owner, Record, TableRow], Record, TableRow <: Row, EnumType <: Enumeration](table: BaseTable[Owner, Record, TableRow], enum: EnumType)

morpheus-mysql/src/main/scala/com/websudos/morpheus/mysql/package.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030

3131
package com.websudos.morpheus
3232

33-
import com.websudos.morpheus.column.{DefaultForeignKeyConstraints, AbstractColumn}
33+
import com.websudos.morpheus.column.{AbstractColumn, DefaultForeignKeyConstraints}
3434
import com.websudos.morpheus.dsl.DefaultImportsDefinition
3535
import com.websudos.morpheus.mysql.query.{MySQLRootSelectQuery, MySQLSelectQuery}
3636
import com.websudos.morpheus.operators.MySQLOperatorSet
3737
import com.websudos.morpheus.query.{AssignUnchainned, Unchainned, Ungroupped, Unlimited, Unordered, Unterminated}
3838

39+
import scala.util.Try
40+
3941

4042
package object mysql extends DefaultImportsDefinition
4143
with MySQLImplicits
@@ -60,4 +62,17 @@ package object mysql extends DefaultImportsDefinition
6062
type Result = com.websudos.morpheus.mysql.MySQLResult
6163

6264
type Table[Owner <: BaseTable[Owner, Record, MySQLRow], Record] = com.websudos.morpheus.mysql.MySQLTable[Owner, Record]
65+
66+
def enumToQueryConditionPrimitive[T <: Enumeration](enum: T)(implicit ev: SQLPrimitive[String]): SQLPrimitive[T#Value] = {
67+
new SQLPrimitive[T#Value] {
68+
69+
override def sqlType: String = ev.sqlType
70+
71+
override def fromRow(row: com.websudos.morpheus.Row, name: String): Try[T#Value] = {
72+
Try { enum.withName(row.string(name)) }
73+
}
74+
75+
override def toSQL(value: T#Value): String = ev.toSQL(value.toString)
76+
}
77+
}
6378
}

morpheus-mysql/src/main/scala/com/websudos/morpheus/mysql/query/MySQLRootInsertQuery.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ private[morpheus] class MySQLInsertSyntaxBlock(query: String, tableName: String)
6666
class MySQLRootInsertQuery[T <: BaseTable[T, _, MySQLRow], R](table: T, st: MySQLInsertSyntaxBlock, rowFunc: MySQLRow => R)
6767
extends RootInsertQuery[T, R, MySQLRow](table, st, rowFunc) {
6868

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

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

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

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

@@ -99,3 +99,7 @@ class MySQLInsertQuery[T <: BaseTable[T, _, MySQLRow],
9999
valuePart: ValuePart = Defaults.EmptyValuePart,
100100
lightweightPart: LightweightPart = Defaults.EmptyLightweightPart
101101
) extends InsertQuery[T, R, MySQLRow, Group, Order, Limit, Chain, AssignChain, Status](table: T, init, rowFunc) {}
102+
103+
object MySQLInsertQuery {
104+
type Default[T <: BaseTable[T, _, MySQLRow], R] = MySQLInsertQuery[T, R, Ungroupped, Unordered, Unlimited, Unchainned, AssignUnchainned, Unterminated]
105+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.websudos.morpheus.mysql
2+
3+
import com.websudos.morpheus.mysql.tables.{TestEnumeration, EnumerationRecord}
4+
import com.websudos.util.testing._
5+
6+
trait Samplers {
7+
implicit object EnumerationRecordSampler extends Sample[EnumerationRecord] {
8+
def sample: EnumerationRecord = {
9+
EnumerationRecord(
10+
id = gen[Int],
11+
enum = oneOf(TestEnumeration)
12+
)
13+
}
14+
}
15+
}

morpheus-mysql/src/test/scala/com/websudos/morpheus/mysql/db/CreateQueryDBTest.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class CreateQueryDBTest extends FlatSpec with MySQLSuite {
4646

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

49-
Console.println(BasicTable.create.ifNotExists.engine(InnoDB).queryString)
50-
5149

5250
BasicTable.create.ifNotExists.engine(InnoDB).execute.successful { _ => }
5351
}

morpheus-mysql/src/test/scala/com/websudos/morpheus/mysql/db/InsertQueryDBTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class InsertQueryDBTest extends FlatSpec with MySQLSuite {
4343

4444
override def beforeAll(): Unit = {
4545
super.beforeAll()
46-
Await.ready(BasicTable.create.ifNotExists.engine(InnoDB).future(), 3.seconds)
46+
Await.result(BasicTable.create.ifNotExists.engine(InnoDB).future(), 3.seconds)
4747
}
4848

4949
it should "store a record in the database and retrieve it by id" in {

morpheus-mysql/src/test/scala/com/websudos/morpheus/mysql/db/package.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.websudos.morpheus.mysql
1818

19-
import com.websudos.morpheus.mysql.tables.BasicRecord
19+
import com.websudos.morpheus.mysql.tables.{TestEnumeration, EnumerationRecord, BasicRecord}
2020
import com.websudos.util.testing._
2121

2222
package object db {
@@ -29,4 +29,13 @@ package object db {
2929
)
3030
}
3131
}
32+
33+
implicit object EnumerationRecordSamplers extends Sample[EnumerationRecord] {
34+
def sample: EnumerationRecord = {
35+
EnumerationRecord(
36+
id = gen[Int],
37+
enum = oneOf(TestEnumeration)
38+
)
39+
}
40+
}
3241
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.websudos.morpheus.mysql.db.specialized
2+
3+
import com.websudos.morpheus.mysql.{Samplers, _}
4+
import com.websudos.morpheus.mysql.db.MySQLSuite
5+
import com.websudos.morpheus.mysql.tables.{TestEnumeration, EnumerationRecord, EnumerationTable}
6+
import com.websudos.util.testing._
7+
import org.scalatest.FlatSpec
8+
9+
import scala.concurrent.Await
10+
import scala.concurrent.ExecutionContext.Implicits.global
11+
import scala.concurrent.duration._
12+
13+
class EnumerationColumnTest extends FlatSpec with MySQLSuite with Samplers {
14+
15+
override def beforeAll(): Unit = {
16+
super.beforeAll()
17+
Await.result(EnumerationTable.create.ifNotExists.engine(InnoDB).future(), 5.seconds)
18+
}
19+
20+
implicit val enumPrimitive: SQLPrimitive[TestEnumeration#Value] = {
21+
enumToQueryConditionPrimitive(TestEnumeration)
22+
}
23+
24+
it should "store a record with an enumeration defined inside it" in {
25+
val record = gen[EnumerationRecord]
26+
27+
val chain = for {
28+
store <- EnumerationTable.insert
29+
.value(_.id, record.id)
30+
.value(_.enum, record.enum)
31+
.future()
32+
get <- EnumerationTable.select.where(_.id eqs record.id).one
33+
} yield get
34+
35+
whenReady(chain) {
36+
res => {
37+
res.value shouldEqual record
38+
}
39+
}
40+
41+
}
42+
}

morpheus-mysql/src/test/scala/com/websudos/morpheus/mysql/tables/Tables.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
package com.websudos.morpheus.mysql.tables
3232

3333
import com.websudos.morpheus.mysql._
34+
import com.websudos.morpheus.mysql.query.MySQLInsertQuery
35+
import com.websudos.morpheus.query.InsertQuery
36+
37+
import scala.concurrent.{ExecutionContext, Future}
3438

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

@@ -165,3 +169,32 @@ class BasicTable extends Table[BasicTable, BasicRecord] {
165169
}
166170

167171
object BasicTable extends BasicTable
172+
173+
174+
trait TestEnumeration extends Enumeration {
175+
val EnumOne = Value("One")
176+
val EnumTwo = Value("Two")
177+
}
178+
179+
object TestEnumeration extends TestEnumeration
180+
181+
case class EnumerationRecord(
182+
id: Int,
183+
enum: TestEnumeration#Value
184+
)
185+
186+
class EnumerationTable extends Table[EnumerationTable, EnumerationRecord] {
187+
188+
object id extends IntColumn(this) with PrimaryKey[Int] with Autoincrement with NotNull
189+
190+
object enum extends EnumColumn[EnumerationTable, EnumerationRecord, TestEnumeration](this, TestEnumeration)
191+
192+
def fromRow(row: Row): EnumerationRecord = {
193+
EnumerationRecord(
194+
id = id(row),
195+
enum = enum(row)
196+
)
197+
}
198+
}
199+
200+
object EnumerationTable extends EnumerationTable

0 commit comments

Comments
 (0)