forked from stephencelis/SQLite.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
132 lines (102 loc) · 3.19 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import SQLite
/// Create an in-memory database
let db = try Connection(.inMemory)
/// enable statement logging
db.trace { print($0) }
/// define a "users" table with some fields
let users = Table("users")
let id = Expression<Int64>("id")
let email = Expression<String>("email") // non-null
let name = Expression<String?>("name") // nullable
/// prepare the query
let statement = users.create { t in
t.column(id, primaryKey: true)
t.column(email, unique: true, check: email.like("%@%"))
t.column(name)
}
/// …and run it
try db.run(statement)
/// insert "alice"
let rowid = try db.run(users.insert(email <- "[email protected]"))
/// insert multiple rows using `insertMany`
let lastRowid = try db.run(users.insertMany([
[email <- "[email protected]"],
[email <- "[email protected]"]
]))
let query = try db.prepare(users)
for user in query {
print("id: \(user[id]), email: \(user[email])")
}
// re-requery just rowid of Alice
let alice = try db.prepare(users.filter(id == rowid))
for user in alice {
print("id: \(user[id]), email: \(user[email])")
}
/// using the `RowIterator` API
let rowIterator = try db.prepareRowIterator(users)
for user in try Array(rowIterator) {
print("id: \(user[id]), email: \(user[email])")
}
/// also with `map()`
let mapRowIterator = try db.prepareRowIterator(users)
let userIds = try mapRowIterator.map { $0[id] }
/// using `failableNext()` on `RowIterator`
let iterator = try db.prepareRowIterator(users)
do {
while let row = try rowIterator.failableNext() {
print(row)
}
} catch {
// Handle error
}
/// define a virtual table for the FTS index
let emails = VirtualTable("emails")
let subject = Expression<String>("subject")
let body = Expression<String?>("body")
/// create the index
try db.run(emails.create(.FTS5(
FTS5Config()
.column(subject)
.column(body)
)))
/// populate with data
try db.run(emails.insert(
subject <- "Hello, world!",
body <- "This is a hello world message."
))
/// run a query
let ftsQuery = try db.prepare(emails.match("hello"))
for row in ftsQuery {
print(row[subject])
}
/// custom aggregations
let reduce: (String, [Binding?]) -> String = { (last, bindings) in
last + " " + (bindings.first as? String ?? "")
}
db.createAggregation("customConcat",
initialValue: "users:",
reduce: reduce,
result: { $0 })
let result = try db.prepare("SELECT customConcat(email) FROM users").scalar() as! String
print(result)
/// schema queries
let schema = db.schema
let objects = try schema.objectDefinitions()
print(objects)
let columns = try schema.columnDefinitions(table: "users")
print(columns)
/// schema alteration
let schemaChanger = SchemaChanger(connection: db)
try schemaChanger.alter(table: "users") { table in
table.add(column: ColumnDefinition(name: "age", type: .INTEGER))
table.rename(column: "email", to: "electronic_mail")
table.drop(column: "name")
}
let changedColumns = try schema.columnDefinitions(table: "users")
print(changedColumns)
let age = Expression<Int?>("age")
let electronicMail = Expression<String>("electronic_mail")
let newRowid = try db.run(users.insert(
electronicMail <- "[email protected]",
age <- 33
))