This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ed3c59f
Showing
26 changed files
with
1,101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
.settings | ||
.classpath | ||
.project | ||
*.iml | ||
*.ipr | ||
*.iws | ||
dist/ | ||
lib_managed/ | ||
project/activator* | ||
project/boot/ | ||
project/plugins/project/ | ||
target/ | ||
|
||
# use glob syntax. | ||
syntax: glob | ||
*.ser | ||
*.class | ||
*~ | ||
*.bak | ||
#*.off | ||
*.old | ||
|
||
# eclipse conf file | ||
.settings | ||
.classpath | ||
.project | ||
.manager | ||
.scala_dependencies | ||
|
||
# idea | ||
.idea | ||
*.iml | ||
|
||
# building | ||
target | ||
build | ||
null | ||
tmp* | ||
temp* | ||
dist | ||
test-output | ||
build.log | ||
|
||
# other scm | ||
.svn | ||
.CVS | ||
.hg* | ||
|
||
# switch to regexp syntax. | ||
# syntax: regexp | ||
# ^\.pc/ | ||
|
||
#SHITTY output not in target directory | ||
build.log | ||
.DS_Store | ||
derby.log | ||
|
||
*.db | ||
|
||
.lib | ||
sbt | ||
|
||
logs | ||
sandbox/db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name=scalikejdbc-activator-template | ||
title=Hello ScalikeJDBC! | ||
description=Just write SQL and get things done! ScalikeJDBC is A tidy SQL-based DB access library for Scala developers. This library naturally wraps JDBC APIs and provides you easy-to-use APIs. | ||
tags=Basics,Scala,Database,Starter | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package controllers | ||
|
||
import play.api._, mvc._ | ||
import play.api.data._, Forms._ | ||
|
||
import org.json4s._, ext.JodaTimeSerializers, native.JsonMethods._ | ||
import com.github.tototoshi.play2.json4s.native._ | ||
|
||
import models._ | ||
|
||
object Companies extends Controller with Json4s { | ||
|
||
implicit val formats = DefaultFormats ++ JodaTimeSerializers.all | ||
|
||
def all = Action { | ||
Ok(Extraction.decompose(Company.findAll)) | ||
} | ||
|
||
def show(id: Long) = Action { | ||
Company.find(id).map { company => Ok(Extraction.decompose(company)) } getOrElse NotFound | ||
} | ||
|
||
private val companyForm = Form(tuple("name" -> text, "url" -> text)) | ||
|
||
def create = Action { implicit req => | ||
val (name, url) = companyForm.bindFromRequest.get | ||
val company = Company.create(name = name, url = if (url.isEmpty) None else Some(url)) | ||
Created.withHeaders(LOCATION -> s"/companies/${company.id}") | ||
} | ||
|
||
def delete(id: Long) = Action { | ||
Company.find(id).map { company => | ||
company.destroy() | ||
NoContent | ||
} getOrElse NotFound | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package controllers | ||
|
||
import play.api._, mvc._ | ||
import play.api.data._, Forms._ | ||
|
||
import org.json4s._, ext.JodaTimeSerializers, native.JsonMethods._ | ||
import com.github.tototoshi.play2.json4s.native._ | ||
|
||
import models._ | ||
|
||
object Programmers extends Controller with Json4s { | ||
|
||
implicit val formats = DefaultFormats ++ JodaTimeSerializers.all | ||
|
||
def all = Action { | ||
Ok(Extraction.decompose(Programmer.findAll)) | ||
} | ||
|
||
def show(id: Long) = Action { | ||
Programmer.find(id).map(programmer => Ok(Extraction.decompose(programmer))) getOrElse NotFound | ||
} | ||
|
||
private val programmerForm = Form(tuple("name" -> text, "companyId" -> optional(longNumber))) | ||
|
||
def create = Action { implicit req => | ||
val (name, companyId) = programmerForm.bindFromRequest.get | ||
val programmer = Programmer.create(name = name, companyId = companyId) | ||
Created.withHeaders(LOCATION -> s"/programmers/${programmer.id}") | ||
} | ||
|
||
def addSkill(programmerId: Long, skillId: Long) = Action { | ||
Programmer.find(programmerId).map { programmer => | ||
try { | ||
Skill.find(skillId).map(skill => programmer.addSkill(skill)) | ||
Ok | ||
} catch { case e: Exception => Conflict } | ||
} getOrElse NotFound | ||
} | ||
|
||
def deleteSkill(programmerId: Long, skillId: Long) = Action { | ||
Programmer.find(programmerId).map { programmer => | ||
Skill.find(skillId).map(skill => programmer.deleteSkill(skill)) | ||
Ok | ||
} getOrElse NotFound | ||
} | ||
|
||
def delete(id: Long) = Action { | ||
Programmer.find(id).map { programmer => | ||
programmer.destroy() | ||
NoContent | ||
} getOrElse NotFound | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package controllers | ||
|
||
import play.api._, mvc._ | ||
|
||
object Root extends Controller { | ||
|
||
def index = Action { | ||
SeeOther("/companies") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package controllers | ||
|
||
import play.api._, mvc._ | ||
import play.api.data._, Forms._ | ||
|
||
import org.json4s._, ext.JodaTimeSerializers, native.JsonMethods._ | ||
import com.github.tototoshi.play2.json4s.native._ | ||
|
||
import models._ | ||
|
||
object Skills extends Controller with Json4s { | ||
|
||
implicit val formats = DefaultFormats ++ JodaTimeSerializers.all | ||
|
||
def all = Action { | ||
Ok(Extraction.decompose(Skill.findAll)) | ||
} | ||
|
||
def show(id: Long) = Action { | ||
Skill.find(id).map(skill => Ok(Extraction.decompose(skill))) getOrElse NotFound | ||
} | ||
|
||
private val skillForm = Form("name" -> text) | ||
|
||
def create = Action { implicit req => | ||
val name = skillForm.bindFromRequest.get | ||
val skill = Skill.create(name = name) | ||
Created.withHeaders(LOCATION -> s"/skills/${skill.id}") | ||
} | ||
|
||
def delete(id: Long) = Action { | ||
Skill.find(id).map { skill => | ||
skill.destroy() | ||
NoContent | ||
} getOrElse NotFound | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package misc | ||
|
||
import scalikejdbc._, SQLInterpolation._ | ||
|
||
object DBInitializer { | ||
|
||
def run() { | ||
DB readOnly { implicit s => | ||
try { | ||
sql"select 1 from programmer limit 1".map(_.long(1)).single.apply() | ||
} catch { | ||
case e: java.sql.SQLException => | ||
DB autoCommit { implicit s => | ||
sql""" | ||
create sequence programmer_id_seq start with 1; | ||
create table programmer ( | ||
id bigint not null default nextval('programmer_id_seq') primary key, | ||
name varchar(255) not null, | ||
company_id bigint, | ||
created_timestamp timestamp not null, | ||
deleted_timestamp timestamp | ||
); | ||
|
||
create sequence company_id_seq start with 1; | ||
create table company ( | ||
id bigint not null default nextval('company_id_seq') primary key, | ||
name varchar(255) not null, | ||
url varchar(255), | ||
created_at timestamp not null, | ||
deleted_at timestamp | ||
); | ||
|
||
create sequence skill_id_seq start with 1; | ||
create table skill ( | ||
id bigint not null default nextval('skill_id_seq') primary key, | ||
name varchar(255) not null, | ||
created_at timestamp not null, | ||
deleted_at timestamp | ||
); | ||
|
||
create table programmer_skill ( | ||
programmer_id bigint not null, | ||
skill_id bigint not null, | ||
primary key(programmer_id, skill_id) | ||
); | ||
|
||
insert into company (name, url, created_at) values ('Typesafe', 'http://typesafe.com/', current_timestamp); | ||
insert into company (name, url, created_at) values ('Oracle', 'http://www.oracle.com/', current_timestamp); | ||
insert into company (name, url, created_at) values ('Google', 'http://www.google.com/', current_timestamp); | ||
insert into company (name, url, created_at) values ('Microsoft', 'http://www.microsoft.com/', current_timestamp); | ||
|
||
insert into skill (name, created_at) values ('Scala', current_timestamp); | ||
insert into skill (name, created_at) values ('Java', current_timestamp); | ||
insert into skill (name, created_at) values ('Ruby', current_timestamp); | ||
insert into skill (name, created_at) values ('MySQL', current_timestamp); | ||
insert into skill (name, created_at) values ('PostgreSQL', current_timestamp); | ||
""".execute.apply() | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package misc | ||
|
||
import scalikejdbc._ | ||
|
||
class HibernateSQLFormatter extends SQLFormatter { | ||
|
||
private val formatter = new org.hibernate.engine.jdbc.internal.BasicFormatterImpl | ||
|
||
override def format(sql: String) = formatter.format(sql) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package models | ||
|
||
import scalikejdbc._, SQLInterpolation._ | ||
import org.joda.time.DateTime | ||
|
||
case class Company( | ||
id: Long, | ||
name: String, | ||
url: Option[String] = None, | ||
createdAt: DateTime, | ||
deletedAt: Option[DateTime] = None) { | ||
|
||
def save()(implicit session: DBSession = Company.autoSession): Company = Company.save(this)(session) | ||
def destroy()(implicit session: DBSession = Company.autoSession): Unit = Company.destroy(id)(session) | ||
} | ||
|
||
object Company extends SQLSyntaxSupport[Company] { | ||
|
||
def apply(c: SyntaxProvider[Company])(rs: WrappedResultSet): Company = apply(c.resultName)(rs) | ||
def apply(c: ResultName[Company])(rs: WrappedResultSet): Company = new Company( | ||
id = rs.long(c.id), | ||
name = rs.string(c.name), | ||
url = rs.stringOpt(c.url), | ||
createdAt = rs.timestamp(c.createdAt).toDateTime, | ||
deletedAt = rs.timestampOpt(c.deletedAt).map(_.toDateTime) | ||
) | ||
|
||
val c = Company.syntax("c") | ||
val autoSession = AutoSession | ||
private val isNotDeleted = sqls.isNull(c.deletedAt) | ||
|
||
def find(id: Long)(implicit session: DBSession = autoSession): Option[Company] = withSQL { | ||
select.from(Company as c).where.eq(c.id, id).and.append(isNotDeleted) | ||
}.map(Company(c)).single.apply() | ||
|
||
def findAll()(implicit session: DBSession = autoSession): List[Company] = withSQL { | ||
select.from(Company as c) | ||
.where.append(isNotDeleted) | ||
.orderBy(c.id) | ||
}.map(Company(c)).list.apply() | ||
|
||
def countAll()(implicit session: DBSession = autoSession): Long = withSQL { | ||
select(sqls.count).from(Company as c).where.append(isNotDeleted) | ||
}.map(rs => rs.long(1)).single.apply().get | ||
|
||
def findAllBy(where: SQLSyntax)(implicit session: DBSession = autoSession): List[Company] = withSQL { | ||
select.from(Company as c) | ||
.where.append(isNotDeleted).and.append(sqls"${where}") | ||
.orderBy(c.id) | ||
}.map(Company(c)).list.apply() | ||
|
||
def countBy(where: SQLSyntax)(implicit session: DBSession = autoSession): Long = withSQL { | ||
select(sqls.count).from(Company as c).where.append(isNotDeleted).and.append(sqls"${where}") | ||
}.map(_.long(1)).single.apply().get | ||
|
||
def create(name: String, url: Option[String] = None, createdAt: DateTime = DateTime.now)(implicit session: DBSession = autoSession): Company = { | ||
val id = withSQL { | ||
insert.into(Company) | ||
.columns(column.name, column.url, column.createdAt) | ||
.values(name, url, createdAt) | ||
}.updateAndReturnGeneratedKey.apply() | ||
|
||
Company(id = id, name = name, url = url, createdAt = createdAt) | ||
} | ||
|
||
def save(m: Company)(implicit session: DBSession = autoSession): Company = { | ||
withSQL { | ||
update(Company).set(column.name -> m.name, column.url -> m.url).where.eq(column.id, m.id).and.isNull(column.deletedAt) | ||
}.update.apply() | ||
m | ||
} | ||
|
||
def destroy(id: Long)(implicit session: DBSession = autoSession): Unit = withSQL { | ||
update(Company).set(column.deletedAt -> DateTime.now).where.eq(column.id, id) | ||
}.update.apply() | ||
|
||
} |
Oops, something went wrong.