Skip to content

Commit

Permalink
Merge branch 'master' into update/scala3-library-3.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jczuchnowski authored Jul 27, 2023
2 parents c19f25f + 18a2ca3 commit 0bde5eb
Show file tree
Hide file tree
Showing 116 changed files with 3,443 additions and 2,921 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Scala Steward: Reformat with scalafmt 3.7.10
9bcedec20a4fc7adedce8aea4921c05b0c7acfa4
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: CI

env:
JDK_JAVA_OPTIONS: -XX:+PrintCommandLineFlags -XX:+UseG1GC -Xmx4g -Xms4g
JVM_OPTS: -XX:+PrintCommandLineFlags -XX:+UseG1GC -Xmx4g -Xms4g
JDK_JAVA_OPTIONS: -XX:+PrintCommandLineFlags -XX:+UseG1GC -Xmx4g -Xms4g -Xss4m
JVM_OPTS: -XX:+PrintCommandLineFlags -XX:+UseG1GC -Xmx4g -Xms4g -Xss4m

on:
pull_request:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
generate-readme:
name: Generate README
runs-on: ubuntu-latest
if: ${{ (github.event_name == 'push') || ((github.event_name == 'release') && (github.event_name == 'published')) }}
if: ${{ (github.event_name == 'push') || ((github.event_name == 'release') && (github.event.action == 'published')) }}
steps:
- name: Git Checkout
uses: actions/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.5.9"
version = "3.7.11"
maxColumn = 120
align.preset = most
continuationIndent.defnSite = 2
Expand Down
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

ZIO SQL lets you write type-safe, type-inferred, and composable SQL queries in ordinary Scala, helping you prevent persistence bugs before they happen, and leverage your IDE to make writing SQL productive, safe, and fun.

[![Production Ready](https://img.shields.io/badge/Project%20Stage-Production%20Ready-brightgreen.svg)](https://github.com/zio/zio/wiki/Project-Stages) ![CI Badge](https://github.com/zio/zio-sql/workflows/CI/badge.svg) [![Sonatype Releases](https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-sql_2.13.svg?label=Sonatype%20Release)](https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-sql_2.13/) [![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-sql_2.13.svg?label=Sonatype%20Snapshot)](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-sql_2.13/) [![javadoc](https://javadoc.io/badge2/dev.zio/zio-sql-docs_2.13/javadoc.svg)](https://javadoc.io/doc/dev.zio/zio-sql-docs_2.13) [![ZIO SQL](https://img.shields.io/github/stars/zio/zio-sql?style=social)](https://github.com/zio/zio-sql)
[![Development](https://img.shields.io/badge/Project%20Stage-Development-green.svg)](https://github.com/zio/zio/wiki/Project-Stages) ![CI Badge](https://github.com/zio/zio-sql/workflows/CI/badge.svg) [![Sonatype Releases](https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-sql_2.13.svg?label=Sonatype%20Release)](https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-sql_2.13/) [![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-sql_2.13.svg?label=Sonatype%20Snapshot)](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-sql_2.13/) [![javadoc](https://javadoc.io/badge2/dev.zio/zio-sql-docs_2.13/javadoc.svg)](https://javadoc.io/doc/dev.zio/zio-sql-docs_2.13) [![ZIO SQL](https://img.shields.io/github/stars/zio/zio-sql?style=social)](https://github.com/zio/zio-sql)

## Introduction

Expand Down Expand Up @@ -63,16 +63,16 @@ ZIO SQL is packaged into separate modules for different databases. Depending on

```scala
//PostgreSQL
libraryDependencies += "dev.zio" %% "zio-sql-postgres" % "0.1.1"
libraryDependencies += "dev.zio" %% "zio-sql-postgres" % "0.1.2"

//MySQL
libraryDependencies += "dev.zio" %% "zio-sql-mysql" % "0.1.1"
libraryDependencies += "dev.zio" %% "zio-sql-mysql" % "0.1.2"

//Oracle
libraryDependencies += "dev.zio" %% "zio-sql-oracle" % "0.1.1"
libraryDependencies += "dev.zio" %% "zio-sql-oracle" % "0.1.2"

//SQL Server
libraryDependencies += "dev.zio" %% "zio-sql-sqlserver" % "0.1.1"
libraryDependencies += "dev.zio" %% "zio-sql-sqlserver" % "0.1.2"
```

## Imports and modules
Expand Down Expand Up @@ -106,9 +106,12 @@ Values that will represent tables in DSL are then created by calling `defineTabl
In order for `defineTable` to work, user need to provide implicit `Schema` of data type.

```scala
import java.util.UUID
import zio.schema.DeriveSchema
import zio.sql.postgresql.PostgresJdbcModule
import zio.sql.table.Table._

import java.time._
import java.util.UUID

object Repository extends PostgresJdbcModule {
final case class Product(id: UUID, name: String, price: BigDecimal)
Expand Down Expand Up @@ -140,43 +143,43 @@ Using the previous example with `Product` and `Order` table
```scala
val (id, name, price) = products.columns

val (orderId, productId, quantity) = orders.columns
val (orderId, productId, quantity, date) = orders.columns
```

## Selects

Simple select.

```scala
val allProducts = select(productId, name, price).from(products)
val allProducts = select(id, name, price).from(products)
```

Using `where` clause.

```scala
def productById(id: UUID) =
select(productId, name, price).from(products).where(productId === id)
def productById(uuid: UUID) =
select(id, name, price).from(products).where(id === uuid)
```

Inner join.

```scala
val ordersWithProductNames =
select(orderId, name).from(products.join(orders).on(productId === fkProductId))
select(orderId, name).from(products.join(orders).on(productId === id))
```

Left outer join.

```scala
val leftOuter =
select(orderId, name).from(products.leftOuter(orders).on(productId === fkProductId))
select(orderId, name).from(products.leftOuter(orders).on(productId === id))
```

Right outer join.

```scala
val rightOuter =
select(orderId, name).from(products.rightOuter(orders).on(productId === fkProductId))
select(orderId, name).from(products.rightOuter(orders).on(productId === id))
```

Using `limit` and `offset`
Expand All @@ -185,26 +188,36 @@ Using `limit` and `offset`
val limitedResults =
select(orderId, name)
.from(products.join(orders)
.on(productId === fkProductId))
.on(productId === id))
.limit(5)
.offset(10)
```

## Inserts

```scala
insertInto(products)
(productId, name, price)
.values((UUID.randomUUID(), "Zionomicon", 10.5))
def insertProduct(uuid: UUID) =
insertInto(products)(id, name, price)
.values((uuid, "Zionomicon", 10.5))
```

## Updates

TODO: details
```scala
def updateProduct(uuid: UUID) =
update(products)
.set(name, "foo")
.set(price, price * 1.1)
.where(id === uuid)
```

## Deletes

TODO: details
```scala
def deleteProduct(uuid: UUID) =
deleteFrom(products)
.where(id === uuid)
```

## Transactions

Expand Down
9 changes: 5 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck"

val zioVersion = "2.0.6"
val zioSchemaVersion = "0.4.2"
val testcontainersVersion = "1.17.6"
val testcontainersScalaVersion = "0.40.11"
val logbackVersion = "1.2.11"
val testcontainersVersion = "1.18.3"
val testcontainersScalaVersion = "0.40.17"
val logbackVersion = "1.2.12"

lazy val root = project
.in(file("."))
Expand Down Expand Up @@ -98,7 +98,7 @@ lazy val docs = project
crossScalaVersions := Seq(Scala213, Scala212, ScalaDotty),
projectName := "ZIO SQL",
mainModuleName := (coreJVM / moduleName).value,
projectStage := ProjectStage.ProductionReady,
projectStage := ProjectStage.Development,
ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(),
docsPublishBranch := "master",
readmeContribution := readmeContribution.value +
Expand Down Expand Up @@ -131,6 +131,7 @@ lazy val examples = project

lazy val driver = project
.in(file("driver"))
.dependsOn(coreJVM)
.settings(stdSettings("zio-sql-driver"))
.settings(buildInfoSettings("zio.sql.driver"))
.settings(
Expand Down
58 changes: 37 additions & 21 deletions core/jvm/src/main/scala/zio/sql/Sql.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package zio.sql

import zio.schema.Schema

trait Sql
extends SelectModule
with DeleteModule
with UpdateModule
with ExprModule
with TableModule
with InsertModule
with UtilsModule
with SelectUtilsModule
with InsertUtilsModule {
self =>
import zio.schema.{ Schema, StandardType }
import zio.sql.table._
import zio.sql.update._
import zio.sql.select._
import zio.sql.insert._
import zio.sql.delete._

case class SqlRow(params: List[SqlParameter])
case class SqlParameter(_type: StandardType[_], value: Any)
case class SqlStatement(query: String, rows: List[SqlRow])

trait Sql {

/*
* (SELECT *, "foo", table.a + table.b AS sum... FROM table WHERE cond) UNION (SELECT ... FROM table)
Expand All @@ -28,24 +27,41 @@ trait Sql
*/
val select: SelectByCommaBuilder = SelectByCommaBuilder()

sealed trait Star
val * : Star = new Star {}

def select(star: Star): SelectAll = {
val _ = star
new SelectAll()
}

def select[F, A, B <: SelectionSet[A]](selection: Selection[F, A, B]): SelectBuilder[F, A, B] =
SelectBuilder[F, A, B](selection)

def subselect[ParentTable]: SubselectPartiallyApplied[ParentTable] = new SubselectPartiallyApplied[ParentTable]
def subselect[ParentTable]: SubselectByCommaBuilder[ParentTable] = new SubselectByCommaBuilder[ParentTable]

def deleteFrom[T <: Table](table: T): Delete[table.TableType] = Delete(table, true)

def update[A](table: Table.Aux[A]): UpdateBuilder[A] = UpdateBuilder(table)

def insertInto[Source, AllColumnIdentities](
table: Table.Source.Aux_[Source, AllColumnIdentities]
): InsertIntoBuilder[Source, AllColumnIdentities] = InsertIntoBuilder(table)
val insertInto: InsertByCommaBuilder = InsertByCommaBuilder()

def renderDelete(delete: Delete[_]): String

def renderDelete(delete: self.Delete[_]): String
def renderRead(read: Read[_]): String

def renderRead(read: self.Read[_]): String
def renderUpdate(update: Update[_]): String

def renderUpdate(update: self.Update[_]): String
def renderInsert[A: Schema](insert: Insert[_, A]): SqlStatement

def renderInsert[A: Schema](insert: self.Insert[_, A]): String
// TODO don't know where to put it now
implicit def convertOptionToSome[A](implicit op: Schema[Option[A]]): Schema[Some[A]] =
op.transformOrFail[Some[A]](
{
case Some(a) => Right(Some(a))
case None => Left("cannot encode Right")
},
someA => Right(someA)
)
implicit val none: Schema[None.type] = Schema.singleton(None)
}
8 changes: 0 additions & 8 deletions core/jvm/src/main/scala/zio/sql/delete.scala

This file was deleted.

8 changes: 8 additions & 0 deletions core/jvm/src/main/scala/zio/sql/delete/Delete.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zio.sql.delete

import zio.sql.table._
import zio.sql.expr.Expr

final case class Delete[A](table: Table.Aux[A], whereExpr: Expr[_, A, Boolean]) {
def where[F](expr: Expr[F, A, Boolean]): Delete[A] = Delete(table, expr)
}
Loading

0 comments on commit 0bde5eb

Please sign in to comment.