Merge pull request #202 from baoyachi/adapter_url_scheme
Adapter postgres url scheme
This commit is contained in:
commit
c12e0cd44e
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
target
|
||||
Cargo.lock
|
||||
*.sublime*
|
||||
.vscode
|
||||
.vscode
|
||||
.idea/*
|
@ -56,7 +56,7 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
|
||||
.filter(|schema| filter_hidden_tables(&schema.info.name))
|
||||
.map(|schema| schema.write())
|
||||
.collect()
|
||||
} else if url.starts_with("postgres://") {
|
||||
} else if url.starts_with("postgres://") || url.starts_with("postgresql://") {
|
||||
use sea_schema::postgres::discovery::SchemaDiscovery;
|
||||
use sqlx::PgPool;
|
||||
|
||||
|
@ -129,6 +129,16 @@ impl DatabaseConnection {
|
||||
}
|
||||
|
||||
impl DbBackend {
|
||||
pub fn is_prefix_of(self, base_url: &str) -> bool {
|
||||
match self {
|
||||
Self::Postgres => {
|
||||
base_url.starts_with("postgres://") || base_url.starts_with("postgresql://")
|
||||
}
|
||||
Self::MySql => base_url.starts_with("mysql://"),
|
||||
Self::Sqlite => base_url.starts_with("sqlite:"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build<S>(&self, statement: &S) -> Statement
|
||||
where
|
||||
S: StatementBuilder,
|
||||
|
@ -18,15 +18,15 @@ pub struct Database;
|
||||
impl Database {
|
||||
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
|
||||
#[cfg(feature = "sqlx-mysql")]
|
||||
if crate::SqlxMySqlConnector::accepts(string) {
|
||||
if DbBackend::MySql.is_prefix_of(string) {
|
||||
return crate::SqlxMySqlConnector::connect(string).await;
|
||||
}
|
||||
#[cfg(feature = "sqlx-postgres")]
|
||||
if crate::SqlxPostgresConnector::accepts(string) {
|
||||
if DbBackend::Postgres.is_prefix_of(string) {
|
||||
return crate::SqlxPostgresConnector::connect(string).await;
|
||||
}
|
||||
#[cfg(feature = "sqlx-sqlite")]
|
||||
if crate::SqlxSqliteConnector::accepts(string) {
|
||||
if DbBackend::Sqlite.is_prefix_of(string) {
|
||||
return crate::SqlxSqliteConnector::connect(string).await;
|
||||
}
|
||||
#[cfg(feature = "mock")]
|
||||
|
@ -31,15 +31,15 @@ impl MockDatabaseConnector {
|
||||
#[allow(unused_variables)]
|
||||
pub fn accepts(string: &str) -> bool {
|
||||
#[cfg(feature = "sqlx-mysql")]
|
||||
if crate::SqlxMySqlConnector::accepts(string) {
|
||||
if DbBackend::MySql.is_prefix_of(string) {
|
||||
return true;
|
||||
}
|
||||
#[cfg(feature = "sqlx-postgres")]
|
||||
if crate::SqlxPostgresConnector::accepts(string) {
|
||||
if DbBackend::Postgres.is_prefix_of(string) {
|
||||
return true;
|
||||
}
|
||||
#[cfg(feature = "sqlx-sqlite")]
|
||||
if crate::SqlxSqliteConnector::accepts(string) {
|
||||
if DbBackend::Sqlite.is_prefix_of(string) {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
|
@ -6,7 +6,7 @@ use sqlx::{
|
||||
sea_query::sea_query_driver_mysql!();
|
||||
use sea_query_driver_mysql::bind_query;
|
||||
|
||||
use crate::{debug_print, error::*, executor::*, DatabaseConnection, Statement};
|
||||
use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbBackend, Statement};
|
||||
|
||||
use super::sqlx_common::*;
|
||||
|
||||
@ -20,7 +20,7 @@ pub struct SqlxMySqlPoolConnection {
|
||||
|
||||
impl SqlxMySqlConnector {
|
||||
pub fn accepts(string: &str) -> bool {
|
||||
string.starts_with("mysql://")
|
||||
DbBackend::MySql.is_prefix_of(string)
|
||||
}
|
||||
|
||||
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
|
||||
|
@ -6,7 +6,7 @@ use sqlx::{
|
||||
sea_query::sea_query_driver_postgres!();
|
||||
use sea_query_driver_postgres::bind_query;
|
||||
|
||||
use crate::{debug_print, error::*, executor::*, DatabaseConnection, Statement};
|
||||
use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbBackend, Statement};
|
||||
|
||||
use super::sqlx_common::*;
|
||||
|
||||
@ -20,7 +20,7 @@ pub struct SqlxPostgresPoolConnection {
|
||||
|
||||
impl SqlxPostgresConnector {
|
||||
pub fn accepts(string: &str) -> bool {
|
||||
string.starts_with("postgres://")
|
||||
DbBackend::Postgres.is_prefix_of(string)
|
||||
}
|
||||
|
||||
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
|
||||
|
@ -6,7 +6,7 @@ use sqlx::{
|
||||
sea_query::sea_query_driver_sqlite!();
|
||||
use sea_query_driver_sqlite::bind_query;
|
||||
|
||||
use crate::{debug_print, error::*, executor::*, DatabaseConnection, Statement};
|
||||
use crate::{debug_print, error::*, executor::*, DatabaseConnection, DbBackend, Statement};
|
||||
|
||||
use super::sqlx_common::*;
|
||||
|
||||
@ -20,7 +20,7 @@ pub struct SqlxSqlitePoolConnection {
|
||||
|
||||
impl SqlxSqliteConnector {
|
||||
pub fn accepts(string: &str) -> bool {
|
||||
string.starts_with("sqlite:")
|
||||
DbBackend::Sqlite.is_prefix_of(string)
|
||||
}
|
||||
|
||||
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
|
||||
|
@ -438,14 +438,8 @@ mod tests {
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
hello::Column::One.def(),
|
||||
ColumnType::Integer.def()
|
||||
);
|
||||
assert_eq!(
|
||||
hello::Column::Two.def(),
|
||||
ColumnType::Integer.def().unique()
|
||||
);
|
||||
assert_eq!(hello::Column::One.def(), ColumnType::Integer.def());
|
||||
assert_eq!(hello::Column::Two.def(), ColumnType::Integer.def().unique());
|
||||
assert_eq!(
|
||||
hello::Column::Three.def(),
|
||||
ColumnType::Integer.def().indexed()
|
||||
|
Loading…
x
Reference in New Issue
Block a user