hyperdb-api 0.1.0

Pure Rust API for Hyper database
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! SQL name types for safe identifier handling.
//!
//! This module provides types that properly escape SQL identifiers to prevent
//! SQL injection attacks.

use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;

use smallvec::SmallVec;

use crate::error::{Error, Result};

/// `PostgreSQL` identifier length limit (in characters).
pub(crate) const PG_IDENTIFIER_LIMIT: usize = 63;

/// Escapes a SQL identifier for safe use in queries.
///
/// This function properly quotes and escapes a name to prevent SQL injection.
/// The result is wrapped in double quotes with internal quotes escaped.
///
/// # Errors
///
/// Returns an error if the name exceeds the `PostgreSQL` identifier limit (63 characters).
/// This behavior is consistent with [`Name::try_new()`].
///
/// # Example
///
/// ```
/// use hyperdb_api::{escape_name, Result};
///
/// fn demo() -> Result<()> {
///     let escaped = escape_name("my_table")?;
///     assert_eq!(escaped, "\"my_table\"");
///
///     let special = escape_name("table\"with\"quotes")?;
///     assert_eq!(special, "\"table\"\"with\"\"quotes\"");
///     Ok(())
/// }
///
/// // Names exceeding 63 characters are rejected
/// let long_name = "a".repeat(64);
/// assert!(escape_name(&long_name).is_err());
/// ```
pub fn escape_name(name: &str) -> Result<String> {
    let len = name.chars().count();
    if len > PG_IDENTIFIER_LIMIT {
        return Err(Error::InvalidName(format!(
            "Name exceeds PostgreSQL identifier limit ({len} > {PG_IDENTIFIER_LIMIT})"
        )));
    }

    let escaped_inner = name.replace('"', "\"\"");
    Ok(format!("\"{escaped_inner}\""))
}

/// Escapes a database file path for safe use in SQL statements.
///
/// This function wraps the path in double quotes and escapes internal quotes,
/// just like [`escape_name()`], but **without** the 63-character `PostgreSQL`
/// identifier length limit. Use this for `CREATE DATABASE`, `ATTACH DATABASE`,
/// `COPY DATABASE`, and similar statements where the argument is a file path
/// rather than an SQL identifier.
///
/// # Example
///
/// ```
/// use hyperdb_api::escape_sql_path;
///
/// let simple = escape_sql_path("/tmp/data.hyper");
/// assert_eq!(simple, "\"/tmp/data.hyper\"");
///
/// let special = escape_sql_path("/tmp/my \"db\".hyper");
/// assert_eq!(special, "\"/tmp/my \"\"db\"\".hyper\"");
/// ```
#[must_use]
pub fn escape_sql_path(path: &str) -> String {
    let escaped_inner = path.replace('"', "\"\"");
    format!("\"{escaped_inner}\"")
}

/// Escapes a SQL string literal for safe use in queries.
///
/// This function properly quotes and escapes a string value to prevent SQL injection.
/// The result is wrapped in single quotes with internal quotes escaped.
///
/// # Example
///
/// ```
/// use hyperdb_api::escape_string_literal;
///
/// let escaped = escape_string_literal("hello");
/// assert_eq!(escaped, "'hello'");
///
/// let special = escape_string_literal("it's a test");
/// assert_eq!(special, "'it''s a test'");
/// ```
#[must_use]
pub fn escape_string_literal(value: &str) -> String {
    format!("'{}'", value.replace('\'', "''"))
}

/// Represents an escaped SQL identifier name.
///
/// `Name` stores both the properly quoted/escaped version (safe for SQL) and
/// the original unescaped version (for display/logging).
///
/// # Example
///
/// ```
/// use hyperdb_api::Name;
///
/// let name = Name::try_new("users")?;
/// assert_eq!(name.to_string(), "\"users\"");
/// assert_eq!(name.unescaped(), "users");
/// # Ok::<(), hyperdb_api::Error>(())
/// ```
#[derive(Clone, Debug)]
#[must_use = "Name represents a validated SQL identifier that should not be discarded. Use it in your SQL queries or table definitions"]
pub struct Name {
    /// The escaped name (safe for SQL).
    escaped: String,
    /// The original unescaped name.
    unescaped: String,
}

impl Name {
    /// Creates a new escaped SQL name.
    ///
    /// # Errors
    ///
    /// Returns an error if the name is empty or exceeds the `PostgreSQL` identifier limit (63 characters).
    ///
    /// # Example
    ///
    /// ```
    /// use hyperdb_api::Name;
    ///
    /// let name = Name::try_new("users")?;
    /// assert_eq!(name.unescaped(), "users");
    /// # Ok::<(), hyperdb_api::Error>(())
    /// ```
    pub fn try_new(name: impl Into<String>) -> Result<Self> {
        let unescaped = name.into();
        if unescaped.is_empty() {
            return Err(Error::InvalidName("Name must not be empty".into()));
        }
        // escape_name validates the length limit and returns an error if exceeded
        let escaped = escape_name(&unescaped)?;
        Ok(Name { escaped, unescaped })
    }

    /// Returns the properly quoted and escaped string representation.
    ///
    /// This is safe to use directly in SQL queries.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.escaped
    }

    /// Returns the original unescaped name.
    ///
    /// **Warning:** Do not use this in SQL queries as it may be vulnerable to
    /// SQL injection. Use this only for logging or display purposes.
    #[must_use]
    pub fn unescaped(&self) -> &str {
        &self.unescaped
    }
}

/// Parses a dot-separated SQL identifier into parts, handling quoted sections.
///
/// This is a common parsing function used by Name, `SchemaName`, and `TableName`.
/// Uses `SmallVec` for efficiency since most identifiers have 1-3 parts.
fn parse_qualified_identifier(s: &str) -> SmallVec<[String; 3]> {
    let mut parts = SmallVec::new();
    let mut current = String::new();
    let mut in_quotes = false;
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            // Toggle the "in_quotes" state
            '"' => {
                // Handle escaped quotes (double double-quotes)
                if in_quotes && chars.peek() == Some(&'"') {
                    current.push('"');
                    chars.next(); // skip the second quote
                } else {
                    in_quotes = !in_quotes;
                    // Don't add the quote character itself to current
                }
            }
            // Split on dots, but ONLY if we aren't inside quotes
            '.' if !in_quotes => {
                if !current.is_empty() {
                    parts.push(current.split_off(0));
                }
            }
            _ => current.push(c),
        }
    }
    if !current.is_empty() {
        parts.push(current);
    }
    parts
}

impl fmt::Display for Name {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.escaped)
    }
}

impl PartialEq for Name {
    fn eq(&self, other: &Self) -> bool {
        self.unescaped == other.unescaped
    }
}

impl Eq for Name {}

impl PartialOrd for Name {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Name {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.unescaped.cmp(&other.unescaped)
    }
}

impl Hash for Name {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.unescaped.hash(state);
    }
}

impl TryFrom<&str> for Name {
    type Error = Error;

    fn try_from(s: &str) -> Result<Self> {
        Self::try_new(s)
    }
}

impl TryFrom<&String> for Name {
    type Error = Error;

    fn try_from(s: &String) -> Result<Self> {
        Self::try_new(s.as_str())
    }
}

impl TryFrom<String> for Name {
    type Error = Error;

    fn try_from(s: String) -> Result<Self> {
        Self::try_new(s)
    }
}

impl FromStr for Name {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        Self::try_new(s)
    }
}

/// Represents an escaped SQL database name.
///
/// # Example
///
/// ```
/// use hyperdb_api::{DatabaseName, Result};
///
/// # fn main() -> Result<()> {
/// let db = DatabaseName::try_new("mydb")?;
/// assert_eq!(db.to_string(), "\"mydb\"");
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[must_use = "DatabaseName represents a validated database identifier that should not be discarded. Use it in your connection or table definitions"]
pub struct DatabaseName {
    name: Name,
}

impl DatabaseName {
    /// Creates a new database name.
    ///
    /// # Errors
    ///
    /// Returns an error if the name is empty or exceeds the `PostgreSQL` identifier limit.
    pub fn try_new(name: impl Into<String>) -> Result<Self> {
        Ok(DatabaseName {
            name: Name::try_new(name)?,
        })
    }

    /// Returns the name component.
    pub fn name(&self) -> &Name {
        &self.name
    }

    /// Returns the unescaped name.
    #[must_use]
    pub fn unescaped(&self) -> &str {
        self.name.unescaped()
    }
}

impl fmt::Display for DatabaseName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl TryFrom<&str> for DatabaseName {
    type Error = Error;

    fn try_from(s: &str) -> Result<Self> {
        Self::try_new(s)
    }
}

impl TryFrom<&String> for DatabaseName {
    type Error = Error;

    fn try_from(s: &String) -> Result<Self> {
        Self::try_new(s.as_str())
    }
}

impl TryFrom<String> for DatabaseName {
    type Error = Error;

    fn try_from(s: String) -> Result<Self> {
        Self::try_new(s)
    }
}

impl From<Name> for DatabaseName {
    fn from(name: Name) -> Self {
        DatabaseName { name }
    }
}

impl FromStr for DatabaseName {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        Self::try_new(s)
    }
}

/// Represents an escaped SQL schema name with optional database qualifier.
///
/// Uses the fluent builder pattern for constructing qualified schema names.
///
/// # Example
///
/// ```
/// use hyperdb_api::{SchemaName, Result};
///
/// # fn main() -> Result<()> {
/// // Simple schema name
/// let schema = SchemaName::try_new("public")?;
/// assert_eq!(schema.to_string(), "\"public\"");
///
/// // Qualified schema name using fluent builder
/// let qualified = SchemaName::try_new("public")?.with_database("mydb")?;
/// assert_eq!(qualified.to_string(), "\"mydb\".\"public\"");
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[must_use = "SchemaName represents a validated schema identifier that should not be discarded. Use it in your table definitions or queries"]
pub struct SchemaName {
    database: Option<DatabaseName>,
    schema: Name,
}

impl SchemaName {
    /// Creates a new schema name without a database qualifier (the starting point).
    ///
    /// # Errors
    ///
    /// Returns an error if the schema name is empty or exceeds the `PostgreSQL` identifier limit.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hyperdb_api::SchemaName;
    ///
    /// let schema = SchemaName::try_new("public")?;
    /// assert_eq!(schema.to_string(), "\"public\"");
    /// # Ok::<(), hyperdb_api::Error>(())
    /// ```
    pub fn try_new(schema: impl Into<String>) -> Result<Self> {
        Ok(SchemaName {
            database: None,
            schema: Name::try_new(schema)?,
        })
    }

    /// Builder method: Sets the database qualifier.
    ///
    /// This method is part of the fluent builder pattern and can be chained.
    /// Returns `Result<Self>` to allow fallible method chaining.
    ///
    /// # Errors
    ///
    /// Returns an error if the database name is empty or exceeds the `PostgreSQL` identifier limit.
    ///
    /// # Example
    ///
    /// ```
    /// use hyperdb_api::SchemaName;
    ///
    /// let schema = SchemaName::try_new("public")?.with_database("mydb")?;
    /// assert_eq!(schema.to_string(), "\"mydb\".\"public\"");
    /// # Ok::<(), hyperdb_api::Error>(())
    /// ```
    pub fn with_database(mut self, database: impl Into<String>) -> Result<Self> {
        self.database = Some(DatabaseName::try_new(database)?);
        Ok(self)
    }

    /// Returns the database name, if any.
    #[must_use]
    pub fn database(&self) -> Option<&DatabaseName> {
        self.database.as_ref()
    }

    /// Returns the schema name component.
    pub fn schema(&self) -> &Name {
        &self.schema
    }

    /// Returns the unescaped schema name.
    #[must_use]
    pub fn unescaped(&self) -> &str {
        self.schema.unescaped()
    }
}

impl fmt::Display for SchemaName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref db) = self.database {
            write!(f, "{}.{}", db, self.schema)
        } else {
            write!(f, "{}", self.schema)
        }
    }
}

impl TryFrom<&str> for SchemaName {
    type Error = Error;

    fn try_from(s: &str) -> Result<Self> {
        s.parse()
    }
}

impl TryFrom<&String> for SchemaName {
    type Error = Error;

    fn try_from(s: &String) -> Result<Self> {
        s.as_str().parse()
    }
}

impl TryFrom<String> for SchemaName {
    type Error = Error;

    fn try_from(s: String) -> Result<Self> {
        s.parse()
    }
}

impl From<Name> for SchemaName {
    fn from(name: Name) -> Self {
        SchemaName {
            database: None,
            schema: name,
        }
    }
}

impl FromStr for SchemaName {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let parts = parse_qualified_identifier(s);

        // Parse database.schema format
        match parts.as_slice() {
            [s] => SchemaName::try_new(s),
            [d, s] => SchemaName::try_new(s)?.with_database(d),
            _ => Err(Error::InvalidName(format!("Invalid SQL identifier: {s}"))),
        }
    }
}

/// Represents a fully qualified SQL table name.
///
/// A table name can optionally include database and schema qualifiers.
/// Uses the fluent builder pattern for constructing qualified table names.
///
/// # Example
///
/// ```
/// use hyperdb_api::{TableName, Result};
///
/// # fn main() -> Result<()> {
/// // Simple table name
/// let table = TableName::try_new("users")?;
/// assert_eq!(table.to_string(), "\"users\"");
///
/// // With schema using fluent builder
/// let with_schema = TableName::try_new("users")?.with_schema("public")?;
/// assert_eq!(with_schema.to_string(), "\"public\".\"users\"");
///
/// // Fully qualified using fluent builder
/// let full = TableName::try_new("users")?
///     .with_schema("public")?
///     .with_database("mydb")?;
/// assert_eq!(full.to_string(), "\"mydb\".\"public\".\"users\"");
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[must_use = "TableName represents a validated table identifier that should not be discarded. Use it in your queries or table operations"]
pub struct TableName {
    database: Option<DatabaseName>,
    schema: Option<Name>,
    table: Name,
}

impl TableName {
    /// Creates a new table name without qualifiers (the starting point).
    ///
    /// # Errors
    ///
    /// Returns an error if the table name is empty or exceeds the `PostgreSQL` identifier limit.
    ///
    /// # Example
    ///
    /// ```
    /// use hyperdb_api::TableName;
    ///
    /// let table = TableName::try_new("users")?;
    /// assert_eq!(table.to_string(), "\"users\"");
    /// # Ok::<(), hyperdb_api::Error>(())
    /// ```
    pub fn try_new(table: impl Into<String>) -> Result<Self> {
        Ok(TableName {
            database: None,
            schema: None,
            table: Name::try_new(table)?,
        })
    }

    /// Builder method: Sets the schema qualifier.
    ///
    /// This method is part of the fluent builder pattern and can be chained.
    /// Returns `Result<Self>` to allow fallible method chaining.
    ///
    /// # Errors
    ///
    /// Returns an error if the schema name is empty or exceeds the `PostgreSQL` identifier limit.
    ///
    /// # Example
    ///
    /// ```
    /// use hyperdb_api::TableName;
    ///
    /// let table = TableName::try_new("users")?.with_schema("public")?;
    /// assert_eq!(table.to_string(), "\"public\".\"users\"");
    /// # Ok::<(), hyperdb_api::Error>(())
    /// ```
    pub fn with_schema(mut self, schema: impl Into<String>) -> Result<Self> {
        self.schema = Some(Name::try_new(schema)?);
        Ok(self)
    }

    /// Builder method: Sets the database qualifier.
    ///
    /// This method is part of the fluent builder pattern and can be chained.
    /// Returns `Result<Self>` to allow fallible method chaining.
    ///
    /// # Errors
    ///
    /// Returns an error if the database name is empty or exceeds the `PostgreSQL` identifier limit.
    ///
    /// # Example
    ///
    /// ```
    /// use hyperdb_api::TableName;
    ///
    /// let table = TableName::try_new("users")?
    ///     .with_schema("public")?
    ///     .with_database("mydb")?;
    /// assert_eq!(table.to_string(), "\"mydb\".\"public\".\"users\"");
    /// # Ok::<(), hyperdb_api::Error>(())
    /// ```
    pub fn with_database(mut self, database: impl Into<String>) -> Result<Self> {
        self.database = Some(DatabaseName::try_new(database)?);
        Ok(self)
    }

    /// Returns the database name, if any.
    #[must_use]
    pub fn database(&self) -> Option<&DatabaseName> {
        self.database.as_ref()
    }

    /// Returns the schema name, if any.
    #[must_use]
    pub fn schema(&self) -> Option<&Name> {
        self.schema.as_ref()
    }

    /// Returns the table name component.
    pub fn table(&self) -> &Name {
        &self.table
    }

    /// Returns the unescaped table name.
    #[must_use]
    pub fn unescaped(&self) -> &str {
        self.table.unescaped()
    }
}

impl fmt::Display for TableName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref db) = self.database {
            write!(f, "{db}.")?;
        }
        if let Some(ref schema) = self.schema {
            write!(f, "{schema}.")?;
        }
        write!(f, "{}", self.table)
    }
}

impl TryFrom<&str> for TableName {
    type Error = Error;

    fn try_from(s: &str) -> Result<Self> {
        s.parse()
    }
}

impl TryFrom<&String> for TableName {
    type Error = Error;

    fn try_from(s: &String) -> Result<Self> {
        s.as_str().parse()
    }
}

impl TryFrom<String> for TableName {
    type Error = Error;

    fn try_from(s: String) -> Result<Self> {
        s.parse()
    }
}

impl From<Name> for TableName {
    fn from(name: Name) -> Self {
        TableName {
            database: None,
            schema: None,
            table: name,
        }
    }
}

impl FromStr for TableName {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let mut parts = Vec::new();
        let mut current = String::new();
        let mut in_quotes = false;
        let mut chars = s.chars().peekable();

        while let Some(c) = chars.next() {
            match c {
                // Toggle the "in_quotes" state
                '"' => {
                    // Handle escaped quotes (double double-quotes)
                    if in_quotes && chars.peek() == Some(&'"') {
                        current.push('"');
                        chars.next(); // skip the second quote
                    } else {
                        in_quotes = !in_quotes;
                        // Don't add the quote character itself to current
                    }
                }
                // Split on dots, but ONLY if we aren't inside quotes
                '.' if !in_quotes => {
                    if !current.is_empty() {
                        parts.push(current.split_off(0));
                    }
                }
                _ => current.push(c),
            }
        }
        if !current.is_empty() {
            parts.push(current);
        }

        // Now we use the same match logic as before
        match parts.as_slice() {
            [t] => TableName::try_new(t),
            [s, t] => TableName::try_new(t)?.with_schema(s),
            [d, s, t] => TableName::try_new(t)?.with_schema(s)?.with_database(d),
            _ => Err(Error::InvalidName(format!("Invalid SQL identifier: {s}"))),
        }
    }
}

/// Creates a `TableName` with optional database and schema qualifiers.
///
/// This macro provides a convenient way to create table names with different
/// levels of qualification. Returns a `Result` that must be handled with `?` or `.unwrap()`.
///
/// # Examples
///
/// ```
/// use hyperdb_api::table_name;
///
/// // Simple table name
/// let table = table_name!("users")?;
/// assert_eq!(table.to_string(), "\"users\"");
///
/// // With schema
/// let table = table_name!("public", "users")?;
/// assert_eq!(table.to_string(), "\"public\".\"users\"");
///
/// // Fully qualified
/// let table = table_name!("mydb", "public", "users")?;
/// assert_eq!(table.to_string(), "\"mydb\".\"public\".\"users\"");
/// # Ok::<(), hyperdb_api::Error>(())
/// ```
#[macro_export]
macro_rules! table_name {
    // Case: table_name!(db, schema, table)
    ($db:expr, $schema:expr, $table:expr) => {
        $crate::TableName::try_new($table)?
            .with_schema($schema)?
            .with_database($db)
    };

    // Case: table_name!(schema, table)
    ($schema:expr, $table:expr) => {
        $crate::TableName::try_new($table)?.with_schema($schema)
    };

    // Case: table_name!(table)
    ($table:expr) => {
        $crate::TableName::try_new($table)
    };
}

/// Creates a `SchemaName` with optional database qualifier.
///
/// This macro provides a convenient way to create schema names with or without
/// a database qualifier. Returns a `Result` that must be handled with `?` or `.unwrap()`.
///
/// # Examples
///
/// ```
/// use hyperdb_api::schema_name;
///
/// // Simple schema name
/// let schema = schema_name!("public")?;
/// assert_eq!(schema.to_string(), "\"public\"");
///
/// // With database
/// let schema = schema_name!("mydb", "public")?;
/// assert_eq!(schema.to_string(), "\"mydb\".\"public\"");
/// # Ok::<(), hyperdb_api::Error>(())
/// ```
#[macro_export]
macro_rules! schema_name {
    // Case: schema_name!(db, schema)
    ($db:expr, $schema:expr) => {
        $crate::SchemaName::try_new($schema)?.with_database($db)
    };

    // Case: schema_name!(schema)
    ($schema:expr) => {
        $crate::SchemaName::try_new($schema)
    };
}

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

    #[test]
    fn test_escape_name() {
        assert_eq!(escape_name("table").unwrap(), "\"table\"");
        assert_eq!(escape_name("my_table").unwrap(), "\"my_table\"");
        assert_eq!(escape_name("table\"quote").unwrap(), "\"table\"\"quote\"");
        assert_eq!(escape_name("").unwrap(), "\"\"");
    }

    #[test]
    fn test_escape_name_too_long() {
        // 63 characters should be OK
        let max_name = "a".repeat(PG_IDENTIFIER_LIMIT);
        assert!(escape_name(&max_name).is_ok());

        // 64 characters should fail
        let too_long = "a".repeat(PG_IDENTIFIER_LIMIT + 1);
        let err = escape_name(&too_long).unwrap_err();
        assert!(err.to_string().contains("identifier limit"));
    }

    #[test]
    fn test_escape_sql_path() {
        assert_eq!(escape_sql_path("/tmp/data.hyper"), "\"/tmp/data.hyper\"");
        assert_eq!(
            escape_sql_path("/tmp/my \"db\".hyper"),
            "\"/tmp/my \"\"db\"\".hyper\""
        );
        assert_eq!(escape_sql_path(""), "\"\"");

        // Long paths are allowed (no 63-char limit)
        let long_path = format!("/very/long/path/{}.hyper", "a".repeat(100));
        let escaped = escape_sql_path(&long_path);
        assert!(escaped.starts_with('"'));
        assert!(escaped.ends_with('"'));
    }

    #[test]
    fn test_escape_string_literal() {
        assert_eq!(escape_string_literal("hello"), "'hello'");
        assert_eq!(escape_string_literal("it's"), "'it''s'");
        assert_eq!(escape_string_literal(""), "''");
    }

    #[test]
    fn test_name() {
        let name = Name::try_new("users").unwrap();
        assert_eq!(name.to_string(), "\"users\"");
        assert_eq!(name.unescaped(), "users");
        assert!(!name.unescaped().is_empty());
    }

    #[test]
    fn test_name_with_quotes() {
        let name = Name::try_new("table\"name").unwrap();
        assert_eq!(name.to_string(), "\"table\"\"name\"");
        assert_eq!(name.unescaped(), "table\"name");
    }

    #[test]
    fn test_database_name() {
        let db = DatabaseName::try_new("mydb").unwrap();
        assert_eq!(db.to_string(), "\"mydb\"");
        assert_eq!(db.unescaped(), "mydb");
    }

    #[test]
    fn test_schema_name() {
        let schema = SchemaName::try_new("public").unwrap();
        assert_eq!(schema.to_string(), "\"public\"");

        let qualified = SchemaName::try_new("public")
            .unwrap()
            .with_database("mydb")
            .unwrap();
        assert_eq!(qualified.to_string(), "\"mydb\".\"public\"");
    }

    #[test]
    fn test_table_name() {
        let simple = TableName::try_new("users").unwrap();
        assert_eq!(simple.to_string(), "\"users\"");

        let with_schema = TableName::try_new("users")
            .unwrap()
            .with_schema("public")
            .unwrap();
        assert_eq!(with_schema.to_string(), "\"public\".\"users\"");

        let full = TableName::try_new("users")
            .unwrap()
            .with_schema("public")
            .unwrap()
            .with_database("mydb")
            .unwrap();
        assert_eq!(full.to_string(), "\"mydb\".\"public\".\"users\"");
    }

    #[test]
    fn test_name_equality() {
        let name1 = Name::try_new("test").unwrap();
        let name2 = Name::try_new("test").unwrap();
        let name3 = Name::try_new("other").unwrap();

        assert_eq!(name1, name2);
        assert_ne!(name1, name3);
    }

    #[test]
    fn test_schema_name_from_str() {
        // Simple schema name (using .parse() which uses FromStr)
        let schema: SchemaName = "public".parse().unwrap();
        assert_eq!(schema.to_string(), "\"public\"");
        assert_eq!(schema.unescaped(), "public");

        // Database.schema format
        let qualified: SchemaName = "mydb.public".parse().unwrap();
        assert_eq!(qualified.to_string(), "\"mydb\".\"public\"");
        assert_eq!(qualified.unescaped(), "public");
        assert_eq!(qualified.database().unwrap().unescaped(), "mydb");

        // Quoted identifiers
        let quoted: SchemaName = "\"my db\".\"my schema\"".parse().unwrap();
        assert_eq!(quoted.to_string(), "\"my db\".\"my schema\"");
        assert_eq!(quoted.unescaped(), "my schema");

        // Escaped quotes
        let escaped: SchemaName = "\"schema\"\"name\"".parse().unwrap();
        assert_eq!(escaped.to_string(), "\"schema\"\"name\"");
        assert_eq!(escaped.unescaped(), "schema\"name");

        // Invalid formats (testing FromStr error handling)
        assert!("db.schema.table".parse::<SchemaName>().is_err());
        assert!("".parse::<SchemaName>().is_err());
    }

    #[test]
    fn test_table_name_from_str() {
        // Simple table name (using .parse() which uses FromStr)
        let table: TableName = "users".parse().unwrap();
        assert_eq!(table.to_string(), "\"users\"");
        assert_eq!(table.unescaped(), "users");

        // Schema.table format
        let with_schema: TableName = "public.users".parse().unwrap();
        assert_eq!(with_schema.to_string(), "\"public\".\"users\"");
        assert_eq!(with_schema.unescaped(), "users");
        assert_eq!(with_schema.schema().unwrap().unescaped(), "public");

        // Database.schema.table format
        let full: TableName = "mydb.public.users".parse().unwrap();
        assert_eq!(full.to_string(), "\"mydb\".\"public\".\"users\"");
        assert_eq!(full.unescaped(), "users");
        assert_eq!(full.schema().unwrap().unescaped(), "public");
        assert_eq!(full.database().unwrap().unescaped(), "mydb");

        // Quoted identifiers
        let quoted: TableName = "\"my db\".\"my schema\".\"my table\"".parse().unwrap();
        assert_eq!(quoted.to_string(), "\"my db\".\"my schema\".\"my table\"");
        assert_eq!(quoted.unescaped(), "my table");

        // Escaped quotes
        let escaped: TableName = "\"table\"\"name\"".parse().unwrap();
        assert_eq!(escaped.to_string(), "\"table\"\"name\"");
        assert_eq!(escaped.unescaped(), "table\"name");

        // Dots inside quoted identifiers should not split
        let with_dots: TableName = "\"schema.name\".\"table.name\"".parse().unwrap();
        assert_eq!(with_dots.to_string(), "\"schema.name\".\"table.name\"");
        assert_eq!(with_dots.schema().unwrap().unescaped(), "schema.name");
        assert_eq!(with_dots.unescaped(), "table.name");

        // Invalid formats (testing FromStr error handling)
        assert!("db.schema.table.extra".parse::<TableName>().is_err());
        assert!("".parse::<TableName>().is_err());
    }

    #[test]
    fn test_schema_name_macro() -> Result<()> {
        // Simple schema name
        let schema = schema_name!("public")?;
        assert_eq!(schema.to_string(), "\"public\"");
        assert_eq!(schema.unescaped(), "public");

        // With database
        let qualified = schema_name!("mydb", "public")?;
        assert_eq!(qualified.to_string(), "\"mydb\".\"public\"");
        assert_eq!(qualified.unescaped(), "public");
        assert_eq!(qualified.database().unwrap().unescaped(), "mydb");
        Ok(())
    }

    #[test]
    fn test_table_name_macro() -> Result<()> {
        // Simple table name
        let table = table_name!("users")?;
        assert_eq!(table.to_string(), "\"users\"");
        assert_eq!(table.unescaped(), "users");

        // With schema
        let with_schema = table_name!("public", "users")?;
        assert_eq!(with_schema.to_string(), "\"public\".\"users\"");
        assert_eq!(with_schema.unescaped(), "users");
        assert_eq!(with_schema.schema().unwrap().unescaped(), "public");

        // Fully qualified
        let full = table_name!("mydb", "public", "users")?;
        assert_eq!(full.to_string(), "\"mydb\".\"public\".\"users\"");
        assert_eq!(full.unescaped(), "users");
        assert_eq!(full.schema().unwrap().unescaped(), "public");
        assert_eq!(full.database().unwrap().unescaped(), "mydb");
        Ok(())
    }

    #[test]
    fn test_schema_name_try_from() {
        // Simple schema name using TryFrom
        let schema: SchemaName = "public".try_into().unwrap();
        assert_eq!(schema.to_string(), "\"public\"");
        assert_eq!(schema.unescaped(), "public");

        // Qualified schema using TryFrom (parses dot-separated format)
        let qualified: SchemaName = "mydb.public".try_into().unwrap();
        assert_eq!(qualified.to_string(), "\"mydb\".\"public\"");
        assert_eq!(qualified.unescaped(), "public");
        assert_eq!(qualified.database().unwrap().unescaped(), "mydb");

        // From String
        let schema_string: SchemaName = String::from("public").try_into().unwrap();
        assert_eq!(schema_string.to_string(), "\"public\"");
    }

    #[test]
    fn test_table_name_try_from() {
        // Simple table name using TryFrom
        let table: TableName = "users".try_into().unwrap();
        assert_eq!(table.to_string(), "\"users\"");
        assert_eq!(table.unescaped(), "users");

        // With schema using TryFrom (parses dot-separated format)
        let with_schema: TableName = "public.users".try_into().unwrap();
        assert_eq!(with_schema.to_string(), "\"public\".\"users\"");
        assert_eq!(with_schema.unescaped(), "users");
        assert_eq!(with_schema.schema().unwrap().unescaped(), "public");

        // Fully qualified using TryFrom (parses dot-separated format)
        let full: TableName = "mydb.public.users".try_into().unwrap();
        assert_eq!(full.to_string(), "\"mydb\".\"public\".\"users\"");
        assert_eq!(full.unescaped(), "users");
        assert_eq!(full.schema().unwrap().unescaped(), "public");
        assert_eq!(full.database().unwrap().unescaped(), "mydb");

        // From String
        let table_string: TableName = String::from("users").try_into().unwrap();
        assert_eq!(table_string.to_string(), "\"users\"");

        // Invalid format returns error
        let invalid: std::result::Result<TableName, _> = "db.schema.table.extra".try_into();
        assert!(invalid.is_err());
    }
}