Skip to content
Extraits de code Groupes Projets
Vérifiée Valider efda09e0 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

RUST-DB: Expose creation and deletion symbols as C symbols

parent 76221408
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -305,6 +305,7 @@ endif()
target_include_directories(lkt PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/inc)
target_include_directories(lektord PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/inc
${CMAKE_CURRENT_SOURCE_DIR}/src/rust/liblektor-rs/inc
${CURL_INCLUDE_DIRS}
)
......
......@@ -9,6 +9,8 @@
#include <lektor/launch.h>
#include <lektor/logfile.h>
#include <liblektor-rs/database.h>
#include <wait.h>
#include <spawn.h>
#include <libgen.h>
......
#if !defined(LIBLEKTOR_RS_DATABASE___)
#define LIBLEKTOR_RS_DATABASE___
#if defined(__cplusplus)
extern "C" {
#endif
struct lkt_sqlite_connection;
typedef struct lkt_sqlite_connection lkt_sqlite_connection;
lkt_sqlite_connection *lkt_database_establish_connection(const char *);
void lkt_database_close_connection(lkt_sqlite_connection *const);
#if defined(__cplusplus)
}
#endif
#endif // LIBLEKTOR_RS_DATABASE___
//! Database implementation in rust for lektor.
pub(self) use diesel::prelude::*;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
pub(self) use log::*;
pub mod models;
pub mod schema;
pub mod unsafe_interface;
/// The migrations!
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
......
//! Models used for querying, inserting or updating the database.
use crate::database::{schema::*, *};
#[derive(Insertable)]
......
//! An unsafe interface around the rust implementation of the databse to be able
//! to call it from C or C++ code.
//!
//! Be carefull when naming things because those names might collide with things
//! defined in lektor's C code...
use super::*;
use std::mem::ManuallyDrop;
/// Wrap the [`establish_connection`] function. On error log the message and
/// return a [`std::ptr::null_mut`].
#[no_mangle]
pub unsafe extern "C" fn lkt_database_establish_connection(
path: *const u8,
) -> *mut SqliteConnection {
let mut path_len = 0;
while *path.offset(path_len) != 0 {
path_len += 1
}
let len = path_len as usize;
let path = ManuallyDrop::new(String::from_raw_parts(path as *mut _, len, len));
match establish_connection(&path[..]) {
Ok(conn) => Box::leak(Box::new(conn)) as *mut _,
Err(err) => {
error!("failed to establish connexion to {}: {err}", &path[..]);
std::ptr::null_mut()
}
}
}
/// Free a database created by [`lkt_database_establish_connection`]. If a null
/// pointer is passed to the function log the error and do nothing. If the
/// passed pointer was not obtained by the correct function the behaviour is
/// undefined.
#[no_mangle]
pub unsafe extern "C" fn lkt_database_close_connection(db: *mut SqliteConnection) {
if db == std::ptr::null_mut() {
error!("can't clost a connexion to a null database!")
} else {
let db = Box::from_raw(db);
drop(db);
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter