Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Dec 10, 2023
1 parent d167ad7 commit bc15f5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
48 changes: 25 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,31 @@ val dbClient = new scalasql.DbClient.Connection(
override def logSql(sql: String, file: String, line: Int) = println(s"$file:$line $sql")
}
)
val db: DbApi = dbClient.getAutoCommitClientConnection

// Initialize database table schema and data
db.updateRaw(os.read(os.Path("scalasql/test/resources/world-schema.sql", os.pwd)))
db.updateRaw(os.read(os.Path("scalasql/test/resources/world-data.sql", os.pwd)))

// Adding up population of all cities in China
val populationSum = db.run(City.select.filter(_.countryCode === "CHN").map(_.population).sum)
// SELECT SUM(city0.population) AS res FROM city city0 WHERE city0.countrycode = ?
println(populationSum)
// 175953614

// Finding the 5-8th largest cities by population
val fewLargestCities = db.run(
City.select
.sortBy(_.population).desc
.drop(5).take(3)
.map(c => (c.name, c.population))
)
// SELECT city0.name AS res__0, city0.population AS res__1
// FROM city city0 ORDER BY res__1 DESC LIMIT ? OFFSET ?
println(fewLargestCities)
// Seq((Karachi, 9269265), (Istanbul, 8787958), (Ciudad de México, 8591309))

dbClient.transaction{ db =>

// Initialize database table schema and data
db.updateRaw(os.read(os.Path("scalasql/test/resources/world-schema.sql", os.pwd)))
db.updateRaw(os.read(os.Path("scalasql/test/resources/world-data.sql", os.pwd)))

// Adding up population of all cities in China
val populationSum = db.run(City.select.filter(_.countryCode === "CHN").map(_.population).sum)
// SELECT SUM(city0.population) AS res FROM city city0 WHERE city0.countrycode = ?
println(populationSum)
// 175953614

// Finding the 5-8th largest cities by population
val fewLargestCities = db.run(
City.select
.sortBy(_.population).desc
.drop(5).take(3)
.map(c => (c.name, c.population))
)
// SELECT city0.name AS res__0, city0.population AS res__1
// FROM city city0 ORDER BY res__1 DESC LIMIT ? OFFSET ?
println(fewLargestCities)
// Seq((Karachi, 9269265), (Istanbul, 8787958), (Ciudad de México, 8591309))
}
```

ScalaSql supports PostgreSQL, MySQL, Sqlite, and H2 databases. Support for additional
Expand Down
9 changes: 5 additions & 4 deletions scalasql/core/src/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import scalasql.core.SqlStr.SqlStringSyntax
* into a SQL string
*/
trait Context {

/**
* Any [[From]]/`FROM` clauses that are in scope, and the aliases those clauses are given
*/
Expand Down Expand Up @@ -77,15 +78,15 @@ object Context {
case (r, i) if !prevContext.fromNaming.contains(r) =>
(r, r.fromRefPrefix(prevContext) + (i + prevSize))
} ++
unPrefixedFroms.iterator.collect{case t if !prevContext.fromNaming.contains(t) =>
t -> t.fromRefPrefix(prevContext)
unPrefixedFroms.iterator.collect {
case t if !prevContext.fromNaming.contains(t) =>
t -> t.fromRefPrefix(prevContext)
}
)

val newExprNaming =
prevContext.exprNaming ++
prefixedFroms
.iterator
prefixedFroms.iterator
.flatMap { t =>
t
.fromExprAliases(prevContext)
Expand Down

0 comments on commit bc15f5d

Please sign in to comment.