Skip to main content

reifydb_sqlite/
connection.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::fs;
5
6use rusqlite::{Connection, OpenFlags as SqliteOpenFlags};
7
8use crate::{DbPath, OpenFlags, error::SqliteError};
9
10pub fn connect(path: &DbPath, flags: SqliteOpenFlags) -> Result<Connection, SqliteError> {
11	match path {
12		DbPath::File(path) => {
13			let path_str = path.to_string_lossy();
14			let is_uri = path_str.contains(':');
15
16			if is_uri {
17				let uri_flags = flags | SqliteOpenFlags::SQLITE_OPEN_URI;
18				let path_string = path_str.to_string();
19				Connection::open_with_flags(path_string, uri_flags).map_err(|source| {
20					SqliteError::Connect {
21						path: path_str.to_string(),
22						source,
23					}
24				})
25			} else {
26				let path_clone = path.clone();
27				Connection::open_with_flags(path_clone, flags).map_err(|source| SqliteError::Connect {
28					path: path.display().to_string(),
29					source,
30				})
31			}
32		}
33		DbPath::Tmpfs(path) => {
34			let path_clone = path.clone();
35			Connection::open_with_flags(path_clone, flags).map_err(|source| SqliteError::Connect {
36				path: path.display().to_string(),
37				source,
38			})
39		}
40		DbPath::Memory(path) => {
41			let path_clone = path.clone();
42			Connection::open_with_flags(path_clone, flags).map_err(|source| SqliteError::Connect {
43				path: path.display().to_string(),
44				source,
45			})
46		}
47	}
48}
49
50pub fn resolve_db_path(db_path: DbPath, default_filename: &str) -> DbPath {
51	match db_path {
52		DbPath::Tmpfs(path) => {
53			if let Some(parent) = path.parent() {
54				fs::create_dir_all(parent).ok();
55			}
56			DbPath::Tmpfs(path)
57		}
58		DbPath::Memory(path) => {
59			if let Some(parent) = path.parent() {
60				fs::create_dir_all(parent).ok();
61			}
62			DbPath::Memory(path)
63		}
64		DbPath::File(config_path) => {
65			let is_uri = config_path.to_string_lossy().contains(':');
66			if is_uri {
67				DbPath::File(config_path)
68			} else if config_path.extension().is_none() {
69				fs::create_dir_all(&config_path).ok();
70				DbPath::File(config_path.join(default_filename))
71			} else {
72				if let Some(parent) = config_path.parent() {
73					fs::create_dir_all(parent).ok();
74				}
75				DbPath::File(config_path)
76			}
77		}
78	}
79}
80
81pub fn convert_flags(flags: &OpenFlags) -> SqliteOpenFlags {
82	let mut rusqlite_flags = SqliteOpenFlags::empty();
83
84	if flags.read_write {
85		rusqlite_flags |= SqliteOpenFlags::SQLITE_OPEN_READ_WRITE;
86	}
87	if flags.create {
88		rusqlite_flags |= SqliteOpenFlags::SQLITE_OPEN_CREATE;
89	}
90	if flags.full_mutex {
91		rusqlite_flags |= SqliteOpenFlags::SQLITE_OPEN_FULL_MUTEX;
92	}
93	if flags.no_mutex {
94		rusqlite_flags |= SqliteOpenFlags::SQLITE_OPEN_NO_MUTEX;
95	}
96	if flags.shared_cache {
97		rusqlite_flags |= SqliteOpenFlags::SQLITE_OPEN_SHARED_CACHE;
98	}
99	if flags.private_cache {
100		rusqlite_flags |= SqliteOpenFlags::SQLITE_OPEN_PRIVATE_CACHE;
101	}
102	if flags.uri {
103		rusqlite_flags |= SqliteOpenFlags::SQLITE_OPEN_URI;
104	}
105
106	rusqlite_flags
107}