AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Rust sqlite async1/27/2024 ![]() ![]() User_id and book_ids are identifiers that represent parameter names. Get_loaned_books and loan_books are idents created from the statement names that can be used to name generated methods It might be used by impl_sql to generate a trait (like include-postgres-sql and include-sqlite-sql do). LibrarySql is a camel-cased identifier derived from the SQL file name. Generated impl_sql Callįor the SQL above include-sql would generate: However, in some cases it could be optional as the following name: meta comment would also auto-terminate the preceding statement. Statements should be terminated with a slash - /. The inner statement comments are allowed and will be discarded by include-sql. However, as they might be used to name method parameters in Rust, include-sql forces them into snake-case. Each parameter starts with : and can be anything the can be an identifier in Rust. :user_id and :book_ids are statement parameters. The rest of the statement doc-comment lines are gathered together to form a Rust doc-comment text for the generated method. ![]() See include-postgres-sql or include-sqlite-sql for an example of how it can be done. impl_sql must be prepared to handle cases when a parameter type should be inferred and generate method parameter that is typed generically. Note that because param: is optional for parameters that are not explicitly described by param: include-sql will create a synthetic parameter description using an inferred type _ for scalar SQL parameters or synthetic generic type for IN list parameters. Note that include-sql expects parameter_type to be a Rust type and will fail if it cannot be parsed as such. It is then converted into * `user_id` - user ID text line and included into the Rust doc-comment for the generated method. For example, this line in SQL: param: user_id: &str - user ID tells include-sql that the type of user_id is &str. Include-sql uses param: to gather parameter types and to generate the Rust doc-comment for the parameter. Text that follows parameter_type is used as a doc-comment for this parameter. It is expressed in parameter_name : parameter_type format. param: is an optional description of a statement parameter.For ? they generate methods that process selected rows, for ! - methods that execute all other - non-select - statements, and for -> - methods that read data from RETURNING statements. When it is absent, an implicit ! will be passed to the impl_sql macro.įor example, include-postgres-sql and include-sqlite-sql recognize ?, !, and -> tags. This tag can be any sequence of Rust punctuation characters as long as they represent a single valid Rust punctuation token. It directs impl_sql to generate a specific implementation. If you want to avoid Rust complaining about it, use the appropriate (snake) case for it. Note that include-sql will use the name as-is. For example, include-postgres-sql and include-sqlite-sql generate a trait method from it. name: a mandatory meta comment that defines an identifier that is used to generate the database access artifact.The latter has a dual purpose - it provides the doc-comment text for the generated method, and it also embeds meta data about the statement that follows it: Each statement must have a preceding doc-comment. name: get_loaned_books? - Returns the list of books loaned to a patron - # Parameters - param: user_id: &str - user ID - SELECT book_title FROM library WHERE loaned_to = : user_id ORDER BY 1 / - name: loan_books! - Updates the book records to reflect loan to a patron - # Parameters - param: book_titles: &str - book titles - param: user_id: &str - user ID - UPDATE library SET loaned_to = : user_id, loaned_on = current_timestamp WHERE book_title IN (: book_titles ) /Īn SQL file can include one or more SQL statements.
0 Comments
Read More
Leave a Reply. |