Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 8 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ A pure [Swift][1.1] framework wrapping [SQLite3][1.2].
[1.3]: https://github.com/stephencelis/SQLite.swift


## Features

- A lightweight, uncomplicated query and parameter binding interface
- A flexible, chainable query builder
- Safe, automatically-typed data access
- Transactions with implicit commit/rollback
- Developer-friendly error handling and debugging
- Well-documented
- Extensively tested


## Usage
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, did my recent README changes somehow get reverted in your commit?


Explore interactively from the Xcode project’s playground.
Expand Down Expand Up @@ -57,38 +46,22 @@ for row in db.prepare("SELECT id, email FROM users") {

db.scalar("SELECT count(*) FROM users") // {Some 2}

let jr = db.prepare("INSERT INTO users (email, manager_id) VALUES (?, ?)")
let jr = db.prepare("INSERT INTO users (email, manager_id) VALUES (? ?)")
db.transaction(
stmt.run("dolly@example.com"),
jr.run("emery@example.com", db.lastID)
)
```

SQLite.swift also exposes a powerful query building interface.

``` swift
let products = db["products"]

let available = products.filter("quantity > ?", 0).order("name").limit(5)
for product in available {
println(product["name"])
}
// SELECT * FROM products WHERE quantity > 0 ORDER BY name LIMIT 5

products.count // SELECT count(*) FROM products
products.average("price") // SELECT avg(price) FROM products

if let id = products.insert(["name": "Sprocket", "price": 19.99]) {
println("inserted \(id)")
}
// INSERT INTO products (name, price) VALUES ('Sprocket', 19.99)

let updates: Int = products.filter(["id": id]).update(["quantity": 20])
// UPDATE products SET quantity = 20 WHERE id = 42
## Features

let deletes: Int = products.filter(["quantity": 0]).delete()
// DELETE FROM products WHERE quantity = 0
```
- Uncomplicated query and parameter binding interface
- Safe, automatically-typed data access
- Implicit commit/rollback interface
- Developer-friendly error handling and debugging
- Well-documented
- Extensively tested


## Installation
Expand Down
49 changes: 31 additions & 18 deletions SQLite Common/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
// THE SOFTWARE.
//

/// A dictionary mapping column names to values.
public typealias Values = [String: Datatype?]

/// A query object. Used to build SQL statements with a collection of chainable
/// helper functions.
/// A query object. Used to build SQL statements using a collection of
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also be reverting recent changes.

/// chainable helper functions.
public struct Query {

private var database: Database
Expand Down Expand Up @@ -319,7 +316,7 @@ public struct Query {
return database.prepare(Swift.join(" ", parts), bindings)
}

private func insertStatement(values: Values) -> Statement {
private func insertStatement(values: [String: Datatype?]) -> Statement {
var (parts, bindings) = (["INSERT INTO \(tableName)"], self.bindings)
let valuesClause = Swift.join(", ", map(values) { columnName, value in
bindings.append(value)
Expand All @@ -330,7 +327,7 @@ public struct Query {
return database.prepare(Swift.join(" ", parts), bindings)
}

private func updateStatement(values: Values) -> Statement {
private func updateStatement(values: [String: Datatype?]) -> Statement {
var (parts, bindings) = (["UPDATE \(tableName)"], [Datatype?]())
let valuesClause = Swift.join(", ", map(values) { columnName, value in
bindings.append(value)
Expand Down Expand Up @@ -388,11 +385,13 @@ public struct Query {

// MARK: - Array

public typealias Element = [String: Datatype?]

/// The first result (or nil if the query has no results).
public var first: Values? { return limit(1).generate().next() }
public var first: Element? { return limit(1).generate().next() }

/// The last result (or nil if the query has no results).
public var last: Values? { return reverse(self).first }
public var last: Element? { return reverse(self).first }

/// Returns true if the query has no results.
public var isEmpty: Bool { return first == nil }
Expand All @@ -404,21 +403,25 @@ public struct Query {
/// :param: values A dictionary of column names to values.
///
/// :returns: The statement.
public func insert(values: Values) -> Statement { return insert(values).statement }
public func insert(values: [String: Datatype?]) -> Statement {
return insert(values).statement
}

/// Runs an INSERT statement with the given row of values.
///
/// :param: values A dictionary of column names to values.
///
/// :returns: The row ID.
public func insert(values: Values) -> Int? { return insert(values).ID }
public func insert(values: [String: Datatype?]) -> Int? {
return insert(values).ID
}

/// Runs an INSERT statement with the given row of values.
///
/// :param: values A dictionary of column names to values.
///
/// :returns: The row ID and statement.
public func insert(values: Values) -> (ID: Int?, statement: Statement) {
public func insert(values: [String: Datatype?]) -> (ID: Int?, statement: Statement) {
let statement = insertStatement(values).run()
return (statement.failed ? nil : database.lastID, statement)
}
Expand All @@ -428,34 +431,42 @@ public struct Query {
/// :param: values A dictionary of column names to values.
///
/// :returns: The statement.
public func update(values: Values) -> Statement { return update(values).statement }
public func update(values: [String: Datatype?]) -> Statement {
return update(values).statement
}

/// Runs an UPDATE statement against the query with the given values.
///
/// :param: values A dictionary of column names to values.
///
/// :returns: The number of updated rows.
public func update(values: Values) -> Int { return update(values).changes }
public func update(values: [String: Datatype?]) -> Int {
return update(values).changes
}

/// Runs an UPDATE statement against the query with the given values.
///
/// :param: values A dictionary of column names to values.
///
/// :returns: The number of updated rows and statement.
public func update(values: Values) -> (changes: Int, statement: Statement) {
public func update(values: [String: Datatype?]) -> (changes: Int, statement: Statement) {
let statement = updateStatement(values).run()
return (statement.failed ? 0 : database.lastChanges ?? 0, statement)
}

/// Runs an DELETE statement against the query.
///
/// :returns: The statement.
public func delete() -> Statement { return delete().statement }
public func delete() -> Statement {
return delete().statement
}

/// Runs an DELETE statement against the query.
///
/// :returns: The number of deleted rows.
public func delete() -> Int { return delete().changes }
public func delete() -> Int {
return delete().changes
}

/// Runs an DELETE statement against the query.
///
Expand Down Expand Up @@ -541,11 +552,13 @@ extension Query: SequenceType {
// MARK: - GeneratorType
public struct QueryGenerator: GeneratorType {

public typealias Element = [String: Datatype?]

private var statement: Statement

private init(_ statement: Statement) { self.statement = statement }

public func next() -> Values? {
public func next() -> Element? {
statement.next()
return statement.values
}
Expand Down
3 changes: 1 addition & 2 deletions SQLite Common/SQLite Common.xcconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
PRODUCT_NAME = SQLite
PRODUCT_VERSION = 0.1
SWIFT_OBJC_BRIDGING_HEADER = SQLite Common/SQLite-Bridging-Header.h
PRODUCT_VERSION = 0.1
30 changes: 0 additions & 30 deletions SQLite Mac/SQLite Mac.h

This file was deleted.

24 changes: 0 additions & 24 deletions SQLite iOS Tests/Info.plist

This file was deleted.

28 changes: 0 additions & 28 deletions SQLite iOS/Info.plist

This file was deleted.

30 changes: 0 additions & 30 deletions SQLite iOS/SQLite iOS.h

This file was deleted.

Loading