feat(sqlite): support custom database file suffixes
This commit is contained in:
1 parent
25bac0c67a
commit
49705a1a8f
10 files changed
+138
-31
No files matched your search
@@ -2,6 +2,7 @@ use percent_encoding::percent_decode_str;
|
||||
use rusqlite::types::ValueRef;
|
||||
use rusqlite::{Connection, LoadExtensionGuard, OpenFlags};
|
||||
use std::collections::HashSet;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
@@ -10,6 +11,8 @@ use super::file_validator::validate_file_path;
|
||||
use crate::sql::starts_with_executable_sql_keyword;
|
||||
use crate::types::{ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, QueryResult, TableInfo, TriggerInfo};
|
||||
|
||||
const SQLITE_DATABASE_HEADER: &[u8; 16] = b"SQLite format 3\0";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SqliteHandle {
|
||||
conn: Arc<Mutex<Connection>>,
|
||||
@@ -77,6 +80,9 @@ fn open_sqlite_handle(
|
||||
if !is_memory && create_if_missing {
|
||||
ensure_parent_dir(path)?;
|
||||
}
|
||||
if !is_memory && !is_network_path(path) {
|
||||
validate_existing_sqlite_file(path)?;
|
||||
}
|
||||
|
||||
let conn = if is_memory {
|
||||
Connection::open_in_memory().map_err(|e| format!("SQLite connection failed: {e}"))?
|
||||
@@ -100,6 +106,31 @@ fn open_sqlite_handle(
|
||||
Ok(SqliteHandle { conn: Arc::new(Mutex::new(conn)) })
|
||||
}
|
||||
|
||||
pub fn path_has_sqlite_header(path: &Path) -> Result<bool, String> {
|
||||
let mut file = std::fs::File::open(path).map_err(|e| format!("failed to open file: {e}"))?;
|
||||
let mut header = [0_u8; 16];
|
||||
match file.read_exact(&mut header) {
|
||||
Ok(()) => Ok(&header == SQLITE_DATABASE_HEADER),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => Ok(false),
|
||||
Err(e) => Err(format!("failed to read file header: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_existing_sqlite_file(path: &str) -> Result<(), String> {
|
||||
let path = Path::new(path);
|
||||
if !path.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
let metadata = path.metadata().map_err(|e| format!("failed to inspect SQLite database file: {e}"))?;
|
||||
if metadata.len() == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
if path_has_sqlite_header(path)? {
|
||||
return Ok(());
|
||||
}
|
||||
Err("Selected file is not a valid SQLite database file.".to_string())
|
||||
}
|
||||
|
||||
fn load_sqlite_extensions(conn: &Connection, extensions: &[SqliteExtensionSpec]) -> Result<(), String> {
|
||||
if extensions.is_empty() {
|
||||
return Ok(());
|
||||
@@ -186,6 +217,48 @@ mod tests {
|
||||
assert_eq!(result.rows[0][0], serde_json::json!("Ada"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_if_missing_rejects_existing_non_sqlite_file() {
|
||||
let path = std::env::temp_dir().join(format!("dbx-not-sqlite-{}.png", uuid::Uuid::new_v4()));
|
||||
std::fs::write(&path, b"\x89PNG\r\n\x1a\nnot sqlite").unwrap();
|
||||
|
||||
let err = match connect_path_create_if_missing(path.to_str().unwrap()).await {
|
||||
Ok(_) => panic!("non-SQLite file should be rejected"),
|
||||
Err(err) => err,
|
||||
};
|
||||
|
||||
assert!(err.contains("not a valid SQLite database"));
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_if_missing_allows_empty_custom_suffix_file() {
|
||||
let path = std::env::temp_dir().join(format!("dbx-empty-sqlite-{}.conf", uuid::Uuid::new_v4()));
|
||||
std::fs::write(&path, b"").unwrap();
|
||||
|
||||
let pool = connect_path_create_if_missing(path.to_str().unwrap()).await.expect("empty file can become SQLite");
|
||||
execute_query(&pool, "CREATE TABLE t (id INTEGER);").await.expect("write sqlite schema");
|
||||
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_if_missing_allows_sqlite_database_with_custom_suffix() {
|
||||
let path = std::env::temp_dir().join(format!("dbx-custom-sqlite-{}.conf", uuid::Uuid::new_v4()));
|
||||
{
|
||||
let pool = connect_path_create_if_missing(path.to_str().unwrap()).await.expect("create sqlite");
|
||||
execute_query(&pool, "CREATE TABLE t (id INTEGER);").await.expect("write sqlite schema");
|
||||
}
|
||||
|
||||
let reopened = connect_path_create_if_missing(path.to_str().unwrap()).await.expect("reopen sqlite");
|
||||
let result = execute_query(&reopened, "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 't';")
|
||||
.await
|
||||
.expect("query sqlite schema");
|
||||
assert_eq!(result.rows[0][0], serde_json::json!("t"));
|
||||
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlite_extension_specs_parse_repeated_and_multiline_url_params() {
|
||||
let params = "cache=shared&sqlite_extension=%2Fopt%2Fregexp.dylib&sqlite_extensions=%2Fopt%2Ftext.dylib%7Csqlite3_text_init%0A%2Fopt%2Fcrypto.dylib";
|
||||
|
||||
Reference in new issue
Block a user