mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Added filtered name lists and the DLL's enumerations, Opened the query connection read-only, Bumped to 0.4.0
- Protocol 8: ListTopics / ListNodes became one ListNames [kind, topics, nodes], each filter a name or a pattern: the topics a node pushed to, the nodes that pushed to a topic, wildcards included. No storage change; the permissions are the lists' (may_list_topic, may_list_nodes_of) - The DLL: the JSON getters are suffixed AsJson (GetNamesListAsJson, GetQueryResultAsJson); ListNamesA/W replaces ListTopics / ListNodes. Beside them, Win32-like enumerations: EnumerateNames / EnumerateQuery, NextName / NextQueryRow (before the first item at first), getters, CloseEnum (enumerations.rs). They don't outlive their client: FreeClient closes them - C#: EnumerateTopics / EnumerateNodes / EnumerateQuery as IEnumerable (QueryRow for a query), fetched at each foreach, closed in finally - Queries: the connection opens the file read-only (SQLITE_OPEN_READ_ONLY), a fifth guard beside query_only, the read-only statement check, one statement only and the PRAGMA-denying authorizer; a unit test checks the handle is read-only - Python, PureBasic, the tool, docs, guides and revamp.md (step 16) updated; versions 0.4.0 everywhere Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
use crate::{Error, QueryRunner, Reader, Result, SCHEMA_VERSION, Writer};
|
||||
use bakashrot_config::Synchronous;
|
||||
use rusqlite::hooks::{AuthAction, AuthContext, Authorization};
|
||||
use rusqlite::{Connection, OptionalExtension, TransactionBehavior};
|
||||
use rusqlite::{Connection, OpenFlags, OptionalExtension, TransactionBehavior};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -129,13 +129,20 @@ impl Database {
|
||||
Ok(Reader::new(self.connect()?))
|
||||
}
|
||||
|
||||
/// A connection for clients' queries: read-only (`query_only`), and
|
||||
/// every PRAGMA refused from then on (SQLite's authorizer). Some take
|
||||
/// effect when they're prepared, before a statement can be refused
|
||||
/// for what it is: `PRAGMA query_only = 0` turned `query_only` off
|
||||
/// for the connection's next queries.
|
||||
/// A connection for clients' queries: the file opened read-only
|
||||
/// (`SQLITE_OPEN_READONLY`: the file handle can't write), `query_only`,
|
||||
/// and every PRAGMA refused from then on (SQLite's authorizer). Some
|
||||
/// take effect when they're prepared, before a statement can be
|
||||
/// refused for what it is: `PRAGMA query_only = 0` turned
|
||||
/// `query_only` off for the connection's next queries. The database is
|
||||
/// in WAL mode already (the writer made it so), which a read-only
|
||||
/// connection can't change and doesn't need to.
|
||||
pub fn query_runner(&self) -> Result<QueryRunner> {
|
||||
let mut connection = self.connect()?;
|
||||
let flags = OpenFlags::SQLITE_OPEN_READ_ONLY
|
||||
| OpenFlags::SQLITE_OPEN_URI
|
||||
| OpenFlags::SQLITE_OPEN_NO_MUTEX;
|
||||
let mut connection = Connection::open_with_flags(&self.path, flags)?;
|
||||
connection.busy_timeout(BUSY_TIMEOUT)?;
|
||||
connection.pragma_update(None, "query_only", true)?;
|
||||
connection.authorizer(Some(|context: AuthContext<'_>| match context.action {
|
||||
AuthAction::Pragma { .. } => Authorization::Deny,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Clients' read-only SQL (`Query`).
|
||||
//!
|
||||
//! Read-only three ways: the connection has `query_only` on (SQLite
|
||||
//! refuses any write), the statement must be one SQLite says only reads
|
||||
//! Read-only four ways: the file is opened read-only (the handle itself
|
||||
//! can't write), the connection has `query_only` on (SQLite refuses any
|
||||
//! write), the statement must be one SQLite says only reads
|
||||
//! (`sqlite3_stmt_readonly`), and only one statement runs (more is an
|
||||
//! error, not ignored). PRAGMAs are refused as they're prepared
|
||||
//! (`Database::query_runner`'s authorizer): some would change the
|
||||
@@ -96,3 +97,33 @@ fn text(value: ValueRef<'_>) -> Value {
|
||||
ValueRef::Blob(bytes) => Value::String(bytes.iter().map(|b| format!("{b:02x}")).collect()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Database;
|
||||
use bakashrot_config::Synchronous;
|
||||
use rusqlite::MAIN_DB;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// The fifth guard, which the others hide from a test of what's
|
||||
/// refused: SQLite says the file itself is read-only on it.
|
||||
#[test]
|
||||
fn the_file_is_opened_read_only() {
|
||||
let folder = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../target/storage-unit");
|
||||
std::fs::create_dir_all(&folder).unwrap();
|
||||
let path = folder.join(format!("query-{}.sqlite", std::process::id()));
|
||||
let database = Database::open(&path, Synchronous::Normal).unwrap();
|
||||
// The server's writer is always open: the WAL's files are there.
|
||||
let writer = database.writer().unwrap();
|
||||
let runner = database.query_runner().unwrap();
|
||||
assert!(runner.connection.is_readonly(MAIN_DB).unwrap());
|
||||
assert_eq!(
|
||||
runner.run("SELECT 1 AS one").unwrap(),
|
||||
r#"{"columns":["one"],"rows":[["1"]]}"#
|
||||
);
|
||||
drop((runner, writer, database));
|
||||
for suffix in ["", "-wal", "-shm"] {
|
||||
let _ = std::fs::remove_file(format!("{}{suffix}", path.display()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,11 +152,6 @@ impl Reader {
|
||||
self.strings("SELECT DISTINCT topic FROM streams ORDER BY topic")
|
||||
}
|
||||
|
||||
/// Every node with messages (or that had some), in order.
|
||||
pub fn nodes(&self) -> Result<Vec<String>> {
|
||||
self.strings("SELECT DISTINCT node FROM streams ORDER BY node")
|
||||
}
|
||||
|
||||
/// Whether `node` has messages in any topic (or had some).
|
||||
fn has_node(&self, node: &str) -> Result<bool> {
|
||||
self.connection
|
||||
|
||||
@@ -159,7 +159,6 @@ fn pushing_and_reading() {
|
||||
|
||||
// Names, heads.
|
||||
assert_eq!(reader.topics().unwrap(), ["logs", "sensors"]);
|
||||
assert_eq!(reader.nodes().unwrap(), ["garage", "kitchen"]);
|
||||
let heads: Vec<_> = reader
|
||||
.heads()
|
||||
.unwrap()
|
||||
|
||||
Reference in New Issue
Block a user