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

View file

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