From 591286e2709ad0530a3d3e1faf1f022d721bba78 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sun, 10 Dec 2023 17:56:46 -0800 Subject: [PATCH] tweak --- scalasql/query/src/CompoundSelect.scala | 8 ++++---- scalasql/query/src/From.scala | 2 +- scalasql/query/src/OnConflict.scala | 6 ++---- scalasql/query/src/Query.scala | 13 ++++++------- scalasql/query/src/Select.scala | 4 ++-- scalasql/query/src/SelectBase.scala | 4 ++-- scalasql/query/src/SimpleSelect.scala | 2 +- scalasql/query/src/Values.scala | 2 +- scalasql/query/src/WithCte.scala | 4 ++-- scalasql/src/dialects/MySqlDialect.scala | 2 +- 10 files changed, 22 insertions(+), 25 deletions(-) diff --git a/scalasql/query/src/CompoundSelect.scala b/scalasql/query/src/CompoundSelect.scala index 71a725f4..567bc3d6 100644 --- a/scalasql/query/src/CompoundSelect.scala +++ b/scalasql/query/src/CompoundSelect.scala @@ -89,8 +89,8 @@ class CompoundSelect[Q, R]( override protected def selectRenderer(prevContext: Context) = new CompoundSelect.Renderer(this, prevContext) - override protected def selectLhsMap(prevContext: Context): Map[Expr.Identity, SqlStr] = { - SelectBase.lhsMap(lhs, prevContext) + override protected def selectColumnExprs(prevContext: Context): Map[Expr.Identity, SqlStr] = { + SelectBase.columnExprs(lhs, prevContext) } } @@ -102,7 +102,7 @@ object CompoundSelect { import query.dialect._ lazy val lhsToSqlQuery = SimpleSelect.getRenderer(query.lhs, prevContext) - lazy val lhsLhsMap = SelectBase.lhsMap(query.lhs, prevContext) + lazy val lhsLhsMap = SelectBase.columnExprs(query.lhs, prevContext) lazy val context = lhsToSqlQuery.context .withExprNaming(lhsToSqlQuery.context.exprNaming ++ lhsLhsMap) @@ -130,7 +130,7 @@ object CompoundSelect { val compound = SqlStr.optSeq(query.compoundOps) { compoundOps => val compoundStrs = compoundOps.map { op => val rhsToSqlQuery = SimpleSelect.getRenderer(op.rhs, prevContext) - lazy val rhsLhsMap = SelectBase.lhsMap(op.rhs, prevContext) + lazy val rhsLhsMap = SelectBase.columnExprs(op.rhs, prevContext) // We match up the RHS SimpleSelect's lhsMap with the LHS SimpleSelect's lhsMap, // because the expressions in the CompoundSelect's lhsMap correspond to those // belonging to the LHS SimpleSelect, but we need the corresponding expressions diff --git a/scalasql/query/src/From.scala b/scalasql/query/src/From.scala index 0d7bab97..4aae64ab 100644 --- a/scalasql/query/src/From.scala +++ b/scalasql/query/src/From.scala @@ -20,7 +20,7 @@ class TableRef(val value: Table.Base) extends From { class SubqueryRef(val value: SelectBase, val qr: Queryable[_, _]) extends From { def fromRefPrefix(prevContext: Context): String = "subquery" - def fromExprAliases(prevContext: Context) = SelectBase.lhsMap(value, prevContext) + def fromExprAliases(prevContext: Context) = SelectBase.columnExprs(value, prevContext) def renderSql(name: SqlStr, prevContext: Context, liveExprs: LiveSqlExprs) = { val renderSql = SelectBase.renderer(value, prevContext) diff --git a/scalasql/query/src/OnConflict.scala b/scalasql/query/src/OnConflict.scala index 70275bd5..21186167 100644 --- a/scalasql/query/src/OnConflict.scala +++ b/scalasql/query/src/OnConflict.scala @@ -15,13 +15,12 @@ class OnConflict[Q, R](query: Query[R] with InsertReturnable[Q], expr: Q, table: object OnConflict { class Ignore[Q, R]( - query: Query[R] with InsertReturnable[Q], + protected val query: Query[R] with InsertReturnable[Q], columns: Seq[Column[_]], val table: TableRef ) extends Query.DelegateQuery[R] with InsertReturnable[Q] { protected def expr = WithSqlExpr.get(query) - protected def queryDelegate = query protected def renderSql(ctx: Context) = { val str = Renderable.renderSql(query)(ctx) str + sql" ON CONFLICT (${SqlStr.join(columns.map(c => SqlStr.raw(c.name)), SqlStr.commaSep)}) DO NOTHING" @@ -34,14 +33,13 @@ object OnConflict { } class Update[Q, R]( - query: Query[R] with InsertReturnable[Q], + protected val query: Query[R] with InsertReturnable[Q], columns: Seq[Column[_]], updates: Seq[Column.Assignment[_]], val table: TableRef ) extends Query.DelegateQuery[R] with InsertReturnable[Q] { protected def expr = WithSqlExpr.get(query) - protected def queryDelegate = query protected def renderSql(ctx: Context) = { implicit val implicitCtx = Context.compute(ctx, Nil, Some(table)) val str = Renderable.renderSql(query) diff --git a/scalasql/query/src/Query.scala b/scalasql/query/src/Query.scala index fb104710..2427b662 100644 --- a/scalasql/query/src/Query.scala +++ b/scalasql/query/src/Query.scala @@ -33,11 +33,11 @@ object Query { * most of the abstract methods to it */ trait DelegateQuery[R] extends scalasql.query.Query[R] { - protected def queryDelegate: Query[_] - protected def queryWalkLabels() = queryDelegate.queryWalkLabels() - protected def queryWalkExprs() = queryDelegate.queryWalkExprs() - protected override def queryIsSingleRow = queryDelegate.queryIsSingleRow - protected override def queryIsExecuteUpdate = queryDelegate.queryIsExecuteUpdate + protected def query: Query[_] + protected def queryWalkLabels() = query.queryWalkLabels() + protected def queryWalkExprs() = query.queryWalkExprs() + protected override def queryIsSingleRow = query.queryIsSingleRow + protected override def queryIsExecuteUpdate = query.queryIsExecuteUpdate } /** @@ -77,8 +77,7 @@ object Query { /** * A [[Query]] that wraps another [[Query]] but sets [[queryIsSingleRow]] to `true` */ - class Single[R](query: Query[Seq[R]]) extends Query.DelegateQuery[R] { - protected def queryDelegate = query + class Single[R](protected val query: Query[Seq[R]]) extends Query.DelegateQuery[R] { protected override def queryIsSingleRow: Boolean = true protected def renderSql(ctx: Context): SqlStr = Renderable.renderSql(query)(ctx) diff --git a/scalasql/query/src/Select.scala b/scalasql/query/src/Select.scala index f82cb2b4..c2c65bc8 100644 --- a/scalasql/query/src/Select.scala +++ b/scalasql/query/src/Select.scala @@ -340,8 +340,8 @@ object Select { override protected def selectRenderer(prevContext: Context): SelectBase.Renderer = SelectBase.renderer(selectToSimpleSelect(), prevContext) - override protected def selectLhsMap(prevContext: Context): Map[Expr.Identity, SqlStr] = - SelectBase.lhsMap(selectToSimpleSelect(), prevContext) + override protected def selectColumnExprs(prevContext: Context): Map[Expr.Identity, SqlStr] = + SelectBase.columnExprs(selectToSimpleSelect(), prevContext) override protected def selectToSimpleSelect(): SimpleSelect[Q, R] diff --git a/scalasql/query/src/SelectBase.scala b/scalasql/query/src/SelectBase.scala index 3d8da65f..9d2155ed 100644 --- a/scalasql/query/src/SelectBase.scala +++ b/scalasql/query/src/SelectBase.scala @@ -3,11 +3,11 @@ package scalasql.query import scalasql.core.{Context, LiveSqlExprs, Expr, SqlStr} trait SelectBase { - protected def selectLhsMap(prevContext: Context): Map[Expr.Identity, SqlStr] + protected def selectColumnExprs(prevContext: Context): Map[Expr.Identity, SqlStr] protected def selectRenderer(prevContext: Context): SelectBase.Renderer } object SelectBase { - def lhsMap(s: SelectBase, prevContext: Context) = s.selectLhsMap(prevContext) + def columnExprs(s: SelectBase, prevContext: Context) = s.selectColumnExprs(prevContext) def renderer(s: SelectBase, prevContext: Context) = s.selectRenderer(prevContext) trait Renderer { diff --git a/scalasql/query/src/SimpleSelect.scala b/scalasql/query/src/SimpleSelect.scala index 2a110026..1aa9ccb3 100644 --- a/scalasql/query/src/SimpleSelect.scala +++ b/scalasql/query/src/SimpleSelect.scala @@ -209,7 +209,7 @@ class SimpleSelect[Q, R]( protected def selectRenderer(prevContext: Context): SimpleSelect.Renderer[_, _] = new SimpleSelect.Renderer(this, prevContext) - protected def selectLhsMap(prevContext: Context): Map[Expr.Identity, SqlStr] = { + protected def selectColumnExprs(prevContext: Context): Map[Expr.Identity, SqlStr] = { lazy val flattenedExpr = qr.walkLabelsAndExprs(expr) diff --git a/scalasql/query/src/Values.scala b/scalasql/query/src/Values.scala index e950c515..9679e75c 100644 --- a/scalasql/query/src/Values.scala +++ b/scalasql/query/src/Values.scala @@ -31,7 +31,7 @@ class Values[Q, R](val ts: Seq[R])( override protected def selectRenderer(prevContext: Context): SelectBase.Renderer = new Values.Renderer(this)(implicitly, prevContext) - override protected def selectLhsMap(prevContext: Context): Map[Expr.Identity, SqlStr] = { + override protected def selectColumnExprs(prevContext: Context): Map[Expr.Identity, SqlStr] = { qr.walkExprs(expr) .zipWithIndex .map { case (e, i) => (Expr.identity(e), SqlStr.raw(columnName(i))) } diff --git a/scalasql/query/src/WithCte.scala b/scalasql/query/src/WithCte.scala index 410baabb..007ed56e 100644 --- a/scalasql/query/src/WithCte.scala +++ b/scalasql/query/src/WithCte.scala @@ -45,8 +45,8 @@ class WithCte[Q, R]( override protected def selectRenderer(prevContext: Context) = new WithCte.Renderer(withPrefix, this, prevContext) - override protected def selectLhsMap(prevContext: Context): Map[Expr.Identity, SqlStr] = { - SelectBase.lhsMap(rhs, prevContext) + override protected def selectColumnExprs(prevContext: Context): Map[Expr.Identity, SqlStr] = { + SelectBase.columnExprs(rhs, prevContext) } override protected def queryConstruct(args: Queryable.ResultSetIterator): Seq[R] = diff --git a/scalasql/src/dialects/MySqlDialect.scala b/scalasql/src/dialects/MySqlDialect.scala index 7262764e..832325b0 100644 --- a/scalasql/src/dialects/MySqlDialect.scala +++ b/scalasql/src/dialects/MySqlDialect.scala @@ -269,7 +269,7 @@ object MySqlDialect extends MySqlDialect { updates: Seq[Column.Assignment[_]], table: TableRef ) extends Query.DelegateQuery[R] { - protected def queryDelegate = insert.query + protected def query = insert.query override def queryIsExecuteUpdate = true protected def renderSql(ctx: Context) = {