alexandrustana / prepy   0.0.7

Apache License 2.0 GitHub

A small library for generating prepared SQL statements based on case classes

Scala versions: 2.13 2.12

prepy

A small library for generating prepared sql statements based on case classes

Module

prepy.syntax

Generates string sql queries based on the structure of case classes, this minimizes the effort of manually writing the name of the columns which must be extracted/updated/inserted in a query.

Features:

case class A(id: Int)
  1. quick transformation to sql. e.g. select[A].from[A].apply()

  2. avoid writing faulty queries. e.g. update[A].set[A].where(t => t.id == 1).apply()

  3. support SQL operators.

  • select[A].from[A].where(t => t.id between 1 and 2).apply()
  • select[A].from[A].where(t => t.id in List(1,2)).apply()

Get started

Prepy is available on Scala 2.13

 libraryDependencies += "com.github.alexandrustana" %% "prepy" % "0.0.7"

Examples

Converting case classes

case class UserTable(id: Int, firstName: String, lastName: String, age: Int, address: String)
case class UserName(id: Int, firstName: String, lastName: String)
case class UserAddress(id: Int, address: String) 

Now use the prepy magic to generate some sql queries

import prepy.syntax._

select[UserName].from[UserTable].where(u => u.id == 1).apply()
// res0: cats.data.Validated[String,String] = Valid(SELECT id, firstName, lastName FROM UserTable WHERE (id == 1))

update[UserTable].set[UserAddress].where(u => u.id == 1 || u.id == 2).apply()
// res1: cats.data.Validated[String,String] = Valid(UPDATE UserTable SET id = ?, address = ? WHERE (id == 1) OR (id == 2))

delete[UserTable].where(u => u.id == 3).apply()
// res2: cats.data.Validated[String,String] = Valid(DELETE FROM UserTable WHERE (id = 3))

Invalid syntax will generate Invalid structures with appropriate messages.

import prepy.syntax._

select[UserName].apply()
// res0: cats.data.Validated.Invalid[String] = Invalid(Incomplete SQL query. `select[T]` must be followed by a `from[K]`)

Support PostgreSQL automatic formatter

import prepy.syntax._
import prepy.formatter.postgresql._

update[UserTable].set[UserName].apply()
//res0: cats.data.Validated[String,String] = Valid(UPDATE user_table SET id = ?, first_name = ?, last_name = ?)

Other examples can be found in tests

Contributors and participation

prepy is currently maintained by Alexandru Stana.

Any form of contribution (issue report, PR, etc) is more than welcome.

The prepy project supports the Typelevel code of conduct and wants all of its channels (Gitter, GitHub, etc.) to be welcoming environments for everyone.

License

prepy is licensed under the Apache License, Version 2.0 (the "License"); you may not use this software except in compliance with the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.