Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31db68c

Browse files
committed
⬆️ use include-sql 0.3
1 parent 19eb8a5 commit 31db68c

14 files changed

Lines changed: 450 additions & 94 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repository = "https://github.com/quietboil/include-sqlite-sql"
1111
homepage = "https://quietboil.github.io/include-sqlite-sql"
1212

1313
[dependencies]
14-
include-sql = "0.2"
14+
include-sql = "0.3"
1515

1616
[dev-dependencies]
1717
rusqlite = ">= 0.25"

docs/index.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Add `include-sqlite-sql` as a dependency:
66

77
```toml
88
[dependencies]
9-
include-sqlite-sql = "0.1"
9+
include-sqlite-sql = "0.2"
1010
```
1111

1212
Write your SQL and save it in a file. For example, let's say the following is the content of the `library.sql` file that is saved in the project's `src` folder:
@@ -19,17 +19,18 @@ Write your SQL and save it in a file. For example, let's say the following is th
1919
SELECT book_title
2020
FROM library
2121
WHERE loaned_to = :user_id
22-
ORDER BY 1;
23-
22+
ORDER BY 1
23+
/
2424
-- name: loan_books!
2525
-- Updates the book records to reflect loan to a patron
2626
-- # Parameters
27+
-- param: book_titles: &str - book titles
2728
-- param: user_id: &str - user ID
28-
-- param: book_ids: u32 - book IDs
2929
UPDATE library
3030
SET loaned_to = :user_id
3131
, loaned_on = current_timestamp
32-
WHERE book_id IN (:book_ids);
32+
WHERE book_title IN (:book_titles)
33+
/
3334
```
3435

3536
And then use it in Rust as:
@@ -41,15 +42,13 @@ use rusqlite::{Result, Connection};
4142
include_sql!("src/library.sql");
4243

4344
fn main() -> Result<()> {
44-
let args : Vec<String> = std::env::args().collect();
45-
let dbpath = &args[1];
46-
let user_id = &args[2];
45+
let db = Connection::open("library.db")?;
4746

48-
let db = Connection::open(dbpath)?;
47+
db.loan_books(&["War and Peace", "Gone With the Wind"], "Sheldon Cooper")?;
4948

50-
db.get_loaned_books(user_id, |row| {
49+
db.get_loaned_books("Sheldon Cooper", |row| {
5150
let book_title : &str = row.get_ref("book_title")?.as_str()?;
52-
println!("{}", book_title);
51+
println!("{book_title}");
5352
Ok(())
5453
})?;
5554

@@ -68,6 +67,7 @@ Please see the **Anatomy of the Included SQL File** in [include-sql][4] document
6867
**include-sqlite-sql** generates 3 variants of database access methods using the following selectors:
6968
* `?` - methods that process rows retrieved by `SELECT`,
7069
* `!` - methods that execute all other non-`SELECT` methods, and
70+
* `&` - methods that execute multiple SQL statements (as a batch), and
7171
* `->` - methods that execute `RETURNING` statements and provide access to returned data.
7272

7373
## Process Selected Rows
@@ -84,7 +84,7 @@ The method with the following signature is generated:
8484

8585
```rust , ignore
8686
fn get_loaned_books<F>(&self, user_id: &str, row_callback: F) -> rusqlite::Result<()>
87-
where F: Fn(&rusqlite::Row<'_>) -> rusqlite::Result<()>;
87+
where F: Fn(&rusqlite::Row) -> rusqlite::Result<()>;
8888
```
8989

9090
Where:
@@ -132,7 +132,7 @@ The method with the following signature is generated:
132132

133133
```rust , ignore
134134
fn add_new_book<F,R>(&self, isbn: &str, book_title: &str, row_callback: F) -> rusqlite::Result<R>
135-
where F: FnOnce(&rusqlite::Row<'_>) -> rusqlite::Result<R>;
135+
where F: FnOnce(&rusqlite::Row) -> rusqlite::Result<R>;
136136
```
137137

138138
# Inferred Parameter Types
@@ -151,7 +151,7 @@ Then the signature of the generated method would be:
151151

152152
```rust , ignore
153153
fn get_loaned_books<F>(&self, user_id: impl rusqlite::ToSql, row_callback: F) -> rusqlite::Result<()>
154-
where F: Fn(&rusqlite::Row<'_>) -> rusqlite::Result<()>;
154+
where F: Fn(&rusqlite::Row) -> rusqlite::Result<()>;
155155
```
156156

157157
For the "IN list" type of parameters **include-sqlite-sql** will generate a method parameter as a slice where each element is the same generic type supplied by **include-sql**:

examples/chinook.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn main() -> Result<()> {
2727
Ok(())
2828
})?;
2929

30-
db.get_customers("CA", &["Apple Inc.", "Google Inc."], |row| {
30+
db.get_customers(&["Apple Inc.", "Google Inc."], "CA", |row| {
3131
let first_name : &str = row.get_ref("first_name")?.as_str()?;
3232
let last_name : &str = row.get_ref("last_name")?.as_str()?;
3333
println!("{}, {}", last_name, first_name);
@@ -42,7 +42,7 @@ fn main() -> Result<()> {
4242
})?;
4343

4444
db.begin_transaction()?;
45-
db.create_new_genre(99, "New Age")?;
45+
db.create_new_genre("New Age", 99)?;
4646
// RETURNING is not available before 3.35.0
4747
println!("sqlite version = {}", rusqlite::version());
4848
let name = db.delete_genre(99, |row| {

examples/chinook.sql

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ SELECT Artist.Name AS artist_name
1414
FROM Album
1515
JOIN Artist ON Artist.ArtistId = Album.ArtistId
1616
WHERE Artist.Name LIKE :artist_name
17-
ORDER BY 1, 2;
17+
ORDER BY 1, 2
18+
/
1819

1920
-- name: count_albums?
2021
-- Returns number of albums for the specified artist(s)
@@ -26,19 +27,21 @@ SELECT Artist.Name AS artist_name
2627
JOIN Artist ON Artist.ArtistId = Album.ArtistId
2728
WHERE Artist.Name LIKE :artist_name
2829
GROUP BY Artist.Name
29-
ORDER BY 2 DESC, 1;
30+
ORDER BY 2 DESC, 1
31+
/
3032

3133
-- name: get_customers?
3234
-- Retrieves names of the customers from the specified state
3335
-- that work at the specified compaies
3436
-- ## Parameters
35-
-- param: state: &str - state abbreviation
3637
-- param: companies: &str - names of companies
38+
-- param: state: &str - state abbreviation
3739
SELECT DISTINCT LastName as last_name, FirstName as first_name
3840
FROM Customer
3941
WHERE State = :state
4042
AND Company IN (:companies)
41-
ORDER BY 1, 2;
43+
ORDER BY 1, 2
44+
/
4245

4346
-- name: get_customers_gen?
4447
-- Retrieves names of the customers from the specified state
@@ -47,17 +50,19 @@ SELECT DISTINCT LastName as last_name, FirstName as first_name
4750
FROM Customer
4851
WHERE State = :state
4952
AND Company IN (:companies)
50-
ORDER BY 1, 2;
53+
ORDER BY 1, 2
54+
/
5155

5256
-- name: create_new_genre!
5357
--
5458
-- Inserts new genre record
5559
--
5660
-- # Parameters
57-
-- param: genre_id: i32 - genre ID
5861
-- param: name: &str - name of the new genre
62+
-- param: genre_id: i32 - genre ID
5963
--
60-
INSERT INTO Genre (GenreId, Name) VALUES (:genre_id, :name);
64+
INSERT INTO Genre (GenreId, Name) VALUES (:genre_id, :name)
65+
/
6166

6267
-- name: delete_genre->
6368
--
@@ -66,12 +71,15 @@ INSERT INTO Genre (GenreId, Name) VALUES (:genre_id, :name);
6671
-- # Parameters
6772
-- param: genre_id: i32 - genre ID
6873
--
69-
DELETE FROM Genre WHERE GenreId = :genre_id RETURNING Name;
74+
DELETE FROM Genre WHERE GenreId = :genre_id RETURNING Name
75+
/
7076

7177
-- name: begin_transaction!
7278
-- Starts database transaction
73-
BEGIN DEFERRED;
79+
BEGIN DEFERRED
80+
/
7481

7582
-- name: rollback!
7683
-- Rolls the current transaction back
77-
ROLLBACK;
84+
ROLLBACK
85+
/

examples/library.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use include_sqlite_sql::{include_sql, impl_sql};
2+
use rusqlite::{Result, Connection};
3+
4+
include_sql!("examples/library.sql");
5+
6+
fn main() -> Result<()> {
7+
let db = Connection::open(":memory:")?;
8+
9+
db.init_library()?;
10+
11+
db.loan_books(&["War and Peace", "Gone With the Wind"], "Sheldon Cooper")?;
12+
db.loan_books(&["The Lord of the Rings", "Master and Commander"], "Leonard Hofstadter")?;
13+
14+
db.get_loaned_books("Sheldon Cooper", |row| {
15+
let book_title : &str = row.get_ref(0)?.as_str()?;
16+
println!("{book_title}");
17+
Ok(())
18+
})?;
19+
20+
println!("---");
21+
22+
db.get_loaned_books("Leonard Hofstadter", |row| {
23+
let book_title : &str = row.get_ref(0)?.as_str()?;
24+
println!("{book_title}");
25+
Ok(())
26+
})?;
27+
28+
Ok(())
29+
}

examples/library.sql

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
-- name: init_library &
2+
--
3+
-- Initializes the demo library
4+
--
5+
CREATE TABLE library (
6+
book_author TEXT,
7+
book_title TEXT,
8+
loaned_to TEXT,
9+
loaned_on DATETIME
10+
);
11+
INSERT INTO library (book_author, book_title) VALUES ('Jane Austen', 'Pride and Prejudice');
12+
INSERT INTO library (book_author, book_title) VALUES ('Charlotte Bronte', 'Jane Eyre');
13+
INSERT INTO library (book_author, book_title) VALUES ('Leo Tolstoy', 'War and Peace');
14+
INSERT INTO library (book_author, book_title) VALUES ('Gustave Flaubert', 'Madame Bovary');
15+
INSERT INTO library (book_author, book_title) VALUES ('George Eliot', 'Middlemarch');
16+
INSERT INTO library (book_author, book_title) VALUES ('John Milton', 'Paradise Lost');
17+
INSERT INTO library (book_author, book_title) VALUES ('Patrick O’Brian', 'Master and Commander');
18+
INSERT INTO library (book_author, book_title) VALUES ('Margaret Mitchell', 'Gone With the Wind');
19+
INSERT INTO library (book_author, book_title) VALUES ('Boris Pasternak', 'Doctor Zhivago');
20+
INSERT INTO library (book_author, book_title) VALUES ('J. R. R. Tolkien', 'The Lord of the Rings');
21+
INSERT INTO library (book_author, book_title) VALUES ('A. A Milne', 'Winnie the Pooh');
22+
/
23+
24+
-- name: get_loaned_books ?
25+
--
26+
-- Returns the list of books loaned to a patron
27+
--
28+
-- # Parameters
29+
--
30+
-- param: user_id: &str - user ID
31+
--
32+
SELECT book_title
33+
FROM library
34+
WHERE loaned_to = :user_id
35+
ORDER BY 1
36+
/
37+
38+
-- name: loan_books!
39+
--
40+
-- Updates the book records to reflect loan to a patron
41+
--
42+
-- # Parameters
43+
--
44+
-- param: book_titles: &str - book titles
45+
-- param: user_id: &str - user ID
46+
--
47+
UPDATE library
48+
SET loaned_to = :user_id
49+
, loaned_on = current_timestamp
50+
WHERE book_title IN (:book_titles)
51+
/

0 commit comments

Comments
 (0)