From bac4b60c8d02785551cbe5e892ed547af28bfaf3 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Wed, 13 Mar 2013 18:48:33 +0900 Subject: [PATCH] Added default tableName implementation --- .../scala/scalikejdbc/SQLInterpolation.scala | 6 ++++- .../scalikejdbc/SQLInterpolationSpec.scala | 25 +++++++------------ 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/scalikejdbc-interpolation/src/main/scala/scalikejdbc/SQLInterpolation.scala b/scalikejdbc-interpolation/src/main/scala/scalikejdbc/SQLInterpolation.scala index 0bc869103..525546159 100644 --- a/scalikejdbc-interpolation/src/main/scala/scalikejdbc/SQLInterpolation.scala +++ b/scalikejdbc-interpolation/src/main/scala/scalikejdbc/SQLInterpolation.scala @@ -25,7 +25,11 @@ object SQLInterpolation { */ trait SQLSyntaxSupport[A] { - def tableName: String + def tableName: String = { + val className = this.getClass.getName.replaceFirst("\\$$", "").replaceFirst("^.+\\.", "").replaceFirst("^.+\\$", "") + SQLSyntaxProvider.toSnakeCase(className) + } + def columns: Seq[String] = SQLSyntaxSupportLoadedColumns.getOrElseUpdate(tableName, DB.getColumnNames(tableName).map(_.toLowerCase)) def forceUpperCase: Boolean = false diff --git a/scalikejdbc-interpolation/src/test/scala/scalikejdbc/SQLInterpolationSpec.scala b/scalikejdbc-interpolation/src/test/scala/scalikejdbc/SQLInterpolationSpec.scala index 5eee33e51..7f587cd89 100644 --- a/scalikejdbc-interpolation/src/test/scala/scalikejdbc/SQLInterpolationSpec.scala +++ b/scalikejdbc-interpolation/src/test/scala/scalikejdbc/SQLInterpolationSpec.scala @@ -275,8 +275,6 @@ class SQLInterpolationSpec extends FlatSpec with ShouldMatchers { case class Issue(id: Int, body: String, tags: Seq[Tag] = Vector()) object Issue extends SQLSyntaxSupport[Issue] { - override val tableName = "issues" - override val columns = Seq("id", "body") def apply(rs: WrappedResultSet, i: ResultName[Issue]): Issue = Issue( id = rs.int(i.id), body = rs.string(i.body) @@ -286,31 +284,26 @@ class SQLInterpolationSpec extends FlatSpec with ShouldMatchers { case class Tag(id: Int, name: String) object Tag extends SQLSyntaxSupport[Tag] { - override val tableName = "tags" - override val columns = Seq("id", "name") def apply(rs: WrappedResultSet, t: ResultName[Tag]): Tag = Tag( id = rs.int(t.id), name = rs.string(t.name) ) } - object IssueTag extends SQLSyntaxSupport[Nothing] { - override val tableName = "issue_tag" - override val columns = Seq("issue_id", "tag_id") - } + object IssueTag extends SQLSyntaxSupport[Nothing] it should "be available for empty relation" in { DB localTx { implicit s => try { - sql"create table issues (id int not null, body varchar(256) not null)".execute.apply() - sql"create table tags (id int not null, name varchar(256) not null)".execute.apply() + sql"create table issue (id int not null, body varchar(256) not null)".execute.apply() + sql"create table tag (id int not null, name varchar(256) not null)".execute.apply() sql"create table issue_tag (issue_id int not null, tag_id int not null)".execute.apply() - sql"insert into issues values (1, ${"Alice"})".update.apply() - sql"insert into issues values (2, ${"Bob"})".update.apply() - sql"insert into issues values (3, ${"Chris"})".update.apply() - sql"insert into issues values (4, ${"Dennis"})".update.apply() + sql"insert into issue values (1, ${"Alice"})".update.apply() + sql"insert into issue values (2, ${"Bob"})".update.apply() + sql"insert into issue values (3, ${"Chris"})".update.apply() + sql"insert into issue values (4, ${"Dennis"})".update.apply() { val (i, it, t) = (Issue.syntax("i"), IssueTag.syntax("it"), Tag.syntax("t")) @@ -355,8 +348,8 @@ class SQLInterpolationSpec extends FlatSpec with ShouldMatchers { } } finally { - sql"drop table issues".execute.apply() - sql"drop table tags".execute.apply() + sql"drop table issue".execute.apply() + sql"drop table tag".execute.apply() sql"drop table issue_tag".execute.apply() } }