我正在为我的 rust 应用程序编写一个数据库模块,使用柴油。
这是编译错误:
error[E0271]: type mismatch resolving `<MysqlConnection as Connection>::Backend == Sqlite`
--> src/quant/common/persistence/database.rs:155:18
|
155 | .load::<NetWorthModel>(&self.connection())
| ^^^^ expected struct `Mysql`, found struct `Sqlite`
|
= note: required because of the requirements on the impl of `LoadQuery<MysqlConnection, NetWorthModel>` for `diesel::query_builder::SelectStatement<table, diesel::query_builder::select_clause::DefaultSelectClause, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::WhereClause<And<And<diesel::expression::operators::Eq<columns::fund_code, diesel::expression::bound::Bound<diesel::sql_types::Text, &str>>, GtEq<columns::date, diesel::expression::bound::Bound<diesel::sql_types::Date, std::string::String>>>, Lt<columns::date, diesel::expression::bound::Bound<diesel::sql_types::Date, std::string::String>>>>, diesel::query_builder::order_clause::OrderClause<Asc<columns::date>>, diesel::query_builder::limit_clause::LimitClause<diesel::expression::bound::Bound<diesel::sql_types::BigInt, i64>>, diesel::query_builder::offset_clause::OffsetClause<diesel::expression::bound::Bound<diesel::sql_types::BigInt, i64>>>`
这是model.rs
:
use super::schema::tb_net_worth;
use diesel::{Identifiable, Insertable, Queryable};
#[derive(Queryable, Insertable, Identifiable)]
#[table_name = "tb_net_worth"]
#[primary_key(fund_code)]
pub struct NetWorthModel {
pub fund_code: String,
pub date: String,
pub create_time: i64,
pub update_time: i64,
pub payload: String,
}
这是我的源代码database.rs
:
use crate::quant::common::persistence::model::NetWorthModel;
use crate::quant::common::yadatetime::Date;
use diesel::mysql::MysqlConnection;
use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool};
use std::collections::HashMap;
use std::sync::Mutex;
pub struct MysqlDatabase {
pool: Pool<ConnectionManager<MysqlConnection>>,
}
impl MysqlDatabase {
fn new(user: &str, passwd: &str, host: &str, port: i32, db: &str) -> MysqlDatabase {
let url = format!("mysql://{}:{}@{}:{}/{}", user, passwd, host, port, db);
let pool_manager = ConnectionManager::<MysqlConnection>::new(url.as_str());
let pool = Pool::builder()
.max_size(16)
.build(pool_manager)
.unwrap();
MysqlDatabase {pool: pool.clone()}
}
fn connection(&self) -> MysqlConnection {
*self.pool.get().unwrap()
}
fn paged_query(&self,
fund_code: &str,
order_by: &str,
start_date: Date,
end_date: Date,
page_index: i32,
page_size: i32) -> Vec[NetWorthModel] {
use super::schema::tb_net_worth::dsl;
let query = dsl::tb_net_worth
.filter(dsl::fund_code.eq(fund_code))
.filter(dsl::date.ge(start_date.to_string()))
.filter(dsl::date.lt(end_date.to_string()));
if order_by == "ASC" {
query.order(dsl::date.asc())
.limit(page_size as i64)
.offset((page_index * page_size) as i64)
.load<NetWorthModel>(&self.connection())
.unwrap()
} else {
query.order(dsl::date.desc())
.limit(page_size as i64)
.offset((page_index * page_size) as i64)
.load<NetWorthModel>(&self.connection())
.unwrap()
}
}
你能帮忙吗?