Skip to content

Commit ce04077

Browse files
committed
Add sqlite module docs
1 parent 092e79e commit ce04077

File tree

1 file changed

+242
-0
lines changed

1 file changed

+242
-0
lines changed

docs/std/db/sqlite.md

+242
Original file line numberDiff line numberDiff line change
@@ -1 +1,243 @@
11
# std.db.sqlite
2+
3+
# std.db.sqlite
4+
5+
The `std.db.sqlite` module provides functions for interacting with SQLite databases, including executing queries, handling transactions, and mapping results to objects.
6+
7+
## Core Functions
8+
9+
## query(sql: string, ...bindings: any)
10+
11+
Executes a SQL query and returns the results as an array of objects.
12+
13+
**Parameters**:
14+
15+
- `sql`: The SQL query string
16+
- `bindings`: Optional parameters to bind to the query
17+
18+
**Return Type**: `Array<Object>`
19+
20+
**Example**:
21+
22+
```rust
23+
use std.db.sqlite;
24+
25+
let results = pg.query("SELECT * FROM users WHERE age > $1", 18);
26+
// Returns an array of objects representing the query results
27+
```
28+
29+
## query_as(class: Class, sql: string, ...bindings: any)
30+
31+
Executes a SQL query and maps the results to instances of the specified class.
32+
33+
**Parameters**:
34+
35+
- `class`: The class to map results to
36+
- `sql`: The SQL query string
37+
- `bindings`: Optional parameters to bind to the query
38+
39+
**Return Type**: `Array<Instance>`
40+
41+
**Example**:
42+
43+
```rust
44+
use std.db.sqlite;
45+
46+
class User {
47+
id: number;
48+
name: string;
49+
age: number;
50+
}
51+
52+
let users = pg.query_as(User, "SELECT * FROM users WHERE age > $1", 18);
53+
// Returns an array of User instances
54+
```
55+
56+
## begin_transaction()
57+
58+
Starts a new database transaction. Returns a Transaction object that provides methods for executing queries within the transaction.
59+
60+
**Return Type**: `Transaction`
61+
62+
**Example**:
63+
64+
```rust
65+
use std.db.sqlite;
66+
67+
let tx = pg.begin_transaction();
68+
// Returns a Transaction object
69+
```
70+
71+
## Transaction Methods
72+
73+
The following methods are available on Transaction objects returned by `begin_transaction()`.
74+
75+
### query(sql: string, ...bindings: any)
76+
77+
Executes a SQL query within the transaction and returns the results as an array of objects.
78+
79+
**Parameters**:
80+
81+
- `sql`: The SQL query string
82+
- `bindings`: Optional parameters to bind to the query
83+
84+
**Return Type**: `Array<Object>`
85+
86+
**Example**:
87+
88+
```rust
89+
use std.db.sqlite;
90+
91+
let tx = pg.begin_transaction();
92+
let results = tx.query("SELECT * FROM users WHERE age > $1", 18);
93+
```
94+
95+
### query_as(class: Class, sql: string, ...bindings: any)
96+
97+
Executes a SQL query within the transaction and maps the results to instances of the specified class.
98+
99+
**Parameters**:
100+
101+
- `class`: The class to map results to
102+
- `sql`: The SQL query string
103+
- `bindings`: Optional parameters to bind to the query
104+
105+
**Return Type**: `Array<Instance>`
106+
107+
**Example**:
108+
109+
```rust
110+
use std.db.sqlite;
111+
112+
class User {
113+
id: number;
114+
name: string;
115+
age: number;
116+
}
117+
118+
let tx = pg.begin_transaction();
119+
let users = tx.query_as(User, "SELECT * FROM users WHERE age > $1", 18);
120+
```
121+
122+
### commit()
123+
124+
Commits the transaction, saving all changes made within the transaction.
125+
126+
**Return Type**: `nil`
127+
128+
**Example**:
129+
130+
```rust
131+
use std.db.sqlite;
132+
133+
let tx = pg.begin_transaction();
134+
// Execute queries...
135+
tx.commit();
136+
```
137+
138+
### rollback()
139+
140+
Rolls back the transaction, discarding all changes made within the transaction.
141+
142+
**Return Type**: `nil`
143+
144+
**Example**:
145+
146+
```rust
147+
use std.db.sqlite;
148+
149+
let tx = pg.begin_transaction();
150+
// Execute queries...
151+
tx.rollback();
152+
```
153+
154+
## Usage Examples
155+
156+
### Basic Query Example
157+
158+
```rust
159+
use std.db.sqlite;
160+
161+
// Simple query
162+
let users = pg.query("SELECT * FROM users WHERE age > $1", 18);
163+
164+
// Iterate through results
165+
for user in users {
166+
print(user.name);
167+
}
168+
```
169+
170+
### Transaction Example
171+
172+
```rust
173+
use std.db.sqlite;
174+
175+
// Start transaction
176+
let tx = pg.begin_transaction();
177+
178+
// Update user balance
179+
tx.query("UPDATE users SET balance = balance - $1 WHERE id = $2", 100, 123);
180+
181+
// Insert transaction record
182+
tx.query("INSERT INTO transactions (user_id, amount) VALUES ($1, $2)", 123, -100);
183+
184+
// Commit if successful
185+
tx.commit();
186+
```
187+
188+
### Typed Query Example
189+
190+
```rust
191+
use std.db.sqlite;
192+
193+
// Define user class
194+
class User {
195+
id: number;
196+
name: string;
197+
email: string;
198+
}
199+
200+
// Query users and map to class
201+
let users = pg.query_as(User, "SELECT * FROM users WHERE active = true");
202+
203+
// Access typed properties
204+
for user in users {
205+
print(`${user.name} <${user.email}>`);
206+
}
207+
```
208+
209+
### Parameter Binding Example
210+
211+
```rust
212+
use std.db.sqlite;
213+
214+
// Query with different parameter types
215+
let results = pg.query(
216+
"SELECT * FROM users WHERE name = $1 AND age > $2 AND active = $3",
217+
"John", // string
218+
25, // number
219+
true // boolean
220+
);
221+
```
222+
223+
### Array Parameter Example
224+
225+
```rust
226+
use std.db.sqlite;
227+
228+
// Query with array parameter
229+
let user_ids = [1, 2, 3, 4];
230+
let users = pg.query("SELECT * FROM users WHERE id = ANY($1)", user_ids);
231+
```
232+
233+
### Date Parameter Example
234+
235+
```rust
236+
use std.db.sqlite;
237+
238+
// Query with date parameter
239+
let users = pg.query(
240+
"SELECT * FROM users WHERE created_at > $1",
241+
"2023-01-01" // Date string in YYYY-MM-DD format
242+
);
243+
```

0 commit comments

Comments
 (0)