0c3847a5b9
`Cargo.toml`: add `column_metadata` to rusqlite's feature list. This pulls in the SQLite `SQLITE_ENABLE_COLUMN_METADATA` compile flag and surfaces `sqlite3_column_table_name` / `sqlite3_column_origin_name` on prepared statements via rusqlite's `Statement::columns_with_metadata()`. `do_run_select` in db.rs now calls a new `resolve_select_column_types(conn, stmt)` helper after `prepare`. The helper walks each result-column's origin metadata; when both `table_name` and `origin_name` come back populated (the result column traces back to a base-table column), it looks up the playground type in `__rdbms_playground_columns`. The per-column types thread through to `format_cell(value, ty)` so the data-table renderer (ADR-0016) gets the same per-type rendering it applies to `show data` results. Effect: ADR-0030 Phase-1 §4.5 (bool SELECT results render as `0` / `1`) is lifted for any bare-column reference whose origin the engine carries through — per ADR-0032 Amendment 1 (2026-05-20 empirical probe), that means all non-recursive CTE bodies, scalar subqueries (aliased or not), derived tables, set ops, and JOINs. Computed projections and recursive-CTE result columns remain typeless (the engine populates no origin), which the renderer handles via neutral alignment. The lookup is engine-driven verbatim — no grammar-side structural classification (ADR-0032 Amendment 1 replaces §12's original "structurally a single column reference" rule with "trust column_table_name / column_origin_name"). Tests (3 new in `tests/sql_select.rs`, all green): - `database_run_select_recovers_bool_column_type` — the Phase-1 §4.5 case: `SELECT Active FROM Products` returns `column_types = [Some(Bool)]` and rows render as `true` / `false`. - `database_run_select_recovers_text_type_through_alias` — `SELECT Name AS n FROM Users` remaps the result column name to `n` but the origin metadata still resolves the playground type to `Some(Text)`. - `database_run_select_computed_expression_stays_typeless` — `SELECT Score + 1 FROM T` keeps `column_types[0] = None`, the documented Amendment-1 exception. The CTE pass-through, scalar subquery, set-op, and JOIN cases all work for free given the empirical findings; their behaviour is asserted by the Amendment-1 probe results recorded in the ADR, so no per-case integration tests are duplicated here. Test totals: 1382 → 1385 passing (+3), 0 failed, 1 ignored. Clippy clean.
365 lines
12 KiB
Rust
365 lines
12 KiB
Rust
//! Phase 1 integration tests for the advanced-mode SQL `SELECT`
|
|
//! surface (ADR-0030 / ADR-0031).
|
|
//!
|
|
//! Covers:
|
|
//! - Advanced-mode `select` dispatches as `Command::Select`
|
|
//! through `App::submit` end to end.
|
|
//! - Simple-mode mode gate: `select` is recognised as SQL and
|
|
//! yields the precise "this is SQL" hint instead of executing
|
|
//! (ADR-0030 §2).
|
|
//! - `:` one-shot from simple mode dispatches the SELECT.
|
|
//! - `__rdbms_*` internal-table references are rejected at the
|
|
//! grammar layer (ADR-0030 §6).
|
|
//! - Worker round-trip: a validated SELECT runs against the
|
|
//! database and returns the row set as a [`DataResult`]
|
|
//! (with `column_types: Vec<None>` per ADR-0030 §6).
|
|
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
|
|
|
use rdbms_playground::action::Action;
|
|
use rdbms_playground::app::{App, OutputKind};
|
|
use rdbms_playground::db::Database;
|
|
use rdbms_playground::dsl::{ColumnSpec, Command, Type, Value};
|
|
use rdbms_playground::event::AppEvent;
|
|
use rdbms_playground::mode::Mode;
|
|
use rdbms_playground::persistence::Persistence;
|
|
use rdbms_playground::project;
|
|
|
|
// =================================================================
|
|
// App-level dispatch
|
|
// =================================================================
|
|
|
|
const fn key(code: KeyCode) -> AppEvent {
|
|
AppEvent::Key(KeyEvent {
|
|
code,
|
|
modifiers: KeyModifiers::NONE,
|
|
kind: KeyEventKind::Press,
|
|
state: crossterm::event::KeyEventState::NONE,
|
|
})
|
|
}
|
|
|
|
fn type_str(app: &mut App, s: &str) -> Vec<Action> {
|
|
let mut actions = Vec::new();
|
|
for c in s.chars() {
|
|
actions.extend(app.update(key(KeyCode::Char(c))));
|
|
}
|
|
actions
|
|
}
|
|
|
|
fn submit(app: &mut App) -> Vec<Action> {
|
|
app.update(key(KeyCode::Enter))
|
|
}
|
|
|
|
#[test]
|
|
fn advanced_mode_select_dispatches_as_command_select() {
|
|
let mut app = App::new();
|
|
app.mode = Mode::Advanced;
|
|
type_str(&mut app, "select 1");
|
|
let actions = submit(&mut app);
|
|
match actions.as_slice() {
|
|
[Action::ExecuteDsl {
|
|
command: Command::Select { sql },
|
|
source,
|
|
}] => {
|
|
assert!(
|
|
sql.contains("select 1"),
|
|
"Command::Select carries the validated SQL text: {sql:?}",
|
|
);
|
|
assert!(
|
|
source.contains("select 1"),
|
|
"the source line is preserved for history.log: {source:?}",
|
|
);
|
|
}
|
|
other => panic!("expected one ExecuteDsl(Select); got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn simple_mode_select_yields_sql_hint_and_does_not_dispatch() {
|
|
let mut app = App::new();
|
|
// Default mode is Simple.
|
|
assert_eq!(app.mode, Mode::Simple);
|
|
type_str(&mut app, "select * from anywhere");
|
|
let actions = submit(&mut app);
|
|
assert!(
|
|
actions.is_empty(),
|
|
"simple-mode `select` must not produce a dispatch action; got {actions:?}",
|
|
);
|
|
// The error output spans multiple lines (the message and a
|
|
// caret pointer). The hint catalog key
|
|
// `advanced_mode.sql_in_simple` (ADR-0030 §2) names the
|
|
// input as SQL and points at the recovery paths.
|
|
let error_text: String = app
|
|
.output
|
|
.iter()
|
|
.filter(|l| l.kind == OutputKind::Error)
|
|
.map(|l| l.text.as_str())
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
assert!(
|
|
error_text.contains("SQL"),
|
|
"hint identifies the input as SQL; full error output:\n{error_text}",
|
|
);
|
|
assert!(
|
|
error_text.contains("advanced") && error_text.contains(":"),
|
|
"hint points at the recovery paths; full error output:\n{error_text}",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn colon_one_shot_from_simple_mode_dispatches_select() {
|
|
let mut app = App::new();
|
|
assert_eq!(app.mode, Mode::Simple);
|
|
type_str(&mut app, ":select 1");
|
|
let actions = submit(&mut app);
|
|
// Persistent mode is unchanged.
|
|
assert_eq!(app.mode, Mode::Simple);
|
|
match actions.as_slice() {
|
|
[Action::ExecuteDsl {
|
|
command: Command::Select { sql },
|
|
..
|
|
}] => {
|
|
assert!(
|
|
sql.contains("select 1") && !sql.starts_with(':'),
|
|
"the `:` is stripped before the SQL is queued: {sql:?}",
|
|
);
|
|
}
|
|
other => panic!("expected one ExecuteDsl(Select); got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn advanced_mode_select_from_internal_table_is_rejected() {
|
|
let mut app = App::new();
|
|
app.mode = Mode::Advanced;
|
|
type_str(&mut app, "select * from __rdbms_playground_columns");
|
|
let actions = submit(&mut app);
|
|
assert!(
|
|
actions.is_empty(),
|
|
"internal-table reference must not dispatch; got {actions:?}",
|
|
);
|
|
let error_text: String = app
|
|
.output
|
|
.iter()
|
|
.filter(|l| l.kind == OutputKind::Error)
|
|
.map(|l| l.text.as_str())
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
assert!(
|
|
error_text.contains("internal") || error_text.contains("system"),
|
|
"the rejection names the offence; full error output:\n{error_text}",
|
|
);
|
|
}
|
|
|
|
// =================================================================
|
|
// Worker round-trip — actual SQL execution
|
|
// =================================================================
|
|
|
|
fn rt() -> tokio::runtime::Runtime {
|
|
tokio::runtime::Builder::new_current_thread()
|
|
.enable_all()
|
|
.build()
|
|
.expect("tokio rt")
|
|
}
|
|
|
|
fn open_project_db() -> (project::Project, Database, tempfile::TempDir) {
|
|
let dir = tempfile::tempdir().expect("create tempdir");
|
|
let project =
|
|
project::open_or_create(None, Some(dir.path())).expect("open or create project");
|
|
let persistence = Persistence::new(project.path().to_path_buf());
|
|
let db = Database::open_with_persistence(project.db_path(), persistence)
|
|
.expect("open db with persistence");
|
|
(project, db, dir)
|
|
}
|
|
|
|
#[test]
|
|
fn database_run_select_constant_returns_a_single_row() {
|
|
let (_p, db, _dir) = open_project_db();
|
|
let data = rt()
|
|
.block_on(db.run_select(
|
|
"select 1".to_string(),
|
|
Some("select 1".to_string()),
|
|
))
|
|
.expect("`select 1` runs clean");
|
|
assert_eq!(data.rows.len(), 1, "one result row");
|
|
assert_eq!(data.rows[0].len(), 1, "one column");
|
|
assert_eq!(
|
|
data.rows[0][0].as_deref(),
|
|
Some("1"),
|
|
"the literal `1` round-trips as a single integer cell",
|
|
);
|
|
// ADR-0030 §6: a SELECT's result columns carry no playground
|
|
// type — every entry is `None` (computed expressions render
|
|
// with neutral alignment in the data-table renderer).
|
|
assert!(
|
|
data.column_types.iter().all(Option::is_none),
|
|
"all result column types are None: {:?}",
|
|
data.column_types,
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn database_run_select_from_user_table_returns_inserted_rows() {
|
|
let (_p, db, _dir) = open_project_db();
|
|
let rt = rt();
|
|
rt.block_on(async {
|
|
db.create_table(
|
|
"T".to_string(),
|
|
vec![
|
|
ColumnSpec::new("id", Type::Serial),
|
|
ColumnSpec::new("Name", Type::Text),
|
|
],
|
|
vec!["id".to_string()],
|
|
None,
|
|
)
|
|
.await
|
|
.expect("create table");
|
|
db.insert(
|
|
"T".to_string(),
|
|
None,
|
|
vec![Value::Text("Ada".to_string())],
|
|
None,
|
|
)
|
|
.await
|
|
.expect("insert row");
|
|
});
|
|
let data = rt
|
|
.block_on(db.run_select("select Name from T".to_string(), None))
|
|
.expect("SELECT runs");
|
|
assert_eq!(data.rows.len(), 1);
|
|
assert_eq!(data.rows[0][0].as_deref(), Some("Ada"));
|
|
assert_eq!(data.columns, vec!["Name".to_string()]);
|
|
}
|
|
|
|
// ---- ADR-0032 §12 + Amendment 1: column-origin type recovery ----
|
|
|
|
#[test]
|
|
fn database_run_select_recovers_bool_column_type() {
|
|
// Lifts Phase-1 §4.5: `SELECT is_active FROM products`
|
|
// previously rendered the bool as `0` / `1`. With the
|
|
// engine's column-origin metadata wired through, the
|
|
// result carries `Some(Type::Bool)` and the renderer
|
|
// formats it as `true` / `false`.
|
|
let (_p, db, _dir) = open_project_db();
|
|
let rt = rt();
|
|
rt.block_on(async {
|
|
db.create_table(
|
|
"Products".to_string(),
|
|
vec![
|
|
ColumnSpec::new("id", Type::Serial),
|
|
ColumnSpec::new("Active", Type::Bool),
|
|
],
|
|
vec!["id".to_string()],
|
|
None,
|
|
)
|
|
.await
|
|
.expect("create table");
|
|
db.insert(
|
|
"Products".to_string(),
|
|
None,
|
|
vec![Value::Bool(true)],
|
|
None,
|
|
)
|
|
.await
|
|
.expect("insert row");
|
|
db.insert(
|
|
"Products".to_string(),
|
|
None,
|
|
vec![Value::Bool(false)],
|
|
None,
|
|
)
|
|
.await
|
|
.expect("insert row");
|
|
});
|
|
let data = rt
|
|
.block_on(db.run_select("select Active from Products".to_string(), None))
|
|
.expect("SELECT runs");
|
|
assert_eq!(data.rows.len(), 2);
|
|
assert_eq!(data.column_types, vec![Some(Type::Bool)]);
|
|
assert_eq!(data.rows[0][0].as_deref(), Some("true"));
|
|
assert_eq!(data.rows[1][0].as_deref(), Some("false"));
|
|
}
|
|
|
|
#[test]
|
|
fn database_run_select_recovers_text_type_through_alias() {
|
|
let (_p, db, _dir) = open_project_db();
|
|
let rt = rt();
|
|
rt.block_on(async {
|
|
db.create_table(
|
|
"Users".to_string(),
|
|
vec![
|
|
ColumnSpec::new("id", Type::Serial),
|
|
ColumnSpec::new("Name", Type::Text),
|
|
],
|
|
vec!["id".to_string()],
|
|
None,
|
|
)
|
|
.await
|
|
.expect("create table");
|
|
db.insert(
|
|
"Users".to_string(),
|
|
None,
|
|
vec![Value::Text("Ada".to_string())],
|
|
None,
|
|
)
|
|
.await
|
|
.expect("insert");
|
|
});
|
|
// The `AS n` alias remaps the result column name; the
|
|
// origin metadata still points at `Users.Name`, so the
|
|
// playground type is recovered.
|
|
let data = rt
|
|
.block_on(
|
|
db.run_select("select Name as n from Users".to_string(), None),
|
|
)
|
|
.expect("SELECT runs");
|
|
assert_eq!(data.columns, vec!["n".to_string()]);
|
|
assert_eq!(data.column_types, vec![Some(Type::Text)]);
|
|
}
|
|
|
|
#[test]
|
|
fn database_run_select_computed_expression_stays_typeless() {
|
|
let (_p, db, _dir) = open_project_db();
|
|
let rt = rt();
|
|
rt.block_on(async {
|
|
db.create_table(
|
|
"T".to_string(),
|
|
vec![
|
|
ColumnSpec::new("id", Type::Serial),
|
|
ColumnSpec::new("Score", Type::Int),
|
|
],
|
|
vec!["id".to_string()],
|
|
None,
|
|
)
|
|
.await
|
|
.expect("create table");
|
|
db.insert("T".to_string(), None, vec![Value::Number("5".to_string())], None)
|
|
.await
|
|
.expect("insert");
|
|
});
|
|
let data = rt
|
|
.block_on(db.run_select("select Score + 1 from T".to_string(), None))
|
|
.expect("SELECT runs");
|
|
assert_eq!(data.column_types, vec![None]);
|
|
}
|
|
|
|
#[test]
|
|
fn database_run_select_appends_to_history_when_source_present() {
|
|
let (project, db, _dir) = open_project_db();
|
|
let history_path = project.path().join("history.log");
|
|
// ADR-0030 §11: the literal submitted line lands in
|
|
// history.log so replay re-runs it.
|
|
let _ = rt()
|
|
.block_on(db.run_select(
|
|
"select 1".to_string(),
|
|
Some("select 1".to_string()),
|
|
))
|
|
.expect("SELECT runs");
|
|
let body = std::fs::read_to_string(&history_path)
|
|
.expect("history.log present after a SELECT");
|
|
assert!(
|
|
body.contains("select 1"),
|
|
"history.log records the literal SELECT line: {body:?}",
|
|
);
|
|
}
|