Skip to content

Commit

Permalink
Merge pull request #668 from sushengloong/cleanup-examples
Browse files Browse the repository at this point in the history
Clean up examples
  • Loading branch information
jczuchnowski authored May 26, 2022
2 parents bf3b198 + b42f332 commit 4969605
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 82 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ lazy val examples = project
publish / skip := true,
moduleName := "examples"
)
.dependsOn(sqlserver)
.dependsOn(postgres)

lazy val driver = project
.in(file("driver"))
Expand Down
57 changes: 0 additions & 57 deletions examples/src/main/scala/Example1.scala

This file was deleted.

51 changes: 31 additions & 20 deletions examples/src/main/scala/zio/sql/Examples.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package zio.sql

import zio.sql.sqlserver.SqlServerJdbcModule
import zio.sql.postgresql.PostgresJdbcModule

object Examples extends App with ShopSchema with SqlServerJdbcModule {
object Examples extends App with ShopSchema with PostgresJdbcModule {
import this.AggregationDef._
import this.FunctionDef._
import this.OrderDetails._
import this.Orders._
import this.Users._

// select first_name, last_name from users
// SELECT "users"."first_name", "users"."last_name" FROM "users"
val basicSelect =
select(fName, lName).from(users)
println(renderRead(basicSelect))

// select first_name as first, last_name as last from users
val basicSelectWithAliases =
select((fName as "first"), (lName as "last")).from(users)
println(renderRead(basicSelectWithAliases))
// SELECT "users"."age" + 2, concat_ws("users"."first_name",' ',"users"."last_name"), abs(-42.0) FROM "users" ORDER BY "users"."age" DESC LIMIT 10 OFFSET 20
val selectWithFunctions =
select(age + 2, ConcatWs3(fName, " ", lName), Abs(-42.0))
.from(users)
.limit(10)
.offset(20)
.orderBy(age.descending)
println(renderRead(selectWithFunctions))

// select top 2 first_name, last_name from users order by last_name, first_name desc
// SELECT "users"."first_name", "users"."last_name" FROM "users" ORDER BY "users"."last_name", "users"."first_name" DESC LIMIT 2
val selectWithRefinements =
select(fName, lName)
.from(users)
Expand All @@ -32,7 +36,7 @@ object Examples extends App with ShopSchema with SqlServerJdbcModule {
// execute(selectWithRefinements).to(Person)
// execute(selectWithRefinements).to((_, _))

// delete from users where first_name = 'Terrence'
// DELETE FROM "users" WHERE "users"."first_name" = 'Terrence'
val basicDelete =
deleteFrom(users).where(fName === "Terrence")
println(renderDelete(basicDelete))
Expand All @@ -42,23 +46,32 @@ object Examples extends App with ShopSchema with SqlServerJdbcModule {
select(userId as "id") from users where (fName === "Fred") //todo fix issue #36
}) */

// select first_name, last_name, order_date from users left join orders on users.usr_id = orders.usr_id
// SELECT "users"."first_name", "users"."last_name", "orders"."order_date" FROM "users" LEFT JOIN "orders" ON "orders"."usr_id" = "users"."id"
val basicJoin =
select(fName, lName, orderDate).from(users.leftOuter(orders).on(fkUserId === userId))
println(renderRead(basicJoin))
/*
select users.usr_id, first_name, last_name, sum(quantity * unit_price) as "total_spend"
from users
left join orders on users.usr_id = orders.usr_id
left join order_details on orders.order_id = order_details.order_id
group by users.usr_id, first_name, last_name */

// UPDATE "users" SET "first_name" = 'foo', "last_name" = 'bar', "age" = "users"."age" + 1 WHERE true and "users"."age" > 100
val basicUpdate =
update(users)
.set(fName, "foo")
.set(lName, "bar")
.set(age, age + 1)
.where(age > 100)
println(renderUpdate(basicUpdate))

/*
SELECT "users"."id", "users"."first_name", "users"."last_name", sum("order_details"."quantity" * "order_details"."unit_price"), sum(abs("order_details"."quantity"))
FROM "users"
INNER JOIN "orders" ON "users"."id" = "orders"."usr_id"
LEFT JOIN "order_details" ON "orders"."id" = "order_details"."order_id"
GROUP BY "users"."id", "users"."first_name", "users"."last_name" */
val orderValues =
select(
userId,
fName,
lName,
(Sum(quantity * unitPrice) as "total_spend"),
Sum(quantity * unitPrice),
Sum(Abs(quantity))
)
.from(
Expand All @@ -73,9 +86,7 @@ object Examples extends App with ShopSchema with SqlServerJdbcModule {

import scala.language.postfixOps

/*
* select users.first_name, users.last_name from users where true and users.first_name is not null
*/
// SELECT "users"."first_name", "users"."last_name" FROM "users" WHERE true and "users"."first_name" is not null
val withPropertyOp = select(fName, lName).from(users).where(fName isNotNull)
println(renderRead(withPropertyOp))
}
4 changes: 2 additions & 2 deletions examples/src/main/scala/zio/sql/GroupByExamples.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package zio.sql

import zio.sql.sqlserver.SqlServerJdbcModule
import zio.sql.postgresql.PostgresJdbcModule

object GroupByExamples extends App with ShopSchema with SqlServerJdbcModule {
object GroupByExamples extends App with ShopSchema with PostgresJdbcModule {
import AggregationDef._
import ColumnSet._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ trait ShopSchema extends Jdbc { self =>
import self.ColumnSet._

object Users {
val users = (uuid("id") ++ localDate("dob") ++ string("first_name") ++ string("last_name")).table("users")
val users =
(uuid("id") ++ int("age") ++ localDate("dob") ++ string("first_name") ++ string("last_name")).table("users")

val (userId, dob, fName, lName) = users.columns
val (userId, age, dob, fName, lName) = users.columns
}
object Orders {
val orders = (uuid("id") ++ uuid("usr_id") ++ localDate("order_date")).table("orders")
Expand Down

0 comments on commit 4969605

Please sign in to comment.