From c4fe3c4ca710cd1df6120a6a6e6136bb17854a65 Mon Sep 17 00:00:00 2001 From: Fankai Liu Date: Thu, 26 Oct 2023 22:17:00 +0800 Subject: [PATCH] add rbatis dependencies --- src/template/data/rbatis_sqlite.db | Bin 0 -> 4096 bytes src/utils/create_project.rs | 203 ++++++++++++++++++----------- 2 files changed, 127 insertions(+), 76 deletions(-) create mode 100644 src/template/data/rbatis_sqlite.db diff --git a/src/template/data/rbatis_sqlite.db b/src/template/data/rbatis_sqlite.db new file mode 100644 index 0000000000000000000000000000000000000000..9a472209435d88229f65ab7a0ca30d67df87a462 GIT binary patch literal 4096 zcmWFz^vNtqRY=P(%1ta$FlG>7U}9o$P*7lCU|@t|AVoG{WY9}{#S79dK(-m98b?E5 nGz3ONU^E0qLtr!nMnhmU1V%$(Gz3ONU^E0qLtr!nC=3ArNJIx= literal 0 HcmV?d00001 diff --git a/src/utils/create_project.rs b/src/utils/create_project.rs index 22e00d0..5513dab 100644 --- a/src/utils/create_project.rs +++ b/src/utils/create_project.rs @@ -77,6 +77,7 @@ fn write_project_file( let is_sqlx = user_selected.db_conn_type == DbConnectionType::Sqlx; let is_sea_orm = user_selected.db_conn_type == DbConnectionType::SeaOrm; let is_diesel = user_selected.db_conn_type == DbConnectionType::Diesel; + let is_rbatis = user_selected.db_conn_type == DbConnectionType::Rbatis; let is_mysql = user_selected.db_type == DbType::Mysql; let is_postgres = user_selected.db_type == DbType::Postgres; let is_sqlite = user_selected.db_type == DbType::Sqlite; @@ -111,6 +112,7 @@ fn write_project_file( "is_sea_orm":is_sea_orm, "is_sea_orm_or_sqlx":is_sea_orm_or_sqlx, "is_diesel":is_diesel, + "is_rbatis":is_rbatis, "main_log_message":t!("main_log_message"), "config_error_no_exits":t!("config_error_no_exits"), "config_error_read":t!("config_error_read"), @@ -135,82 +137,15 @@ fn write_project_file( "create_success_sea_orm__mysql_or_pgsql_fist_use":t!("create_success_sea_orm__mysql_or_pgsql_fist_use").replace(r"\n", "\n"), "create_success_diesel__mysql_or_pgsql_fist_use":t!("create_success_diesel__mysql_or_pgsql_fist_use").replace(r"\n", "\n"), }); - if need_db_conn { - // Add sqlx dependencies - let mut dependencies = data["dependencies"].clone(); - if is_sqlx { - if is_mysql { - dependencies["sqlx"] = json!({ - "version": "0.7", - "features": ["runtime-tokio", "macros", "mysql"] - }); - } - if is_postgres { - dependencies["sqlx"] = json!({ - "version": "0.7", - "features": ["runtime-tokio", "macros", "postgres"] - }); - } - if is_sqlite { - dependencies["sqlx"] = json!({ - "version": "0.7", - "features": ["runtime-tokio", "macros", "sqlite"] - }); - } - } else if is_sea_orm { - if is_mysql { - dependencies["sea-orm"] = json!({ - "version": "0", - "features": ["runtime-tokio-native-tls","sqlx-mysql"] - }); - } - if is_postgres { - dependencies["sea-orm"] = json!({ - "version": "0", - "features": ["runtime-tokio-native-tls","sqlx-postgres"] - }); - } - if is_sqlite { - dependencies["sea-orm"] = json!({ - "version": "0", - "features": ["runtime-tokio-native-tls","sqlx-sqlite"] - }); - } - } else if is_diesel { - if is_mysql { - dependencies["diesel"] = json!({ - "version": "2.1.0", - "features": ["mysql"] - }); - } - if is_postgres { - dependencies["diesel"] = json!({ - "version": "2.1.0", - "features": ["postgres"] - }); - } - if is_sqlite { - dependencies["diesel"] = json!({ - "version": "2.1.0", - "features": ["sqlite","returning_clauses_for_sqlite_3_35"] - }); - } - } - //add uuid dependency - dependencies["uuid"] = json!({ - "version": "1.4.1", - "features": ["v4", "fast-rng", "macro-diagnostics"] - }); - //add rand dependency - dependencies["rand"] = json!({ - "version": "0.8.5", - }); - //add argon2 dependency - dependencies["argon2"] = json!({ - "version": "0.5.2", - }); - data["dependencies"] = dependencies; - } + let mut dependencies = data["dependencies"].clone(); + handle_dependencies( + &mut dependencies, + need_db_conn, + user_selected.db_type, + user_selected.db_conn_type, + ); + data["dependencies"] = dependencies; + std::fs::create_dir_all(project_path)?; @@ -623,6 +558,122 @@ fn check_path(path: &Path) -> Result<()> { Ok(()) } +fn handle_dependencies( + dependencies: &mut serde_json::Value, + need_db_conn: bool, + db_type: DbType, + conn_type: DbConnectionType +) { + if need_db_conn { + match (conn_type, db_type) { + (DbConnectionType::Sqlx, DbType::Mysql) => { + dependencies["sqlx"] = json!({ + "version": "0.7", + "features": ["runtime-tokio", "macros", "mysql"] + }); + } + (DbConnectionType::Sqlx, DbType::Postgres) => { + dependencies["sqlx"] = json!({ + "version": "0.7", + "features": ["runtime-tokio", "macros", "postgres"] + }); + } + (DbConnectionType::Sqlx, DbType::Sqlite) => { + dependencies["sqlx"] = json!({ + "version": "0.7", + "features": ["runtime-tokio", "macros", "sqlite"] + }); + } + (DbConnectionType::SeaOrm, DbType::Mysql) => { + dependencies["sea-orm"] = json!({ + "version": "0", + "features": ["runtime-tokio-native-tls","sqlx-mysql"] + }); + } + (DbConnectionType::SeaOrm, DbType::Postgres) => { + dependencies["sea-orm"] = json!({ + "version": "0", + "features": ["runtime-tokio-native-tls","sqlx-postgres"] + }); + } + (DbConnectionType::SeaOrm, DbType::Sqlite) => { + dependencies["sea-orm"] = json!({ + "version": "0", + "features": ["runtime-tokio-native-tls","sqlx-sqlite"] + }); + } + (DbConnectionType::Diesel, DbType::Mysql) => { + dependencies["diesel"] = json!({ + "version": "2.1.0", + "features": ["mysql"] + }); + } + (DbConnectionType::Diesel, DbType::Postgres) => { + dependencies["diesel"] = json!({ + "version": "2.1.0", + "features": ["postgres"] + }); + } + (DbConnectionType::Diesel, DbType::Sqlite) => { + dependencies["diesel"] = json!({ + "version": "2.1.0", + "features": ["sqlite","returning_clauses_for_sqlite_3_35"] + }); + } + (DbConnectionType::Rbatis, DbType::Mysql) => { + dependencies["rbdc-mysql"] = json!({ + "version": "4.4" + }); + dependencies["rbatis"] = json!({ + "version": "4.4", + "features": ["debug_mode"] + }); + } + (DbConnectionType::Rbatis, DbType::Postgres) => { + dependencies["rbdc-pg"] = json!({ + "version": "4.4" + }); + dependencies["rbatis"] = json!({ + "version": "4.4", + "features": ["debug_mode"] + }); + } + (DbConnectionType::Rbatis, DbType::Sqlite) => { + dependencies["rbdc-sqlite"] = json!({ + "version": "4.4" + }); + dependencies["rbatis"] = json!({ + "version": "4.4", + "features": ["debug_mode"] + }); + } + (DbConnectionType::Rbatis, DbType::Mssql) => { + dependencies["rbdc-mssql"] = json!({ + "version": "4.4" + }); + dependencies["rbatis"] = json!({ + "version": "4.4", + "features": ["debug_mode"] + }); + } + _ => {} + } + //add uuid dependency + dependencies["uuid"] = json!({ + "version": "1.4.1", + "features": ["v4", "fast-rng", "macro-diagnostics"] + }); + //add rand dependency + dependencies["rand"] = json!({ + "version": "0.8.5", + }); + //add argon2 dependency + dependencies["argon2"] = json!({ + "version": "0.5.2", + }); + } +} + pub fn join_paths>(paths: &[T], env: &str) -> Result { env::join_paths(paths.iter()).with_context(|| { let mut message = t!("erroe_join_paths", env = env).replace(r"\n", "\n");