Skip to content
Merged
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
4 changes: 2 additions & 2 deletions SQLite.swift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SQLite.swift"
s.version = "0.12.2"
s.version = "0.12.2.2"
s.summary = "A type-safe, Swift-language layer over SQLite3 for iOS and macOS."

s.description = <<-DESC
Expand All @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
s.homepage = "https://github.com/stephencelis/SQLite.swift"
s.license = 'MIT'
s.author = { "Stephen Celis" => "stephen@stephencelis.com" }
s.source = { :git => "https://github.com/stephencelis/SQLite.swift.git", :tag => s.version.to_s }
s.source = { :git => "https://github.com/stephanheilner/SQLite.swift.git", :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/stephencelis'

s.module_name = 'SQLite'
Expand Down
8 changes: 8 additions & 0 deletions SQLite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
49EB68C51F7B3CB400D89D40 /* Coding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49EB68C31F7B3CB400D89D40 /* Coding.swift */; };
49EB68C61F7B3CB400D89D40 /* Coding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49EB68C31F7B3CB400D89D40 /* Coding.swift */; };
49EB68C71F7B3CB400D89D40 /* Coding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49EB68C31F7B3CB400D89D40 /* Coding.swift */; };
D4DB368C20C09CFB00D5A58E /* SelectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4DB368A20C09C9B00D5A58E /* SelectTests.swift */; };
D4DB368D20C09CFC00D5A58E /* SelectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4DB368A20C09C9B00D5A58E /* SelectTests.swift */; };
D4DB368E20C09CFD00D5A58E /* SelectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4DB368A20C09C9B00D5A58E /* SelectTests.swift */; };
EE247AD71C3F04ED00AE3E12 /* SQLite.h in Headers */ = {isa = PBXBuildFile; fileRef = EE247AD61C3F04ED00AE3E12 /* SQLite.h */; settings = {ATTRIBUTES = (Public, ); }; };
EE247ADE1C3F04ED00AE3E12 /* SQLite.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EE247AD31C3F04ED00AE3E12 /* SQLite.framework */; };
EE247B031C3F06E900AE3E12 /* Blob.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE247AEE1C3F06E900AE3E12 /* Blob.swift */; };
Expand Down Expand Up @@ -228,6 +231,7 @@
3D67B3E51DB2469200A4F4C6 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = Platforms/WatchOS.platform/Developer/SDKs/WatchOS3.0.sdk/usr/lib/libsqlite3.tbd; sourceTree = DEVELOPER_DIR; };
49EB68C31F7B3CB400D89D40 /* Coding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coding.swift; sourceTree = "<group>"; };
A121AC451CA35C79005A31D1 /* SQLite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D4DB368A20C09C9B00D5A58E /* SelectTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectTests.swift; sourceTree = "<group>"; };
EE247AD31C3F04ED00AE3E12 /* SQLite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
EE247AD61C3F04ED00AE3E12 /* SQLite.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQLite.h; sourceTree = "<group>"; };
EE247AD81C3F04ED00AE3E12 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -418,6 +422,7 @@
19A17B93B48B5560E6E51791 /* Fixtures.swift */,
19A175C1F9CB3BBAB8FCEC7B /* RowTests.swift */,
19A1729B75C33F9A0B9A89C1 /* DateAndTimeFunctionTests.swift */,
D4DB368A20C09C9B00D5A58E /* SelectTests.swift */,
);
name = SQLiteTests;
path = Tests/SQLiteTests;
Expand Down Expand Up @@ -847,6 +852,7 @@
19A17F60B685636D1F83C2DD /* Fixtures.swift in Sources */,
19A1785195182AF8731A8BDA /* RowTests.swift in Sources */,
19A1769C1F3A7542BECF50FF /* DateAndTimeFunctionTests.swift in Sources */,
D4DB368E20C09CFD00D5A58E /* SelectTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -935,6 +941,7 @@
19A17408007B182F884E3A53 /* Fixtures.swift in Sources */,
19A1720B67ED13E6150C6A3D /* RowTests.swift in Sources */,
19A17C80076860CF7751A056 /* DateAndTimeFunctionTests.swift in Sources */,
D4DB368C20C09CFB00D5A58E /* SelectTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -993,6 +1000,7 @@
19A1709C3E7A406E62293B2A /* Fixtures.swift in Sources */,
19A171967CC511C4F6F773C9 /* RowTests.swift in Sources */,
19A172EB202970561E5C4245 /* DateAndTimeFunctionTests.swift in Sources */,
D4DB368D20C09CFC00D5A58E /* SelectTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
27 changes: 15 additions & 12 deletions Sources/SQLite/Typed/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ public protocol ExpressionType : Expressible { // extensions cannot have inherit

var template: String { get }
var bindings: [Binding?] { get }

init(_ template: String, _ bindings: [Binding?])
var alias: String? { get }

init(_ template: String, _ bindings: [Binding?], _ alias: String?)

}

extension ExpressionType {

public init(literal: String) {
self.init(literal, [])
public init(literal: String, alias: String? = nil) {
self.init(literal, [], alias)
}

public init(_ identifier: String) {
self.init(literal: identifier.quote())
}

public init<U : ExpressionType>(_ expression: U) {
self.init(expression.template, expression.bindings)
self.init(expression.template, expression.bindings, expression.alias)
}

}
Expand All @@ -56,10 +57,12 @@ public struct Expression<Datatype> : ExpressionType {

public var template: String
public var bindings: [Binding?]

public init(_ template: String, _ bindings: [Binding?]) {
public var alias: String?

public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
self.template = template
self.bindings = bindings
self.alias = alias
}

}
Expand Down Expand Up @@ -95,7 +98,7 @@ extension Expressible {
extension ExpressionType {

public var expression: Expression<Void> {
return Expression(template, bindings)
return Expression(template, bindings, alias)
}

public var asc: Expressible {
Expand All @@ -111,7 +114,7 @@ extension ExpressionType {
extension ExpressionType where UnderlyingType : Value {

public init(value: UnderlyingType) {
self.init("?", [value.datatypeValue])
self.init("?", [value.datatypeValue], nil)
}

}
Expand All @@ -123,7 +126,7 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr
}

public init(value: UnderlyingType.WrappedType?) {
self.init("?", [value?.datatypeValue])
self.init("?", [value?.datatypeValue], nil)
}

}
Expand All @@ -139,9 +142,9 @@ extension Value {
public let rowid = Expression<Int64>("ROWID")

public func cast<T: Value, U: Value>(_ expression: Expression<T>) -> Expression<U> {
return Expression("CAST (\(expression.template) AS \(U.declaredDatatype))", expression.bindings)
return Expression("CAST (\(expression.template) AS \(U.declaredDatatype))", expression.bindings, expression.alias)
}

public func cast<T: Value, U: Value>(_ expression: Expression<T?>) -> Expression<U?> {
return Expression("CAST (\(expression.template) AS \(U.declaredDatatype))", expression.bindings)
return Expression("CAST (\(expression.template) AS \(U.declaredDatatype))", expression.bindings, expression.alias)
}
39 changes: 28 additions & 11 deletions Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,22 @@ extension QueryType {
// MARK: -

fileprivate var selectClause: Expressible {
return " ".join([
let blah = " ".join([
Expression<Void>(literal: clauses.select.distinct ? "SELECT DISTINCT" : "SELECT"),
", ".join(clauses.select.columns),
", ".join(clauses.select.columns.map { column in
if let alias = column.expression.alias {
return " ".join([
Expression<Void>(literal: column.expression.template.quote()),
Expression<Void>(literal: "AS"),
Expression<Void>(literal: alias.quote())
])
}
return column
}),
Expression<Void>(literal: "FROM"),
tableName(alias: true)
])
return blah
}

fileprivate var joinClause: Expressible? {
Expand Down Expand Up @@ -852,10 +862,12 @@ public struct Select<T> : ExpressionType {

public var template: String
public var bindings: [Binding?]
public var alias: String?

public init(_ template: String, _ bindings: [Binding?]) {
public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
self.template = template
self.bindings = bindings
self.alias = alias
}

}
Expand All @@ -864,10 +876,12 @@ public struct Insert : ExpressionType {

public var template: String
public var bindings: [Binding?]

public init(_ template: String, _ bindings: [Binding?]) {
public var alias: String?

public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
self.template = template
self.bindings = bindings
self.alias = alias
}

}
Expand All @@ -876,10 +890,12 @@ public struct Update : ExpressionType {

public var template: String
public var bindings: [Binding?]

public init(_ template: String, _ bindings: [Binding?]) {
public var alias: String?

public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
self.template = template
self.bindings = bindings
self.alias = alias
}

}
Expand All @@ -888,10 +904,12 @@ public struct Delete : ExpressionType {

public var template: String
public var bindings: [Binding?]

public init(_ template: String, _ bindings: [Binding?]) {
public var alias: String?

public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
self.template = template
self.bindings = bindings
self.alias = alias
}

}
Expand Down Expand Up @@ -963,7 +981,6 @@ extension Connection {
try expandGlob(true)(q)
continue column
}
throw QueryError.noSuchTable(name: namespace)
}
throw QueryError.noSuchTable(name: namespace)
}
Expand All @@ -973,7 +990,7 @@ extension Connection {
continue
}

columnNames[each.expression.template] = idx
columnNames[each.expression.alias ?? each.expression.template] = idx
idx += 1
}
return columnNames
Expand Down
2 changes: 1 addition & 1 deletion Tests/SQLiteTests/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ConnectionTests : SQLiteTestCase {
let db = try! Connection("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3")
XCTAssertEqual("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3", db.description)
}

func test_readonly_returnsFalseOnReadWriteConnections() {
XCTAssertFalse(db.readonly)
}
Expand Down
45 changes: 45 additions & 0 deletions Tests/SQLiteTests/SelectTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import XCTest
@testable import SQLite

class SelectTests: SQLiteTestCase {

override func setUp() {
super.setUp()
CreateUsersTable()
CreateUsersDataTable()
}

func CreateUsersDataTable() {
try! db.execute("""
CREATE TABLE users_name (
id INTEGER,
user_id INTEGER REFERENCES users(id),
name TEXT
)
"""
)
}

func test_select_columns_from_multiple_tables() {
let usersData = Table("users_name")
let users = Table("users")

let name = Expression<String>("name")
let id = Expression<Int64>("id")
let userID = Expression<Int64>("user_id")
let email = Expression<String>("email")

try! InsertUser("Joey")
try! db.run(usersData.insert(
id <- 1,
userID <- 1,
name <- "Joey"
))

try! db.prepare(users.select(name, email).join(usersData, on: userID == users[id])).forEach {
XCTAssertEqual($0[name], "Joey")
XCTAssertEqual($0[email], "Joey@example.com")
}
}

}