medoo_rs 0.1.0

Query builder dinámico multi-backend (Postgres/MySQL/SQLite) inspirado en Medoo (PHP). Núcleo sin dependencias, pool async opcional.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! DDL: `CREATE TABLE`, `DROP TABLE`, `ALTER TABLE`. Genera SQL
//! parametrizado por backend con identifiers validados. No incluye
//! abstracción de tipos exhaustiva — los tipos comunes están
//! cubiertos; lo raro va vía `ColType::Raw("...")`.

use crate::backend::Backend;
use crate::error::{QueryError, Result};
use crate::ident;

/// Valida nombres de charset / collation. Whitelist `[A-Za-z0-9_.-]`,
/// máximo 64 chars. Acepta `.` y `-` para soportar locales POSIX de
/// Postgres (`en_US.UTF-8`, `C.UTF-8`). Sigue siendo seguro: no entran
/// quotes, espacios, ni metacaracteres SQL.
fn validate_collation_name(s: &str) -> Result<()> {
    if s.is_empty() || s.len() > 64 {
        return Err(QueryError::InvalidIdentifier(s.to_string()));
    }
    for c in s.chars() {
        if !(c.is_ascii_alphanumeric() || c == '_' || c == '.' || c == '-') {
            return Err(QueryError::InvalidIdentifier(s.to_string()));
        }
    }
    Ok(())
}

#[derive(Debug, Clone)]
pub enum ColType {
    /// MySQL TINYINT (1 byte). En PG/SQLite cae a SMALLINT/INTEGER.
    TinyInt,
    /// SMALLINT (2 bytes).
    SmallInt,
    Int,
    BigInt,
    /// `DECIMAL(p,s)` / `NUMERIC(p,s)`. Útil para plata. Al leer desde
    /// el pool retorna `Value::Text` para no perder precisión vs f64.
    Decimal(u32, u32),
    Bool,
    Float,
    Double,
    Text,
    /// MySQL `TINYTEXT` (255). PG/SQLite caen a TEXT.
    TinyText,
    /// MySQL `MEDIUMTEXT` (16 MB). PG/SQLite caen a TEXT.
    MediumText,
    /// MySQL `LONGTEXT` (4 GB). PG/SQLite caen a TEXT.
    LongText,
    Char(u32),
    Varchar(u32),
    /// `UUID` en PG; `CHAR(36)` en MySQL/SQLite (forma string).
    Uuid,
    Bytes,
    /// MySQL `BINARY(n)`. PG/SQLite caen a BYTEA/BLOB.
    Binary(u32),
    /// MySQL `VARBINARY(n)`. PG/SQLite caen a BYTEA/BLOB.
    VarBinary(u32),
    /// MySQL `TINYBLOB` / `MEDIUMBLOB` / `LONGBLOB`. Otros: BYTEA/BLOB.
    TinyBlob,
    MediumBlob,
    LongBlob,
    Json,
    /// **TZ-aware**: `TIMESTAMPTZ` (PG) / `TIMESTAMP` (MySQL: guarda
    /// en UTC y convierte a session TZ al leer) / `TEXT` (SQLite, vos
    /// guardás ISO 8601 con offset). Usá esta para instantes globales.
    Timestamp,
    /// **TZ-naive**: `TIMESTAMP` (PG, sin TZ) / `DATETIME` (MySQL,
    /// wall-clock crudo) / `TEXT` (SQLite). Usá esta cuando la fecha/
    /// hora representa un momento local que no debe ser convertido.
    DateTime,
    /// `DATE` en todos los backends (SQLite igual lo guarda como TEXT).
    Date,
    /// `TIME` (sin TZ) en MySQL/PG; `TEXT` en SQLite.
    Time,
    /// `TIME WITH TIME ZONE` (PG). MySQL/SQLite caen a TEXT — vos
    /// guardás el offset en el string.
    TimeTz,
    /// MySQL `YEAR`. PG/SQLite: SMALLINT.
    Year,
    /// MySQL `ENUM('a','b','c')`. En Postgres se cae a `TEXT` (PG tiene
    /// CREATE TYPE separado, no inline). En SQLite también `TEXT`.
    Enum(Vec<String>),
    /// MySQL `SET('a','b','c')`. Fuera de MySQL se cae a `TEXT`.
    Set(Vec<String>),
    Raw(String),
}

impl ColType {
    pub fn render(&self, backend: Backend) -> String {
        match (self, backend) {
            (ColType::TinyInt, Backend::MySql) => "TINYINT".into(),
            (ColType::TinyInt, _) => "SMALLINT".into(),
            (ColType::SmallInt, Backend::Sqlite) => "INTEGER".into(),
            (ColType::SmallInt, _) => "SMALLINT".into(),
            (ColType::Int, _) => "INTEGER".into(),
            (ColType::BigInt, Backend::Sqlite) => "INTEGER".into(),
            (ColType::BigInt, _) => "BIGINT".into(),
            (ColType::Decimal(p, s), _) => format!("DECIMAL({},{})", p, s),
            (ColType::Bool, Backend::Postgres) => "BOOLEAN".into(),
            (ColType::Bool, Backend::MySql) => "TINYINT(1)".into(),
            (ColType::Bool, Backend::Sqlite) => "INTEGER".into(),
            (ColType::Float, _) => "REAL".into(),
            (ColType::Double, Backend::Postgres) => "DOUBLE PRECISION".into(),
            (ColType::Double, _) => "DOUBLE".into(),
            (ColType::Text, _) => "TEXT".into(),
            (ColType::TinyText, Backend::MySql) => "TINYTEXT".into(),
            (ColType::TinyText, _) => "TEXT".into(),
            (ColType::MediumText, Backend::MySql) => "MEDIUMTEXT".into(),
            (ColType::MediumText, _) => "TEXT".into(),
            (ColType::LongText, Backend::MySql) => "LONGTEXT".into(),
            (ColType::LongText, _) => "TEXT".into(),
            (ColType::Char(n), _) => format!("CHAR({})", n),
            (ColType::Varchar(n), _) => format!("VARCHAR({})", n),
            (ColType::Uuid, Backend::Postgres) => "UUID".into(),
            (ColType::Uuid, _) => "CHAR(36)".into(),
            (ColType::Bytes, Backend::Postgres) => "BYTEA".into(),
            (ColType::Bytes, _) => "BLOB".into(),
            (ColType::Binary(n), Backend::MySql) => format!("BINARY({})", n),
            (ColType::Binary(_), Backend::Postgres) => "BYTEA".into(),
            (ColType::Binary(_), Backend::Sqlite) => "BLOB".into(),
            (ColType::VarBinary(n), Backend::MySql) => format!("VARBINARY({})", n),
            (ColType::VarBinary(_), Backend::Postgres) => "BYTEA".into(),
            (ColType::VarBinary(_), Backend::Sqlite) => "BLOB".into(),
            (ColType::TinyBlob, Backend::MySql) => "TINYBLOB".into(),
            (ColType::TinyBlob, Backend::Postgres) => "BYTEA".into(),
            (ColType::TinyBlob, Backend::Sqlite) => "BLOB".into(),
            (ColType::MediumBlob, Backend::MySql) => "MEDIUMBLOB".into(),
            (ColType::MediumBlob, Backend::Postgres) => "BYTEA".into(),
            (ColType::MediumBlob, Backend::Sqlite) => "BLOB".into(),
            (ColType::LongBlob, Backend::MySql) => "LONGBLOB".into(),
            (ColType::LongBlob, Backend::Postgres) => "BYTEA".into(),
            (ColType::LongBlob, Backend::Sqlite) => "BLOB".into(),
            (ColType::Json, Backend::Postgres) => "JSONB".into(),
            (ColType::Json, Backend::MySql) => "JSON".into(),
            (ColType::Json, Backend::Sqlite) => "TEXT".into(),
            (ColType::Timestamp, Backend::Postgres) => "TIMESTAMPTZ".into(),
            (ColType::Timestamp, Backend::MySql) => "TIMESTAMP".into(),
            (ColType::Timestamp, Backend::Sqlite) => "TEXT".into(),
            (ColType::DateTime, Backend::Postgres) => "TIMESTAMP".into(),
            (ColType::DateTime, Backend::MySql) => "DATETIME".into(),
            (ColType::DateTime, Backend::Sqlite) => "TEXT".into(),
            (ColType::Date, _) => "DATE".into(),
            (ColType::Time, Backend::Sqlite) => "TEXT".into(),
            (ColType::Time, _) => "TIME".into(),
            (ColType::TimeTz, Backend::Postgres) => "TIMETZ".into(),
            (ColType::TimeTz, _) => "TEXT".into(),
            (ColType::Year, Backend::MySql) => "YEAR".into(),
            (ColType::Year, Backend::Sqlite) => "INTEGER".into(),
            (ColType::Year, _) => "SMALLINT".into(),
            (ColType::Enum(vals), Backend::MySql) => format!("ENUM({})", quote_str_list(vals)),
            (ColType::Enum(_), _) => "TEXT".into(),
            (ColType::Set(vals), Backend::MySql) => format!("SET({})", quote_str_list(vals)),
            (ColType::Set(_), _) => "TEXT".into(),
            (ColType::Raw(s), _) => s.clone(),
        }
    }
}

/// Cita una lista de strings para ENUM/SET. Asume valores ya validados
/// (sin `'` ni `\`).
fn quote_str_list(vals: &[String]) -> String {
    vals.iter()
        .map(|v| format!("'{}'", v))
        .collect::<Vec<_>>()
        .join(",")
}

/// Verifica que los valores de un ENUM/SET no contengan caracteres que
/// puedan romper el quoting (`'` o `\`). Llamar antes de renderizar.
fn validate_enum_values(vals: &[String]) -> Result<()> {
    if vals.is_empty() {
        return Err(QueryError::EmptyRecord);
    }
    for v in vals {
        if v.contains('\'') || v.contains('\\') {
            return Err(QueryError::InvalidIdentifier(v.clone()));
        }
    }
    Ok(())
}

#[derive(Debug, Clone)]
pub struct ColDef {
    pub name: String,
    pub ty: ColType,
    pub nullable: bool,
    pub primary_key: bool,
    pub auto_increment: bool,
    pub unique: bool,
    pub default_raw: Option<String>, // sin parámetros: literales SQL como "now()" o "0"
    pub charset: Option<String>,     // MySQL: CHARACTER SET <name>
    pub collation: Option<String>,   // MySQL: COLLATE <name> · PG/SQLite: COLLATE "<name>"
}

impl ColDef {
    pub fn new<S: Into<String>>(name: S, ty: ColType) -> Self {
        Self {
            name: name.into(),
            ty,
            nullable: true,
            primary_key: false,
            auto_increment: false,
            unique: false,
            default_raw: None,
            charset: None,
            collation: None,
        }
    }
    pub fn charset<S: Into<String>>(mut self, s: S) -> Self {
        self.charset = Some(s.into());
        self
    }
    pub fn collation<S: Into<String>>(mut self, s: S) -> Self {
        self.collation = Some(s.into());
        self
    }
    pub fn not_null(mut self) -> Self {
        self.nullable = false;
        self
    }
    pub fn primary_key(mut self) -> Self {
        self.primary_key = true;
        self.nullable = false;
        self
    }
    pub fn auto_increment(mut self) -> Self {
        self.auto_increment = true;
        self
    }
    pub fn unique(mut self) -> Self {
        self.unique = true;
        self
    }
    pub fn default_raw<S: Into<String>>(mut self, expr: S) -> Self {
        let expr = expr.into();
        // bloqueo básico anti-injection: prohibir `;` y `--`.
        let safe = !expr.contains(';') && !expr.contains("--");
        if !safe {
            // se pospone al render: marcamos con el propio string para
            // que falle ahí. Más explícito:
        }
        self.default_raw = Some(expr);
        self
    }

    fn render(&self, backend: Backend) -> Result<String> {
        ident::validate(&self.name)?;
        // ENUM/SET: validar valores antes de renderizar
        match &self.ty {
            ColType::Enum(v) | ColType::Set(v) => validate_enum_values(v)?,
            _ => {}
        }
        let mut parts: Vec<String> = Vec::new();
        parts.push(backend.quote_ident(&self.name));

        // tipo + auto_increment ajustado por backend
        let type_sql = match (self.auto_increment, backend) {
            (true, Backend::Postgres) => match self.ty {
                ColType::BigInt => "BIGSERIAL".into(),
                _ => "SERIAL".into(),
            },
            (true, Backend::Sqlite) => "INTEGER".into(),
            _ => self.ty.render(backend),
        };
        parts.push(type_sql);

        // CHARACTER SET / COLLATE — antes de PK/NOT NULL/DEFAULT por
        // convención MySQL/Postgres.
        if let Some(cs) = &self.charset {
            validate_collation_name(cs)?;
            if backend == Backend::MySql {
                parts.push(format!("CHARACTER SET {}", cs));
            }
            // Postgres y SQLite no tienen charset por columna: ignoramos.
        }
        if let Some(co) = &self.collation {
            validate_collation_name(co)?;
            match backend {
                Backend::MySql => parts.push(format!("COLLATE {}", co)),
                Backend::Postgres => parts.push(format!("COLLATE \"{}\"", co)),
                Backend::Sqlite => parts.push(format!("COLLATE {}", co)),
            }
        }

        if self.primary_key {
            parts.push("PRIMARY KEY".into());
            if self.auto_increment && backend == Backend::Sqlite {
                parts.push("AUTOINCREMENT".into());
            }
        }
        if self.auto_increment && backend == Backend::MySql {
            parts.push("AUTO_INCREMENT".into());
        }
        if !self.nullable && !self.primary_key {
            parts.push("NOT NULL".into());
        }
        if self.unique && !self.primary_key {
            parts.push("UNIQUE".into());
        }
        if let Some(d) = &self.default_raw {
            if d.contains(';') || d.contains("--") {
                return Err(QueryError::InvalidIdentifier(d.clone()));
            }
            parts.push(format!("DEFAULT {}", d));
        }
        Ok(parts.join(" "))
    }
}

#[derive(Debug, Clone)]
pub struct CreateTable {
    backend: Backend,
    table: String,
    if_not_exists: bool,
    columns: Vec<ColDef>,
    primary_key_compound: Vec<String>,
    default_charset: Option<String>,
    default_collation: Option<String>,
    engine: Option<String>,
}

impl CreateTable {
    pub fn new(backend: Backend, table: &str) -> Self {
        Self {
            backend,
            table: table.to_string(),
            if_not_exists: false,
            columns: Vec::new(),
            primary_key_compound: Vec::new(),
            default_charset: None,
            default_collation: None,
            engine: None,
        }
    }
    /// `DEFAULT CHARSET=<name>` (MySQL). En Postgres/SQLite se ignora.
    pub fn default_charset<S: Into<String>>(mut self, s: S) -> Self {
        self.default_charset = Some(s.into());
        self
    }
    /// `COLLATE=<name>` a nivel tabla (MySQL). En Postgres/SQLite se ignora.
    pub fn default_collation<S: Into<String>>(mut self, s: S) -> Self {
        self.default_collation = Some(s.into());
        self
    }
    /// `ENGINE=<name>` (MySQL). En otros backends se ignora.
    pub fn engine<S: Into<String>>(mut self, s: S) -> Self {
        self.engine = Some(s.into());
        self
    }
    pub fn if_not_exists(mut self) -> Self {
        self.if_not_exists = true;
        self
    }
    pub fn col(mut self, c: ColDef) -> Self {
        self.columns.push(c);
        self
    }
    pub fn primary_key<I: IntoIterator<Item = S>, S: Into<String>>(mut self, cols: I) -> Self {
        self.primary_key_compound = cols.into_iter().map(|s| s.into()).collect();
        self
    }
    pub fn to_sql(&self) -> Result<String> {
        if self.columns.is_empty() {
            return Err(QueryError::EmptyRecord);
        }
        let mut col_sql: Vec<String> = Vec::new();
        for c in &self.columns {
            col_sql.push(c.render(self.backend)?);
        }
        if !self.primary_key_compound.is_empty() {
            let mut qcols: Vec<String> = Vec::new();
            for c in &self.primary_key_compound {
                qcols.push(ident::quote(self.backend, c)?);
            }
            col_sql.push(format!("PRIMARY KEY ({})", qcols.join(", ")));
        }
        let head = if self.if_not_exists { "CREATE TABLE IF NOT EXISTS" } else { "CREATE TABLE" };
        let mut sql = format!(
            "{} {} ({})",
            head,
            ident::quote(self.backend, &self.table)?,
            col_sql.join(", ")
        );
        // Suffix por backend: ENGINE / CHARSET / COLLATE solo aplican en MySQL.
        if self.backend == Backend::MySql {
            if let Some(eng) = &self.engine {
                validate_collation_name(eng)?;
                sql.push_str(&format!(" ENGINE={}", eng));
            }
            if let Some(cs) = &self.default_charset {
                validate_collation_name(cs)?;
                sql.push_str(&format!(" DEFAULT CHARSET={}", cs));
            }
            if let Some(co) = &self.default_collation {
                validate_collation_name(co)?;
                sql.push_str(&format!(" COLLATE={}", co));
            }
        } else {
            // Validamos igual aunque no se emitan, por consistencia.
            if let Some(s) = &self.default_charset {
                validate_collation_name(s)?;
            }
            if let Some(s) = &self.default_collation {
                validate_collation_name(s)?;
            }
            if let Some(s) = &self.engine {
                validate_collation_name(s)?;
            }
        }
        Ok(sql)
    }
}

#[derive(Debug, Clone)]
pub struct DropTable {
    backend: Backend,
    table: String,
    if_exists: bool,
    cascade: bool,
}

impl DropTable {
    pub fn new(backend: Backend, table: &str) -> Self {
        Self { backend, table: table.to_string(), if_exists: false, cascade: false }
    }
    pub fn if_exists(mut self) -> Self {
        self.if_exists = true;
        self
    }
    pub fn cascade(mut self) -> Self {
        self.cascade = true;
        self
    }
    pub fn to_sql(&self) -> Result<String> {
        let mut s = String::from("DROP TABLE");
        if self.if_exists {
            s.push_str(" IF EXISTS");
        }
        s.push(' ');
        s.push_str(&ident::quote(self.backend, &self.table)?);
        if self.cascade && self.backend == Backend::Postgres {
            s.push_str(" CASCADE");
        }
        Ok(s)
    }
}

#[derive(Debug, Clone)]
pub enum AlterAction {
    AddColumn(ColDef),
    DropColumn(String),
    RenameColumn { from: String, to: String },
    RenameTable { to: String },
    /// MySQL: `CONVERT TO CHARACTER SET ... COLLATE ...` (convierte datos).
    /// PG/SQLite: skip silencioso (no tienen equivalente).
    ConvertToCharset { charset: String, collation: Option<String> },
    /// MySQL: `DEFAULT CHARACTER SET ... COLLATE ...` (solo default,
    /// no convierte datos existentes).
    SetDefaultCharset { charset: String, collation: Option<String> },
    /// MySQL: `ENGINE=...`.
    SetEngine(String),
}

#[derive(Debug, Clone)]
pub struct AlterTable {
    backend: Backend,
    table: String,
    actions: Vec<AlterAction>,
}

impl AlterTable {
    pub fn new(backend: Backend, table: &str) -> Self {
        Self { backend, table: table.to_string(), actions: Vec::new() }
    }
    pub fn add_column(mut self, c: ColDef) -> Self {
        self.actions.push(AlterAction::AddColumn(c));
        self
    }
    pub fn drop_column<S: Into<String>>(mut self, name: S) -> Self {
        self.actions.push(AlterAction::DropColumn(name.into()));
        self
    }
    pub fn rename_column<A: Into<String>, B: Into<String>>(mut self, from: A, to: B) -> Self {
        self.actions.push(AlterAction::RenameColumn { from: from.into(), to: to.into() });
        self
    }
    pub fn rename_table<S: Into<String>>(mut self, to: S) -> Self {
        self.actions.push(AlterAction::RenameTable { to: to.into() });
        self
    }
    /// `CONVERT TO CHARACTER SET <cs>` (MySQL). Convierte datos existentes.
    pub fn convert_to_charset<S: Into<String>>(mut self, charset: S) -> Self {
        self.actions.push(AlterAction::ConvertToCharset {
            charset: charset.into(),
            collation: None,
        });
        self
    }
    /// `CONVERT TO CHARACTER SET <cs> COLLATE <co>` (MySQL).
    pub fn convert_to_charset_collate<A: Into<String>, B: Into<String>>(
        mut self,
        charset: A,
        collation: B,
    ) -> Self {
        self.actions.push(AlterAction::ConvertToCharset {
            charset: charset.into(),
            collation: Some(collation.into()),
        });
        self
    }
    /// `DEFAULT CHARACTER SET <cs>` (MySQL). No toca datos existentes.
    pub fn set_default_charset<S: Into<String>>(mut self, charset: S) -> Self {
        self.actions.push(AlterAction::SetDefaultCharset {
            charset: charset.into(),
            collation: None,
        });
        self
    }
    /// `DEFAULT CHARACTER SET <cs> COLLATE <co>` (MySQL).
    pub fn set_default_charset_collate<A: Into<String>, B: Into<String>>(
        mut self,
        charset: A,
        collation: B,
    ) -> Self {
        self.actions.push(AlterAction::SetDefaultCharset {
            charset: charset.into(),
            collation: Some(collation.into()),
        });
        self
    }
    /// `ENGINE=<name>` (MySQL).
    pub fn set_engine<S: Into<String>>(mut self, engine: S) -> Self {
        self.actions.push(AlterAction::SetEngine(engine.into()));
        self
    }

    /// Retorna una o más sentencias (algunos backends no soportan
    /// múltiples acciones en un solo ALTER).
    pub fn to_sql(&self) -> Result<Vec<String>> {
        let qtable = ident::quote(self.backend, &self.table)?;
        let mut out = Vec::new();
        for a in &self.actions {
            match a {
                AlterAction::AddColumn(c) => {
                    out.push(format!("ALTER TABLE {} ADD COLUMN {}", qtable, c.render(self.backend)?));
                }
                AlterAction::DropColumn(n) => {
                    out.push(format!(
                        "ALTER TABLE {} DROP COLUMN {}",
                        qtable,
                        ident::quote(self.backend, n)?
                    ));
                }
                AlterAction::RenameColumn { from, to } => {
                    let qf = ident::quote(self.backend, from)?;
                    let qt = ident::quote(self.backend, to)?;
                    out.push(match self.backend {
                        Backend::MySql => format!("ALTER TABLE {} CHANGE {} {} /* MySQL: añade tipo si lo necesitás vía Raw */", qtable, qf, qt),
                        _ => format!("ALTER TABLE {} RENAME COLUMN {} TO {}", qtable, qf, qt),
                    });
                }
                AlterAction::RenameTable { to } => {
                    let qt = ident::quote(self.backend, to)?;
                    out.push(match self.backend {
                        Backend::MySql => format!("RENAME TABLE {} TO {}", qtable, qt),
                        _ => format!("ALTER TABLE {} RENAME TO {}", qtable, qt),
                    });
                }
                AlterAction::ConvertToCharset { charset, collation } => {
                    validate_collation_name(charset)?;
                    if let Some(co) = collation {
                        validate_collation_name(co)?;
                    }
                    if self.backend == Backend::MySql {
                        let mut s = format!("ALTER TABLE {} CONVERT TO CHARACTER SET {}", qtable, charset);
                        if let Some(co) = collation {
                            s.push_str(&format!(" COLLATE {}", co));
                        }
                        out.push(s);
                    }
                    // PG/SQLite: skip silencioso (sin equivalente directo).
                }
                AlterAction::SetDefaultCharset { charset, collation } => {
                    validate_collation_name(charset)?;
                    if let Some(co) = collation {
                        validate_collation_name(co)?;
                    }
                    if self.backend == Backend::MySql {
                        let mut s = format!("ALTER TABLE {} DEFAULT CHARACTER SET {}", qtable, charset);
                        if let Some(co) = collation {
                            s.push_str(&format!(" COLLATE {}", co));
                        }
                        out.push(s);
                    }
                }
                AlterAction::SetEngine(eng) => {
                    validate_collation_name(eng)?;
                    if self.backend == Backend::MySql {
                        out.push(format!("ALTER TABLE {} ENGINE={}", qtable, eng));
                    }
                }
            }
        }
        Ok(out)
    }
}

// CREATE DATABASE / DROP DATABASE

#[derive(Debug, Clone)]
pub struct CreateDatabase {
    backend: Backend,
    name: String,
    if_not_exists: bool,
    charset: Option<String>,
    collation: Option<String>,
}

impl CreateDatabase {
    pub fn new(backend: Backend, name: &str) -> Self {
        Self {
            backend,
            name: name.to_string(),
            if_not_exists: false,
            charset: None,
            collation: None,
        }
    }
    pub fn if_not_exists(mut self) -> Self {
        self.if_not_exists = true;
        self
    }
    /// MySQL: `DEFAULT CHARSET=<name>` · Postgres: `ENCODING '<name>'`.
    pub fn default_charset<S: Into<String>>(mut self, s: S) -> Self {
        self.charset = Some(s.into());
        self
    }
    /// MySQL: `COLLATE=<name>` · Postgres: `LC_COLLATE '<name>'`.
    pub fn default_collation<S: Into<String>>(mut self, s: S) -> Self {
        self.collation = Some(s.into());
        self
    }
    pub fn to_sql(&self) -> Result<String> {
        ident::validate(&self.name)?;
        if self.backend == Backend::Sqlite {
            return Err(QueryError::InvalidOperator(
                "CREATE DATABASE no aplica en SQLite (un archivo = una BD)".to_string(),
            ));
        }
        let mut sql = String::from("CREATE DATABASE");
        if self.if_not_exists {
            sql.push_str(" IF NOT EXISTS");
        }
        sql.push(' ');
        sql.push_str(&self.backend.quote_ident(&self.name));
        match self.backend {
            Backend::MySql => {
                if let Some(cs) = &self.charset {
                    validate_collation_name(cs)?;
                    sql.push_str(&format!(" DEFAULT CHARSET={}", cs));
                }
                if let Some(co) = &self.collation {
                    validate_collation_name(co)?;
                    sql.push_str(&format!(" COLLATE={}", co));
                }
            }
            Backend::Postgres => {
                if let Some(cs) = &self.charset {
                    validate_collation_name(cs)?;
                    sql.push_str(&format!(" ENCODING '{}'", cs));
                }
                if let Some(co) = &self.collation {
                    validate_collation_name(co)?;
                    sql.push_str(&format!(" LC_COLLATE '{}'", co));
                }
            }
            Backend::Sqlite => unreachable!(),
        }
        Ok(sql)
    }
}

#[derive(Debug, Clone)]
pub struct DropDatabase {
    backend: Backend,
    name: String,
    if_exists: bool,
}

impl DropDatabase {
    pub fn new(backend: Backend, name: &str) -> Self {
        Self { backend, name: name.to_string(), if_exists: false }
    }
    pub fn if_exists(mut self) -> Self {
        self.if_exists = true;
        self
    }
    pub fn to_sql(&self) -> Result<String> {
        ident::validate(&self.name)?;
        if self.backend == Backend::Sqlite {
            return Err(QueryError::InvalidOperator(
                "DROP DATABASE no aplica en SQLite".to_string(),
            ));
        }
        let mut sql = String::from("DROP DATABASE");
        if self.if_exists {
            sql.push_str(" IF EXISTS");
        }
        sql.push(' ');
        sql.push_str(&self.backend.quote_ident(&self.name));
        Ok(sql)
    }
}