forked from tursodatabase/turso-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
159 lines (148 loc) · 4.34 KB
/
lib.rs
File metadata and controls
159 lines (148 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
mod rows;
#[allow(dead_code)]
mod statement;
mod types;
use std::{
ffi::{c_char, c_void},
sync::{Arc, OnceLock},
};
extern crate turso_core;
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
use turso_core::{Connection, LimboError};
use crate::types::ResultCode;
static TRACING_GUARD: OnceLock<tracing_appender::non_blocking::WorkerGuard> = OnceLock::new();
/// # Safety
/// Safe to be called from Go with null terminated DSN string.
/// performs null check on the path.
#[unsafe(no_mangle)]
#[allow(clippy::arc_with_non_send_sync)]
pub unsafe extern "C" fn db_open(path: *const c_char) -> *mut c_void {
if path.is_null() {
println!("Path is null");
return std::ptr::null_mut();
}
let path = unsafe { std::ffi::CStr::from_ptr(path) };
let path = path.to_str().unwrap();
let _ = init_tracing();
let indexes = true;
let mvcc = false;
let encryption = true;
let views = false;
let strict = false;
let custom_modules = false;
let autovacuum = false;
let Ok((io, conn)) = Connection::from_uri(
path,
indexes,
mvcc,
views,
strict,
encryption,
custom_modules,
autovacuum,
) else {
panic!("Failed to open connection with path: {path}");
};
TursoConn::new(conn, io).to_ptr()
}
/// # Safety
/// The caller must ensure that ctx is a valid pointer to a TursoConn.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn db_ping(ctx: *mut c_void) -> ResultCode {
let conn = TursoConn::from_ptr(ctx);
match conn.conn.query("SELECT 1") {
Ok(Some(_)) => ResultCode::Ok,
Ok(None) => {
conn.err = Some(LimboError::InternalError(
"Nothing returned for SELECT 1".to_string(),
));
ResultCode::Error
}
Err(e) => {
conn.err = Some(e);
ResultCode::Error
}
}
}
pub fn init_tracing() -> Result<(), std::io::Error> {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let Ok(file) = std::env::var("TURSO_LOG_FILE") else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"TURSO_LOG_FILE not set",
));
};
let (non_blocking, guard) = tracing_appender::non_blocking(
std::fs::File::options()
.append(true)
.create(true)
.open(file)?,
);
let _ = tracing_subscriber::registry()
.with(filter)
.with(
tracing_subscriber::fmt::layer()
.with_writer(non_blocking)
.with_line_number(true)
.with_thread_ids(true)
.with_ansi(false),
)
.try_init();
TRACING_GUARD.set(guard).ok();
Ok(())
}
#[allow(dead_code)]
struct TursoConn {
conn: Arc<Connection>,
io: Arc<dyn turso_core::IO>,
err: Option<LimboError>,
}
impl TursoConn {
fn new(conn: Arc<Connection>, io: Arc<dyn turso_core::IO>) -> Self {
TursoConn {
conn,
io,
err: None,
}
}
#[allow(clippy::wrong_self_convention)]
fn to_ptr(self) -> *mut c_void {
Box::into_raw(Box::new(self)) as *mut c_void
}
fn from_ptr(ptr: *mut c_void) -> &'static mut TursoConn {
if ptr.is_null() {
panic!("Null pointer");
}
unsafe { &mut *(ptr as *mut TursoConn) }
}
fn get_error(&mut self) -> *const c_char {
if let Some(err) = &self.err {
let err = format!("{err}");
let c_str = std::ffi::CString::new(err).unwrap();
self.err = None;
c_str.into_raw() as *const c_char
} else {
std::ptr::null()
}
}
}
/// Get the error value from the connection, if any, as a null
/// terminated string. The caller is responsible for freeing the
/// memory with `free_string`.
#[unsafe(no_mangle)]
pub extern "C" fn db_get_error(ctx: *mut c_void) -> *const c_char {
if ctx.is_null() {
return std::ptr::null();
}
let conn = TursoConn::from_ptr(ctx);
conn.get_error()
}
/// Close the database connection
/// # Safety
/// safely frees the connection's memory
#[unsafe(no_mangle)]
pub unsafe extern "C" fn db_close(db: *mut c_void) {
if !db.is_null() {
let _ = unsafe { Box::from_raw(db as *mut TursoConn) };
}
}