Postgresql scalike jdbc-如何在查询中指定列别名?
我定义了一个对象来帮助将结果集转换为POJOPostgresql scalike jdbc-如何在查询中指定列别名?,postgresql,scala,jdbc,scalikejdbc,Postgresql,Scala,Jdbc,Scalikejdbc,我定义了一个对象来帮助将结果集转换为POJO // an object, that help to map from query result to POJO, object Dummy extends SQLSyntaxSupport[Dummy] { override val tableName = "dummy" def apply(rs: WrappedResultSet) = new Dummy( rs.long("id"), rs.stringOp
// an object, that help to map from query result to POJO,
object Dummy extends SQLSyntaxSupport[Dummy] {
override val tableName = "dummy"
def apply(rs: WrappedResultSet) = new Dummy(
rs.long("id"), rs.stringOpt("name"), rs.intOpt("size"), rs.jodaDateTime("create_date"))
}
然后下面的代码尝试从表中进行查询
/*** query - with condition - start ***/
// use paste mode (:paste) on the Scala REPL
val d = Dummy.syntax("d")
val name = "hello"
val helloDummyOpt: Option[Dummy] = withSQL {
select.from(Dummy as d).where.eq(d.name, name).limit(1)
}.map(rs => Dummy(rs)).single.apply()
printf("hello dummy:\t%s\n", if(helloDummyOpt.isDefined) helloDummyOpt.get else "")
/*** query - with condition - end ***/
从控制台中,我可以看到生成的sql是:
select d.id as i_on_d, d.name as n_on_d, d.size as s_on_d, d.create_date as cd_on_d
from dummy d where d.name = 'hello' limit 1;
为了使转换工作,需要将生成的sql中的列别名更改为表中的原始名称,例如name
,而不是n\u on\u d
那么,如何做到这一点,谢谢
@更新 完整代码如下:
package eric.scalikejdbc
import scalikejdbc._
import org.postgresql.Driver._
import org.joda.time._
/**
* scalikejdbc hello.
* @author eric
* @date Jul 6, 2016 23:14:07 PM
*/
object ScalikeJdbcHello extends App {
Class.forName("org.postgresql.Driver")
ConnectionPool.singleton("jdbc:postgresql://localhost:5432/sportslight", "postgres", "123456")
implicit val session = AutoSession
/*** query - parse as List of Map - start ***/
// find rows, with limit 10000,
val entities: List[Map[String, Any]] = sql"select * from dummy limit 10000".map(_.toMap).list.apply()
printf("dummy count: %d\n", entities.size)
/*** query - parse as List of Map - end ***/
/*** query - parse as POJO - start ***/
// define a POJO class,
case class Dummy(id: Long, name: Option[String], size: Option[Int], createDate: DateTime)
// an object, that help to map from query result to POJO,
object Dummy extends SQLSyntaxSupport[Dummy] {
override val tableName = "dummy"
def apply(rs: WrappedResultSet) = new Dummy(
rs.long("id"), rs.stringOpt("name"), rs.intOpt("size"), rs.jodaDateTime("create_date"))
}
// find top 10 rows,
val dummyList: List[Dummy] = sql"select * from dummy limit 10".map(rs => Dummy(rs)).list.apply()
var i = 0;
printf("dummy top rows:\n")
for (d <- dummyList) {
printf("\t[%d] %s\n", i, d)
i = i + 1
}
/*** query - parse as POJO - end ***/
/*** query - with condition - start ***/
// TODO ... specify column name in result,
val d = Dummy.syntax("d")
val name = "hello"
val helloDummyOpt: Option[Dummy] = withSQL {
select.from(Dummy as d).where.eq(d.name, name).limit(1)
}.map(rs => Dummy(rs)).single.apply()
printf("hello dummy:\t%s\n", if(helloDummyOpt.isDefined) helloDummyOpt.get else "")
/*** query - with condition - end ***/
}
您使用的是半类型安全DSL,半默认API。你应该选择一个,然后继续 对于类型安全DSL,您不应使用自定义
SqlSyntaxSupport
实现
您应该能够使用sql查询中定义的d Dummy
对象来操作,而不是.map(rs=>Dummy(rs))
val d = Dummy.syntax("d")
val helloDummyOpt: Option[Dummy] = withSQL {
select.from(Dummy as d).where.eq(d.name, name).limit(1)
}.single.apply()
有关更多示例,请查看TypeSafe DSL部分:
如果要接收这些自动生成的名称,应通过对象d引用它们:
val d = Dummy.syntax("d")
d.id // = i_on_d
d.name // = n_on_d
d.create_date // = cd_on_d
您使用的是半类型安全DSL,半默认API。你应该选择一个,然后继续 对于类型安全DSL,您不应使用自定义
SqlSyntaxSupport
实现
您应该能够使用sql查询中定义的d Dummy
对象来操作,而不是.map(rs=>Dummy(rs))
val d = Dummy.syntax("d")
val helloDummyOpt: Option[Dummy] = withSQL {
select.from(Dummy as d).where.eq(d.name, name).limit(1)
}.single.apply()
有关更多示例,请查看TypeSafe DSL部分:
如果要接收这些自动生成的名称,应通过对象d引用它们:
val d = Dummy.syntax("d")
d.id // = i_on_d
d.name // = n_on_d
d.create_date // = cd_on_d
select.from
调用alias.result.*
。调用alias.*
只需按原样返回列名
scala> select(a.*).from(Article as a).toSQL.statement
res7: String = select a.id, a.title, a.body, a.created_at, a.updated_at from articles a
scala> select(a.result.*).from(Article as a).toSQL.statement
res8: String = select a.id as i_on_a, a.title as t_on_a, a.body as b_on_a, a.created_at as ca_on_a, a.updated_at as ua_on_a from articles a
scala> select.from(Article as a).toSQL.statement
res9: String = select a.id as i_on_a, a.title as t_on_a, a.body as b_on_a, a.created_at as ca_on_a, a.updated_at as ua_on_a from articles a
select.from
调用alias.result.*
。调用alias.*
只需按原样返回列名
scala> select(a.*).from(Article as a).toSQL.statement
res7: String = select a.id, a.title, a.body, a.created_at, a.updated_at from articles a
scala> select(a.result.*).from(Article as a).toSQL.statement
res8: String = select a.id as i_on_a, a.title as t_on_a, a.body as b_on_a, a.created_at as ca_on_a, a.updated_at as ua_on_a from articles a
scala> select.from(Article as a).toSQL.statement
res9: String = select a.id as i_on_a, a.title as t_on_a, a.body as b_on_a, a.created_at as ca_on_a, a.updated_at as ua_on_a from articles a
你为什么担心别名?当您使用虚拟对象时,您不应该看到在代码中使用它们。@Rumoku我更新了问题,并提供了更多信息。您为什么担心别名?当您使用虚拟对象时,您不应该看到在代码中使用它们。@Rumoku我更新了问题,并提供了更多信息。谢谢,但实际上代码是模仿第一个示例部分的。我试过你的代码,它说
错误:(51,17)没有指定提取器。您忘记了在#apply()之前调用#map(…)。single.apply()
。我已经在问题中添加了完整的代码,您能否在此基础上进行修改,使其可运行?谢谢,但实际上代码是模仿第一个示例的部分。我试过你的代码,它说错误:(51,17)没有指定提取器。您忘记了在#apply()之前调用#map(…)。single.apply()
。我已经为问题添加了完整的代码,您能否在此基础上进行修改以使其可运行?第一个示例修复了代码,如下所示:val helloDummyOpt:Option[Dummy]=withSQL{select(d.*).from(Dummy为d).where.eq(d.name,name).limit(1)}.map(rs=>Dummy(rs)).single.apply()
,但是可能是http://scalikejdbc.org/
第一个示例最后一个查询以val alice:
开始,也应该以类似的方式进行调整。第一个示例修复了代码,如下所示:val helloDummyOpt:Option[Dummy]=withSQL{select(d.*).from(Dummy为d).where.eq(d.name,name).limit(1)}.map(rs=>Dummy(rs)).single.apply()
,但可能是http://scalikejdbc.org/
第一个示例以val alice:
开头的最终查询也应以类似方式进行调整。