Skip to content

Commit

Permalink
use Fragment.const0 for doobie to avoid extra whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed May 18, 2024
1 parent db86337 commit 742a7b1
Show file tree
Hide file tree
Showing 81 changed files with 515 additions and 521 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class PersonRepoImpl extends PersonRepo {
}
override def insert(unsaved: PersonRowUnsaved): ConnectionIO[PersonRow] = {
val fs = List(
Some((Fragment.const(s""""name""""), fr"${fromWrite(unsaved.name)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""name""""), fr"${fromWrite(unsaved.name)(Write.fromPutOption(Meta.StringMeta.put))}")),
unsaved.one match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""one""""), fr"${fromWrite(value: Long)(Write.fromPut(Meta.LongMeta.put))}::int8"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""one""""), fr"${fromWrite(value: Long)(Write.fromPut(Meta.LongMeta.put))}::int8"))
},
unsaved.two match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""two""""), fr"${fromWrite(value: Option[String])(Write.fromPutOption(Meta.StringMeta.put))}"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""two""""), fr"${fromWrite(value: Option[String])(Write.fromPutOption(Meta.StringMeta.put))}"))
}
).flatten

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,29 @@ class PersonRepoImpl extends PersonRepo {
}
override def insert(unsaved: PersonRowUnsaved): ConnectionIO[PersonRow] = {
val fs = List(
Some((Fragment.const(s""""favourite_football_club_id""""), fr"${fromWrite(unsaved.favouriteFootballClubId)(Write.fromPut(FootballClubId.put))}")),
Some((Fragment.const(s""""name""""), fr"${fromWrite(unsaved.name)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""nick_name""""), fr"${fromWrite(unsaved.nickName)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""blog_url""""), fr"${fromWrite(unsaved.blogUrl)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""email""""), fr"${fromWrite(unsaved.email)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""phone""""), fr"${fromWrite(unsaved.phone)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""likes_pizza""""), fr"${fromWrite(unsaved.likesPizza)(Write.fromPut(Meta.BooleanMeta.put))}")),
Some((Fragment.const(s""""work_email""""), fr"${fromWrite(unsaved.workEmail)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""favourite_football_club_id""""), fr"${fromWrite(unsaved.favouriteFootballClubId)(Write.fromPut(FootballClubId.put))}")),
Some((Fragment.const0(s""""name""""), fr"${fromWrite(unsaved.name)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""nick_name""""), fr"${fromWrite(unsaved.nickName)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""blog_url""""), fr"${fromWrite(unsaved.blogUrl)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""email""""), fr"${fromWrite(unsaved.email)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""phone""""), fr"${fromWrite(unsaved.phone)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""likes_pizza""""), fr"${fromWrite(unsaved.likesPizza)(Write.fromPut(Meta.BooleanMeta.put))}")),
Some((Fragment.const0(s""""work_email""""), fr"${fromWrite(unsaved.workEmail)(Write.fromPutOption(Meta.StringMeta.put))}")),
unsaved.id match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""id""""), fr"${fromWrite(value: PersonId)(Write.fromPut(PersonId.put))}::int8"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""id""""), fr"${fromWrite(value: PersonId)(Write.fromPut(PersonId.put))}::int8"))
},
unsaved.maritalStatusId match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""marital_status_id""""), fr"${fromWrite(value: MaritalStatusId)(Write.fromPut(MaritalStatusId.put))}"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""marital_status_id""""), fr"${fromWrite(value: MaritalStatusId)(Write.fromPut(MaritalStatusId.put))}"))
},
unsaved.sector match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""sector""""), fr"${fromWrite(value: Sector)(Write.fromPut(Sector.put))}::myschema.sector"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""sector""""), fr"${fromWrite(value: Sector)(Write.fromPut(Sector.put))}::myschema.sector"))
},
unsaved.favoriteNumber match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""favorite_number""""), fr"${fromWrite(value: Number)(Write.fromPut(Number.put))}::myschema.number"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""favorite_number""""), fr"${fromWrite(value: Number)(Write.fromPut(Number.put))}::myschema.number"))
}
).flatten

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class PersonRepoImpl extends PersonRepo {
}
override def insert(unsaved: PersonRowUnsaved): ConnectionIO[PersonRow] = {
val fs = List(
Some((Fragment.const(s""""name""""), fr"${fromWrite(unsaved.name)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""name""""), fr"${fromWrite(unsaved.name)(Write.fromPutOption(Meta.StringMeta.put))}")),
unsaved.one match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""one""""), fr"${fromWrite(value: Long)(Write.fromPut(Meta.LongMeta.put))}::int8"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""one""""), fr"${fromWrite(value: Long)(Write.fromPut(Meta.LongMeta.put))}::int8"))
},
unsaved.two match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""two""""), fr"${fromWrite(value: Option[String])(Write.fromPutOption(Meta.StringMeta.put))}"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""two""""), fr"${fromWrite(value: Option[String])(Write.fromPutOption(Meta.StringMeta.put))}"))
}
).flatten

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,29 @@ class PersonRepoImpl extends PersonRepo {
}
override def insert(unsaved: PersonRowUnsaved): ConnectionIO[PersonRow] = {
val fs = List(
Some((Fragment.const(s""""favourite_football_club_id""""), fr"${fromWrite(unsaved.favouriteFootballClubId)(Write.fromPut(FootballClubId.put))}")),
Some((Fragment.const(s""""name""""), fr"${fromWrite(unsaved.name)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""nick_name""""), fr"${fromWrite(unsaved.nickName)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""blog_url""""), fr"${fromWrite(unsaved.blogUrl)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""email""""), fr"${fromWrite(unsaved.email)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""phone""""), fr"${fromWrite(unsaved.phone)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const(s""""likes_pizza""""), fr"${fromWrite(unsaved.likesPizza)(Write.fromPut(Meta.BooleanMeta.put))}")),
Some((Fragment.const(s""""work_email""""), fr"${fromWrite(unsaved.workEmail)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""favourite_football_club_id""""), fr"${fromWrite(unsaved.favouriteFootballClubId)(Write.fromPut(FootballClubId.put))}")),
Some((Fragment.const0(s""""name""""), fr"${fromWrite(unsaved.name)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""nick_name""""), fr"${fromWrite(unsaved.nickName)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""blog_url""""), fr"${fromWrite(unsaved.blogUrl)(Write.fromPutOption(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""email""""), fr"${fromWrite(unsaved.email)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""phone""""), fr"${fromWrite(unsaved.phone)(Write.fromPut(Meta.StringMeta.put))}")),
Some((Fragment.const0(s""""likes_pizza""""), fr"${fromWrite(unsaved.likesPizza)(Write.fromPut(Meta.BooleanMeta.put))}")),
Some((Fragment.const0(s""""work_email""""), fr"${fromWrite(unsaved.workEmail)(Write.fromPutOption(Meta.StringMeta.put))}")),
unsaved.id match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""id""""), fr"${fromWrite(value: PersonId)(Write.fromPut(PersonId.put))}::int8"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""id""""), fr"${fromWrite(value: PersonId)(Write.fromPut(PersonId.put))}::int8"))
},
unsaved.maritalStatusId match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""marital_status_id""""), fr"${fromWrite(value: MaritalStatusId)(Write.fromPut(MaritalStatusId.put))}"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""marital_status_id""""), fr"${fromWrite(value: MaritalStatusId)(Write.fromPut(MaritalStatusId.put))}"))
},
unsaved.sector match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""sector""""), fr"${fromWrite(value: Sector)(Write.fromPut(Sector.put))}::myschema.sector"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""sector""""), fr"${fromWrite(value: Sector)(Write.fromPut(Sector.put))}::myschema.sector"))
},
unsaved.favoriteNumber match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""favorite_number""""), fr"${fromWrite(value: Number)(Write.fromPut(Number.put))}::myschema.number"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""favorite_number""""), fr"${fromWrite(value: Number)(Write.fromPut(Number.put))}::myschema.number"))
}
).flatten

Expand Down
10 changes: 5 additions & 5 deletions site/blog/emailaddress/EmailaddressRepoImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ class EmailaddressRepoImpl extends EmailaddressRepo {
}
override def insert(unsaved: EmailaddressRowUnsaved): ConnectionIO[EmailaddressRow] = {
val fs = List(
Some((Fragment.const(s""""businessentityid""""), fr"${unsaved.businessentityid}::int4")),
Some((Fragment.const(s""""emailaddress""""), fr"${unsaved.emailaddress}")),
Some((Fragment.const0(s""""businessentityid""""), fr"${unsaved.businessentityid}::int4")),
Some((Fragment.const0(s""""emailaddress""""), fr"${unsaved.emailaddress}")),
unsaved.emailaddressid match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""emailaddressid""""), fr"${value: Int}::int4"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""emailaddressid""""), fr"${value: Int}::int4"))
},
unsaved.rowguid match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""rowguid""""), fr"${value: TypoUUID}::uuid"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""rowguid""""), fr"${value: TypoUUID}::uuid"))
},
unsaved.modifieddate match {
case Defaulted.UseDefault => None
case Defaulted.Provided(value) => Some((Fragment.const(s""""modifieddate""""), fr"${value: TypoLocalDateTime}::timestamp"))
case Defaulted.Provided(value) => Some((Fragment.const0(s""""modifieddate""""), fr"${value: TypoLocalDateTime}::timestamp"))
}
).flatten

Expand Down
2 changes: 1 addition & 1 deletion typo-dsl-doobie/src/scala/typo/dsl/DeleteBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object DeleteBuilder {

def mkSql(counter: AtomicInteger): Fragment = {
List[Iterable[Fragment]](
Some(fr"delete from ${Fragment.const(name)}"),
Some(fr"delete from ${Fragment.const0(name)}"),
params.where
.map(w => w(structure.fields))
.reduceLeftOption(_.and(_))
Expand Down
18 changes: 9 additions & 9 deletions typo-dsl-doobie/src/scala/typo/dsl/SelectBuilderSql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ object SelectBuilderSql {

private def sql(counter: AtomicInteger): Fragment = {
val cols = structure.columns
.map(x => Fragment.const(x.sqlReadCast.foldLeft("\"" + x.value + "\"") { case (acc, cast) => s"$acc::$cast" }))
.intercalate(Fragment.const(", "))
val baseSql = fr"select $cols from ${Fragment.const(name)}"
.map(x => Fragment.const0(x.sqlReadCast.foldLeft("\"" + x.value + "\"") { case (acc, cast) => s"$acc::$cast" }))
.intercalate(Fragment.const0(", "))
val baseSql = fr"select $cols from ${Fragment.const0(name)}"
SelectParams.render(structure.fields, baseSql, counter, params)
}

Expand Down Expand Up @@ -108,16 +108,16 @@ object SelectBuilderSql {
case NonEmptyList(one, Nil) => one.sqlFrag
case NonEmptyList(first, rest) =>
val prelude =
fr"""|select ${instance.columns.map(c => Fragment.const(c.value)).intercalate(Fragment.const(", "))}
fr"""|select ${instance.columns.map(c => Fragment.const0(c.value)).intercalate(Fragment.const0(", "))}
|from (
|${first.sqlFrag}
|) ${Fragment.const(first.alias)}
|) ${Fragment.const0(first.alias)}
|""".stripMargin

val joins = rest.map { case SelectBuilderSql.InstantiatedPart(alias, _, sqlFrag, joinFrag) =>
fr"""|join (
|$sqlFrag
|) ${Fragment.const(alias)} on $joinFrag
|) ${Fragment.const0(alias)} on $joinFrag
|""".stripMargin
}
prelude ++ joins.reduce(_ ++ _)
Expand Down Expand Up @@ -167,16 +167,16 @@ object SelectBuilderSql {
case NonEmptyList(one, Nil) => one.sqlFrag
case NonEmptyList(first, rest) =>
val prelude =
fr"""|select ${fragments.comma(instance.columns.map(c => Fragment.const(c.value)))}
fr"""|select ${fragments.comma(instance.columns.map(c => Fragment.const0(c.value)))}
|from (
| ${first.sqlFrag}
|) ${Fragment.const(first.alias)}
|) ${Fragment.const0(first.alias)}
|""".stripMargin

val joins = rest.map { case SelectBuilderSql.InstantiatedPart(alias, _, sqlFrag, joinFrag) =>
fr"""|left join (
|${sqlFrag}
|) ${Fragment.const(alias)} on $joinFrag
|) ${Fragment.const0(alias)} on $joinFrag
|""".stripMargin
}
prelude ++ joins.reduce(_ ++ _)
Expand Down
12 changes: 6 additions & 6 deletions typo-dsl-doobie/src/scala/typo/dsl/SqlExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ object SqlExpr {
) extends SqlExpr[T, N, R]
with FieldLikeNoHkt[N[T], R] {
override def eval(row: R): N[T] = get(row)
override def render(counter: AtomicInteger): Fragment = Fragment.const(value)
override def render(counter: AtomicInteger): Fragment = Fragment.const0(value)
}

sealed trait FieldLikeNotId[T, N[_], R] extends FieldLike[T, N, R] with FieldLikeNotIdNoHkt[N[T], R]
Expand All @@ -146,7 +146,7 @@ object SqlExpr {
override def render(counter: AtomicInteger): Fragment = {
val cast = P match {
case _: Put.Basic[_] => Fragment.empty
case p: Put.Advanced[_] => Fragment.const(s"::${p.schemaTypes.head}")
case p: Put.Advanced[_] => Fragment.const0(s"::${p.schemaTypes.head}")
}
fr"${W.toFragment(value)}$cast"
}
Expand All @@ -168,14 +168,14 @@ object SqlExpr {
override def eval(row: R): N[O] =
N.mapN(arg1.eval(row))(f.eval)
override def render(counter: AtomicInteger): Fragment =
fr"${Fragment.const(f.name)}(${arg1.render(counter)})"
fr"${Fragment.const0(f.name)}(${arg1.render(counter)})"
}

case class Apply2[T1, T2, O, N1[_], N2[_], N[_], R](f: SqlFunction2[T1, T2, O], arg1: SqlExpr[T1, N1, R], arg2: SqlExpr[T2, N2, R], N: Nullability2[N1, N2, N]) extends SqlExpr[O, N, R] {
override def eval(row: R): N[O] =
N.mapN(arg1.eval(row), arg2.eval(row))(f.eval)
override def render(counter: AtomicInteger): Fragment =
fr"${Fragment.const(f.name)}(${arg1.render(counter)}, ${arg2.render(counter)})"
fr"${Fragment.const0(f.name)}(${arg1.render(counter)}, ${arg2.render(counter)})"
}

case class Apply3[T1, T2, T3, N1[_], N2[_], N3[_], N[_], O, R](
Expand All @@ -188,14 +188,14 @@ object SqlExpr {
override def eval(row: R): N[O] =
N.mapN(arg1.eval(row), arg2.eval(row), arg3.eval(row))(f.eval)
override def render(counter: AtomicInteger): Fragment =
fr"${Fragment.const(f.name)}(${arg1.render(counter)}, ${arg2.render(counter)}, ${arg3.render(counter)})"
fr"${Fragment.const0(f.name)}(${arg1.render(counter)}, ${arg2.render(counter)}, ${arg3.render(counter)})"
}

case class Binary[T1, T2, O, N1[_], N2[_], N[_], R](left: SqlExpr[T1, N1, R], op: SqlOperator[T1, T2, O], right: SqlExpr[T2, N2, R], N: Nullability2[N1, N2, N]) extends SqlExpr[O, N, R] {
override def eval(row: R): N[O] =
N.mapN(left.eval(row), right.eval(row))(op.eval)
override def render(counter: AtomicInteger): Fragment =
fr"(${left.render(counter)} ${Fragment.const(op.name)} ${right.render(counter)})"
fr"(${left.render(counter)} ${Fragment.const0(op.name)} ${right.render(counter)})"
}

case class Underlying[T, TT, N[_], R](expr: SqlExpr[T, N, R], bijection: Bijection[T, TT], N: Nullability[N]) extends SqlExpr[TT, N, R] {
Expand Down
6 changes: 3 additions & 3 deletions typo-dsl-doobie/src/scala/typo/dsl/UpdateBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ object UpdateBuilder {

def mkSql(counter: AtomicInteger, returning: Boolean): Fragment = {
List[Option[Fragment]](
Some(fr"update ${Fragment.const(name)}"),
Some(fr"update ${Fragment.const0(name)}"),
NonEmptyList.fromList(params.setters) match {
case None =>
sys.error("you must specify a columns to set. use `set` method")
case Some(setters) =>
val setFragments = setters.map { setter =>
val fieldExpr = setter.col(structure.fields)
val valueExpr = setter.value(structure.fields)
fr"${fieldExpr.render(counter)} = ${valueExpr.render(counter)}${fieldExpr.sqlWriteCast.fold(Fragment.empty)(cast => Fragment.const(s"::$cast"))}"
fr"${fieldExpr.render(counter)} = ${valueExpr.render(counter)}${fieldExpr.sqlWriteCast.fold(Fragment.empty)(cast => Fragment.const0(s"::$cast"))}"
}
Some(fragments.set(setFragments))
},
Expand All @@ -71,7 +71,7 @@ object UpdateBuilder {
if (returning) {
val colFragments = fragments.comma(
NonEmptyList.fromListUnsafe(structure.columns).map { col =>
Fragment.const(col.sqlReadCast.foldLeft("\"" + col.value + "\"") { case (acc, cast) => s"$acc::$cast" })
Fragment.const0(col.sqlReadCast.foldLeft("\"" + col.value + "\"") { case (acc, cast) => s"$acc::$cast" })
}
)
Some(fr"returning $colFragments")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import org.scalatest.funsuite.AnyFunSuite
class SeekTest extends AnyFunSuite with TypeCheckedTripleEquals {
val productRepo = new ProductRepoImpl

val base =
"""select "productid", "name", "productnumber", "makeflag", "finishedgoodsflag", "color", "safetystocklevel", "reorderpoint", "standardcost", "listprice", "size", "sizeunitmeasurecode", "weightunitmeasurecode", "weight", "daystomanufacture", "productline", "class", "style", "productsubcategoryid", "productmodelid", "sellstartdate"::text, "sellenddate"::text, "discontinueddate"::text, "rowguid", "modifieddate"::text from production.product"""

test("uniform ascending") {
val query = productRepo.select
.seek(_.name.asc)(Name("foo"))
Expand Down
Loading

0 comments on commit 742a7b1

Please sign in to comment.