pub trait ReadOnlyQuery {
// Required method
fn query(&self, sql: &str) -> Result<SelectResult, ReadOnlyError>;
}Expand description
Extension trait for read-only query execution on Database.
This trait provides a query(&self) method that enables executing read-only
SQL queries without requiring mutable access to the database. This is essential
for concurrent read access in multi-connection scenarios.
Required Methods§
Sourcefn query(&self, sql: &str) -> Result<SelectResult, ReadOnlyError>
fn query(&self, sql: &str) -> Result<SelectResult, ReadOnlyError>
Execute a read-only SQL query.
This method parses the SQL string and executes it if it’s a SELECT statement. Any other statement type (INSERT, UPDATE, DELETE, DDL) will return an error.
§Arguments
sql- The SQL query string to execute
§Returns
Ok(SelectResult)- The query results including column names and rowsErr(ReadOnlyError::NotReadOnly)- If the SQL is not a SELECT statementErr(ReadOnlyError::ParseError)- If the SQL cannot be parsedErr(ReadOnlyError::ExecutionError)- If the query execution fails
§Example
use vibesql_executor::readonly::ReadOnlyQuery;
let db = Database::new();
// ... create tables and insert data ...
// Read-only query works with &self (no mutation)
let result = db.query("SELECT * FROM users")?;
for row in &result.rows {
println!("{:?}", row);
}
// DML queries are rejected
let err = db.query("INSERT INTO users VALUES (1, 'test')");
assert!(matches!(err, Err(ReadOnlyError::NotReadOnly { .. })));