All Projects → nerdyc → Squeal

nerdyc / Squeal

Licence: mit
A Swift wrapper for SQLite databases

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Squeal

Better Sqlite3
The fastest and simplest library for SQLite3 in Node.js.
Stars: ✭ 2,778 (+816.83%)
Mutual labels:  sql, database, sqlite, sqlite3
Avsqldebugger
A Simple Core Data Debugger that will look inside your apps DB
Stars: ✭ 30 (-90.1%)
Mutual labels:  sql, database, sqlite, sqlite-database
Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (+318.15%)
Mutual labels:  sql, database, sqlite, sqlite3
Sqlcheck
Automatically identify anti-patterns in SQL queries
Stars: ✭ 2,062 (+580.53%)
Mutual labels:  sql, database, sqlite3
Sqhell.vim
An SQL wrapper for vim
Stars: ✭ 113 (-62.71%)
Mutual labels:  sql, sqlite, sqlite3
Kangaroo
SQL client and admin tool for popular databases
Stars: ✭ 127 (-58.09%)
Mutual labels:  sql, database, sqlite
Sqlite orm
❤️ SQLite ORM light header only library for modern C++
Stars: ✭ 1,121 (+269.97%)
Mutual labels:  sql, sqlite, sqlite3
Web Database Analytics
Web scrapping and related analytics using Python tools
Stars: ✭ 175 (-42.24%)
Mutual labels:  sql, database, sqlite3
Rom Sql
SQL support for rom-rb
Stars: ✭ 169 (-44.22%)
Mutual labels:  sql, sqlite, sqlite3
Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (-40.26%)
Mutual labels:  sql, database, sqlite
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 2,315 (+664.03%)
Mutual labels:  sql, database, sqlite
Grdbcombine
GRDB ❤️ Combine
Stars: ✭ 220 (-27.39%)
Mutual labels:  sql, database, sqlite
Qtl
A friendly and lightweight C++ database library for MySQL, PostgreSQL, SQLite and ODBC.
Stars: ✭ 92 (-69.64%)
Mutual labels:  sql, database, sqlite
Goose
A database migration tool. Supports SQL migrations and Go functions.
Stars: ✭ 2,112 (+597.03%)
Mutual labels:  sql, database, sqlite
Ebean
Ebean ORM
Stars: ✭ 1,172 (+286.8%)
Mutual labels:  sql, database, sqlite
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+629.7%)
Mutual labels:  sql, database, sqlite
nim-gatabase
Connection-Pooling Compile-Time ORM for Nim
Stars: ✭ 103 (-66.01%)
Mutual labels:  sqlite, sqlite-database, sqlite3
Rqlite
The lightweight, distributed relational database built on SQLite
Stars: ✭ 9,147 (+2918.81%)
Mutual labels:  sql, database, sqlite
Sql.js
A javascript library to run SQLite on the web.
Stars: ✭ 9,594 (+3066.34%)
Mutual labels:  sql, database, sqlite
Trilogy
TypeScript SQLite layer with support for both native C++ & pure JavaScript drivers.
Stars: ✭ 195 (-35.64%)
Mutual labels:  sql, database, sqlite

Squeal, a Swift interface to SQLite

Squeal provides access to SQLite databases in Swift. Its goal is to provide a simple and straight-forward base API, allowing developers to build on top in ways that make sense for their apps. The API provides direct SQL access, as well as a complete set of helpers to reduce SQL drudgery. It's not a goal of this project to hide SQL from the developer, or to provide a generic object-mapping on top of SQLite.

Features

  • Small, straightforward Swift interface for accessing SQLite databases via SQL.
  • Helper methods for most common types of SQL statements.
  • Easy database schema versioning and migration DSL.
  • Simple DatabasePool implementation for concurrent access to a database.

Basic Usage

import Squeal

let db = Database()

// Create:
try db.createTable("contacts", definitions: [
    "id INTEGER PRIMARY KEY",
    "name TEXT",
    "email TEXT NOT NULL"
])

// Insert:
let contactId = try db.insertInto(
    "contacts",
    values: [
        "name": "Amelia Grey",
        "email": "[email protected]"
    ]
)

// Select:
struct Contact {
    let id:Int
    let name:String?
    let email:String
    
    init(row:Statement) throws {
        id = row.intValue("id") ?? 0
        name = row.stringValue("name")
        email = row.stringValue("email") ?? ""
    }
}

let contacts:[Contact] = try db.selectFrom(
    "contacts",
    whereExpr:"name IS NOT NULL",
    block: Contact.init
)

// Count:
let numberOfContacts = try db.countFrom("contacts")

The above example can be found in Squeal.playground to allow further exploration of Squeal's interface.

Migrations

Any non-trivial app will need to change its database schema as features are added or updated. Unfortunately, SQLite provides only minimal support for updating a database's schema. Things like removing a column or removing a NON NULL require the entire database to be re-created with the new schema.

Squeal makes migrations easy by including a Schema class with a simple DSL for declaring your database migrations. Once defined, the Schema can be used to migrate your database to the latest version.

Here's an example:

import Squeal

// Define a Schema:
let AppSchema = Schema(identifier:"contacts") { schema in
    // Version 1:
    schema.version(1) { v1 in
        // Create a Table:
        v1.createTable("contacts") { contacts in
            contacts.primaryKey("id")
            contacts.column("name", type:.Text)
            contacts.column("email", type:.Text, constraints:["NOT NULL"])
        }

        // Add an index
        v1.createIndex(
            "contacts_email",
            on: "contacts",
            columns: [ "email" ]
        )
    }
    
    // Version 2:
    schema.version(2) { v2 in        
        // Arbitrary SQL:
        v2.execute { db in
            try db.deleteFrom("contacts", whereExpr: "name IS NULL")
        }        
        // Tables can be altered in many ways.
        v2.alterTable("contacts") { contacts in
            contacts.alterColumn(
                "name",
                setConstraints: [ "NOT NULL" ]
            )
            contacts.addColumn("url", type: .Text)            
        }
    }
}

let db = Database()

// Migrate to the latest version:
let didMigrate = try AppSchema.migrate(db)

// Get the database version:
let migratedVersion = try db.queryUserVersionNumber()

// Reset the database:
try AppSchema.migrate(db, toVersion: 0)

The above example can be found in Migrations.playground.

Installation

Squeal can be installed via Carthage or CocoaPods.

Carthage

To install using Carthage, simply add the following line to your Cartfile:

github "nerdyc/Squeal"

CocoaPods

To install using Carthage, simply add the following to the appropriate target in your Podfile:

pod "Squeal"

License

Squeal is released under the MIT License. Details are in the LICENSE.txt file in the project.

Contributing

Contributions and suggestions are very welcome! No contribution is too small. Squeal (like Swift) is still evolving and feedback from the community is appreciated. Open an Issue, or submit a pull request!

The main requirement is for new code to be tested. Nobody appreciates bugs in their database.

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