All Projects → vapor → Sql Kit

vapor / Sql Kit

Licence: mit
*️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Sql Kit

Evolve
Database migration tool for .NET and .NET Core projects. Inspired by Flyway.
Stars: ✭ 477 (+314.78%)
Mutual labels:  sql, mysql, postgresql, sqlite
Sequelize
An easy-to-use and promise-based multi SQL dialects ORM tool for Node.js
Stars: ✭ 25,422 (+22006.09%)
Mutual labels:  sql, mysql, postgresql, sqlite
Go Sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM.
Stars: ✭ 539 (+368.7%)
Mutual labels:  sql, mysql, postgresql, sqlite
Sqlx
🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, SQLite, and MSSQL.
Stars: ✭ 5,039 (+4281.74%)
Mutual labels:  sql, mysql, postgresql, sqlite
Eosio sql plugin
EOSIO sql database plugin
Stars: ✭ 21 (-81.74%)
Mutual labels:  sql, mysql, postgresql, sqlite
Dbeaver
Free universal database tool and SQL client
Stars: ✭ 23,752 (+20553.91%)
Mutual labels:  sql, mysql, postgresql, sqlite
Sqlancer
Detecting Logic Bugs in DBMS
Stars: ✭ 672 (+484.35%)
Mutual labels:  sql, mysql, postgresql, sqlite
Db
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
Stars: ✭ 2,832 (+2362.61%)
Mutual labels:  sql, mysql, postgresql, sqlite
Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (+1001.74%)
Mutual labels:  sql, mysql, postgresql, sqlite
Smartsql
SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics
Stars: ✭ 775 (+573.91%)
Mutual labels:  sql, mysql, postgresql, 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 (+2486.09%)
Mutual labels:  sql, mysql, postgresql, sqlite
Xeus Sql
xeus-sql is a Jupyter kernel for general SQL implementations.
Stars: ✭ 85 (-26.09%)
Mutual labels:  sql, mysql, postgresql, sqlite
E Commerce Db
Database schema for e-commerce (webstores) sites.
Stars: ✭ 245 (+113.04%)
Mutual labels:  sql, mysql, postgresql, 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 (+233.91%)
Mutual labels:  sql, mysql, postgresql, sqlite
Sequelize Auto Migrations
Migration generator && runner for sequelize
Stars: ✭ 233 (+102.61%)
Mutual labels:  sql, mysql, postgresql, sqlite
Beekeeper Studio
Modern and easy to use SQL client for MySQL, Postgres, SQLite, SQL Server, and more. Linux, MacOS, and Windows.
Stars: ✭ 8,053 (+6902.61%)
Mutual labels:  sql, mysql, postgresql, sqlite
Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (+57.39%)
Mutual labels:  sql, mysql, postgresql, sqlite
Heidisql
A lightweight client for managing MariaDB, MySQL, SQL Server, PostgreSQL and SQLite, written in Delphi
Stars: ✭ 2,864 (+2390.43%)
Mutual labels:  sql, mysql, postgresql, sqlite
Vscode Sqltools
Database management for VSCode
Stars: ✭ 741 (+544.35%)
Mutual labels:  sql, mysql, postgresql, sqlite
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+755.65%)
Mutual labels:  sql, mysql, postgresql, sqlite
SQLKit
Documentation Team Chat MIT License Continuous Integration Swift 5.2

Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.

Major Releases

The table below shows a list of SQLKit major releases alongside their compatible NIO and Swift versions.

Version NIO Swift SPM
3.0 2.0+ 5.2+ from: "3.0.0"
2.0 1.0+ 4.0+ from: "2.0.0"
1.0 n/a 4.0+ from: "1.0.0"

Use the SPM string to easily include the dependendency in your Package.swift file.

.package(url: "https://github.com/vapor/sql-kit.git", from: ...)

Supported Platforms

PostgresNIO supports the following platforms:

  • Ubuntu 16.04+
  • macOS 10.15+

Overview

SQLKit is an API for building and serializing SQL queries in Swift. SQLKit attempts to abstract away SQL dialect inconsistencies where possible allowing you to write queries that can run on multiple database flavors. Where abstraction is not possible, SQLKit provides powerful APIs for custom or dynamic behavior.

Supported Databases

These database packages are built on SQLKit:

Configuration

SQLKit does not deal with creating or managing database connections itself. This package is focused entirely around building and serializing SQL queries. To connect to your SQL database, refer to your specific database package's documentation. Once you are connected to your database and have an instance of SQLDatabase, you are ready to continue.

Database

Instances of SQLDatabase are capable of serializing and executing SQLExpression.

let db: SQLDatabase = ...
db.execute(sql: SQLExpression, onRow: (SQLRow) -> ())

SQLExpression is a protocol that represents a SQL query string and optional bind values. It can represent an entire SQL query or just a fragment.

SQLKit provides SQLExpressions for common queries like SELECT, UPDATE, INSERT, DELETE, CREATE TABLE, and more.

var select = SQLSelect()
select.columns = [...]
select.tables = [...]
select.predicate = ...

SQLDatabase can be used to create fluent query builders for most of these query types.

let planets = try db.select()
    .column("*")
    .from("planets")
    .where("name", .equal, "Earth")
    .all().wait()

You can execute a query builder by calling run().

Rows

For query builders that support returning results, like select(), there are additional methods for handling the database output.

  • all(): Returns an array of rows.
  • first(): Returns an optional row.
  • run(_:): Accepts a closure that handles rows as they are returned.

Each of these methods returns SQLRow which has methods for access column values.

let row: SQLRow
let name = try row.decode(column: "name", as: String.self)
print(name) // String

Codable

SQLRow also supports decoding Codable models directly from a row.

struct Planet: Codable {
    var name: String
}

let planet = try row.decode(model: Planet.self)

Query builders that support returning results have convenience methods for automatically decoding models.

let planets = try db.select()
    ...
    .all(decoding: Planet.self).wait()

Select

The select() method creates a SELECT query builder.

let planets = try db.select()
    .columns("id", "name")
    .from("planets")
    .where("name", .equal, "Earth")
    .all().wait()

This code would generate the following SQL:

SELECT id, name FROM planets WHERE name = ?

Notice that Encodable values are automatically bound as parameters instead of being serialized directly to the query.

The select builder has the following methods.

  • columns
  • from
  • where (orWhere)
  • limit
  • offset
  • groupBy
  • having (orHaving)
  • distinct
  • for (lockingClause)
  • join

By default, query components like where will be joined by AND. Methods prefixed with or exist for joining by OR.

builder.where("name", .equal, "Earth").orWhere("name", .equal, "Mars")

This code would generate the following SQL:

name = ? OR name = ?

where also supports creating grouped clauses.

builder.where("name", .notEqual, SQLLiteral.null).where {
    $0.where("name", .equal, SQLBind("Milky Way"))
        .orWhere("name", .equal, SQLBind("Andromeda"))
}

This code generates the following SQL:

name != NULL AND (name == ? OR name == ?)

Insert

The insert(into:) method creates an INSERT query builder.

try db.insert(into: "galaxies")
    .columns("id", "name")
    .values(SQLLiteral.default, SQLBind("Milky Way"))
    .values(SQLLiteral.default, SQLBind("Andromeda"))
    .run().wait()

This code would generate the following SQL:

INSERT INTO galaxies (id, name) VALUES (DEFAULT, ?) (DEFAULT, ?)

The insert builder also has a method for encoding a Codable type as a set of values.

struct Galaxy: Codable {
    var name: String
}

try builder.model(Galaxy(name: "Milky Way"))

Update

The update(_:) method creates an UPDATE query builder.

try db.update("planets")
    .where("name", .equal, "Jpuiter")
    .set("name", to: "Jupiter")
    .run().wait()

This code generates the following SQL:

UPDATE planets SET name = ? WHERE name = ?

The update builder supports the same where and orWhere methods as the select builder.

Delete

The delete(from:) method creates a DELETE query builder.

try db.delete(from: "planets")
    .where("name", .equal, "Jupiter")
    .run().wait()

This code generates the following SQL:

DELETE FROM planets WHERE name = ?

The delete builder supports the same where and orWhere methods as the select builder.

Raw

The raw(_:) method allows for passing custom SQL query strings with support for parameterized binds.

let table = "planets"
let planets = try db.raw("SELECT * FROM \(table) WHERE name = \(bind: planet)")
    .all().wait()

This code generates the following SQL:

SELECT * FROM planets WHERE name = ?

The \(bind:) interpolation should be used for any user input to avoid SQL injection.

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].