Port to stable diesel API for user defined sqlite functions

This commit is contained in:
Magnus Hoff 2018-06-13 22:46:53 +02:00
parent a582d3a627
commit d577eabc9b
2 changed files with 17 additions and 21 deletions

View file

@ -1,6 +1,6 @@
#[macro_use] extern crate quote; #[macro_use] extern crate quote;
#[macro_use] extern crate diesel;
extern crate diesel_migrations; extern crate diesel_migrations;
extern crate diesel;
extern crate walkdir; extern crate walkdir;
use diesel::Connection; use diesel::Connection;
@ -11,9 +11,10 @@ use std::io::prelude::*;
use std::path::Path; use std::path::Path;
use walkdir::WalkDir; use walkdir::WalkDir;
use std::ffi::CString; #[allow(dead_code)]
fn markdown_to_fts(_: &::diesel::sqlite::Context) -> CString { mod sqlfunc {
panic!("Should never be called when running migrations on build.db") use diesel::sql_types::Text;
sql_function!(fn markdown_to_fts(text: Text) -> Text);
} }
fn main() { fn main() {
@ -23,7 +24,7 @@ fn main() {
let _ignore_failure = std::fs::remove_file(db_path); let _ignore_failure = std::fs::remove_file(db_path);
let mut connection = SqliteConnection::establish(db_path) let connection = SqliteConnection::establish(db_path)
.expect(&format!("Error esablishing a database connection to {}", db_path)); .expect(&format!("Error esablishing a database connection to {}", db_path));
// Integer is a dummy placeholder. Compiling fails when passing (). // Integer is a dummy placeholder. Compiling fails when passing ().
@ -31,12 +32,7 @@ fn main() {
.execute(&connection) .execute(&connection)
.expect("Should be able to enable foreign keys"); .expect("Should be able to enable foreign keys");
connection.create_scalar_function( sqlfunc::markdown_to_fts::register_impl(&connection, |_: String| -> String { unreachable!() }).unwrap();
"markdown_to_fts",
1,
true,
markdown_to_fts,
).unwrap();
diesel_migrations::run_pending_migrations(&connection).unwrap(); diesel_migrations::run_pending_migrations(&connection).unwrap();

View file

@ -9,11 +9,10 @@ embed_migrations!();
#[derive(Debug)] #[derive(Debug)]
struct SqliteInitializer; struct SqliteInitializer;
use std::ffi::CString; #[allow(dead_code)]
mod sqlfunc {
fn markdown_to_fts(ctx: &::diesel::sqlite::Context) -> CString { use diesel::sql_types::Text;
use rendering; sql_function!(fn markdown_to_fts(text: Text) -> Text);
CString::new(rendering::render_markdown_for_fts(&ctx.get::<String>(0))).unwrap()
} }
impl CustomizeConnection<SqliteConnection, r2d2_diesel::Error> for SqliteInitializer { impl CustomizeConnection<SqliteConnection, r2d2_diesel::Error> for SqliteInitializer {
@ -22,11 +21,12 @@ impl CustomizeConnection<SqliteConnection, r2d2_diesel::Error> for SqliteInitial
.execute(conn) .execute(conn)
.map_err(|x| r2d2_diesel::Error::QueryError(x))?; .map_err(|x| r2d2_diesel::Error::QueryError(x))?;
conn.create_scalar_function( sqlfunc::markdown_to_fts::register_impl(
"markdown_to_fts", conn,
1, |text: String| {
true, use rendering;
markdown_to_fts, rendering::render_markdown_for_fts(&text)
}
).map_err(|x| r2d2_diesel::Error::QueryError(x))?; ).map_err(|x| r2d2_diesel::Error::QueryError(x))?;
Ok(()) Ok(())