feat(duckdb): add connection bootstrap and remote catalogs
This commit is contained in:
41 files changed
+893
-194
No files matched your search
@@ -64,6 +64,19 @@ pub fn connect_path(path: &str) -> Result<Arc<DuckDbConnection>, String> {
|
||||
Ok(Arc::new(DuckDbConnection::new(connection)))
|
||||
}
|
||||
|
||||
/// Runs a connection init script statement-by-statement so a failure can
|
||||
/// point at the offending statement instead of the whole batch.
|
||||
pub fn run_init_script(con: &duckdb::Connection, script: &str) -> Result<(), String> {
|
||||
for (index, statement) in crate::db::duckdb_sql::split_sql_statements(script).iter().enumerate() {
|
||||
if crate::db::duckdb_sql::strip_leading_comments(statement).is_empty() {
|
||||
continue;
|
||||
}
|
||||
con.execute_batch(statement)
|
||||
.map_err(|e| format!("Connection init script statement {} failed: {e}", index + 1))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_duckdb_path(path: &str) -> Result<(), String> {
|
||||
if path.is_empty() {
|
||||
return Err("Database file path cannot be empty".to_string());
|
||||
@@ -168,4 +181,16 @@ mod tests {
|
||||
assert!(std::fs::metadata(&path).expect("database metadata").len() > 0);
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_init_script_executes_duckdb_escape_strings() {
|
||||
let con = connect_path(":memory:").expect("connect in-memory DuckDB");
|
||||
let locked = con.lock().expect("lock connection");
|
||||
|
||||
run_init_script(&locked, r#"CREATE TABLE probe AS SELECT E'it\'s;ok' AS value; SELECT 2;"#)
|
||||
.expect("run init script");
|
||||
let value: String = locked.query_row("SELECT value FROM probe", [], |row| row.get(0)).expect("select value");
|
||||
|
||||
assert_eq!(value, "it's;ok");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
//! Quote-aware helpers for inspecting DuckDB SQL snippets without a full
|
||||
//! parser. Shared by the DuckDB worker (execute-time ATTACH tracking) and the
|
||||
//! connection layer (init-script handling), and kept free of `duckdb-bundled`
|
||||
//! so config-derived metadata works in every build.
|
||||
|
||||
use crate::models::connection::ConnectionConfig;
|
||||
|
||||
/// Splits a SQL script into statements on `;`, ignoring separators inside
|
||||
/// single/double quotes, `--` line comments, and `/* */` block comments.
|
||||
/// Statements are trimmed; empty fragments are dropped.
|
||||
pub fn split_sql_statements(script: &str) -> Vec<String> {
|
||||
let bytes = script.as_bytes();
|
||||
let mut statements = Vec::new();
|
||||
let mut start = 0;
|
||||
let mut i = 0;
|
||||
let mut in_single = false;
|
||||
let mut in_double = false;
|
||||
let mut in_escape_single = false;
|
||||
while i < bytes.len() {
|
||||
match bytes[i] {
|
||||
b'\\' if in_single && in_escape_single => {
|
||||
// DuckDB E/e strings use backslash escapes, so an escaped
|
||||
// quote or semicolon must not change statement boundaries.
|
||||
i = (i + 2).min(bytes.len());
|
||||
continue;
|
||||
}
|
||||
b'\'' if !in_double => {
|
||||
if in_single && i + 1 < bytes.len() && bytes[i + 1] == b'\'' {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if !in_single {
|
||||
in_escape_single = is_escape_string_quote(bytes, i);
|
||||
}
|
||||
in_single = !in_single;
|
||||
if !in_single {
|
||||
in_escape_single = false;
|
||||
}
|
||||
}
|
||||
b'"' if !in_single => {
|
||||
if in_double && i + 1 < bytes.len() && bytes[i + 1] == b'"' {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
in_double = !in_double;
|
||||
}
|
||||
b'-' if !in_single && !in_double && bytes.get(i + 1) == Some(&b'-') => {
|
||||
while i < bytes.len() && bytes[i] != b'\n' {
|
||||
i += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
b'/' if !in_single && !in_double && bytes.get(i + 1) == Some(&b'*') => {
|
||||
i += 2;
|
||||
while i < bytes.len() && !(bytes[i] == b'*' && bytes.get(i + 1) == Some(&b'/')) {
|
||||
i += 1;
|
||||
}
|
||||
i = (i + 2).min(bytes.len());
|
||||
continue;
|
||||
}
|
||||
b'$' if !in_single && !in_double => {
|
||||
if let Some(end) = dollar_quote_end(bytes, i) {
|
||||
i = end;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
b';' if !in_single && !in_double => {
|
||||
push_statement(&mut statements, &script[start..i]);
|
||||
start = i + 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
push_statement(&mut statements, &script[start..]);
|
||||
statements
|
||||
}
|
||||
|
||||
fn is_escape_string_quote(bytes: &[u8], quote_index: usize) -> bool {
|
||||
if quote_index == 0 || !matches!(bytes[quote_index - 1], b'E' | b'e') {
|
||||
return false;
|
||||
}
|
||||
quote_index == 1 || !(bytes[quote_index - 2].is_ascii_alphanumeric() || bytes[quote_index - 2] == b'_')
|
||||
}
|
||||
|
||||
fn push_statement(statements: &mut Vec<String>, fragment: &str) {
|
||||
let trimmed = fragment.trim();
|
||||
if !trimmed.is_empty() {
|
||||
statements.push(trimmed.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the index right after the closing `$tag$` when `start` opens a
|
||||
/// dollar-quoted string (Postgres rules: empty tag or an identifier that does
|
||||
/// not start with a digit, so `$1` placeholders never match). An unterminated
|
||||
/// dollar quote swallows the rest of the script, like an unterminated quote.
|
||||
fn dollar_quote_end(bytes: &[u8], start: usize) -> Option<usize> {
|
||||
let mut j = start + 1;
|
||||
while j < bytes.len() && (bytes[j] == b'_' || bytes[j].is_ascii_alphanumeric()) {
|
||||
j += 1;
|
||||
}
|
||||
if j >= bytes.len() || bytes[j] != b'$' {
|
||||
return None;
|
||||
}
|
||||
if j > start + 1 && bytes[start + 1].is_ascii_digit() {
|
||||
return None;
|
||||
}
|
||||
let tag = &bytes[start..=j];
|
||||
let mut k = j + 1;
|
||||
while k + tag.len() <= bytes.len() {
|
||||
if &bytes[k..k + tag.len()] == tag {
|
||||
return Some(k + tag.len());
|
||||
}
|
||||
k += 1;
|
||||
}
|
||||
Some(bytes.len())
|
||||
}
|
||||
|
||||
/// Returns the alias of an `ATTACH ... AS <name>` statement, or `None` when
|
||||
/// the statement is not an ATTACH or carries no alias. Leading comments are
|
||||
/// skipped so `-- note\nATTACH ...` is still recognized.
|
||||
pub fn attached_name_from_attach_sql(sql: &str) -> Option<String> {
|
||||
let trimmed = strip_leading_comments(sql);
|
||||
let first_word = trimmed.split(|ch: char| ch.is_whitespace() || ch == ';').next().unwrap_or_default();
|
||||
if !first_word.eq_ignore_ascii_case("ATTACH") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let as_index = find_as_keyword_outside_quotes(trimmed)?;
|
||||
parse_identifier_after_as(&trimmed[as_index + 2..])
|
||||
}
|
||||
|
||||
pub(crate) fn strip_leading_comments(sql: &str) -> &str {
|
||||
let mut rest = sql.trim_start();
|
||||
loop {
|
||||
if let Some(after) = rest.strip_prefix("--") {
|
||||
rest = after.split_once('\n').map(|(_, tail)| tail).unwrap_or("").trim_start();
|
||||
} else if let Some(after) = rest.strip_prefix("/*") {
|
||||
rest = after.split_once("*/").map(|(_, tail)| tail).unwrap_or("").trim_start();
|
||||
} else {
|
||||
return rest;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects the aliases of every `ATTACH ... AS <name>` statement in a script.
|
||||
pub fn init_script_attached_names(script: &str) -> Vec<String> {
|
||||
split_sql_statements(script).iter().filter_map(|statement| attached_name_from_attach_sql(statement)).collect()
|
||||
}
|
||||
|
||||
/// Attached-database names for a connection: the configured attach list plus
|
||||
/// aliases attached by the connection init script (case-insensitive dedup).
|
||||
pub fn config_attached_names(config: &ConnectionConfig) -> Vec<String> {
|
||||
let mut names: Vec<String> = config.attached_databases.iter().map(|database| database.name.clone()).collect();
|
||||
if let Some(script) = config.init_script.as_deref() {
|
||||
for name in init_script_attached_names(script) {
|
||||
if !names.iter().any(|existing| existing.eq_ignore_ascii_case(&name)) {
|
||||
names.push(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
names
|
||||
}
|
||||
|
||||
fn find_as_keyword_outside_quotes(sql: &str) -> Option<usize> {
|
||||
let bytes = sql.as_bytes();
|
||||
let mut i = 0;
|
||||
let mut in_single = false;
|
||||
let mut in_double = false;
|
||||
while i < bytes.len() {
|
||||
match bytes[i] {
|
||||
b'\'' if !in_double => {
|
||||
if in_single && i + 1 < bytes.len() && bytes[i + 1] == b'\'' {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
in_single = !in_single;
|
||||
}
|
||||
b'"' if !in_single => {
|
||||
if in_double && i + 1 < bytes.len() && bytes[i + 1] == b'"' {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
in_double = !in_double;
|
||||
}
|
||||
b'a' | b'A' if !in_single && !in_double && i + 1 < bytes.len() => {
|
||||
if (bytes[i + 1] == b's' || bytes[i + 1] == b'S')
|
||||
&& is_sql_word_boundary(bytes.get(i.wrapping_sub(1)).copied())
|
||||
&& is_sql_word_boundary(bytes.get(i + 2).copied())
|
||||
{
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn is_sql_word_boundary(byte: Option<u8>) -> bool {
|
||||
!matches!(byte, Some(b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_'))
|
||||
}
|
||||
|
||||
fn parse_identifier_after_as(input: &str) -> Option<String> {
|
||||
let input = input.trim_start();
|
||||
if input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if let Some(rest) = input.strip_prefix('"') {
|
||||
let mut name = String::new();
|
||||
let mut chars = rest.chars().peekable();
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '"' {
|
||||
if chars.peek() == Some(&'"') {
|
||||
name.push('"');
|
||||
chars.next();
|
||||
continue;
|
||||
}
|
||||
return (!name.trim().is_empty()).then_some(name);
|
||||
}
|
||||
name.push(ch);
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
let name = input.split(|ch: char| ch.is_whitespace() || ch == ';').next().unwrap_or_default().trim();
|
||||
(!name.is_empty()).then(|| name.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn attach_sql_alias_parser_handles_generated_sql() {
|
||||
assert_eq!(
|
||||
attached_name_from_attach_sql("ATTACH 'D:\\tmp\\sales.duckdb' AS \"sales db\";"),
|
||||
Some("sales db".to_string())
|
||||
);
|
||||
assert_eq!(attached_name_from_attach_sql("select 'not attach' as value"), None);
|
||||
assert_eq!(
|
||||
attached_name_from_attach_sql("-- note; with semicolon\n/* block */ ATTACH 'x.duckdb' AS warehouse"),
|
||||
Some("warehouse".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_ignores_separators_in_quotes_and_comments() {
|
||||
// Comment text stays inside the statement (DuckDB parses it); the
|
||||
// splitter only refuses to break on `;` inside quotes or comments.
|
||||
let script = "SET s3_endpoint='host;port'; -- comment; with semicolon\nATTACH 'db;x.duckdb' AS a; /* multi;\nline */ SELECT 1";
|
||||
let statements = split_sql_statements(script);
|
||||
assert_eq!(
|
||||
statements,
|
||||
vec![
|
||||
"SET s3_endpoint='host;port'".to_string(),
|
||||
"-- comment; with semicolon\nATTACH 'db;x.duckdb' AS a".to_string(),
|
||||
"/* multi;\nline */ SELECT 1".to_string()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_ignores_semicolons_in_dollar_quoted_strings() {
|
||||
let script = "SELECT $$a;b$$; SELECT $body$c;d$body$; SELECT 1";
|
||||
assert_eq!(
|
||||
split_sql_statements(script),
|
||||
vec!["SELECT $$a;b$$".to_string(), "SELECT $body$c;d$body$".to_string(), "SELECT 1".to_string()]
|
||||
);
|
||||
// $1 placeholders are not dollar-quote openers
|
||||
assert_eq!(split_sql_statements("SELECT $1; SELECT $2").len(), 2);
|
||||
// unterminated dollar quote swallows the rest instead of splitting it
|
||||
assert_eq!(split_sql_statements("SELECT $$a;b").len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_ignores_escaped_quotes_and_semicolons_in_escape_strings() {
|
||||
let script = r#"SELECT E'it\'s;ok'; SELECT e'path\\;name'; SELECT 2"#;
|
||||
assert_eq!(
|
||||
split_sql_statements(script),
|
||||
vec![r#"SELECT E'it\'s;ok'"#.to_string(), r#"SELECT e'path\\;name'"#.to_string(), "SELECT 2".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_script_names_collects_attach_aliases() {
|
||||
let script = "INSTALL httpfs; LOAD httpfs;\nCREATE SECRET (TYPE s3, KEY_ID 'k', SECRET 's');\nATTACH 'ducklake:postgres:dbname=lake' AS lake;\nATTACH 'sales.duckdb' AS \"sales db\";";
|
||||
assert_eq!(init_script_attached_names(script), vec!["lake".to_string(), "sales db".to_string()]);
|
||||
}
|
||||
}
|
||||
@@ -63,29 +63,37 @@ struct WorkerProcessState {
|
||||
}
|
||||
|
||||
impl DuckDbWorkerClient {
|
||||
pub async fn open(path: String, attached_databases: Vec<AttachedDatabaseConfig>) -> Result<Self, String> {
|
||||
pub async fn open(
|
||||
path: String,
|
||||
attached_databases: Vec<AttachedDatabaseConfig>,
|
||||
init_script: Option<String>,
|
||||
) -> Result<Self, String> {
|
||||
let executable = std::env::current_exe().map_err(|e| e.to_string())?;
|
||||
Self::open_with_executable(executable, path, attached_databases).await
|
||||
Self::open_with_executable(executable, path, attached_databases, init_script).await
|
||||
}
|
||||
|
||||
pub async fn open_with_process_limit(
|
||||
path: String,
|
||||
attached_databases: Vec<AttachedDatabaseConfig>,
|
||||
init_script: Option<String>,
|
||||
process_limit: usize,
|
||||
) -> Result<Self, String> {
|
||||
let executable = std::env::current_exe().map_err(|e| e.to_string())?;
|
||||
Self::open_with_executable_and_process_limit(executable, path, attached_databases, process_limit).await
|
||||
Self::open_with_executable_and_process_limit(executable, path, attached_databases, init_script, process_limit)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn open_with_executable(
|
||||
executable: PathBuf,
|
||||
path: String,
|
||||
attached_databases: Vec<AttachedDatabaseConfig>,
|
||||
init_script: Option<String>,
|
||||
) -> Result<Self, String> {
|
||||
Self::open_with_executable_and_process_limit(
|
||||
executable,
|
||||
path,
|
||||
attached_databases,
|
||||
init_script,
|
||||
DUCKDB_WORKER_MAX_PROCESSES_DEFAULT,
|
||||
)
|
||||
.await
|
||||
@@ -95,12 +103,14 @@ impl DuckDbWorkerClient {
|
||||
executable: PathBuf,
|
||||
path: String,
|
||||
attached_databases: Vec<AttachedDatabaseConfig>,
|
||||
init_script: Option<String>,
|
||||
process_limit: usize,
|
||||
) -> Result<Self, String> {
|
||||
let client = Self::new_unconnected_with_timeouts(
|
||||
executable,
|
||||
path,
|
||||
attached_databases,
|
||||
init_script,
|
||||
process_limit,
|
||||
DEFAULT_WORKER_REQUEST_TIMEOUT,
|
||||
DEFAULT_WORKER_START_WAIT,
|
||||
@@ -114,6 +124,7 @@ impl DuckDbWorkerClient {
|
||||
executable: PathBuf,
|
||||
path: String,
|
||||
attached_databases: Vec<AttachedDatabaseConfig>,
|
||||
init_script: Option<String>,
|
||||
process_limit: usize,
|
||||
request_timeout: Duration,
|
||||
worker_start_timeout: Duration,
|
||||
@@ -128,7 +139,7 @@ impl DuckDbWorkerClient {
|
||||
process_limiter,
|
||||
process_limit,
|
||||
executable,
|
||||
connect_params: DuckDbWorkerConnectParams { path, attached_databases },
|
||||
connect_params: DuckDbWorkerConnectParams { path, attached_databases, init_script },
|
||||
request_timeout,
|
||||
worker_start_timeout,
|
||||
next_id: AtomicU64::new(1),
|
||||
|
||||
@@ -100,6 +100,8 @@ pub struct DuckDbWorkerConnectParams {
|
||||
pub path: String,
|
||||
#[serde(default)]
|
||||
pub attached_databases: Vec<AttachedDatabaseConfig>,
|
||||
#[serde(default)]
|
||||
pub init_script: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
@@ -148,6 +150,7 @@ mod tests {
|
||||
name: "analytics".to_string(),
|
||||
path: "D:/tmp/analytics.duckdb".to_string(),
|
||||
}],
|
||||
init_script: None,
|
||||
},
|
||||
)
|
||||
.expect("serialize request");
|
||||
|
||||
@@ -30,6 +30,20 @@ impl DuckDbWorkerSession {
|
||||
crate::schema::duckdb_attach_database(&locked, &attached.name, &path)?;
|
||||
attached_names.push(attached.name.clone());
|
||||
}
|
||||
if let Some(script) = params.init_script.as_deref() {
|
||||
// Track script attachments by diffing the actual catalog list
|
||||
// instead of parsing the SQL, so ATTACH without an alias,
|
||||
// dynamic statements, and extension-specific syntax all count.
|
||||
let before = duckdb_catalog_list(&locked)?;
|
||||
db::duckdb_driver::run_init_script(&locked, script)?;
|
||||
for name in duckdb_catalog_list(&locked)? {
|
||||
if !before.iter().any(|existing| existing.eq_ignore_ascii_case(&name))
|
||||
&& !attached_names.iter().any(|attached| attached.eq_ignore_ascii_case(&name))
|
||||
{
|
||||
attached_names.push(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.connection = Some(connection);
|
||||
self.attached_names = attached_names;
|
||||
@@ -46,7 +60,7 @@ impl DuckDbWorkerSession {
|
||||
¶ms.sql,
|
||||
params.max_rows,
|
||||
)?;
|
||||
if let Some(name) = duckdb_attached_name_from_attach_sql(¶ms.sql) {
|
||||
if let Some(name) = crate::db::duckdb_sql::attached_name_from_attach_sql(¶ms.sql) {
|
||||
if !self.attached_names.iter().any(|attached| attached.eq_ignore_ascii_case(&name)) {
|
||||
self.attached_names.push(name);
|
||||
}
|
||||
@@ -125,6 +139,12 @@ impl DuckDbWorkerSession {
|
||||
}
|
||||
}
|
||||
|
||||
fn duckdb_catalog_list(con: &duckdb::Connection) -> Result<Vec<String>, String> {
|
||||
let mut stmt = con.prepare("SHOW DATABASES").map_err(|e| e.to_string())?;
|
||||
let rows = stmt.query_map([], |row| row.get::<_, String>(0)).map_err(|e| e.to_string())?;
|
||||
Ok(rows.filter_map(|row| row.ok()).collect())
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct DuckDbWorkerRuntime {
|
||||
session: Arc<Mutex<DuckDbWorkerSession>>,
|
||||
@@ -334,83 +354,6 @@ async fn write_response(stdout: Arc<tokio::sync::Mutex<Stdout>>, response: &Duck
|
||||
let _ = stdout.flush().await;
|
||||
}
|
||||
|
||||
fn duckdb_attached_name_from_attach_sql(sql: &str) -> Option<String> {
|
||||
let trimmed = sql.trim_start();
|
||||
let first_word = trimmed.split(|ch: char| ch.is_whitespace() || ch == ';').next().unwrap_or_default();
|
||||
if !first_word.eq_ignore_ascii_case("ATTACH") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let as_index = find_as_keyword_outside_quotes(trimmed)?;
|
||||
parse_identifier_after_as(&trimmed[as_index + 2..])
|
||||
}
|
||||
|
||||
fn find_as_keyword_outside_quotes(sql: &str) -> Option<usize> {
|
||||
let bytes = sql.as_bytes();
|
||||
let mut i = 0;
|
||||
let mut in_single = false;
|
||||
let mut in_double = false;
|
||||
while i < bytes.len() {
|
||||
match bytes[i] {
|
||||
b'\'' if !in_double => {
|
||||
if in_single && i + 1 < bytes.len() && bytes[i + 1] == b'\'' {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
in_single = !in_single;
|
||||
}
|
||||
b'"' if !in_single => {
|
||||
if in_double && i + 1 < bytes.len() && bytes[i + 1] == b'"' {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
in_double = !in_double;
|
||||
}
|
||||
b'a' | b'A' if !in_single && !in_double && i + 1 < bytes.len() => {
|
||||
if (bytes[i + 1] == b's' || bytes[i + 1] == b'S')
|
||||
&& is_sql_word_boundary(bytes.get(i.wrapping_sub(1)).copied())
|
||||
&& is_sql_word_boundary(bytes.get(i + 2).copied())
|
||||
{
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn is_sql_word_boundary(byte: Option<u8>) -> bool {
|
||||
!matches!(byte, Some(b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_'))
|
||||
}
|
||||
|
||||
fn parse_identifier_after_as(input: &str) -> Option<String> {
|
||||
let input = input.trim_start();
|
||||
if input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if let Some(rest) = input.strip_prefix('"') {
|
||||
let mut name = String::new();
|
||||
let mut chars = rest.chars().peekable();
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '"' {
|
||||
if chars.peek() == Some(&'"') {
|
||||
name.push('"');
|
||||
chars.next();
|
||||
continue;
|
||||
}
|
||||
return (!name.trim().is_empty()).then_some(name);
|
||||
}
|
||||
name.push(ch);
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
let name = input.split(|ch: char| ch.is_whitespace() || ch == ';').next().unwrap_or_default().trim();
|
||||
(!name.is_empty()).then(|| name.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -420,7 +363,11 @@ mod tests {
|
||||
let mut session = DuckDbWorkerSession::default();
|
||||
|
||||
session
|
||||
.connect(DuckDbWorkerConnectParams { path: ":memory:".to_string(), attached_databases: Vec::new() })
|
||||
.connect(DuckDbWorkerConnectParams {
|
||||
path: ":memory:".to_string(),
|
||||
attached_databases: Vec::new(),
|
||||
init_script: None,
|
||||
})
|
||||
.expect("connect");
|
||||
|
||||
assert_eq!(session.list_databases().expect("list databases")[0].name, "main");
|
||||
@@ -430,7 +377,11 @@ mod tests {
|
||||
fn worker_session_executes_select_query() {
|
||||
let mut session = DuckDbWorkerSession::default();
|
||||
session
|
||||
.connect(DuckDbWorkerConnectParams { path: ":memory:".to_string(), attached_databases: Vec::new() })
|
||||
.connect(DuckDbWorkerConnectParams {
|
||||
path: ":memory:".to_string(),
|
||||
attached_databases: Vec::new(),
|
||||
init_script: None,
|
||||
})
|
||||
.expect("connect");
|
||||
|
||||
let result = session
|
||||
@@ -445,7 +396,11 @@ mod tests {
|
||||
fn worker_session_lists_tables_and_columns() {
|
||||
let mut session = DuckDbWorkerSession::default();
|
||||
session
|
||||
.connect(DuckDbWorkerConnectParams { path: ":memory:".to_string(), attached_databases: Vec::new() })
|
||||
.connect(DuckDbWorkerConnectParams {
|
||||
path: ":memory:".to_string(),
|
||||
attached_databases: Vec::new(),
|
||||
init_script: None,
|
||||
})
|
||||
.expect("connect");
|
||||
session
|
||||
.execute(DuckDbWorkerExecuteParams {
|
||||
@@ -484,7 +439,11 @@ mod tests {
|
||||
}
|
||||
let mut session = DuckDbWorkerSession::default();
|
||||
session
|
||||
.connect(DuckDbWorkerConnectParams { path: ":memory:".to_string(), attached_databases: Vec::new() })
|
||||
.connect(DuckDbWorkerConnectParams {
|
||||
path: ":memory:".to_string(),
|
||||
attached_databases: Vec::new(),
|
||||
init_script: None,
|
||||
})
|
||||
.expect("connect");
|
||||
|
||||
session
|
||||
@@ -504,6 +463,79 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_session_runs_init_script_and_tracks_attached_alias() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-duckdb-worker-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&dir).expect("create temp dir");
|
||||
let attached_path = dir.join("warehouse.duckdb");
|
||||
{
|
||||
let con = duckdb::Connection::open(&attached_path).expect("create attached db");
|
||||
con.execute_batch("CREATE TABLE facts (id INTEGER);").expect("create attached table");
|
||||
}
|
||||
let mut session = DuckDbWorkerSession::default();
|
||||
let script = format!(
|
||||
"SET memory_limit='512MB'; -- session tuning\nATTACH '{}' AS warehouse;\nCREATE TABLE staging (id INTEGER);",
|
||||
attached_path.to_string_lossy().replace('\'', "''")
|
||||
);
|
||||
session
|
||||
.connect(DuckDbWorkerConnectParams {
|
||||
path: ":memory:".to_string(),
|
||||
attached_databases: Vec::new(),
|
||||
init_script: Some(script),
|
||||
})
|
||||
.expect("connect");
|
||||
|
||||
assert!(session.attached_names.iter().any(|name| name == "warehouse"));
|
||||
let databases = session.list_databases().expect("list databases");
|
||||
assert!(databases.iter().any(|database| database.name == "warehouse"));
|
||||
let tables = session
|
||||
.list_tables(DuckDbWorkerTableParams { database: "warehouse".to_string(), schema: "main".to_string() })
|
||||
.expect("list tables");
|
||||
assert!(tables.iter().any(|table| table.name == "facts"));
|
||||
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_session_tracks_init_script_attach_without_alias() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-duckdb-worker-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&dir).expect("create temp dir");
|
||||
let attached_path = dir.join("noalias.duckdb");
|
||||
{
|
||||
let con = duckdb::Connection::open(&attached_path).expect("create attached db");
|
||||
con.execute_batch("CREATE TABLE items (id INTEGER);").expect("create attached table");
|
||||
}
|
||||
let mut session = DuckDbWorkerSession::default();
|
||||
session
|
||||
.connect(DuckDbWorkerConnectParams {
|
||||
path: ":memory:".to_string(),
|
||||
attached_databases: Vec::new(),
|
||||
init_script: Some(format!("ATTACH '{}';", attached_path.to_string_lossy().replace('\'', "''"))),
|
||||
})
|
||||
.expect("connect");
|
||||
|
||||
assert!(session.attached_names.iter().any(|name| name == "noalias"), "names: {:?}", session.attached_names);
|
||||
let tables = session
|
||||
.list_tables(DuckDbWorkerTableParams { database: "noalias".to_string(), schema: "main".to_string() })
|
||||
.expect("list tables");
|
||||
assert!(tables.iter().any(|table| table.name == "items"));
|
||||
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_session_reports_failing_init_script_statement() {
|
||||
let mut session = DuckDbWorkerSession::default();
|
||||
let err = session
|
||||
.connect(DuckDbWorkerConnectParams {
|
||||
path: ":memory:".to_string(),
|
||||
attached_databases: Vec::new(),
|
||||
init_script: Some("SELECT 1; SELECT * FROM missing_table;".to_string()),
|
||||
})
|
||||
.expect_err("init script should fail");
|
||||
assert!(err.contains("statement 2"), "unexpected error: {err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_session_tracks_attach_sql_alias() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-duckdb-worker-{}", uuid::Uuid::new_v4()));
|
||||
@@ -515,7 +547,11 @@ mod tests {
|
||||
}
|
||||
let mut session = DuckDbWorkerSession::default();
|
||||
session
|
||||
.connect(DuckDbWorkerConnectParams { path: ":memory:".to_string(), attached_databases: Vec::new() })
|
||||
.connect(DuckDbWorkerConnectParams {
|
||||
path: ":memory:".to_string(),
|
||||
attached_databases: Vec::new(),
|
||||
init_script: None,
|
||||
})
|
||||
.expect("connect");
|
||||
|
||||
session
|
||||
@@ -554,13 +590,4 @@ mod tests {
|
||||
assert!(!response.ok);
|
||||
assert_eq!(response.error.expect("error").code, "duckdb_worker_busy");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attach_sql_alias_parser_handles_generated_sql() {
|
||||
assert_eq!(
|
||||
duckdb_attached_name_from_attach_sql("ATTACH 'D:\\tmp\\sales.duckdb' AS \"sales db\";"),
|
||||
Some("sales db".to_string())
|
||||
);
|
||||
assert_eq!(duckdb_attached_name_from_attach_sql("select 'not attach' as value"), None);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ pub mod clickhouse_driver;
|
||||
pub mod cloudflare_d1;
|
||||
pub use cloudflare_d1 as cloudflare_d1_driver;
|
||||
pub mod duckdb_driver;
|
||||
pub mod duckdb_sql;
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
pub mod duckdb_worker_process;
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
|
||||
@@ -3078,6 +3078,7 @@ mod tests {
|
||||
visible_databases: None,
|
||||
visible_schemas: None,
|
||||
attached_databases: Vec::new(),
|
||||
init_script: None,
|
||||
color: None,
|
||||
transport_layers: Vec::new(),
|
||||
connect_timeout_secs: crate::models::connection::default_connect_timeout_secs(),
|
||||
|
||||
Reference in new issue
Block a user