Skip to content

Commit

Permalink
增加支持与r-nacos导出中间数据文件相互转化的sqlite dao模块 #138
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Oct 18, 2024
1 parent d6d7be3 commit 8f183e4
Show file tree
Hide file tree
Showing 9 changed files with 840 additions and 3 deletions.
46 changes: 46 additions & 0 deletions doc/sql/r-nacos-transfer-db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

create table if not exists tb_config(
id integer primary key autoincrement,
data_id text,
group_id text,
tenant_id text,
content text,
config_type text,
config_desc text,
last_time long
);
create index if not exists tb_config_key_idx on tb_config(data_id,group_id,tenant_id);

create table if not exists tb_config_history(
id integer primary key autoincrement,
data_id text,
group_id text,
tenant_id text,
content text,
config_type text,
config_desc text,
op_user text,
last_time long
);
create index if not exists tb_config_history_key_idx on tb_config_history(data_id,group_id,tenant_id);

create table if not exists tb_tenant(
id integer primary key autoincrement,
tenant_id text,
tenant_name text,
tenant_desc text,
create_flag integer
);

create table if not exists tb_user(
id integer primary key autoincrement,
username text,
nickname text,
password_hash text,
gmt_create integer,
gmt_modified integer,
enabled text,
roles text,
extend_info text
);

7 changes: 4 additions & 3 deletions src/console/user_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ pub async fn remove_user(
.json(ApiResult::<()>::error(e.to_string(), Some(e.to_string())))),
}
}
Err(e) => {
Ok(HttpResponse::Ok().json(ApiResult::<()>::error("SYSTEM_ERROR".to_owned(), Some(e.to_string()))))
}
Err(e) => Ok(HttpResponse::Ok().json(ApiResult::<()>::error(
"SYSTEM_ERROR".to_owned(),
Some(e.to_string()),
))),
}
}
1 change: 1 addition & 0 deletions src/transfer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod model;
pub mod reader;
pub mod sqlite;
pub mod writer;
195 changes: 195 additions & 0 deletions src/transfer/sqlite/dao/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#[allow(dead_code, unused_imports)]
use rsql_builder::B;
use rusqlite::{Connection, Row};
use serde::{Deserialize, Serialize};
use std::rc::Rc;

use crate::common::rusqlite_utils::{
get_row_value, sqlite_execute, sqlite_fetch, sqlite_fetch_count,
};

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ConfigDO {
pub id: Option<i64>,
pub data_id: Option<String>,
pub group_id: Option<String>,
pub tenant_id: Option<String>,
pub content: Option<String>,
pub config_type: Option<String>,
pub config_desc: Option<String>,
pub last_time: Option<f64>,
}

impl ConfigDO {
fn from_row(r: &Row) -> Self {
let mut s = Self::default();
s.id = get_row_value(r, "id");
s.data_id = get_row_value(r, "data_id");
s.group_id = get_row_value(r, "group_id");
s.tenant_id = get_row_value(r, "tenant_id");
s.content = get_row_value(r, "content");
s.config_type = get_row_value(r, "config_type");
s.config_desc = get_row_value(r, "config_desc");
s.last_time = get_row_value(r, "last_time");
s
}
}

#[derive(Debug, Default)]
pub struct ConfigParam {
pub id: Option<i64>,
pub limit: Option<i64>,
pub offset: Option<i64>,
}
pub struct ConfigSql {}

impl ConfigSql {
fn conditions(&self, param: &ConfigParam) -> B {
let mut whr = B::new_where();
if let Some(id) = &param.id {
whr.eq("id", id);
}
whr
}

pub fn query_prepare(&self, param: &ConfigParam) -> (String, Vec<serde_json::Value>) {
B::prepare(
B::new_sql("select id, data_id, group_id, tenant_id, content, config_type, config_desc, last_time from tb_config")
.push_build(&mut self.conditions(param))
)
}

pub fn insert_prepare(&self, record: &ConfigDO) -> (String, Vec<serde_json::Value>) {
let mut field_builder = B::new_comma_paren();
let mut value_builder = B::new_comma_paren();
if let Some(id) = &record.id {
field_builder.push_sql("id");
value_builder.push("?", id);
}
if let Some(data_id) = &record.data_id {
field_builder.push_sql("data_id");
value_builder.push("?", data_id);
}
if let Some(group_id) = &record.group_id {
field_builder.push_sql("group_id");
value_builder.push("?", group_id);
}
if let Some(tenant_id) = &record.tenant_id {
field_builder.push_sql("tenant_id");
value_builder.push("?", tenant_id);
}
if let Some(content) = &record.content {
field_builder.push_sql("content");
value_builder.push("?", content);
}
if let Some(config_type) = &record.config_type {
field_builder.push_sql("config_type");
value_builder.push("?", config_type);
}
if let Some(config_desc) = &record.config_desc {
field_builder.push_sql("config_desc");
value_builder.push("?", config_desc);
}
if let Some(last_time) = &record.last_time {
field_builder.push_sql("last_time");
value_builder.push("?", last_time);
}
B::prepare(
B::new_sql("insert into tb_config")
.push_build(&mut field_builder)
.push_sql("values")
.push_build(&mut value_builder),
)
}

pub fn update_prepare(
&self,
record: &ConfigDO,
param: &ConfigParam,
) -> (String, Vec<serde_json::Value>) {
let mut set_builder = B::new_comma();
if let Some(id) = &record.id {
set_builder.eq("id", id);
}
if let Some(data_id) = &record.data_id {
set_builder.eq("data_id", data_id);
}
if let Some(group_id) = &record.group_id {
set_builder.eq("group_id", group_id);
}
if let Some(tenant_id) = &record.tenant_id {
set_builder.eq("tenant_id", tenant_id);
}
if let Some(content) = &record.content {
set_builder.eq("content", content);
}
if let Some(config_type) = &record.config_type {
set_builder.eq("config_type", config_type);
}
if let Some(config_desc) = &record.config_desc {
set_builder.eq("config_desc", config_desc);
}
if let Some(last_time) = &record.last_time {
set_builder.eq("last_time", last_time);
}
let mut whr = self.conditions(param);
if whr.is_empty() {
panic!("update conditions is empty");
}
B::prepare(
B::new_sql("update tb_config set ")
.push_build(&mut set_builder)
.push_build(&mut whr),
)
}

pub fn delete_prepare(&self, param: &ConfigParam) -> (String, Vec<serde_json::Value>) {
B::prepare(B::new_sql("delete from tb_config").push_build(&mut self.conditions(param)))
}
}

pub struct ConfigDao {
conn: Rc<Connection>,
inner: ConfigSql,
}

impl ConfigDao {
pub fn new(conn: Rc<Connection>) -> Self {
Self {
conn,
inner: ConfigSql {},
}
}

pub fn execute(&self, sql: &str, args: &Vec<serde_json::Value>) -> anyhow::Result<usize> {
sqlite_execute(&self.conn, sql, args)
}

pub fn fetch(&self, sql: &str, args: &Vec<serde_json::Value>) -> anyhow::Result<Vec<ConfigDO>> {
sqlite_fetch(&self.conn, sql, args, ConfigDO::from_row)
}

pub fn fetch_count(&self, sql: &str, args: &Vec<serde_json::Value>) -> anyhow::Result<u64> {
sqlite_fetch_count(&self.conn, sql, args)
}

pub fn insert(&self, record: &ConfigDO) -> anyhow::Result<usize> {
let (sql, args) = self.inner.insert_prepare(record);
self.execute(&sql, &args)
}

pub fn update(&self, record: &ConfigDO, param: &ConfigParam) -> anyhow::Result<usize> {
let (sql, args) = self.inner.update_prepare(record, param);
self.execute(&sql, &args)
}

pub fn delete(&self, param: &ConfigParam) -> anyhow::Result<usize> {
let (sql, args) = self.inner.delete_prepare(param);
self.execute(&sql, &args)
}

pub fn query(&self, param: &ConfigParam) -> anyhow::Result<Vec<ConfigDO>> {
let (sql, args) = self.inner.query_prepare(param);
self.fetch(&sql, &args)
}
}
Loading

0 comments on commit 8f183e4

Please sign in to comment.