Skip to content

Commit f1f224e

Browse files
Add examples
1 parent 16ebae8 commit f1f224e

18 files changed

+263
-1
lines changed

Examples/Batch/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Batch
2+
3+
This example demonstrates how to use libSQL to execute a batch of SQL statements.
4+
5+
## Running
6+
7+
Execute the example:
8+
9+
```bash
10+
swift run Batch
11+
```
12+
13+
This will setup a SQLite database, execute a batch of SQL statements, and then query the results.

Examples/Batch/main.swift

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import Foundation
3+
import Libsql
4+
5+
let db = try Database(":memory:")
6+
let conn = try db.connect()
7+
8+
_ = try conn.executeBatch("""
9+
DROP TABLE IF EXISTS users;
10+
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT);
11+
INSERT INTO users VALUES (1, 'first@example.com');
12+
INSERT INTO users VALUES (2, 'second@example.com');
13+
INSERT INTO users VALUES (3, 'third@example.com');
14+
15+
""")
16+
17+
for row in try conn.query("select * from users", [1]) {
18+
print(try row.getInt(0), try row.getString(1))
19+
}
20+

Examples/Local/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db*

Examples/Local/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Local
2+
3+
This example demonstrates how to use libSQL with a local SQLite file.
4+
5+
## Running
6+
7+
Execute the example:
8+
9+
```bash
10+
swift run Local
11+
```
12+
13+
This will setup a local SQLite database, insert some data, and then query the results.

Examples/Local/main.swift

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Libsql
2+
3+
let db = try Database("./local.db")
4+
let conn = try db.connect()
5+
6+
_ = try conn.executeBatch("""
7+
DROP TABLE IF EXISTS users;
8+
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT);
9+
INSERT INTO users VALUES (1, 'first@example.com');
10+
INSERT INTO users VALUES (2, 'second@example.com');
11+
INSERT INTO users VALUES (3, 'third@example.com');
12+
13+
""")
14+
15+
for row in try conn.query("select * from users", [1]) {
16+
print(try row.getInt(0), try row.getString(1))
17+
}
18+

Examples/Memory/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Memory
2+
3+
This example demonstrates how to use libsql with an in-memory SQLite database.
4+
5+
## Running
6+
7+
Execute the example:
8+
9+
```bash
10+
swift run Memory
11+
```
12+
13+
This will create an in-memory SQLite database, insert some data, and then query the results.

Examples/Memory/main.swift

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import Foundation
3+
import Libsql
4+
5+
let db = try Database(":memory:")
6+
let conn = try db.connect()
7+
8+
_ = try conn.executeBatch("""
9+
DROP TABLE IF EXISTS users;
10+
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT);
11+
INSERT INTO users VALUES (1, 'first@example.com');
12+
INSERT INTO users VALUES (2, 'second@example.com');
13+
INSERT INTO users VALUES (3, 'third@example.com');
14+
15+
""")
16+
17+
for row in try conn.query("select * from users", [1]) {
18+
print(try row.getInt(0), try row.getString(1))
19+
}
20+

Examples/Remote/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db*

Examples/Remote/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Remote
2+
3+
This example demonstrates how to use libSQL with a remote database.
4+
5+
## Running
6+
7+
Execute the example:
8+
9+
```bash
10+
TURSO_DATABASE_URL="..." TURSO_AUTH_TOKEN="..." swift run Remote
11+
```
12+
13+
This will connect to a remote SQLite database, insert some data, and then query the results.

Examples/Remote/main.swift

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Libsql
2+
import Foundation
3+
4+
let db = try Database(
5+
path: "./local.db",
6+
url: ProcessInfo.processInfo.environment["TURSO_DATABASE_URL"] ?? "",
7+
authToken: ProcessInfo.processInfo.environment["TURSO_AUTH_TOKEN"] ?? ""
8+
)
9+
let conn = try db.connect()
10+
11+
_ = try conn.executeBatch("""
12+
DROP TABLE IF EXISTS users;
13+
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT);
14+
INSERT INTO users VALUES (1, 'first@example.com');
15+
INSERT INTO users VALUES (2, 'second@example.com');
16+
INSERT INTO users VALUES (3, 'third@example.com');
17+
18+
""")
19+
20+
for row in try conn.query("select * from users", [1]) {
21+
print(try row.getInt(0), try row.getString(1))
22+
}

Examples/Sync/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db*

Examples/Sync/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Sync
2+
3+
This example demonstrates how to use libSQL with a synced database (local file synced with a remote database).
4+
5+
## Running
6+
7+
Execute the example:
8+
9+
```bash
10+
TURSO_DATABASE_URL="..." TURSO_AUTH_TOKEN="..." swift run Sync
11+
```
12+
13+
This will connect to a remote SQLite database, insert some data, and then query the results.

Examples/Sync/main.swift

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Libsql
2+
import Foundation
3+
4+
let db = try Database(
5+
path: "./local.db",
6+
url: ProcessInfo.processInfo.environment["TURSO_DATABASE_URL"] ?? "",
7+
authToken: ProcessInfo.processInfo.environment["TURSO_AUTH_TOKEN"] ?? "",
8+
syncInterval: 1000
9+
10+
)
11+
let conn = try db.connect()
12+
13+
_ = try conn.executeBatch("""
14+
DROP TABLE IF EXISTS users;
15+
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT);
16+
INSERT INTO users VALUES (1, 'first@example.com');
17+
INSERT INTO users VALUES (2, 'second@example.com');
18+
INSERT INTO users VALUES (3, 'third@example.com');
19+
20+
""")
21+
22+
for row in try conn.query("select * from users", [1]) {
23+
print(try row.getInt(0), try row.getString(1))
24+
}

Examples/Transactions/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db*

Examples/Transactions/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Transactions
2+
3+
This example demonstrates how to use transactions with libSQL.
4+
5+
## Running
6+
7+
Execute the example:
8+
9+
```bash
10+
swift run Transactions
11+
```
12+
13+
This example will:
14+
15+
1. Create a new table called `users`.
16+
2. Start a transaction.
17+
3. Insert multiple users within the transaction.
18+
4. Demonstrate how to rollback a transaction.
19+
5. Start another transaction.
20+
6. Insert more users and commit the transaction.
21+
7. Query and display the final state of the `users` table.
22+

Examples/Transactions/main.swift

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Foundation
2+
import Libsql
3+
4+
let db = try Database(":memory:")
5+
let conn = try db.connect()
6+
7+
_ = try conn.executeBatch("""
8+
create table users(id integer primary key autoincrement, name text);
9+
insert into users (name) values ('First Iku Turso');
10+
""")
11+
12+
let tx = try conn.transaction();
13+
14+
let fullNames = ["John Doe", "Mary Smith", "Alice Jones", "Mark Taylor"]
15+
16+
for fullName in fullNames {
17+
_ = try tx.execute(
18+
"insert into users (name) values (?)",
19+
[fullName]
20+
);
21+
}
22+
23+
tx.rollback() // Discards all inserts
24+
25+
_ = try conn.execute("insert into users (name) values (?)", ["Second Iku Turso"])
26+
27+
// Only returns "1 Iku turso", since the transaction was rollbacked.
28+
for row in try conn.query("select * from users", [1]) {
29+
print(try row.getInt(0), try row.getString(1))
30+
}

Examples/local.db

8 KB
Binary file not shown.

Package.swift

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.10
1+
// swift-tools-version: 5.9.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import Foundation
@@ -14,6 +14,7 @@ var package = Package(
1414
.executable(name: "Query", targets: ["Query"]),
1515
.executable(name: "Transaction", targets: ["Transaction"]),
1616
.executable(name: "Vector", targets: ["Vector"]),
17+
.executable(name: "Batch", targets: ["Batch"]),
1718
],
1819
targets: [
1920
.target(name: "Libsql", dependencies: ["CLibsql"]),
@@ -36,5 +37,41 @@ var package = Package(
3637
dependencies: ["Libsql"],
3738
path: "Examples/Vector"
3839
),
40+
.executableTarget(
41+
name: "Batch",
42+
dependencies: ["Libsql"],
43+
path: "Examples/Batch",
44+
exclude: ["README.md"]
45+
),
46+
.executableTarget(
47+
name: "Local",
48+
dependencies: ["Libsql"],
49+
path: "Examples/Local",
50+
exclude: ["README.md", "local.db"]
51+
),
52+
.executableTarget(
53+
name: "Memory",
54+
dependencies: ["Libsql"],
55+
path: "Examples/Memory",
56+
exclude: ["README.md"]
57+
),
58+
.executableTarget(
59+
name: "Remote",
60+
dependencies: ["Libsql"],
61+
path: "Examples/Remote",
62+
exclude: ["README.md", "local.db", "local.db-shm", "local.db-client_wal_index", "local.db-wal"]
63+
),
64+
.executableTarget(
65+
name: "Sync",
66+
dependencies: ["Libsql"],
67+
path: "Examples/Sync",
68+
exclude: ["README.md", "local.db", "local.db-shm", "local.db-client_wal_index", "local.db-wal"]
69+
),
70+
.executableTarget(
71+
name: "Transactions",
72+
dependencies: ["Libsql"],
73+
path: "Examples/Transactions",
74+
exclude: ["README.md", "local.db"]
75+
),
3976
]
4077
)

0 commit comments

Comments
 (0)