feat: add diesel, but not works
This commit is contained in:
2
__database/diesel/.env
Normal file
2
__database/diesel/.env
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
DATABASE_URL=file:test.db
|
||||||
|
|
||||||
100
__database/diesel/Cargo.lock
generated
Normal file
100
__database/diesel/Cargo.lock
generated
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "byteorder"
|
||||||
|
version = "1.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diesel"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"diesel 1.4.5",
|
||||||
|
"dotenv",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diesel"
|
||||||
|
version = "1.4.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3e2de9deab977a153492a1468d1b1c0662c1cf39e5ea87d0c060ecd59ef18d8c"
|
||||||
|
dependencies = [
|
||||||
|
"byteorder",
|
||||||
|
"diesel_derives",
|
||||||
|
"libsqlite3-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diesel_derives"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dotenv"
|
||||||
|
version = "0.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libsqlite3-sys"
|
||||||
|
version = "0.18.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e704a02bcaecd4a08b93a23f6be59d0bd79cd161e0963e9499165a0a35df7bd"
|
||||||
|
dependencies = [
|
||||||
|
"pkg-config",
|
||||||
|
"vcpkg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pkg-config"
|
||||||
|
version = "0.3.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.24"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-xid",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "1.0.60"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-xid",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-xid"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcpkg"
|
||||||
|
version = "0.2.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b00bca6106a5e23f3eee943593759b7fcddb00554332e856d990c893966879fb"
|
||||||
12
__database/diesel/Cargo.toml
Normal file
12
__database/diesel/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "diesel"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
diesel = { version = "1.4.5", features = ["sqlite"] }
|
||||||
|
dotenv = "0.15.0"
|
||||||
|
|
||||||
10
__database/diesel/README.md
Normal file
10
__database/diesel/README.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
!!NOT WORKS...
|
||||||
|
|
||||||
|
cargo install diesel_cli --no-default-features --features sqlite
|
||||||
|
|
||||||
|
|
||||||
|
diesel setup --database-url=file:test.db
|
||||||
|
|
||||||
|
diesel migration generate create_posts
|
||||||
|
|
||||||
|
diesel migration run --database-url=file:test.db
|
||||||
5
__database/diesel/diesel.toml
Normal file
5
__database/diesel/diesel.toml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# For documentation on how to configure this file,
|
||||||
|
# see diesel.rs/guides/configuring-diesel-cli
|
||||||
|
|
||||||
|
[print_schema]
|
||||||
|
file = "src/schema.rs"
|
||||||
0
__database/diesel/migrations/.gitkeep
Normal file
0
__database/diesel/migrations/.gitkeep
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
DROP TABLE posts
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
-- Your SQL goes here
|
||||||
|
CREATE TABLE posts (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
title VARCHAR NOT NULL,
|
||||||
|
body TEXT NOT NULL,
|
||||||
|
published BOOLEAN NOT NULL DEFAULT 'f'
|
||||||
|
)
|
||||||
|
|
||||||
37
__database/diesel/src/main.rs
Normal file
37
__database/diesel/src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate diesel;
|
||||||
|
|
||||||
|
pub mod schema;
|
||||||
|
pub mod models;
|
||||||
|
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use diesel::sqlite::SqliteConnection;
|
||||||
|
use dotenv::dotenv;
|
||||||
|
use std::env;
|
||||||
|
use crate::models::Post;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
use crate::schema::posts::dsl::*;
|
||||||
|
|
||||||
|
let connection = establish_connection();
|
||||||
|
let results = posts.filter(published.eq(true))
|
||||||
|
.limit(5)
|
||||||
|
.load::<Post>(&connection)
|
||||||
|
.expect("Error loading posts");
|
||||||
|
|
||||||
|
println!("Displaying {} posts", results.len());
|
||||||
|
for post in results {
|
||||||
|
println!("{}", post.title);
|
||||||
|
println!("----------\n");
|
||||||
|
println!("{}", post.body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn establish_connection() -> SqliteConnection {
|
||||||
|
dotenv().ok();
|
||||||
|
|
||||||
|
let database_url = env::var("DATABASE_URL")
|
||||||
|
.expect("DATABASE_URL must be set");
|
||||||
|
SqliteConnection::establish(&database_url)
|
||||||
|
.expect(&format!("Error connecting to {}", database_url))
|
||||||
|
}
|
||||||
7
__database/diesel/src/models.rs
Normal file
7
__database/diesel/src/models.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#[derive(Queryable)]
|
||||||
|
pub struct Post {
|
||||||
|
pub id: i32,
|
||||||
|
pub title: String,
|
||||||
|
pub body: String,
|
||||||
|
pub published: bool,
|
||||||
|
}
|
||||||
8
__database/diesel/src/schema.rs
Normal file
8
__database/diesel/src/schema.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
table! {
|
||||||
|
posts (id) {
|
||||||
|
id -> Nullable<Integer>,
|
||||||
|
title -> Text,
|
||||||
|
body -> Text,
|
||||||
|
published -> Bool,
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
__database/diesel/test.db
Normal file
BIN
__database/diesel/test.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user