@@ -4,18 +4,73 @@ use serde::Deserialize;
44use std:: error:: Error ;
55use std:: fmt:: Display ;
66
7- /// Holds the current supported databases by Canyon-SQL
7+ /// Represents the supported database backends in **Canyon-SQL**.
8+ ///
9+ /// This enum abstracts over the specific database dialects supported by Canyon,
10+ /// allowing queries and builders to adapt automatically to the correct SQL syntax
11+ /// and placeholder conventions (`$1`, `?`, `@P1`, etc.) according to the active
12+ /// [`DatabaseType`].
13+ ///
14+ /// The variant used at runtime is determined either:
15+ /// - Explicitly, when passed to a [`QueryBuilder`] constructor, or
16+ /// - Implicitly, from the first configured data source via
17+ /// [`Canyon::get_default_db_type()`].
18+ ///
19+ /// # Example
20+ /// ```rust,ignore
21+ /// use canyon_core::connection::database_type::DatabaseType;
22+ ///
23+ /// // Create a query builder explicitly targeting PostgreSQL:
24+ /// let builder = QueryBuilder::new_for(table, columns, DatabaseType::PostgreSql)?;
25+ ///
26+ /// // Or defer the database type resolution until runtime:
27+ /// let builder = QueryBuilder::new_for(table, columns, DatabaseType::Deferred)?;
28+ /// let query = builder.build()?; // will resolve to the default DB type
29+ /// ```
830#[ derive( Deserialize , Debug , Eq , PartialEq , Clone , Copy ) ]
931pub enum DatabaseType {
32+ /// The Postgres database backend.
1033 #[ cfg( feature = "postgres" ) ]
1134 #[ serde( alias = "postgres" , alias = "postgresql" ) ]
1235 PostgreSql ,
36+
37+ /// The Microsoft SQL Server backend.
1338 #[ cfg( feature = "mssql" ) ]
1439 #[ serde( alias = "sqlserver" , alias = "mssql" ) ]
1540 SqlServer ,
41+
42+ /// The MySQL or MariaDB backend.
1643 #[ cfg( feature = "mysql" ) ]
1744 #[ serde( alias = "mysql" ) ]
1845 MySQL ,
46+
47+ /// A **placeholder variant** used when the database dialect
48+ /// cannot be determined at compile time.
49+ ///
50+ /// The [`Deferred`](Self::Deferred) variant allows you to construct a query
51+ /// (for example, through a procedural macro like `CanyonCrud`) before the
52+ /// actual database type is known — typically at compile-time code generation.
53+ ///
54+ /// When using this variant, the [`QueryBuilder::build()`] method will automatically
55+ /// attempt to resolve the concrete database type from the active [`Canyon`]
56+ /// instance at runtime.
57+ ///
58+ /// # Use case
59+ /// This is particularly useful for macros and compile-time query generation,
60+ /// where it’s desirable to emit code that’s agnostic of the target database.
61+ /// The macro can safely emit `DatabaseType::Deferred` in the generated code,
62+ /// and Canyon will resolve it dynamically when executing queries.
63+ ///
64+ /// # Example
65+ /// ```rust,ignore
66+ /// let query = SelectQueryBuilder::new_for(
67+ /// &table_metadata,
68+ /// DatabaseType::Deferred
69+ /// )?
70+ /// .where_("id", Comp::Eq, &42)
71+ /// .build()?; // resolved dynamically to the active database type
72+ /// ```
73+ Deferred ,
1974}
2075
2176impl Display for DatabaseType {
@@ -33,7 +88,7 @@ impl From<&Auth> for DatabaseType {
3388/// The default implementation for [`DatabaseType`] returns the database type for the first
3489/// datasource configured
3590impl DatabaseType {
36- pub async fn default_type ( ) -> Result < Self , Box < dyn Error + Send + Sync > > {
91+ pub fn default_type ( ) -> Result < Self , Box < dyn Error + Send + Sync > > {
3792 Canyon :: instance ( ) ?
3893 . get_default_db_type ( )
3994 . map_err ( |err| Box :: new ( err) as Box < dyn Error + Send + Sync > )
0 commit comments