From 4c709046fe329113113b15de62a181cfc8fa5c13 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 1 Feb 2015 00:48:17 +0100 Subject: Add model code generator --- app/controllers/Application.scala | 1 + app/models/Tables.scala | 223 +++++++++++++++++--------------------- app/models/Views.scala | 84 ++++++++++++++ build.sbt | 45 +++++--- codegen/ModelGenerator.scala | 60 ++++++++++ conf/application.conf | 14 ++- generateModel.sbt | 21 ++++ 7 files changed, 307 insertions(+), 141 deletions(-) create mode 100644 app/models/Views.scala create mode 100644 codegen/ModelGenerator.scala create mode 100644 generateModel.sbt diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 43b579e..3380e38 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -3,6 +3,7 @@ package controllers import play.api._ import play.api.mvc._ + object Application extends Controller { def index = Action { diff --git a/app/models/Tables.scala b/app/models/Tables.scala index 7d4406c..ff1f267 100644 --- a/app/models/Tables.scala +++ b/app/models/Tables.scala @@ -1,89 +1,78 @@ package models - // AUTO-GENERATED Slick data model /** Stand-alone Slick data model for immediate use */ object Tables extends { - val profile = scala.slick.driver.PostgresDriver + val profile = play.api.db.slick.Config.driver } with Tables /** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */ trait Tables { val profile: scala.slick.driver.JdbcProfile - import profile.simple._ import scala.slick.model.ForeignKeyAction - // NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns. - import scala.slick.jdbc.{GetResult => GR} - + /** DDL for all tables. Call .create to execute. */ lazy val ddl = Bids.ddl ++ Charges.ddl ++ Items.ddl ++ Transactions.ddl ++ Users.ddl - + /** Entity class storing rows of table Bids - * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) - * @param itemUuid Database column item_uuid DBType(varchar), Length(36,true) - * @param userUuid Database column user_uuid DBType(varchar), Length(36,true) - * @param bidDate Database column bid_date DBType(timestamptz) - * @param offer Database column offer DBType(numeric) */ + * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) + * @param itemUuid Database column item_uuid DBType(varchar), Length(36,true) + * @param userUuid Database column user_uuid DBType(varchar), Length(36,true) + * @param bidDate Database column bid_date DBType(timestamptz) + * @param offer Database column offer DBType(numeric) */ case class Bid(uuid: String, itemUuid: String, userUuid: String, bidDate: java.sql.Timestamp, offer: scala.math.BigDecimal) - /** GetResult implicit for fetching Bid objects using plain SQL queries */ - implicit def GetResultBid(implicit e0: GR[String], e1: GR[java.sql.Timestamp], e2: GR[scala.math.BigDecimal]): GR[Bid] = GR { + implicit def GetResultBid(implicit e0: GR[String], e1: GR[java.sql.Timestamp], e2: GR[scala.math.BigDecimal]): GR[Bid] = GR{ prs => import prs._ - Bid.tupled((<<[String], <<[String], <<[String], <<[java.sql.Timestamp], <<[scala.math.BigDecimal])) + Bid.tupled((<<[String], <<[String], <<[String], <<[java.sql.Timestamp], <<[scala.math.BigDecimal])) } - /** Table description of table bids. Objects of this class serve as prototypes for rows in queries. */ class Bids(_tableTag: Tag) extends Table[Bid](_tableTag, "bids") { - def * = (uuid, itemUuid, userUuid, bidDate, offer) <>(Bid.tupled, Bid.unapply) - + def * = (uuid, itemUuid, userUuid, bidDate, offer) <> (Bid.tupled, Bid.unapply) /** Maps whole row to an option. Useful for outer joins. */ - def ? = (uuid.?, itemUuid.?, userUuid.?, bidDate.?, offer.?).shaped.<>({ r => import r._; _1.map(_ => Bid.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_: Any) => throw new Exception("Inserting into ? projection not supported.")) - + def ? = (uuid.?, itemUuid.?, userUuid.?, bidDate.?, offer.?).shaped.<>({r=>import r._; _1.map(_=> Bid.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported.")) + /** Database column uuid DBType(varchar), PrimaryKey, Length(36,true) */ - val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36, varying = true)) + val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36,varying=true)) /** Database column item_uuid DBType(varchar), Length(36,true) */ - val itemUuid: Column[String] = column[String]("item_uuid", O.Length(36, varying = true)) + val itemUuid: Column[String] = column[String]("item_uuid", O.Length(36,varying=true)) /** Database column user_uuid DBType(varchar), Length(36,true) */ - val userUuid: Column[String] = column[String]("user_uuid", O.Length(36, varying = true)) + val userUuid: Column[String] = column[String]("user_uuid", O.Length(36,varying=true)) /** Database column bid_date DBType(timestamptz) */ val bidDate: Column[java.sql.Timestamp] = column[java.sql.Timestamp]("bid_date") /** Database column offer DBType(numeric) */ val offer: Column[scala.math.BigDecimal] = column[scala.math.BigDecimal]("offer") - + /** Foreign key referencing Items (database name bids_items_fk) */ - lazy val itemsFk = foreignKey("bids_items_fk", itemUuid, Items)(r => r.uuid, onUpdate = ForeignKeyAction.NoAction, onDelete = ForeignKeyAction.NoAction) + lazy val itemsFk = foreignKey("bids_items_fk", itemUuid, Items)(r => r.uuid, onUpdate=ForeignKeyAction.NoAction, onDelete=ForeignKeyAction.NoAction) /** Foreign key referencing Users (database name bids_users_fk) */ - lazy val usersFk = foreignKey("bids_users_fk", userUuid, Users)(r => r.uuid, onUpdate = ForeignKeyAction.NoAction, onDelete = ForeignKeyAction.NoAction) + lazy val usersFk = foreignKey("bids_users_fk", userUuid, Users)(r => r.uuid, onUpdate=ForeignKeyAction.NoAction, onDelete=ForeignKeyAction.NoAction) } - /** Collection-like TableQuery object for table Bids */ lazy val Bids = new TableQuery(tag => new Bids(tag)) - + /** Entity class storing rows of table Charges - * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) - * @param startDate Database column start_date DBType(timestamptz) - * @param endDate Database column end_date DBType(timestamptz) - * @param fee Database column fee DBType(numeric) - * @param rate Database column rate DBType(numeric) */ + * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) + * @param startDate Database column start_date DBType(timestamptz) + * @param endDate Database column end_date DBType(timestamptz) + * @param fee Database column fee DBType(numeric) + * @param rate Database column rate DBType(numeric) */ case class Charge(uuid: String, startDate: java.sql.Timestamp, endDate: java.sql.Timestamp, fee: scala.math.BigDecimal, rate: scala.math.BigDecimal) - /** GetResult implicit for fetching Charge objects using plain SQL queries */ - implicit def GetResultCharge(implicit e0: GR[String], e1: GR[java.sql.Timestamp], e2: GR[scala.math.BigDecimal]): GR[Charge] = GR { + implicit def GetResultCharge(implicit e0: GR[String], e1: GR[java.sql.Timestamp], e2: GR[scala.math.BigDecimal]): GR[Charge] = GR{ prs => import prs._ - Charge.tupled((<<[String], <<[java.sql.Timestamp], <<[java.sql.Timestamp], <<[scala.math.BigDecimal], <<[scala.math.BigDecimal])) + Charge.tupled((<<[String], <<[java.sql.Timestamp], <<[java.sql.Timestamp], <<[scala.math.BigDecimal], <<[scala.math.BigDecimal])) } - /** Table description of table charges. Objects of this class serve as prototypes for rows in queries. */ class Charges(_tableTag: Tag) extends Table[Charge](_tableTag, "charges") { - def * = (uuid, startDate, endDate, fee, rate) <>(Charge.tupled, Charge.unapply) - + def * = (uuid, startDate, endDate, fee, rate) <> (Charge.tupled, Charge.unapply) /** Maps whole row to an option. Useful for outer joins. */ - def ? = (uuid.?, startDate.?, endDate.?, fee.?, rate.?).shaped.<>({ r => import r._; _1.map(_ => Charge.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_: Any) => throw new Exception("Inserting into ? projection not supported.")) - + def ? = (uuid.?, startDate.?, endDate.?, fee.?, rate.?).shaped.<>({r=>import r._; _1.map(_=> Charge.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported.")) + /** Database column uuid DBType(varchar), PrimaryKey, Length(36,true) */ - val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36, varying = true)) + val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36,varying=true)) /** Database column start_date DBType(timestamptz) */ val startDate: Column[java.sql.Timestamp] = column[java.sql.Timestamp]("start_date") /** Database column end_date DBType(timestamptz) */ @@ -93,151 +82,141 @@ trait Tables { /** Database column rate DBType(numeric) */ val rate: Column[scala.math.BigDecimal] = column[scala.math.BigDecimal]("rate") } - /** Collection-like TableQuery object for table Charges */ lazy val Charges = new TableQuery(tag => new Charges(tag)) - + /** Entity class storing rows of table Items - * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) - * @param userUuid Database column user_uuid DBType(varchar), Length(36,true) - * @param startDate Database column start_date DBType(timestamptz) - * @param endDate Database column end_date DBType(timestamptz) - * @param itemName Database column item_name DBType(varchar), Length(20,true) - * @param shortDesc Database column short_desc DBType(varchar), Length(30,true) - * @param longDesc Database column long_desc DBType(text), Length(2147483647,true) - * @param initialPrice Database column initial_price DBType(numeric) */ + * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) + * @param userUuid Database column user_uuid DBType(varchar), Length(36,true) + * @param startDate Database column start_date DBType(timestamptz) + * @param endDate Database column end_date DBType(timestamptz) + * @param itemName Database column item_name DBType(varchar), Length(20,true) + * @param shortDesc Database column short_desc DBType(varchar), Length(30,true) + * @param longDesc Database column long_desc DBType(text), Length(2147483647,true) + * @param initialPrice Database column initial_price DBType(numeric) */ case class Item(uuid: String, userUuid: String, startDate: java.sql.Timestamp, endDate: java.sql.Timestamp, itemName: String, shortDesc: String, longDesc: String, initialPrice: scala.math.BigDecimal) - /** GetResult implicit for fetching Item objects using plain SQL queries */ - implicit def GetResultItem(implicit e0: GR[String], e1: GR[java.sql.Timestamp], e2: GR[scala.math.BigDecimal]): GR[Item] = GR { + implicit def GetResultItem(implicit e0: GR[String], e1: GR[java.sql.Timestamp], e2: GR[scala.math.BigDecimal]): GR[Item] = GR{ prs => import prs._ - Item.tupled((<<[String], <<[String], <<[java.sql.Timestamp], <<[java.sql.Timestamp], <<[String], <<[String], <<[String], <<[scala.math.BigDecimal])) + Item.tupled((<<[String], <<[String], <<[java.sql.Timestamp], <<[java.sql.Timestamp], <<[String], <<[String], <<[String], <<[scala.math.BigDecimal])) } - /** Table description of table items. Objects of this class serve as prototypes for rows in queries. */ class Items(_tableTag: Tag) extends Table[Item](_tableTag, "items") { - def * = (uuid, userUuid, startDate, endDate, itemName, shortDesc, longDesc, initialPrice) <>(Item.tupled, Item.unapply) - + def * = (uuid, userUuid, startDate, endDate, itemName, shortDesc, longDesc, initialPrice) <> (Item.tupled, Item.unapply) /** Maps whole row to an option. Useful for outer joins. */ - def ? = (uuid.?, userUuid.?, startDate.?, endDate.?, itemName.?, shortDesc.?, longDesc.?, initialPrice.?).shaped.<>({ r => import r._; _1.map(_ => Item.tupled((_1.get, _2.get, _3.get, _4.get, _5.get, _6.get, _7.get, _8.get)))}, (_: Any) => throw new Exception("Inserting into ? projection not supported.")) - + def ? = (uuid.?, userUuid.?, startDate.?, endDate.?, itemName.?, shortDesc.?, longDesc.?, initialPrice.?).shaped.<>({r=>import r._; _1.map(_=> Item.tupled((_1.get, _2.get, _3.get, _4.get, _5.get, _6.get, _7.get, _8.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported.")) + /** Database column uuid DBType(varchar), PrimaryKey, Length(36,true) */ - val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36, varying = true)) + val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36,varying=true)) /** Database column user_uuid DBType(varchar), Length(36,true) */ - val userUuid: Column[String] = column[String]("user_uuid", O.Length(36, varying = true)) + val userUuid: Column[String] = column[String]("user_uuid", O.Length(36,varying=true)) /** Database column start_date DBType(timestamptz) */ val startDate: Column[java.sql.Timestamp] = column[java.sql.Timestamp]("start_date") /** Database column end_date DBType(timestamptz) */ val endDate: Column[java.sql.Timestamp] = column[java.sql.Timestamp]("end_date") /** Database column item_name DBType(varchar), Length(20,true) */ - val itemName: Column[String] = column[String]("item_name", O.Length(20, varying = true)) + val itemName: Column[String] = column[String]("item_name", O.Length(20,varying=true)) /** Database column short_desc DBType(varchar), Length(30,true) */ - val shortDesc: Column[String] = column[String]("short_desc", O.Length(30, varying = true)) + val shortDesc: Column[String] = column[String]("short_desc", O.Length(30,varying=true)) /** Database column long_desc DBType(text), Length(2147483647,true) */ - val longDesc: Column[String] = column[String]("long_desc", O.Length(2147483647, varying = true)) + val longDesc: Column[String] = column[String]("long_desc", O.Length(2147483647,varying=true)) /** Database column initial_price DBType(numeric) */ val initialPrice: Column[scala.math.BigDecimal] = column[scala.math.BigDecimal]("initial_price") - + /** Foreign key referencing Users (database name items_users_fk) */ - lazy val usersFk = foreignKey("items_users_fk", userUuid, Users)(r => r.uuid, onUpdate = ForeignKeyAction.NoAction, onDelete = ForeignKeyAction.NoAction) + lazy val usersFk = foreignKey("items_users_fk", userUuid, Users)(r => r.uuid, onUpdate=ForeignKeyAction.NoAction, onDelete=ForeignKeyAction.NoAction) } - /** Collection-like TableQuery object for table Items */ lazy val Items = new TableQuery(tag => new Items(tag)) - + /** Entity class storing rows of table Transactions - * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) - * @param userUuid Database column user_uuid DBType(varchar), Length(36,true) - * @param transactionDate Database column transaction_date DBType(timestamptz) - * @param amount Database column amount DBType(numeric) - * @param label Database column label DBType(varchar), Length(255,true) */ + * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) + * @param userUuid Database column user_uuid DBType(varchar), Length(36,true) + * @param transactionDate Database column transaction_date DBType(timestamptz) + * @param amount Database column amount DBType(numeric) + * @param label Database column label DBType(varchar), Length(255,true) */ case class Transaction(uuid: String, userUuid: String, transactionDate: java.sql.Timestamp, amount: scala.math.BigDecimal, label: String) - /** GetResult implicit for fetching Transaction objects using plain SQL queries */ - implicit def GetResultTransaction(implicit e0: GR[String], e1: GR[java.sql.Timestamp], e2: GR[scala.math.BigDecimal]): GR[Transaction] = GR { + implicit def GetResultTransaction(implicit e0: GR[String], e1: GR[java.sql.Timestamp], e2: GR[scala.math.BigDecimal]): GR[Transaction] = GR{ prs => import prs._ - Transaction.tupled((<<[String], <<[String], <<[java.sql.Timestamp], <<[scala.math.BigDecimal], <<[String])) + Transaction.tupled((<<[String], <<[String], <<[java.sql.Timestamp], <<[scala.math.BigDecimal], <<[String])) } - /** Table description of table transactions. Objects of this class serve as prototypes for rows in queries. */ class Transactions(_tableTag: Tag) extends Table[Transaction](_tableTag, "transactions") { - def * = (uuid, userUuid, transactionDate, amount, label) <>(Transaction.tupled, Transaction.unapply) - + def * = (uuid, userUuid, transactionDate, amount, label) <> (Transaction.tupled, Transaction.unapply) /** Maps whole row to an option. Useful for outer joins. */ - def ? = (uuid.?, userUuid.?, transactionDate.?, amount.?, label.?).shaped.<>({ r => import r._; _1.map(_ => Transaction.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_: Any) => throw new Exception("Inserting into ? projection not supported.")) - + def ? = (uuid.?, userUuid.?, transactionDate.?, amount.?, label.?).shaped.<>({r=>import r._; _1.map(_=> Transaction.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported.")) + /** Database column uuid DBType(varchar), PrimaryKey, Length(36,true) */ - val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36, varying = true)) + val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36,varying=true)) /** Database column user_uuid DBType(varchar), Length(36,true) */ - val userUuid: Column[String] = column[String]("user_uuid", O.Length(36, varying = true)) + val userUuid: Column[String] = column[String]("user_uuid", O.Length(36,varying=true)) /** Database column transaction_date DBType(timestamptz) */ val transactionDate: Column[java.sql.Timestamp] = column[java.sql.Timestamp]("transaction_date") /** Database column amount DBType(numeric) */ val amount: Column[scala.math.BigDecimal] = column[scala.math.BigDecimal]("amount") /** Database column label DBType(varchar), Length(255,true) */ - val label: Column[String] = column[String]("label", O.Length(255, varying = true)) - + val label: Column[String] = column[String]("label", O.Length(255,varying=true)) + /** Foreign key referencing Users (database name transactions_users_fk) */ - lazy val usersFk = foreignKey("transactions_users_fk", userUuid, Users)(r => r.uuid, onUpdate = ForeignKeyAction.NoAction, onDelete = ForeignKeyAction.NoAction) + lazy val usersFk = foreignKey("transactions_users_fk", userUuid, Users)(r => r.uuid, onUpdate=ForeignKeyAction.NoAction, onDelete=ForeignKeyAction.NoAction) } - /** Collection-like TableQuery object for table Transactions */ lazy val Transactions = new TableQuery(tag => new Transactions(tag)) - + /** Entity class storing rows of table Users - * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) - * @param username Database column username DBType(varchar), Length(20,true) - * @param email Database column email DBType(varchar), Length(255,true) - * @param userPassword Database column user_password DBType(varchar), Length(255,true) - * @param firstName Database column first_name DBType(varchar), Length(255,true) - * @param lastName Database column last_name DBType(varchar), Length(255,true) - * @param countryCode Database column country_code DBType(varchar), Length(2,true) - * @param postalCode Database column postal_code DBType(varchar), Length(20,true) - * @param address Database column address DBType(varchar), Length(255,true) - * @param phone Database column phone DBType(varchar), Length(20,true) - * @param birthdate Database column birthdate DBType(timestamptz) */ - case class User(uuid: String, username: String, email: String, userPassword: String, firstName: String, lastName: String, countryCode: String, postalCode: String, address: String, phone: String, birthdate: java.sql.Timestamp) - + * @param uuid Database column uuid DBType(varchar), PrimaryKey, Length(36,true) + * @param username Database column username DBType(varchar), Length(20,true) + * @param email Database column email DBType(varchar), Length(255,true) + * @param userPassword Database column user_password DBType(varchar), Length(255,true) + * @param creationDate Database column creation_date DBType(timestamptz) + * @param firstName Database column first_name DBType(varchar), Length(255,true) + * @param lastName Database column last_name DBType(varchar), Length(255,true) + * @param countryCode Database column country_code DBType(varchar), Length(2,true) + * @param postalCode Database column postal_code DBType(varchar), Length(20,true) + * @param address Database column address DBType(varchar), Length(255,true) + * @param phone Database column phone DBType(varchar), Length(20,true) + * @param birthdate Database column birthdate DBType(timestamptz) */ + case class User(uuid: String, username: String, email: String, userPassword: String, creationDate: java.sql.Timestamp, firstName: String, lastName: String, countryCode: String, postalCode: String, address: String, phone: String, birthdate: java.sql.Timestamp) /** GetResult implicit for fetching User objects using plain SQL queries */ - implicit def GetResultUser(implicit e0: GR[String], e1: GR[java.sql.Timestamp]): GR[User] = GR { + implicit def GetResultUser(implicit e0: GR[String], e1: GR[java.sql.Timestamp]): GR[User] = GR{ prs => import prs._ - User.tupled((<<[String], <<[String], <<[String], <<[String], <<[String], <<[String], <<[String], <<[String], <<[String], <<[String], <<[java.sql.Timestamp])) + User.tupled((<<[String], <<[String], <<[String], <<[String], <<[java.sql.Timestamp], <<[String], <<[String], <<[String], <<[String], <<[String], <<[String], <<[java.sql.Timestamp])) } - /** Table description of table users. Objects of this class serve as prototypes for rows in queries. */ class Users(_tableTag: Tag) extends Table[User](_tableTag, "users") { - def * = (uuid, username, email, userPassword, firstName, lastName, countryCode, postalCode, address, phone, birthdate) <>(User.tupled, User.unapply) - + def * = (uuid, username, email, userPassword, creationDate, firstName, lastName, countryCode, postalCode, address, phone, birthdate) <> (User.tupled, User.unapply) /** Maps whole row to an option. Useful for outer joins. */ - def ? = (uuid.?, username.?, email.?, userPassword.?, firstName.?, lastName.?, countryCode.?, postalCode.?, address.?, phone.?, birthdate.?).shaped.<>({ r => import r._; _1.map(_ => User.tupled((_1.get, _2.get, _3.get, _4.get, _5.get, _6.get, _7.get, _8.get, _9.get, _10.get, _11.get)))}, (_: Any) => throw new Exception("Inserting into ? projection not supported.")) - + def ? = (uuid.?, username.?, email.?, userPassword.?, creationDate.?, firstName.?, lastName.?, countryCode.?, postalCode.?, address.?, phone.?, birthdate.?).shaped.<>({r=>import r._; _1.map(_=> User.tupled((_1.get, _2.get, _3.get, _4.get, _5.get, _6.get, _7.get, _8.get, _9.get, _10.get, _11.get, _12.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported.")) + /** Database column uuid DBType(varchar), PrimaryKey, Length(36,true) */ - val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36, varying = true)) + val uuid: Column[String] = column[String]("uuid", O.PrimaryKey, O.Length(36,varying=true)) /** Database column username DBType(varchar), Length(20,true) */ - val username: Column[String] = column[String]("username", O.Length(20, varying = true)) + val username: Column[String] = column[String]("username", O.Length(20,varying=true)) /** Database column email DBType(varchar), Length(255,true) */ - val email: Column[String] = column[String]("email", O.Length(255, varying = true)) + val email: Column[String] = column[String]("email", O.Length(255,varying=true)) /** Database column user_password DBType(varchar), Length(255,true) */ - val userPassword: Column[String] = column[String]("user_password", O.Length(255, varying = true)) + val userPassword: Column[String] = column[String]("user_password", O.Length(255,varying=true)) + /** Database column creation_date DBType(timestamptz) */ + val creationDate: Column[java.sql.Timestamp] = column[java.sql.Timestamp]("creation_date") /** Database column first_name DBType(varchar), Length(255,true) */ - val firstName: Column[String] = column[String]("first_name", O.Length(255, varying = true)) + val firstName: Column[String] = column[String]("first_name", O.Length(255,varying=true)) /** Database column last_name DBType(varchar), Length(255,true) */ - val lastName: Column[String] = column[String]("last_name", O.Length(255, varying = true)) + val lastName: Column[String] = column[String]("last_name", O.Length(255,varying=true)) /** Database column country_code DBType(varchar), Length(2,true) */ - val countryCode: Column[String] = column[String]("country_code", O.Length(2, varying = true)) + val countryCode: Column[String] = column[String]("country_code", O.Length(2,varying=true)) /** Database column postal_code DBType(varchar), Length(20,true) */ - val postalCode: Column[String] = column[String]("postal_code", O.Length(20, varying = true)) + val postalCode: Column[String] = column[String]("postal_code", O.Length(20,varying=true)) /** Database column address DBType(varchar), Length(255,true) */ - val address: Column[String] = column[String]("address", O.Length(255, varying = true)) + val address: Column[String] = column[String]("address", O.Length(255,varying=true)) /** Database column phone DBType(varchar), Length(20,true) */ - val phone: Column[String] = column[String]("phone", O.Length(20, varying = true)) + val phone: Column[String] = column[String]("phone", O.Length(20,varying=true)) /** Database column birthdate DBType(timestamptz) */ val birthdate: Column[java.sql.Timestamp] = column[java.sql.Timestamp]("birthdate") - + /** Uniqueness Index over (username) (database name username_unique) */ - val index1 = index("username_unique", username, unique = true) + val index1 = index("username_unique", username, unique=true) } - /** Collection-like TableQuery object for table Users */ lazy val Users = new TableQuery(tag => new Users(tag)) } \ No newline at end of file diff --git a/app/models/Views.scala b/app/models/Views.scala new file mode 100644 index 0000000..a76b043 --- /dev/null +++ b/app/models/Views.scala @@ -0,0 +1,84 @@ +package models +// AUTO-GENERATED Slick data model +/** Stand-alone Slick data model for immediate use */ +object Views extends { + val profile = play.api.db.slick.Config.driver +} with Views + +/** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */ +trait Views { + val profile: scala.slick.driver.JdbcProfile + import profile.simple._ + import scala.slick.model.ForeignKeyAction + // NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns. + import scala.slick.jdbc.{GetResult => GR} + + /** DDL for all tables. Call .create to execute. */ + lazy val ddl = Accounts.ddl ++ Sales.ddl + + /** Entity class storing rows of table Accounts + * @param userUuid Database column user_uuid DBType(varchar), Length(36,true), Default(None) + * @param balance Database column balance DBType(numeric), Default(None) + * @param openBids Database column open_bids DBType(numeric), Default(None) + * @param equity Database column equity DBType(numeric), Default(None) */ + case class Account(userUuid: Option[String] = None, balance: Option[scala.math.BigDecimal] = None, openBids: Option[scala.math.BigDecimal] = None, equity: Option[scala.math.BigDecimal] = None) + /** GetResult implicit for fetching Account objects using plain SQL queries */ + implicit def GetResultAccount(implicit e0: GR[Option[String]], e1: GR[Option[scala.math.BigDecimal]]): GR[Account] = GR{ + prs => import prs._ + Account.tupled((< (Account.tupled, Account.unapply) + + /** Database column user_uuid DBType(varchar), Length(36,true), Default(None) */ + val userUuid: Column[Option[String]] = column[Option[String]]("user_uuid", O.Length(36,varying=true), O.Default(None)) + /** Database column balance DBType(numeric), Default(None) */ + val balance: Column[Option[scala.math.BigDecimal]] = column[Option[scala.math.BigDecimal]]("balance", O.Default(None)) + /** Database column open_bids DBType(numeric), Default(None) */ + val openBids: Column[Option[scala.math.BigDecimal]] = column[Option[scala.math.BigDecimal]]("open_bids", O.Default(None)) + /** Database column equity DBType(numeric), Default(None) */ + val equity: Column[Option[scala.math.BigDecimal]] = column[Option[scala.math.BigDecimal]]("equity", O.Default(None)) + } + /** Collection-like TableQuery object for table Accounts */ + lazy val Accounts = new TableQuery(tag => new Accounts(tag)) + + /** Entity class storing rows of table Sales + * @param itemUuid Database column item_uuid DBType(varchar), Length(36,true), Default(None) + * @param sellerUuid Database column seller_uuid DBType(varchar), Length(36,true), Default(None) + * @param startDate Database column start_date DBType(timestamptz), Default(None) + * @param endDate Database column end_date DBType(timestamptz), Default(None) + * @param bestBidUuid Database column best_bid_uuid DBType(varchar), Length(36,true), Default(None) + * @param bestBidderUuid Database column best_bidder_uuid DBType(varchar), Length(36,true), Default(None) + * @param bestOffer Database column best_offer DBType(numeric), Default(None) + * @param charges Database column charges DBType(numeric), Default(None) */ + case class Sale(itemUuid: Option[String] = None, sellerUuid: Option[String] = None, startDate: Option[java.sql.Timestamp] = None, endDate: Option[java.sql.Timestamp] = None, bestBidUuid: Option[String] = None, bestBidderUuid: Option[String] = None, bestOffer: Option[scala.math.BigDecimal] = None, charges: Option[scala.math.BigDecimal] = None) + /** GetResult implicit for fetching Sale objects using plain SQL queries */ + implicit def GetResultSale(implicit e0: GR[Option[String]], e1: GR[Option[java.sql.Timestamp]], e2: GR[Option[scala.math.BigDecimal]]): GR[Sale] = GR{ + prs => import prs._ + Sale.tupled((< (Sale.tupled, Sale.unapply) + + /** Database column item_uuid DBType(varchar), Length(36,true), Default(None) */ + val itemUuid: Column[Option[String]] = column[Option[String]]("item_uuid", O.Length(36,varying=true), O.Default(None)) + /** Database column seller_uuid DBType(varchar), Length(36,true), Default(None) */ + val sellerUuid: Column[Option[String]] = column[Option[String]]("seller_uuid", O.Length(36,varying=true), O.Default(None)) + /** Database column start_date DBType(timestamptz), Default(None) */ + val startDate: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("start_date", O.Default(None)) + /** Database column end_date DBType(timestamptz), Default(None) */ + val endDate: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("end_date", O.Default(None)) + /** Database column best_bid_uuid DBType(varchar), Length(36,true), Default(None) */ + val bestBidUuid: Column[Option[String]] = column[Option[String]]("best_bid_uuid", O.Length(36,varying=true), O.Default(None)) + /** Database column best_bidder_uuid DBType(varchar), Length(36,true), Default(None) */ + val bestBidderUuid: Column[Option[String]] = column[Option[String]]("best_bidder_uuid", O.Length(36,varying=true), O.Default(None)) + /** Database column best_offer DBType(numeric), Default(None) */ + val bestOffer: Column[Option[scala.math.BigDecimal]] = column[Option[scala.math.BigDecimal]]("best_offer", O.Default(None)) + /** Database column charges DBType(numeric), Default(None) */ + val charges: Column[Option[scala.math.BigDecimal]] = column[Option[scala.math.BigDecimal]]("charges", O.Default(None)) + } + /** Collection-like TableQuery object for table Sales */ + lazy val Sales = new TableQuery(tag => new Sales(tag)) +} \ No newline at end of file diff --git a/build.sbt b/build.sbt index 62c7ad4..6fda37f 100644 --- a/build.sbt +++ b/build.sbt @@ -1,19 +1,32 @@ -name := """mini-bay""" -version := "1.0-SNAPSHOT" - -lazy val root = (project in file(".")).enablePlugins(PlayScala) +lazy val commonSettings = Seq( + scalaVersion := "2.11.1", + version := "1.0-SNAPSHOT" +) -scalaVersion := "2.11.1" +lazy val root = (project in file(".")) + .settings(commonSettings: _*) + .settings( + name := """mini-bay""", + libraryDependencies ++= Seq( + jdbc, + cache, + ws, + "com.typesafe.slick" %% "slick" % "2.1.0", + "com.typesafe.slick" %% "slick-extensions" % "2.1.0", + "postgresql" % "postgresql" % "9.1-901.jdbc4", + "com.typesafe.play" %% "play-slick" % "0.8.1", + "org.slf4j" % "slf4j-nop" % "1.6.4" + ) + ) + .enablePlugins(PlayScala).dependsOn(codegen) -libraryDependencies ++= Seq( - jdbc, - anorm, - cache, - ws, - "com.typesafe.slick" %% "slick" % "2.1.0", - //"com.typesafe.slick" %% "slick-codegen" % "2.1.0", - "postgresql" % "postgresql" % "9.1-901.jdbc4", - "com.typesafe.play" %% "play-slick" % "0.8.1", - "org.slf4j" % "slf4j-nop" % "1.6.4" -) +lazy val codegen = (project in file("codegen")) + .settings(commonSettings: _*) + .settings( + name := """codegen""", + libraryDependencies ++= Seq( + "com.typesafe.slick" %% "slick-codegen" % "2.1.0", + "org.scala-lang" % "scala-reflect" % "2.11.1" + ) + ) diff --git a/codegen/ModelGenerator.scala b/codegen/ModelGenerator.scala new file mode 100644 index 0000000..9e74ff1 --- /dev/null +++ b/codegen/ModelGenerator.scala @@ -0,0 +1,60 @@ +package codegen + +object ModelGenerator { + + import scala.reflect.runtime.currentMirror + import scala.slick.codegen.SourceCodeGenerator + import scala.slick.driver.JdbcProfile + import scala.slick.model.Model + + + class CustomSourceCodeGenerator(model: Model) extends SourceCodeGenerator(model) { + override def entityName = dbTableName => dbTableName.dropRight(1).toLowerCase.toCamelCase + + override def tableName = dbTableName => dbTableName.toLowerCase.toCamelCase + } + + def main(args: Array[String]) = { + + args.toList match { + + case List(slickDriver, jdbcDriver, url, tables, outputFolder, className, pkg) => + + val driver: JdbcProfile = { + val module = currentMirror.staticModule(slickDriver) + val reflectedModule = currentMirror.reflectModule(module) + val driver = reflectedModule.instance.asInstanceOf[JdbcProfile] + driver + } + + driver.simple.Database.forURL(url, driver = jdbcDriver).withSession { implicit session => + val tableSeq = Option(driver.getTables.list.filter { t => tables.split(",") contains t.name.name}.toSeq) + val model = driver.createModel(tableSeq) + new CustomSourceCodeGenerator(model).writeToFile( + "play.api.db.slick.Config.driver", + outputFolder, + pkg, + className, + className + ".scala" + ) + } + + + case _ => + println( """ +Usage: SourceCodeGenerator.main(Array( slickDriver, jdbcDriver, url, outputFolder, pkg )) +slickDriver: Fully qualified name of Slick driver class, e.g. "scala.slick.driver.PostgresDriver" +jdbcDriver: Fully qualified name of jdbc driver class, e.g. "org.postgresql.Driver" +url: jdbc url, e.g. "jdbc:postgresql://localhost/test?user=someone&password=somepass&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory" +tables: List of the tables +outputFolder: Place where the package folder structure should be put +className : Name of the class +pkg: Scala package the generated code should be places in + """.trim + ) + + } + + } + +} diff --git a/conf/application.conf b/conf/application.conf index 1db3b0e..72c25b7 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -9,6 +9,7 @@ # # See http://www.playframework.com/documentation/latest/ApplicationSecret for more details. application.secret="e@_?aqO:dGjH9LfXK?T;V`Pb9K?S22igdQVvgN?b?vnb[UkXbORmWO`O9>mWabI5" +application.secret=${?APP_SECRET} # The application languages # ~~~~~ @@ -36,15 +37,22 @@ application.langs="en" # You can declare as many datasources as you want. # By convention, the default datasource is named `default` # -# db.default.driver=org.h2.Driver -# db.default.url="jdbc:h2:mem:play" +db.default.driver=org.postgresql.Driver +db.default.driver=${?DATABASE_DRIVER} +db.default.url="jdbc:postgresql://localhost/minibay_dev" +db.default.url=${?DATABASE_URL} # db.default.user=sa # db.default.password="" +slick.default="models.*" +slick.profile="scala.slick.driver.PostgresDriver" +slick.profile=${?SLICK_PROFILE} + # Evolutions # ~~~~~ # You can disable evolutions if needed -# evolutionplugin=disabled +evolutionplugin=disabled +# applyEvolutions.default=true # Logger # ~~~~~ diff --git a/generateModel.sbt b/generateModel.sbt new file mode 100644 index 0000000..8597c05 --- /dev/null +++ b/generateModel.sbt @@ -0,0 +1,21 @@ + +// code generation task that calls the customized code generator +lazy val slick = TaskKey[Seq[File]]("generateModel") +lazy val slickCodeGenTask = (sourceManaged, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) => + //val outputDir = (dir / "slick").getPath + val outputDir = "app" + val url = System.getenv("DATABASE_URL") + val jdbcDriver = "org.postgresql.Driver" + val slickDriver = "scala.slick.driver.PostgresDriver" + val pkg = "models" + val tables = "bids,charges,items,transactions,users" + val views = "accounts,sales" + toError(r.run("codegen.ModelGenerator", cp.files, Array(slickDriver, jdbcDriver, url, tables, outputDir, "Tables", pkg), s.log)) + toError(r.run("codegen.ModelGenerator", cp.files, Array(slickDriver, jdbcDriver, url, views, outputDir, "Views", pkg), s.log)) + Seq( + file(outputDir + "/models/Tables.scala"), + file(outputDir + "/models/Views.scala") + ) +} + +slick <<= slickCodeGenTask -- cgit v1.2.3