All Projects → x2bool → Kuery

x2bool / Kuery

Licence: mit
Strongly typed SQL in Kotlin

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Kuery

Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+1011.06%)
Mutual labels:  sql, orm, sqlite
Beam
A type-safe, non-TH Haskell SQL library and ORM
Stars: ✭ 454 (+128.14%)
Mutual labels:  sql, orm, sqlite
Freezer
A simple & fluent Android ORM, how can it be easier ? RxJava2 compatible
Stars: ✭ 326 (+63.82%)
Mutual labels:  sql, orm, sqlite
Walkable
A Clojure(script) SQL library for building APIs: Datomic® (GraphQL-ish) pull syntax, data driven configuration, dynamic filtering with relations in mind
Stars: ✭ 384 (+92.96%)
Mutual labels:  sql, orm, sqlite
Smartsql
SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics
Stars: ✭ 775 (+289.45%)
Mutual labels:  sql, orm, sqlite
Xo
Command line tool to generate idiomatic Go code for SQL databases supporting PostgreSQL, MySQL, SQLite, Oracle, and Microsoft SQL Server
Stars: ✭ 2,974 (+1394.47%)
Mutual labels:  sql, orm, sqlite
Android Orma
An ORM for Android with type-safety and painless smart migrations
Stars: ✭ 442 (+122.11%)
Mutual labels:  sql, orm, sqlite
Db
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
Stars: ✭ 2,832 (+1323.12%)
Mutual labels:  sql, orm, sqlite
Nano Sql
Universal database layer for the client, server & mobile devices. It's like Lego for databases.
Stars: ✭ 717 (+260.3%)
Mutual labels:  sql, orm, sqlite
Sequelize
An easy-to-use and promise-based multi SQL dialects ORM tool for Node.js
Stars: ✭ 25,422 (+12674.87%)
Mutual labels:  sql, orm, sqlite
Go Sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM.
Stars: ✭ 539 (+170.85%)
Mutual labels:  sql, orm, sqlite
Ebean
Ebean ORM
Stars: ✭ 1,172 (+488.94%)
Mutual labels:  sql, orm, sqlite
Sqlite orm
❤️ SQLite ORM light header only library for modern C++
Stars: ✭ 1,121 (+463.32%)
Mutual labels:  sql, orm, sqlite
Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (-9.05%)
Mutual labels:  sql, orm, sqlite
Old Rustorm
An ORM for rust
Stars: ✭ 168 (-15.58%)
Mutual labels:  orm, sqlite
Sqlservice
The missing SQLAlchemy ORM interface.
Stars: ✭ 159 (-20.1%)
Mutual labels:  sql, orm
Rom Sql
SQL support for rom-rb
Stars: ✭ 169 (-15.08%)
Mutual labels:  sql, sqlite
Oreilly getting started with sql
Database files for the O'Reilly book "Getting Started with SQL: A hands on approach for beginners" http://goo.gl/z3zG54
Stars: ✭ 156 (-21.61%)
Mutual labels:  sql, sqlite
Simple Crud
PHP library to provide magic CRUD in MySQL/Sqlite databases with zero configuration
Stars: ✭ 190 (-4.52%)
Mutual labels:  orm, sqlite
Trilogy
TypeScript SQLite layer with support for both native C++ & pure JavaScript drivers.
Stars: ✭ 195 (-2.01%)
Mutual labels:  sql, sqlite

Kuery - strongly typed SQL in Kotlin

The library is a strongly typed alternative to plain text SQL. The main goal of this project is to make database-related code easier to develop and maintain. The project uses some of the Kotlin language features to achieve type safety.

Features

  • SQL-like syntax. Use language constructions you already know. Designed to cover the most common SQL features.
  • Strongly typed DSL makes it harder to make mistakes. Some of the most common errors are catched at compile time.
  • IDE's assist in code editing.
  • Easier and safer refactoring/renaming.
  • No reflection

Foundation

Database structure is defined by classes/objects inherited from the Table class. Tables are not domain model classes. Their purpose is to simply define relationships between tables and columns.

import tel.egram.kuery.*

object Organizations : Table("organizations") {
	val id = Column("id")
	val name = Column("name")
}

object Employees : Table("employees") {
	val id = Column("id")
	val name = Column("name")
	val organizationId = Column("organization_id")
}

Statements are the building blocks of the library. A statement usually starts with one of the following function calls:

  • over(table) - used for CREATE TABLE and DROP TABLE statements
  • into(table) - used for INSERT statements
  • from(table) - used for SELECT, UPDATE and DELETE statements

Dialects are responsible for converting statements into actual SQL:

import tel.egram.kuery.*
import tel.egram.kuery.sqlite.*

val statement = from(Employees).where { e -> e.id eq 1 }.select { e -> e.name }
val sql = statement.toString(SQLiteDialect)
print(sql) // SELECT "name" FROM "employees" WHERE "id" = 1

Data Definition Language

Some parts of data definition language are specific to SQL dialects. An example for SQLite might look like this:

CREATE TABLE statement

import tel.egram.kuery.*
import tel.egram.kuery.sqlite.*

// CREATE TABLE "organizations" ...
over(Organizations)
    .create {
        integer(it.id).primaryKey(autoIncrement = true)..
        text(it.name).unique().notNull()
    }
    
// CREATE TABLE "employees" ...
over(Employees)
    .create {
        integer(it.id).primaryKey(autoIncrement = true)..
        text(it.name).unique().notNull()..
        integer(it.organizationId).foreignKey(references = Organizations.id)
    }

DROP TABLE statement

// DROP TABLE "employees"
over(Employees).drop()

Data Manipulation Language

Data manipulation is the most powerfull and complex part of SQL. The library supports INSERT, SELECT, UPDATE and DELETE statements.

INSERT statement

// INSERT INTO "employees"("name", "organization_id") VALUES("John Doe", 1)
into(Employees)
    .insert { e -> e.name("John Doe") .. e.organizationId(1) }

SELECT statement

The library provides the following operators to compose queries:

  • and
  • or
  • not
  • eq (equals)
  • ne (not equals)
  • lt (less than)
  • lte (less than or equal to)
  • gt (greater than)
  • gte (greater than or equal to)
// SELECT "id", "name" FROM "organizations" WHERE ...
from(Employees)
    .where { e -> (e.organizationId ne null) and (e.name eq "John Doe") }
    .groupBy { e -> e.name }
    .having { e -> e.id ne null }
    .orderBy { e -> e.name.asc .. e.id.desc }
    .limit { 10 }
    .offset { 10 }
    .select { e -> e.id .. e.name }

JOINs are also supported in select statements

// SELECT ... FROM "organizations" JOIN "employees" ON ...
from(Organizations)
    .join(Employees).on { o, e -> o.id eq e.organizationId }
    .select { o, e -> o.name .. e.name }

UPDATE statement

// UPDATE "organizations" SET "name" = 'John Doe' WHERE "id" = 1
from(Organizations)
    .where { o -> o.id eq 1 }
    .update { o -> o.name("John Doe") }

DELETE statement

// DELETE FROM "organizations" WHERE "id" = 0
from(Organizations)
    .where { o -> o.id eq 0 }
    .delete()

Download

Maven:

<!-- Core library -->
<dependency>
  <groupId>tel.egram.kuery</groupId>
  <artifactId>core</artifactId>
  <version>0.5.3</version>
  <type>pom</type>
</dependency>

<!-- SQLite dialect -->
<dependency>
  <groupId>tel.egram.kuery</groupId>
  <artifactId>sqlite</artifactId>
  <version>0.5.3</version>
  <type>pom</type>
</dependency>

Gradle:

// Core library
compile 'tel.egram.kuery:core:0.5.3'
// SQLite dialect
compile 'tel.egram.kuery:sqlite:0.5.3'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].