@@ -32,6 +32,24 @@ class ColumnComments:
3232 after : str = ""
3333
3434
35+ @dataclass (frozen = True )
36+ class UniqueColumn :
37+ name : str
38+ collation : str = ""
39+ order : str = ""
40+
41+
42+ @dataclass
43+ class Unique :
44+ columns : tuple [UniqueColumn , ...]
45+ name : str = ""
46+ column : str = ""
47+ conflict : str = ""
48+ sql : str = field (default = "" , compare = False , repr = False )
49+ start : int = field (default = - 1 , compare = False , repr = False )
50+ end : int = field (default = - 1 , compare = False , repr = False )
51+
52+
3553class ParseError (ValueError ):
3654 pass
3755
@@ -592,6 +610,181 @@ def parse_autoincrement(create_sql: str) -> str | None:
592610 return None
593611
594612
613+ _CONFLICT_ACTIONS = frozenset (("ROLLBACK" , "ABORT" , "FAIL" , "IGNORE" , "REPLACE" ))
614+
615+
616+ def _conflict_after (tokens : list [_Token ], index : int ) -> tuple [str , int ]:
617+ if index >= len (tokens ) or not tokens [index ].is_keyword ("ON" ):
618+ return "" , index
619+ if index + 2 >= len (tokens ) or not tokens [index + 1 ].is_keyword ("CONFLICT" ):
620+ raise ParseError ("ON after UNIQUE must be followed by CONFLICT and an action" )
621+ action = tokens [index + 2 ].text .upper ()
622+ if tokens [index + 2 ].kind != "word" or action not in _CONFLICT_ACTIONS :
623+ raise ParseError ("Invalid UNIQUE ON CONFLICT action" )
624+ return action , index + 3
625+
626+
627+ def _unique_columns (
628+ item : str , tokens : list [_Token ], open_index : int
629+ ) -> tuple [tuple [UniqueColumn , ...], int ]:
630+ close = _matching_paren (tokens , open_index )
631+ inner = item [tokens [open_index ].end : tokens [close ].start ]
632+ columns : list [UniqueColumn ] = []
633+ for raw_column in _split_ranges (inner , _lex (inner )):
634+ column_tokens = _meaningful (_lex (raw_column ))
635+ if not column_tokens or column_tokens [0 ].kind not in (
636+ "word" ,
637+ "identifier" ,
638+ "string" ,
639+ ):
640+ raise ParseError ("UNIQUE constraint has an invalid column" )
641+ name = _unquote (column_tokens [0 ].text )
642+ collation = ""
643+ order = ""
644+ index = 1
645+ if index < len (column_tokens ) and column_tokens [index ].is_keyword ("COLLATE" ):
646+ if index + 1 >= len (column_tokens ):
647+ raise ParseError ("COLLATE in UNIQUE constraint is missing its name" )
648+ collation = _unquote (column_tokens [index + 1 ].text )
649+ index += 2
650+ if index < len (column_tokens ) and (
651+ column_tokens [index ].is_keyword ("ASC" )
652+ or column_tokens [index ].is_keyword ("DESC" )
653+ ):
654+ order = column_tokens [index ].text .upper ()
655+ index += 1
656+ if index != len (column_tokens ):
657+ raise ParseError ("UNIQUE constraint has an invalid indexed column" )
658+ columns .append (UniqueColumn (name , collation = collation , order = order ))
659+ if not columns :
660+ raise ParseError ("UNIQUE constraint must include at least one column" )
661+ return tuple (columns ), close + 1
662+
663+
664+ def _column_uniques (
665+ item : str , tokens : list [_Token ], column : str , base_offset : int
666+ ) -> list [Unique ]:
667+ uniques : list [Unique ] = []
668+ collation = ""
669+ collation_index = 1
670+ while collation_index < len (tokens ):
671+ token = tokens [collation_index ]
672+ if token .text == "(" :
673+ collation_index = _matching_paren (tokens , collation_index ) + 1
674+ continue
675+ if token .is_keyword ("COLLATE" ):
676+ if collation_index + 1 >= len (tokens ):
677+ raise ParseError ("COLLATE is missing its name" )
678+ collation = _unquote (tokens [collation_index + 1 ].text )
679+ collation_index += 2
680+ continue
681+ collation_index += 1
682+ pending_name = ""
683+ pending_start : int | None = None
684+ index = 1
685+ while index < len (tokens ):
686+ token = tokens [index ]
687+ if token .text == "(" :
688+ index = _matching_paren (tokens , index ) + 1
689+ continue
690+ if token .is_keyword ("CONSTRAINT" ):
691+ if index + 1 >= len (tokens ):
692+ raise ParseError ("CONSTRAINT is missing its name" )
693+ pending_name = _unquote (tokens [index + 1 ].text )
694+ pending_start = index
695+ index += 2
696+ continue
697+ if token .is_keyword ("UNIQUE" ):
698+ source_start = tokens [
699+ pending_start if pending_start is not None else index
700+ ].start
701+ conflict , next_index = _conflict_after (tokens , index + 1 )
702+ source_end = tokens [next_index - 1 ].end
703+ uniques .append (
704+ Unique (
705+ (UniqueColumn (column , collation = collation ),),
706+ name = pending_name ,
707+ column = column ,
708+ conflict = conflict ,
709+ sql = item [source_start :source_end ],
710+ start = base_offset + source_start ,
711+ end = base_offset + source_end ,
712+ )
713+ )
714+ pending_name = ""
715+ pending_start = None
716+ index = next_index
717+ continue
718+ if (
719+ token .kind == "word"
720+ and token .text .upper () in _OTHER_COLUMN_CONSTRAINT_KEYWORDS
721+ ):
722+ pending_name = ""
723+ pending_start = None
724+ index += 1
725+ return uniques
726+
727+
728+ def parse_uniques (create_sql : str ) -> list [Unique ]:
729+ """Return column-level and table-level UNIQUE constraints."""
730+ body_info = _table_body (create_sql )
731+ if body_info is None :
732+ return []
733+ body , body_start = body_info
734+ uniques : list [Unique ] = []
735+ for item , item_start , _ in _split_spans (body , _lex (body )):
736+ item_tokens = _meaningful (_lex (item ))
737+ if not item_tokens :
738+ continue
739+ item_index = 0
740+ constraint_name = ""
741+ if item_tokens [item_index ].is_keyword ("CONSTRAINT" ):
742+ if len (item_tokens ) < 2 :
743+ raise ParseError ("CONSTRAINT is missing its name" )
744+ constraint_name = _unquote (item_tokens [1 ].text )
745+ item_index = 2
746+ head = item_tokens [item_index ] if item_index < len (item_tokens ) else None
747+ if head and head .is_keyword ("UNIQUE" ):
748+ if (
749+ item_index + 1 >= len (item_tokens )
750+ or item_tokens [item_index + 1 ].text != "("
751+ ):
752+ raise ParseError ("Table UNIQUE must be followed by a column list" )
753+ columns , next_index = _unique_columns (item , item_tokens , item_index + 1 )
754+ conflict , next_index = _conflict_after (item_tokens , next_index )
755+ if next_index != len (item_tokens ):
756+ raise ParseError ("Unexpected SQL after UNIQUE constraint" )
757+ source_start = item_tokens [0 ].start
758+ source_end = item_tokens [next_index - 1 ].end
759+ uniques .append (
760+ Unique (
761+ columns ,
762+ name = constraint_name ,
763+ conflict = conflict ,
764+ sql = item [source_start :source_end ],
765+ start = body_start + item_start + source_start ,
766+ end = body_start + item_start + source_end ,
767+ )
768+ )
769+ continue
770+ if (
771+ head
772+ and head .kind == "word"
773+ and head .text .upper () in _TABLE_CONSTRAINT_KEYWORDS
774+ ):
775+ continue
776+ column = _unquote (item_tokens [0 ].text )
777+ uniques .extend (
778+ _column_uniques (
779+ item ,
780+ item_tokens ,
781+ column ,
782+ body_start + item_start ,
783+ )
784+ )
785+ return uniques
786+
787+
595788def parse_column_comments (create_sql : str ) -> dict [str , ColumnComments ]:
596789 """Return comments immediately before and after each column definition."""
597790 body_info = _table_body (create_sql )
0 commit comments