Add url query param parsing to postgres url connector init.
@@ -2,7 +2,7 @@ use crate::{
error::SqlError, query_builder::RelatedNodesWithRowNumber, MutationBuilder, RawQuery, SqlId, SqlResult, SqlRow,
ToSqlRow, Transaction, Transactional,
};
-use chrono::{DateTime, NaiveDateTime, Utc, NaiveDate};
+use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};
use connector::{error::*, ConnectorResult};
use datamodel::configuration::Source;
use native_tls::TlsConnector;
@@ -36,8 +36,48 @@ impl TryFrom<&Box<dyn Source>> for PostgreSql {
/// Todo connection limit configuration
fn try_from(source: &Box<dyn Source>) -> SqlResult<PostgreSql> {
- let mut config = Config::from_str(source.url())?;
- config.ssl_mode(SslMode::Prefer);
+ let mut url: url::Url = url::Url::parse(source.url())?;
+
+ // Supported official connection url parameters (empty = strip all)
+ let official = vec![];
+
+ let (supported, unsupported): (Vec<(String, String)>, Vec<(String, String)>) = url
+ .query_pairs()
+ .into_iter()
+ .map(|(k, v)| (String::from(k), String::from(v)))
+ .collect::<Vec<(String, String)>>()
+ .into_iter()
+ .partition(|(k, _)| official.contains(&k.as_str()));
+
+ // Reset params and append supported ones, then create a valid config & set keys based on custom params.
+ url.query_pairs_mut().clear();
+
+ supported.into_iter().for_each(|(k, v)| {
+ url.query_pairs_mut().append_pair(&k, &v);
+ });
+
+ let mut config = Config::from_str(&url.to_string())?;
+
+ unsupported.into_iter().for_each(|(k, v)| {
+ match k.as_ref() {
+ "schema" => {
+ debug!("Using schema: {}", v);
+ config.dbname(&v.as_str());
+ }
+ "sslmode" => {
+ match v.as_ref() {
+ "disable" => config.ssl_mode(SslMode::Disable),
+ "prefer" => config.ssl_mode(SslMode::Prefer),
+ "require" => config.ssl_mode(SslMode::Require),
+ _ => {
+ debug!("Unsupported ssl mode {}, defaulting to 'prefer'", v);
+ config.ssl_mode(SslMode::Prefer)
+ }
+ };
+ }
+ _ => trace!("Discarding connection string param: {}", k),
+ };
+ });
trace!("{:?}", &config);
Ok(Self::new(config, 10)?)
@@ -268,8 +308,8 @@ impl<'a> Transaction for PostgresTransaction<'a> {
let date: NaiveDate = val;
Value::String(date.format("%Y-%m-%d").to_string())
}
- None => Value::Null
- }
+ None => Value::Null,
+ },
PostgresType::UUID => match row.try_get(i)? {
Some(val) => {
let val: Uuid = val;
@@ -479,14 +519,14 @@ impl ToSqlRow for PostgresRow {
PrismaValue::DateTime(DateTime::<Utc>::from_utc(dt, Utc))
}
None => PrismaValue::Null,
- }
+ },
_ => match row.try_get(i)? {
Some(val) => {
let ts: NaiveDateTime = val;
PrismaValue::DateTime(DateTime::<Utc>::from_utc(ts, Utc))
}
None => PrismaValue::Null,
- }
+ },
},
};
