Skip to content
SQLDelight - Generates typesafe Kotlin APIs from SQL https://cashapp.github.io/sqldelight/
Kotlin Java Other
Branch: master
Clone or download
JakeWharton Merge pull request #1527 from saket/saket/dec15/missing-migrations-doc
Restore missing section for migrations to mkdocs
Latest commit 7f4cfd8 Dec 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.buildscript Fix emulators (#1512) Dec 10, 2019
docs Restore missing section for migrations to mkdocs Dec 15, 2019
drivers Add windows support (#1337) Dec 11, 2019
extensions Print remaining rows when they are not all consumed (#1424) Jul 25, 2019
gradle Include API docs in the docs website (#1523) Dec 14, 2019
sample Update the iOS sample to use cocoapods (#1519) Dec 12, 2019
sqldelight-compiler Ensure Truth.assertThat is always imported Dec 13, 2019
sqldelight-gradle-plugin Ensure Truth.assertThat is always imported Dec 13, 2019
sqldelight-idea-plugin Prepare for release 1.2.1 Dec 11, 2019
sqldelight-runtime Add windows support (#1337) Dec 11, 2019
sqlite-migrations Tell SchemaCrawler not to get routines (#1429) Aug 7, 2019
test-util Compile against IntelliJ 2019.2 Aug 30, 2019
.gitignore Include API docs in the docs website (#1523) Dec 14, 2019
.travis.yml Update the iOS sample to use cocoapods (#1519) Dec 12, 2019
CHANGELOG.md Use MkDocs to generate documentation website (#1520) Dec 13, 2019
CONTRIBUTING.md Use MkDocs to generate documentation website (#1520) Dec 13, 2019
LICENSE.txt Add some open source ceremony. Feb 9, 2016
README.md Update README.md Dec 13, 2019
RELEASING.md Run the 'publish' task explicitly for release (#1002) Oct 9, 2018
UPGRADING.md Use MkDocs to generate documentation website (#1520) Dec 13, 2019
build.gradle Update kotlin to 1.3.30 (#1293) Apr 13, 2019
deploy_website.sh Include API docs in the docs website (#1523) Dec 14, 2019
gradle.properties Prepare next development version. Dec 11, 2019
gradlew Update gradle to 5.4 (#1318) Apr 23, 2019
gradlew.bat Update gradle to 5.4 (#1318) Apr 23, 2019
mkdocs.yml Restore missing section for migrations to mkdocs Dec 15, 2019
settings.gradle Update sample to 1.1.3 (#1302) Dec 11, 2019
update-expected.sh Script to generate expected files. Mar 8, 2016

README.md

SQLDelight

See the project website for documentation and APIs.

SQLDelight generates typesafe APIs from your SQL statements. It compile-time verifies your schema, statements, and migrations and provides IDE features like autocomplete and refactoring which make writing and maintaining SQL simple. SQLDelight currently supports the SQLite dialect and there are supported SQLite drivers on Android, JVM, iOS, and Windows.

Example

To use SQLDelight, apply the gradle plugin and put your SQL statements in a .sq file in src/main/sqldelight. Typically the first statement in the SQL file creates a table.

-- src/main/sqldelight/com/example/sqldelight/hockey/data/Player.sq

CREATE TABLE hockeyPlayer (
  player_number INTEGER NOT NULL,
  full_name TEXT NOT NULL
);

CREATE INDEX hockeyPlayer_full_name ON hockeyPlayer(full_name);

INSERT INTO hockeyPlayer (player_number, full_name)
VALUES (15, 'Ryan Getzlaf');

From this SQLDelight will generate a Database Kotlin class with an associated Schema object that can be used to create your database and run your statements on it. Doing this also requires a driver, which SQLDelight provides implementations of:

Android

dependencies {
  implementation "com.squareup.sqldelight:android-driver:1.2.1"
}
val driver: SqlDriver = AndroidSqliteDriver(Database.Schema, context, "test.db")

iOS, or Windows (Using Kotlin/Native)

dependencies {
  implementation "com.squareup.sqldelight:native-driver:1.2.1"
}

// You'll also need to have SQLite linked via -lsqlite3 during compilation.
val driver: SqlDriver = NativeSqliteDriver(Database.Schema, "test.db")

JVM

dependencies {
  implementation "com.squareup.sqldelight:sqlite-driver:1.2.1"
}
val driver: SqlDriver = JdbcSqliteDriver(IN_MEMORY)
Database.Schema.create(driver)

SQL statements inside a .sq file can be labeled to have a typesafe function generated for them available at runtime.

selectAll:
SELECT *
FROM hockeyPlayer;

insert:
INSERT INTO hockeyPlayer(player_number, full_name)
VALUES (?, ?);

insertFullPlayerObject:
INSERT INTO hockeyPlayer(player_number, full_name)
VALUES ?;

Files with labeled statements in them will have a queries file generated from them that matches the .sq file name - putting the above sql into Player.sq generates PlayerQueries.kt. To get a reference to PlayerQueries you need to wrap the driver we made above:

// In reality the database and driver above should be created a single time
// and passed around using your favourite dependency injection/service locator/singleton pattern.
val database = Database(driver)

val playerQueries: PlayerQueries = database.playerQueries

println(playerQueries.selectAll().executeAsList())
// Prints [HockeyPlayer.Impl(15, "Ryan Getzlaf")]

playerQueries.insert(player_number = 10, full_name = "Corey Perry")
println(playerQueries.selectAll().executeAsList())
// Prints [HockeyPlayer.Impl(15, "Ryan Getzlaf"), HockeyPlayer.Impl(10, "Corey Perry")]

val player = HockeyPlayer(10, "Ronald McDonald")
playerQueries.insertFullPlayerObject(player)

Gradle

buildscript {
  repositories {
    google()
    mavenCentral()
  }
  dependencies {
    classpath 'com.squareup.sqldelight:gradle-plugin:1.2.1'
  }
}

apply plugin: 'com.squareup.sqldelight'

License

Copyright 2016 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

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.
You can’t perform that action at this time.