@@ -1,9 +0,0 @@
|
||||
# For documentation on how to configure this file,
|
||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/schema.rs"
|
||||
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
||||
|
||||
[migrations_directory]
|
||||
dir = "migrations"
|
||||
@@ -1,2 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
drop table words
|
||||
@@ -1,6 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
create table words (
|
||||
id integer not null primary key autoincrement,
|
||||
word varchar not null,
|
||||
info text not null
|
||||
)
|
||||
@@ -1,94 +0,0 @@
|
||||
use diesel::prelude::*;
|
||||
use diesel::connection::SimpleConnection;
|
||||
use diesel::r2d2::{ConnectionManager, CustomizeConnection, Pool};
|
||||
use std::env;
|
||||
use crate::models::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Fuck ();
|
||||
|
||||
impl CustomizeConnection<SqliteConnection, diesel::r2d2::Error> for Fuck {
|
||||
fn on_acquire (&self, conn: &mut SqliteConnection) -> Result<
|
||||
(), diesel::r2d2::Error
|
||||
> {
|
||||
conn.batch_execute ("
|
||||
PRAGMA busy_timeout = 15000;
|
||||
PRAGMA journal_mode = WAL;
|
||||
PRAGMA synchronous = NORMAL;
|
||||
PRAGMA wal_autocheckpoint = 1000;
|
||||
PRAGMA wal_checkpoint(TRUNCATE);
|
||||
PRAGMA cache_size=-2000000
|
||||
").unwrap ();
|
||||
Ok (())
|
||||
}
|
||||
|
||||
fn on_release (&self, _conn: SqliteConnection) {}
|
||||
}
|
||||
|
||||
pub fn get_pool () -> Pool<ConnectionManager<SqliteConnection>> {
|
||||
let url = env::var ("DATABASE_URL")
|
||||
.expect ("DATABASE_URL must be set");
|
||||
let manager = ConnectionManager::<SqliteConnection>::new (url);
|
||||
Pool::builder ()
|
||||
.connection_customizer (Box::new (Fuck ()))
|
||||
.test_on_check_out (true)
|
||||
.build (manager)
|
||||
.expect ("Could not build connection pool")
|
||||
}
|
||||
|
||||
pub fn connect () -> SqliteConnection {
|
||||
let database_url = env::var ("DATABASE_URL")
|
||||
.expect ("DATABASE_URL must be set");
|
||||
let mut conn = SqliteConnection::establish (&database_url)
|
||||
.unwrap_or_else (|_| panic! ("Error connecting to {}", database_url));
|
||||
conn.batch_execute ("
|
||||
PRAGMA journal_mode = WAL; -- better write-concurrency
|
||||
PRAGMA synchronous = NORMAL; -- fsync only in critical moments
|
||||
PRAGMA wal_autocheckpoint = 1000; -- write WAL changes back every 1000 pages, for an in average 1MB WAL file. May affect readers if number is increased
|
||||
PRAGMA wal_checkpoint(TRUNCATE); -- free some space by truncating possibly massive WAL files from the last run.
|
||||
PRAGMA busy_timeout = 5000; -- sleep if the database is busy
|
||||
PRAGMA cache_size=-2000000
|
||||
").unwrap ();
|
||||
conn
|
||||
}
|
||||
|
||||
pub fn get_word (w : &str) -> Vec<Word> {
|
||||
let conn = &mut connect ();
|
||||
use crate::schema::words::dsl::*;
|
||||
let results = words
|
||||
.filter (word.eq (w))
|
||||
.limit (5)
|
||||
.select (Word::as_select ())
|
||||
.load (conn)
|
||||
.expect ("error loading words");
|
||||
for wd in results {
|
||||
println! ("{}", wd.word);
|
||||
}
|
||||
todo! ()
|
||||
}
|
||||
|
||||
pub fn entry_as_word (entry : &serde_json::Value) -> Option<&str> {
|
||||
entry.get ("word")?.as_str ()
|
||||
}
|
||||
|
||||
pub fn insert_word (
|
||||
conn : &mut SqliteConnection,
|
||||
entry : serde_json::Value,
|
||||
) -> Option<()> {
|
||||
use crate::schema::words;
|
||||
let word = entry_as_word (&entry)?;
|
||||
let new_word = NewWord {
|
||||
word,
|
||||
info: &entry.to_string (),
|
||||
};
|
||||
diesel::insert_into (words::table)
|
||||
.values (new_word)
|
||||
.execute (conn).map_or_else (
|
||||
|e| {
|
||||
println! ("error for {}: {}", word, e);
|
||||
None
|
||||
},
|
||||
Some
|
||||
)?;
|
||||
Some (())
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
use diesel::prelude::*;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = crate::schema::words)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Word {
|
||||
pub id : i32,
|
||||
pub word : String,
|
||||
pub info : String,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = crate::schema::words)]
|
||||
pub struct NewWord<'a> {
|
||||
pub word : &'a str,
|
||||
pub info : &'a str,
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
// @generated automatically by Diesel CLI.
|
||||
|
||||
diesel::table! {
|
||||
words (id) {
|
||||
id -> Integer,
|
||||
word -> Text,
|
||||
info -> Text,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user