Permalink
Browse files

Implemented O.Unique Column Option

  • Loading branch information...
1 parent e47994f commit 6b59d0fc228bc4262491d0cf7110eaac0e0d8a7f @trevorsibanda trevorsibanda committed Jun 12, 2016
@@ -164,6 +164,7 @@ class $name(_tableTag: Tag) extends Table[$elementType](_tableTag, ${args.mkStri
case SqlType(dbType) => Some(s"""O.SqlType("$dbType")""")
case Length(length,varying) => Some(s"O.Length($length,varying=$varying)")
case AutoInc => Some(s"O.AutoInc")
+ case Unique => Some(s"O.Unique")
case NotNull|Nullable => throw new SlickException( s"Please don't use Nullable or NotNull column options. Use an Option type, respectively the nullable flag in Slick's model model Column." )
case o => None // throw new SlickException( s"Don't know how to generate code for unexpected ColumnOption $o." )
}
@@ -124,7 +124,7 @@ class Tables(val profile: JdbcProfile){
)
)](tag, "TYPE_TEST") {
def `type` = column[String]("type") // <- test escaping of keywords
- def Boolean = column[Boolean]("Boolean",O.Default(true))
+ def Boolean = column[Boolean]("Boolean",O.Default(true), O.Unique)
def Byte = column[Byte]("Byte")
def Short = column[Short]("Short")
def Int = column[Int]("Int",O.Default(-5))
@@ -54,6 +54,31 @@ class InsertTest extends AsyncTest[JdbcTestDB] {
)
}
+
+ def testUniqueInsert = {
+ case class ARow(email: String , id: Int = 0)
+ class A(tag: Tag) extends Table[ARow](tag , "A_UNIQUEINSERT"){
+ def id = column[Int]("id" , O.AutoInc , O.PrimaryKey)
+ def email = column[String]("email" , O.Unique , O.Length(254))
+
+ def * = (email , id)<>(ARow.tupled , ARow.unapply )
+ }
+ val atq = TableQuery[A]
+
+ import scala.util.{Success, Failure}
+ DBIO.seq(
+ atq.schema.create,
+ atq ++= Seq( ARow("unique@site.com") , ARow("user@site.com") ),
+ ( atq += ARow("unique@site.com") ).asTry.map{
+ case Failure(e:java.sql.SQLException) if e.getMessage.toLowerCase.contains("unique") => ()
+ case Failure(e:java.sql.BatchUpdateException) if e.getMessage.toLowerCase.contains("unique") => ()
+ case Failure( e ) => throw e
+ case Success(_) => throw new Exception("Should have failed with UNIQUE constraint violation")
+ },
+ atq.result.map( _.size shouldBe 2 )
+ )
+ }
+
def testReturning = ifCap(jcap.returnInsertKey) {
class A(tag: Tag) extends Table[(Int, String, String)](tag, "A") {
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
@@ -13,4 +13,6 @@ object ColumnOption {
/** Mark a column as the table's primary key. For named and composite primary keys you have to
* use the Table's `primaryKey` method instead. */
case object PrimaryKey extends ColumnOption[Nothing]
+
+ case object Unique extends ColumnOption[Nothing]
}
@@ -195,6 +195,7 @@ trait DerbyProfile extends JdbcProfile {
if(notNull) sb append " NOT NULL"
if(primaryKey) sb append " PRIMARY KEY"
if(autoIncrement) sb append " GENERATED BY DEFAULT AS IDENTITY"
+ if( unique ) sb append " UNIQUE"
}
}
@@ -684,6 +684,7 @@ trait JdbcStatementBuilderComponent { self: JdbcProfile =>
protected var notNull = !isOption
protected var autoIncrement = false
protected var primaryKey = false
+ protected var unique = false
protected var defaultLiteral: String = null
init()
@@ -704,6 +705,7 @@ trait JdbcStatementBuilderComponent { self: JdbcProfile =>
case SqlProfile.ColumnOption.Nullable => notNull = false
case ColumnOption.AutoInc => autoIncrement = true
case ColumnOption.PrimaryKey => primaryKey = true
+ case ColumnOption.Unique => unique = true
case RelationalProfile.ColumnOption.Default(v) => defaultLiteral = valueToSQLLiteral(v, column.tpe)
}
@@ -720,6 +722,7 @@ trait JdbcStatementBuilderComponent { self: JdbcProfile =>
if(autoIncrement) sb append " GENERATED BY DEFAULT AS IDENTITY(START WITH 1)"
if(notNull) sb append " NOT NULL"
if(primaryKey) sb append " PRIMARY KEY"
+ if( unique ) sb append " UNIQUE"
}
}
@@ -208,6 +208,7 @@ trait MySQLProfile extends JdbcProfile { profile =>
else if(sqlType.toUpperCase == "TIMESTAMP") sb append " NULL"
if(autoIncrement) sb append " AUTO_INCREMENT"
if(primaryKey) sb append " PRIMARY KEY"
+ if( unique ) sb append " UNIQUE"
}
}
@@ -215,6 +215,7 @@ trait OracleProfile extends JdbcProfile {
if(defaultLiteral ne null) sb append " DEFAULT " append defaultLiteral
if(notNull) sb append " NOT NULL"
if(primaryKey) sb append " PRIMARY KEY"
+ if( unique ) sb append " UNIQUE"
}
override protected def handleColumnOption(o: ColumnOption[_]): Unit = o match {
@@ -214,6 +214,7 @@ trait SQLServerProfile extends JdbcProfile {
if(notNull) sb append " NOT NULL"
if(primaryKey) sb append " PRIMARY KEY"
if(autoIncrement) sb append " IDENTITY"
+ if( unique ) sb append " UNIQUE"
}
}
@@ -212,6 +212,7 @@ trait SQLiteProfile extends JdbcProfile {
if(autoIncrement) sb append " PRIMARY KEY AUTOINCREMENT"
else if(primaryKey) sb append " PRIMARY KEY"
if(notNull) sb append " NOT NULL"
+ if( unique ) sb append " UNIQUE"
}
}
@@ -110,6 +110,7 @@ trait RelationalTableComponent { self: RelationalProfile =>
val PrimaryKey = ColumnOption.PrimaryKey
def Default[T](defaultValue: T) = RelationalProfile.ColumnOption.Default[T](defaultValue)
val AutoInc = ColumnOption.AutoInc
+ val Unique = ColumnOption.Unique
val Length = RelationalProfile.ColumnOption.Length
}

0 comments on commit 6b59d0f

Please sign in to comment.