Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up examples #668

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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