turso_orm/entity/iden.rs
1//! Static identifiers and enum iteration, modeled by [`IdenStatic`] and [`Iterable`].
2//!
3//! Table and column names are known at compile time, so the entity layer
4//! works with `&'static str` identifiers instead of allocating `Ident`s and
5//! only converts at the SQL boundary. [`Iterable`] gives the query builders a
6//! way to walk every column, key or relation of an entity without reflection;
7//! the derive macros fill in `ALL` in declaration order so that generated
8//! `SELECT` lists and `INSERT` column lists are stable.
9//!
10//! [`Static`] is the bridge to `turso_sql`: it is re-exported through the
11//! crate's `__private` module for generated code and is not part of the
12//! public API.
13
14use turso_sql::{Ident, IntoIden};
15
16/// An identifier known at compile time, such as a table or column name.
17pub trait IdenStatic: Copy + Send + Sync + std::fmt::Debug + 'static {
18 /// The unquoted name.
19 fn as_str(&self) -> &'static str;
20
21 /// Converts the name into an owned SQL identifier.
22 fn into_ident(self) -> Ident {
23 Ident::new_static(self.as_str())
24 }
25}
26
27/// An enumeration whose variants can be listed: columns, primary keys and relations.
28///
29/// Generated by the derive macros; `ALL` is in declaration order.
30pub trait Iterable: Sized + Copy + 'static {
31 /// Every variant, in declaration order.
32 const ALL: &'static [Self];
33
34 /// Iterates over the variants in declaration order.
35 fn iter() -> std::iter::Copied<std::slice::Iter<'static, Self>> {
36 Self::ALL.iter().copied()
37 }
38}
39
40/// A wrapper that lets any [`IdenStatic`] be used where a [`turso_sql::IntoIden`] is expected.
41#[derive(Clone, Copy, Debug)]
42pub struct Static<T: IdenStatic>(pub T);
43
44impl<T: IdenStatic> IntoIden for Static<T> {
45 fn into_iden(self) -> Ident {
46 self.0.into_ident()
47 }
48}