Skip to content

Commit 21dfe27

Browse files
Merge pull request #1 from LDSChurch/alias
Commented out check breaking queries
2 parents ed8f603 + 394c3ff commit 21dfe27

File tree

4 files changed

+141
-26
lines changed

4 files changed

+141
-26
lines changed

Sources/SQLite/Core/Connection.swift

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,86 @@ public final class Connection {
8383
}
8484
}
8585
}
86+
87+
/// A sqlite3_open_v2 open flag.
88+
public enum Flag {
89+
90+
/// SQLITE_OPEN_READONLY
91+
case readonly
92+
93+
/// SQLITE_OPEN_READWRITE
94+
case readwrite
95+
96+
/// SQLITE_OPEN_CREATE
97+
case create
98+
99+
/// SQLITE_OPEN_URI
100+
case uri
101+
102+
/// SQLITE_OPEN_MEMORY
103+
case memory
104+
105+
/// SQLITE_OPEN_NOMUTEX
106+
case nomutex
107+
108+
/// SQLITE_OPEN_FULLMUTEX
109+
case fullmutex
110+
111+
/// SQLITE_OPEN_SHAREDCACHE
112+
case sharedcache
113+
114+
/// SQLITE_OPEN_PRIVATECACHE
115+
case privatecache
116+
117+
var rawValue: Int32 {
118+
switch self {
119+
case .readonly:
120+
return SQLITE_OPEN_READONLY
121+
case .readwrite:
122+
return SQLITE_OPEN_READWRITE
123+
case .create:
124+
return SQLITE_OPEN_CREATE
125+
case .uri:
126+
return SQLITE_OPEN_URI
127+
case .memory:
128+
return SQLITE_OPEN_MEMORY
129+
case .nomutex:
130+
return SQLITE_OPEN_NOMUTEX
131+
case .fullmutex:
132+
return SQLITE_OPEN_FULLMUTEX
133+
case .sharedcache:
134+
return SQLITE_OPEN_SHAREDCACHE
135+
case .privatecache:
136+
return SQLITE_OPEN_PRIVATECACHE
137+
}
138+
}
139+
}
86140

87141
public var handle: OpaquePointer { return _handle! }
88142

89143
fileprivate var _handle: OpaquePointer? = nil
90-
144+
145+
/// Initializes a new SQLite connection.
146+
///
147+
/// - Parameters:
148+
///
149+
/// - location: The location of the database. Creates a new database if it
150+
/// doesn’t already exist (unless in read-only mode).
151+
///
152+
/// Default: `.inMemory`.
153+
///
154+
/// - flags: sqlite3_open_v2 option flags.
155+
///
156+
/// - Returns: A new database connection.
157+
public init(_ location: Location = .inMemory, flags: [Flag]) throws {
158+
var flags = flags
159+
let firstFlag = flags.remove(at: 0)
160+
let sqlite3Flags = flags.reduce(firstFlag.rawValue) { $0 | $1.rawValue }
161+
162+
try check(sqlite3_open_v2(location.description, &_handle, sqlite3Flags, nil))
163+
queue.setSpecific(key: Connection.queueKey, value: queueContext)
164+
}
165+
91166
/// Initializes a new SQLite connection.
92167
///
93168
/// - Parameters:
@@ -102,12 +177,27 @@ public final class Connection {
102177
/// Default: `false`.
103178
///
104179
/// - Returns: A new database connection.
105-
public init(_ location: Location = .inMemory, readonly: Bool = false) throws {
106-
let flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE
107-
try check(sqlite3_open_v2(location.description, &_handle, flags | SQLITE_OPEN_FULLMUTEX, nil))
108-
queue.setSpecific(key: Connection.queueKey, value: queueContext)
180+
public convenience init(_ location: Location = .inMemory, readonly: Bool = false) throws {
181+
let flags: [Flag] = readonly ? [.readonly, .fullmutex] : [.create, .readwrite, .fullmutex]
182+
try self.init(location, flags: flags)
109183
}
110-
184+
185+
/// Initializes a new connection to a database.
186+
///
187+
/// - Parameters:
188+
///
189+
/// - filename: The location of the database. Creates a new database if
190+
/// it doesn’t already exist (unless in read-only mode).
191+
///
192+
/// - flags: sqlite3_open_v2 option flags.
193+
///
194+
/// - Throws: `Result.Error` iff a connection cannot be established.
195+
///
196+
/// - Returns: A new database connection.
197+
public convenience init(_ filename: String, flags: [Flag]) throws {
198+
try self.init(.uri(filename), flags: flags)
199+
}
200+
111201
/// Initializes a new connection to a database.
112202
///
113203
/// - Parameters:
@@ -123,7 +213,8 @@ public final class Connection {
123213
///
124214
/// - Returns: A new database connection.
125215
public convenience init(_ filename: String, readonly: Bool = false) throws {
126-
try self.init(.uri(filename), readonly: readonly)
216+
let flags: [Flag] = readonly ? [.readonly, .fullmutex] : [.create, .readwrite, .fullmutex]
217+
try self.init(.uri(filename), flags: flags)
127218
}
128219

129220
deinit {

Sources/SQLite/Typed/Expression.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,24 @@ public protocol ExpressionType : Expressible { // extensions cannot have inherit
2828

2929
var template: String { get }
3030
var bindings: [Binding?] { get }
31-
32-
init(_ template: String, _ bindings: [Binding?])
31+
var alias: String? { get }
32+
33+
init(_ template: String, _ bindings: [Binding?], _ alias: String?)
3334

3435
}
3536

3637
extension ExpressionType {
3738

38-
public init(literal: String) {
39-
self.init(literal, [])
39+
public init(literal: String, alias: String? = nil) {
40+
self.init(literal, [], alias)
4041
}
4142

4243
public init(_ identifier: String) {
4344
self.init(literal: identifier.quote())
4445
}
4546

4647
public init<U : ExpressionType>(_ expression: U) {
47-
self.init(expression.template, expression.bindings)
48+
self.init(expression.template, expression.bindings, expression.alias)
4849
}
4950

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

5758
public var template: String
5859
public var bindings: [Binding?]
59-
60-
public init(_ template: String, _ bindings: [Binding?]) {
60+
public var alias: String?
61+
62+
public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
6163
self.template = template
6264
self.bindings = bindings
65+
self.alias = alias
6366
}
6467

6568
}
@@ -95,7 +98,7 @@ extension Expressible {
9598
extension ExpressionType {
9699

97100
public var expression: Expression<Void> {
98-
return Expression(template, bindings)
101+
return Expression(template, bindings, alias)
99102
}
100103

101104
public var asc: Expressible {
@@ -111,7 +114,7 @@ extension ExpressionType {
111114
extension ExpressionType where UnderlyingType : Value {
112115

113116
public init(value: UnderlyingType) {
114-
self.init("?", [value.datatypeValue])
117+
self.init("?", [value.datatypeValue], nil)
115118
}
116119

117120
}
@@ -123,7 +126,7 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr
123126
}
124127

125128
public init(value: UnderlyingType.WrappedType?) {
126-
self.init("?", [value?.datatypeValue])
129+
self.init("?", [value?.datatypeValue], nil)
127130
}
128131

129132
}

Sources/SQLite/Typed/Query.swift

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,16 @@ extension QueryType {
506506
fileprivate var selectClause: Expressible {
507507
return " ".join([
508508
Expression<Void>(literal: clauses.select.distinct ? "SELECT DISTINCT" : "SELECT"),
509-
", ".join(clauses.select.columns),
509+
", ".join(clauses.select.columns.map { column in
510+
if let alias = column.expression.alias {
511+
return " ".join([
512+
Expression<Void>(literal: column.expression.template.quote()),
513+
Expression<Void>(literal: "AS"),
514+
Expression<Void>(literal: alias.quote())
515+
])
516+
}
517+
return column
518+
}),
510519
Expression<Void>(literal: "FROM"),
511520
tableName(alias: true)
512521
])
@@ -852,10 +861,12 @@ public struct Select<T> : ExpressionType {
852861

853862
public var template: String
854863
public var bindings: [Binding?]
855-
856-
public init(_ template: String, _ bindings: [Binding?]) {
864+
public var alias: String?
865+
866+
public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
857867
self.template = template
858868
self.bindings = bindings
869+
self.alias = alias
859870
}
860871

861872
}
@@ -864,10 +875,12 @@ public struct Insert : ExpressionType {
864875

865876
public var template: String
866877
public var bindings: [Binding?]
867-
868-
public init(_ template: String, _ bindings: [Binding?]) {
878+
public var alias: String?
879+
880+
public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
869881
self.template = template
870882
self.bindings = bindings
883+
self.alias = alias
871884
}
872885

873886
}
@@ -876,10 +889,12 @@ public struct Update : ExpressionType {
876889

877890
public var template: String
878891
public var bindings: [Binding?]
892+
public var alias: String?
879893

880-
public init(_ template: String, _ bindings: [Binding?]) {
894+
public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
881895
self.template = template
882896
self.bindings = bindings
897+
self.alias = alias
883898
}
884899

885900
}
@@ -888,10 +903,12 @@ public struct Delete : ExpressionType {
888903

889904
public var template: String
890905
public var bindings: [Binding?]
906+
public var alias: String?
891907

892-
public init(_ template: String, _ bindings: [Binding?]) {
908+
public init(_ template: String, _ bindings: [Binding?], _ alias: String? = nil) {
893909
self.template = template
894910
self.bindings = bindings
911+
self.alias = alias
895912
}
896913

897914
}
@@ -963,7 +980,6 @@ extension Connection {
963980
try expandGlob(true)(q)
964981
continue column
965982
}
966-
throw QueryError.noSuchTable(name: namespace)
967983
}
968984
throw QueryError.noSuchTable(name: namespace)
969985
}
@@ -973,7 +989,7 @@ extension Connection {
973989
continue
974990
}
975991

976-
columnNames[each.expression.template] = idx
992+
columnNames[each.expression.alias ?? each.expression.template] = idx
977993
idx += 1
978994
}
979995
return columnNames

Tests/SQLiteTests/ConnectionTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class ConnectionTests : SQLiteTestCase {
4545
let db = try! Connection("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3")
4646
XCTAssertEqual("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3", db.description)
4747
}
48+
49+
func test_init_withOpenFlags_returnsURIConnection() {
50+
let db = try! Connection("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3", flags: [.sharedcache, .create, .readwrite])
51+
XCTAssertEqual("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3", db.description)
52+
}
4853

4954
func test_readonly_returnsFalseOnReadWriteConnections() {
5055
XCTAssertFalse(db.readonly)

0 commit comments

Comments
 (0)