Skip to content

Commit c82559f

Browse files
committed
Document FTS
And update a few other documentation-related things along the way. Signed-off-by: Stephen Celis <stephen@stephencelis.com>
1 parent 4521c35 commit c82559f

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

Documentation/Index.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
- [Aggregate SQLite Functions](#aggregate-sqlite-functions)
5454
- [Custom SQL Functions](#custom-sql-functions)
5555
- [Custom Collations](#custom-collations)
56+
- [Full-text Search](#full-text-search)
5657
- [Executing Arbitrary SQL](#executing-arbitrary-sql)
5758
- [Logging](#logging)
5859

@@ -81,13 +82,16 @@ You should now be able to `import SQLite` from any of your target’s source fil
8182

8283
### SQLCipher
8384

84-
To install SQLite.swift with [SQLCipher][] support:
85+
To install SQLite.swift with [SQLCipher](http://sqlcipher.net) support:
8586

8687
1. Make sure the **sqlcipher** working copy is checked out in Xcode. If **sqlcipher.xcodeproj** (in the **Vendor** group) is unavailable (and appears red), go to the **Source Control** menu and select **Check Out sqlcipher…** from the **sqlcipher** menu item.
8788

8889
2. Follow [the instructions above](#installation) with the **SQLiteCipher** target, instead.
8990

90-
[SQLCipher]: http://sqlcipher.net
91+
> _Note:_ By default, SQLCipher compiles [without support for full-text search](https://github.com/sqlcipher/sqlcipher/issues/102). If you intend to use [FTS4](#full-text-search), make sure you add the following to **Other C Flags** in the **Build Settings** of the **sqlcipher** target (in the **sqlcipher.xcodeproj** project):
92+
>
93+
> - `-DSQLITE_ENABLE_FTS4`
94+
> - `-DSQLITE_ENABLE_FTS3_PARENTHESIS`
9195
9296

9397
### Frameworkless Targets
@@ -101,7 +105,7 @@ It’s possible to use SQLite.swift in a target that doesn’t support framework
101105
3. Add the following line to your project’s [bridging header](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_79) (a file usually in the form of `$(TARGET_NAME)-Bridging-Header.h`.
102106

103107
``` swift
104-
#import "SQLite-Bridging.h'
108+
#import "SQLite-Bridging.h"
105109
```
106110

107111
> _Note:_ Adding SQLite.swift source files directly to your application will both remove the `SQLite` module namespace and expose internal functions and variables. Please [report any namespace collisions and bugs](https://github.com/stephencelis/SQLite.swift/issues/new) you encounter.
@@ -144,7 +148,7 @@ var path = NSSearchPathForDirectoriesInDomains(
144148
.ApplicationSupportDirectory, .UserDomainMask, true
145149
).first as String + NSBundle.mainBundle().bundleIdentifier!
146150

147-
// create parent directory iff it doesn't exist
151+
// create parent directory iff it doesnt exist
148152
NSFileManager.defaultManager().createDirectoryAtPath(
149153
path, withIntermediateDirectories: true, attributes: nil, error: nil
150154
)
@@ -205,6 +209,8 @@ SQLite.swift comes with a typed expression layer that directly maps [Swift types
205209
> SQLite.swift defines its own `Blob` structure, which safely wraps the underlying bytes.
206210
>
207211
> See [Custom Types](#custom-types) for more information about extending other classes and structures to work with SQLite.swift.
212+
>
213+
> See [Executing Arbitrary SQL](#executing-arbitrary-sql) to forego the typed layer and execute raw SQL, instead.
208214

209215
These expressions (in the form of the structure, [`Expression`](#expressions)) build on one another and, with a query ([`Query`](#queries)), can create and execute SQL statements.
210216

@@ -1348,9 +1354,45 @@ restaurants.order(collate(.Custom("NODIACRITIC"), name))
13481354
```
13491355

13501356

1357+
## Full-text Search
1358+
1359+
We can create a virtual table using the [FTS4 module](http://www.sqlite.org/fts3.html) by calling `create(vtable:)` on a database connection.
1360+
1361+
``` swift
1362+
let emails = db["emails"]
1363+
let subject = Expression<String>("subject")
1364+
let body = Expression<String>("body")
1365+
1366+
db.create(vtable: emails, using: fts4(subject, body))
1367+
// CREATE VIRTUAL TABLE "emails" USING fts4("subject", "body")
1368+
```
1369+
1370+
We can specify a [tokenizer](http://www.sqlite.org/fts3.html#tokenizer) using the `tokenize` parameter.
1371+
1372+
``` swift
1373+
db.create(vtable: emails, using: fts4([subject, body], tokenize: .Porter))
1374+
// CREATE VIRTUAL TABLE "emails" USING fts4("subject", "body", tokenize=porter)
1375+
```
1376+
1377+
Once we insert a few rows, we can search using the `match` function, which takes a table or column as its first argument and a query string as its second.
1378+
1379+
``` swift
1380+
emails.insert(
1381+
subject <- "Just Checking In",
1382+
body <- "Hey, I was just wondering...did you get my last email?"
1383+
)!
1384+
1385+
emails.filter(match(emails, "wonder*"))
1386+
// SELECT * FROM "emails" WHERE "emails" MATCH 'wonder*'
1387+
1388+
emails.filter(match(subject, "Re:*"))
1389+
// SELECT * FROM "emails" WHERE "subject" MATCH 'Re:*'
1390+
```
1391+
1392+
13511393
## Executing Arbitrary SQL
13521394

1353-
Though we recommend you stick with SQLite.swift’s type-safe system whenever possible, it is possible to simply and safely prepare and execute raw SQL statements via a `Database` connection using the following functions.
1395+
Though we recommend you stick with SQLite.swift’s [type-safe system](#building-type-safe-sql) whenever possible, it is possible to simply and safely prepare and execute raw SQL statements via a `Database` connection using the following functions.
13541396

13551397
- `execute` runs an arbitrary number of SQL statements as a convenience.
13561398

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ syntax _and_ intent.
1919
- A lightweight, uncomplicated query and parameter binding interface
2020
- Transactions with implicit commit/rollback
2121
- Developer-friendly error handling and debugging
22+
- [Full-text search][] support
2223
- [SQLCipher](#sqlcipher) support
2324
- [Well-documented][See Documentation]
2425
- Extensively tested
2526

27+
[Full-text search]: Documentation/Index.md#full-text-search
2628
[See Documentation]: Documentation/Index.md#sqliteswift-documentation
2729

2830

0 commit comments

Comments
 (0)