Skip to content

Instantly share code, notes, and snippets.

@nuboat
Created June 30, 2018 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuboat/e370632dd0a0e38818be67c4d6e27121 to your computer and use it in GitHub Desktop.
Save nuboat/e370632dd0a0e38818be67c4d6e27121 to your computer and use it in GitHub Desktop.
Entity
@TableSchema(pk = "id", name = "accounts")
case class AccountEntity(id: Long
, clientId: Long
, isActive: Boolean
, isVerify: Boolean
, accountRole: Int
, username: String
, passwordHash: String = ""
, email: String = ""
, firstname: String = ""
, lastname: String = ""
, metaJson: String = "{}"
, creatorId: Long
, created: DateTime = DateTime.now)
package in.norbor.yoda.orm.generated
import java.sql.{Connection, ResultSet}
import entities.AccountEntity
import in.norbor.yoda.implicits.JavaSqlImprovement._
import in.norbor.yoda.jtype._
import in.norbor.yoda.orm.PStatement
/**
* @author Yoda B
*/
trait accountsSQLGenerated {
private val QUERY_ID: String = "SELECT * FROM accounts WHERE id = ?"
private val INSERT: String = "INSERT INTO accounts (id, client_id, is_active, is_verify, account_role, username, password_hash, email, firstname, lastname, meta_json, creator_id, created) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
private val UPDATE: String = "UPDATE accounts SET client_id = ?, is_active = ?, is_verify = ?, account_role = ?, username = ?, password_hash = ?, email = ?, firstname = ?, lastname = ?, meta_json = ?, creator_id = ?, created = ? WHERE id = ?"
private val DELETE: String = "DELETE FROM accounts WHERE id = ?"
private val COUNT: String = "SELECT COUNT(1) FROM accounts"
private val COLUMNS: Set[String] = Set("id", "client_id", "is_active", "is_verify", "account_role", "username", "password_hash", "email", "firstname", "lastname", "meta_json", "creator_id", "created")
def insert(e: AccountEntity)
(implicit conn: Connection): Int = PStatement(INSERT)
.setLong(e.id)
.setLong(e.clientId)
.setBoolean(e.isActive)
.setBoolean(e.isVerify)
.setInt(e.accountRole)
.setString(e.username)
.setString(e.passwordHash)
.setString(e.email)
.setString(e.firstname)
.setString(e.lastname)
.setString(e.metaJson)
.setLong(e.creatorId)
.setDateTime(e.created)
.update
def get(id: Long)
(implicit conn: Connection): Option[AccountEntity] = PStatement(QUERY_ID)
.setLong(id)
.queryOne(parse)
def update(e: AccountEntity)
(implicit conn: Connection): Int = PStatement(UPDATE)
.setLong(e.clientId)
.setBoolean(e.isActive)
.setBoolean(e.isVerify)
.setInt(e.accountRole)
.setString(e.username)
.setString(e.passwordHash)
.setString(e.email)
.setString(e.firstname)
.setString(e.lastname)
.setString(e.metaJson)
.setLong(e.creatorId)
.setDateTime(e.created)
.setLong(e.id)
.update
def delete(id: Long)(implicit conn: Connection): Int = PStatement(DELETE)
.setLong(id)
.update
def count()(implicit conn: Connection): Long = PStatement(COUNT)
.queryOne(rs => rs.getLong(1))
.get
protected def verifyName(p: String): Unit = if (!COLUMNS.contains(p)) throw new IllegalArgumentException(s"$p has problem.")
protected def parse(rs: ResultSet) = AccountEntity(
id = rs.getLong("id")
, clientId = rs.getLong("client_id")
, isActive = rs.getBoolean("is_active")
, isVerify = rs.getBoolean("is_verify")
, accountRole = rs.getInt("account_role")
, username = rs.getString("username")
, passwordHash = rs.getString("password_hash")
, email = rs.getString("email")
, firstname = rs.getString("firstname")
, lastname = rs.getString("lastname")
, metaJson = rs.getString("meta_json")
, creatorId = rs.getLong("creator_id")
, created = rs.getDateTime("created")
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment