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