anycms-i18n-sqlx 0.2.4

SQLx database backend for anycms-i18n
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! # anycms-i18n-sqlx
//!
//! SQLx database backend for [`anycms-i18n`].
//!
//! Loads translations from any SQLx-supported database (PostgreSQL, MySQL, SQLite)
//! into an in-memory cache at startup, then serves translations synchronously
//! via the [`Backend`] trait.
//!
//! ## Quick Start
//!
//! ```rust,ignore
//! use anycms_i18n_sqlx::SqlxBackend;
//!
//! // PostgreSQL
//! let pool = sqlx::PgPool::connect("postgres://...").await?;
//! let backend = SqlxBackend::from_postgres(&pool).await?;
//!
//! // MySQL
//! let pool = sqlx::MySqlPool::connect("mysql://...").await?;
//! let backend = SqlxBackend::from_mysql(&pool).await?;
//!
//! // SQLite
//! let pool = sqlx::SqlitePool::connect("sqlite:translations.db").await?;
//! let backend = SqlxBackend::from_sqlite(&pool).await?;
//!
//! // Use as a Backend
//! use anycms_i18n::Backend;
//! assert!(backend.has_locale("en"));
//! ```
//!
//! ## Custom Table / Column Names
//!
//! Use [`SqlxBackendBuilder`] to customize the table and column names:
//!
//! ```rust,ignore
//! let backend = SqlxBackendBuilder::new()
//!     .table("my_translations")
//!     .locale_col("lang")
//!     .key_col("msg_key")
//!     .value_col("msg_value")
//!     .build_postgres(&pool)
//!     .await?;
//! ```

use std::collections::HashMap;

use anycms_i18n::Backend;
#[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
use anycms_i18n::I18nError;
use dashmap::DashMap;

// ---------------------------------------------------------------------------
// SqlxBackend
// ---------------------------------------------------------------------------

/// Database-backed translation backend powered by SQLx.
///
/// Translations are loaded asynchronously into an in-memory [`DashMap`] cache.
/// All [`Backend`] trait methods are synchronous and read from cache only.
pub struct SqlxBackend {
    cache: DashMap<String, HashMap<String, String>>,
}

impl SqlxBackend {
    /// Create an empty backend with no translations.
    pub fn new() -> Self {
        Self {
            cache: DashMap::new(),
        }
    }

    /// Create from an iterator of `(locale, key, value)` tuples.
    ///
    /// This is the core constructor used by all database-specific loaders.
    /// You can also call this directly if you have translations from another source.
    ///
    /// # Example
    ///
    /// ```
    /// use anycms_i18n::Backend;
    /// use anycms_i18n_sqlx::SqlxBackend;
    ///
    /// let backend = SqlxBackend::from_translations(vec![
    ///     ("en".to_string(), "hello".to_string(), "Hello".to_string()),
    ///     ("zh-CN".to_string(), "hello".to_string(), "你好".to_string()),
    /// ]);
    ///
    /// assert_eq!(backend.get("en", "hello").as_deref(), Some("Hello"));
    /// assert_eq!(backend.get("zh-CN", "hello").as_deref(), Some("你好"));
    /// assert!(backend.has_locale("en"));
    /// assert!(!backend.has_locale("ja"));
    /// ```
    pub fn from_translations(
        translations: impl IntoIterator<Item = (String, String, String)>,
    ) -> Self {
        let cache = DashMap::new();
        for (locale, key, value) in translations {
            cache
                .entry(locale)
                .or_insert_with(HashMap::new)
                .insert(key, value);
        }
        Self { cache }
    }

    /// Async reload: clear the cache and re-populate from an iterator.
    ///
    /// This is a convenience wrapper that clears the internal cache and rebuilds
    /// it from the provided translations. In practice you would call one of the
    /// database-specific reload methods instead.
    pub fn reload_from_translations(
        &self,
        translations: impl IntoIterator<Item = (String, String, String)>,
    ) {
        self.cache.clear();
        for (locale, key, value) in translations {
            self.cache.entry(locale).or_default().insert(key, value);
        }
    }

    // -- PostgreSQL -----------------------------------------------------------

    /// Load all translations from a PostgreSQL pool.
    ///
    /// Queries `SELECT locale, key, value FROM i18n_translations`.
    #[cfg(feature = "postgres")]
    pub async fn from_postgres(pool: &sqlx::PgPool) -> Result<Self, I18nError> {
        let rows: Vec<(String, String, String)> =
            sqlx::query_as("SELECT locale, key, value FROM i18n_translations")
                .fetch_all(pool)
                .await
                .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        Ok(Self::from_translations(rows))
    }

    /// Reload translations from a PostgreSQL pool.
    #[cfg(feature = "postgres")]
    pub async fn reload_postgres(&self, pool: &sqlx::PgPool) -> Result<(), I18nError> {
        let rows: Vec<(String, String, String)> =
            sqlx::query_as("SELECT locale, key, value FROM i18n_translations")
                .fetch_all(pool)
                .await
                .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        self.reload_from_translations(rows);
        Ok(())
    }

    // -- MySQL ----------------------------------------------------------------

    /// Load all translations from a MySQL pool.
    ///
    /// Queries `SELECT locale, key, value FROM i18n_translations`.
    #[cfg(feature = "mysql")]
    pub async fn from_mysql(pool: &sqlx::MySqlPool) -> Result<Self, I18nError> {
        let rows: Vec<(String, String, String)> =
            sqlx::query_as("SELECT locale, key, value FROM i18n_translations")
                .fetch_all(pool)
                .await
                .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        Ok(Self::from_translations(rows))
    }

    /// Reload translations from a MySQL pool.
    #[cfg(feature = "mysql")]
    pub async fn reload_mysql(&self, pool: &sqlx::MySqlPool) -> Result<(), I18nError> {
        let rows: Vec<(String, String, String)> =
            sqlx::query_as("SELECT locale, key, value FROM i18n_translations")
                .fetch_all(pool)
                .await
                .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        self.reload_from_translations(rows);
        Ok(())
    }

    // -- SQLite ---------------------------------------------------------------

    /// Load all translations from a SQLite pool.
    ///
    /// Queries `SELECT locale, key, value FROM i18n_translations`.
    #[cfg(feature = "sqlite")]
    pub async fn from_sqlite(pool: &sqlx::SqlitePool) -> Result<Self, I18nError> {
        let rows: Vec<(String, String, String)> =
            sqlx::query_as("SELECT locale, key, value FROM i18n_translations")
                .fetch_all(pool)
                .await
                .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        Ok(Self::from_translations(rows))
    }

    /// Reload translations from a SQLite pool.
    #[cfg(feature = "sqlite")]
    pub async fn reload_sqlite(&self, pool: &sqlx::SqlitePool) -> Result<(), I18nError> {
        let rows: Vec<(String, String, String)> =
            sqlx::query_as("SELECT locale, key, value FROM i18n_translations")
                .fetch_all(pool)
                .await
                .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        self.reload_from_translations(rows);
        Ok(())
    }
}

impl Default for SqlxBackend {
    fn default() -> Self {
        Self::new()
    }
}

impl Backend for SqlxBackend {
    fn get(&self, locale: &str, key: &str) -> Option<String> {
        self.cache.get(locale).and_then(|map| map.get(key).cloned())
    }

    fn available_locales(&self) -> Vec<String> {
        self.cache.iter().map(|r| r.key().clone()).collect()
    }

    fn has_locale(&self, locale: &str) -> bool {
        self.cache.contains_key(locale)
    }

    fn dump(&self, locale: &str) -> HashMap<String, String> {
        self.cache
            .get(locale)
            .map(|m| m.clone())
            .unwrap_or_default()
    }
}

// ---------------------------------------------------------------------------
// SqlxBackendBuilder
// ---------------------------------------------------------------------------

/// Builder for [`SqlxBackend`] with custom table and column names.
///
/// ```rust,ignore
/// let backend = SqlxBackendBuilder::new()
///     .table("my_translations")
///     .locale_col("lang")
///     .build_postgres(&pool)
///     .await?;
/// ```
pub struct SqlxBackendBuilder {
    table: String,
    locale_col: String,
    key_col: String,
    value_col: String,
}

impl SqlxBackendBuilder {
    /// Create a new builder with default names:
    /// - table: `i18n_translations`
    /// - locale column: `locale`
    /// - key column: `key`
    /// - value column: `value`
    pub fn new() -> Self {
        Self {
            table: "i18n_translations".into(),
            locale_col: "locale".into(),
            key_col: "key".into(),
            value_col: "value".into(),
        }
    }

    /// Set a custom table name.
    pub fn table(mut self, name: impl Into<String>) -> Self {
        self.table = name.into();
        self
    }

    /// Set a custom locale column name.
    pub fn locale_col(mut self, name: impl Into<String>) -> Self {
        self.locale_col = name.into();
        self
    }

    /// Set a custom key column name.
    pub fn key_col(mut self, name: impl Into<String>) -> Self {
        self.key_col = name.into();
        self
    }

    /// Set a custom value column name.
    pub fn value_col(mut self, name: impl Into<String>) -> Self {
        self.value_col = name.into();
        self
    }

    /// Build the SQL query string from configured names.
    #[allow(dead_code)] // used by feature-gated build_* methods
    fn query(&self) -> String {
        format!(
            "SELECT {} AS locale, {} AS key, {} AS value FROM {}",
            self.locale_col, self.key_col, self.value_col, self.table,
        )
    }

    /// Build from a PostgreSQL pool.
    #[cfg(feature = "postgres")]
    pub async fn build_postgres(&self, pool: &sqlx::PgPool) -> Result<SqlxBackend, I18nError> {
        let sql = self.query();
        let rows: Vec<(String, String, String)> = sqlx::query_as(sql.as_str())
            .fetch_all(pool)
            .await
            .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        Ok(SqlxBackend::from_translations(rows))
    }

    /// Build from a MySQL pool.
    #[cfg(feature = "mysql")]
    pub async fn build_mysql(&self, pool: &sqlx::MySqlPool) -> Result<SqlxBackend, I18nError> {
        let sql = self.query();
        let rows: Vec<(String, String, String)> = sqlx::query_as(sql.as_str())
            .fetch_all(pool)
            .await
            .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        Ok(SqlxBackend::from_translations(rows))
    }

    /// Build from a SQLite pool.
    #[cfg(feature = "sqlite")]
    pub async fn build_sqlite(&self, pool: &sqlx::SqlitePool) -> Result<SqlxBackend, I18nError> {
        let sql = self.query();
        let rows: Vec<(String, String, String)> = sqlx::query_as(sql.as_str())
            .fetch_all(pool)
            .await
            .map_err(|e| I18nError::DatabaseError(e.to_string()))?;

        Ok(SqlxBackend::from_translations(rows))
    }
}

impl Default for SqlxBackendBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_backend() {
        let backend = SqlxBackend::new();
        assert!(!backend.has_locale("en"));
        assert!(backend.available_locales().is_empty());
        assert_eq!(backend.get("en", "hello"), None);
    }

    #[test]
    fn from_translations() {
        let backend = SqlxBackend::from_translations(vec![
            ("en".into(), "hello".into(), "Hello".into()),
            ("en".into(), "world".into(), "World".into()),
            ("zh-CN".into(), "hello".into(), "你好".into()),
        ]);

        assert!(backend.has_locale("en"));
        assert!(backend.has_locale("zh-CN"));
        assert!(!backend.has_locale("ja"));
        assert_eq!(backend.get("en", "hello"), Some("Hello".into()));
        assert_eq!(backend.get("en", "world"), Some("World".into()));
        assert_eq!(backend.get("zh-CN", "hello"), Some("你好".into()));
        assert_eq!(backend.get("en", "missing"), None);

        let mut locales = backend.available_locales();
        locales.sort();
        assert_eq!(locales, vec!["en", "zh-CN"]);
    }

    #[test]
    fn reload_clears_old_data() {
        let backend =
            SqlxBackend::from_translations(vec![("en".into(), "hello".into(), "Hello".into())]);
        assert_eq!(backend.get("en", "hello"), Some("Hello".into()));

        backend.reload_from_translations(vec![("de".into(), "hello".into(), "Hallo".into())]);

        assert_eq!(backend.get("en", "hello"), None);
        assert_eq!(backend.get("de", "hello"), Some("Hallo".into()));
    }

    #[test]
    fn builder_default_query() {
        let builder = SqlxBackendBuilder::new();
        assert_eq!(
            builder.query(),
            "SELECT locale AS locale, key AS key, value AS value FROM i18n_translations"
        );
    }

    #[test]
    fn builder_custom_query() {
        let builder = SqlxBackendBuilder::new()
            .table("my_table")
            .locale_col("lang")
            .key_col("msg_key")
            .value_col("msg_val");

        assert_eq!(
            builder.query(),
            "SELECT lang AS locale, msg_key AS key, msg_val AS value FROM my_table"
        );
    }

    #[test]
    fn dump_returns_all_keys_for_locale() {
        let backend = SqlxBackend::from_translations(vec![
            ("en".into(), "hello".into(), "Hello".into()),
            ("en".into(), "world".into(), "World".into()),
            ("zh-CN".into(), "hello".into(), "你好".into()),
        ]);

        let en = backend.dump("en");
        assert_eq!(en.len(), 2);
        assert_eq!(en.get("hello").unwrap(), "Hello");
        assert_eq!(en.get("world").unwrap(), "World");

        let zh = backend.dump("zh-CN");
        assert_eq!(zh.len(), 1);
        assert_eq!(zh.get("hello").unwrap(), "你好");

        // Missing locale -> empty map.
        assert!(backend.dump("ja").is_empty());
    }
}