Skip to main content

nautilus_infrastructure/python/sql/
cache.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use nautilus_common::{
17    cache::database::CacheDatabaseFactory,
18    python::cache::get_global_cache_database_factory_registry,
19};
20use nautilus_core::python::to_pyruntime_err;
21use pyo3::prelude::*;
22
23use crate::sql::cache::PostgresCacheConfig;
24
25#[pymethods]
26#[pyo3_stub_gen::derive::gen_stub_pymethods]
27impl PostgresCacheConfig {
28    /// Configuration for a Postgres-backed cache database.
29    ///
30    /// Missing fields are resolved from Postgres environment variables and then built-in defaults.
31    #[new]
32    #[pyo3(signature = (host=None, port=None, username=None, password=None, database=None))]
33    fn py_new(
34        host: Option<String>,
35        port: Option<u16>,
36        username: Option<String>,
37        password: Option<String>,
38        database: Option<String>,
39    ) -> Self {
40        Self {
41            host,
42            port,
43            username,
44            password,
45            database,
46        }
47    }
48
49    #[getter]
50    fn host(&self) -> Option<&str> {
51        self.host.as_deref()
52    }
53
54    #[getter]
55    const fn port(&self) -> Option<u16> {
56        self.port
57    }
58
59    #[getter]
60    fn username(&self) -> Option<&str> {
61        self.username.as_deref()
62    }
63
64    #[getter]
65    fn password(&self) -> Option<&str> {
66        self.password.as_deref()
67    }
68
69    #[getter]
70    fn database(&self) -> Option<&str> {
71        self.database.as_deref()
72    }
73}
74
75#[expect(clippy::needless_pass_by_value)]
76fn extract_postgres_cache_database_factory(
77    py: Python<'_>,
78    factory: Py<PyAny>,
79) -> PyResult<Box<dyn CacheDatabaseFactory>> {
80    Ok(Box::new(factory.extract::<PostgresCacheConfig>(py)?))
81}
82
83pub(in crate::python) fn register_postgres_cache_database_factory() -> PyResult<()> {
84    get_global_cache_database_factory_registry()
85        .register(
86            stringify!(PostgresCacheConfig).to_string(),
87            extract_postgres_cache_database_factory,
88        )
89        .map_err(to_pyruntime_err)
90}