### Start and Rollback a Database Transaction with Welds Source: https://github.com/weldsorm/welds/blob/main/welds-connections/README.md This Rust example demonstrates how to establish a database connection using `welds_connections::connect`, start a transaction with the `TransactStart` trait, and then explicitly roll back the transaction. It showcases the transactional capabilities provided by Welds. ```Rust use welds_connections::{Client, TransactStart}; #[tokio::main] async fn main() -> std::result::Result<(), Box> { let url = "sqlite://./test.sqlite"; let client = welds_connections::connect(url).await?; let transaction = client.begin().await?; transaction.rollback.await?; } ``` -------------------------------- ### Install Welds CLI Tool Source: https://github.com/weldsorm/welds/blob/main/welds-cli/README.md This command installs the `welds-cli` tool globally using Rust's Cargo package manager. It fetches the latest version from crates.io and compiles it, making the `welds` executable available in your system's PATH. ```Bash cargo install welds-cli ``` -------------------------------- ### Perform a Basic Select Query with Welds in Rust Source: https://github.com/weldsorm/welds/blob/main/welds/README.md This example shows how to connect to a database using Welds and perform a basic `WHERE` clause query. It filters products based on their price using the `where_col` and `run` methods, demonstrating a fundamental read operation. ```rust let url = "postgres://postgres:password@localhost:5432"; let client = welds::connections::connect(url).await.unwrap(); let products = Product::where_col(|p| p.price.equal(3.50)).run(&client).await?; ``` -------------------------------- ### Create and Update Records with Welds in Rust Source: https://github.com/weldsorm/welds/blob/main/README.md This example demonstrates how to create a new record and then update an existing one using the `new()` constructor and the `save()` method provided by Welds for database interaction. The `save()` method handles both insertion and updates based on the record's state. ```Rust let client = welds::connections::connect(url).await.unwrap(); let mut cookies = Product::new(); cookies.name = "cookies".to_owned(); // Creates the product cookie cookies.save(&client).await?; cookies.description = "Yum".to_owned(); // Updates the Cookies cookies.save(&client).await?; ``` -------------------------------- ### Perform a Basic Select Query with Welds in Rust Source: https://github.com/weldsorm/welds/blob/main/README.md This example shows how to establish a connection to a PostgreSQL database and perform a basic `SELECT` operation using Welds' `where_col` method to filter records based on a column value. ```Rust let url = "postgres://postgres:password@localhost:5432"; let client = welds::connections::connect(url).await.unwrap(); let products = Product::where_col(|p| p.price.equal(3.50)).run(&client).await?; ``` -------------------------------- ### Fetch Data from Database using Welds Client Trait Source: https://github.com/weldsorm/welds/blob/main/welds-connections/README.md This Rust example illustrates connecting to a SQLite database, executing a parameterized SQL query using the `fetch_rows` method of the `Client` trait, and then iterating over the returned rows to extract and print data. It demonstrates basic data retrieval functionality. ```Rust use welds_connections::{Client, TransactStart}; #[tokio::main] async fn main() -> std::result::Result<(), Box> { let url = "sqlite://./test.sqlite"; let client = welds_connections::connect(url).await?; let sql = "SELECT name from people where name like ?"; let filter = "James%".to_string(); let rows = client.fetch_rows(sql, &[&filter]).await?; for row in rows { let name: String = row.get("name").unwrap(); println!("ROW: {:?}", &name); } } ``` -------------------------------- ### Create and Update Records with Welds in Rust Source: https://github.com/weldsorm/welds/blob/main/welds/README.md This example demonstrates how to create a new record and subsequently update an existing one using the `save` method in Welds. It shows the instantiation of a new model, initial saving, and then modifying and re-saving the same instance to persist changes. ```rust let client = welds::connections::connect(url).await.unwrap(); let mut cookies = Product::new(); cookies.name = "cookies".to_owned(); // Creates the product cookie cookies.save(&client).await?; cookies.description = "Yum".to_owned(); // Updates the Cookies cookies.save(&client).await?; ``` -------------------------------- ### Run Welds MSSQL Tests in Single Thread Source: https://github.com/weldsorm/welds/blob/main/tests/mssql/README.md This command executes the Microsoft SQL Server tests for the 'welds' project. It specifies running tests in a single thread, which is necessary due to migration-related dependencies that can cause issues when run concurrently. This addresses a known limitation in the test setup. ```Shell cargo test -- --test-threads=1 ``` -------------------------------- ### Run Integration Tests with Cargo Source: https://github.com/weldsorm/welds/blob/main/tests/README.md To execute the integration tests for Welds, navigate into each individual database directory within the project. From there, use the `cargo test` command to run the tests. This process assumes a running Docker environment is available. ```Rust cargo test ``` -------------------------------- ### Perform Basic Select Query with Welds in Rust Source: https://github.com/weldsorm/welds/blob/main/welds-macros/README.md Shows how to establish a connection to a PostgreSQL database using `welds::connections` and execute a basic select query with a `WHERE` clause to filter products based on a price condition. ```Rust let url = "postgres://postgres:password@localhost:5432"; let client = welds::connections::postgres::connect(url).await.unwrap(); let products = Product::where_col(|p| p.price.equal(3.50)).run(&client).await?; ``` -------------------------------- ### Generate Welds Database Definition File Source: https://github.com/weldsorm/welds/blob/main/welds-cli/README.md This command instructs `welds-cli` to connect to the database specified by `DATABASE_URL` and generate a `welds.yaml` file. This YAML file contains the schema definition derived from your database, which is then used for code generation. ```Bash welds update ``` -------------------------------- ### Create and Update Records with Welds in Rust Source: https://github.com/weldsorm/welds/blob/main/welds-macros/README.md Demonstrates how to connect to a SQLite database, create a new instance of a `Product` model, save it to the database, and then update an existing field on the same instance and save the changes. ```Rust let client = welds::connections::sqlite::connect(url).await.unwrap(); let mut cookies = Product::new(); cookies.name = "cookies".to_owned(); // Creates the product cookie cookies.save.await(&client)?; cookies.description = "Yum".to_owned(); // Updates the Cookies cookies.save.await(&client)?; ``` -------------------------------- ### Configure Database Connection String Source: https://github.com/weldsorm/welds/blob/main/welds-cli/README.md This command sets the `DATABASE_URL` environment variable, which `welds-cli` uses to connect to your database. It specifies the database type (PostgreSQL), username, password, host, and port for the connection. ```Bash export DATABASE_URL=postgres://postgres:password@localhost:5432 ``` -------------------------------- ### Generate Rust ORM Code Source: https://github.com/weldsorm/welds/blob/main/welds-cli/README.md This command uses the `welds.yaml` definition file to generate Rust code, typically struct definitions, that map to your database tables. The generated files provide the ORM capabilities for interacting with your database in Rust. ```Bash welds generate ``` -------------------------------- ### Set DATABASE_URL for SQLite in Shell Source: https://github.com/weldsorm/welds/blob/main/welds-cli/examples/sqlite_curd/README.md This snippet demonstrates how to set the `DATABASE_URL` environment variable in a Unix-like shell environment. It configures a SQLite database connection pointing to a file named `test.sqlite` located in the current working directory (`$PWD`). This is typically used for local development or testing purposes. ```Shell export DATABASE_URL="sqlite://$PWD/test.sqlite" ``` -------------------------------- ### Welds Connection Features for MSSQL (Tiberius) Source: https://github.com/weldsorm/welds/blob/main/welds-macros/README.md Lists the specific features required for the `welds-connections` crate when working with MSSQL (Tiberius) to enable support for external types from crates like `chrono`, `time`, `rust_decimal`, and `bigdecimal`. ```APIDOC welds-connections features needed for mssql (tiberius): * mssql-chrono * mssql-time * mssql-rust_decimal * mssql-bigdecimal ``` -------------------------------- ### Define Welds ORM Model in Rust Source: https://github.com/weldsorm/welds/blob/main/welds-macros/README.md Demonstrates how to define a data model (`Product` struct) using the `WeldsModel` derive macro. It specifies the database schema, table name, primary key, and defines a `BelongsTo` relationship to another model. ```Rust #[derive(Debug, WeldsModel)] #[welds(schema= "inventory", table = "products")] #[welds(BelongsTo(seller, super::people::People, "seller_id"))] pub struct Product { #[welds(rename = "product_id")] #[welds(primary_key)] pub id: i32, pub name: String, pub seller_id: Option, pub description: Option, pub price: Option, } ``` -------------------------------- ### Define Welds Client Trait for Database Operations Source: https://github.com/weldsorm/welds/blob/main/welds-connections/README.md This Rust trait defines the core interface for database connections and transactions in Welds. It provides methods for executing SQL commands, fetching collections of rows, fetching multiple row sets concurrently, and determining the SQL dialect. ```Rust /// The common trait for database connections and transactions. pub trait Client { /// Execute a sql command. returns the number of rows that were affected async fn execute(&self, sql: &str, params: &[&(dyn Param + Sync)]) -> Result; /// Runs SQL and returns a collection of rows from the database. async fn fetch_rows(&self, sql: &str, params: &[&(dyn Param + Sync)]) -> Result>; /// Run several `fetch_rows` command on the same connection in the connection pool async fn fetch_many(&self, args: &[Fetch]) -> Result>>; // Returns what syntax (dialect) of SQL the backend is expecting fn syntax(&self) -> Syntax; } ``` -------------------------------- ### Define a Welds Model Struct in Rust Source: https://github.com/weldsorm/welds/blob/main/README.md This snippet demonstrates how to define a data model using the `WeldsModel` derive macro in Rust. It maps a struct to a database table, specifies a primary key, and defines a `BelongsTo` relationship for foreign key associations. ```Rust #[derive(Debug, WeldsModel)] #[welds(schema= "inventory", table = "products")] #[welds(BelongsTo(seller, super::people::People, "seller_id"))] pub struct Product { #[welds(rename = "product_id")] #[welds(primary_key)] pub id: i32, pub name: String, pub seller_id: Option, pub description: Option, pub price: Option, } ``` -------------------------------- ### Filter Across Related Tables with Welds in Rust Source: https://github.com/weldsorm/welds/blob/main/welds-macros/README.md Illustrates how to connect to an MSSQL database and perform a query that filters across related tables. It demonstrates using `map_query` to traverse relationships and apply additional `where_col` filters on the related entity. ```Rust let client = welds::connections::mssql::connect(url).await.unwrap(); let sellers = Product::where_col(|product| product.price.equal(3.50)) .map_query(|product| product.seller ) .where_col(|seller| seller.name.ilike("%Nessie%") ) .run(&client).await?; ``` -------------------------------- ### Define a Welds ORM Model in Rust Source: https://github.com/weldsorm/welds/blob/main/welds/README.md This snippet demonstrates how to define a Rust struct as a Welds ORM model. It uses `#[derive(Debug, WeldsModel)]` and `#[welds]` attributes to specify schema, table name, primary key, and define relationships like `BelongsTo` for ORM mapping. ```rust #[derive(Debug, WeldsModel)] #[welds(schema= "inventory", table = "products")] #[welds(BelongsTo(seller, super::people::People, "seller_id"))] pub struct Product { #[welds(rename = "product_id")] #[welds(primary_key)] pub id: i32, pub name: String, pub seller_id: Option, pub description: Option, pub price: Option, } ``` -------------------------------- ### Filter Data Across Related Tables in Welds (Rust) Source: https://github.com/weldsorm/welds/blob/main/README.md This snippet illustrates how to perform a filtered query that spans across related tables using `map_query` to navigate relationships and `where_col` to apply conditions on the joined data, effectively performing a join and filter. ```Rust let client = welds::connections::connect(url).await.unwrap(); let sellers = Product::where_col(|product| product.price.equal(3.50)) .map_query(|product| product.seller ) .where_col(|seller| seller.name.ilike("%Nessie%") ) .run(&client).await?; ``` -------------------------------- ### Filter Data Across Related Tables with Welds in Rust Source: https://github.com/weldsorm/welds/blob/main/welds/README.md This snippet illustrates how to perform a filtered query that spans across related tables in Welds. It demonstrates mapping a query from `Product` to its associated `Seller` and then applying a filter on the seller's name using `map_query` and `where_col`. ```rust let client = welds::connections::connect(url).await.unwrap(); let sellers = Product::where_col(|product| product.price.equal(3.50)) .map_query(|product| product.seller ) .where_col(|seller| seller.name.ilike("%Nessie%") ) .run(&client).await?; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.