(options: ConnectOptions)
| 60 | /// Add configuration options for the PostgreSQL database |
| 61 | #[instrument(level = "trace")] |
| 62 | pub async fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> { |
| 63 | let mut sqlx_opts = options |
| 64 | .url |
| 65 | .parse::<PgConnectOptions>() |
| 66 | .map_err(sqlx_error_to_conn_err)?; |
| 67 | use sqlx::ConnectOptions; |
| 68 | if !options.sqlx_logging { |
| 69 | sqlx_opts = sqlx_opts.disable_statement_logging(); |
| 70 | } else { |
| 71 | sqlx_opts = sqlx_opts.log_statements(options.sqlx_logging_level); |
| 72 | if options.sqlx_slow_statements_logging_level != LevelFilter::Off { |
| 73 | sqlx_opts = sqlx_opts.log_slow_statements( |
| 74 | options.sqlx_slow_statements_logging_level, |
| 75 | options.sqlx_slow_statements_logging_threshold, |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if let Some(f) = &options.pg_opts_fn { |
| 81 | sqlx_opts = f(sqlx_opts); |
| 82 | } |
| 83 | |
| 84 | let set_search_path_sql = options.schema_search_path.as_ref().map(|schema| { |
| 85 | let mut string = "SET search_path = ".to_owned(); |
| 86 | if schema.starts_with('"') { |
| 87 | write!(&mut string, "{schema}").unwrap(); |
| 88 | } else { |
| 89 | for (i, schema) in schema.split(',').enumerate() { |
| 90 | if i > 0 { |
| 91 | write!(&mut string, ",").unwrap(); |
| 92 | } |
| 93 | if schema.starts_with('"') { |
| 94 | write!(&mut string, "{schema}").unwrap(); |
| 95 | } else { |
| 96 | write!(&mut string, "\"{schema}\"").unwrap(); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | string |
| 101 | }); |
| 102 | let lazy = options.connect_lazy; |
| 103 | let mut pool_options = options.sqlx_pool_options(); |
| 104 | if let Some(sql) = set_search_path_sql { |
| 105 | pool_options = pool_options.after_connect(move |conn, _| { |
| 106 | let sql = sql.clone(); |
| 107 | Box::pin(async move { |
| 108 | sqlx::Executor::execute(conn, sql.as_str()) |
| 109 | .await |
| 110 | .map(|_| ()) |
| 111 | }) |
| 112 | }); |
| 113 | } |
| 114 | let pool = if lazy { |
| 115 | pool_options.connect_lazy_with(sqlx_opts) |
| 116 | } else { |
| 117 | pool_options |
| 118 | .connect_with(sqlx_opts) |
| 119 | .await |
nothing calls this directly
no test coverage detected